Chromium Code Reviews| 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) | |
|
DaleCurtis
2014/01/22 18:43:26
This still needs to hit the stop_cb_ run below.
rileya (GONE FROM CHROMIUM)
2014/01/22 18:58:52
Fixed (assuming there's no issue with posting the
| |
| 204 return; | |
| 205 | |
| 206 ChangeState_Locked(kStopped); | |
| 207 algorithm_.reset(); | |
| 208 underflow_cb_.Reset(); | |
| 209 time_cb_.Reset(); | |
| 210 flush_cb_.Reset(); | |
| 211 } | |
| 212 | |
| 192 if (sink_) { | 213 if (sink_) { |
| 193 sink_->Stop(); | 214 sink_->Stop(); |
| 194 sink_ = NULL; | 215 sink_ = NULL; |
| 195 } | 216 } |
| 196 | 217 |
| 197 { | 218 if (decoder_) { |
| 198 base::AutoLock auto_lock(lock_); | 219 decoder_->Stop(base::ResetAndReturn(&stop_cb_)); |
| 199 ChangeState_Locked(kStopped); | 220 return; |
| 200 algorithm_.reset(NULL); | |
| 201 init_cb_.Reset(); | |
| 202 underflow_cb_.Reset(); | |
| 203 time_cb_.Reset(); | |
| 204 flush_cb_.Reset(); | |
| 205 } | 221 } |
| 206 | 222 |
| 207 callback.Run(); | 223 task_runner_->PostTask(FROM_HERE, base::ResetAndReturn(&stop_cb_)); |
| 208 } | 224 } |
| 209 | 225 |
| 210 void AudioRendererImpl::Preroll(base::TimeDelta time, | 226 void AudioRendererImpl::Preroll(base::TimeDelta time, |
| 211 const PipelineStatusCB& cb) { | 227 const PipelineStatusCB& cb) { |
| 212 DCHECK(task_runner_->BelongsToCurrentThread()); | 228 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 213 | 229 |
| 214 base::AutoLock auto_lock(lock_); | 230 base::AutoLock auto_lock(lock_); |
| 215 DCHECK(!sink_playing_); | 231 DCHECK(!sink_playing_); |
| 216 DCHECK_EQ(state_, kPaused); | 232 DCHECK_EQ(state_, kPaused); |
| 217 DCHECK(!pending_read_) << "Pending read must complete before seeking"; | 233 DCHECK(!pending_read_) << "Pending read must complete before seeking"; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 238 DCHECK(!init_cb.is_null()); | 254 DCHECK(!init_cb.is_null()); |
| 239 DCHECK(!statistics_cb.is_null()); | 255 DCHECK(!statistics_cb.is_null()); |
| 240 DCHECK(!underflow_cb.is_null()); | 256 DCHECK(!underflow_cb.is_null()); |
| 241 DCHECK(!time_cb.is_null()); | 257 DCHECK(!time_cb.is_null()); |
| 242 DCHECK(!ended_cb.is_null()); | 258 DCHECK(!ended_cb.is_null()); |
| 243 DCHECK(!disabled_cb.is_null()); | 259 DCHECK(!disabled_cb.is_null()); |
| 244 DCHECK(!error_cb.is_null()); | 260 DCHECK(!error_cb.is_null()); |
| 245 DCHECK_EQ(kUninitialized, state_); | 261 DCHECK_EQ(kUninitialized, state_); |
| 246 DCHECK(sink_); | 262 DCHECK(sink_); |
| 247 | 263 |
| 264 state_ = kInitializing; | |
| 265 | |
| 248 weak_this_ = weak_factory_.GetWeakPtr(); | 266 weak_this_ = weak_factory_.GetWeakPtr(); |
| 249 init_cb_ = init_cb; | 267 init_cb_ = init_cb; |
| 250 statistics_cb_ = statistics_cb; | 268 statistics_cb_ = statistics_cb; |
| 251 underflow_cb_ = underflow_cb; | 269 underflow_cb_ = underflow_cb; |
| 252 time_cb_ = time_cb; | 270 time_cb_ = time_cb; |
| 253 ended_cb_ = ended_cb; | 271 ended_cb_ = ended_cb; |
| 254 disabled_cb_ = disabled_cb; | 272 disabled_cb_ = disabled_cb; |
| 255 error_cb_ = error_cb; | 273 error_cb_ = error_cb; |
| 256 | 274 |
| 257 decoder_selector_->SelectAudioDecoder( | 275 decoder_selector_->SelectAudioDecoder( |
| 258 stream, | 276 stream, |
| 259 statistics_cb, | 277 statistics_cb, |
| 260 base::Bind(&AudioRendererImpl::OnDecoderSelected, weak_this_)); | 278 base::Bind(&AudioRendererImpl::OnDecoderSelected, weak_this_)); |
| 261 } | 279 } |
| 262 | 280 |
| 263 void AudioRendererImpl::OnDecoderSelected( | 281 void AudioRendererImpl::OnDecoderSelected( |
| 264 scoped_ptr<AudioDecoder> decoder, | 282 scoped_ptr<AudioDecoder> decoder, |
| 265 scoped_ptr<DecryptingDemuxerStream> decrypting_demuxer_stream) { | 283 scoped_ptr<DecryptingDemuxerStream> decrypting_demuxer_stream) { |
| 266 DCHECK(task_runner_->BelongsToCurrentThread()); | 284 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 267 | 285 |
| 268 base::AutoLock auto_lock(lock_); | |
| 269 scoped_ptr<AudioDecoderSelector> deleter(decoder_selector_.Pass()); | 286 scoped_ptr<AudioDecoderSelector> deleter(decoder_selector_.Pass()); |
| 270 | 287 |
| 271 if (state_ == kStopped) { | 288 if (!decoder) { |
| 272 DCHECK(!sink_); | 289 { |
| 290 base::AutoLock auto_lock(lock_); | |
| 291 ChangeState_Locked(kUninitialized); | |
| 292 } | |
| 293 // Stop() called during initialization. | |
| 294 if (!stop_cb_.is_null()) { | |
| 295 base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_ABORT); | |
| 296 Stop(base::ResetAndReturn(&stop_cb_)); | |
| 297 } else { | |
| 298 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); | |
| 299 } | |
| 273 return; | 300 return; |
| 301 } else { | |
|
DaleCurtis
2014/01/22 18:43:26
Why add the else {, the block above returns early?
rileya (GONE FROM CHROMIUM)
2014/01/22 18:58:52
I needed to scope the whole block for the AutoLock
| |
| 302 base::AutoLock auto_lock(lock_); | |
| 303 decoder_ = decoder.Pass(); | |
| 304 decrypting_demuxer_stream_ = decrypting_demuxer_stream.Pass(); | |
| 305 | |
| 306 int sample_rate = decoder_->samples_per_second(); | |
| 307 | |
| 308 // The actual buffer size is controlled via the size of the AudioBus | |
| 309 // provided to Render(), so just choose something reasonable here for looks. | |
| 310 int buffer_size = decoder_->samples_per_second() / 100; | |
| 311 audio_parameters_ = AudioParameters( | |
| 312 AudioParameters::AUDIO_PCM_LOW_LATENCY, decoder_->channel_layout(), | |
| 313 sample_rate, decoder_->bits_per_channel(), buffer_size); | |
| 314 if (!audio_parameters_.IsValid()) { | |
| 315 ChangeState_Locked(kUninitialized); | |
| 316 base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_INITIALIZATION_FAILED); | |
| 317 return; | |
| 318 } | |
| 319 | |
| 320 splicer_.reset(new AudioSplicer(sample_rate)); | |
| 321 | |
| 322 // We're all good! Continue initializing the rest of the audio renderer | |
| 323 // based on the decoder format. | |
| 324 algorithm_.reset(new AudioRendererAlgorithm()); | |
| 325 algorithm_->Initialize(0, audio_parameters_); | |
| 326 | |
| 327 ChangeState_Locked(kPaused); | |
| 328 | |
| 329 HistogramRendererEvent(INITIALIZED); | |
| 330 | |
| 331 { | |
| 332 base::AutoUnlock auto_unlock(lock_); | |
| 333 sink_->Initialize(audio_parameters_, weak_this_.get()); | |
| 334 sink_->Start(); | |
| 335 | |
| 336 // Some sinks play on start... | |
| 337 sink_->Pause(); | |
| 338 } | |
| 339 | |
| 340 DCHECK(!sink_playing_); | |
| 341 | |
| 342 base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK); | |
| 274 } | 343 } |
| 275 | |
| 276 if (!decoder) { | |
| 277 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); | |
| 278 return; | |
| 279 } | |
| 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 } | 344 } |
| 321 | 345 |
| 322 void AudioRendererImpl::ResumeAfterUnderflow() { | 346 void AudioRendererImpl::ResumeAfterUnderflow() { |
| 323 DCHECK(task_runner_->BelongsToCurrentThread()); | 347 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 324 base::AutoLock auto_lock(lock_); | 348 base::AutoLock auto_lock(lock_); |
| 325 if (state_ == kUnderflow) { | 349 if (state_ == kUnderflow) { |
| 326 // The "!preroll_aborted_" is a hack. If preroll is aborted, then we | 350 // 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 | 351 // 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 | 352 // 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) | 353 // 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()) | 443 if (!buffer->frame_count()) |
| 420 return true; | 444 return true; |
| 421 } | 445 } |
| 422 | 446 |
| 423 if (state_ != kUninitialized && state_ != kStopped) | 447 if (state_ != kUninitialized && state_ != kStopped) |
| 424 algorithm_->EnqueueBuffer(buffer); | 448 algorithm_->EnqueueBuffer(buffer); |
| 425 } | 449 } |
| 426 | 450 |
| 427 switch (state_) { | 451 switch (state_) { |
| 428 case kUninitialized: | 452 case kUninitialized: |
| 453 case kInitializing: | |
| 429 case kFlushing: | 454 case kFlushing: |
| 430 NOTREACHED(); | 455 NOTREACHED(); |
| 431 return false; | 456 return false; |
| 432 | 457 |
| 433 case kPaused: | 458 case kPaused: |
| 434 DCHECK(!pending_read_); | 459 DCHECK(!pending_read_); |
| 435 return false; | 460 return false; |
| 436 | 461 |
| 437 case kPrerolling: | 462 case kPrerolling: |
| 438 if (!buffer->end_of_stream() && !algorithm_->IsQueueFull()) | 463 if (!buffer->end_of_stream() && !algorithm_->IsQueueFull()) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 471 | 496 |
| 472 pending_read_ = true; | 497 pending_read_ = true; |
| 473 decoder_->Read(base::Bind(&AudioRendererImpl::DecodedAudioReady, weak_this_)); | 498 decoder_->Read(base::Bind(&AudioRendererImpl::DecodedAudioReady, weak_this_)); |
| 474 } | 499 } |
| 475 | 500 |
| 476 bool AudioRendererImpl::CanRead_Locked() { | 501 bool AudioRendererImpl::CanRead_Locked() { |
| 477 lock_.AssertAcquired(); | 502 lock_.AssertAcquired(); |
| 478 | 503 |
| 479 switch (state_) { | 504 switch (state_) { |
| 480 case kUninitialized: | 505 case kUninitialized: |
| 506 case kInitializing: | |
| 481 case kPaused: | 507 case kPaused: |
| 482 case kFlushing: | 508 case kFlushing: |
| 483 case kStopped: | 509 case kStopped: |
| 484 return false; | 510 return false; |
| 485 | 511 |
| 486 case kPrerolling: | 512 case kPrerolling: |
| 487 case kPlaying: | 513 case kPlaying: |
| 488 case kUnderflow: | 514 case kUnderflow: |
| 489 case kRebuffering: | 515 case kRebuffering: |
| 490 break; | 516 break; |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 660 void AudioRendererImpl::DisableUnderflowForTesting() { | 686 void AudioRendererImpl::DisableUnderflowForTesting() { |
| 661 underflow_disabled_ = true; | 687 underflow_disabled_ = true; |
| 662 } | 688 } |
| 663 | 689 |
| 664 void AudioRendererImpl::HandleAbortedReadOrDecodeError(bool is_decode_error) { | 690 void AudioRendererImpl::HandleAbortedReadOrDecodeError(bool is_decode_error) { |
| 665 lock_.AssertAcquired(); | 691 lock_.AssertAcquired(); |
| 666 | 692 |
| 667 PipelineStatus status = is_decode_error ? PIPELINE_ERROR_DECODE : PIPELINE_OK; | 693 PipelineStatus status = is_decode_error ? PIPELINE_ERROR_DECODE : PIPELINE_OK; |
| 668 switch (state_) { | 694 switch (state_) { |
| 669 case kUninitialized: | 695 case kUninitialized: |
| 696 case kInitializing: | |
| 670 NOTREACHED(); | 697 NOTREACHED(); |
| 671 return; | 698 return; |
| 672 case kPaused: | 699 case kPaused: |
| 673 if (status != PIPELINE_OK) | 700 if (status != PIPELINE_OK) |
| 674 error_cb_.Run(status); | 701 error_cb_.Run(status); |
| 675 return; | 702 return; |
| 676 case kFlushing: | 703 case kFlushing: |
| 677 ChangeState_Locked(kPaused); | 704 ChangeState_Locked(kPaused); |
| 678 | 705 |
| 679 if (status == PIPELINE_OK) { | 706 if (status == PIPELINE_OK) { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 700 } | 727 } |
| 701 } | 728 } |
| 702 | 729 |
| 703 void AudioRendererImpl::ChangeState_Locked(State new_state) { | 730 void AudioRendererImpl::ChangeState_Locked(State new_state) { |
| 704 DVLOG(1) << __FUNCTION__ << " : " << state_ << " -> " << new_state; | 731 DVLOG(1) << __FUNCTION__ << " : " << state_ << " -> " << new_state; |
| 705 lock_.AssertAcquired(); | 732 lock_.AssertAcquired(); |
| 706 state_ = new_state; | 733 state_ = new_state; |
| 707 } | 734 } |
| 708 | 735 |
| 709 } // namespace media | 736 } // namespace media |
| OLD | NEW |