Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / media / base / android / media_codec_player.cc
blob70fc00d5f86e68b367c2b48260bd2a72b314f737
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"
8 #include "base/bind.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, ...) \
19 do { \
20 if (!GetMediaTaskRunner()->BelongsToCurrentThread()) { \
21 DCHECK(ui_task_runner_->BelongsToCurrentThread()); \
22 GetMediaTaskRunner()->PostTask( \
23 FROM_HERE, base::Bind(&MediaCodecPlayer::METHOD, media_weak_this_, \
24 ##__VA_ARGS__)); \
25 return; \
26 } \
27 } while (0)
29 namespace media {
31 class MediaThread : public base::Thread {
32 public:
33 MediaThread() : base::Thread("BrowserMediaThread") {
34 Start();
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(
50 int player_id,
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,
56 manager.get(),
57 request_media_resources_cb,
58 frame_url),
59 ui_task_runner_(base::ThreadTaskRunnerHandle::Get()),
60 demuxer_(demuxer.Pass()),
61 state_(kStatePaused),
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);
72 completion_cb_ =
73 base::Bind(&MediaPlayerManager::OnPlaybackComplete, manager, player_id);
74 seek_done_cb_ =
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);
79 detach_listener_cb_ =
80 base::Bind(&MediaPlayerAndroid::DetachListener, WeakPtrForUIThread());
81 metadata_changed_cb_ = base::Bind(&MediaPlayerAndroid::OnMediaMetadataChanged,
82 WeakPtrForUIThread());
83 time_update_cb_ =
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.
103 if (video_decoder_)
104 video_decoder_->ReleaseDecoderResources();
105 if (audio_decoder_)
106 audio_decoder_->ReleaseDecoderResources();
109 void MediaCodecPlayer::Initialize() {
110 DVLOG(1) << __FUNCTION__;
111 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
113 interpolator_.SetUpperBound(base::TimeDelta());
115 CreateDecoders();
117 // This call might in turn call MediaCodecPlayer::OnDemuxerConfigsAvailable()
118 // which propagates configs into decoders. Therefore CreateDecoders() should
119 // be called first.
120 demuxer_->Initialize(this);
123 // The implementation of MediaPlayerAndroid interface.
125 void MediaCodecPlayer::DeleteOnCorrectThread() {
126 DVLOG(1) << __FUNCTION__;
127 DCHECK(ui_task_runner_->BelongsToCurrentThread());
129 DetachListener();
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";
151 return;
154 video_decoder_->SetVideoSurface(surface.Pass());
156 if (surface_is_empty) {
157 // Remove video surface.
158 switch (state_) {
159 case kStatePlaying:
160 if (VideoFinished())
161 break;
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();
171 } else {
172 SetState(kStateWaitingForConfig);
174 break;
176 default:
177 break; // ignore
179 } else {
180 // Replace video surface.
181 switch (state_) {
182 case kStateWaitingForSurface:
183 SetState(kStatePlaying);
184 StartPlaybackOrBrowserSeek();
185 break;
187 case kStatePlaying:
188 if (VideoFinished())
189 break;
191 DVLOG(1) << __FUNCTION__ << ": requesting to stop and restart";
192 SetState(kStateStopping);
193 RequestToStopDecoders();
194 SetPendingStart(true);
195 break;
197 default:
198 break; // ignore
203 void MediaCodecPlayer::Start() {
204 RUN_ON_MEDIA_THREAD(Start);
206 DVLOG(1) << __FUNCTION__;
208 switch (state_) {
209 case kStatePaused:
210 // Prefetch or wait for initial configuration.
211 if (HasAudio() || HasVideo()) {
212 SetState(kStatePrefetching);
213 StartPrefetchDecoders();
214 } else {
215 SetState(kStateWaitingForConfig);
217 break;
218 case kStateStopping:
219 case kStateWaitingForSeek:
220 SetPendingStart(true);
221 break;
222 case kStateWaitingForConfig:
223 case kStatePrefetching:
224 case kStatePlaying:
225 case kStateWaitingForSurface:
226 case kStateError:
227 break; // Ignore
228 default:
229 NOTREACHED();
230 break;
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);
241 switch (state_) {
242 case kStateWaitingForConfig:
243 case kStatePrefetching:
244 case kStateWaitingForSurface:
245 SetState(kStatePaused);
246 StopDecoders();
247 break;
248 case kStatePlaying:
249 SetState(kStateStopping);
250 RequestToStopDecoders();
251 break;
252 case kStatePaused:
253 case kStateStopping:
254 case kStateWaitingForSeek:
255 case kStateError:
256 break; // Ignore
257 default:
258 NOTREACHED();
259 break;
263 void MediaCodecPlayer::SeekTo(base::TimeDelta timestamp) {
264 RUN_ON_MEDIA_THREAD(SeekTo, timestamp);
266 DVLOG(1) << __FUNCTION__ << " " << timestamp;
268 switch (state_) {
269 case kStatePaused:
270 SetState(kStateWaitingForSeek);
271 RequestDemuxerSeek(timestamp);
272 break;
273 case kStateWaitingForConfig:
274 case kStatePrefetching:
275 case kStateWaitingForSurface:
276 SetState(kStateWaitingForSeek);
277 StopDecoders();
278 SetPendingStart(true);
279 RequestDemuxerSeek(timestamp);
280 break;
281 case kStatePlaying:
282 SetState(kStateStopping);
283 RequestToStopDecoders();
284 SetPendingStart(true);
285 SetPendingSeek(timestamp);
286 break;
287 case kStateStopping:
288 SetPendingSeek(timestamp);
289 break;
290 case kStateWaitingForSeek:
291 SetPendingSeek(timestamp);
292 break;
293 case kStateError:
294 break; // ignore
295 default:
296 NOTREACHED();
297 break;
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
355 // Media thread.
356 return state_ == kStatePlaying || state_ == kStateStopping;
359 bool MediaCodecPlayer::CanPause() {
360 DCHECK(ui_task_runner_->BelongsToCurrentThread());
361 NOTIMPLEMENTED();
362 return false;
365 bool MediaCodecPlayer::CanSeekForward() {
366 DCHECK(ui_task_runner_->BelongsToCurrentThread());
367 NOTIMPLEMENTED();
368 return false;
371 bool MediaCodecPlayer::CanSeekBackward() {
372 DCHECK(ui_task_runner_->BelongsToCurrentThread());
373 NOTIMPLEMENTED();
374 return false;
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.
381 return true;
384 void MediaCodecPlayer::SetCdm(BrowserCdm* cdm) {
385 DCHECK(ui_task_runner_->BelongsToCurrentThread());
386 NOTIMPLEMENTED();
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));
457 seek_info_.reset();
458 return;
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);
468 return;
471 if (HasPendingStart()) {
472 SetPendingStart(false);
473 // Prefetch or wait for initial configuration.
474 if (HasAudio() || HasVideo()) {
475 SetState(kStatePrefetching);
476 StartPrefetchDecoders();
477 } else {
478 SetState(kStateWaitingForConfig);
480 } else {
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));
488 seek_info_.reset();
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();
534 else
535 return false;
538 // http://crbug.com/526755
539 void MediaCodecPlayer::SetVerboseForTests(bool value) {
540 DCHECK(ui_task_runner_->BelongsToCurrentThread());
542 if (audio_decoder_)
543 audio_decoder_->SetVerboseForTests(value);
544 if (video_decoder_)
545 video_decoder_->SetVerboseForTests(value);
548 // Events from Player, called on UI thread
550 void MediaCodecPlayer::OnMediaMetadataChanged(base::TimeDelta duration,
551 const gfx::Size& video_size) {
552 DCHECK(ui_task_runner_->BelongsToCurrentThread());
554 if (duration != kNoTimestamp())
555 metadata_cache_.duration = duration;
557 if (!video_size.IsEmpty())
558 metadata_cache_.video_size = video_size;
560 manager()->OnMediaMetadataChanged(player_id(), metadata_cache_.duration,
561 metadata_cache_.video_size.width(),
562 metadata_cache_.video_size.height(), true);
565 void MediaCodecPlayer::OnTimeUpdate(base::TimeDelta current_timestamp,
566 base::TimeTicks current_time_ticks) {
567 DCHECK(ui_task_runner_->BelongsToCurrentThread());
569 current_time_cache_ = current_timestamp;
570 manager()->OnTimeUpdate(player_id(), current_timestamp, current_time_ticks);
573 // Events from Decoders, called on Media thread
575 void MediaCodecPlayer::RequestDemuxerData(DemuxerStream::Type stream_type) {
576 DVLOG(2) << __FUNCTION__ << " streamType:" << stream_type;
578 // Use this method instead of directly binding with
579 // DemuxerAndroid::RequestDemuxerData() to avoid the race condition on
580 // deletion:
581 // 1. DeleteSoon is posted from UI to Media thread.
582 // 2. RequestDemuxerData callback is posted from Decoder to Media thread.
583 // 3. DeleteSoon arrives, we delete the player and detach from
584 // BrowserDemuxerAndroid.
585 // 4. RequestDemuxerData is processed by the media thread queue. Since the
586 // weak_ptr was invalidated in (3), this is a no-op. If we used
587 // DemuxerAndroid::RequestDemuxerData() it would arrive and will try to
588 // call the client, but the client (i.e. this player) would not exist.
589 demuxer_->RequestDemuxerData(stream_type);
592 void MediaCodecPlayer::OnPrefetchDone() {
593 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
595 if (state_ != kStatePrefetching) {
596 DVLOG(1) << __FUNCTION__ << " wrong state " << AsString(state_)
597 << " ignoring";
598 return; // Ignore
601 DVLOG(1) << __FUNCTION__;
603 if (!HasAudio() && !HasVideo()) {
604 // No configuration at all after prefetching.
605 // This is an error, initial configuration is expected
606 // before the first data chunk.
607 DCHECK(!internal_error_cb_.is_null());
608 GetMediaTaskRunner()->PostTask(FROM_HERE, internal_error_cb_);
609 return;
612 if (HasVideo() && !video_decoder_->HasVideoSurface()) {
613 SetState(kStateWaitingForSurface);
614 return;
617 SetState(kStatePlaying);
618 StartPlaybackOrBrowserSeek();
621 void MediaCodecPlayer::OnPrerollDone() {
622 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
624 // http://crbug.com/526755
625 DVLOG(0) << __FUNCTION__;
627 if (state_ != kStatePlaying) {
628 DVLOG(1) << __FUNCTION__ << ": in state " << AsString(state_)
629 << ", ignoring";
630 return;
633 StartStatus status = StartDecoders();
634 if (status != kStartOk)
635 GetMediaTaskRunner()->PostTask(FROM_HERE, internal_error_cb_);
638 void MediaCodecPlayer::OnDecoderDrained(DemuxerStream::Type type) {
639 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
640 DVLOG(1) << __FUNCTION__ << " " << type;
642 // We expect that OnStopDone() comes next.
644 DCHECK(type == DemuxerStream::AUDIO || type == DemuxerStream::VIDEO);
645 DCHECK(state_ == kStatePlaying || state_ == kStateStopping)
646 << __FUNCTION__ << " illegal state: " << AsString(state_);
648 switch (state_) {
649 case kStatePlaying:
650 SetState(kStateStopping);
651 SetPendingStart(true);
653 if (type == DemuxerStream::AUDIO && !VideoFinished()) {
654 DVLOG(1) << __FUNCTION__ << " requesting to stop video";
655 video_decoder_->RequestToStop();
656 } else if (type == DemuxerStream::VIDEO && !AudioFinished()) {
657 DVLOG(1) << __FUNCTION__ << " requesting to stop audio";
658 audio_decoder_->RequestToStop();
660 break;
662 default:
663 break;
667 void MediaCodecPlayer::OnStopDone(DemuxerStream::Type type) {
668 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
669 DVLOG(1) << __FUNCTION__ << " " << type
670 << " interpolated time:" << GetInterpolatedTime();
672 if (!(audio_decoder_->IsStopped() && video_decoder_->IsStopped())) {
673 DVLOG(1) << __FUNCTION__ << " both audio and video has to be stopped"
674 << ", ignoring";
675 return; // Wait until other stream is stopped
678 // At this point decoder threads should not be running
679 if (interpolator_.interpolating())
680 interpolator_.StopInterpolating();
682 base::TimeDelta seek_time;
683 switch (state_) {
684 case kStateStopping: {
685 base::TimeDelta seek_time = GetPendingSeek();
686 if (seek_time != kNoTimestamp()) {
687 SetState(kStateWaitingForSeek);
688 SetPendingSeek(kNoTimestamp());
689 RequestDemuxerSeek(seek_time);
690 } else if (HasPendingStart()) {
691 SetPendingStart(false);
692 SetState(kStatePrefetching);
693 StartPrefetchDecoders();
694 } else {
695 SetState(kStatePaused);
697 } break;
698 case kStatePlaying:
699 // Unexpected stop means completion
700 SetState(kStatePaused);
701 break;
702 default:
703 // DVLOG(0) << __FUNCTION__ << " illegal state: " << AsString(state_);
704 // NOTREACHED();
705 // Ignore! There can be a race condition: audio posts OnStopDone,
706 // then video posts, then first OnStopDone arrives at which point
707 // both streams are already stopped, then second OnStopDone arrives. When
708 // the second one arrives, the state us not kStateStopping any more.
709 return;
712 // DetachListener to UI thread
713 ui_task_runner_->PostTask(FROM_HERE, detach_listener_cb_);
715 if (AudioFinished() && VideoFinished())
716 ui_task_runner_->PostTask(FROM_HERE, completion_cb_);
719 void MediaCodecPlayer::OnError() {
720 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
721 DVLOG(1) << __FUNCTION__;
723 // kStateError blocks all events
724 SetState(kStateError);
726 ReleaseDecoderResources();
728 ui_task_runner_->PostTask(FROM_HERE,
729 base::Bind(error_cb_, MEDIA_ERROR_DECODE));
732 void MediaCodecPlayer::OnStarvation(DemuxerStream::Type type) {
733 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
734 DVLOG(1) << __FUNCTION__ << " stream type:" << type;
736 if (state_ != kStatePlaying)
737 return; // Ignore
739 SetState(kStateStopping);
740 RequestToStopDecoders();
741 SetPendingStart(true);
744 void MediaCodecPlayer::OnTimeIntervalUpdate(DemuxerStream::Type type,
745 base::TimeDelta now_playing,
746 base::TimeDelta last_buffered,
747 bool postpone) {
748 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
750 DVLOG(2) << __FUNCTION__ << ": stream type:" << type << " [" << now_playing
751 << "," << last_buffered << "]" << (postpone ? " postpone" : "");
753 // For testing only: report time interval as we receive it from decoders
754 // as an indication of what is being rendered. Do not post this callback
755 // for postponed frames: although the PTS is correct, the tests also care
756 // about the wall clock time this callback arrives and deduce the rendering
757 // moment from it.
758 if (!decoders_time_cb_.is_null() && !postpone) {
759 ui_task_runner_->PostTask(
760 FROM_HERE,
761 base::Bind(decoders_time_cb_, type, now_playing, last_buffered));
764 // I assume that audio stream cannot be added after we get configs by
765 // OnDemuxerConfigsAvailable(), but that audio can finish early.
767 if (type == DemuxerStream::VIDEO) {
768 // Ignore video PTS if there is audio stream or if it's behind current
769 // time as set by audio stream.
770 if (!AudioFinished() ||
771 (HasAudio() && now_playing < interpolator_.GetInterpolatedTime()))
772 return;
775 interpolator_.SetBounds(now_playing, last_buffered);
777 // Post to UI thread
778 if (!postpone) {
779 ui_task_runner_->PostTask(FROM_HERE,
780 base::Bind(time_update_cb_, GetInterpolatedTime(),
781 base::TimeTicks::Now()));
785 void MediaCodecPlayer::OnVideoCodecCreated() {
786 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
788 // This callback requests resources by releasing other players.
789 ui_task_runner_->PostTask(FROM_HERE, request_resources_cb_);
792 void MediaCodecPlayer::OnVideoResolutionChanged(const gfx::Size& size) {
793 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
795 DVLOG(1) << __FUNCTION__ << " " << size.width() << "x" << size.height();
797 // Update cache and notify manager on UI thread
798 ui_task_runner_->PostTask(
799 FROM_HERE, base::Bind(metadata_changed_cb_, kNoTimestamp(), size));
802 // State machine operations, called on Media thread
804 void MediaCodecPlayer::SetState(PlayerState new_state) {
805 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
807 DVLOG(1) << "SetState:" << AsString(state_) << " -> " << AsString(new_state);
808 state_ = new_state;
811 void MediaCodecPlayer::SetPendingStart(bool need_to_start) {
812 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
813 DVLOG(1) << __FUNCTION__ << ": " << need_to_start;
814 pending_start_ = need_to_start;
817 bool MediaCodecPlayer::HasPendingStart() const {
818 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
819 return pending_start_;
822 void MediaCodecPlayer::SetPendingSeek(base::TimeDelta timestamp) {
823 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
824 DVLOG(1) << __FUNCTION__ << ": " << timestamp;
825 pending_seek_ = timestamp;
828 base::TimeDelta MediaCodecPlayer::GetPendingSeek() const {
829 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
830 return pending_seek_;
833 bool MediaCodecPlayer::HasAudio() const {
834 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
835 return audio_decoder_->HasStream();
838 bool MediaCodecPlayer::HasVideo() const {
839 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
840 return video_decoder_->HasStream();
843 void MediaCodecPlayer::SetDemuxerConfigs(const DemuxerConfigs& configs) {
844 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
845 DVLOG(1) << __FUNCTION__ << " " << configs;
847 DCHECK(audio_decoder_);
848 DCHECK(video_decoder_);
850 // At least one valid codec must be present.
851 DCHECK(configs.audio_codec != kUnknownAudioCodec ||
852 configs.video_codec != kUnknownVideoCodec);
854 if (configs.audio_codec != kUnknownAudioCodec)
855 audio_decoder_->SetDemuxerConfigs(configs);
857 if (configs.video_codec != kUnknownVideoCodec)
858 video_decoder_->SetDemuxerConfigs(configs);
860 if (state_ == kStateWaitingForConfig) {
861 SetState(kStatePrefetching);
862 StartPrefetchDecoders();
866 void MediaCodecPlayer::StartPrefetchDecoders() {
867 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
868 DVLOG(1) << __FUNCTION__;
870 bool do_audio = false;
871 bool do_video = false;
872 int count = 0;
873 if (!AudioFinished()) {
874 do_audio = true;
875 ++count;
877 if (!VideoFinished()) {
878 do_video = true;
879 ++count;
882 DCHECK_LT(0, count); // at least one decoder should be active
884 base::Closure prefetch_cb = base::BarrierClosure(
885 count, base::Bind(&MediaCodecPlayer::OnPrefetchDone, media_weak_this_));
887 if (do_audio)
888 audio_decoder_->Prefetch(prefetch_cb);
890 if (do_video)
891 video_decoder_->Prefetch(prefetch_cb);
894 void MediaCodecPlayer::StartPlaybackOrBrowserSeek() {
895 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
896 DVLOG(1) << __FUNCTION__;
898 // TODO(timav): consider replacing this method with posting a
899 // browser seek task (i.e. generate an event) from StartPlaybackDecoders().
901 StartStatus status = StartPlaybackDecoders();
903 switch (status) {
904 case kStartBrowserSeekRequired:
905 // Browser seek
906 SetState(kStateWaitingForSeek);
907 SetPendingStart(true);
908 StopDecoders();
909 RequestDemuxerSeek(GetInterpolatedTime(), true);
910 break;
911 case kStartFailed:
912 GetMediaTaskRunner()->PostTask(FROM_HERE, internal_error_cb_);
913 break;
914 case kStartOk:
915 break;
919 MediaCodecPlayer::StartStatus MediaCodecPlayer::StartPlaybackDecoders() {
920 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
921 DVLOG(1) << __FUNCTION__;
923 // Configure all streams before the start since we may discover that browser
924 // seek is required.
925 MediaCodecPlayer::StartStatus status = ConfigureDecoders();
926 if (status != kStartOk)
927 return status;
929 bool preroll_required = false;
930 status = MaybePrerollDecoders(&preroll_required);
931 if (preroll_required)
932 return status;
934 return StartDecoders();
937 MediaCodecPlayer::StartStatus MediaCodecPlayer::ConfigureDecoders() {
938 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
939 DVLOG(1) << __FUNCTION__;
941 const bool do_audio = !AudioFinished();
942 const bool do_video = !VideoFinished();
944 // If there is nothing to play, the state machine should determine this at the
945 // prefetch state and never call this method.
946 DCHECK(do_audio || do_video);
948 // Start with video: if browser seek is required it would
949 // not make sense to configure audio.
951 if (do_video) {
952 MediaCodecDecoder::ConfigStatus status = video_decoder_->Configure();
953 switch (status) {
954 case MediaCodecDecoder::kConfigOk:
955 break;
956 case MediaCodecDecoder::kConfigKeyFrameRequired:
957 // TODO(timav): post a task or return the status?
958 return kStartBrowserSeekRequired;
959 case MediaCodecDecoder::kConfigFailure:
960 return kStartFailed;
964 if (do_audio) {
965 MediaCodecDecoder::ConfigStatus status = audio_decoder_->Configure();
966 if (status != MediaCodecDecoder::kConfigOk) {
967 return kStartFailed;
971 return kStartOk;
974 MediaCodecPlayer::StartStatus MediaCodecPlayer::MaybePrerollDecoders(
975 bool* preroll_required) {
976 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
978 DVLOG(1) << __FUNCTION__ << " current_time:" << GetInterpolatedTime();
980 // If requested, preroll is always done in the beginning of the playback,
981 // after prefetch. The request might not happen at all though, in which case
982 // we won't have prerolling phase. We need the prerolling when we (re)create
983 // the decoder, because its configuration and initialization (getting input,
984 // but not making output) can take time, and after the seek because there
985 // could be some data to be skipped and there is again initialization after
986 // the flush.
988 *preroll_required = false;
990 int count = 0;
991 const bool do_audio = audio_decoder_->NotCompletedAndNeedsPreroll();
992 if (do_audio)
993 ++count;
995 const bool do_video = video_decoder_->NotCompletedAndNeedsPreroll();
996 if (do_video)
997 ++count;
999 if (count == 0) {
1000 DVLOG(1) << __FUNCTION__ << ": preroll is not required, skipping";
1001 return kStartOk;
1004 *preroll_required = true;
1006 DCHECK(count > 0);
1007 DCHECK(do_audio || do_video);
1009 DVLOG(1) << __FUNCTION__ << ": preroll for " << count << " stream(s)";
1011 base::Closure preroll_cb = base::BarrierClosure(
1012 count, base::Bind(&MediaCodecPlayer::OnPrerollDone, media_weak_this_));
1014 if (do_audio && !audio_decoder_->Preroll(preroll_cb))
1015 return kStartFailed;
1017 if (do_video && !video_decoder_->Preroll(preroll_cb))
1018 return kStartFailed;
1020 return kStartOk;
1023 MediaCodecPlayer::StartStatus MediaCodecPlayer::StartDecoders() {
1024 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
1026 if (!interpolator_.interpolating())
1027 interpolator_.StartInterpolating();
1029 base::TimeDelta current_time = GetInterpolatedTime();
1031 // http://crbug.com/526755
1032 DVLOG(0) << __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;
1047 return kStartOk;
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())
1066 do_audio = true;
1067 if (video_decoder_->IsPrefetchingOrPlaying())
1068 do_video = true;
1070 if (!do_audio && !do_video) {
1071 GetMediaTaskRunner()->PostTask(
1072 FROM_HERE, base::Bind(&MediaCodecPlayer::OnStopDone, media_weak_this_,
1073 DemuxerStream::UNKNOWN));
1074 return;
1077 if (do_audio)
1078 audio_decoder_->RequestToStop();
1079 if (do_video)
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__;
1104 if (audio_decoder_)
1105 audio_decoder_->ReleaseDecoderResources();
1107 if (video_decoder_)
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),
1130 internal_error_cb_,
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),
1143 internal_error_cb_,
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) \
1167 case x: \
1168 return #x;
1170 const char* MediaCodecPlayer::AsString(PlayerState state) {
1171 switch (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