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"
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 "base/time/default_tick_clock.h"
18 #include "media/base/audio_buffer.h"
19 #include "media/base/audio_buffer_converter.h"
20 #include "media/base/audio_hardware_config.h"
21 #include "media/base/audio_splicer.h"
22 #include "media/base/bind_to_current_loop.h"
23 #include "media/base/demuxer_stream.h"
24 #include "media/base/media_log.h"
25 #include "media/base/timestamp_constants.h"
26 #include "media/filters/audio_clock.h"
27 #include "media/filters/decrypting_demuxer_stream.h"
33 enum AudioRendererEvent
{
36 RENDER_EVENT_MAX
= RENDER_ERROR
,
39 void HistogramRendererEvent(AudioRendererEvent event
) {
40 UMA_HISTOGRAM_ENUMERATION(
41 "Media.AudioRendererEvents", event
, RENDER_EVENT_MAX
+ 1);
46 AudioRendererImpl::AudioRendererImpl(
47 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
48 media::AudioRendererSink
* sink
,
49 ScopedVector
<AudioDecoder
> decoders
,
50 const AudioHardwareConfig
& hardware_config
,
51 const scoped_refptr
<MediaLog
>& media_log
)
52 : task_runner_(task_runner
),
53 expecting_config_changes_(false),
56 new AudioBufferStream(task_runner
, decoders
.Pass(), media_log
)),
57 hardware_config_(hardware_config
),
58 media_log_(media_log
),
59 tick_clock_(new base::DefaultTickClock()),
61 state_(kUninitialized
),
62 buffering_state_(BUFFERING_HAVE_NOTHING
),
66 received_end_of_stream_(false),
67 rendered_end_of_stream_(false),
69 audio_buffer_stream_
->set_splice_observer(base::Bind(
70 &AudioRendererImpl::OnNewSpliceBuffer
, weak_factory_
.GetWeakPtr()));
71 audio_buffer_stream_
->set_config_change_observer(base::Bind(
72 &AudioRendererImpl::OnConfigChange
, weak_factory_
.GetWeakPtr()));
75 AudioRendererImpl::~AudioRendererImpl() {
76 DVLOG(1) << __FUNCTION__
;
77 DCHECK(task_runner_
->BelongsToCurrentThread());
79 // If Render() is in progress, this call will wait for Render() to finish.
80 // After this call, the |sink_| will not call back into |this| anymore.
83 if (!init_cb_
.is_null())
84 base::ResetAndReturn(&init_cb_
).Run(PIPELINE_ERROR_ABORT
);
87 void AudioRendererImpl::StartTicking() {
88 DVLOG(1) << __FUNCTION__
;
89 DCHECK(task_runner_
->BelongsToCurrentThread());
93 base::AutoLock
auto_lock(lock_
);
94 // Wait for an eventual call to SetPlaybackRate() to start rendering.
95 if (playback_rate_
== 0) {
96 DCHECK(!sink_playing_
);
100 StartRendering_Locked();
103 void AudioRendererImpl::StartRendering_Locked() {
104 DVLOG(1) << __FUNCTION__
;
105 DCHECK(task_runner_
->BelongsToCurrentThread());
106 DCHECK_EQ(state_
, kPlaying
);
107 DCHECK(!sink_playing_
);
108 DCHECK_NE(playback_rate_
, 0.0);
109 lock_
.AssertAcquired();
111 sink_playing_
= true;
113 base::AutoUnlock
auto_unlock(lock_
);
117 void AudioRendererImpl::StopTicking() {
118 DVLOG(1) << __FUNCTION__
;
119 DCHECK(task_runner_
->BelongsToCurrentThread());
123 base::AutoLock
auto_lock(lock_
);
124 // Rendering should have already been stopped with a zero playback rate.
125 if (playback_rate_
== 0) {
126 DCHECK(!sink_playing_
);
130 StopRendering_Locked();
133 void AudioRendererImpl::StopRendering_Locked() {
134 DCHECK(task_runner_
->BelongsToCurrentThread());
135 DCHECK_EQ(state_
, kPlaying
);
136 DCHECK(sink_playing_
);
137 lock_
.AssertAcquired();
139 sink_playing_
= false;
141 base::AutoUnlock
auto_unlock(lock_
);
143 stop_rendering_time_
= last_render_time_
;
146 void AudioRendererImpl::SetMediaTime(base::TimeDelta time
) {
147 DVLOG(1) << __FUNCTION__
<< "(" << time
<< ")";
148 DCHECK(task_runner_
->BelongsToCurrentThread());
150 base::AutoLock
auto_lock(lock_
);
152 DCHECK_EQ(state_
, kFlushed
);
154 start_timestamp_
= time
;
155 ended_timestamp_
= kInfiniteDuration();
156 last_render_time_
= stop_rendering_time_
= base::TimeTicks();
157 first_packet_timestamp_
= kNoTimestamp();
158 audio_clock_
.reset(new AudioClock(time
, audio_parameters_
.sample_rate()));
161 base::TimeDelta
AudioRendererImpl::CurrentMediaTime() {
162 // In practice the Render() method is called with a high enough frequency
163 // that returning only the front timestamp is good enough and also prevents
164 // returning values that go backwards in time.
165 base::TimeDelta current_media_time
;
167 base::AutoLock
auto_lock(lock_
);
168 current_media_time
= audio_clock_
->front_timestamp();
171 DVLOG(2) << __FUNCTION__
<< ": " << current_media_time
;
172 return current_media_time
;
175 bool AudioRendererImpl::GetWallClockTimes(
176 const std::vector
<base::TimeDelta
>& media_timestamps
,
177 std::vector
<base::TimeTicks
>* wall_clock_times
) {
178 base::AutoLock
auto_lock(lock_
);
179 DCHECK(wall_clock_times
->empty());
181 // When playback is paused (rate is zero), assume a rate of 1.0.
182 const double playback_rate
= playback_rate_
? playback_rate_
: 1.0;
183 const bool is_time_moving
= sink_playing_
&& playback_rate_
&&
184 !last_render_time_
.is_null() &&
185 stop_rendering_time_
.is_null();
187 // Pre-compute the time until playback of the audio buffer extents, since
188 // these values are frequently used below.
189 const base::TimeDelta time_until_front
=
190 audio_clock_
->TimeUntilPlayback(audio_clock_
->front_timestamp());
191 const base::TimeDelta time_until_back
=
192 audio_clock_
->TimeUntilPlayback(audio_clock_
->back_timestamp());
194 if (media_timestamps
.empty()) {
195 // Return the current media time as a wall clock time while accounting for
196 // frames which may be in the process of play out.
197 wall_clock_times
->push_back(std::min(
198 std::max(tick_clock_
->NowTicks(), last_render_time_
+ time_until_front
),
199 last_render_time_
+ time_until_back
));
200 return is_time_moving
;
203 wall_clock_times
->reserve(media_timestamps
.size());
204 for (const auto& media_timestamp
: media_timestamps
) {
205 // When time was or is moving and the requested media timestamp is within
206 // range of played out audio, we can provide an exact conversion.
207 if (!last_render_time_
.is_null() &&
208 media_timestamp
>= audio_clock_
->front_timestamp() &&
209 media_timestamp
<= audio_clock_
->back_timestamp()) {
210 wall_clock_times
->push_back(
211 last_render_time_
+ audio_clock_
->TimeUntilPlayback(media_timestamp
));
215 base::TimeDelta base_timestamp
, time_until_playback
;
216 if (media_timestamp
< audio_clock_
->front_timestamp()) {
217 base_timestamp
= audio_clock_
->front_timestamp();
218 time_until_playback
= time_until_front
;
220 base_timestamp
= audio_clock_
->back_timestamp();
221 time_until_playback
= time_until_back
;
224 // In practice, most calls will be estimates given the relatively small
225 // window in which clients can get the actual time.
226 wall_clock_times
->push_back(last_render_time_
+ time_until_playback
+
227 (media_timestamp
- base_timestamp
) /
231 return is_time_moving
;
234 TimeSource
* AudioRendererImpl::GetTimeSource() {
238 void AudioRendererImpl::Flush(const base::Closure
& callback
) {
239 DVLOG(1) << __FUNCTION__
;
240 DCHECK(task_runner_
->BelongsToCurrentThread());
242 base::AutoLock
auto_lock(lock_
);
243 DCHECK_EQ(state_
, kPlaying
);
244 DCHECK(flush_cb_
.is_null());
246 flush_cb_
= callback
;
247 ChangeState_Locked(kFlushing
);
252 ChangeState_Locked(kFlushed
);
256 void AudioRendererImpl::DoFlush_Locked() {
257 DCHECK(task_runner_
->BelongsToCurrentThread());
258 lock_
.AssertAcquired();
260 DCHECK(!pending_read_
);
261 DCHECK_EQ(state_
, kFlushed
);
263 audio_buffer_stream_
->Reset(base::Bind(&AudioRendererImpl::ResetDecoderDone
,
264 weak_factory_
.GetWeakPtr()));
267 void AudioRendererImpl::ResetDecoderDone() {
268 DCHECK(task_runner_
->BelongsToCurrentThread());
270 base::AutoLock
auto_lock(lock_
);
272 DCHECK_EQ(state_
, kFlushed
);
273 DCHECK(!flush_cb_
.is_null());
275 received_end_of_stream_
= false;
276 rendered_end_of_stream_
= false;
278 // Flush() may have been called while underflowed/not fully buffered.
279 if (buffering_state_
!= BUFFERING_HAVE_NOTHING
)
280 SetBufferingState_Locked(BUFFERING_HAVE_NOTHING
);
283 if (buffer_converter_
)
284 buffer_converter_
->Reset();
285 algorithm_
->FlushBuffers();
288 // Changes in buffering state are always posted. Flush callback must only be
289 // run after buffering state has been set back to nothing.
290 task_runner_
->PostTask(FROM_HERE
, base::ResetAndReturn(&flush_cb_
));
293 void AudioRendererImpl::StartPlaying() {
294 DVLOG(1) << __FUNCTION__
;
295 DCHECK(task_runner_
->BelongsToCurrentThread());
297 base::AutoLock
auto_lock(lock_
);
298 DCHECK(!sink_playing_
);
299 DCHECK_EQ(state_
, kFlushed
);
300 DCHECK_EQ(buffering_state_
, BUFFERING_HAVE_NOTHING
);
301 DCHECK(!pending_read_
) << "Pending read must complete before seeking";
303 ChangeState_Locked(kPlaying
);
304 AttemptRead_Locked();
307 void AudioRendererImpl::Initialize(
308 DemuxerStream
* stream
,
309 const PipelineStatusCB
& init_cb
,
310 const SetDecryptorReadyCB
& set_decryptor_ready_cb
,
311 const StatisticsCB
& statistics_cb
,
312 const BufferingStateCB
& buffering_state_cb
,
313 const base::Closure
& ended_cb
,
314 const PipelineStatusCB
& error_cb
,
315 const base::Closure
& waiting_for_decryption_key_cb
) {
316 DVLOG(1) << __FUNCTION__
;
317 DCHECK(task_runner_
->BelongsToCurrentThread());
319 DCHECK_EQ(stream
->type(), DemuxerStream::AUDIO
);
320 DCHECK(!init_cb
.is_null());
321 DCHECK(!statistics_cb
.is_null());
322 DCHECK(!buffering_state_cb
.is_null());
323 DCHECK(!ended_cb
.is_null());
324 DCHECK(!error_cb
.is_null());
325 DCHECK_EQ(kUninitialized
, state_
);
328 state_
= kInitializing
;
330 // Always post |init_cb_| because |this| could be destroyed if initialization
332 init_cb_
= BindToCurrentLoop(init_cb
);
334 buffering_state_cb_
= buffering_state_cb
;
335 ended_cb_
= ended_cb
;
336 error_cb_
= error_cb
;
338 const AudioParameters
& hw_params
= hardware_config_
.GetOutputConfig();
339 expecting_config_changes_
= stream
->SupportsConfigChanges();
340 if (!expecting_config_changes_
|| !hw_params
.IsValid()) {
341 // The actual buffer size is controlled via the size of the AudioBus
342 // provided to Render(), so just choose something reasonable here for looks.
343 int buffer_size
= stream
->audio_decoder_config().samples_per_second() / 100;
344 audio_parameters_
.Reset(
345 AudioParameters::AUDIO_PCM_LOW_LATENCY
,
346 stream
->audio_decoder_config().channel_layout(),
347 stream
->audio_decoder_config().samples_per_second(),
348 stream
->audio_decoder_config().bits_per_channel(),
350 buffer_converter_
.reset();
352 audio_parameters_
.Reset(
354 // Always use the source's channel layout to avoid premature downmixing
355 // (http://crbug.com/379288), platform specific issues around channel
356 // layouts (http://crbug.com/266674), and unnecessary upmixing overhead.
357 stream
->audio_decoder_config().channel_layout(),
358 hw_params
.sample_rate(), hw_params
.bits_per_sample(),
359 hardware_config_
.GetHighLatencyBufferSize());
363 new AudioClock(base::TimeDelta(), audio_parameters_
.sample_rate()));
365 audio_buffer_stream_
->Initialize(
366 stream
, base::Bind(&AudioRendererImpl::OnAudioBufferStreamInitialized
,
367 weak_factory_
.GetWeakPtr()),
368 set_decryptor_ready_cb
, statistics_cb
, waiting_for_decryption_key_cb
);
371 void AudioRendererImpl::OnAudioBufferStreamInitialized(bool success
) {
372 DVLOG(1) << __FUNCTION__
<< ": " << success
;
373 DCHECK(task_runner_
->BelongsToCurrentThread());
375 base::AutoLock
auto_lock(lock_
);
378 state_
= kUninitialized
;
379 base::ResetAndReturn(&init_cb_
).Run(DECODER_ERROR_NOT_SUPPORTED
);
383 if (!audio_parameters_
.IsValid()) {
384 DVLOG(1) << __FUNCTION__
<< ": Invalid audio parameters: "
385 << audio_parameters_
.AsHumanReadableString();
386 ChangeState_Locked(kUninitialized
);
387 base::ResetAndReturn(&init_cb_
).Run(PIPELINE_ERROR_INITIALIZATION_FAILED
);
391 if (expecting_config_changes_
)
392 buffer_converter_
.reset(new AudioBufferConverter(audio_parameters_
));
393 splicer_
.reset(new AudioSplicer(audio_parameters_
.sample_rate(), media_log_
));
395 // We're all good! Continue initializing the rest of the audio renderer
396 // based on the decoder format.
397 algorithm_
.reset(new AudioRendererAlgorithm());
398 algorithm_
->Initialize(audio_parameters_
);
400 ChangeState_Locked(kFlushed
);
402 HistogramRendererEvent(INITIALIZED
);
405 base::AutoUnlock
auto_unlock(lock_
);
406 sink_
->Initialize(audio_parameters_
, this);
409 // Some sinks play on start...
413 DCHECK(!sink_playing_
);
414 base::ResetAndReturn(&init_cb_
).Run(PIPELINE_OK
);
417 void AudioRendererImpl::SetVolume(float volume
) {
418 DCHECK(task_runner_
->BelongsToCurrentThread());
420 sink_
->SetVolume(volume
);
423 void AudioRendererImpl::DecodedAudioReady(
424 AudioBufferStream::Status status
,
425 const scoped_refptr
<AudioBuffer
>& buffer
) {
426 DVLOG(2) << __FUNCTION__
<< "(" << status
<< ")";
427 DCHECK(task_runner_
->BelongsToCurrentThread());
429 base::AutoLock
auto_lock(lock_
);
430 DCHECK(state_
!= kUninitialized
);
432 CHECK(pending_read_
);
433 pending_read_
= false;
435 if (status
== AudioBufferStream::ABORTED
||
436 status
== AudioBufferStream::DEMUXER_READ_ABORTED
) {
437 HandleAbortedReadOrDecodeError(false);
441 if (status
== AudioBufferStream::DECODE_ERROR
) {
442 HandleAbortedReadOrDecodeError(true);
446 DCHECK_EQ(status
, AudioBufferStream::OK
);
447 DCHECK(buffer
.get());
449 if (state_
== kFlushing
) {
450 ChangeState_Locked(kFlushed
);
455 if (expecting_config_changes_
) {
456 DCHECK(buffer_converter_
);
457 buffer_converter_
->AddInput(buffer
);
458 while (buffer_converter_
->HasNextBuffer()) {
459 if (!splicer_
->AddInput(buffer_converter_
->GetNextBuffer())) {
460 HandleAbortedReadOrDecodeError(true);
465 if (!splicer_
->AddInput(buffer
)) {
466 HandleAbortedReadOrDecodeError(true);
471 if (!splicer_
->HasNextBuffer()) {
472 AttemptRead_Locked();
476 bool need_another_buffer
= false;
477 while (splicer_
->HasNextBuffer())
478 need_another_buffer
= HandleSplicerBuffer_Locked(splicer_
->GetNextBuffer());
480 if (!need_another_buffer
&& !CanRead_Locked())
483 AttemptRead_Locked();
486 bool AudioRendererImpl::HandleSplicerBuffer_Locked(
487 const scoped_refptr
<AudioBuffer
>& buffer
) {
488 lock_
.AssertAcquired();
489 if (buffer
->end_of_stream()) {
490 received_end_of_stream_
= true;
492 if (state_
== kPlaying
) {
493 if (IsBeforeStartTime(buffer
))
496 // Trim off any additional time before the start timestamp.
497 const base::TimeDelta trim_time
= start_timestamp_
- buffer
->timestamp();
498 if (trim_time
> base::TimeDelta()) {
499 buffer
->TrimStart(buffer
->frame_count() *
500 (static_cast<double>(trim_time
.InMicroseconds()) /
501 buffer
->duration().InMicroseconds()));
503 // If the entire buffer was trimmed, request a new one.
504 if (!buffer
->frame_count())
508 if (state_
!= kUninitialized
)
509 algorithm_
->EnqueueBuffer(buffer
);
512 // Store the timestamp of the first packet so we know when to start actual
514 if (first_packet_timestamp_
== kNoTimestamp())
515 first_packet_timestamp_
= buffer
->timestamp();
525 DCHECK(!pending_read_
);
529 if (buffer
->end_of_stream() || algorithm_
->IsQueueFull()) {
530 if (buffering_state_
== BUFFERING_HAVE_NOTHING
)
531 SetBufferingState_Locked(BUFFERING_HAVE_ENOUGH
);
539 void AudioRendererImpl::AttemptRead() {
540 base::AutoLock
auto_lock(lock_
);
541 AttemptRead_Locked();
544 void AudioRendererImpl::AttemptRead_Locked() {
545 DCHECK(task_runner_
->BelongsToCurrentThread());
546 lock_
.AssertAcquired();
548 if (!CanRead_Locked())
551 pending_read_
= true;
552 audio_buffer_stream_
->Read(base::Bind(&AudioRendererImpl::DecodedAudioReady
,
553 weak_factory_
.GetWeakPtr()));
556 bool AudioRendererImpl::CanRead_Locked() {
557 lock_
.AssertAcquired();
570 return !pending_read_
&& !received_end_of_stream_
&&
571 !algorithm_
->IsQueueFull();
574 void AudioRendererImpl::SetPlaybackRate(double playback_rate
) {
575 DVLOG(1) << __FUNCTION__
<< "(" << playback_rate
<< ")";
576 DCHECK(task_runner_
->BelongsToCurrentThread());
577 DCHECK_GE(playback_rate
, 0);
580 base::AutoLock
auto_lock(lock_
);
582 // We have two cases here:
583 // Play: current_playback_rate == 0 && playback_rate != 0
584 // Pause: current_playback_rate != 0 && playback_rate == 0
585 double current_playback_rate
= playback_rate_
;
586 playback_rate_
= playback_rate
;
591 if (current_playback_rate
== 0 && playback_rate
!= 0) {
592 StartRendering_Locked();
596 if (current_playback_rate
!= 0 && playback_rate
== 0) {
597 StopRendering_Locked();
602 bool AudioRendererImpl::IsBeforeStartTime(
603 const scoped_refptr
<AudioBuffer
>& buffer
) {
604 DCHECK_EQ(state_
, kPlaying
);
605 return buffer
.get() && !buffer
->end_of_stream() &&
606 (buffer
->timestamp() + buffer
->duration()) < start_timestamp_
;
609 int AudioRendererImpl::Render(AudioBus
* audio_bus
,
610 int audio_delay_milliseconds
) {
611 const int requested_frames
= audio_bus
->frames();
612 base::TimeDelta playback_delay
= base::TimeDelta::FromMilliseconds(
613 audio_delay_milliseconds
);
614 const int delay_frames
= static_cast<int>(playback_delay
.InSecondsF() *
615 audio_parameters_
.sample_rate());
616 int frames_written
= 0;
618 base::AutoLock
auto_lock(lock_
);
619 last_render_time_
= tick_clock_
->NowTicks();
621 if (!stop_rendering_time_
.is_null()) {
622 audio_clock_
->CompensateForSuspendedWrites(
623 last_render_time_
- stop_rendering_time_
, delay_frames
);
624 stop_rendering_time_
= base::TimeTicks();
627 // Ensure Stop() hasn't destroyed our |algorithm_| on the pipeline thread.
629 audio_clock_
->WroteAudio(
630 0, requested_frames
, delay_frames
, playback_rate_
);
634 if (playback_rate_
== 0) {
635 audio_clock_
->WroteAudio(
636 0, requested_frames
, delay_frames
, playback_rate_
);
640 // Mute audio by returning 0 when not playing.
641 if (state_
!= kPlaying
) {
642 audio_clock_
->WroteAudio(
643 0, requested_frames
, delay_frames
, playback_rate_
);
647 // Delay playback by writing silence if we haven't reached the first
648 // timestamp yet; this can occur if the video starts before the audio.
649 if (algorithm_
->frames_buffered() > 0) {
650 DCHECK(first_packet_timestamp_
!= kNoTimestamp());
651 const base::TimeDelta play_delay
=
652 first_packet_timestamp_
- audio_clock_
->back_timestamp();
653 if (play_delay
> base::TimeDelta()) {
654 DCHECK_EQ(frames_written
, 0);
656 std::min(static_cast<int>(play_delay
.InSecondsF() *
657 audio_parameters_
.sample_rate()),
659 audio_bus
->ZeroFramesPartial(0, frames_written
);
662 // If there's any space left, actually render the audio; this is where the
663 // aural magic happens.
664 if (frames_written
< requested_frames
) {
665 frames_written
+= algorithm_
->FillBuffer(
666 audio_bus
, frames_written
, requested_frames
- frames_written
,
671 // We use the following conditions to determine end of playback:
672 // 1) Algorithm can not fill the audio callback buffer
673 // 2) We received an end of stream buffer
674 // 3) We haven't already signalled that we've ended
675 // 4) We've played all known audio data sent to hardware
677 // We use the following conditions to determine underflow:
678 // 1) Algorithm can not fill the audio callback buffer
679 // 2) We have NOT received an end of stream buffer
680 // 3) We are in the kPlaying state
682 // Otherwise the buffer has data we can send to the device.
684 // Per the TimeSource API the media time should always increase even after
685 // we've rendered all known audio data. Doing so simplifies scenarios where
686 // we have other sources of media data that need to be scheduled after audio
689 // That being said, we don't want to advance time when underflowed as we
690 // know more decoded frames will eventually arrive. If we did, we would
691 // throw things out of sync when said decoded frames arrive.
692 int frames_after_end_of_stream
= 0;
693 if (frames_written
== 0) {
694 if (received_end_of_stream_
) {
695 if (ended_timestamp_
== kInfiniteDuration())
696 ended_timestamp_
= audio_clock_
->back_timestamp();
697 frames_after_end_of_stream
= requested_frames
;
698 } else if (state_
== kPlaying
&&
699 buffering_state_
!= BUFFERING_HAVE_NOTHING
) {
700 algorithm_
->IncreaseQueueCapacity();
701 SetBufferingState_Locked(BUFFERING_HAVE_NOTHING
);
705 audio_clock_
->WroteAudio(frames_written
+ frames_after_end_of_stream
,
710 if (CanRead_Locked()) {
711 task_runner_
->PostTask(FROM_HERE
,
712 base::Bind(&AudioRendererImpl::AttemptRead
,
713 weak_factory_
.GetWeakPtr()));
716 if (audio_clock_
->front_timestamp() >= ended_timestamp_
&&
717 !rendered_end_of_stream_
) {
718 rendered_end_of_stream_
= true;
719 task_runner_
->PostTask(FROM_HERE
, ended_cb_
);
723 DCHECK_LE(frames_written
, requested_frames
);
724 return frames_written
;
727 void AudioRendererImpl::OnRenderError() {
728 // UMA data tells us this happens ~0.01% of the time. Trigger an error instead
729 // of trying to gracefully fall back to a fake sink. It's very likely
730 // OnRenderError() should be removed and the audio stack handle errors without
731 // notifying clients. See http://crbug.com/234708 for details.
732 HistogramRendererEvent(RENDER_ERROR
);
734 MEDIA_LOG(ERROR
, media_log_
) << "audio render error";
736 // Post to |task_runner_| as this is called on the audio callback thread.
737 task_runner_
->PostTask(FROM_HERE
,
738 base::Bind(error_cb_
, PIPELINE_ERROR_DECODE
));
741 void AudioRendererImpl::HandleAbortedReadOrDecodeError(bool is_decode_error
) {
742 DCHECK(task_runner_
->BelongsToCurrentThread());
743 lock_
.AssertAcquired();
745 PipelineStatus status
= is_decode_error
? PIPELINE_ERROR_DECODE
: PIPELINE_OK
;
752 ChangeState_Locked(kFlushed
);
753 if (status
== PIPELINE_OK
) {
758 MEDIA_LOG(ERROR
, media_log_
) << "audio decode error during flushing";
759 error_cb_
.Run(status
);
760 base::ResetAndReturn(&flush_cb_
).Run();
765 if (status
!= PIPELINE_OK
) {
766 MEDIA_LOG(ERROR
, media_log_
) << "audio decode error during playing";
767 error_cb_
.Run(status
);
773 void AudioRendererImpl::ChangeState_Locked(State new_state
) {
774 DVLOG(1) << __FUNCTION__
<< " : " << state_
<< " -> " << new_state
;
775 lock_
.AssertAcquired();
779 void AudioRendererImpl::OnNewSpliceBuffer(base::TimeDelta splice_timestamp
) {
780 DCHECK(task_runner_
->BelongsToCurrentThread());
781 splicer_
->SetSpliceTimestamp(splice_timestamp
);
784 void AudioRendererImpl::OnConfigChange() {
785 DCHECK(task_runner_
->BelongsToCurrentThread());
786 DCHECK(expecting_config_changes_
);
787 buffer_converter_
->ResetTimestampState();
788 // Drain flushed buffers from the converter so the AudioSplicer receives all
789 // data ahead of any OnNewSpliceBuffer() calls. Since discontinuities should
790 // only appear after config changes, AddInput() should never fail here.
791 while (buffer_converter_
->HasNextBuffer())
792 CHECK(splicer_
->AddInput(buffer_converter_
->GetNextBuffer()));
795 void AudioRendererImpl::SetBufferingState_Locked(
796 BufferingState buffering_state
) {
797 DVLOG(1) << __FUNCTION__
<< " : " << buffering_state_
<< " -> "
799 DCHECK_NE(buffering_state_
, buffering_state
);
800 lock_
.AssertAcquired();
801 buffering_state_
= buffering_state
;
803 task_runner_
->PostTask(FROM_HERE
,
804 base::Bind(buffering_state_cb_
, buffering_state_
));