| 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 } |
| 275 | 304 |
| 276 if (!decoder) { | 305 base::AutoLock auto_lock(lock_); |
| 277 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); | |
| 278 return; | |
| 279 } | |
| 280 | |
| 281 decoder_ = decoder.Pass(); | 306 decoder_ = decoder.Pass(); |
| 282 decrypting_demuxer_stream_ = decrypting_demuxer_stream.Pass(); | 307 decrypting_demuxer_stream_ = decrypting_demuxer_stream.Pass(); |
| 283 | 308 |
| 284 int sample_rate = decoder_->samples_per_second(); | 309 int sample_rate = decoder_->samples_per_second(); |
| 285 | 310 |
| 286 // The actual buffer size is controlled via the size of the AudioBus provided | 311 // The actual buffer size is controlled via the size of the AudioBus |
| 287 // to Render(), so just choose something reasonable here for looks. | 312 // provided to Render(), so just choose something reasonable here for looks. |
| 288 int buffer_size = decoder_->samples_per_second() / 100; | 313 int buffer_size = decoder_->samples_per_second() / 100; |
| 289 audio_parameters_ = AudioParameters( | 314 audio_parameters_ = AudioParameters( |
| 290 AudioParameters::AUDIO_PCM_LOW_LATENCY, decoder_->channel_layout(), | 315 AudioParameters::AUDIO_PCM_LOW_LATENCY, decoder_->channel_layout(), |
| 291 sample_rate, decoder_->bits_per_channel(), buffer_size); | 316 sample_rate, decoder_->bits_per_channel(), buffer_size); |
| 292 if (!audio_parameters_.IsValid()) { | 317 if (!audio_parameters_.IsValid()) { |
| 318 ChangeState_Locked(kUninitialized); |
| 293 base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_INITIALIZATION_FAILED); | 319 base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_INITIALIZATION_FAILED); |
| 294 return; | 320 return; |
| 295 } | 321 } |
| 296 | 322 |
| 297 splicer_.reset(new AudioSplicer(sample_rate)); | 323 splicer_.reset(new AudioSplicer(sample_rate)); |
| 298 | 324 |
| 299 // We're all good! Continue initializing the rest of the audio renderer based | 325 // We're all good! Continue initializing the rest of the audio renderer |
| 300 // on the decoder format. | 326 // based on the decoder format. |
| 301 algorithm_.reset(new AudioRendererAlgorithm()); | 327 algorithm_.reset(new AudioRendererAlgorithm()); |
| 302 algorithm_->Initialize(0, audio_parameters_); | 328 algorithm_->Initialize(0, audio_parameters_); |
| 303 | 329 |
| 304 ChangeState_Locked(kPaused); | 330 ChangeState_Locked(kPaused); |
| 305 | 331 |
| 306 HistogramRendererEvent(INITIALIZED); | 332 HistogramRendererEvent(INITIALIZED); |
| 307 | 333 |
| 308 { | 334 { |
| 309 base::AutoUnlock auto_unlock(lock_); | 335 base::AutoUnlock auto_unlock(lock_); |
| 310 sink_->Initialize(audio_parameters_, weak_this_.get()); | 336 sink_->Initialize(audio_parameters_, weak_this_.get()); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 if (!buffer->frame_count()) | 445 if (!buffer->frame_count()) |
| 420 return true; | 446 return true; |
| 421 } | 447 } |
| 422 | 448 |
| 423 if (state_ != kUninitialized && state_ != kStopped) | 449 if (state_ != kUninitialized && state_ != kStopped) |
| 424 algorithm_->EnqueueBuffer(buffer); | 450 algorithm_->EnqueueBuffer(buffer); |
| 425 } | 451 } |
| 426 | 452 |
| 427 switch (state_) { | 453 switch (state_) { |
| 428 case kUninitialized: | 454 case kUninitialized: |
| 455 case kInitializing: |
| 429 case kFlushing: | 456 case kFlushing: |
| 430 NOTREACHED(); | 457 NOTREACHED(); |
| 431 return false; | 458 return false; |
| 432 | 459 |
| 433 case kPaused: | 460 case kPaused: |
| 434 DCHECK(!pending_read_); | 461 DCHECK(!pending_read_); |
| 435 return false; | 462 return false; |
| 436 | 463 |
| 437 case kPrerolling: | 464 case kPrerolling: |
| 438 if (!buffer->end_of_stream() && !algorithm_->IsQueueFull()) | 465 if (!buffer->end_of_stream() && !algorithm_->IsQueueFull()) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 | 498 |
| 472 pending_read_ = true; | 499 pending_read_ = true; |
| 473 decoder_->Read(base::Bind(&AudioRendererImpl::DecodedAudioReady, weak_this_)); | 500 decoder_->Read(base::Bind(&AudioRendererImpl::DecodedAudioReady, weak_this_)); |
| 474 } | 501 } |
| 475 | 502 |
| 476 bool AudioRendererImpl::CanRead_Locked() { | 503 bool AudioRendererImpl::CanRead_Locked() { |
| 477 lock_.AssertAcquired(); | 504 lock_.AssertAcquired(); |
| 478 | 505 |
| 479 switch (state_) { | 506 switch (state_) { |
| 480 case kUninitialized: | 507 case kUninitialized: |
| 508 case kInitializing: |
| 481 case kPaused: | 509 case kPaused: |
| 482 case kFlushing: | 510 case kFlushing: |
| 483 case kStopped: | 511 case kStopped: |
| 484 return false; | 512 return false; |
| 485 | 513 |
| 486 case kPrerolling: | 514 case kPrerolling: |
| 487 case kPlaying: | 515 case kPlaying: |
| 488 case kUnderflow: | 516 case kUnderflow: |
| 489 case kRebuffering: | 517 case kRebuffering: |
| 490 break; | 518 break; |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 660 void AudioRendererImpl::DisableUnderflowForTesting() { | 688 void AudioRendererImpl::DisableUnderflowForTesting() { |
| 661 underflow_disabled_ = true; | 689 underflow_disabled_ = true; |
| 662 } | 690 } |
| 663 | 691 |
| 664 void AudioRendererImpl::HandleAbortedReadOrDecodeError(bool is_decode_error) { | 692 void AudioRendererImpl::HandleAbortedReadOrDecodeError(bool is_decode_error) { |
| 665 lock_.AssertAcquired(); | 693 lock_.AssertAcquired(); |
| 666 | 694 |
| 667 PipelineStatus status = is_decode_error ? PIPELINE_ERROR_DECODE : PIPELINE_OK; | 695 PipelineStatus status = is_decode_error ? PIPELINE_ERROR_DECODE : PIPELINE_OK; |
| 668 switch (state_) { | 696 switch (state_) { |
| 669 case kUninitialized: | 697 case kUninitialized: |
| 698 case kInitializing: |
| 670 NOTREACHED(); | 699 NOTREACHED(); |
| 671 return; | 700 return; |
| 672 case kPaused: | 701 case kPaused: |
| 673 if (status != PIPELINE_OK) | 702 if (status != PIPELINE_OK) |
| 674 error_cb_.Run(status); | 703 error_cb_.Run(status); |
| 675 return; | 704 return; |
| 676 case kFlushing: | 705 case kFlushing: |
| 677 ChangeState_Locked(kPaused); | 706 ChangeState_Locked(kPaused); |
| 678 | 707 |
| 679 if (status == PIPELINE_OK) { | 708 if (status == PIPELINE_OK) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 700 } | 729 } |
| 701 } | 730 } |
| 702 | 731 |
| 703 void AudioRendererImpl::ChangeState_Locked(State new_state) { | 732 void AudioRendererImpl::ChangeState_Locked(State new_state) { |
| 704 DVLOG(1) << __FUNCTION__ << " : " << state_ << " -> " << new_state; | 733 DVLOG(1) << __FUNCTION__ << " : " << state_ << " -> " << new_state; |
| 705 lock_.AssertAcquired(); | 734 lock_.AssertAcquired(); |
| 706 state_ = new_state; | 735 state_ = new_state; |
| 707 } | 736 } |
| 708 | 737 |
| 709 } // namespace media | 738 } // namespace media |
| OLD | NEW |