1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Audio rendering unit utilizing an AudioRendererSink to output data.
7 // This class lives inside three threads during it's lifetime, namely:
9 // Where the object is created.
10 // 2. Media thread (provided via constructor)
11 // All AudioDecoder methods are called on this thread.
12 // 3. Audio thread created by the AudioRendererSink.
13 // Render() is called here where audio data is decoded into raw PCM data.
15 // AudioRendererImpl talks to an AudioRendererAlgorithm that takes care of
16 // queueing audio data and stretching/shrinking audio data when playback rate !=
19 #ifndef MEDIA_FILTERS_AUDIO_RENDERER_IMPL_H_
20 #define MEDIA_FILTERS_AUDIO_RENDERER_IMPL_H_
24 #include "base/gtest_prod_util.h"
25 #include "base/memory/scoped_ptr.h"
26 #include "base/memory/weak_ptr.h"
27 #include "base/synchronization/lock.h"
28 #include "media/base/audio_decoder.h"
29 #include "media/base/audio_renderer.h"
30 #include "media/base/audio_renderer_sink.h"
31 #include "media/base/decryptor.h"
32 #include "media/base/media_log.h"
33 #include "media/base/time_source.h"
34 #include "media/filters/audio_renderer_algorithm.h"
35 #include "media/filters/decoder_stream.h"
38 class SingleThreadTaskRunner
;
43 class AudioBufferConverter
;
46 class AudioHardwareConfig
;
48 class DecryptingDemuxerStream
;
50 class MEDIA_EXPORT AudioRendererImpl
51 : public AudioRenderer
,
53 NON_EXPORTED_BASE(public AudioRendererSink::RenderCallback
) {
55 // |task_runner| is the thread on which AudioRendererImpl will execute.
57 // |sink| is used as the destination for the rendered audio.
59 // |decoders| contains the AudioDecoders to use when initializing.
61 // |set_decryptor_ready_cb| is fired when the audio decryptor is available
62 // (only applicable if the stream is encrypted and we have a decryptor).
64 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
65 AudioRendererSink
* sink
,
66 ScopedVector
<AudioDecoder
> decoders
,
67 const SetDecryptorReadyCB
& set_decryptor_ready_cb
,
68 const AudioHardwareConfig
& hardware_params
,
69 const scoped_refptr
<MediaLog
>& media_log
);
70 ~AudioRendererImpl() override
;
72 // TimeSource implementation.
73 void StartTicking() override
;
74 void StopTicking() override
;
75 void SetPlaybackRate(float rate
) override
;
76 void SetMediaTime(base::TimeDelta time
) override
;
77 base::TimeDelta
CurrentMediaTime() override
;
78 base::TimeDelta
CurrentMediaTimeForSyncingVideo() override
;
80 // AudioRenderer implementation.
81 void Initialize(DemuxerStream
* stream
,
82 const PipelineStatusCB
& init_cb
,
83 const StatisticsCB
& statistics_cb
,
84 const BufferingStateCB
& buffering_state_cb
,
85 const base::Closure
& ended_cb
,
86 const PipelineStatusCB
& error_cb
) override
;
87 TimeSource
* GetTimeSource() override
;
88 void Flush(const base::Closure
& callback
) override
;
89 void StartPlaying() override
;
90 void SetVolume(float volume
) override
;
93 friend class AudioRendererImplTest
;
95 // Important detail: being in kPlaying doesn't imply that audio is being
96 // rendered. Rather, it means that the renderer is ready to go. The actual
97 // rendering of audio is controlled via Start/StopRendering().
104 // | Decoders initialized
107 // kFlushed <------------------ kFlushing
108 // | StartPlaying() ^
111 // `---------> kPlaying --------'
120 // Callback from the audio decoder delivering decoded audio samples.
121 void DecodedAudioReady(AudioBufferStream::Status status
,
122 const scoped_refptr
<AudioBuffer
>& buffer
);
124 // Handles buffers that come out of |splicer_|.
125 // Returns true if more buffers are needed.
126 bool HandleSplicerBuffer_Locked(const scoped_refptr
<AudioBuffer
>& buffer
);
128 // Helper functions for AudioDecoder::Status values passed to
129 // DecodedAudioReady().
130 void HandleAbortedReadOrDecodeError(bool is_decode_error
);
132 void StartRendering_Locked();
133 void StopRendering_Locked();
135 // AudioRendererSink::RenderCallback implementation.
137 // NOTE: These are called on the audio callback thread!
139 // Render() fills the given buffer with audio data by delegating to its
140 // |algorithm_|. Render() also takes care of updating the clock.
141 // Returns the number of frames copied into |audio_bus|, which may be less
142 // than or equal to the initial number of frames in |audio_bus|
144 // If this method returns fewer frames than the initial number of frames in
145 // |audio_bus|, it could be a sign that the pipeline is stalled or unable to
146 // stream the data fast enough. In such scenarios, the callee should zero out
147 // unused portions of their buffer to play back silence.
149 // Render() updates the pipeline's playback timestamp. If Render() is
150 // not called at the same rate as audio samples are played, then the reported
151 // timestamp in the pipeline will be ahead of the actual audio playback. In
152 // this case |audio_delay_milliseconds| should be used to indicate when in the
153 // future should the filled buffer be played.
154 int Render(AudioBus
* audio_bus
, int audio_delay_milliseconds
) override
;
155 void OnRenderError() override
;
157 // Helper methods that schedule an asynchronous read from the decoder as long
158 // as there isn't a pending read.
160 // Must be called on |task_runner_|.
162 void AttemptRead_Locked();
163 bool CanRead_Locked();
164 void ChangeState_Locked(State new_state
);
166 // Returns true if the data in the buffer is all before |start_timestamp_|.
167 // This can only return true while in the kPlaying state.
168 bool IsBeforeStartTime(const scoped_refptr
<AudioBuffer
>& buffer
);
170 // Called upon AudioBufferStream initialization, or failure thereof (indicated
171 // by the value of |success|).
172 void OnAudioBufferStreamInitialized(bool succes
);
174 // Used to initiate the flush operation once all pending reads have
176 void DoFlush_Locked();
178 // Calls |decoder_|.Reset() and arranges for ResetDecoderDone() to get
179 // called when the reset completes.
182 // Called when the |decoder_|.Reset() has completed.
183 void ResetDecoderDone();
185 // Called by the AudioBufferStream when a splice buffer is demuxed.
186 void OnNewSpliceBuffer(base::TimeDelta
);
188 // Called by the AudioBufferStream when a config change occurs.
189 void OnConfigChange();
191 // Updates |buffering_state_| and fires |buffering_state_cb_|.
192 void SetBufferingState_Locked(BufferingState buffering_state
);
194 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
196 scoped_ptr
<AudioSplicer
> splicer_
;
197 scoped_ptr
<AudioBufferConverter
> buffer_converter_
;
199 // Whether or not we expect to handle config changes.
200 bool expecting_config_changes_
;
202 // The sink (destination) for rendered audio. |sink_| must only be accessed
203 // on |task_runner_|. |sink_| must never be called under |lock_| or else we
204 // may deadlock between |task_runner_| and the audio callback thread.
205 scoped_refptr
<media::AudioRendererSink
> sink_
;
207 scoped_ptr
<AudioBufferStream
> audio_buffer_stream_
;
209 // Interface to the hardware audio params.
210 const AudioHardwareConfig
& hardware_config_
;
212 // Cached copy of hardware params from |hardware_config_|.
213 AudioParameters audio_parameters_
;
215 // Callbacks provided during Initialize().
216 PipelineStatusCB init_cb_
;
217 BufferingStateCB buffering_state_cb_
;
218 base::Closure ended_cb_
;
219 PipelineStatusCB error_cb_
;
221 // Callback provided to Flush().
222 base::Closure flush_cb_
;
224 // After Initialize() has completed, all variables below must be accessed
225 // under |lock_|. ------------------------------------------------------------
228 // Algorithm for scaling audio.
229 float playback_rate_
;
230 scoped_ptr
<AudioRendererAlgorithm
> algorithm_
;
232 // Simple state tracking variable.
235 BufferingState buffering_state_
;
237 // Keep track of whether or not the sink is playing and whether we should be
242 // Keep track of our outstanding read to |decoder_|.
245 // Keeps track of whether we received and rendered the end of stream buffer.
246 bool received_end_of_stream_
;
247 bool rendered_end_of_stream_
;
249 scoped_ptr
<AudioClock
> audio_clock_
;
251 // The media timestamp to begin playback at after seeking. Set via
253 base::TimeDelta start_timestamp_
;
255 // The media timestamp to signal end of audio playback. Determined during
256 // Render() when writing the final frames of decoded audio data.
257 base::TimeDelta ended_timestamp_
;
259 // Set every Render() and used to provide an interpolated time value to
260 // CurrentMediaTimeForSyncingVideo().
261 base::TimeTicks last_render_ticks_
;
263 // End variables which must be accessed under |lock_|. ----------------------
265 // NOTE: Weak pointers must be invalidated before all other member variables.
266 base::WeakPtrFactory
<AudioRendererImpl
> weak_factory_
;
268 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl
);
273 #endif // MEDIA_FILTERS_AUDIO_RENDERER_IMPL_H_