| OLD | NEW |
| (Empty) |
| 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 #include "media/filters/audio_renderer_impl.h" | |
| 6 | |
| 7 #include <math.h> | |
| 8 | |
| 9 #include <algorithm> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/callback.h" | |
| 13 #include "base/callback_helpers.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/metrics/histogram.h" | |
| 16 #include "base/single_thread_task_runner.h" | |
| 17 #include "media/base/audio_buffer.h" | |
| 18 #include "media/base/audio_buffer_converter.h" | |
| 19 #include "media/base/audio_hardware_config.h" | |
| 20 #include "media/base/audio_splicer.h" | |
| 21 #include "media/base/bind_to_current_loop.h" | |
| 22 #include "media/base/demuxer_stream.h" | |
| 23 #include "media/filters/audio_clock.h" | |
| 24 #include "media/filters/decrypting_demuxer_stream.h" | |
| 25 | |
| 26 namespace media { | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 enum AudioRendererEvent { | |
| 31 INITIALIZED, | |
| 32 RENDER_ERROR, | |
| 33 RENDER_EVENT_MAX = RENDER_ERROR, | |
| 34 }; | |
| 35 | |
| 36 void HistogramRendererEvent(AudioRendererEvent event) { | |
| 37 UMA_HISTOGRAM_ENUMERATION( | |
| 38 "Media.AudioRendererEvents", event, RENDER_EVENT_MAX + 1); | |
| 39 } | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 AudioRendererImpl::AudioRendererImpl( | |
| 44 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | |
| 45 media::AudioRendererSink* sink, | |
| 46 ScopedVector<AudioDecoder> decoders, | |
| 47 const AudioHardwareConfig& hardware_config, | |
| 48 const scoped_refptr<MediaLog>& media_log) | |
| 49 : task_runner_(task_runner), | |
| 50 expecting_config_changes_(false), | |
| 51 sink_(sink), | |
| 52 audio_buffer_stream_( | |
| 53 new AudioBufferStream(task_runner, decoders.Pass(), media_log)), | |
| 54 hardware_config_(hardware_config), | |
| 55 playback_rate_(0), | |
| 56 state_(kUninitialized), | |
| 57 buffering_state_(BUFFERING_HAVE_NOTHING), | |
| 58 rendering_(false), | |
| 59 sink_playing_(false), | |
| 60 pending_read_(false), | |
| 61 received_end_of_stream_(false), | |
| 62 rendered_end_of_stream_(false), | |
| 63 weak_factory_(this) { | |
| 64 audio_buffer_stream_->set_splice_observer(base::Bind( | |
| 65 &AudioRendererImpl::OnNewSpliceBuffer, weak_factory_.GetWeakPtr())); | |
| 66 audio_buffer_stream_->set_config_change_observer(base::Bind( | |
| 67 &AudioRendererImpl::OnConfigChange, weak_factory_.GetWeakPtr())); | |
| 68 } | |
| 69 | |
| 70 AudioRendererImpl::~AudioRendererImpl() { | |
| 71 DVLOG(1) << __FUNCTION__; | |
| 72 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 73 | |
| 74 // If Render() is in progress, this call will wait for Render() to finish. | |
| 75 // After this call, the |sink_| will not call back into |this| anymore. | |
| 76 sink_->Stop(); | |
| 77 | |
| 78 if (!init_cb_.is_null()) | |
| 79 base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_ABORT); | |
| 80 } | |
| 81 | |
| 82 void AudioRendererImpl::StartTicking() { | |
| 83 DVLOG(1) << __FUNCTION__; | |
| 84 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 85 DCHECK(!rendering_); | |
| 86 rendering_ = true; | |
| 87 | |
| 88 base::AutoLock auto_lock(lock_); | |
| 89 // Wait for an eventual call to SetPlaybackRate() to start rendering. | |
| 90 if (playback_rate_ == 0) { | |
| 91 DCHECK(!sink_playing_); | |
| 92 return; | |
| 93 } | |
| 94 | |
| 95 StartRendering_Locked(); | |
| 96 } | |
| 97 | |
| 98 void AudioRendererImpl::StartRendering_Locked() { | |
| 99 DVLOG(1) << __FUNCTION__; | |
| 100 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 101 DCHECK_EQ(state_, kPlaying); | |
| 102 DCHECK(!sink_playing_); | |
| 103 DCHECK_NE(playback_rate_, 0); | |
| 104 lock_.AssertAcquired(); | |
| 105 | |
| 106 sink_playing_ = true; | |
| 107 | |
| 108 base::AutoUnlock auto_unlock(lock_); | |
| 109 sink_->Play(); | |
| 110 } | |
| 111 | |
| 112 void AudioRendererImpl::StopTicking() { | |
| 113 DVLOG(1) << __FUNCTION__; | |
| 114 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 115 DCHECK(rendering_); | |
| 116 rendering_ = false; | |
| 117 | |
| 118 base::AutoLock auto_lock(lock_); | |
| 119 // Rendering should have already been stopped with a zero playback rate. | |
| 120 if (playback_rate_ == 0) { | |
| 121 DCHECK(!sink_playing_); | |
| 122 return; | |
| 123 } | |
| 124 | |
| 125 StopRendering_Locked(); | |
| 126 } | |
| 127 | |
| 128 void AudioRendererImpl::StopRendering_Locked() { | |
| 129 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 130 DCHECK_EQ(state_, kPlaying); | |
| 131 DCHECK(sink_playing_); | |
| 132 lock_.AssertAcquired(); | |
| 133 | |
| 134 sink_playing_ = false; | |
| 135 | |
| 136 base::AutoUnlock auto_unlock(lock_); | |
| 137 sink_->Pause(); | |
| 138 } | |
| 139 | |
| 140 void AudioRendererImpl::SetMediaTime(base::TimeDelta time) { | |
| 141 DVLOG(1) << __FUNCTION__ << "(" << time << ")"; | |
| 142 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 143 | |
| 144 base::AutoLock auto_lock(lock_); | |
| 145 DCHECK(!rendering_); | |
| 146 DCHECK_EQ(state_, kFlushed); | |
| 147 | |
| 148 start_timestamp_ = time; | |
| 149 ended_timestamp_ = kInfiniteDuration(); | |
| 150 last_render_ticks_ = base::TimeTicks(); | |
| 151 first_packet_timestamp_ = kNoTimestamp(); | |
| 152 audio_clock_.reset(new AudioClock(time, audio_parameters_.sample_rate())); | |
| 153 } | |
| 154 | |
| 155 base::TimeDelta AudioRendererImpl::CurrentMediaTime() { | |
| 156 // In practice the Render() method is called with a high enough frequency | |
| 157 // that returning only the front timestamp is good enough and also prevents | |
| 158 // returning values that go backwards in time. | |
| 159 base::TimeDelta current_media_time; | |
| 160 { | |
| 161 base::AutoLock auto_lock(lock_); | |
| 162 current_media_time = audio_clock_->front_timestamp(); | |
| 163 } | |
| 164 | |
| 165 DVLOG(2) << __FUNCTION__ << ": " << current_media_time; | |
| 166 return current_media_time; | |
| 167 } | |
| 168 | |
| 169 base::TimeDelta AudioRendererImpl::CurrentMediaTimeForSyncingVideo() { | |
| 170 DVLOG(3) << __FUNCTION__; | |
| 171 | |
| 172 base::AutoLock auto_lock(lock_); | |
| 173 if (last_render_ticks_.is_null()) | |
| 174 return audio_clock_->front_timestamp(); | |
| 175 | |
| 176 return audio_clock_->TimestampSinceWriting(base::TimeTicks::Now() - | |
| 177 last_render_ticks_); | |
| 178 } | |
| 179 | |
| 180 TimeSource* AudioRendererImpl::GetTimeSource() { | |
| 181 return this; | |
| 182 } | |
| 183 | |
| 184 void AudioRendererImpl::Flush(const base::Closure& callback) { | |
| 185 DVLOG(1) << __FUNCTION__; | |
| 186 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 187 | |
| 188 base::AutoLock auto_lock(lock_); | |
| 189 DCHECK_EQ(state_, kPlaying); | |
| 190 DCHECK(flush_cb_.is_null()); | |
| 191 | |
| 192 flush_cb_ = callback; | |
| 193 ChangeState_Locked(kFlushing); | |
| 194 | |
| 195 if (pending_read_) | |
| 196 return; | |
| 197 | |
| 198 ChangeState_Locked(kFlushed); | |
| 199 DoFlush_Locked(); | |
| 200 } | |
| 201 | |
| 202 void AudioRendererImpl::DoFlush_Locked() { | |
| 203 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 204 lock_.AssertAcquired(); | |
| 205 | |
| 206 DCHECK(!pending_read_); | |
| 207 DCHECK_EQ(state_, kFlushed); | |
| 208 | |
| 209 audio_buffer_stream_->Reset(base::Bind(&AudioRendererImpl::ResetDecoderDone, | |
| 210 weak_factory_.GetWeakPtr())); | |
| 211 } | |
| 212 | |
| 213 void AudioRendererImpl::ResetDecoderDone() { | |
| 214 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 215 { | |
| 216 base::AutoLock auto_lock(lock_); | |
| 217 | |
| 218 DCHECK_EQ(state_, kFlushed); | |
| 219 DCHECK(!flush_cb_.is_null()); | |
| 220 | |
| 221 received_end_of_stream_ = false; | |
| 222 rendered_end_of_stream_ = false; | |
| 223 | |
| 224 // Flush() may have been called while underflowed/not fully buffered. | |
| 225 if (buffering_state_ != BUFFERING_HAVE_NOTHING) | |
| 226 SetBufferingState_Locked(BUFFERING_HAVE_NOTHING); | |
| 227 | |
| 228 splicer_->Reset(); | |
| 229 if (buffer_converter_) | |
| 230 buffer_converter_->Reset(); | |
| 231 algorithm_->FlushBuffers(); | |
| 232 } | |
| 233 | |
| 234 // Changes in buffering state are always posted. Flush callback must only be | |
| 235 // run after buffering state has been set back to nothing. | |
| 236 task_runner_->PostTask(FROM_HERE, base::ResetAndReturn(&flush_cb_)); | |
| 237 } | |
| 238 | |
| 239 void AudioRendererImpl::StartPlaying() { | |
| 240 DVLOG(1) << __FUNCTION__; | |
| 241 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 242 | |
| 243 base::AutoLock auto_lock(lock_); | |
| 244 DCHECK(!sink_playing_); | |
| 245 DCHECK_EQ(state_, kFlushed); | |
| 246 DCHECK_EQ(buffering_state_, BUFFERING_HAVE_NOTHING); | |
| 247 DCHECK(!pending_read_) << "Pending read must complete before seeking"; | |
| 248 | |
| 249 ChangeState_Locked(kPlaying); | |
| 250 AttemptRead_Locked(); | |
| 251 } | |
| 252 | |
| 253 void AudioRendererImpl::Initialize( | |
| 254 DemuxerStream* stream, | |
| 255 const PipelineStatusCB& init_cb, | |
| 256 const SetDecryptorReadyCB& set_decryptor_ready_cb, | |
| 257 const StatisticsCB& statistics_cb, | |
| 258 const BufferingStateCB& buffering_state_cb, | |
| 259 const base::Closure& ended_cb, | |
| 260 const PipelineStatusCB& error_cb) { | |
| 261 DVLOG(1) << __FUNCTION__; | |
| 262 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 263 DCHECK(stream); | |
| 264 DCHECK_EQ(stream->type(), DemuxerStream::AUDIO); | |
| 265 DCHECK(!init_cb.is_null()); | |
| 266 DCHECK(!statistics_cb.is_null()); | |
| 267 DCHECK(!buffering_state_cb.is_null()); | |
| 268 DCHECK(!ended_cb.is_null()); | |
| 269 DCHECK(!error_cb.is_null()); | |
| 270 DCHECK_EQ(kUninitialized, state_); | |
| 271 DCHECK(sink_.get()); | |
| 272 | |
| 273 state_ = kInitializing; | |
| 274 | |
| 275 // Always post |init_cb_| because |this| could be destroyed if initialization | |
| 276 // failed. | |
| 277 init_cb_ = BindToCurrentLoop(init_cb); | |
| 278 | |
| 279 buffering_state_cb_ = buffering_state_cb; | |
| 280 ended_cb_ = ended_cb; | |
| 281 error_cb_ = error_cb; | |
| 282 | |
| 283 expecting_config_changes_ = stream->SupportsConfigChanges(); | |
| 284 if (!expecting_config_changes_) { | |
| 285 // The actual buffer size is controlled via the size of the AudioBus | |
| 286 // provided to Render(), so just choose something reasonable here for looks. | |
| 287 int buffer_size = stream->audio_decoder_config().samples_per_second() / 100; | |
| 288 audio_parameters_.Reset( | |
| 289 AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
| 290 stream->audio_decoder_config().channel_layout(), | |
| 291 ChannelLayoutToChannelCount( | |
| 292 stream->audio_decoder_config().channel_layout()), | |
| 293 stream->audio_decoder_config().samples_per_second(), | |
| 294 stream->audio_decoder_config().bits_per_channel(), | |
| 295 buffer_size); | |
| 296 buffer_converter_.reset(); | |
| 297 } else { | |
| 298 // TODO(rileya): Support hardware config changes | |
| 299 const AudioParameters& hw_params = hardware_config_.GetOutputConfig(); | |
| 300 audio_parameters_.Reset( | |
| 301 hw_params.format(), | |
| 302 // Always use the source's channel layout and channel count to avoid | |
| 303 // premature downmixing (http://crbug.com/379288), platform specific | |
| 304 // issues around channel layouts (http://crbug.com/266674), and | |
| 305 // unnecessary upmixing overhead. | |
| 306 stream->audio_decoder_config().channel_layout(), | |
| 307 ChannelLayoutToChannelCount( | |
| 308 stream->audio_decoder_config().channel_layout()), | |
| 309 hw_params.sample_rate(), | |
| 310 hw_params.bits_per_sample(), | |
| 311 hardware_config_.GetHighLatencyBufferSize()); | |
| 312 } | |
| 313 | |
| 314 audio_clock_.reset( | |
| 315 new AudioClock(base::TimeDelta(), audio_parameters_.sample_rate())); | |
| 316 | |
| 317 audio_buffer_stream_->Initialize( | |
| 318 stream, base::Bind(&AudioRendererImpl::OnAudioBufferStreamInitialized, | |
| 319 weak_factory_.GetWeakPtr()), | |
| 320 set_decryptor_ready_cb, statistics_cb); | |
| 321 } | |
| 322 | |
| 323 void AudioRendererImpl::OnAudioBufferStreamInitialized(bool success) { | |
| 324 DVLOG(1) << __FUNCTION__ << ": " << success; | |
| 325 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 326 | |
| 327 base::AutoLock auto_lock(lock_); | |
| 328 | |
| 329 if (!success) { | |
| 330 state_ = kUninitialized; | |
| 331 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); | |
| 332 return; | |
| 333 } | |
| 334 | |
| 335 if (!audio_parameters_.IsValid()) { | |
| 336 DVLOG(1) << __FUNCTION__ << ": Invalid audio parameters: " | |
| 337 << audio_parameters_.AsHumanReadableString(); | |
| 338 ChangeState_Locked(kUninitialized); | |
| 339 base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_INITIALIZATION_FAILED); | |
| 340 return; | |
| 341 } | |
| 342 | |
| 343 if (expecting_config_changes_) | |
| 344 buffer_converter_.reset(new AudioBufferConverter(audio_parameters_)); | |
| 345 splicer_.reset(new AudioSplicer(audio_parameters_.sample_rate())); | |
| 346 | |
| 347 // We're all good! Continue initializing the rest of the audio renderer | |
| 348 // based on the decoder format. | |
| 349 algorithm_.reset(new AudioRendererAlgorithm()); | |
| 350 algorithm_->Initialize(audio_parameters_); | |
| 351 | |
| 352 ChangeState_Locked(kFlushed); | |
| 353 | |
| 354 HistogramRendererEvent(INITIALIZED); | |
| 355 | |
| 356 { | |
| 357 base::AutoUnlock auto_unlock(lock_); | |
| 358 sink_->Initialize(audio_parameters_, this); | |
| 359 sink_->Start(); | |
| 360 | |
| 361 // Some sinks play on start... | |
| 362 sink_->Pause(); | |
| 363 } | |
| 364 | |
| 365 DCHECK(!sink_playing_); | |
| 366 base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK); | |
| 367 } | |
| 368 | |
| 369 void AudioRendererImpl::SetVolume(float volume) { | |
| 370 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 371 DCHECK(sink_.get()); | |
| 372 sink_->SetVolume(volume); | |
| 373 } | |
| 374 | |
| 375 void AudioRendererImpl::DecodedAudioReady( | |
| 376 AudioBufferStream::Status status, | |
| 377 const scoped_refptr<AudioBuffer>& buffer) { | |
| 378 DVLOG(2) << __FUNCTION__ << "(" << status << ")"; | |
| 379 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 380 | |
| 381 base::AutoLock auto_lock(lock_); | |
| 382 DCHECK(state_ != kUninitialized); | |
| 383 | |
| 384 CHECK(pending_read_); | |
| 385 pending_read_ = false; | |
| 386 | |
| 387 if (status == AudioBufferStream::ABORTED || | |
| 388 status == AudioBufferStream::DEMUXER_READ_ABORTED) { | |
| 389 HandleAbortedReadOrDecodeError(false); | |
| 390 return; | |
| 391 } | |
| 392 | |
| 393 if (status == AudioBufferStream::DECODE_ERROR) { | |
| 394 HandleAbortedReadOrDecodeError(true); | |
| 395 return; | |
| 396 } | |
| 397 | |
| 398 DCHECK_EQ(status, AudioBufferStream::OK); | |
| 399 DCHECK(buffer.get()); | |
| 400 | |
| 401 if (state_ == kFlushing) { | |
| 402 ChangeState_Locked(kFlushed); | |
| 403 DoFlush_Locked(); | |
| 404 return; | |
| 405 } | |
| 406 | |
| 407 if (expecting_config_changes_) { | |
| 408 DCHECK(buffer_converter_); | |
| 409 buffer_converter_->AddInput(buffer); | |
| 410 while (buffer_converter_->HasNextBuffer()) { | |
| 411 if (!splicer_->AddInput(buffer_converter_->GetNextBuffer())) { | |
| 412 HandleAbortedReadOrDecodeError(true); | |
| 413 return; | |
| 414 } | |
| 415 } | |
| 416 } else { | |
| 417 if (!splicer_->AddInput(buffer)) { | |
| 418 HandleAbortedReadOrDecodeError(true); | |
| 419 return; | |
| 420 } | |
| 421 } | |
| 422 | |
| 423 if (!splicer_->HasNextBuffer()) { | |
| 424 AttemptRead_Locked(); | |
| 425 return; | |
| 426 } | |
| 427 | |
| 428 bool need_another_buffer = false; | |
| 429 while (splicer_->HasNextBuffer()) | |
| 430 need_another_buffer = HandleSplicerBuffer_Locked(splicer_->GetNextBuffer()); | |
| 431 | |
| 432 if (!need_another_buffer && !CanRead_Locked()) | |
| 433 return; | |
| 434 | |
| 435 AttemptRead_Locked(); | |
| 436 } | |
| 437 | |
| 438 bool AudioRendererImpl::HandleSplicerBuffer_Locked( | |
| 439 const scoped_refptr<AudioBuffer>& buffer) { | |
| 440 lock_.AssertAcquired(); | |
| 441 if (buffer->end_of_stream()) { | |
| 442 received_end_of_stream_ = true; | |
| 443 } else { | |
| 444 if (state_ == kPlaying) { | |
| 445 if (IsBeforeStartTime(buffer)) | |
| 446 return true; | |
| 447 | |
| 448 // Trim off any additional time before the start timestamp. | |
| 449 const base::TimeDelta trim_time = start_timestamp_ - buffer->timestamp(); | |
| 450 if (trim_time > base::TimeDelta()) { | |
| 451 buffer->TrimStart(buffer->frame_count() * | |
| 452 (static_cast<double>(trim_time.InMicroseconds()) / | |
| 453 buffer->duration().InMicroseconds())); | |
| 454 } | |
| 455 // If the entire buffer was trimmed, request a new one. | |
| 456 if (!buffer->frame_count()) | |
| 457 return true; | |
| 458 } | |
| 459 | |
| 460 if (state_ != kUninitialized) | |
| 461 algorithm_->EnqueueBuffer(buffer); | |
| 462 } | |
| 463 | |
| 464 // Store the timestamp of the first packet so we know when to start actual | |
| 465 // audio playback. | |
| 466 if (first_packet_timestamp_ == kNoTimestamp()) | |
| 467 first_packet_timestamp_ = buffer->timestamp(); | |
| 468 | |
| 469 switch (state_) { | |
| 470 case kUninitialized: | |
| 471 case kInitializing: | |
| 472 case kFlushing: | |
| 473 NOTREACHED(); | |
| 474 return false; | |
| 475 | |
| 476 case kFlushed: | |
| 477 DCHECK(!pending_read_); | |
| 478 return false; | |
| 479 | |
| 480 case kPlaying: | |
| 481 if (buffer->end_of_stream() || algorithm_->IsQueueFull()) { | |
| 482 if (buffering_state_ == BUFFERING_HAVE_NOTHING) | |
| 483 SetBufferingState_Locked(BUFFERING_HAVE_ENOUGH); | |
| 484 return false; | |
| 485 } | |
| 486 return true; | |
| 487 } | |
| 488 return false; | |
| 489 } | |
| 490 | |
| 491 void AudioRendererImpl::AttemptRead() { | |
| 492 base::AutoLock auto_lock(lock_); | |
| 493 AttemptRead_Locked(); | |
| 494 } | |
| 495 | |
| 496 void AudioRendererImpl::AttemptRead_Locked() { | |
| 497 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 498 lock_.AssertAcquired(); | |
| 499 | |
| 500 if (!CanRead_Locked()) | |
| 501 return; | |
| 502 | |
| 503 pending_read_ = true; | |
| 504 audio_buffer_stream_->Read(base::Bind(&AudioRendererImpl::DecodedAudioReady, | |
| 505 weak_factory_.GetWeakPtr())); | |
| 506 } | |
| 507 | |
| 508 bool AudioRendererImpl::CanRead_Locked() { | |
| 509 lock_.AssertAcquired(); | |
| 510 | |
| 511 switch (state_) { | |
| 512 case kUninitialized: | |
| 513 case kInitializing: | |
| 514 case kFlushing: | |
| 515 case kFlushed: | |
| 516 return false; | |
| 517 | |
| 518 case kPlaying: | |
| 519 break; | |
| 520 } | |
| 521 | |
| 522 return !pending_read_ && !received_end_of_stream_ && | |
| 523 !algorithm_->IsQueueFull(); | |
| 524 } | |
| 525 | |
| 526 void AudioRendererImpl::SetPlaybackRate(float playback_rate) { | |
| 527 DVLOG(1) << __FUNCTION__ << "(" << playback_rate << ")"; | |
| 528 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 529 DCHECK_GE(playback_rate, 0); | |
| 530 DCHECK(sink_.get()); | |
| 531 | |
| 532 base::AutoLock auto_lock(lock_); | |
| 533 | |
| 534 // We have two cases here: | |
| 535 // Play: current_playback_rate == 0 && playback_rate != 0 | |
| 536 // Pause: current_playback_rate != 0 && playback_rate == 0 | |
| 537 float current_playback_rate = playback_rate_; | |
| 538 playback_rate_ = playback_rate; | |
| 539 | |
| 540 if (!rendering_) | |
| 541 return; | |
| 542 | |
| 543 if (current_playback_rate == 0 && playback_rate != 0) { | |
| 544 StartRendering_Locked(); | |
| 545 return; | |
| 546 } | |
| 547 | |
| 548 if (current_playback_rate != 0 && playback_rate == 0) { | |
| 549 StopRendering_Locked(); | |
| 550 return; | |
| 551 } | |
| 552 } | |
| 553 | |
| 554 bool AudioRendererImpl::IsBeforeStartTime( | |
| 555 const scoped_refptr<AudioBuffer>& buffer) { | |
| 556 DCHECK_EQ(state_, kPlaying); | |
| 557 return buffer.get() && !buffer->end_of_stream() && | |
| 558 (buffer->timestamp() + buffer->duration()) < start_timestamp_; | |
| 559 } | |
| 560 | |
| 561 int AudioRendererImpl::Render(AudioBus* audio_bus, | |
| 562 int audio_delay_milliseconds) { | |
| 563 const int requested_frames = audio_bus->frames(); | |
| 564 base::TimeDelta playback_delay = base::TimeDelta::FromMilliseconds( | |
| 565 audio_delay_milliseconds); | |
| 566 const int delay_frames = static_cast<int>(playback_delay.InSecondsF() * | |
| 567 audio_parameters_.sample_rate()); | |
| 568 int frames_written = 0; | |
| 569 { | |
| 570 base::AutoLock auto_lock(lock_); | |
| 571 last_render_ticks_ = base::TimeTicks::Now(); | |
| 572 | |
| 573 // Ensure Stop() hasn't destroyed our |algorithm_| on the pipeline thread. | |
| 574 if (!algorithm_) { | |
| 575 audio_clock_->WroteAudio( | |
| 576 0, requested_frames, delay_frames, playback_rate_); | |
| 577 return 0; | |
| 578 } | |
| 579 | |
| 580 if (playback_rate_ == 0) { | |
| 581 audio_clock_->WroteAudio( | |
| 582 0, requested_frames, delay_frames, playback_rate_); | |
| 583 return 0; | |
| 584 } | |
| 585 | |
| 586 // Mute audio by returning 0 when not playing. | |
| 587 if (state_ != kPlaying) { | |
| 588 audio_clock_->WroteAudio( | |
| 589 0, requested_frames, delay_frames, playback_rate_); | |
| 590 return 0; | |
| 591 } | |
| 592 | |
| 593 // Delay playback by writing silence if we haven't reached the first | |
| 594 // timestamp yet; this can occur if the video starts before the audio. | |
| 595 if (algorithm_->frames_buffered() > 0) { | |
| 596 DCHECK(first_packet_timestamp_ != kNoTimestamp()); | |
| 597 const base::TimeDelta play_delay = | |
| 598 first_packet_timestamp_ - audio_clock_->back_timestamp(); | |
| 599 if (play_delay > base::TimeDelta()) { | |
| 600 DCHECK_EQ(frames_written, 0); | |
| 601 frames_written = | |
| 602 std::min(static_cast<int>(play_delay.InSecondsF() * | |
| 603 audio_parameters_.sample_rate()), | |
| 604 requested_frames); | |
| 605 audio_bus->ZeroFramesPartial(0, frames_written); | |
| 606 } | |
| 607 | |
| 608 // If there's any space left, actually render the audio; this is where the | |
| 609 // aural magic happens. | |
| 610 if (frames_written < requested_frames) { | |
| 611 frames_written += algorithm_->FillBuffer( | |
| 612 audio_bus, frames_written, requested_frames - frames_written, | |
| 613 playback_rate_); | |
| 614 } | |
| 615 } | |
| 616 | |
| 617 // We use the following conditions to determine end of playback: | |
| 618 // 1) Algorithm can not fill the audio callback buffer | |
| 619 // 2) We received an end of stream buffer | |
| 620 // 3) We haven't already signalled that we've ended | |
| 621 // 4) We've played all known audio data sent to hardware | |
| 622 // | |
| 623 // We use the following conditions to determine underflow: | |
| 624 // 1) Algorithm can not fill the audio callback buffer | |
| 625 // 2) We have NOT received an end of stream buffer | |
| 626 // 3) We are in the kPlaying state | |
| 627 // | |
| 628 // Otherwise the buffer has data we can send to the device. | |
| 629 // | |
| 630 // Per the TimeSource API the media time should always increase even after | |
| 631 // we've rendered all known audio data. Doing so simplifies scenarios where | |
| 632 // we have other sources of media data that need to be scheduled after audio | |
| 633 // data has ended. | |
| 634 // | |
| 635 // That being said, we don't want to advance time when underflowed as we | |
| 636 // know more decoded frames will eventually arrive. If we did, we would | |
| 637 // throw things out of sync when said decoded frames arrive. | |
| 638 int frames_after_end_of_stream = 0; | |
| 639 if (frames_written == 0) { | |
| 640 if (received_end_of_stream_) { | |
| 641 if (ended_timestamp_ == kInfiniteDuration()) | |
| 642 ended_timestamp_ = audio_clock_->back_timestamp(); | |
| 643 frames_after_end_of_stream = requested_frames; | |
| 644 } else if (state_ == kPlaying && | |
| 645 buffering_state_ != BUFFERING_HAVE_NOTHING) { | |
| 646 algorithm_->IncreaseQueueCapacity(); | |
| 647 SetBufferingState_Locked(BUFFERING_HAVE_NOTHING); | |
| 648 } | |
| 649 } | |
| 650 | |
| 651 audio_clock_->WroteAudio(frames_written + frames_after_end_of_stream, | |
| 652 requested_frames, | |
| 653 delay_frames, | |
| 654 playback_rate_); | |
| 655 | |
| 656 if (CanRead_Locked()) { | |
| 657 task_runner_->PostTask(FROM_HERE, | |
| 658 base::Bind(&AudioRendererImpl::AttemptRead, | |
| 659 weak_factory_.GetWeakPtr())); | |
| 660 } | |
| 661 | |
| 662 if (audio_clock_->front_timestamp() >= ended_timestamp_ && | |
| 663 !rendered_end_of_stream_) { | |
| 664 rendered_end_of_stream_ = true; | |
| 665 task_runner_->PostTask(FROM_HERE, ended_cb_); | |
| 666 } | |
| 667 } | |
| 668 | |
| 669 DCHECK_LE(frames_written, requested_frames); | |
| 670 return frames_written; | |
| 671 } | |
| 672 | |
| 673 void AudioRendererImpl::OnRenderError() { | |
| 674 // UMA data tells us this happens ~0.01% of the time. Trigger an error instead | |
| 675 // of trying to gracefully fall back to a fake sink. It's very likely | |
| 676 // OnRenderError() should be removed and the audio stack handle errors without | |
| 677 // notifying clients. See http://crbug.com/234708 for details. | |
| 678 HistogramRendererEvent(RENDER_ERROR); | |
| 679 // Post to |task_runner_| as this is called on the audio callback thread. | |
| 680 task_runner_->PostTask(FROM_HERE, | |
| 681 base::Bind(error_cb_, PIPELINE_ERROR_DECODE)); | |
| 682 } | |
| 683 | |
| 684 void AudioRendererImpl::HandleAbortedReadOrDecodeError(bool is_decode_error) { | |
| 685 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 686 lock_.AssertAcquired(); | |
| 687 | |
| 688 PipelineStatus status = is_decode_error ? PIPELINE_ERROR_DECODE : PIPELINE_OK; | |
| 689 switch (state_) { | |
| 690 case kUninitialized: | |
| 691 case kInitializing: | |
| 692 NOTREACHED(); | |
| 693 return; | |
| 694 case kFlushing: | |
| 695 ChangeState_Locked(kFlushed); | |
| 696 if (status == PIPELINE_OK) { | |
| 697 DoFlush_Locked(); | |
| 698 return; | |
| 699 } | |
| 700 | |
| 701 error_cb_.Run(status); | |
| 702 base::ResetAndReturn(&flush_cb_).Run(); | |
| 703 return; | |
| 704 | |
| 705 case kFlushed: | |
| 706 case kPlaying: | |
| 707 if (status != PIPELINE_OK) | |
| 708 error_cb_.Run(status); | |
| 709 return; | |
| 710 } | |
| 711 } | |
| 712 | |
| 713 void AudioRendererImpl::ChangeState_Locked(State new_state) { | |
| 714 DVLOG(1) << __FUNCTION__ << " : " << state_ << " -> " << new_state; | |
| 715 lock_.AssertAcquired(); | |
| 716 state_ = new_state; | |
| 717 } | |
| 718 | |
| 719 void AudioRendererImpl::OnNewSpliceBuffer(base::TimeDelta splice_timestamp) { | |
| 720 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 721 splicer_->SetSpliceTimestamp(splice_timestamp); | |
| 722 } | |
| 723 | |
| 724 void AudioRendererImpl::OnConfigChange() { | |
| 725 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 726 DCHECK(expecting_config_changes_); | |
| 727 buffer_converter_->ResetTimestampState(); | |
| 728 // Drain flushed buffers from the converter so the AudioSplicer receives all | |
| 729 // data ahead of any OnNewSpliceBuffer() calls. Since discontinuities should | |
| 730 // only appear after config changes, AddInput() should never fail here. | |
| 731 while (buffer_converter_->HasNextBuffer()) | |
| 732 CHECK(splicer_->AddInput(buffer_converter_->GetNextBuffer())); | |
| 733 } | |
| 734 | |
| 735 void AudioRendererImpl::SetBufferingState_Locked( | |
| 736 BufferingState buffering_state) { | |
| 737 DVLOG(1) << __FUNCTION__ << " : " << buffering_state_ << " -> " | |
| 738 << buffering_state; | |
| 739 DCHECK_NE(buffering_state_, buffering_state); | |
| 740 lock_.AssertAcquired(); | |
| 741 buffering_state_ = buffering_state; | |
| 742 | |
| 743 task_runner_->PostTask(FROM_HERE, | |
| 744 base::Bind(buffering_state_cb_, buffering_state_)); | |
| 745 } | |
| 746 | |
| 747 } // namespace media | |
| OLD | NEW |