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. 4 5 // Audio rendering unit utilizing an AudioRendererSink to output data. 6 // 7 // This class lives inside three threads during it's lifetime, namely: 8 // 1. Render thread 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. 14 // 15 // AudioRendererImpl talks to an AudioRendererAlgorithm that takes care of 16 // queueing audio data and stretching/shrinking audio data when playback rate != 17 // 1.0 or 0.0. 18 19 #ifndef MEDIA_FILTERS_AUDIO_RENDERER_IMPL_H_ 20 #define MEDIA_FILTERS_AUDIO_RENDERER_IMPL_H_ 21 22 #include <deque> 23 24 #include "base/gtest_prod_util.h" 25 #include "base/memory/weak_ptr.h" 26 #include "base/synchronization/lock.h" 27 #include "media/base/audio_decoder.h" 28 #include "media/base/audio_renderer.h" 29 #include "media/base/audio_renderer_sink.h" 30 #include "media/base/decryptor.h" 31 #include "media/filters/audio_renderer_algorithm.h" 32 33 namespace base { 34 class MessageLoopProxy; 35 } 36 37 namespace media { 38 39 class AudioBus; 40 class AudioDecoderSelector; 41 class AudioSplicer; 42 class DecryptingDemuxerStream; 43 44 class MEDIA_EXPORT AudioRendererImpl 45 : public AudioRenderer, 46 NON_EXPORTED_BASE(public AudioRendererSink::RenderCallback) { 47 public: 48 // |message_loop| is the thread on which AudioRendererImpl will execute. 49 // 50 // |sink| is used as the destination for the rendered audio. 51 // 52 // |decoders| contains the AudioDecoders to use when initializing. 53 // 54 // |set_decryptor_ready_cb| is fired when the audio decryptor is available 55 // (only applicable if the stream is encrypted and we have a decryptor). 56 // 57 // |increase_preroll_on_underflow| Set to true if the preroll duration 58 // should be increased when ResumeAfterUnderflow() is called. 59 AudioRendererImpl(const scoped_refptr<base::MessageLoopProxy>& message_loop, 60 AudioRendererSink* sink, 61 ScopedVector<AudioDecoder> decoders, 62 const SetDecryptorReadyCB& set_decryptor_ready_cb, 63 bool increase_preroll_on_underflow); 64 virtual ~AudioRendererImpl(); 65 66 // AudioRenderer implementation. 67 virtual void Initialize(DemuxerStream* stream, 68 const PipelineStatusCB& init_cb, 69 const StatisticsCB& statistics_cb, 70 const base::Closure& underflow_cb, 71 const TimeCB& time_cb, 72 const base::Closure& ended_cb, 73 const base::Closure& disabled_cb, 74 const PipelineStatusCB& error_cb) OVERRIDE; 75 virtual void Play(const base::Closure& callback) OVERRIDE; 76 virtual void Pause(const base::Closure& callback) OVERRIDE; 77 virtual void Flush(const base::Closure& callback) OVERRIDE; 78 virtual void Stop(const base::Closure& callback) OVERRIDE; 79 virtual void SetPlaybackRate(float rate) OVERRIDE; 80 virtual void Preroll(base::TimeDelta time, 81 const PipelineStatusCB& cb) OVERRIDE; 82 virtual void ResumeAfterUnderflow() OVERRIDE; 83 virtual void SetVolume(float volume) OVERRIDE; 84 85 // Disables underflow support. When used, |state_| will never transition to 86 // kUnderflow resulting in Render calls that underflow returning 0 frames 87 // instead of some number of silence frames. Must be called prior to 88 // Initialize(). 89 void DisableUnderflowForTesting(); 90 91 // Allows injection of a custom time callback for non-realtime testing. 92 typedef base::Callback<base::TimeTicks()> NowCB; 93 void set_now_cb_for_testing(const NowCB& now_cb) { 94 now_cb_ = now_cb; 95 } 96 97 private: 98 friend class AudioRendererImplTest; 99 100 // Callback from the audio decoder delivering decoded audio samples. 101 void DecodedAudioReady(AudioDecoder::Status status, 102 const scoped_refptr<AudioBuffer>& buffer); 103 104 // Handles buffers that come out of |splicer_|. 105 // Returns true if more buffers are needed. 106 bool HandleSplicerBuffer(const scoped_refptr<AudioBuffer>& buffer); 107 108 // Helper functions for AudioDecoder::Status values passed to 109 // DecodedAudioReady(). 110 void HandleAbortedReadOrDecodeError(bool is_decode_error); 111 112 // Fills the given buffer with audio data by delegating to its |algorithm_|. 113 // FillBuffer() also takes care of updating the clock. Returns the number of 114 // frames copied into |dest|, which may be less than or equal to 115 // |requested_frames|. 116 // 117 // If this method returns fewer frames than |requested_frames|, it could 118 // be a sign that the pipeline is stalled or unable to stream the data fast 119 // enough. In such scenarios, the callee should zero out unused portions 120 // of their buffer to playback silence. 121 // 122 // FillBuffer() updates the pipeline's playback timestamp. If FillBuffer() is 123 // not called at the same rate as audio samples are played, then the reported 124 // timestamp in the pipeline will be ahead of the actual audio playback. In 125 // this case |playback_delay| should be used to indicate when in the future 126 // should the filled buffer be played. 127 // 128 // Safe to call on any thread. 129 uint32 FillBuffer(AudioBus* dest, 130 uint32 requested_frames, 131 int audio_delay_milliseconds); 132 133 // Estimate earliest time when current buffer can stop playing. 134 void UpdateEarliestEndTime_Locked(int frames_filled, 135 const base::TimeDelta& playback_delay, 136 const base::TimeTicks& time_now); 137 138 void DoPlay(); 139 void DoPause(); 140 141 // AudioRendererSink::RenderCallback implementation. 142 // 143 // NOTE: These are called on the audio callback thread! 144 virtual int Render(AudioBus* audio_bus, 145 int audio_delay_milliseconds) OVERRIDE; 146 virtual void OnRenderError() OVERRIDE; 147 148 // Helper methods that schedule an asynchronous read from the decoder as long 149 // as there isn't a pending read. 150 // 151 // Must be called on |message_loop_|. 152 void AttemptRead(); 153 void AttemptRead_Locked(); 154 bool CanRead_Locked(); 155 156 // Returns true if the data in the buffer is all before 157 // |preroll_timestamp_|. This can only return true while 158 // in the kPrerolling state. 159 bool IsBeforePrerollTime(const scoped_refptr<AudioBuffer>& buffer); 160 161 // Called when |decoder_selector_| has selected |decoder| or is null if no 162 // decoder could be selected. 163 // 164 // |decrypting_demuxer_stream| is non-null if a DecryptingDemuxerStream was 165 // created to help decrypt the encrypted stream. 166 void OnDecoderSelected( 167 scoped_ptr<AudioDecoder> decoder, 168 scoped_ptr<DecryptingDemuxerStream> decrypting_demuxer_stream); 169 170 void ResetDecoder(const base::Closure& callback); 171 172 scoped_refptr<base::MessageLoopProxy> message_loop_; 173 base::WeakPtrFactory<AudioRendererImpl> weak_factory_; 174 base::WeakPtr<AudioRendererImpl> weak_this_; 175 176 scoped_ptr<AudioSplicer> splicer_; 177 178 // The sink (destination) for rendered audio. |sink_| must only be accessed 179 // on |message_loop_|. |sink_| must never be called under |lock_| or else we 180 // may deadlock between |message_loop_| and the audio callback thread. 181 scoped_refptr<media::AudioRendererSink> sink_; 182 183 scoped_ptr<AudioDecoderSelector> decoder_selector_; 184 185 // These two will be set by AudioDecoderSelector::SelectAudioDecoder(). 186 scoped_ptr<AudioDecoder> decoder_; 187 scoped_ptr<DecryptingDemuxerStream> decrypting_demuxer_stream_; 188 189 // AudioParameters constructed during Initialize() based on |decoder_|. 190 AudioParameters audio_parameters_; 191 192 // Callbacks provided during Initialize(). 193 PipelineStatusCB init_cb_; 194 StatisticsCB statistics_cb_; 195 base::Closure underflow_cb_; 196 TimeCB time_cb_; 197 base::Closure ended_cb_; 198 base::Closure disabled_cb_; 199 PipelineStatusCB error_cb_; 200 201 // Callback provided to Pause(). 202 base::Closure pause_cb_; 203 204 // Callback provided to Preroll(). 205 PipelineStatusCB preroll_cb_; 206 207 // Typically calls base::TimeTicks::Now() but can be overridden by a test. 208 NowCB now_cb_; 209 210 // After Initialize() has completed, all variables below must be accessed 211 // under |lock_|. ------------------------------------------------------------ 212 base::Lock lock_; 213 214 // Algorithm for scaling audio. 215 scoped_ptr<AudioRendererAlgorithm> algorithm_; 216 217 // Simple state tracking variable. 218 enum State { 219 kUninitialized, 220 kPaused, 221 kPrerolling, 222 kPlaying, 223 kStopped, 224 kUnderflow, 225 kRebuffering, 226 }; 227 State state_; 228 229 // Keep track of whether or not the sink is playing. 230 bool sink_playing_; 231 232 // Keep track of our outstanding read to |decoder_|. 233 bool pending_read_; 234 235 // Keeps track of whether we received and rendered the end of stream buffer. 236 bool received_end_of_stream_; 237 bool rendered_end_of_stream_; 238 239 // The timestamp of the last frame (i.e. furthest in the future) buffered as 240 // well as the current time that takes current playback delay into account. 241 base::TimeDelta audio_time_buffered_; 242 base::TimeDelta current_time_; 243 244 base::TimeDelta preroll_timestamp_; 245 246 // We're supposed to know amount of audio data OS or hardware buffered, but 247 // that is not always so -- on my Linux box 248 // AudioBuffersState::hardware_delay_bytes never reaches 0. 249 // 250 // As a result we cannot use it to find when stream ends. If we just ignore 251 // buffered data we will notify host that stream ended before it is actually 252 // did so, I've seen it done ~140ms too early when playing ~150ms file. 253 // 254 // Instead of trying to invent OS-specific solution for each and every OS we 255 // are supporting, use simple workaround: every time we fill the buffer we 256 // remember when it should stop playing, and do not assume that buffer is 257 // empty till that time. Workaround is not bulletproof, as we don't exactly 258 // know when that particular data would start playing, but it is much better 259 // than nothing. 260 base::TimeTicks earliest_end_time_; 261 size_t total_frames_filled_; 262 263 bool underflow_disabled_; 264 bool increase_preroll_on_underflow_; 265 266 // True if the renderer receives a buffer with kAborted status during preroll, 267 // false otherwise. This flag is cleared on the next Preroll() call. 268 bool preroll_aborted_; 269 270 // End variables which must be accessed under |lock_|. ---------------------- 271 272 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); 273 }; 274 275 } // namespace media 276 277 #endif // MEDIA_FILTERS_AUDIO_RENDERER_IMPL_H_ 278