Decryptors can report kNoKey to WebMediaPlayer
[chromium-blink-merge.git] / media / renderers / audio_renderer_impl.cc
blob74ef1d249526e694db66df69135315b062108fbc
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.
5 #include "media/renderers/audio_renderer_impl.h"
7 #include <math.h>
9 #include <algorithm>
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"
26 namespace media {
28 namespace {
30 enum AudioRendererEvent {
31 INITIALIZED,
32 RENDER_ERROR,
33 RENDER_EVENT_MAX = RENDER_ERROR,
36 void HistogramRendererEvent(AudioRendererEvent event) {
37 UMA_HISTOGRAM_ENUMERATION(
38 "Media.AudioRendererEvents", event, RENDER_EVENT_MAX + 1);
41 } // namespace
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()));
70 AudioRendererImpl::~AudioRendererImpl() {
71 DVLOG(1) << __FUNCTION__;
72 DCHECK(task_runner_->BelongsToCurrentThread());
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();
78 if (!init_cb_.is_null())
79 base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_ABORT);
82 void AudioRendererImpl::StartTicking() {
83 DVLOG(1) << __FUNCTION__;
84 DCHECK(task_runner_->BelongsToCurrentThread());
85 DCHECK(!rendering_);
86 rendering_ = true;
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;
95 StartRendering_Locked();
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();
106 sink_playing_ = true;
108 base::AutoUnlock auto_unlock(lock_);
109 sink_->Play();
112 void AudioRendererImpl::StopTicking() {
113 DVLOG(1) << __FUNCTION__;
114 DCHECK(task_runner_->BelongsToCurrentThread());
115 DCHECK(rendering_);
116 rendering_ = false;
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;
125 StopRendering_Locked();
128 void AudioRendererImpl::StopRendering_Locked() {
129 DCHECK(task_runner_->BelongsToCurrentThread());
130 DCHECK_EQ(state_, kPlaying);
131 DCHECK(sink_playing_);
132 lock_.AssertAcquired();
134 sink_playing_ = false;
136 base::AutoUnlock auto_unlock(lock_);
137 sink_->Pause();
140 void AudioRendererImpl::SetMediaTime(base::TimeDelta time) {
141 DVLOG(1) << __FUNCTION__ << "(" << time << ")";
142 DCHECK(task_runner_->BelongsToCurrentThread());
144 base::AutoLock auto_lock(lock_);
145 DCHECK(!rendering_);
146 DCHECK_EQ(state_, kFlushed);
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()));
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;
161 base::AutoLock auto_lock(lock_);
162 current_media_time = audio_clock_->front_timestamp();
165 DVLOG(2) << __FUNCTION__ << ": " << current_media_time;
166 return current_media_time;
169 base::TimeDelta AudioRendererImpl::CurrentMediaTimeForSyncingVideo() {
170 DVLOG(3) << __FUNCTION__;
172 base::AutoLock auto_lock(lock_);
173 if (last_render_ticks_.is_null())
174 return audio_clock_->front_timestamp();
176 return audio_clock_->TimestampSinceWriting(base::TimeTicks::Now() -
177 last_render_ticks_);
180 TimeSource* AudioRendererImpl::GetTimeSource() {
181 return this;
184 void AudioRendererImpl::Flush(const base::Closure& callback) {
185 DVLOG(1) << __FUNCTION__;
186 DCHECK(task_runner_->BelongsToCurrentThread());
188 base::AutoLock auto_lock(lock_);
189 DCHECK_EQ(state_, kPlaying);
190 DCHECK(flush_cb_.is_null());
192 flush_cb_ = callback;
193 ChangeState_Locked(kFlushing);
195 if (pending_read_)
196 return;
198 ChangeState_Locked(kFlushed);
199 DoFlush_Locked();
202 void AudioRendererImpl::DoFlush_Locked() {
203 DCHECK(task_runner_->BelongsToCurrentThread());
204 lock_.AssertAcquired();
206 DCHECK(!pending_read_);
207 DCHECK_EQ(state_, kFlushed);
209 audio_buffer_stream_->Reset(base::Bind(&AudioRendererImpl::ResetDecoderDone,
210 weak_factory_.GetWeakPtr()));
213 void AudioRendererImpl::ResetDecoderDone() {
214 DCHECK(task_runner_->BelongsToCurrentThread());
216 base::AutoLock auto_lock(lock_);
218 DCHECK_EQ(state_, kFlushed);
219 DCHECK(!flush_cb_.is_null());
221 received_end_of_stream_ = false;
222 rendered_end_of_stream_ = false;
224 // Flush() may have been called while underflowed/not fully buffered.
225 if (buffering_state_ != BUFFERING_HAVE_NOTHING)
226 SetBufferingState_Locked(BUFFERING_HAVE_NOTHING);
228 splicer_->Reset();
229 if (buffer_converter_)
230 buffer_converter_->Reset();
231 algorithm_->FlushBuffers();
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_));
239 void AudioRendererImpl::StartPlaying() {
240 DVLOG(1) << __FUNCTION__;
241 DCHECK(task_runner_->BelongsToCurrentThread());
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";
249 ChangeState_Locked(kPlaying);
250 AttemptRead_Locked();
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 const base::Closure& waiting_for_decryption_key_cb) {
262 DVLOG(1) << __FUNCTION__;
263 DCHECK(task_runner_->BelongsToCurrentThread());
264 DCHECK(stream);
265 DCHECK_EQ(stream->type(), DemuxerStream::AUDIO);
266 DCHECK(!init_cb.is_null());
267 DCHECK(!statistics_cb.is_null());
268 DCHECK(!buffering_state_cb.is_null());
269 DCHECK(!ended_cb.is_null());
270 DCHECK(!error_cb.is_null());
271 DCHECK_EQ(kUninitialized, state_);
272 DCHECK(sink_.get());
274 state_ = kInitializing;
276 // Always post |init_cb_| because |this| could be destroyed if initialization
277 // failed.
278 init_cb_ = BindToCurrentLoop(init_cb);
280 buffering_state_cb_ = buffering_state_cb;
281 ended_cb_ = ended_cb;
282 error_cb_ = error_cb;
284 expecting_config_changes_ = stream->SupportsConfigChanges();
285 if (!expecting_config_changes_) {
286 // The actual buffer size is controlled via the size of the AudioBus
287 // provided to Render(), so just choose something reasonable here for looks.
288 int buffer_size = stream->audio_decoder_config().samples_per_second() / 100;
289 audio_parameters_.Reset(
290 AudioParameters::AUDIO_PCM_LOW_LATENCY,
291 stream->audio_decoder_config().channel_layout(),
292 ChannelLayoutToChannelCount(
293 stream->audio_decoder_config().channel_layout()),
294 stream->audio_decoder_config().samples_per_second(),
295 stream->audio_decoder_config().bits_per_channel(),
296 buffer_size);
297 buffer_converter_.reset();
298 } else {
299 // TODO(rileya): Support hardware config changes
300 const AudioParameters& hw_params = hardware_config_.GetOutputConfig();
301 audio_parameters_.Reset(
302 hw_params.format(),
303 // Always use the source's channel layout and channel count to avoid
304 // premature downmixing (http://crbug.com/379288), platform specific
305 // issues around channel layouts (http://crbug.com/266674), and
306 // unnecessary upmixing overhead.
307 stream->audio_decoder_config().channel_layout(),
308 ChannelLayoutToChannelCount(
309 stream->audio_decoder_config().channel_layout()),
310 hw_params.sample_rate(),
311 hw_params.bits_per_sample(),
312 hardware_config_.GetHighLatencyBufferSize());
315 audio_clock_.reset(
316 new AudioClock(base::TimeDelta(), audio_parameters_.sample_rate()));
318 audio_buffer_stream_->Initialize(
319 stream, base::Bind(&AudioRendererImpl::OnAudioBufferStreamInitialized,
320 weak_factory_.GetWeakPtr()),
321 set_decryptor_ready_cb, statistics_cb, waiting_for_decryption_key_cb);
324 void AudioRendererImpl::OnAudioBufferStreamInitialized(bool success) {
325 DVLOG(1) << __FUNCTION__ << ": " << success;
326 DCHECK(task_runner_->BelongsToCurrentThread());
328 base::AutoLock auto_lock(lock_);
330 if (!success) {
331 state_ = kUninitialized;
332 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED);
333 return;
336 if (!audio_parameters_.IsValid()) {
337 DVLOG(1) << __FUNCTION__ << ": Invalid audio parameters: "
338 << audio_parameters_.AsHumanReadableString();
339 ChangeState_Locked(kUninitialized);
340 base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_INITIALIZATION_FAILED);
341 return;
344 if (expecting_config_changes_)
345 buffer_converter_.reset(new AudioBufferConverter(audio_parameters_));
346 splicer_.reset(new AudioSplicer(audio_parameters_.sample_rate()));
348 // We're all good! Continue initializing the rest of the audio renderer
349 // based on the decoder format.
350 algorithm_.reset(new AudioRendererAlgorithm());
351 algorithm_->Initialize(audio_parameters_);
353 ChangeState_Locked(kFlushed);
355 HistogramRendererEvent(INITIALIZED);
358 base::AutoUnlock auto_unlock(lock_);
359 sink_->Initialize(audio_parameters_, this);
360 sink_->Start();
362 // Some sinks play on start...
363 sink_->Pause();
366 DCHECK(!sink_playing_);
367 base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK);
370 void AudioRendererImpl::SetVolume(float volume) {
371 DCHECK(task_runner_->BelongsToCurrentThread());
372 DCHECK(sink_.get());
373 sink_->SetVolume(volume);
376 void AudioRendererImpl::DecodedAudioReady(
377 AudioBufferStream::Status status,
378 const scoped_refptr<AudioBuffer>& buffer) {
379 DVLOG(2) << __FUNCTION__ << "(" << status << ")";
380 DCHECK(task_runner_->BelongsToCurrentThread());
382 base::AutoLock auto_lock(lock_);
383 DCHECK(state_ != kUninitialized);
385 CHECK(pending_read_);
386 pending_read_ = false;
388 if (status == AudioBufferStream::ABORTED ||
389 status == AudioBufferStream::DEMUXER_READ_ABORTED) {
390 HandleAbortedReadOrDecodeError(false);
391 return;
394 if (status == AudioBufferStream::DECODE_ERROR) {
395 HandleAbortedReadOrDecodeError(true);
396 return;
399 DCHECK_EQ(status, AudioBufferStream::OK);
400 DCHECK(buffer.get());
402 if (state_ == kFlushing) {
403 ChangeState_Locked(kFlushed);
404 DoFlush_Locked();
405 return;
408 if (expecting_config_changes_) {
409 DCHECK(buffer_converter_);
410 buffer_converter_->AddInput(buffer);
411 while (buffer_converter_->HasNextBuffer()) {
412 if (!splicer_->AddInput(buffer_converter_->GetNextBuffer())) {
413 HandleAbortedReadOrDecodeError(true);
414 return;
417 } else {
418 if (!splicer_->AddInput(buffer)) {
419 HandleAbortedReadOrDecodeError(true);
420 return;
424 if (!splicer_->HasNextBuffer()) {
425 AttemptRead_Locked();
426 return;
429 bool need_another_buffer = false;
430 while (splicer_->HasNextBuffer())
431 need_another_buffer = HandleSplicerBuffer_Locked(splicer_->GetNextBuffer());
433 if (!need_another_buffer && !CanRead_Locked())
434 return;
436 AttemptRead_Locked();
439 bool AudioRendererImpl::HandleSplicerBuffer_Locked(
440 const scoped_refptr<AudioBuffer>& buffer) {
441 lock_.AssertAcquired();
442 if (buffer->end_of_stream()) {
443 received_end_of_stream_ = true;
444 } else {
445 if (state_ == kPlaying) {
446 if (IsBeforeStartTime(buffer))
447 return true;
449 // Trim off any additional time before the start timestamp.
450 const base::TimeDelta trim_time = start_timestamp_ - buffer->timestamp();
451 if (trim_time > base::TimeDelta()) {
452 buffer->TrimStart(buffer->frame_count() *
453 (static_cast<double>(trim_time.InMicroseconds()) /
454 buffer->duration().InMicroseconds()));
456 // If the entire buffer was trimmed, request a new one.
457 if (!buffer->frame_count())
458 return true;
461 if (state_ != kUninitialized)
462 algorithm_->EnqueueBuffer(buffer);
465 // Store the timestamp of the first packet so we know when to start actual
466 // audio playback.
467 if (first_packet_timestamp_ == kNoTimestamp())
468 first_packet_timestamp_ = buffer->timestamp();
470 switch (state_) {
471 case kUninitialized:
472 case kInitializing:
473 case kFlushing:
474 NOTREACHED();
475 return false;
477 case kFlushed:
478 DCHECK(!pending_read_);
479 return false;
481 case kPlaying:
482 if (buffer->end_of_stream() || algorithm_->IsQueueFull()) {
483 if (buffering_state_ == BUFFERING_HAVE_NOTHING)
484 SetBufferingState_Locked(BUFFERING_HAVE_ENOUGH);
485 return false;
487 return true;
489 return false;
492 void AudioRendererImpl::AttemptRead() {
493 base::AutoLock auto_lock(lock_);
494 AttemptRead_Locked();
497 void AudioRendererImpl::AttemptRead_Locked() {
498 DCHECK(task_runner_->BelongsToCurrentThread());
499 lock_.AssertAcquired();
501 if (!CanRead_Locked())
502 return;
504 pending_read_ = true;
505 audio_buffer_stream_->Read(base::Bind(&AudioRendererImpl::DecodedAudioReady,
506 weak_factory_.GetWeakPtr()));
509 bool AudioRendererImpl::CanRead_Locked() {
510 lock_.AssertAcquired();
512 switch (state_) {
513 case kUninitialized:
514 case kInitializing:
515 case kFlushing:
516 case kFlushed:
517 return false;
519 case kPlaying:
520 break;
523 return !pending_read_ && !received_end_of_stream_ &&
524 !algorithm_->IsQueueFull();
527 void AudioRendererImpl::SetPlaybackRate(float playback_rate) {
528 DVLOG(1) << __FUNCTION__ << "(" << playback_rate << ")";
529 DCHECK(task_runner_->BelongsToCurrentThread());
530 DCHECK_GE(playback_rate, 0);
531 DCHECK(sink_.get());
533 base::AutoLock auto_lock(lock_);
535 // We have two cases here:
536 // Play: current_playback_rate == 0 && playback_rate != 0
537 // Pause: current_playback_rate != 0 && playback_rate == 0
538 float current_playback_rate = playback_rate_;
539 playback_rate_ = playback_rate;
541 if (!rendering_)
542 return;
544 if (current_playback_rate == 0 && playback_rate != 0) {
545 StartRendering_Locked();
546 return;
549 if (current_playback_rate != 0 && playback_rate == 0) {
550 StopRendering_Locked();
551 return;
555 bool AudioRendererImpl::IsBeforeStartTime(
556 const scoped_refptr<AudioBuffer>& buffer) {
557 DCHECK_EQ(state_, kPlaying);
558 return buffer.get() && !buffer->end_of_stream() &&
559 (buffer->timestamp() + buffer->duration()) < start_timestamp_;
562 int AudioRendererImpl::Render(AudioBus* audio_bus,
563 int audio_delay_milliseconds) {
564 const int requested_frames = audio_bus->frames();
565 base::TimeDelta playback_delay = base::TimeDelta::FromMilliseconds(
566 audio_delay_milliseconds);
567 const int delay_frames = static_cast<int>(playback_delay.InSecondsF() *
568 audio_parameters_.sample_rate());
569 int frames_written = 0;
571 base::AutoLock auto_lock(lock_);
572 last_render_ticks_ = base::TimeTicks::Now();
574 // Ensure Stop() hasn't destroyed our |algorithm_| on the pipeline thread.
575 if (!algorithm_) {
576 audio_clock_->WroteAudio(
577 0, requested_frames, delay_frames, playback_rate_);
578 return 0;
581 if (playback_rate_ == 0) {
582 audio_clock_->WroteAudio(
583 0, requested_frames, delay_frames, playback_rate_);
584 return 0;
587 // Mute audio by returning 0 when not playing.
588 if (state_ != kPlaying) {
589 audio_clock_->WroteAudio(
590 0, requested_frames, delay_frames, playback_rate_);
591 return 0;
594 // Delay playback by writing silence if we haven't reached the first
595 // timestamp yet; this can occur if the video starts before the audio.
596 if (algorithm_->frames_buffered() > 0) {
597 DCHECK(first_packet_timestamp_ != kNoTimestamp());
598 const base::TimeDelta play_delay =
599 first_packet_timestamp_ - audio_clock_->back_timestamp();
600 if (play_delay > base::TimeDelta()) {
601 DCHECK_EQ(frames_written, 0);
602 frames_written =
603 std::min(static_cast<int>(play_delay.InSecondsF() *
604 audio_parameters_.sample_rate()),
605 requested_frames);
606 audio_bus->ZeroFramesPartial(0, frames_written);
609 // If there's any space left, actually render the audio; this is where the
610 // aural magic happens.
611 if (frames_written < requested_frames) {
612 frames_written += algorithm_->FillBuffer(
613 audio_bus, frames_written, requested_frames - frames_written,
614 playback_rate_);
618 // We use the following conditions to determine end of playback:
619 // 1) Algorithm can not fill the audio callback buffer
620 // 2) We received an end of stream buffer
621 // 3) We haven't already signalled that we've ended
622 // 4) We've played all known audio data sent to hardware
624 // We use the following conditions to determine underflow:
625 // 1) Algorithm can not fill the audio callback buffer
626 // 2) We have NOT received an end of stream buffer
627 // 3) We are in the kPlaying state
629 // Otherwise the buffer has data we can send to the device.
631 // Per the TimeSource API the media time should always increase even after
632 // we've rendered all known audio data. Doing so simplifies scenarios where
633 // we have other sources of media data that need to be scheduled after audio
634 // data has ended.
636 // That being said, we don't want to advance time when underflowed as we
637 // know more decoded frames will eventually arrive. If we did, we would
638 // throw things out of sync when said decoded frames arrive.
639 int frames_after_end_of_stream = 0;
640 if (frames_written == 0) {
641 if (received_end_of_stream_) {
642 if (ended_timestamp_ == kInfiniteDuration())
643 ended_timestamp_ = audio_clock_->back_timestamp();
644 frames_after_end_of_stream = requested_frames;
645 } else if (state_ == kPlaying &&
646 buffering_state_ != BUFFERING_HAVE_NOTHING) {
647 algorithm_->IncreaseQueueCapacity();
648 SetBufferingState_Locked(BUFFERING_HAVE_NOTHING);
652 audio_clock_->WroteAudio(frames_written + frames_after_end_of_stream,
653 requested_frames,
654 delay_frames,
655 playback_rate_);
657 if (CanRead_Locked()) {
658 task_runner_->PostTask(FROM_HERE,
659 base::Bind(&AudioRendererImpl::AttemptRead,
660 weak_factory_.GetWeakPtr()));
663 if (audio_clock_->front_timestamp() >= ended_timestamp_ &&
664 !rendered_end_of_stream_) {
665 rendered_end_of_stream_ = true;
666 task_runner_->PostTask(FROM_HERE, ended_cb_);
670 DCHECK_LE(frames_written, requested_frames);
671 return frames_written;
674 void AudioRendererImpl::OnRenderError() {
675 // UMA data tells us this happens ~0.01% of the time. Trigger an error instead
676 // of trying to gracefully fall back to a fake sink. It's very likely
677 // OnRenderError() should be removed and the audio stack handle errors without
678 // notifying clients. See http://crbug.com/234708 for details.
679 HistogramRendererEvent(RENDER_ERROR);
680 // Post to |task_runner_| as this is called on the audio callback thread.
681 task_runner_->PostTask(FROM_HERE,
682 base::Bind(error_cb_, PIPELINE_ERROR_DECODE));
685 void AudioRendererImpl::HandleAbortedReadOrDecodeError(bool is_decode_error) {
686 DCHECK(task_runner_->BelongsToCurrentThread());
687 lock_.AssertAcquired();
689 PipelineStatus status = is_decode_error ? PIPELINE_ERROR_DECODE : PIPELINE_OK;
690 switch (state_) {
691 case kUninitialized:
692 case kInitializing:
693 NOTREACHED();
694 return;
695 case kFlushing:
696 ChangeState_Locked(kFlushed);
697 if (status == PIPELINE_OK) {
698 DoFlush_Locked();
699 return;
702 error_cb_.Run(status);
703 base::ResetAndReturn(&flush_cb_).Run();
704 return;
706 case kFlushed:
707 case kPlaying:
708 if (status != PIPELINE_OK)
709 error_cb_.Run(status);
710 return;
714 void AudioRendererImpl::ChangeState_Locked(State new_state) {
715 DVLOG(1) << __FUNCTION__ << " : " << state_ << " -> " << new_state;
716 lock_.AssertAcquired();
717 state_ = new_state;
720 void AudioRendererImpl::OnNewSpliceBuffer(base::TimeDelta splice_timestamp) {
721 DCHECK(task_runner_->BelongsToCurrentThread());
722 splicer_->SetSpliceTimestamp(splice_timestamp);
725 void AudioRendererImpl::OnConfigChange() {
726 DCHECK(task_runner_->BelongsToCurrentThread());
727 DCHECK(expecting_config_changes_);
728 buffer_converter_->ResetTimestampState();
729 // Drain flushed buffers from the converter so the AudioSplicer receives all
730 // data ahead of any OnNewSpliceBuffer() calls. Since discontinuities should
731 // only appear after config changes, AddInput() should never fail here.
732 while (buffer_converter_->HasNextBuffer())
733 CHECK(splicer_->AddInput(buffer_converter_->GetNextBuffer()));
736 void AudioRendererImpl::SetBufferingState_Locked(
737 BufferingState buffering_state) {
738 DVLOG(1) << __FUNCTION__ << " : " << buffering_state_ << " -> "
739 << buffering_state;
740 DCHECK_NE(buffering_state_, buffering_state);
741 lock_.AssertAcquired();
742 buffering_state_ = buffering_state;
744 task_runner_->PostTask(FROM_HERE,
745 base::Bind(buffering_state_cb_, buffering_state_));
748 } // namespace media