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 #include "media/filters/audio_renderer_impl.h" | 5 #include "media/filters/audio_renderer_impl.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
140 } | 140 } |
141 | 141 |
142 void AudioRendererImpl::DoFlush_Locked() { | 142 void AudioRendererImpl::DoFlush_Locked() { |
143 DCHECK(task_runner_->BelongsToCurrentThread()); | 143 DCHECK(task_runner_->BelongsToCurrentThread()); |
144 lock_.AssertAcquired(); | 144 lock_.AssertAcquired(); |
145 | 145 |
146 DCHECK(!pending_read_); | 146 DCHECK(!pending_read_); |
147 DCHECK_EQ(state_, kPaused); | 147 DCHECK_EQ(state_, kPaused); |
148 | 148 |
149 if (decrypting_demuxer_stream_) { | 149 if (decrypting_demuxer_stream_) { |
150 decrypting_demuxer_stream_->Reset(BindToCurrentLoop( | 150 decrypting_demuxer_stream_->Reset( |
151 base::Bind(&AudioRendererImpl::ResetDecoder, weak_this_))); | 151 base::Bind(&AudioRendererImpl::ResetDecoder, weak_this_)); |
152 return; | 152 return; |
153 } | 153 } |
154 | 154 |
155 ResetDecoder(); | 155 ResetDecoder(); |
156 } | 156 } |
157 | 157 |
158 void AudioRendererImpl::ResetDecoder() { | 158 void AudioRendererImpl::ResetDecoder() { |
159 DCHECK(task_runner_->BelongsToCurrentThread()); | 159 DCHECK(task_runner_->BelongsToCurrentThread()); |
160 decoder_->Reset(BindToCurrentLoop( | 160 decoder_->Reset(base::Bind(&AudioRendererImpl::ResetDecoderDone, weak_this_)); |
161 base::Bind(&AudioRendererImpl::ResetDecoderDone, weak_this_))); | |
162 } | 161 } |
163 | 162 |
164 void AudioRendererImpl::ResetDecoderDone() { | 163 void AudioRendererImpl::ResetDecoderDone() { |
165 base::AutoLock auto_lock(lock_); | 164 DCHECK(task_runner_->BelongsToCurrentThread()); |
166 if (state_ == kStopped) | 165 { |
167 return; | 166 base::AutoLock auto_lock(lock_); |
167 if (state_ == kStopped) | |
168 return; | |
168 | 169 |
169 DCHECK_EQ(state_, kPaused); | 170 DCHECK_EQ(state_, kPaused); |
170 DCHECK(!flush_cb_.is_null()); | 171 DCHECK(!flush_cb_.is_null()); |
171 | 172 |
172 audio_time_buffered_ = kNoTimestamp(); | 173 audio_time_buffered_ = kNoTimestamp(); |
173 current_time_ = kNoTimestamp(); | 174 current_time_ = kNoTimestamp(); |
174 received_end_of_stream_ = false; | 175 received_end_of_stream_ = false; |
175 rendered_end_of_stream_ = false; | 176 rendered_end_of_stream_ = false; |
176 preroll_aborted_ = false; | 177 preroll_aborted_ = false; |
177 | 178 |
178 earliest_end_time_ = now_cb_.Run(); | 179 earliest_end_time_ = now_cb_.Run(); |
179 splicer_->Reset(); | 180 splicer_->Reset(); |
180 algorithm_->FlushBuffers(); | 181 algorithm_->FlushBuffers(); |
181 | 182 } |
182 base::ResetAndReturn(&flush_cb_).Run(); | 183 base::ResetAndReturn(&flush_cb_).Run(); |
183 } | 184 } |
184 | 185 |
185 void AudioRendererImpl::Stop(const base::Closure& callback) { | 186 void AudioRendererImpl::Stop(const base::Closure& callback) { |
186 DCHECK(task_runner_->BelongsToCurrentThread()); | 187 DCHECK(task_runner_->BelongsToCurrentThread()); |
187 DCHECK(!callback.is_null()); | 188 DCHECK(!callback.is_null()); |
189 DCHECK(stop_cb_.is_null()); | |
190 | |
191 stop_cb_ = callback; | |
188 | 192 |
189 // TODO(scherkus): Consider invalidating |weak_factory_| and replacing | 193 // TODO(scherkus): Consider invalidating |weak_factory_| and replacing |
190 // task-running guards that check |state_| with DCHECK(). | 194 // task-running guards that check |state_| with DCHECK(). |
191 | 195 |
196 { | |
197 base::AutoLock auto_lock(lock_); | |
198 if (state_ == kInitializing) { | |
199 decoder_selector_->Abort(); | |
200 return; | |
201 } | |
202 | |
203 if (state_ == kStopped) { | |
204 task_runner_->PostTask(FROM_HERE, base::ResetAndReturn(&stop_cb_)); | |
205 return; | |
206 } | |
207 | |
208 ChangeState_Locked(kStopped); | |
209 algorithm_.reset(); | |
210 underflow_cb_.Reset(); | |
211 time_cb_.Reset(); | |
212 flush_cb_.Reset(); | |
213 } | |
214 | |
192 if (sink_) { | 215 if (sink_) { |
193 sink_->Stop(); | 216 sink_->Stop(); |
194 sink_ = NULL; | 217 sink_ = NULL; |
195 } | 218 } |
196 | 219 |
197 { | 220 if (decoder_) { |
198 base::AutoLock auto_lock(lock_); | 221 decoder_->Stop(base::ResetAndReturn(&stop_cb_)); |
199 ChangeState_Locked(kStopped); | 222 return; |
200 algorithm_.reset(NULL); | |
201 init_cb_.Reset(); | |
202 underflow_cb_.Reset(); | |
203 time_cb_.Reset(); | |
204 flush_cb_.Reset(); | |
205 } | 223 } |
206 | 224 |
207 callback.Run(); | 225 task_runner_->PostTask(FROM_HERE, base::ResetAndReturn(&stop_cb_)); |
208 } | 226 } |
209 | 227 |
210 void AudioRendererImpl::Preroll(base::TimeDelta time, | 228 void AudioRendererImpl::Preroll(base::TimeDelta time, |
211 const PipelineStatusCB& cb) { | 229 const PipelineStatusCB& cb) { |
212 DCHECK(task_runner_->BelongsToCurrentThread()); | 230 DCHECK(task_runner_->BelongsToCurrentThread()); |
213 | 231 |
214 base::AutoLock auto_lock(lock_); | 232 base::AutoLock auto_lock(lock_); |
215 DCHECK(!sink_playing_); | 233 DCHECK(!sink_playing_); |
216 DCHECK_EQ(state_, kPaused); | 234 DCHECK_EQ(state_, kPaused); |
217 DCHECK(!pending_read_) << "Pending read must complete before seeking"; | 235 DCHECK(!pending_read_) << "Pending read must complete before seeking"; |
(...skipping 20 matching lines...) Expand all Loading... | |
238 DCHECK(!init_cb.is_null()); | 256 DCHECK(!init_cb.is_null()); |
239 DCHECK(!statistics_cb.is_null()); | 257 DCHECK(!statistics_cb.is_null()); |
240 DCHECK(!underflow_cb.is_null()); | 258 DCHECK(!underflow_cb.is_null()); |
241 DCHECK(!time_cb.is_null()); | 259 DCHECK(!time_cb.is_null()); |
242 DCHECK(!ended_cb.is_null()); | 260 DCHECK(!ended_cb.is_null()); |
243 DCHECK(!disabled_cb.is_null()); | 261 DCHECK(!disabled_cb.is_null()); |
244 DCHECK(!error_cb.is_null()); | 262 DCHECK(!error_cb.is_null()); |
245 DCHECK_EQ(kUninitialized, state_); | 263 DCHECK_EQ(kUninitialized, state_); |
246 DCHECK(sink_); | 264 DCHECK(sink_); |
247 | 265 |
266 state_ = kInitializing; | |
267 | |
248 weak_this_ = weak_factory_.GetWeakPtr(); | 268 weak_this_ = weak_factory_.GetWeakPtr(); |
249 init_cb_ = init_cb; | 269 init_cb_ = init_cb; |
250 statistics_cb_ = statistics_cb; | 270 statistics_cb_ = statistics_cb; |
251 underflow_cb_ = underflow_cb; | 271 underflow_cb_ = underflow_cb; |
252 time_cb_ = time_cb; | 272 time_cb_ = time_cb; |
253 ended_cb_ = ended_cb; | 273 ended_cb_ = ended_cb; |
254 disabled_cb_ = disabled_cb; | 274 disabled_cb_ = disabled_cb; |
255 error_cb_ = error_cb; | 275 error_cb_ = error_cb; |
256 | 276 |
257 decoder_selector_->SelectAudioDecoder( | 277 decoder_selector_->SelectAudioDecoder( |
258 stream, | 278 stream, |
259 statistics_cb, | 279 statistics_cb, |
260 base::Bind(&AudioRendererImpl::OnDecoderSelected, weak_this_)); | 280 base::Bind(&AudioRendererImpl::OnDecoderSelected, weak_this_)); |
261 } | 281 } |
262 | 282 |
263 void AudioRendererImpl::OnDecoderSelected( | 283 void AudioRendererImpl::OnDecoderSelected( |
264 scoped_ptr<AudioDecoder> decoder, | 284 scoped_ptr<AudioDecoder> decoder, |
265 scoped_ptr<DecryptingDemuxerStream> decrypting_demuxer_stream) { | 285 scoped_ptr<DecryptingDemuxerStream> decrypting_demuxer_stream) { |
266 DCHECK(task_runner_->BelongsToCurrentThread()); | 286 DCHECK(task_runner_->BelongsToCurrentThread()); |
267 | 287 |
268 base::AutoLock auto_lock(lock_); | |
269 scoped_ptr<AudioDecoderSelector> deleter(decoder_selector_.Pass()); | 288 scoped_ptr<AudioDecoderSelector> deleter(decoder_selector_.Pass()); |
270 | 289 |
271 if (state_ == kStopped) { | 290 if (!decoder) { |
272 DCHECK(!sink_); | 291 { |
292 base::AutoLock auto_lock(lock_); | |
293 ChangeState_Locked(kUninitialized); | |
294 } | |
295 // Stop() called during initialization. | |
296 if (!stop_cb_.is_null()) { | |
297 base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_ABORT); | |
298 Stop(base::ResetAndReturn(&stop_cb_)); | |
299 } else { | |
300 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); | |
301 } | |
273 return; | 302 return; |
274 } | 303 } |
304 { | |
DaleCurtis
2014/01/22 19:06:17
No need to scope since you're holding it for the r
rileya (GONE FROM CHROMIUM)
2014/01/22 19:18:59
Right, makes sense, for some reason I was thinking
| |
305 base::AutoLock auto_lock(lock_); | |
306 decoder_ = decoder.Pass(); | |
307 decrypting_demuxer_stream_ = decrypting_demuxer_stream.Pass(); | |
275 | 308 |
276 if (!decoder) { | 309 int sample_rate = decoder_->samples_per_second(); |
277 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); | 310 |
278 return; | 311 // The actual buffer size is controlled via the size of the AudioBus |
312 // provided to Render(), so just choose something reasonable here for looks. | |
313 int buffer_size = decoder_->samples_per_second() / 100; | |
314 audio_parameters_ = AudioParameters( | |
315 AudioParameters::AUDIO_PCM_LOW_LATENCY, decoder_->channel_layout(), | |
316 sample_rate, decoder_->bits_per_channel(), buffer_size); | |
317 if (!audio_parameters_.IsValid()) { | |
318 ChangeState_Locked(kUninitialized); | |
319 base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_INITIALIZATION_FAILED); | |
320 return; | |
321 } | |
322 | |
323 splicer_.reset(new AudioSplicer(sample_rate)); | |
324 | |
325 // We're all good! Continue initializing the rest of the audio renderer | |
326 // based on the decoder format. | |
327 algorithm_.reset(new AudioRendererAlgorithm()); | |
328 algorithm_->Initialize(0, audio_parameters_); | |
329 | |
330 ChangeState_Locked(kPaused); | |
331 | |
332 HistogramRendererEvent(INITIALIZED); | |
333 | |
334 { | |
335 base::AutoUnlock auto_unlock(lock_); | |
336 sink_->Initialize(audio_parameters_, weak_this_.get()); | |
337 sink_->Start(); | |
338 | |
339 // Some sinks play on start... | |
340 sink_->Pause(); | |
341 } | |
342 | |
343 DCHECK(!sink_playing_); | |
344 | |
345 base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK); | |
279 } | 346 } |
280 | |
281 decoder_ = decoder.Pass(); | |
282 decrypting_demuxer_stream_ = decrypting_demuxer_stream.Pass(); | |
283 | |
284 int sample_rate = decoder_->samples_per_second(); | |
285 | |
286 // The actual buffer size is controlled via the size of the AudioBus provided | |
287 // to Render(), so just choose something reasonable here for looks. | |
288 int buffer_size = decoder_->samples_per_second() / 100; | |
289 audio_parameters_ = AudioParameters( | |
290 AudioParameters::AUDIO_PCM_LOW_LATENCY, decoder_->channel_layout(), | |
291 sample_rate, decoder_->bits_per_channel(), buffer_size); | |
292 if (!audio_parameters_.IsValid()) { | |
293 base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_INITIALIZATION_FAILED); | |
294 return; | |
295 } | |
296 | |
297 splicer_.reset(new AudioSplicer(sample_rate)); | |
298 | |
299 // We're all good! Continue initializing the rest of the audio renderer based | |
300 // on the decoder format. | |
301 algorithm_.reset(new AudioRendererAlgorithm()); | |
302 algorithm_->Initialize(0, audio_parameters_); | |
303 | |
304 ChangeState_Locked(kPaused); | |
305 | |
306 HistogramRendererEvent(INITIALIZED); | |
307 | |
308 { | |
309 base::AutoUnlock auto_unlock(lock_); | |
310 sink_->Initialize(audio_parameters_, weak_this_.get()); | |
311 sink_->Start(); | |
312 | |
313 // Some sinks play on start... | |
314 sink_->Pause(); | |
315 } | |
316 | |
317 DCHECK(!sink_playing_); | |
318 | |
319 base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK); | |
320 } | 347 } |
321 | 348 |
322 void AudioRendererImpl::ResumeAfterUnderflow() { | 349 void AudioRendererImpl::ResumeAfterUnderflow() { |
323 DCHECK(task_runner_->BelongsToCurrentThread()); | 350 DCHECK(task_runner_->BelongsToCurrentThread()); |
324 base::AutoLock auto_lock(lock_); | 351 base::AutoLock auto_lock(lock_); |
325 if (state_ == kUnderflow) { | 352 if (state_ == kUnderflow) { |
326 // The "!preroll_aborted_" is a hack. If preroll is aborted, then we | 353 // The "!preroll_aborted_" is a hack. If preroll is aborted, then we |
327 // shouldn't even reach the kUnderflow state to begin with. But for now | 354 // shouldn't even reach the kUnderflow state to begin with. But for now |
328 // we're just making sure that the audio buffer capacity (i.e. the | 355 // we're just making sure that the audio buffer capacity (i.e. the |
329 // number of bytes that need to be buffered for preroll to complete) | 356 // number of bytes that need to be buffered for preroll to complete) |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
419 if (!buffer->frame_count()) | 446 if (!buffer->frame_count()) |
420 return true; | 447 return true; |
421 } | 448 } |
422 | 449 |
423 if (state_ != kUninitialized && state_ != kStopped) | 450 if (state_ != kUninitialized && state_ != kStopped) |
424 algorithm_->EnqueueBuffer(buffer); | 451 algorithm_->EnqueueBuffer(buffer); |
425 } | 452 } |
426 | 453 |
427 switch (state_) { | 454 switch (state_) { |
428 case kUninitialized: | 455 case kUninitialized: |
456 case kInitializing: | |
429 case kFlushing: | 457 case kFlushing: |
430 NOTREACHED(); | 458 NOTREACHED(); |
431 return false; | 459 return false; |
432 | 460 |
433 case kPaused: | 461 case kPaused: |
434 DCHECK(!pending_read_); | 462 DCHECK(!pending_read_); |
435 return false; | 463 return false; |
436 | 464 |
437 case kPrerolling: | 465 case kPrerolling: |
438 if (!buffer->end_of_stream() && !algorithm_->IsQueueFull()) | 466 if (!buffer->end_of_stream() && !algorithm_->IsQueueFull()) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
471 | 499 |
472 pending_read_ = true; | 500 pending_read_ = true; |
473 decoder_->Read(base::Bind(&AudioRendererImpl::DecodedAudioReady, weak_this_)); | 501 decoder_->Read(base::Bind(&AudioRendererImpl::DecodedAudioReady, weak_this_)); |
474 } | 502 } |
475 | 503 |
476 bool AudioRendererImpl::CanRead_Locked() { | 504 bool AudioRendererImpl::CanRead_Locked() { |
477 lock_.AssertAcquired(); | 505 lock_.AssertAcquired(); |
478 | 506 |
479 switch (state_) { | 507 switch (state_) { |
480 case kUninitialized: | 508 case kUninitialized: |
509 case kInitializing: | |
481 case kPaused: | 510 case kPaused: |
482 case kFlushing: | 511 case kFlushing: |
483 case kStopped: | 512 case kStopped: |
484 return false; | 513 return false; |
485 | 514 |
486 case kPrerolling: | 515 case kPrerolling: |
487 case kPlaying: | 516 case kPlaying: |
488 case kUnderflow: | 517 case kUnderflow: |
489 case kRebuffering: | 518 case kRebuffering: |
490 break; | 519 break; |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
660 void AudioRendererImpl::DisableUnderflowForTesting() { | 689 void AudioRendererImpl::DisableUnderflowForTesting() { |
661 underflow_disabled_ = true; | 690 underflow_disabled_ = true; |
662 } | 691 } |
663 | 692 |
664 void AudioRendererImpl::HandleAbortedReadOrDecodeError(bool is_decode_error) { | 693 void AudioRendererImpl::HandleAbortedReadOrDecodeError(bool is_decode_error) { |
665 lock_.AssertAcquired(); | 694 lock_.AssertAcquired(); |
666 | 695 |
667 PipelineStatus status = is_decode_error ? PIPELINE_ERROR_DECODE : PIPELINE_OK; | 696 PipelineStatus status = is_decode_error ? PIPELINE_ERROR_DECODE : PIPELINE_OK; |
668 switch (state_) { | 697 switch (state_) { |
669 case kUninitialized: | 698 case kUninitialized: |
699 case kInitializing: | |
670 NOTREACHED(); | 700 NOTREACHED(); |
671 return; | 701 return; |
672 case kPaused: | 702 case kPaused: |
673 if (status != PIPELINE_OK) | 703 if (status != PIPELINE_OK) |
674 error_cb_.Run(status); | 704 error_cb_.Run(status); |
675 return; | 705 return; |
676 case kFlushing: | 706 case kFlushing: |
677 ChangeState_Locked(kPaused); | 707 ChangeState_Locked(kPaused); |
678 | 708 |
679 if (status == PIPELINE_OK) { | 709 if (status == PIPELINE_OK) { |
(...skipping 20 matching lines...) Expand all Loading... | |
700 } | 730 } |
701 } | 731 } |
702 | 732 |
703 void AudioRendererImpl::ChangeState_Locked(State new_state) { | 733 void AudioRendererImpl::ChangeState_Locked(State new_state) { |
704 DVLOG(1) << __FUNCTION__ << " : " << state_ << " -> " << new_state; | 734 DVLOG(1) << __FUNCTION__ << " : " << state_ << " -> " << new_state; |
705 lock_.AssertAcquired(); | 735 lock_.AssertAcquired(); |
706 state_ = new_state; | 736 state_ = new_state; |
707 } | 737 } |
708 | 738 |
709 } // namespace media | 739 } // namespace media |
OLD | NEW |