1 // Copyright 2015 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/base/android/media_codec_player.h"
7 #include "base/barrier_closure.h"
9 #include "base/lazy_instance.h"
10 #include "base/logging.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/threading/thread.h"
13 #include "media/base/android/media_codec_audio_decoder.h"
14 #include "media/base/android/media_codec_video_decoder.h"
15 #include "media/base/android/media_player_manager.h"
16 #include "media/base/timestamp_constants.h"
18 #define RUN_ON_MEDIA_THREAD(METHOD, ...) \
20 if (!GetMediaTaskRunner()->BelongsToCurrentThread()) { \
21 DCHECK(ui_task_runner_->BelongsToCurrentThread()); \
22 GetMediaTaskRunner()->PostTask( \
23 FROM_HERE, base::Bind(&MediaCodecPlayer::METHOD, media_weak_this_, \
31 class MediaThread
: public base::Thread
{
33 MediaThread() : base::Thread("BrowserMediaThread") {
38 // Create media thread
39 base::LazyInstance
<MediaThread
>::Leaky
40 g_media_thread
= LAZY_INSTANCE_INITIALIZER
;
43 scoped_refptr
<base::SingleThreadTaskRunner
> GetMediaTaskRunner() {
44 return g_media_thread
.Pointer()->task_runner();
47 // MediaCodecPlayer implementation.
49 MediaCodecPlayer::MediaCodecPlayer(
51 base::WeakPtr
<MediaPlayerManager
> manager
,
52 const RequestMediaResourcesCB
& request_media_resources_cb
,
53 scoped_ptr
<DemuxerAndroid
> demuxer
,
54 const GURL
& frame_url
)
55 : MediaPlayerAndroid(player_id
,
57 request_media_resources_cb
,
59 ui_task_runner_(base::ThreadTaskRunnerHandle::Get()),
60 demuxer_(demuxer
.Pass()),
62 interpolator_(&default_tick_clock_
),
63 pending_start_(false),
64 pending_seek_(kNoTimestamp()),
65 media_weak_factory_(this) {
66 DCHECK(ui_task_runner_
->BelongsToCurrentThread());
68 DVLOG(1) << "MediaCodecPlayer::MediaCodecPlayer: player_id:" << player_id
;
70 request_resources_cb_
= base::Bind(request_media_resources_cb_
, player_id
);
73 base::Bind(&MediaPlayerManager::OnPlaybackComplete
, manager
, player_id
);
75 base::Bind(&MediaPlayerManager::OnSeekComplete
, manager
, player_id
);
76 error_cb_
= base::Bind(&MediaPlayerManager::OnError
, manager
, player_id
);
77 attach_listener_cb_
= base::Bind(&MediaPlayerAndroid::AttachListener
,
78 WeakPtrForUIThread(), nullptr);
80 base::Bind(&MediaPlayerAndroid::DetachListener
, WeakPtrForUIThread());
81 metadata_changed_cb_
= base::Bind(&MediaPlayerAndroid::OnMediaMetadataChanged
,
82 WeakPtrForUIThread());
84 base::Bind(&MediaPlayerAndroid::OnTimeUpdate
, WeakPtrForUIThread());
86 media_weak_this_
= media_weak_factory_
.GetWeakPtr();
88 // Finish initializaton on Media thread
89 GetMediaTaskRunner()->PostTask(
90 FROM_HERE
, base::Bind(&MediaCodecPlayer::Initialize
, media_weak_this_
));
93 MediaCodecPlayer::~MediaCodecPlayer()
95 DVLOG(1) << "MediaCodecPlayer::~MediaCodecPlayer";
96 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
98 // Currently the unit tests wait for the MediaCodecPlayer destruction by
99 // watching the demuxer, which is destroyed as one of the member variables.
100 // Release the codecs here, before any member variable is destroyed to make
101 // the unit tests happy.
104 video_decoder_
->ReleaseDecoderResources();
106 audio_decoder_
->ReleaseDecoderResources();
109 void MediaCodecPlayer::Initialize() {
110 DVLOG(1) << __FUNCTION__
;
111 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
113 interpolator_
.SetUpperBound(base::TimeDelta());
117 // This call might in turn call MediaCodecPlayer::OnDemuxerConfigsAvailable()
118 // which propagates configs into decoders. Therefore CreateDecoders() should
120 demuxer_
->Initialize(this);
123 // The implementation of MediaPlayerAndroid interface.
125 void MediaCodecPlayer::DeleteOnCorrectThread() {
126 DVLOG(1) << __FUNCTION__
;
127 DCHECK(ui_task_runner_
->BelongsToCurrentThread());
131 // The base class part that deals with MediaPlayerListener
132 // has to be destroyed on UI thread.
133 DestroyListenerOnUIThread();
135 // Post deletion onto Media thread
136 GetMediaTaskRunner()->DeleteSoon(FROM_HERE
, this);
139 void MediaCodecPlayer::SetVideoSurface(gfx::ScopedJavaSurface surface
) {
140 RUN_ON_MEDIA_THREAD(SetVideoSurface
, base::Passed(&surface
));
142 DVLOG(1) << __FUNCTION__
<< (surface
.IsEmpty() ? " empty" : " non-empty");
144 // Save the empty-ness before we pass the surface to the decoder.
145 bool surface_is_empty
= surface
.IsEmpty();
147 // Apparently RemoveVideoSurface() can be called several times in a row,
148 // ignore the second and subsequent calls.
149 if (surface_is_empty
&& !video_decoder_
->HasVideoSurface()) {
150 DVLOG(1) << __FUNCTION__
<< ": surface already removed, ignoring";
154 video_decoder_
->SetVideoSurface(surface
.Pass());
156 if (surface_is_empty
) {
157 // Remove video surface.
163 DVLOG(1) << __FUNCTION__
<< ": stopping and restarting";
164 // Stop decoders as quickly as possible.
165 StopDecoders(); // synchronous stop
167 // Prefetch or wait for initial configuration.
168 if (HasAudio() || HasVideo()) {
169 SetState(kStatePrefetching
);
170 StartPrefetchDecoders();
172 SetState(kStateWaitingForConfig
);
180 // Replace video surface.
182 case kStateWaitingForSurface
:
183 SetState(kStatePlaying
);
184 StartPlaybackOrBrowserSeek();
191 DVLOG(1) << __FUNCTION__
<< ": requesting to stop and restart";
192 SetState(kStateStopping
);
193 RequestToStopDecoders();
194 SetPendingStart(true);
203 void MediaCodecPlayer::Start() {
204 RUN_ON_MEDIA_THREAD(Start
);
206 DVLOG(1) << __FUNCTION__
;
210 // Prefetch or wait for initial configuration.
211 if (HasAudio() || HasVideo()) {
212 SetState(kStatePrefetching
);
213 StartPrefetchDecoders();
215 SetState(kStateWaitingForConfig
);
219 case kStateWaitingForSeek
:
220 SetPendingStart(true);
222 case kStateWaitingForConfig
:
223 case kStatePrefetching
:
225 case kStateWaitingForSurface
:
234 void MediaCodecPlayer::Pause(bool is_media_related_action
) {
235 RUN_ON_MEDIA_THREAD(Pause
, is_media_related_action
);
237 DVLOG(1) << __FUNCTION__
;
239 SetPendingStart(false);
242 case kStateWaitingForConfig
:
243 case kStatePrefetching
:
244 case kStateWaitingForSurface
:
245 SetState(kStatePaused
);
249 SetState(kStateStopping
);
250 RequestToStopDecoders();
254 case kStateWaitingForSeek
:
263 void MediaCodecPlayer::SeekTo(base::TimeDelta timestamp
) {
264 RUN_ON_MEDIA_THREAD(SeekTo
, timestamp
);
266 DVLOG(1) << __FUNCTION__
<< " " << timestamp
;
270 SetState(kStateWaitingForSeek
);
271 RequestDemuxerSeek(timestamp
);
273 case kStateWaitingForConfig
:
274 case kStatePrefetching
:
275 case kStateWaitingForSurface
:
276 SetState(kStateWaitingForSeek
);
278 SetPendingStart(true);
279 RequestDemuxerSeek(timestamp
);
282 SetState(kStateStopping
);
283 RequestToStopDecoders();
284 SetPendingStart(true);
285 SetPendingSeek(timestamp
);
288 SetPendingSeek(timestamp
);
290 case kStateWaitingForSeek
:
291 SetPendingSeek(timestamp
);
301 void MediaCodecPlayer::Release() {
302 RUN_ON_MEDIA_THREAD(Release
);
304 DVLOG(1) << __FUNCTION__
;
306 // Stop decoding threads and delete MediaCodecs, but keep IPC between browser
307 // and renderer processes going. Seek should work across and after Release().
309 ReleaseDecoderResources();
311 SetPendingStart(false);
313 if (state_
!= kStateWaitingForSeek
)
314 SetState(kStatePaused
);
316 base::TimeDelta pending_seek_time
= GetPendingSeek();
317 if (pending_seek_time
!= kNoTimestamp()) {
318 SetPendingSeek(kNoTimestamp());
319 SetState(kStateWaitingForSeek
);
320 RequestDemuxerSeek(pending_seek_time
);
324 void MediaCodecPlayer::SetVolume(double volume
) {
325 RUN_ON_MEDIA_THREAD(SetVolume
, volume
);
327 DVLOG(1) << __FUNCTION__
<< " " << volume
;
328 audio_decoder_
->SetVolume(volume
);
331 int MediaCodecPlayer::GetVideoWidth() {
332 DCHECK(ui_task_runner_
->BelongsToCurrentThread());
333 return metadata_cache_
.video_size
.width();
336 int MediaCodecPlayer::GetVideoHeight() {
337 DCHECK(ui_task_runner_
->BelongsToCurrentThread());
338 return metadata_cache_
.video_size
.height();
341 base::TimeDelta
MediaCodecPlayer::GetCurrentTime() {
342 DCHECK(ui_task_runner_
->BelongsToCurrentThread());
343 return current_time_cache_
;
346 base::TimeDelta
MediaCodecPlayer::GetDuration() {
347 DCHECK(ui_task_runner_
->BelongsToCurrentThread());
348 return metadata_cache_
.duration
;
351 bool MediaCodecPlayer::IsPlaying() {
352 DCHECK(ui_task_runner_
->BelongsToCurrentThread());
354 // TODO(timav): Use another variable since |state_| should only be accessed on
356 return state_
== kStatePlaying
|| state_
== kStateStopping
;
359 bool MediaCodecPlayer::CanPause() {
360 DCHECK(ui_task_runner_
->BelongsToCurrentThread());
365 bool MediaCodecPlayer::CanSeekForward() {
366 DCHECK(ui_task_runner_
->BelongsToCurrentThread());
371 bool MediaCodecPlayer::CanSeekBackward() {
372 DCHECK(ui_task_runner_
->BelongsToCurrentThread());
377 bool MediaCodecPlayer::IsPlayerReady() {
378 DCHECK(ui_task_runner_
->BelongsToCurrentThread());
379 // This method is called to check whether it's safe to release the player when
380 // the OS needs more resources. This class can be released at any time.
384 void MediaCodecPlayer::SetCdm(BrowserCdm
* cdm
) {
385 DCHECK(ui_task_runner_
->BelongsToCurrentThread());
389 // Callbacks from Demuxer.
391 void MediaCodecPlayer::OnDemuxerConfigsAvailable(
392 const DemuxerConfigs
& configs
) {
393 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
395 DVLOG(1) << __FUNCTION__
;
397 duration_
= configs
.duration
;
399 SetDemuxerConfigs(configs
);
401 // Update cache and notify manager on UI thread
402 gfx::Size video_size
= HasVideo() ? configs
.video_size
: gfx::Size();
403 ui_task_runner_
->PostTask(
404 FROM_HERE
, base::Bind(metadata_changed_cb_
, duration_
, video_size
));
407 void MediaCodecPlayer::OnDemuxerDataAvailable(const DemuxerData
& data
) {
408 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
410 DCHECK_LT(0u, data
.access_units
.size());
411 CHECK_GE(1u, data
.demuxer_configs
.size());
413 DVLOG(2) << "Player::" << __FUNCTION__
;
415 if (data
.type
== DemuxerStream::AUDIO
)
416 audio_decoder_
->OnDemuxerDataAvailable(data
);
418 if (data
.type
== DemuxerStream::VIDEO
)
419 video_decoder_
->OnDemuxerDataAvailable(data
);
422 void MediaCodecPlayer::OnDemuxerSeekDone(
423 base::TimeDelta actual_browser_seek_time
) {
424 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
426 DVLOG(1) << __FUNCTION__
<< " actual_time:" << actual_browser_seek_time
;
428 DCHECK(seek_info_
.get());
429 DCHECK(seek_info_
->seek_time
!= kNoTimestamp());
431 // A browser seek must not jump into the past. Ideally, it seeks to the
432 // requested time, but it might jump into the future.
433 DCHECK(!seek_info_
->is_browser_seek
||
434 seek_info_
->seek_time
<= actual_browser_seek_time
);
436 // Restrict the current time to be equal to seek_time
437 // for the next StartPlaybackDecoders() call.
439 base::TimeDelta seek_time
= seek_info_
->is_browser_seek
440 ? actual_browser_seek_time
441 : seek_info_
->seek_time
;
443 interpolator_
.SetBounds(seek_time
, seek_time
);
445 audio_decoder_
->SetBaseTimestamp(seek_time
);
447 audio_decoder_
->SetPrerollTimestamp(seek_time
);
448 video_decoder_
->SetPrerollTimestamp(seek_time
);
450 // The Flush() might set the state to kStateError.
451 if (state_
== kStateError
) {
452 // Notify the Renderer.
453 if (!seek_info_
->is_browser_seek
)
454 ui_task_runner_
->PostTask(FROM_HERE
,
455 base::Bind(seek_done_cb_
, seek_time
));
461 DCHECK_EQ(kStateWaitingForSeek
, state_
);
463 base::TimeDelta pending_seek_time
= GetPendingSeek();
464 if (pending_seek_time
!= kNoTimestamp()) {
465 // Keep kStateWaitingForSeek
466 SetPendingSeek(kNoTimestamp());
467 RequestDemuxerSeek(pending_seek_time
);
471 if (HasPendingStart()) {
472 SetPendingStart(false);
473 // Prefetch or wait for initial configuration.
474 if (HasAudio() || HasVideo()) {
475 SetState(kStatePrefetching
);
476 StartPrefetchDecoders();
478 SetState(kStateWaitingForConfig
);
481 SetState(kStatePaused
);
484 // Notify the Renderer.
485 if (!seek_info_
->is_browser_seek
)
486 ui_task_runner_
->PostTask(FROM_HERE
, base::Bind(seek_done_cb_
, seek_time
));
491 void MediaCodecPlayer::OnDemuxerDurationChanged(
492 base::TimeDelta duration
) {
493 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
494 DVLOG(1) << __FUNCTION__
<< " duration:" << duration
;
496 duration_
= duration
;
499 void MediaCodecPlayer::SetDecodersTimeCallbackForTests(
500 DecodersTimeCallback cb
) {
501 DCHECK(ui_task_runner_
->BelongsToCurrentThread());
502 decoders_time_cb_
= cb
;
505 void MediaCodecPlayer::SetCodecCreatedCallbackForTests(
506 CodecCreatedCallback cb
) {
507 DCHECK(ui_task_runner_
->BelongsToCurrentThread());
508 DCHECK(audio_decoder_
&& video_decoder_
);
510 audio_decoder_
->SetCodecCreatedCallbackForTests(
511 base::Bind(cb
, DemuxerStream::AUDIO
));
512 video_decoder_
->SetCodecCreatedCallbackForTests(
513 base::Bind(cb
, DemuxerStream::VIDEO
));
516 void MediaCodecPlayer::SetAlwaysReconfigureForTests(DemuxerStream::Type type
) {
517 DCHECK(ui_task_runner_
->BelongsToCurrentThread());
518 DCHECK(audio_decoder_
&& video_decoder_
);
520 if (type
== DemuxerStream::AUDIO
)
521 audio_decoder_
->SetAlwaysReconfigureForTests();
522 else if (type
== DemuxerStream::VIDEO
)
523 video_decoder_
->SetAlwaysReconfigureForTests();
526 bool MediaCodecPlayer::IsPrerollingForTests(DemuxerStream::Type type
) const {
527 DCHECK(ui_task_runner_
->BelongsToCurrentThread());
528 DCHECK(audio_decoder_
&& video_decoder_
);
530 if (type
== DemuxerStream::AUDIO
)
531 return audio_decoder_
->IsPrerollingForTests();
532 else if (type
== DemuxerStream::VIDEO
)
533 return video_decoder_
->IsPrerollingForTests();
538 // Events from Player, called on UI thread
540 void MediaCodecPlayer::OnMediaMetadataChanged(base::TimeDelta duration
,
541 const gfx::Size
& video_size
) {
542 DCHECK(ui_task_runner_
->BelongsToCurrentThread());
544 if (duration
!= kNoTimestamp())
545 metadata_cache_
.duration
= duration
;
547 if (!video_size
.IsEmpty())
548 metadata_cache_
.video_size
= video_size
;
550 manager()->OnMediaMetadataChanged(player_id(), metadata_cache_
.duration
,
551 metadata_cache_
.video_size
.width(),
552 metadata_cache_
.video_size
.height(), true);
555 void MediaCodecPlayer::OnTimeUpdate(base::TimeDelta current_timestamp
,
556 base::TimeTicks current_time_ticks
) {
557 DCHECK(ui_task_runner_
->BelongsToCurrentThread());
559 current_time_cache_
= current_timestamp
;
560 manager()->OnTimeUpdate(player_id(), current_timestamp
, current_time_ticks
);
563 // Events from Decoders, called on Media thread
565 void MediaCodecPlayer::RequestDemuxerData(DemuxerStream::Type stream_type
) {
566 DVLOG(2) << __FUNCTION__
<< " streamType:" << stream_type
;
568 // Use this method instead of directly binding with
569 // DemuxerAndroid::RequestDemuxerData() to avoid the race condition on
571 // 1. DeleteSoon is posted from UI to Media thread.
572 // 2. RequestDemuxerData callback is posted from Decoder to Media thread.
573 // 3. DeleteSoon arrives, we delete the player and detach from
574 // BrowserDemuxerAndroid.
575 // 4. RequestDemuxerData is processed by the media thread queue. Since the
576 // weak_ptr was invalidated in (3), this is a no-op. If we used
577 // DemuxerAndroid::RequestDemuxerData() it would arrive and will try to
578 // call the client, but the client (i.e. this player) would not exist.
579 demuxer_
->RequestDemuxerData(stream_type
);
582 void MediaCodecPlayer::OnPrefetchDone() {
583 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
585 if (state_
!= kStatePrefetching
) {
586 DVLOG(1) << __FUNCTION__
<< " wrong state " << AsString(state_
)
591 DVLOG(1) << __FUNCTION__
;
593 if (!HasAudio() && !HasVideo()) {
594 // No configuration at all after prefetching.
595 // This is an error, initial configuration is expected
596 // before the first data chunk.
597 DCHECK(!internal_error_cb_
.is_null());
598 GetMediaTaskRunner()->PostTask(FROM_HERE
, internal_error_cb_
);
602 if (HasVideo() && !video_decoder_
->HasVideoSurface()) {
603 SetState(kStateWaitingForSurface
);
607 SetState(kStatePlaying
);
608 StartPlaybackOrBrowserSeek();
611 void MediaCodecPlayer::OnPrerollDone() {
612 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
614 if (state_
!= kStatePlaying
) {
615 DVLOG(1) << __FUNCTION__
<< ": in state " << AsString(state_
)
620 DVLOG(1) << __FUNCTION__
;
622 StartStatus status
= StartDecoders();
623 if (status
!= kStartOk
)
624 GetMediaTaskRunner()->PostTask(FROM_HERE
, internal_error_cb_
);
627 void MediaCodecPlayer::OnDecoderDrained(DemuxerStream::Type type
) {
628 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
629 DVLOG(1) << __FUNCTION__
<< " " << type
;
631 // We expect that OnStopDone() comes next.
633 DCHECK(type
== DemuxerStream::AUDIO
|| type
== DemuxerStream::VIDEO
);
635 // DCHECK(state_ == kStatePlaying || state_ == kStateStopping)
636 // << __FUNCTION__ << " illegal state: " << AsString(state_);
638 // With simultaneous reconfiguration of audio and video streams the state
639 // can be kStatePrefetching as well:
640 // OnLastFrameRendered VIDEO (VIDEO decoder is stopped)
641 // OnLastFrameRendered AUDIO (AUDIO decoder is stopped)
642 // OnDecoderDrained VIDEO (kStatePlaying -> kStateStopping)
643 // OnStopDone VIDEO (kStateStopping -> kStatePrefetching)
644 // OnDecoderDrained AUDIO
647 // TODO(timav): combine OnDecoderDrained() and OnStopDone() ?
651 SetState(kStateStopping
);
652 SetPendingStart(true);
654 if (type
== DemuxerStream::AUDIO
&& !VideoFinished()) {
655 DVLOG(1) << __FUNCTION__
<< " requesting to stop video";
656 video_decoder_
->RequestToStop();
657 } else if (type
== DemuxerStream::VIDEO
&& !AudioFinished()) {
658 DVLOG(1) << __FUNCTION__
<< " requesting to stop audio";
659 audio_decoder_
->RequestToStop();
668 void MediaCodecPlayer::OnStopDone(DemuxerStream::Type type
) {
669 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
670 DVLOG(1) << __FUNCTION__
<< " " << type
671 << " interpolated time:" << GetInterpolatedTime();
673 if (!(audio_decoder_
->IsStopped() && video_decoder_
->IsStopped())) {
674 DVLOG(1) << __FUNCTION__
<< " both audio and video has to be stopped"
676 return; // Wait until other stream is stopped
679 // At this point decoder threads should not be running
680 if (interpolator_
.interpolating())
681 interpolator_
.StopInterpolating();
683 base::TimeDelta seek_time
;
685 case kStateStopping
: {
686 base::TimeDelta seek_time
= GetPendingSeek();
687 if (seek_time
!= kNoTimestamp()) {
688 SetState(kStateWaitingForSeek
);
689 SetPendingSeek(kNoTimestamp());
690 RequestDemuxerSeek(seek_time
);
691 } else if (HasPendingStart()) {
692 SetPendingStart(false);
693 SetState(kStatePrefetching
);
694 StartPrefetchDecoders();
696 SetState(kStatePaused
);
700 // Unexpected stop means completion
701 SetState(kStatePaused
);
704 // DVLOG(0) << __FUNCTION__ << " illegal state: " << AsString(state_);
706 // Ignore! There can be a race condition: audio posts OnStopDone,
707 // then video posts, then first OnStopDone arrives at which point
708 // both streams are already stopped, then second OnStopDone arrives. When
709 // the second one arrives, the state us not kStateStopping any more.
713 // DetachListener to UI thread
714 ui_task_runner_
->PostTask(FROM_HERE
, detach_listener_cb_
);
716 if (AudioFinished() && VideoFinished())
717 ui_task_runner_
->PostTask(FROM_HERE
, completion_cb_
);
720 void MediaCodecPlayer::OnError() {
721 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
722 DVLOG(1) << __FUNCTION__
;
724 // kStateError blocks all events
725 SetState(kStateError
);
727 ReleaseDecoderResources();
729 ui_task_runner_
->PostTask(FROM_HERE
,
730 base::Bind(error_cb_
, MEDIA_ERROR_DECODE
));
733 void MediaCodecPlayer::OnStarvation(DemuxerStream::Type type
) {
734 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
735 DVLOG(1) << __FUNCTION__
<< " stream type:" << type
;
737 if (state_
!= kStatePlaying
)
740 SetState(kStateStopping
);
741 RequestToStopDecoders();
742 SetPendingStart(true);
745 void MediaCodecPlayer::OnTimeIntervalUpdate(DemuxerStream::Type type
,
746 base::TimeDelta now_playing
,
747 base::TimeDelta last_buffered
,
749 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
751 DVLOG(2) << __FUNCTION__
<< ": stream type:" << type
<< " [" << now_playing
752 << "," << last_buffered
<< "]" << (postpone
? " postpone" : "");
754 // For testing only: report time interval as we receive it from decoders
755 // as an indication of what is being rendered. Do not post this callback
756 // for postponed frames: although the PTS is correct, the tests also care
757 // about the wall clock time this callback arrives and deduce the rendering
759 if (!decoders_time_cb_
.is_null() && !postpone
) {
760 ui_task_runner_
->PostTask(
762 base::Bind(decoders_time_cb_
, type
, now_playing
, last_buffered
));
765 // I assume that audio stream cannot be added after we get configs by
766 // OnDemuxerConfigsAvailable(), but that audio can finish early.
768 if (type
== DemuxerStream::VIDEO
) {
769 // Ignore video PTS if there is audio stream or if it's behind current
770 // time as set by audio stream.
771 if (!AudioFinished() ||
772 (HasAudio() && now_playing
< interpolator_
.GetInterpolatedTime()))
776 interpolator_
.SetBounds(now_playing
, last_buffered
);
780 ui_task_runner_
->PostTask(FROM_HERE
,
781 base::Bind(time_update_cb_
, GetInterpolatedTime(),
782 base::TimeTicks::Now()));
786 void MediaCodecPlayer::OnVideoCodecCreated() {
787 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
789 // This callback requests resources by releasing other players.
790 ui_task_runner_
->PostTask(FROM_HERE
, request_resources_cb_
);
793 void MediaCodecPlayer::OnVideoResolutionChanged(const gfx::Size
& size
) {
794 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
796 DVLOG(1) << __FUNCTION__
<< " " << size
.width() << "x" << size
.height();
798 // Update cache and notify manager on UI thread
799 ui_task_runner_
->PostTask(
800 FROM_HERE
, base::Bind(metadata_changed_cb_
, kNoTimestamp(), size
));
803 // State machine operations, called on Media thread
805 void MediaCodecPlayer::SetState(PlayerState new_state
) {
806 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
808 DVLOG(1) << "SetState:" << AsString(state_
) << " -> " << AsString(new_state
);
812 void MediaCodecPlayer::SetPendingStart(bool need_to_start
) {
813 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
814 DVLOG(1) << __FUNCTION__
<< ": " << need_to_start
;
815 pending_start_
= need_to_start
;
818 bool MediaCodecPlayer::HasPendingStart() const {
819 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
820 return pending_start_
;
823 void MediaCodecPlayer::SetPendingSeek(base::TimeDelta timestamp
) {
824 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
825 DVLOG(1) << __FUNCTION__
<< ": " << timestamp
;
826 pending_seek_
= timestamp
;
829 base::TimeDelta
MediaCodecPlayer::GetPendingSeek() const {
830 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
831 return pending_seek_
;
834 bool MediaCodecPlayer::HasAudio() const {
835 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
836 return audio_decoder_
->HasStream();
839 bool MediaCodecPlayer::HasVideo() const {
840 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
841 return video_decoder_
->HasStream();
844 void MediaCodecPlayer::SetDemuxerConfigs(const DemuxerConfigs
& configs
) {
845 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
846 DVLOG(1) << __FUNCTION__
<< " " << configs
;
848 DCHECK(audio_decoder_
);
849 DCHECK(video_decoder_
);
851 // At least one valid codec must be present.
852 DCHECK(configs
.audio_codec
!= kUnknownAudioCodec
||
853 configs
.video_codec
!= kUnknownVideoCodec
);
855 if (configs
.audio_codec
!= kUnknownAudioCodec
)
856 audio_decoder_
->SetDemuxerConfigs(configs
);
858 if (configs
.video_codec
!= kUnknownVideoCodec
)
859 video_decoder_
->SetDemuxerConfigs(configs
);
861 if (state_
== kStateWaitingForConfig
) {
862 SetState(kStatePrefetching
);
863 StartPrefetchDecoders();
867 void MediaCodecPlayer::StartPrefetchDecoders() {
868 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
869 DVLOG(1) << __FUNCTION__
;
871 bool do_audio
= false;
872 bool do_video
= false;
874 if (!AudioFinished()) {
878 if (!VideoFinished()) {
883 DCHECK_LT(0, count
); // at least one decoder should be active
885 base::Closure prefetch_cb
= base::BarrierClosure(
886 count
, base::Bind(&MediaCodecPlayer::OnPrefetchDone
, media_weak_this_
));
889 audio_decoder_
->Prefetch(prefetch_cb
);
892 video_decoder_
->Prefetch(prefetch_cb
);
895 void MediaCodecPlayer::StartPlaybackOrBrowserSeek() {
896 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
897 DVLOG(1) << __FUNCTION__
;
899 // TODO(timav): consider replacing this method with posting a
900 // browser seek task (i.e. generate an event) from StartPlaybackDecoders().
902 StartStatus status
= StartPlaybackDecoders();
905 case kStartBrowserSeekRequired
:
907 SetState(kStateWaitingForSeek
);
908 SetPendingStart(true);
910 RequestDemuxerSeek(GetInterpolatedTime(), true);
913 GetMediaTaskRunner()->PostTask(FROM_HERE
, internal_error_cb_
);
920 MediaCodecPlayer::StartStatus
MediaCodecPlayer::StartPlaybackDecoders() {
921 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
922 DVLOG(1) << __FUNCTION__
;
924 // Configure all streams before the start since we may discover that browser
926 MediaCodecPlayer::StartStatus status
= ConfigureDecoders();
927 if (status
!= kStartOk
)
930 bool preroll_required
= false;
931 status
= MaybePrerollDecoders(&preroll_required
);
932 if (preroll_required
)
935 return StartDecoders();
938 MediaCodecPlayer::StartStatus
MediaCodecPlayer::ConfigureDecoders() {
939 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
940 DVLOG(1) << __FUNCTION__
;
942 const bool do_audio
= !AudioFinished();
943 const bool do_video
= !VideoFinished();
945 // If there is nothing to play, the state machine should determine this at the
946 // prefetch state and never call this method.
947 DCHECK(do_audio
|| do_video
);
949 // Start with video: if browser seek is required it would
950 // not make sense to configure audio.
953 MediaCodecDecoder::ConfigStatus status
= video_decoder_
->Configure();
955 case MediaCodecDecoder::kConfigOk
:
957 case MediaCodecDecoder::kConfigKeyFrameRequired
:
958 // TODO(timav): post a task or return the status?
959 return kStartBrowserSeekRequired
;
960 case MediaCodecDecoder::kConfigFailure
:
966 MediaCodecDecoder::ConfigStatus status
= audio_decoder_
->Configure();
967 if (status
!= MediaCodecDecoder::kConfigOk
) {
975 MediaCodecPlayer::StartStatus
MediaCodecPlayer::MaybePrerollDecoders(
976 bool* preroll_required
) {
977 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
979 DVLOG(1) << __FUNCTION__
<< " current_time:" << GetInterpolatedTime();
981 // If requested, preroll is always done in the beginning of the playback,
982 // after prefetch. The request might not happen at all though, in which case
983 // we won't have prerolling phase. We need the prerolling when we (re)create
984 // the decoder, because its configuration and initialization (getting input,
985 // but not making output) can take time, and after the seek because there
986 // could be some data to be skipped and there is again initialization after
989 *preroll_required
= false;
992 const bool do_audio
= audio_decoder_
->NotCompletedAndNeedsPreroll();
996 const bool do_video
= video_decoder_
->NotCompletedAndNeedsPreroll();
1001 DVLOG(1) << __FUNCTION__
<< ": preroll is not required, skipping";
1005 *preroll_required
= true;
1008 DCHECK(do_audio
|| do_video
);
1010 DVLOG(1) << __FUNCTION__
<< ": preroll for " << count
<< " stream(s)";
1012 base::Closure preroll_cb
= base::BarrierClosure(
1013 count
, base::Bind(&MediaCodecPlayer::OnPrerollDone
, media_weak_this_
));
1015 if (do_audio
&& !audio_decoder_
->Preroll(preroll_cb
))
1016 return kStartFailed
;
1018 if (do_video
&& !video_decoder_
->Preroll(preroll_cb
))
1019 return kStartFailed
;
1024 MediaCodecPlayer::StartStatus
MediaCodecPlayer::StartDecoders() {
1025 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
1027 if (!interpolator_
.interpolating())
1028 interpolator_
.StartInterpolating();
1030 base::TimeDelta current_time
= GetInterpolatedTime();
1032 DVLOG(1) << __FUNCTION__
<< " current_time:" << current_time
;
1034 if (!AudioFinished()) {
1035 if (!audio_decoder_
->Start(current_time
))
1036 return kStartFailed
;
1038 // Attach listener on UI thread
1039 ui_task_runner_
->PostTask(FROM_HERE
, attach_listener_cb_
);
1042 if (!VideoFinished()) {
1043 if (!video_decoder_
->Start(current_time
))
1044 return kStartFailed
;
1050 void MediaCodecPlayer::StopDecoders() {
1051 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
1052 DVLOG(1) << __FUNCTION__
;
1054 video_decoder_
->SyncStop();
1055 audio_decoder_
->SyncStop();
1058 void MediaCodecPlayer::RequestToStopDecoders() {
1059 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
1060 DVLOG(1) << __FUNCTION__
;
1062 bool do_audio
= false;
1063 bool do_video
= false;
1065 if (audio_decoder_
->IsPrefetchingOrPlaying())
1067 if (video_decoder_
->IsPrefetchingOrPlaying())
1070 if (!do_audio
&& !do_video
) {
1071 GetMediaTaskRunner()->PostTask(
1072 FROM_HERE
, base::Bind(&MediaCodecPlayer::OnStopDone
, media_weak_this_
,
1073 DemuxerStream::UNKNOWN
));
1078 audio_decoder_
->RequestToStop();
1080 video_decoder_
->RequestToStop();
1083 void MediaCodecPlayer::RequestDemuxerSeek(base::TimeDelta seek_time
,
1084 bool is_browser_seek
) {
1085 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
1086 DVLOG(1) << __FUNCTION__
<< " " << seek_time
1087 << (is_browser_seek
? " BROWSER_SEEK" : "");
1089 // Flush decoders before requesting demuxer.
1090 audio_decoder_
->Flush();
1091 video_decoder_
->Flush();
1093 // Save active seek data. Logically it is attached to kStateWaitingForSeek.
1094 DCHECK_EQ(kStateWaitingForSeek
, state_
);
1095 seek_info_
.reset(new SeekInfo(seek_time
, is_browser_seek
));
1097 demuxer_
->RequestDemuxerSeek(seek_time
, is_browser_seek
);
1100 void MediaCodecPlayer::ReleaseDecoderResources() {
1101 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
1102 DVLOG(1) << __FUNCTION__
;
1105 audio_decoder_
->ReleaseDecoderResources();
1108 video_decoder_
->ReleaseDecoderResources();
1110 // At this point decoder threads should not be running
1111 if (interpolator_
.interpolating())
1112 interpolator_
.StopInterpolating();
1115 void MediaCodecPlayer::CreateDecoders() {
1116 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
1117 DVLOG(1) << __FUNCTION__
;
1119 internal_error_cb_
= base::Bind(&MediaCodecPlayer::OnError
, media_weak_this_
);
1121 audio_decoder_
.reset(new MediaCodecAudioDecoder(
1122 GetMediaTaskRunner(), base::Bind(&MediaCodecPlayer::RequestDemuxerData
,
1123 media_weak_this_
, DemuxerStream::AUDIO
),
1124 base::Bind(&MediaCodecPlayer::OnStarvation
, media_weak_this_
,
1125 DemuxerStream::AUDIO
),
1126 base::Bind(&MediaCodecPlayer::OnDecoderDrained
, media_weak_this_
,
1127 DemuxerStream::AUDIO
),
1128 base::Bind(&MediaCodecPlayer::OnStopDone
, media_weak_this_
,
1129 DemuxerStream::AUDIO
),
1131 base::Bind(&MediaCodecPlayer::OnTimeIntervalUpdate
, media_weak_this_
,
1132 DemuxerStream::AUDIO
)));
1134 video_decoder_
.reset(new MediaCodecVideoDecoder(
1135 GetMediaTaskRunner(), base::Bind(&MediaCodecPlayer::RequestDemuxerData
,
1136 media_weak_this_
, DemuxerStream::VIDEO
),
1137 base::Bind(&MediaCodecPlayer::OnStarvation
, media_weak_this_
,
1138 DemuxerStream::VIDEO
),
1139 base::Bind(&MediaCodecPlayer::OnDecoderDrained
, media_weak_this_
,
1140 DemuxerStream::VIDEO
),
1141 base::Bind(&MediaCodecPlayer::OnStopDone
, media_weak_this_
,
1142 DemuxerStream::VIDEO
),
1144 base::Bind(&MediaCodecPlayer::OnTimeIntervalUpdate
, media_weak_this_
,
1145 DemuxerStream::VIDEO
),
1146 base::Bind(&MediaCodecPlayer::OnVideoResolutionChanged
, media_weak_this_
),
1147 base::Bind(&MediaCodecPlayer::OnVideoCodecCreated
, media_weak_this_
)));
1150 bool MediaCodecPlayer::AudioFinished() const {
1151 return audio_decoder_
->IsCompleted() || !audio_decoder_
->HasStream();
1154 bool MediaCodecPlayer::VideoFinished() const {
1155 return video_decoder_
->IsCompleted() || !video_decoder_
->HasStream();
1158 base::TimeDelta
MediaCodecPlayer::GetInterpolatedTime() {
1159 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
1161 base::TimeDelta interpolated_time
= interpolator_
.GetInterpolatedTime();
1162 return std::min(interpolated_time
, duration_
);
1165 #undef RETURN_STRING
1166 #define RETURN_STRING(x) \
1170 const char* MediaCodecPlayer::AsString(PlayerState state
) {
1172 RETURN_STRING(kStatePaused
);
1173 RETURN_STRING(kStateWaitingForConfig
);
1174 RETURN_STRING(kStatePrefetching
);
1175 RETURN_STRING(kStatePlaying
);
1176 RETURN_STRING(kStateStopping
);
1177 RETURN_STRING(kStateWaitingForSurface
);
1178 RETURN_STRING(kStateWaitingForSeek
);
1179 RETURN_STRING(kStateError
);
1181 return nullptr; // crash early
1184 #undef RETURN_STRING
1186 } // namespace media