OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Audio rendering unit utilizing an AudioRendererSink to output data. | 5 // Audio rendering unit utilizing an AudioRendererSink to output data. |
6 // | 6 // |
7 // This class lives inside three threads during it's lifetime, namely: | 7 // This class lives inside three threads during it's lifetime, namely: |
8 // 1. Render thread | 8 // 1. Render thread |
9 // Where the object is created. | 9 // Where the object is created. |
10 // 2. Media thread (provided via constructor) | 10 // 2. Media thread (provided via constructor) |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 AudioRendererSink* sink, | 61 AudioRendererSink* sink, |
62 ScopedVector<AudioDecoder> decoders, | 62 ScopedVector<AudioDecoder> decoders, |
63 const SetDecryptorReadyCB& set_decryptor_ready_cb, | 63 const SetDecryptorReadyCB& set_decryptor_ready_cb, |
64 AudioHardwareConfig* hardware_params); | 64 AudioHardwareConfig* hardware_params); |
65 virtual ~AudioRendererImpl(); | 65 virtual ~AudioRendererImpl(); |
66 | 66 |
67 // AudioRenderer implementation. | 67 // AudioRenderer implementation. |
68 virtual void Initialize(DemuxerStream* stream, | 68 virtual void Initialize(DemuxerStream* stream, |
69 const PipelineStatusCB& init_cb, | 69 const PipelineStatusCB& init_cb, |
70 const StatisticsCB& statistics_cb, | 70 const StatisticsCB& statistics_cb, |
| 71 const base::Closure& underflow_cb, |
71 const TimeCB& time_cb, | 72 const TimeCB& time_cb, |
72 const BufferingStateCB& buffering_state_cb, | |
73 const base::Closure& ended_cb, | 73 const base::Closure& ended_cb, |
74 const PipelineStatusCB& error_cb) OVERRIDE; | 74 const PipelineStatusCB& error_cb) OVERRIDE; |
75 virtual void StartRendering() OVERRIDE; | 75 virtual void StartRendering() OVERRIDE; |
76 virtual void StopRendering() OVERRIDE; | 76 virtual void StopRendering() OVERRIDE; |
77 virtual void Flush(const base::Closure& callback) OVERRIDE; | 77 virtual void Flush(const base::Closure& callback) OVERRIDE; |
78 virtual void Stop(const base::Closure& callback) OVERRIDE; | 78 virtual void Stop(const base::Closure& callback) OVERRIDE; |
79 virtual void SetPlaybackRate(float rate) OVERRIDE; | 79 virtual void SetPlaybackRate(float rate) OVERRIDE; |
80 virtual void StartPlayingFrom(base::TimeDelta timestamp) OVERRIDE; | 80 virtual void Preroll(base::TimeDelta time, |
| 81 const PipelineStatusCB& cb) OVERRIDE; |
| 82 virtual void ResumeAfterUnderflow() OVERRIDE; |
81 virtual void SetVolume(float volume) OVERRIDE; | 83 virtual void SetVolume(float volume) OVERRIDE; |
82 | 84 |
83 // Allows injection of a custom time callback for non-realtime testing. | 85 // Allows injection of a custom time callback for non-realtime testing. |
84 typedef base::Callback<base::TimeTicks()> NowCB; | 86 typedef base::Callback<base::TimeTicks()> NowCB; |
85 void set_now_cb_for_testing(const NowCB& now_cb) { | 87 void set_now_cb_for_testing(const NowCB& now_cb) { |
86 now_cb_ = now_cb; | 88 now_cb_ = now_cb; |
87 } | 89 } |
88 | 90 |
89 private: | 91 private: |
90 friend class AudioRendererImplTest; | 92 friend class AudioRendererImplTest; |
91 | 93 |
92 // Important detail: being in kPlaying doesn't imply that audio is being | 94 // Important detail: being in kPlaying doesn't imply that audio is being |
93 // rendered. Rather, it means that the renderer is ready to go. The actual | 95 // rendered. Rather, it means that the renderer is ready to go. The actual |
94 // rendering of audio is controlled via Start/StopRendering(). | 96 // rendering of audio is controlled via Start/StopRendering(). |
95 // | 97 // |
96 // kUninitialized | 98 // kUninitialized |
97 // | Initialize() | 99 // | Initialize() |
98 // | | 100 // | |
99 // V | 101 // V |
100 // kInitializing | 102 // kInitializing |
101 // | Decoders initialized | 103 // | Decoders initialized |
102 // | | 104 // | |
103 // V Decoders reset | 105 // V Decoders reset |
104 // kFlushed <------------------ kFlushing | 106 // kFlushed <------------------ kFlushing |
105 // | StartPlayingFrom() ^ | 107 // | Preroll() ^ |
106 // | | | 108 // | | |
107 // | | Flush() | 109 // V | Flush() |
108 // `---------> kPlaying --------' | 110 // kPrerolling ----------------> kPlaying ---------. |
| 111 // Enough data buffered ^ | Not enough data |
| 112 // | | buffered |
| 113 // Enough data buffered | V |
| 114 // kRebuffering <--- kUnderflow |
| 115 // ResumeAfterUnderflow() |
109 enum State { | 116 enum State { |
110 kUninitialized, | 117 kUninitialized, |
111 kInitializing, | 118 kInitializing, |
112 kFlushing, | 119 kFlushing, |
113 kFlushed, | 120 kFlushed, |
| 121 kPrerolling, |
114 kPlaying, | 122 kPlaying, |
115 kStopped, | 123 kStopped, |
| 124 kUnderflow, |
| 125 kRebuffering, |
116 }; | 126 }; |
117 | 127 |
118 // Callback from the audio decoder delivering decoded audio samples. | 128 // Callback from the audio decoder delivering decoded audio samples. |
119 void DecodedAudioReady(AudioBufferStream::Status status, | 129 void DecodedAudioReady(AudioBufferStream::Status status, |
120 const scoped_refptr<AudioBuffer>& buffer); | 130 const scoped_refptr<AudioBuffer>& buffer); |
121 | 131 |
122 // Handles buffers that come out of |splicer_|. | 132 // Handles buffers that come out of |splicer_|. |
123 // Returns true if more buffers are needed. | 133 // Returns true if more buffers are needed. |
124 bool HandleSplicerBuffer_Locked(const scoped_refptr<AudioBuffer>& buffer); | 134 bool HandleSplicerBuffer(const scoped_refptr<AudioBuffer>& buffer); |
125 | 135 |
126 // Helper functions for AudioDecoder::Status values passed to | 136 // Helper functions for AudioDecoder::Status values passed to |
127 // DecodedAudioReady(). | 137 // DecodedAudioReady(). |
128 void HandleAbortedReadOrDecodeError(bool is_decode_error); | 138 void HandleAbortedReadOrDecodeError(bool is_decode_error); |
129 | 139 |
130 // Estimate earliest time when current buffer can stop playing. | 140 // Estimate earliest time when current buffer can stop playing. |
131 void UpdateEarliestEndTime_Locked(int frames_filled, | 141 void UpdateEarliestEndTime_Locked(int frames_filled, |
132 const base::TimeDelta& playback_delay, | 142 const base::TimeDelta& playback_delay, |
133 const base::TimeTicks& time_now); | 143 const base::TimeTicks& time_now); |
134 | 144 |
(...skipping 25 matching lines...) Expand all Loading... |
160 | 170 |
161 // Helper methods that schedule an asynchronous read from the decoder as long | 171 // Helper methods that schedule an asynchronous read from the decoder as long |
162 // as there isn't a pending read. | 172 // as there isn't a pending read. |
163 // | 173 // |
164 // Must be called on |task_runner_|. | 174 // Must be called on |task_runner_|. |
165 void AttemptRead(); | 175 void AttemptRead(); |
166 void AttemptRead_Locked(); | 176 void AttemptRead_Locked(); |
167 bool CanRead_Locked(); | 177 bool CanRead_Locked(); |
168 void ChangeState_Locked(State new_state); | 178 void ChangeState_Locked(State new_state); |
169 | 179 |
170 // Returns true if the data in the buffer is all before |start_timestamp_|. | 180 // Returns true if the data in the buffer is all before |
171 // This can only return true while in the kPlaying state. | 181 // |preroll_timestamp_|. This can only return true while |
172 bool IsBeforeStartTime(const scoped_refptr<AudioBuffer>& buffer); | 182 // in the kPrerolling state. |
| 183 bool IsBeforePrerollTime(const scoped_refptr<AudioBuffer>& buffer); |
173 | 184 |
174 // Called upon AudioBufferStream initialization, or failure thereof (indicated | 185 // Called upon AudioBufferStream initialization, or failure thereof (indicated |
175 // by the value of |success|). | 186 // by the value of |success|). |
176 void OnAudioBufferStreamInitialized(bool succes); | 187 void OnAudioBufferStreamInitialized(bool succes); |
177 | 188 |
178 // Used to initiate the flush operation once all pending reads have | 189 // Used to initiate the flush operation once all pending reads have |
179 // completed. | 190 // completed. |
180 void DoFlush_Locked(); | 191 void DoFlush_Locked(); |
181 | 192 |
182 // Calls |decoder_|.Reset() and arranges for ResetDecoderDone() to get | 193 // Calls |decoder_|.Reset() and arranges for ResetDecoderDone() to get |
183 // called when the reset completes. | 194 // called when the reset completes. |
184 void ResetDecoder(); | 195 void ResetDecoder(); |
185 | 196 |
186 // Called when the |decoder_|.Reset() has completed. | 197 // Called when the |decoder_|.Reset() has completed. |
187 void ResetDecoderDone(); | 198 void ResetDecoderDone(); |
188 | 199 |
189 // Called by the AudioBufferStream when a splice buffer is demuxed. | 200 // Called by the AudioBufferStream when a splice buffer is demuxed. |
190 void OnNewSpliceBuffer(base::TimeDelta); | 201 void OnNewSpliceBuffer(base::TimeDelta); |
191 | 202 |
192 // Called by the AudioBufferStream when a config change occurs. | 203 // Called by the AudioBufferStream when a config change occurs. |
193 void OnConfigChange(); | 204 void OnConfigChange(); |
194 | 205 |
195 // Updates |buffering_state_| and fires |buffering_state_cb_|. | |
196 void SetBufferingState_Locked(BufferingState buffering_state); | |
197 | |
198 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 206 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
199 | 207 |
200 scoped_ptr<AudioSplicer> splicer_; | 208 scoped_ptr<AudioSplicer> splicer_; |
201 scoped_ptr<AudioBufferConverter> buffer_converter_; | 209 scoped_ptr<AudioBufferConverter> buffer_converter_; |
202 | 210 |
203 // Whether or not we expect to handle config changes. | 211 // Whether or not we expect to handle config changes. |
204 bool expecting_config_changes_; | 212 bool expecting_config_changes_; |
205 | 213 |
206 // The sink (destination) for rendered audio. |sink_| must only be accessed | 214 // The sink (destination) for rendered audio. |sink_| must only be accessed |
207 // on |task_runner_|. |sink_| must never be called under |lock_| or else we | 215 // on |task_runner_|. |sink_| must never be called under |lock_| or else we |
208 // may deadlock between |task_runner_| and the audio callback thread. | 216 // may deadlock between |task_runner_| and the audio callback thread. |
209 scoped_refptr<media::AudioRendererSink> sink_; | 217 scoped_refptr<media::AudioRendererSink> sink_; |
210 | 218 |
211 AudioBufferStream audio_buffer_stream_; | 219 AudioBufferStream audio_buffer_stream_; |
212 | 220 |
213 // Interface to the hardware audio params. | 221 // Interface to the hardware audio params. |
214 const AudioHardwareConfig* const hardware_config_; | 222 const AudioHardwareConfig* const hardware_config_; |
215 | 223 |
216 // Cached copy of hardware params from |hardware_config_|. | 224 // Cached copy of hardware params from |hardware_config_|. |
217 AudioParameters audio_parameters_; | 225 AudioParameters audio_parameters_; |
218 | 226 |
219 // Callbacks provided during Initialize(). | 227 // Callbacks provided during Initialize(). |
220 PipelineStatusCB init_cb_; | 228 PipelineStatusCB init_cb_; |
| 229 base::Closure underflow_cb_; |
221 TimeCB time_cb_; | 230 TimeCB time_cb_; |
222 BufferingStateCB buffering_state_cb_; | |
223 base::Closure ended_cb_; | 231 base::Closure ended_cb_; |
224 PipelineStatusCB error_cb_; | 232 PipelineStatusCB error_cb_; |
225 | 233 |
226 // Callback provided to Flush(). | 234 // Callback provided to Flush(). |
227 base::Closure flush_cb_; | 235 base::Closure flush_cb_; |
228 | 236 |
| 237 // Callback provided to Preroll(). |
| 238 PipelineStatusCB preroll_cb_; |
| 239 |
229 // Typically calls base::TimeTicks::Now() but can be overridden by a test. | 240 // Typically calls base::TimeTicks::Now() but can be overridden by a test. |
230 NowCB now_cb_; | 241 NowCB now_cb_; |
231 | 242 |
232 // After Initialize() has completed, all variables below must be accessed | 243 // After Initialize() has completed, all variables below must be accessed |
233 // under |lock_|. ------------------------------------------------------------ | 244 // under |lock_|. ------------------------------------------------------------ |
234 base::Lock lock_; | 245 base::Lock lock_; |
235 | 246 |
236 // Algorithm for scaling audio. | 247 // Algorithm for scaling audio. |
237 scoped_ptr<AudioRendererAlgorithm> algorithm_; | 248 scoped_ptr<AudioRendererAlgorithm> algorithm_; |
238 | 249 |
239 // Simple state tracking variable. | 250 // Simple state tracking variable. |
240 State state_; | 251 State state_; |
241 | 252 |
242 BufferingState buffering_state_; | |
243 | |
244 // Keep track of whether or not the sink is playing and whether we should be | 253 // Keep track of whether or not the sink is playing and whether we should be |
245 // rendering. | 254 // rendering. |
246 bool rendering_; | 255 bool rendering_; |
247 bool sink_playing_; | 256 bool sink_playing_; |
248 | 257 |
249 // Keep track of our outstanding read to |decoder_|. | 258 // Keep track of our outstanding read to |decoder_|. |
250 bool pending_read_; | 259 bool pending_read_; |
251 | 260 |
252 // Keeps track of whether we received and rendered the end of stream buffer. | 261 // Keeps track of whether we received and rendered the end of stream buffer. |
253 bool received_end_of_stream_; | 262 bool received_end_of_stream_; |
254 bool rendered_end_of_stream_; | 263 bool rendered_end_of_stream_; |
255 | 264 |
256 scoped_ptr<AudioClock> audio_clock_; | 265 scoped_ptr<AudioClock> audio_clock_; |
257 | 266 |
258 base::TimeDelta start_timestamp_; | 267 base::TimeDelta preroll_timestamp_; |
259 | 268 |
260 // We're supposed to know amount of audio data OS or hardware buffered, but | 269 // We're supposed to know amount of audio data OS or hardware buffered, but |
261 // that is not always so -- on my Linux box | 270 // that is not always so -- on my Linux box |
262 // AudioBuffersState::hardware_delay_bytes never reaches 0. | 271 // AudioBuffersState::hardware_delay_bytes never reaches 0. |
263 // | 272 // |
264 // As a result we cannot use it to find when stream ends. If we just ignore | 273 // As a result we cannot use it to find when stream ends. If we just ignore |
265 // buffered data we will notify host that stream ended before it is actually | 274 // buffered data we will notify host that stream ended before it is actually |
266 // did so, I've seen it done ~140ms too early when playing ~150ms file. | 275 // did so, I've seen it done ~140ms too early when playing ~150ms file. |
267 // | 276 // |
268 // Instead of trying to invent OS-specific solution for each and every OS we | 277 // Instead of trying to invent OS-specific solution for each and every OS we |
269 // are supporting, use simple workaround: every time we fill the buffer we | 278 // are supporting, use simple workaround: every time we fill the buffer we |
270 // remember when it should stop playing, and do not assume that buffer is | 279 // remember when it should stop playing, and do not assume that buffer is |
271 // empty till that time. Workaround is not bulletproof, as we don't exactly | 280 // empty till that time. Workaround is not bulletproof, as we don't exactly |
272 // know when that particular data would start playing, but it is much better | 281 // know when that particular data would start playing, but it is much better |
273 // than nothing. | 282 // than nothing. |
274 base::TimeTicks earliest_end_time_; | 283 base::TimeTicks earliest_end_time_; |
275 size_t total_frames_filled_; | 284 size_t total_frames_filled_; |
276 | 285 |
| 286 // True if the renderer receives a buffer with kAborted status during preroll, |
| 287 // false otherwise. This flag is cleared on the next Preroll() call. |
| 288 bool preroll_aborted_; |
| 289 |
277 // End variables which must be accessed under |lock_|. ---------------------- | 290 // End variables which must be accessed under |lock_|. ---------------------- |
278 | 291 |
279 // NOTE: Weak pointers must be invalidated before all other member variables. | 292 // NOTE: Weak pointers must be invalidated before all other member variables. |
280 base::WeakPtrFactory<AudioRendererImpl> weak_factory_; | 293 base::WeakPtrFactory<AudioRendererImpl> weak_factory_; |
281 | 294 |
282 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); | 295 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); |
283 }; | 296 }; |
284 | 297 |
285 } // namespace media | 298 } // namespace media |
286 | 299 |
287 #endif // MEDIA_FILTERS_AUDIO_RENDERER_IMPL_H_ | 300 #endif // MEDIA_FILTERS_AUDIO_RENDERER_IMPL_H_ |
OLD | NEW |