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 PaintCB
& paint_cb
,
85 const base::Closure
& ended_cb
,
86 const PipelineStatusCB
& error_cb
,
87 const base::Closure
& waiting_for_decryption_key_cb
) {
88 DVLOG(1) << __FUNCTION__
;
89 DCHECK(task_runner_
->BelongsToCurrentThread());
90 DCHECK_EQ(state_
, STATE_UNINITIALIZED
);
91 DCHECK(!init_cb
.is_null());
92 DCHECK(!statistics_cb
.is_null());
93 DCHECK(!buffering_state_cb
.is_null());
94 DCHECK(!paint_cb
.is_null());
95 DCHECK(!ended_cb
.is_null());
96 DCHECK(!error_cb
.is_null());
97 DCHECK(demuxer_stream_provider
->GetStream(DemuxerStream::AUDIO
) ||
98 demuxer_stream_provider
->GetStream(DemuxerStream::VIDEO
));
100 demuxer_stream_provider_
= demuxer_stream_provider
;
101 statistics_cb_
= statistics_cb
;
102 buffering_state_cb_
= buffering_state_cb
;
103 paint_cb_
= paint_cb
;
104 ended_cb_
= ended_cb
;
105 error_cb_
= error_cb
;
107 waiting_for_decryption_key_cb_
= waiting_for_decryption_key_cb
;
109 state_
= STATE_INITIALIZING
;
110 InitializeAudioRenderer();
113 void RendererImpl::SetCdm(CdmContext
* cdm_context
,
114 const CdmAttachedCB
& cdm_attached_cb
) {
115 DVLOG(1) << __FUNCTION__
;
116 DCHECK(task_runner_
->BelongsToCurrentThread());
120 DVLOG(1) << "Switching CDM not supported.";
121 cdm_attached_cb
.Run(false);
125 cdm_context_
= cdm_context
;
127 if (decryptor_ready_cb_
.is_null()) {
128 cdm_attached_cb
.Run(true);
132 base::ResetAndReturn(&decryptor_ready_cb_
)
133 .Run(cdm_context
->GetDecryptor(), cdm_attached_cb
);
136 void RendererImpl::Flush(const base::Closure
& flush_cb
) {
137 DVLOG(1) << __FUNCTION__
;
138 DCHECK(task_runner_
->BelongsToCurrentThread());
139 DCHECK(flush_cb_
.is_null());
141 if (state_
!= STATE_PLAYING
) {
142 DCHECK_EQ(state_
, STATE_ERROR
);
146 flush_cb_
= flush_cb
;
147 state_
= STATE_FLUSHING
;
152 FlushAudioRenderer();
155 void RendererImpl::StartPlayingFrom(base::TimeDelta time
) {
156 DVLOG(1) << __FUNCTION__
;
157 DCHECK(task_runner_
->BelongsToCurrentThread());
159 if (state_
!= STATE_PLAYING
) {
160 DCHECK_EQ(state_
, STATE_ERROR
);
164 time_source_
->SetMediaTime(time
);
167 audio_renderer_
->StartPlaying();
169 video_renderer_
->StartPlayingFrom(time
);
172 void RendererImpl::SetPlaybackRate(float playback_rate
) {
173 DVLOG(1) << __FUNCTION__
<< "(" << playback_rate
<< ")";
174 DCHECK(task_runner_
->BelongsToCurrentThread());
176 // Playback rate changes are only carried out while playing.
177 if (state_
!= STATE_PLAYING
)
180 time_source_
->SetPlaybackRate(playback_rate
);
182 const float old_rate
= playback_rate_
;
183 playback_rate_
= playback_rate
;
184 if (!time_ticking_
|| !video_renderer_
)
187 if (old_rate
== 0 && playback_rate
> 0)
188 video_renderer_
->OnTimeStateChanged(true);
189 else if (old_rate
> 0 && playback_rate
== 0)
190 video_renderer_
->OnTimeStateChanged(false);
193 void RendererImpl::SetVolume(float volume
) {
194 DVLOG(1) << __FUNCTION__
;
195 DCHECK(task_runner_
->BelongsToCurrentThread());
198 audio_renderer_
->SetVolume(volume
);
201 base::TimeDelta
RendererImpl::GetMediaTime() {
202 // No BelongsToCurrentThread() checking because this can be called from other
204 return time_source_
->CurrentMediaTime();
207 bool RendererImpl::HasAudio() {
208 DCHECK(task_runner_
->BelongsToCurrentThread());
209 return audio_renderer_
!= NULL
;
212 bool RendererImpl::HasVideo() {
213 DCHECK(task_runner_
->BelongsToCurrentThread());
214 return video_renderer_
!= NULL
;
217 void RendererImpl::DisableUnderflowForTesting() {
218 DVLOG(1) << __FUNCTION__
;
219 DCHECK(task_runner_
->BelongsToCurrentThread());
220 DCHECK_EQ(state_
, STATE_UNINITIALIZED
);
222 underflow_disabled_for_testing_
= true;
225 void RendererImpl::EnableClocklessVideoPlaybackForTesting() {
226 DVLOG(1) << __FUNCTION__
;
227 DCHECK(task_runner_
->BelongsToCurrentThread());
228 DCHECK_EQ(state_
, STATE_UNINITIALIZED
);
229 DCHECK(underflow_disabled_for_testing_
)
230 << "Underflow must be disabled for clockless video playback";
232 clockless_video_playback_enabled_for_testing_
= true;
235 base::TimeTicks
RendererImpl::GetWallClockTime(base::TimeDelta time
) {
236 // No BelongsToCurrentThread() checking because this can be called from other
239 // TODO(scherkus): Currently called from VideoRendererImpl's internal thread,
240 // which should go away at some point http://crbug.com/110814
241 if (clockless_video_playback_enabled_for_testing_
)
242 return base::TimeTicks::Now();
244 return time_source_
->GetWallClockTime(time
);
247 void RendererImpl::SetDecryptorReadyCallback(
248 const DecryptorReadyCB
& decryptor_ready_cb
) {
249 // Cancels the previous decryptor request.
250 if (decryptor_ready_cb
.is_null()) {
251 if (!decryptor_ready_cb_
.is_null()) {
252 base::ResetAndReturn(&decryptor_ready_cb_
)
253 .Run(nullptr, base::Bind(IgnoreCdmAttached
));
258 // We initialize audio and video decoders in sequence.
259 DCHECK(decryptor_ready_cb_
.is_null());
262 decryptor_ready_cb
.Run(cdm_context_
->GetDecryptor(),
263 base::Bind(IgnoreCdmAttached
));
267 decryptor_ready_cb_
= decryptor_ready_cb
;
270 void RendererImpl::InitializeAudioRenderer() {
271 DVLOG(1) << __FUNCTION__
;
272 DCHECK(task_runner_
->BelongsToCurrentThread());
273 DCHECK_EQ(state_
, STATE_INITIALIZING
);
274 DCHECK(!init_cb_
.is_null());
276 PipelineStatusCB done_cb
=
277 base::Bind(&RendererImpl::OnAudioRendererInitializeDone
, weak_this_
);
279 if (!demuxer_stream_provider_
->GetStream(DemuxerStream::AUDIO
)) {
280 audio_renderer_
.reset();
281 task_runner_
->PostTask(FROM_HERE
, base::Bind(done_cb
, PIPELINE_OK
));
285 // Note: After the initialization of a renderer, error events from it may
286 // happen at any time and all future calls must guard against STATE_ERROR.
287 audio_renderer_
->Initialize(
288 demuxer_stream_provider_
->GetStream(DemuxerStream::AUDIO
), done_cb
,
289 base::Bind(&RendererImpl::SetDecryptorReadyCallback
, weak_this_
),
290 base::Bind(&RendererImpl::OnUpdateStatistics
, weak_this_
),
291 base::Bind(&RendererImpl::OnBufferingStateChanged
, weak_this_
,
292 &audio_buffering_state_
),
293 base::Bind(&RendererImpl::OnAudioRendererEnded
, weak_this_
),
294 base::Bind(&RendererImpl::OnError
, weak_this_
),
295 waiting_for_decryption_key_cb_
);
298 void RendererImpl::OnAudioRendererInitializeDone(PipelineStatus status
) {
299 DVLOG(1) << __FUNCTION__
<< ": " << status
;
300 DCHECK(task_runner_
->BelongsToCurrentThread());
302 // OnError() may be fired at any time by the renderers, even if they thought
303 // they initialized successfully (due to delayed output device setup).
304 if (state_
!= STATE_INITIALIZING
) {
305 DCHECK(init_cb_
.is_null());
306 audio_renderer_
.reset();
310 if (status
!= PIPELINE_OK
) {
311 base::ResetAndReturn(&init_cb_
).Run(status
);
315 DCHECK(!init_cb_
.is_null());
316 InitializeVideoRenderer();
319 void RendererImpl::InitializeVideoRenderer() {
320 DVLOG(1) << __FUNCTION__
;
321 DCHECK(task_runner_
->BelongsToCurrentThread());
322 DCHECK_EQ(state_
, STATE_INITIALIZING
);
323 DCHECK(!init_cb_
.is_null());
325 PipelineStatusCB done_cb
=
326 base::Bind(&RendererImpl::OnVideoRendererInitializeDone
, weak_this_
);
328 if (!demuxer_stream_provider_
->GetStream(DemuxerStream::VIDEO
)) {
329 video_renderer_
.reset();
330 task_runner_
->PostTask(FROM_HERE
, base::Bind(done_cb
, PIPELINE_OK
));
334 video_renderer_
->Initialize(
335 demuxer_stream_provider_
->GetStream(DemuxerStream::VIDEO
), done_cb
,
336 base::Bind(&RendererImpl::SetDecryptorReadyCallback
, weak_this_
),
337 base::Bind(&RendererImpl::OnUpdateStatistics
, weak_this_
),
338 base::Bind(&RendererImpl::OnBufferingStateChanged
, weak_this_
,
339 &video_buffering_state_
),
340 base::ResetAndReturn(&paint_cb_
),
341 base::Bind(&RendererImpl::OnVideoRendererEnded
, weak_this_
),
342 base::Bind(&RendererImpl::OnError
, weak_this_
),
343 base::Bind(&RendererImpl::GetWallClockTime
, base::Unretained(this)),
344 waiting_for_decryption_key_cb_
);
347 void RendererImpl::OnVideoRendererInitializeDone(PipelineStatus status
) {
348 DVLOG(1) << __FUNCTION__
<< ": " << status
;
349 DCHECK(task_runner_
->BelongsToCurrentThread());
351 // OnError() may be fired at any time by the renderers, even if they thought
352 // they initialized successfully (due to delayed output device setup).
353 if (state_
!= STATE_INITIALIZING
) {
354 DCHECK(init_cb_
.is_null());
355 audio_renderer_
.reset();
356 video_renderer_
.reset();
360 DCHECK(!init_cb_
.is_null());
362 if (status
!= PIPELINE_OK
) {
363 base::ResetAndReturn(&init_cb_
).Run(status
);
367 if (audio_renderer_
) {
368 time_source_
= audio_renderer_
->GetTimeSource();
369 } else if (!time_source_
) {
370 wall_clock_time_source_
.reset(new WallClockTimeSource());
371 time_source_
= wall_clock_time_source_
.get();
374 state_
= STATE_PLAYING
;
375 DCHECK(time_source_
);
376 DCHECK(audio_renderer_
|| video_renderer_
);
377 base::ResetAndReturn(&init_cb_
).Run(PIPELINE_OK
);
380 void RendererImpl::FlushAudioRenderer() {
381 DVLOG(1) << __FUNCTION__
;
382 DCHECK(task_runner_
->BelongsToCurrentThread());
383 DCHECK_EQ(state_
, STATE_FLUSHING
);
384 DCHECK(!flush_cb_
.is_null());
386 if (!audio_renderer_
) {
387 OnAudioRendererFlushDone();
391 audio_renderer_
->Flush(
392 base::Bind(&RendererImpl::OnAudioRendererFlushDone
, weak_this_
));
395 void RendererImpl::OnAudioRendererFlushDone() {
396 DVLOG(1) << __FUNCTION__
;
397 DCHECK(task_runner_
->BelongsToCurrentThread());
399 if (state_
== STATE_ERROR
) {
400 DCHECK(flush_cb_
.is_null());
404 DCHECK_EQ(state_
, STATE_FLUSHING
);
405 DCHECK(!flush_cb_
.is_null());
407 DCHECK_EQ(audio_buffering_state_
, BUFFERING_HAVE_NOTHING
);
408 audio_ended_
= false;
409 FlushVideoRenderer();
412 void RendererImpl::FlushVideoRenderer() {
413 DVLOG(1) << __FUNCTION__
;
414 DCHECK(task_runner_
->BelongsToCurrentThread());
415 DCHECK_EQ(state_
, STATE_FLUSHING
);
416 DCHECK(!flush_cb_
.is_null());
418 if (!video_renderer_
) {
419 OnVideoRendererFlushDone();
423 video_renderer_
->Flush(
424 base::Bind(&RendererImpl::OnVideoRendererFlushDone
, weak_this_
));
427 void RendererImpl::OnVideoRendererFlushDone() {
428 DVLOG(1) << __FUNCTION__
;
429 DCHECK(task_runner_
->BelongsToCurrentThread());
431 if (state_
== STATE_ERROR
) {
432 DCHECK(flush_cb_
.is_null());
436 DCHECK_EQ(state_
, STATE_FLUSHING
);
437 DCHECK(!flush_cb_
.is_null());
439 DCHECK_EQ(video_buffering_state_
, BUFFERING_HAVE_NOTHING
);
440 video_ended_
= false;
441 state_
= STATE_PLAYING
;
442 base::ResetAndReturn(&flush_cb_
).Run();
445 void RendererImpl::OnUpdateStatistics(const PipelineStatistics
& stats
) {
446 DCHECK(task_runner_
->BelongsToCurrentThread());
447 statistics_cb_
.Run(stats
);
450 void RendererImpl::OnBufferingStateChanged(BufferingState
* buffering_state
,
451 BufferingState new_buffering_state
) {
452 const bool is_audio
= buffering_state
== &audio_buffering_state_
;
453 DVLOG(1) << __FUNCTION__
<< "(" << *buffering_state
<< ", "
454 << new_buffering_state
<< ") " << (is_audio
? "audio" : "video");
455 DCHECK(task_runner_
->BelongsToCurrentThread());
457 bool was_waiting_for_enough_data
= WaitingForEnoughData();
459 // When audio is present, defer underflow callbacks for some time to avoid
460 // unnecessary glitches in audio; see http://crbug.com/144683#c53.
461 if (audio_renderer_
&& !is_audio
&& state_
== STATE_PLAYING
) {
462 if (video_buffering_state_
== BUFFERING_HAVE_ENOUGH
&&
463 new_buffering_state
== BUFFERING_HAVE_NOTHING
&&
464 deferred_underflow_cb_
.IsCancelled()) {
465 deferred_underflow_cb_
.Reset(base::Bind(
466 &RendererImpl::OnBufferingStateChanged
, weak_factory_
.GetWeakPtr(),
467 buffering_state
, new_buffering_state
));
468 task_runner_
->PostDelayedTask(FROM_HERE
,
469 deferred_underflow_cb_
.callback(),
470 video_underflow_threshold_
);
474 deferred_underflow_cb_
.Cancel();
475 } else if (!deferred_underflow_cb_
.IsCancelled() && is_audio
&&
476 new_buffering_state
== BUFFERING_HAVE_NOTHING
) {
477 // If audio underflows while we have a deferred video underflow in progress
478 // we want to mark video as underflowed immediately and cancel the deferral.
479 deferred_underflow_cb_
.Cancel();
480 video_buffering_state_
= BUFFERING_HAVE_NOTHING
;
483 *buffering_state
= new_buffering_state
;
485 // Disable underflow by ignoring updates that renderers have ran out of data.
486 if (state_
== STATE_PLAYING
&& underflow_disabled_for_testing_
&&
488 DVLOG(1) << "Update ignored because underflow is disabled for testing.";
492 // Renderer underflowed.
493 if (!was_waiting_for_enough_data
&& WaitingForEnoughData()) {
496 // TODO(scherkus): Fire BUFFERING_HAVE_NOTHING callback to alert clients of
497 // underflow state http://crbug.com/144683
501 // Renderer prerolled.
502 if (was_waiting_for_enough_data
&& !WaitingForEnoughData()) {
504 buffering_state_cb_
.Run(BUFFERING_HAVE_ENOUGH
);
509 bool RendererImpl::WaitingForEnoughData() const {
510 DCHECK(task_runner_
->BelongsToCurrentThread());
511 if (state_
!= STATE_PLAYING
)
513 if (audio_renderer_
&& audio_buffering_state_
!= BUFFERING_HAVE_ENOUGH
)
515 if (video_renderer_
&& video_buffering_state_
!= BUFFERING_HAVE_ENOUGH
)
520 void RendererImpl::PausePlayback() {
521 DVLOG(1) << __FUNCTION__
;
522 DCHECK(task_runner_
->BelongsToCurrentThread());
523 DCHECK(time_ticking_
);
526 DCHECK(PlaybackHasEnded() || WaitingForEnoughData())
527 << "Playback should only pause due to ending or underflowing";
531 // It's OK to pause playback when flushing.
534 case STATE_UNINITIALIZED
:
535 case STATE_INITIALIZING
:
536 NOTREACHED() << "Invalid state: " << state_
;
540 // An error state may occur at any time.
544 time_ticking_
= false;
545 time_source_
->StopTicking();
546 if (playback_rate_
> 0 && video_renderer_
)
547 video_renderer_
->OnTimeStateChanged(false);
550 void RendererImpl::StartPlayback() {
551 DVLOG(1) << __FUNCTION__
;
552 DCHECK(task_runner_
->BelongsToCurrentThread());
553 DCHECK_EQ(state_
, STATE_PLAYING
);
554 DCHECK(!time_ticking_
);
555 DCHECK(!WaitingForEnoughData());
557 time_ticking_
= true;
558 time_source_
->StartTicking();
559 if (playback_rate_
> 0 && video_renderer_
)
560 video_renderer_
->OnTimeStateChanged(true);
563 void RendererImpl::OnAudioRendererEnded() {
564 DVLOG(1) << __FUNCTION__
;
565 DCHECK(task_runner_
->BelongsToCurrentThread());
567 if (state_
!= STATE_PLAYING
)
570 DCHECK(!audio_ended_
);
573 RunEndedCallbackIfNeeded();
576 void RendererImpl::OnVideoRendererEnded() {
577 DVLOG(1) << __FUNCTION__
;
578 DCHECK(task_runner_
->BelongsToCurrentThread());
580 if (state_
!= STATE_PLAYING
)
583 DCHECK(!video_ended_
);
586 RunEndedCallbackIfNeeded();
589 bool RendererImpl::PlaybackHasEnded() const {
590 DVLOG(1) << __FUNCTION__
;
591 DCHECK(task_runner_
->BelongsToCurrentThread());
593 if (audio_renderer_
&& !audio_ended_
)
596 if (video_renderer_
&& !video_ended_
)
602 void RendererImpl::RunEndedCallbackIfNeeded() {
603 DVLOG(1) << __FUNCTION__
;
604 DCHECK(task_runner_
->BelongsToCurrentThread());
606 if (!PlaybackHasEnded())
615 void RendererImpl::OnError(PipelineStatus error
) {
616 DVLOG(1) << __FUNCTION__
<< "(" << error
<< ")";
617 DCHECK(task_runner_
->BelongsToCurrentThread());
618 DCHECK_NE(PIPELINE_OK
, error
) << "PIPELINE_OK isn't an error!";
620 // An error has already been delivered.
621 if (state_
== STATE_ERROR
)
624 const State old_state
= state_
;
625 state_
= STATE_ERROR
;
627 if (old_state
== STATE_INITIALIZING
) {
628 base::ResetAndReturn(&init_cb_
).Run(error
);
632 // After OnError() returns, the pipeline may destroy |this|.
633 base::ResetAndReturn(&error_cb_
).Run(error
);
635 if (!flush_cb_
.is_null())
636 base::ResetAndReturn(&flush_cb_
).Run();