Roll src/third_party/WebKit d10c917:a1123a1 (svn 198729:198730)
[chromium-blink-merge.git] / media / base / android / media_source_player.cc
blob460fbec8fb6ced9c158d034b403ee382df4c37e7
1 // Copyright (c) 2013 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_source_player.h"
7 #include <limits>
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/barrier_closure.h"
12 #include "base/basictypes.h"
13 #include "base/bind.h"
14 #include "base/callback_helpers.h"
15 #include "base/logging.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/trace_event/trace_event.h"
18 #include "media/base/android/audio_decoder_job.h"
19 #include "media/base/android/media_player_manager.h"
20 #include "media/base/android/video_decoder_job.h"
22 namespace media {
24 MediaSourcePlayer::MediaSourcePlayer(
25 int player_id,
26 MediaPlayerManager* manager,
27 const RequestMediaResourcesCB& request_media_resources_cb,
28 scoped_ptr<DemuxerAndroid> demuxer,
29 const GURL& frame_url)
30 : MediaPlayerAndroid(player_id,
31 manager,
32 request_media_resources_cb,
33 frame_url),
34 demuxer_(demuxer.Pass()),
35 pending_event_(NO_EVENT_PENDING),
36 playing_(false),
37 interpolator_(&default_tick_clock_),
38 doing_browser_seek_(false),
39 pending_seek_(false),
40 drm_bridge_(NULL),
41 cdm_registration_id_(0),
42 is_waiting_for_key_(false),
43 key_added_while_decode_pending_(false),
44 is_waiting_for_audio_decoder_(false),
45 is_waiting_for_video_decoder_(false),
46 prerolling_(true),
47 weak_factory_(this) {
48 audio_decoder_job_.reset(new AudioDecoderJob(
49 base::Bind(&DemuxerAndroid::RequestDemuxerData,
50 base::Unretained(demuxer_.get()),
51 DemuxerStream::AUDIO),
52 base::Bind(&MediaSourcePlayer::OnDemuxerConfigsChanged,
53 weak_factory_.GetWeakPtr())));
54 video_decoder_job_.reset(new VideoDecoderJob(
55 base::Bind(&DemuxerAndroid::RequestDemuxerData,
56 base::Unretained(demuxer_.get()),
57 DemuxerStream::VIDEO),
58 base::Bind(request_media_resources_cb_, player_id),
59 base::Bind(&MediaSourcePlayer::OnDemuxerConfigsChanged,
60 weak_factory_.GetWeakPtr())));
61 demuxer_->Initialize(this);
62 interpolator_.SetUpperBound(base::TimeDelta());
63 weak_this_ = weak_factory_.GetWeakPtr();
66 MediaSourcePlayer::~MediaSourcePlayer() {
67 Release();
68 DCHECK_EQ(!drm_bridge_, !cdm_registration_id_);
69 if (drm_bridge_) {
70 drm_bridge_->UnregisterPlayer(cdm_registration_id_);
71 cdm_registration_id_ = 0;
75 void MediaSourcePlayer::SetVideoSurface(gfx::ScopedJavaSurface surface) {
76 DVLOG(1) << __FUNCTION__;
77 if (!video_decoder_job_->SetVideoSurface(surface.Pass()))
78 return;
79 // Retry video decoder creation.
80 RetryDecoderCreation(false, true);
83 void MediaSourcePlayer::ScheduleSeekEventAndStopDecoding(
84 base::TimeDelta seek_time) {
85 DVLOG(1) << __FUNCTION__ << "(" << seek_time.InSecondsF() << ")";
86 DCHECK(!IsEventPending(SEEK_EVENT_PENDING));
88 pending_seek_ = false;
90 interpolator_.SetBounds(seek_time, seek_time);
92 if (audio_decoder_job_->is_decoding())
93 audio_decoder_job_->StopDecode();
94 if (video_decoder_job_->is_decoding())
95 video_decoder_job_->StopDecode();
97 SetPendingEvent(SEEK_EVENT_PENDING);
98 ProcessPendingEvents();
101 void MediaSourcePlayer::BrowserSeekToCurrentTime() {
102 DVLOG(1) << __FUNCTION__;
104 DCHECK(!IsEventPending(SEEK_EVENT_PENDING));
105 doing_browser_seek_ = true;
106 ScheduleSeekEventAndStopDecoding(GetCurrentTime());
109 bool MediaSourcePlayer::Seekable() {
110 // If the duration TimeDelta, converted to milliseconds from microseconds,
111 // is >= 2^31, then the media is assumed to be unbounded and unseekable.
112 // 2^31 is the bound due to java player using 32-bit integer for time
113 // values at millisecond resolution.
114 return duration_ <
115 base::TimeDelta::FromMilliseconds(std::numeric_limits<int32>::max());
118 void MediaSourcePlayer::Start() {
119 DVLOG(1) << __FUNCTION__;
121 playing_ = true;
123 StartInternal();
126 void MediaSourcePlayer::Pause(bool is_media_related_action) {
127 DVLOG(1) << __FUNCTION__;
129 // Since decoder jobs have their own thread, decoding is not fully paused
130 // until all the decoder jobs call MediaDecoderCallback(). It is possible
131 // that Start() is called while the player is waiting for
132 // MediaDecoderCallback(). In that case, decoding will continue when
133 // MediaDecoderCallback() is called.
134 playing_ = false;
135 start_time_ticks_ = base::TimeTicks();
137 SetAudible(false);
140 bool MediaSourcePlayer::IsPlaying() {
141 return playing_;
144 int MediaSourcePlayer::GetVideoWidth() {
145 return video_decoder_job_->output_width();
148 int MediaSourcePlayer::GetVideoHeight() {
149 return video_decoder_job_->output_height();
152 void MediaSourcePlayer::SeekTo(base::TimeDelta timestamp) {
153 DVLOG(1) << __FUNCTION__ << "(" << timestamp.InSecondsF() << ")";
155 if (IsEventPending(SEEK_EVENT_PENDING)) {
156 DCHECK(doing_browser_seek_) << "SeekTo while SeekTo in progress";
157 DCHECK(!pending_seek_) << "SeekTo while SeekTo pending browser seek";
159 // There is a browser seek currently in progress to obtain I-frame to feed
160 // a newly constructed video decoder. Remember this real seek request so
161 // it can be initiated once OnDemuxerSeekDone() occurs for the browser seek.
162 pending_seek_ = true;
163 pending_seek_time_ = timestamp;
164 return;
167 doing_browser_seek_ = false;
168 ScheduleSeekEventAndStopDecoding(timestamp);
171 base::TimeDelta MediaSourcePlayer::GetCurrentTime() {
172 return std::min(interpolator_.GetInterpolatedTime(), duration_);
175 base::TimeDelta MediaSourcePlayer::GetDuration() {
176 return duration_;
179 void MediaSourcePlayer::Release() {
180 DVLOG(1) << __FUNCTION__;
182 audio_decoder_job_->ReleaseDecoderResources();
183 video_decoder_job_->ReleaseDecoderResources();
185 // Prevent player restart, including job re-creation attempts.
186 playing_ = false;
188 decoder_starvation_callback_.Cancel();
190 SetAudible(false);
191 DetachListener();
194 void MediaSourcePlayer::SetVolume(double volume) {
195 audio_decoder_job_->SetVolume(volume);
198 bool MediaSourcePlayer::CanPause() {
199 return Seekable();
202 bool MediaSourcePlayer::CanSeekForward() {
203 return Seekable();
206 bool MediaSourcePlayer::CanSeekBackward() {
207 return Seekable();
210 bool MediaSourcePlayer::IsPlayerReady() {
211 return audio_decoder_job_ || video_decoder_job_;
214 void MediaSourcePlayer::StartInternal() {
215 DVLOG(1) << __FUNCTION__;
216 // If there are pending events, wait for them finish.
217 if (pending_event_ != NO_EVENT_PENDING)
218 return;
220 if (!manager()->RequestPlay(player_id())) {
221 Pause(true);
222 return;
225 // When we start, we could have new demuxed data coming in. This new data
226 // could be clear (not encrypted) or encrypted with different keys. So key
227 // related info should all be cleared.
228 is_waiting_for_key_ = false;
229 key_added_while_decode_pending_ = false;
230 AttachListener(NULL);
232 SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
233 ProcessPendingEvents();
236 void MediaSourcePlayer::OnDemuxerConfigsAvailable(
237 const DemuxerConfigs& configs) {
238 DVLOG(1) << __FUNCTION__;
239 DCHECK(!HasAudio() && !HasVideo());
241 duration_ = configs.duration;
243 audio_decoder_job_->SetDemuxerConfigs(configs);
244 video_decoder_job_->SetDemuxerConfigs(configs);
245 OnDemuxerConfigsChanged();
248 void MediaSourcePlayer::OnDemuxerDataAvailable(const DemuxerData& data) {
249 DVLOG(1) << __FUNCTION__ << "(" << data.type << ")";
250 DCHECK_LT(0u, data.access_units.size());
251 CHECK_GE(1u, data.demuxer_configs.size());
253 if (data.type == DemuxerStream::AUDIO)
254 audio_decoder_job_->OnDataReceived(data);
255 else if (data.type == DemuxerStream::VIDEO)
256 video_decoder_job_->OnDataReceived(data);
259 void MediaSourcePlayer::OnDemuxerDurationChanged(base::TimeDelta duration) {
260 duration_ = duration;
263 void MediaSourcePlayer::OnMediaCryptoReady() {
264 DCHECK(!drm_bridge_->GetMediaCrypto().is_null());
265 drm_bridge_->SetMediaCryptoReadyCB(base::Closure());
267 // Retry decoder creation if the decoders are waiting for MediaCrypto.
268 RetryDecoderCreation(true, true);
271 void MediaSourcePlayer::SetCdm(BrowserCdm* cdm) {
272 // Currently we don't support DRM change during the middle of playback, even
273 // if the player is paused.
274 // TODO(qinmin): support DRM change after playback has started.
275 // http://crbug.com/253792.
276 if (GetCurrentTime() > base::TimeDelta()) {
277 VLOG(0) << "Setting DRM bridge after playback has started. "
278 << "This is not well supported!";
281 if (drm_bridge_) {
282 NOTREACHED() << "Currently we do not support resetting CDM.";
283 return;
286 // Only MediaDrmBridge will be set on MediaSourcePlayer.
287 drm_bridge_ = static_cast<MediaDrmBridge*>(cdm);
289 cdm_registration_id_ = drm_bridge_->RegisterPlayer(
290 base::Bind(&MediaSourcePlayer::OnKeyAdded, weak_this_),
291 base::Bind(&MediaSourcePlayer::OnCdmUnset, weak_this_));
293 audio_decoder_job_->SetDrmBridge(drm_bridge_);
294 video_decoder_job_->SetDrmBridge(drm_bridge_);
296 if (drm_bridge_->GetMediaCrypto().is_null()) {
297 drm_bridge_->SetMediaCryptoReadyCB(
298 base::Bind(&MediaSourcePlayer::OnMediaCryptoReady, weak_this_));
299 return;
302 // If the player is previously waiting for CDM, retry decoder creation.
303 RetryDecoderCreation(true, true);
306 void MediaSourcePlayer::OnDemuxerSeekDone(
307 base::TimeDelta actual_browser_seek_time) {
308 DVLOG(1) << __FUNCTION__;
310 ClearPendingEvent(SEEK_EVENT_PENDING);
311 if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING))
312 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
314 if (pending_seek_) {
315 DVLOG(1) << __FUNCTION__ << "processing pending seek";
316 DCHECK(doing_browser_seek_);
317 pending_seek_ = false;
318 SeekTo(pending_seek_time_);
319 return;
322 // It is possible that a browser seek to I-frame had to seek to a buffered
323 // I-frame later than the requested one due to data removal or GC. Update
324 // player clock to the actual seek target.
325 if (doing_browser_seek_) {
326 DCHECK(actual_browser_seek_time != kNoTimestamp());
327 base::TimeDelta seek_time = actual_browser_seek_time;
328 // A browser seek must not jump into the past. Ideally, it seeks to the
329 // requested time, but it might jump into the future.
330 DCHECK(seek_time >= GetCurrentTime());
331 DVLOG(1) << __FUNCTION__ << " : setting clock to actual browser seek time: "
332 << seek_time.InSecondsF();
333 interpolator_.SetBounds(seek_time, seek_time);
334 audio_decoder_job_->SetBaseTimestamp(seek_time);
335 } else {
336 DCHECK(actual_browser_seek_time == kNoTimestamp());
339 base::TimeDelta current_time = GetCurrentTime();
340 // TODO(qinmin): Simplify the logic by using |start_presentation_timestamp_|
341 // to preroll media decoder jobs. Currently |start_presentation_timestamp_|
342 // is calculated from decoder output, while preroll relies on the access
343 // unit's timestamp. There are some differences between the two.
344 preroll_timestamp_ = current_time;
345 if (HasAudio())
346 audio_decoder_job_->BeginPrerolling(preroll_timestamp_);
347 if (HasVideo())
348 video_decoder_job_->BeginPrerolling(preroll_timestamp_);
349 prerolling_ = true;
351 if (!doing_browser_seek_)
352 manager()->OnSeekComplete(player_id(), current_time);
354 ProcessPendingEvents();
357 void MediaSourcePlayer::UpdateTimestamps(
358 base::TimeDelta current_presentation_timestamp,
359 base::TimeDelta max_presentation_timestamp) {
360 interpolator_.SetBounds(current_presentation_timestamp,
361 max_presentation_timestamp);
362 manager()->OnTimeUpdate(player_id(),
363 GetCurrentTime(),
364 base::TimeTicks::Now());
367 void MediaSourcePlayer::ProcessPendingEvents() {
368 DVLOG(1) << __FUNCTION__ << " : 0x" << std::hex << pending_event_;
369 // Wait for all the decoding jobs to finish before processing pending tasks.
370 if (video_decoder_job_->is_decoding()) {
371 DVLOG(1) << __FUNCTION__ << " : A video job is still decoding.";
372 return;
375 if (audio_decoder_job_->is_decoding()) {
376 DVLOG(1) << __FUNCTION__ << " : An audio job is still decoding.";
377 return;
380 if (IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
381 DVLOG(1) << __FUNCTION__ << " : PREFETCH_DONE still pending.";
382 return;
385 if (IsEventPending(SEEK_EVENT_PENDING)) {
386 DVLOG(1) << __FUNCTION__ << " : Handling SEEK_EVENT";
387 ClearDecodingData();
388 audio_decoder_job_->SetBaseTimestamp(GetCurrentTime());
389 demuxer_->RequestDemuxerSeek(GetCurrentTime(), doing_browser_seek_);
390 return;
393 if (IsEventPending(DECODER_CREATION_EVENT_PENDING)) {
394 // Don't continue if one of the decoder is not created.
395 if (is_waiting_for_audio_decoder_ || is_waiting_for_video_decoder_)
396 return;
397 ClearPendingEvent(DECODER_CREATION_EVENT_PENDING);
400 if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING)) {
401 DVLOG(1) << __FUNCTION__ << " : Handling PREFETCH_REQUEST_EVENT.";
403 int count = (AudioFinished() ? 0 : 1) + (VideoFinished() ? 0 : 1);
405 // It is possible that all streams have finished decode, yet starvation
406 // occurred during the last stream's EOS decode. In this case, prefetch is a
407 // no-op.
408 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
409 if (count == 0)
410 return;
412 SetPendingEvent(PREFETCH_DONE_EVENT_PENDING);
413 base::Closure barrier = BarrierClosure(
414 count, base::Bind(&MediaSourcePlayer::OnPrefetchDone, weak_this_));
416 if (!AudioFinished())
417 audio_decoder_job_->Prefetch(barrier);
419 if (!VideoFinished())
420 video_decoder_job_->Prefetch(barrier);
422 return;
425 DCHECK_EQ(pending_event_, NO_EVENT_PENDING);
427 // Now that all pending events have been handled, resume decoding if we are
428 // still playing.
429 if (playing_)
430 StartInternal();
433 void MediaSourcePlayer::MediaDecoderCallback(
434 bool is_audio, MediaCodecStatus status,
435 base::TimeDelta current_presentation_timestamp,
436 base::TimeDelta max_presentation_timestamp) {
437 DVLOG(1) << __FUNCTION__ << ": " << is_audio << ", " << status;
439 // TODO(xhwang): Drop IntToString() when http://crbug.com/303899 is fixed.
440 if (is_audio) {
441 TRACE_EVENT_ASYNC_END1("media",
442 "MediaSourcePlayer::DecodeMoreAudio",
443 audio_decoder_job_.get(),
444 "MediaCodecStatus",
445 base::IntToString(status));
446 } else {
447 TRACE_EVENT_ASYNC_END1("media",
448 "MediaSourcePlayer::DecodeMoreVideo",
449 video_decoder_job_.get(),
450 "MediaCodecStatus",
451 base::IntToString(status));
454 // Let tests hook the completion of this decode cycle.
455 if (!decode_callback_for_testing_.is_null())
456 base::ResetAndReturn(&decode_callback_for_testing_).Run();
458 bool is_clock_manager = is_audio || !HasAudio();
460 if (is_clock_manager)
461 decoder_starvation_callback_.Cancel();
463 if (status == MEDIA_CODEC_ERROR) {
464 DVLOG(1) << __FUNCTION__ << " : decode error";
465 Release();
466 manager()->OnError(player_id(), MEDIA_ERROR_DECODE);
467 return;
470 DCHECK(!IsEventPending(PREFETCH_DONE_EVENT_PENDING));
472 // Let |SEEK_EVENT_PENDING| (the highest priority event outside of
473 // |PREFETCH_DONE_EVENT_PENDING|) preempt output EOS detection here. Process
474 // any other pending events only after handling EOS detection.
475 if (IsEventPending(SEEK_EVENT_PENDING)) {
476 ProcessPendingEvents();
477 return;
480 if ((status == MEDIA_CODEC_OK || status == MEDIA_CODEC_INPUT_END_OF_STREAM) &&
481 is_clock_manager && current_presentation_timestamp != kNoTimestamp()) {
482 UpdateTimestamps(current_presentation_timestamp,
483 max_presentation_timestamp);
486 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM) {
487 PlaybackCompleted(is_audio);
488 if (is_clock_manager)
489 interpolator_.StopInterpolating();
492 if (pending_event_ != NO_EVENT_PENDING) {
493 ProcessPendingEvents();
494 return;
497 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM) {
498 if (is_audio)
499 SetAudible(false);
500 return;
503 if (!playing_) {
504 if (is_clock_manager)
505 interpolator_.StopInterpolating();
507 if (is_audio)
508 SetAudible(false);
509 return;
512 if (status == MEDIA_CODEC_NO_KEY) {
513 if (key_added_while_decode_pending_) {
514 DVLOG(2) << __FUNCTION__ << ": Key was added during decoding.";
515 ResumePlaybackAfterKeyAdded();
516 } else {
517 if (is_audio)
518 SetAudible(false);
520 is_waiting_for_key_ = true;
521 manager()->OnWaitingForDecryptionKey(player_id());
523 return;
526 // If |key_added_while_decode_pending_| is true and both audio and video
527 // decoding succeeded, we should clear |key_added_while_decode_pending_| here.
528 // But that would add more complexity into this function. If we don't clear it
529 // here, the worst case would be we call ResumePlaybackAfterKeyAdded() when
530 // we don't really have a new key. This should rarely happen and the
531 // performance impact should be pretty small.
532 // TODO(qinmin/xhwang): This class is complicated because we handle both audio
533 // and video in one file. If we separate them, we should be able to remove a
534 // lot of duplication.
536 // If the status is MEDIA_CODEC_ABORT, stop decoding new data. The player is
537 // in the middle of a seek or stop event and needs to wait for the IPCs to
538 // come.
539 if (status == MEDIA_CODEC_ABORT) {
540 if (is_audio)
541 SetAudible(false);
542 return;
545 if (prerolling_ && IsPrerollFinished(is_audio)) {
546 if (IsPrerollFinished(!is_audio)) {
547 prerolling_ = false;
548 StartInternal();
550 return;
553 // We successfully decoded a frame and going to the next one.
554 // Set the audible state.
555 if (is_audio) {
556 bool is_audible = !prerolling_ && audio_decoder_job_->volume() > 0;
557 SetAudible(is_audible);
560 if (is_clock_manager) {
561 // If we have a valid timestamp, start the starvation callback. Otherwise,
562 // reset the |start_time_ticks_| so that the next frame will not suffer
563 // from the decoding delay caused by the current frame.
564 if (current_presentation_timestamp != kNoTimestamp())
565 StartStarvationCallback(current_presentation_timestamp,
566 max_presentation_timestamp);
567 else
568 start_time_ticks_ = base::TimeTicks::Now();
571 if (is_audio)
572 DecodeMoreAudio();
573 else
574 DecodeMoreVideo();
577 bool MediaSourcePlayer::IsPrerollFinished(bool is_audio) const {
578 if (is_audio)
579 return !HasAudio() || !audio_decoder_job_->prerolling();
580 return !HasVideo() || !video_decoder_job_->prerolling();
583 void MediaSourcePlayer::DecodeMoreAudio() {
584 DVLOG(1) << __FUNCTION__;
585 DCHECK(!audio_decoder_job_->is_decoding());
586 DCHECK(!AudioFinished());
588 MediaDecoderJob::MediaDecoderJobStatus status = audio_decoder_job_->Decode(
589 start_time_ticks_,
590 start_presentation_timestamp_,
591 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_, true));
593 switch (status) {
594 case MediaDecoderJob::STATUS_SUCCESS:
595 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreAudio",
596 audio_decoder_job_.get());
597 break;
598 case MediaDecoderJob::STATUS_KEY_FRAME_REQUIRED:
599 NOTREACHED();
600 break;
601 case MediaDecoderJob::STATUS_FAILURE:
602 is_waiting_for_audio_decoder_ = true;
603 if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
604 SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
605 break;
609 void MediaSourcePlayer::DecodeMoreVideo() {
610 DVLOG(1) << __FUNCTION__;
611 DCHECK(!video_decoder_job_->is_decoding());
612 DCHECK(!VideoFinished());
614 MediaDecoderJob::MediaDecoderJobStatus status = video_decoder_job_->Decode(
615 start_time_ticks_,
616 start_presentation_timestamp_,
617 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_,
618 false));
620 switch (status) {
621 case MediaDecoderJob::STATUS_SUCCESS:
622 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreVideo",
623 video_decoder_job_.get());
624 break;
625 case MediaDecoderJob::STATUS_KEY_FRAME_REQUIRED:
626 BrowserSeekToCurrentTime();
627 break;
628 case MediaDecoderJob::STATUS_FAILURE:
629 is_waiting_for_video_decoder_ = true;
630 if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
631 SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
632 break;
636 void MediaSourcePlayer::PlaybackCompleted(bool is_audio) {
637 DVLOG(1) << __FUNCTION__ << "(" << is_audio << ")";
639 if (AudioFinished() && VideoFinished()) {
640 playing_ = false;
641 start_time_ticks_ = base::TimeTicks();
642 manager()->OnPlaybackComplete(player_id());
646 void MediaSourcePlayer::ClearDecodingData() {
647 DVLOG(1) << __FUNCTION__;
648 audio_decoder_job_->Flush();
649 video_decoder_job_->Flush();
650 start_time_ticks_ = base::TimeTicks();
653 bool MediaSourcePlayer::HasVideo() const {
654 return video_decoder_job_->HasStream();
657 bool MediaSourcePlayer::HasAudio() const {
658 return audio_decoder_job_->HasStream();
661 bool MediaSourcePlayer::AudioFinished() {
662 return audio_decoder_job_->OutputEOSReached() || !HasAudio();
665 bool MediaSourcePlayer::VideoFinished() {
666 return video_decoder_job_->OutputEOSReached() || !HasVideo();
669 void MediaSourcePlayer::OnDecoderStarved() {
670 DVLOG(1) << __FUNCTION__;
672 if (HasAudio()) {
673 // If the starvation timer fired but there are no encoded frames
674 // in the queue we believe the demuxer (i.e. renderer process) froze.
675 if (!audio_decoder_job_->HasData())
676 SetAudible(false);
679 SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
680 ProcessPendingEvents();
683 void MediaSourcePlayer::StartStarvationCallback(
684 base::TimeDelta current_presentation_timestamp,
685 base::TimeDelta max_presentation_timestamp) {
686 // 20ms was chosen because it is the typical size of a compressed audio frame.
687 // Anything smaller than this would likely cause unnecessary cycling in and
688 // out of the prefetch state.
689 const base::TimeDelta kMinStarvationTimeout =
690 base::TimeDelta::FromMilliseconds(20);
692 base::TimeDelta current_timestamp = GetCurrentTime();
693 base::TimeDelta timeout;
694 if (HasAudio()) {
695 timeout = max_presentation_timestamp - current_timestamp;
696 } else {
697 DCHECK(current_timestamp <= current_presentation_timestamp);
699 // For video only streams, fps can be estimated from the difference
700 // between the previous and current presentation timestamps. The
701 // previous presentation timestamp is equal to current_timestamp.
702 // TODO(qinmin): determine whether 2 is a good coefficient for estimating
703 // video frame timeout.
704 timeout = 2 * (current_presentation_timestamp - current_timestamp);
707 timeout = std::max(timeout, kMinStarvationTimeout);
709 decoder_starvation_callback_.Reset(
710 base::Bind(&MediaSourcePlayer::OnDecoderStarved, weak_this_));
711 base::MessageLoop::current()->PostDelayedTask(
712 FROM_HERE, decoder_starvation_callback_.callback(), timeout);
715 void MediaSourcePlayer::OnPrefetchDone() {
716 DVLOG(1) << __FUNCTION__;
717 DCHECK(!audio_decoder_job_->is_decoding());
718 DCHECK(!video_decoder_job_->is_decoding());
720 // A previously posted OnPrefetchDone() could race against a Release(). If
721 // Release() won the race, we should no longer have decoder jobs.
722 // TODO(qinmin/wolenetz): Maintain channel state to not double-request data
723 // or drop data received across Release()+Start(). See http://crbug.com/306314
724 // and http://crbug.com/304234.
725 if (!IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
726 DVLOG(1) << __FUNCTION__ << " : aborting";
727 return;
730 ClearPendingEvent(PREFETCH_DONE_EVENT_PENDING);
732 if (pending_event_ != NO_EVENT_PENDING) {
733 ProcessPendingEvents();
734 return;
737 if (!playing_)
738 return;
740 start_time_ticks_ = base::TimeTicks::Now();
741 start_presentation_timestamp_ = GetCurrentTime();
742 if (!interpolator_.interpolating())
743 interpolator_.StartInterpolating();
745 if (!AudioFinished())
746 DecodeMoreAudio();
748 if (!VideoFinished())
749 DecodeMoreVideo();
752 void MediaSourcePlayer::OnDemuxerConfigsChanged() {
753 manager()->OnMediaMetadataChanged(
754 player_id(), duration_, GetVideoWidth(), GetVideoHeight(), true);
757 const char* MediaSourcePlayer::GetEventName(PendingEventFlags event) {
758 // Please keep this in sync with PendingEventFlags.
759 static const char* kPendingEventNames[] = {
760 "PREFETCH_DONE",
761 "SEEK",
762 "DECODER_CREATION",
763 "PREFETCH_REQUEST",
766 int mask = 1;
767 for (size_t i = 0; i < arraysize(kPendingEventNames); ++i, mask <<= 1) {
768 if (event & mask)
769 return kPendingEventNames[i];
772 return "UNKNOWN";
775 bool MediaSourcePlayer::IsEventPending(PendingEventFlags event) const {
776 return pending_event_ & event;
779 void MediaSourcePlayer::SetPendingEvent(PendingEventFlags event) {
780 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
781 DCHECK_NE(event, NO_EVENT_PENDING);
782 DCHECK(!IsEventPending(event));
784 pending_event_ |= event;
787 void MediaSourcePlayer::ClearPendingEvent(PendingEventFlags event) {
788 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
789 DCHECK_NE(event, NO_EVENT_PENDING);
790 DCHECK(IsEventPending(event)) << GetEventName(event);
792 pending_event_ &= ~event;
795 void MediaSourcePlayer::RetryDecoderCreation(bool audio, bool video) {
796 if (audio)
797 is_waiting_for_audio_decoder_ = false;
798 if (video)
799 is_waiting_for_video_decoder_ = false;
800 if (IsEventPending(DECODER_CREATION_EVENT_PENDING))
801 ProcessPendingEvents();
804 void MediaSourcePlayer::OnKeyAdded() {
805 DVLOG(1) << __FUNCTION__;
807 if (is_waiting_for_key_) {
808 ResumePlaybackAfterKeyAdded();
809 return;
812 if ((audio_decoder_job_->is_content_encrypted() &&
813 audio_decoder_job_->is_decoding()) ||
814 (video_decoder_job_->is_content_encrypted() &&
815 video_decoder_job_->is_decoding())) {
816 DVLOG(1) << __FUNCTION__ << ": " << "Key added during pending decode.";
817 key_added_while_decode_pending_ = true;
821 void MediaSourcePlayer::ResumePlaybackAfterKeyAdded() {
822 DVLOG(1) << __FUNCTION__;
823 DCHECK(is_waiting_for_key_ || key_added_while_decode_pending_);
825 is_waiting_for_key_ = false;
826 key_added_while_decode_pending_ = false;
828 // StartInternal() will trigger a prefetch, where in most cases we'll just
829 // use previously received data.
830 if (playing_)
831 StartInternal();
834 void MediaSourcePlayer::OnCdmUnset() {
835 DVLOG(1) << __FUNCTION__;
836 DCHECK(drm_bridge_);
837 // TODO(xhwang): Currently this is only called during teardown. Support full
838 // detachment of CDM during playback. This will be needed when we start to
839 // support setMediaKeys(0) (see http://crbug.com/330324), or when we release
840 // MediaDrm when the video is paused, or when the device goes to sleep (see
841 // http://crbug.com/272421).
842 audio_decoder_job_->SetDrmBridge(NULL);
843 video_decoder_job_->SetDrmBridge(NULL);
844 cdm_registration_id_ = 0;
845 drm_bridge_ = NULL;
848 } // namespace media