1 // Copyright 2014 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/renderer_impl.h"
8 #include "base/callback.h"
9 #include "base/callback_helpers.h"
10 #include "base/command_line.h"
11 #include "base/compiler_specific.h"
12 #include "base/location.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "media/base/audio_renderer.h"
16 #include "media/base/bind_to_current_loop.h"
17 #include "media/base/demuxer_stream_provider.h"
18 #include "media/base/media_switches.h"
19 #include "media/base/time_source.h"
20 #include "media/base/video_renderer.h"
21 #include "media/base/wall_clock_time_source.h"
25 // See |video_underflow_threshold_|.
26 static const int kDefaultVideoUnderflowThresholdMs
= 3000;
28 RendererImpl::RendererImpl(
29 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
30 scoped_ptr
<AudioRenderer
> audio_renderer
,
31 scoped_ptr
<VideoRenderer
> video_renderer
)
32 : state_(STATE_UNINITIALIZED
),
33 task_runner_(task_runner
),
34 audio_renderer_(audio_renderer
.Pass()),
35 video_renderer_(video_renderer
.Pass()),
39 audio_buffering_state_(BUFFERING_HAVE_NOTHING
),
40 video_buffering_state_(BUFFERING_HAVE_NOTHING
),
43 cdm_context_(nullptr),
44 underflow_disabled_for_testing_(false),
45 clockless_video_playback_enabled_for_testing_(false),
46 video_underflow_threshold_(
47 base::TimeDelta::FromMilliseconds(kDefaultVideoUnderflowThresholdMs
)),
49 weak_this_
= weak_factory_
.GetWeakPtr();
50 DVLOG(1) << __FUNCTION__
;
52 // TODO(dalecurtis): Remove once experiments for http://crbug.com/470940 are
55 std::string
threshold_ms_str(
56 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
57 switches::kVideoUnderflowThresholdMs
));
58 if (base::StringToInt(threshold_ms_str
, &threshold_ms
) && threshold_ms
> 0) {
59 video_underflow_threshold_
=
60 base::TimeDelta::FromMilliseconds(threshold_ms
);
64 RendererImpl::~RendererImpl() {
65 DVLOG(1) << __FUNCTION__
;
66 DCHECK(task_runner_
->BelongsToCurrentThread());
68 // Tear down in opposite order of construction as |video_renderer_| can still
69 // need |time_source_| (which can be |audio_renderer_|) to be alive.
70 video_renderer_
.reset();
71 audio_renderer_
.reset();
73 if (!init_cb_
.is_null())
74 base::ResetAndReturn(&init_cb_
).Run(PIPELINE_ERROR_ABORT
);
75 else if (!flush_cb_
.is_null())
76 base::ResetAndReturn(&flush_cb_
).Run();
79 void RendererImpl::Initialize(
80 DemuxerStreamProvider
* demuxer_stream_provider
,
81 const PipelineStatusCB
& init_cb
,
82 const StatisticsCB
& statistics_cb
,
83 const BufferingStateCB
& buffering_state_cb
,
84 const base::Closure
& ended_cb
,
85 const PipelineStatusCB
& error_cb
,
86 const base::Closure
& waiting_for_decryption_key_cb
) {
87 DVLOG(1) << __FUNCTION__
;
88 DCHECK(task_runner_
->BelongsToCurrentThread());
89 DCHECK_EQ(state_
, STATE_UNINITIALIZED
);
90 DCHECK(!init_cb
.is_null());
91 DCHECK(!statistics_cb
.is_null());
92 DCHECK(!buffering_state_cb
.is_null());
93 DCHECK(!ended_cb
.is_null());
94 DCHECK(!error_cb
.is_null());
95 DCHECK(demuxer_stream_provider
->GetStream(DemuxerStream::AUDIO
) ||
96 demuxer_stream_provider
->GetStream(DemuxerStream::VIDEO
));
98 demuxer_stream_provider_
= demuxer_stream_provider
;
99 statistics_cb_
= statistics_cb
;
100 buffering_state_cb_
= buffering_state_cb
;
101 ended_cb_
= ended_cb
;
102 error_cb_
= error_cb
;
104 waiting_for_decryption_key_cb_
= waiting_for_decryption_key_cb
;
106 state_
= STATE_INITIALIZING
;
107 InitializeAudioRenderer();
110 void RendererImpl::SetCdm(CdmContext
* cdm_context
,
111 const CdmAttachedCB
& cdm_attached_cb
) {
112 DVLOG(1) << __FUNCTION__
;
113 DCHECK(task_runner_
->BelongsToCurrentThread());
117 DVLOG(1) << "Switching CDM not supported.";
118 cdm_attached_cb
.Run(false);
122 cdm_context_
= cdm_context
;
124 if (decryptor_ready_cb_
.is_null()) {
125 cdm_attached_cb
.Run(true);
129 base::ResetAndReturn(&decryptor_ready_cb_
)
130 .Run(cdm_context
->GetDecryptor(), cdm_attached_cb
);
133 void RendererImpl::Flush(const base::Closure
& flush_cb
) {
134 DVLOG(1) << __FUNCTION__
;
135 DCHECK(task_runner_
->BelongsToCurrentThread());
136 DCHECK(flush_cb_
.is_null());
138 if (state_
!= STATE_PLAYING
) {
139 DCHECK_EQ(state_
, STATE_ERROR
);
143 flush_cb_
= flush_cb
;
144 state_
= STATE_FLUSHING
;
149 FlushAudioRenderer();
152 void RendererImpl::StartPlayingFrom(base::TimeDelta time
) {
153 DVLOG(1) << __FUNCTION__
;
154 DCHECK(task_runner_
->BelongsToCurrentThread());
156 if (state_
!= STATE_PLAYING
) {
157 DCHECK_EQ(state_
, STATE_ERROR
);
161 time_source_
->SetMediaTime(time
);
164 audio_renderer_
->StartPlaying();
166 video_renderer_
->StartPlayingFrom(time
);
169 void RendererImpl::SetPlaybackRate(double playback_rate
) {
170 DVLOG(1) << __FUNCTION__
<< "(" << playback_rate
<< ")";
171 DCHECK(task_runner_
->BelongsToCurrentThread());
173 // Playback rate changes are only carried out while playing.
174 if (state_
!= STATE_PLAYING
)
177 time_source_
->SetPlaybackRate(playback_rate
);
179 const double old_rate
= playback_rate_
;
180 playback_rate_
= playback_rate
;
181 if (!time_ticking_
|| !video_renderer_
)
184 if (old_rate
== 0 && playback_rate
> 0)
185 video_renderer_
->OnTimeStateChanged(true);
186 else if (old_rate
> 0 && playback_rate
== 0)
187 video_renderer_
->OnTimeStateChanged(false);
190 void RendererImpl::SetVolume(float volume
) {
191 DVLOG(1) << __FUNCTION__
;
192 DCHECK(task_runner_
->BelongsToCurrentThread());
195 audio_renderer_
->SetVolume(volume
);
198 base::TimeDelta
RendererImpl::GetMediaTime() {
199 // No BelongsToCurrentThread() checking because this can be called from other
201 return time_source_
->CurrentMediaTime();
204 bool RendererImpl::HasAudio() {
205 DCHECK(task_runner_
->BelongsToCurrentThread());
206 return audio_renderer_
!= NULL
;
209 bool RendererImpl::HasVideo() {
210 DCHECK(task_runner_
->BelongsToCurrentThread());
211 return video_renderer_
!= NULL
;
214 void RendererImpl::DisableUnderflowForTesting() {
215 DVLOG(1) << __FUNCTION__
;
216 DCHECK(task_runner_
->BelongsToCurrentThread());
217 DCHECK_EQ(state_
, STATE_UNINITIALIZED
);
219 underflow_disabled_for_testing_
= true;
222 void RendererImpl::EnableClocklessVideoPlaybackForTesting() {
223 DVLOG(1) << __FUNCTION__
;
224 DCHECK(task_runner_
->BelongsToCurrentThread());
225 DCHECK_EQ(state_
, STATE_UNINITIALIZED
);
226 DCHECK(underflow_disabled_for_testing_
)
227 << "Underflow must be disabled for clockless video playback";
229 clockless_video_playback_enabled_for_testing_
= true;
232 base::TimeTicks
RendererImpl::GetWallClockTime(base::TimeDelta time
) {
233 // No BelongsToCurrentThread() checking because this can be called from other
236 // TODO(scherkus): Currently called from VideoRendererImpl's internal thread,
237 // which should go away at some point http://crbug.com/110814
238 if (clockless_video_playback_enabled_for_testing_
)
239 return base::TimeTicks::Now();
241 return time_source_
->GetWallClockTime(time
);
244 void RendererImpl::SetDecryptorReadyCallback(
245 const DecryptorReadyCB
& decryptor_ready_cb
) {
246 // Cancels the previous decryptor request.
247 if (decryptor_ready_cb
.is_null()) {
248 if (!decryptor_ready_cb_
.is_null()) {
249 base::ResetAndReturn(&decryptor_ready_cb_
)
250 .Run(nullptr, base::Bind(IgnoreCdmAttached
));
255 // We initialize audio and video decoders in sequence.
256 DCHECK(decryptor_ready_cb_
.is_null());
259 decryptor_ready_cb
.Run(cdm_context_
->GetDecryptor(),
260 base::Bind(IgnoreCdmAttached
));
264 decryptor_ready_cb_
= decryptor_ready_cb
;
267 void RendererImpl::InitializeAudioRenderer() {
268 DVLOG(1) << __FUNCTION__
;
269 DCHECK(task_runner_
->BelongsToCurrentThread());
270 DCHECK_EQ(state_
, STATE_INITIALIZING
);
271 DCHECK(!init_cb_
.is_null());
273 PipelineStatusCB done_cb
=
274 base::Bind(&RendererImpl::OnAudioRendererInitializeDone
, weak_this_
);
276 if (!demuxer_stream_provider_
->GetStream(DemuxerStream::AUDIO
)) {
277 audio_renderer_
.reset();
278 task_runner_
->PostTask(FROM_HERE
, base::Bind(done_cb
, PIPELINE_OK
));
282 // Note: After the initialization of a renderer, error events from it may
283 // happen at any time and all future calls must guard against STATE_ERROR.
284 audio_renderer_
->Initialize(
285 demuxer_stream_provider_
->GetStream(DemuxerStream::AUDIO
), done_cb
,
286 base::Bind(&RendererImpl::SetDecryptorReadyCallback
, weak_this_
),
287 base::Bind(&RendererImpl::OnUpdateStatistics
, weak_this_
),
288 base::Bind(&RendererImpl::OnBufferingStateChanged
, weak_this_
,
289 &audio_buffering_state_
),
290 base::Bind(&RendererImpl::OnAudioRendererEnded
, weak_this_
),
291 base::Bind(&RendererImpl::OnError
, weak_this_
),
292 waiting_for_decryption_key_cb_
);
295 void RendererImpl::OnAudioRendererInitializeDone(PipelineStatus status
) {
296 DVLOG(1) << __FUNCTION__
<< ": " << status
;
297 DCHECK(task_runner_
->BelongsToCurrentThread());
299 // OnError() may be fired at any time by the renderers, even if they thought
300 // they initialized successfully (due to delayed output device setup).
301 if (state_
!= STATE_INITIALIZING
) {
302 DCHECK(init_cb_
.is_null());
303 audio_renderer_
.reset();
307 if (status
!= PIPELINE_OK
) {
308 base::ResetAndReturn(&init_cb_
).Run(status
);
312 DCHECK(!init_cb_
.is_null());
313 InitializeVideoRenderer();
316 void RendererImpl::InitializeVideoRenderer() {
317 DVLOG(1) << __FUNCTION__
;
318 DCHECK(task_runner_
->BelongsToCurrentThread());
319 DCHECK_EQ(state_
, STATE_INITIALIZING
);
320 DCHECK(!init_cb_
.is_null());
322 PipelineStatusCB done_cb
=
323 base::Bind(&RendererImpl::OnVideoRendererInitializeDone
, weak_this_
);
325 if (!demuxer_stream_provider_
->GetStream(DemuxerStream::VIDEO
)) {
326 video_renderer_
.reset();
327 task_runner_
->PostTask(FROM_HERE
, base::Bind(done_cb
, PIPELINE_OK
));
331 video_renderer_
->Initialize(
332 demuxer_stream_provider_
->GetStream(DemuxerStream::VIDEO
), done_cb
,
333 base::Bind(&RendererImpl::SetDecryptorReadyCallback
, weak_this_
),
334 base::Bind(&RendererImpl::OnUpdateStatistics
, weak_this_
),
335 base::Bind(&RendererImpl::OnBufferingStateChanged
, weak_this_
,
336 &video_buffering_state_
),
337 base::Bind(&RendererImpl::OnVideoRendererEnded
, weak_this_
),
338 base::Bind(&RendererImpl::OnError
, weak_this_
),
339 base::Bind(&RendererImpl::GetWallClockTime
, base::Unretained(this)),
340 waiting_for_decryption_key_cb_
);
343 void RendererImpl::OnVideoRendererInitializeDone(PipelineStatus status
) {
344 DVLOG(1) << __FUNCTION__
<< ": " << status
;
345 DCHECK(task_runner_
->BelongsToCurrentThread());
347 // OnError() may be fired at any time by the renderers, even if they thought
348 // they initialized successfully (due to delayed output device setup).
349 if (state_
!= STATE_INITIALIZING
) {
350 DCHECK(init_cb_
.is_null());
351 audio_renderer_
.reset();
352 video_renderer_
.reset();
356 DCHECK(!init_cb_
.is_null());
358 if (status
!= PIPELINE_OK
) {
359 base::ResetAndReturn(&init_cb_
).Run(status
);
363 if (audio_renderer_
) {
364 time_source_
= audio_renderer_
->GetTimeSource();
365 } else if (!time_source_
) {
366 wall_clock_time_source_
.reset(new WallClockTimeSource());
367 time_source_
= wall_clock_time_source_
.get();
370 state_
= STATE_PLAYING
;
371 DCHECK(time_source_
);
372 DCHECK(audio_renderer_
|| video_renderer_
);
373 base::ResetAndReturn(&init_cb_
).Run(PIPELINE_OK
);
376 void RendererImpl::FlushAudioRenderer() {
377 DVLOG(1) << __FUNCTION__
;
378 DCHECK(task_runner_
->BelongsToCurrentThread());
379 DCHECK_EQ(state_
, STATE_FLUSHING
);
380 DCHECK(!flush_cb_
.is_null());
382 if (!audio_renderer_
) {
383 OnAudioRendererFlushDone();
387 audio_renderer_
->Flush(
388 base::Bind(&RendererImpl::OnAudioRendererFlushDone
, weak_this_
));
391 void RendererImpl::OnAudioRendererFlushDone() {
392 DVLOG(1) << __FUNCTION__
;
393 DCHECK(task_runner_
->BelongsToCurrentThread());
395 if (state_
== STATE_ERROR
) {
396 DCHECK(flush_cb_
.is_null());
400 DCHECK_EQ(state_
, STATE_FLUSHING
);
401 DCHECK(!flush_cb_
.is_null());
403 DCHECK_EQ(audio_buffering_state_
, BUFFERING_HAVE_NOTHING
);
404 audio_ended_
= false;
405 FlushVideoRenderer();
408 void RendererImpl::FlushVideoRenderer() {
409 DVLOG(1) << __FUNCTION__
;
410 DCHECK(task_runner_
->BelongsToCurrentThread());
411 DCHECK_EQ(state_
, STATE_FLUSHING
);
412 DCHECK(!flush_cb_
.is_null());
414 if (!video_renderer_
) {
415 OnVideoRendererFlushDone();
419 video_renderer_
->Flush(
420 base::Bind(&RendererImpl::OnVideoRendererFlushDone
, weak_this_
));
423 void RendererImpl::OnVideoRendererFlushDone() {
424 DVLOG(1) << __FUNCTION__
;
425 DCHECK(task_runner_
->BelongsToCurrentThread());
427 if (state_
== STATE_ERROR
) {
428 DCHECK(flush_cb_
.is_null());
432 DCHECK_EQ(state_
, STATE_FLUSHING
);
433 DCHECK(!flush_cb_
.is_null());
435 DCHECK_EQ(video_buffering_state_
, BUFFERING_HAVE_NOTHING
);
436 video_ended_
= false;
437 state_
= STATE_PLAYING
;
438 base::ResetAndReturn(&flush_cb_
).Run();
441 void RendererImpl::OnUpdateStatistics(const PipelineStatistics
& stats
) {
442 DCHECK(task_runner_
->BelongsToCurrentThread());
443 statistics_cb_
.Run(stats
);
446 void RendererImpl::OnBufferingStateChanged(BufferingState
* buffering_state
,
447 BufferingState new_buffering_state
) {
448 const bool is_audio
= buffering_state
== &audio_buffering_state_
;
449 DVLOG(1) << __FUNCTION__
<< "(" << *buffering_state
<< ", "
450 << new_buffering_state
<< ") " << (is_audio
? "audio" : "video");
451 DCHECK(task_runner_
->BelongsToCurrentThread());
453 bool was_waiting_for_enough_data
= WaitingForEnoughData();
455 // When audio is present, defer underflow callbacks for some time to avoid
456 // unnecessary glitches in audio; see http://crbug.com/144683#c53.
457 if (audio_renderer_
&& !is_audio
&& state_
== STATE_PLAYING
) {
458 if (video_buffering_state_
== BUFFERING_HAVE_ENOUGH
&&
459 new_buffering_state
== BUFFERING_HAVE_NOTHING
&&
460 deferred_underflow_cb_
.IsCancelled()) {
461 deferred_underflow_cb_
.Reset(base::Bind(
462 &RendererImpl::OnBufferingStateChanged
, weak_factory_
.GetWeakPtr(),
463 buffering_state
, new_buffering_state
));
464 task_runner_
->PostDelayedTask(FROM_HERE
,
465 deferred_underflow_cb_
.callback(),
466 video_underflow_threshold_
);
470 deferred_underflow_cb_
.Cancel();
471 } else if (!deferred_underflow_cb_
.IsCancelled() && is_audio
&&
472 new_buffering_state
== BUFFERING_HAVE_NOTHING
) {
473 // If audio underflows while we have a deferred video underflow in progress
474 // we want to mark video as underflowed immediately and cancel the deferral.
475 deferred_underflow_cb_
.Cancel();
476 video_buffering_state_
= BUFFERING_HAVE_NOTHING
;
479 *buffering_state
= new_buffering_state
;
481 // Disable underflow by ignoring updates that renderers have ran out of data.
482 if (state_
== STATE_PLAYING
&& underflow_disabled_for_testing_
&&
484 DVLOG(1) << "Update ignored because underflow is disabled for testing.";
488 // Renderer underflowed.
489 if (!was_waiting_for_enough_data
&& WaitingForEnoughData()) {
492 // TODO(scherkus): Fire BUFFERING_HAVE_NOTHING callback to alert clients of
493 // underflow state http://crbug.com/144683
497 // Renderer prerolled.
498 if (was_waiting_for_enough_data
&& !WaitingForEnoughData()) {
500 buffering_state_cb_
.Run(BUFFERING_HAVE_ENOUGH
);
505 bool RendererImpl::WaitingForEnoughData() const {
506 DCHECK(task_runner_
->BelongsToCurrentThread());
507 if (state_
!= STATE_PLAYING
)
509 if (audio_renderer_
&& audio_buffering_state_
!= BUFFERING_HAVE_ENOUGH
)
511 if (video_renderer_
&& video_buffering_state_
!= BUFFERING_HAVE_ENOUGH
)
516 void RendererImpl::PausePlayback() {
517 DVLOG(1) << __FUNCTION__
;
518 DCHECK(task_runner_
->BelongsToCurrentThread());
519 DCHECK(time_ticking_
);
522 DCHECK(PlaybackHasEnded() || WaitingForEnoughData())
523 << "Playback should only pause due to ending or underflowing";
527 // It's OK to pause playback when flushing.
530 case STATE_UNINITIALIZED
:
531 case STATE_INITIALIZING
:
532 NOTREACHED() << "Invalid state: " << state_
;
536 // An error state may occur at any time.
540 time_ticking_
= false;
541 time_source_
->StopTicking();
542 if (playback_rate_
> 0 && video_renderer_
)
543 video_renderer_
->OnTimeStateChanged(false);
546 void RendererImpl::StartPlayback() {
547 DVLOG(1) << __FUNCTION__
;
548 DCHECK(task_runner_
->BelongsToCurrentThread());
549 DCHECK_EQ(state_
, STATE_PLAYING
);
550 DCHECK(!time_ticking_
);
551 DCHECK(!WaitingForEnoughData());
553 time_ticking_
= true;
554 time_source_
->StartTicking();
555 if (playback_rate_
> 0 && video_renderer_
)
556 video_renderer_
->OnTimeStateChanged(true);
559 void RendererImpl::OnAudioRendererEnded() {
560 DVLOG(1) << __FUNCTION__
;
561 DCHECK(task_runner_
->BelongsToCurrentThread());
563 if (state_
!= STATE_PLAYING
)
566 DCHECK(!audio_ended_
);
569 RunEndedCallbackIfNeeded();
572 void RendererImpl::OnVideoRendererEnded() {
573 DVLOG(1) << __FUNCTION__
;
574 DCHECK(task_runner_
->BelongsToCurrentThread());
576 if (state_
!= STATE_PLAYING
)
579 DCHECK(!video_ended_
);
582 RunEndedCallbackIfNeeded();
585 bool RendererImpl::PlaybackHasEnded() const {
586 DVLOG(1) << __FUNCTION__
;
587 DCHECK(task_runner_
->BelongsToCurrentThread());
589 if (audio_renderer_
&& !audio_ended_
)
592 if (video_renderer_
&& !video_ended_
)
598 void RendererImpl::RunEndedCallbackIfNeeded() {
599 DVLOG(1) << __FUNCTION__
;
600 DCHECK(task_runner_
->BelongsToCurrentThread());
602 if (!PlaybackHasEnded())
611 void RendererImpl::OnError(PipelineStatus error
) {
612 DVLOG(1) << __FUNCTION__
<< "(" << error
<< ")";
613 DCHECK(task_runner_
->BelongsToCurrentThread());
614 DCHECK_NE(PIPELINE_OK
, error
) << "PIPELINE_OK isn't an error!";
616 // An error has already been delivered.
617 if (state_
== STATE_ERROR
)
620 const State old_state
= state_
;
621 state_
= STATE_ERROR
;
623 if (old_state
== STATE_INITIALIZING
) {
624 base::ResetAndReturn(&init_cb_
).Run(error
);
628 // After OnError() returns, the pipeline may destroy |this|.
629 base::ResetAndReturn(&error_cb_
).Run(error
);
631 if (!flush_cb_
.is_null())
632 base::ResetAndReturn(&flush_cb_
).Run();