Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / media / base / android / media_source_player.cc
bloba91e29d95b6fbdd846e0b5945ce165db55334662
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"
21 #include "media/base/buffers.h"
23 namespace media {
25 MediaSourcePlayer::MediaSourcePlayer(
26 int player_id,
27 MediaPlayerManager* manager,
28 const RequestMediaResourcesCB& request_media_resources_cb,
29 scoped_ptr<DemuxerAndroid> demuxer,
30 const GURL& frame_url)
31 : MediaPlayerAndroid(player_id,
32 manager,
33 request_media_resources_cb,
34 frame_url),
35 demuxer_(demuxer.Pass()),
36 pending_event_(NO_EVENT_PENDING),
37 playing_(false),
38 interpolator_(&default_tick_clock_),
39 doing_browser_seek_(false),
40 pending_seek_(false),
41 drm_bridge_(NULL),
42 cdm_registration_id_(0),
43 is_waiting_for_key_(false),
44 key_added_while_decode_pending_(false),
45 is_waiting_for_audio_decoder_(false),
46 is_waiting_for_video_decoder_(false),
47 prerolling_(true),
48 weak_factory_(this) {
49 audio_decoder_job_.reset(new AudioDecoderJob(
50 base::Bind(&DemuxerAndroid::RequestDemuxerData,
51 base::Unretained(demuxer_.get()),
52 DemuxerStream::AUDIO),
53 base::Bind(&MediaSourcePlayer::OnDemuxerConfigsChanged,
54 weak_factory_.GetWeakPtr())));
55 video_decoder_job_.reset(new VideoDecoderJob(
56 base::Bind(&DemuxerAndroid::RequestDemuxerData,
57 base::Unretained(demuxer_.get()),
58 DemuxerStream::VIDEO),
59 base::Bind(request_media_resources_cb_, player_id),
60 base::Bind(&MediaSourcePlayer::OnDemuxerConfigsChanged,
61 weak_factory_.GetWeakPtr())));
62 demuxer_->Initialize(this);
63 interpolator_.SetUpperBound(base::TimeDelta());
64 weak_this_ = weak_factory_.GetWeakPtr();
67 MediaSourcePlayer::~MediaSourcePlayer() {
68 Release();
69 DCHECK_EQ(!drm_bridge_, !cdm_registration_id_);
70 if (drm_bridge_) {
71 drm_bridge_->UnregisterPlayer(cdm_registration_id_);
72 cdm_registration_id_ = 0;
76 void MediaSourcePlayer::SetVideoSurface(gfx::ScopedJavaSurface surface) {
77 DVLOG(1) << __FUNCTION__;
78 if (!video_decoder_job_->SetVideoSurface(surface.Pass()))
79 return;
80 // Retry video decoder creation.
81 RetryDecoderCreation(false, true);
84 void MediaSourcePlayer::ScheduleSeekEventAndStopDecoding(
85 base::TimeDelta seek_time) {
86 DVLOG(1) << __FUNCTION__ << "(" << seek_time.InSecondsF() << ")";
87 DCHECK(!IsEventPending(SEEK_EVENT_PENDING));
89 pending_seek_ = false;
91 interpolator_.SetBounds(seek_time, seek_time);
93 if (audio_decoder_job_->is_decoding())
94 audio_decoder_job_->StopDecode();
95 if (video_decoder_job_->is_decoding())
96 video_decoder_job_->StopDecode();
98 SetPendingEvent(SEEK_EVENT_PENDING);
99 ProcessPendingEvents();
102 void MediaSourcePlayer::BrowserSeekToCurrentTime() {
103 DVLOG(1) << __FUNCTION__;
105 DCHECK(!IsEventPending(SEEK_EVENT_PENDING));
106 doing_browser_seek_ = true;
107 ScheduleSeekEventAndStopDecoding(GetCurrentTime());
110 bool MediaSourcePlayer::Seekable() {
111 // If the duration TimeDelta, converted to milliseconds from microseconds,
112 // is >= 2^31, then the media is assumed to be unbounded and unseekable.
113 // 2^31 is the bound due to java player using 32-bit integer for time
114 // values at millisecond resolution.
115 return duration_ <
116 base::TimeDelta::FromMilliseconds(std::numeric_limits<int32>::max());
119 void MediaSourcePlayer::Start() {
120 DVLOG(1) << __FUNCTION__;
122 playing_ = true;
124 StartInternal();
127 void MediaSourcePlayer::Pause(bool is_media_related_action) {
128 DVLOG(1) << __FUNCTION__;
130 // Since decoder jobs have their own thread, decoding is not fully paused
131 // until all the decoder jobs call MediaDecoderCallback(). It is possible
132 // that Start() is called while the player is waiting for
133 // MediaDecoderCallback(). In that case, decoding will continue when
134 // MediaDecoderCallback() is called.
135 playing_ = false;
136 start_time_ticks_ = base::TimeTicks();
138 SetAudible(false);
141 bool MediaSourcePlayer::IsPlaying() {
142 return playing_;
145 int MediaSourcePlayer::GetVideoWidth() {
146 return video_decoder_job_->output_width();
149 int MediaSourcePlayer::GetVideoHeight() {
150 return video_decoder_job_->output_height();
153 void MediaSourcePlayer::SeekTo(base::TimeDelta timestamp) {
154 DVLOG(1) << __FUNCTION__ << "(" << timestamp.InSecondsF() << ")";
156 if (IsEventPending(SEEK_EVENT_PENDING)) {
157 DCHECK(doing_browser_seek_) << "SeekTo while SeekTo in progress";
158 DCHECK(!pending_seek_) << "SeekTo while SeekTo pending browser seek";
160 // There is a browser seek currently in progress to obtain I-frame to feed
161 // a newly constructed video decoder. Remember this real seek request so
162 // it can be initiated once OnDemuxerSeekDone() occurs for the browser seek.
163 pending_seek_ = true;
164 pending_seek_time_ = timestamp;
165 return;
168 doing_browser_seek_ = false;
169 ScheduleSeekEventAndStopDecoding(timestamp);
172 base::TimeDelta MediaSourcePlayer::GetCurrentTime() {
173 return std::min(interpolator_.GetInterpolatedTime(), duration_);
176 base::TimeDelta MediaSourcePlayer::GetDuration() {
177 return duration_;
180 void MediaSourcePlayer::Release() {
181 DVLOG(1) << __FUNCTION__;
183 audio_decoder_job_->ReleaseDecoderResources();
184 video_decoder_job_->ReleaseDecoderResources();
186 // Prevent player restart, including job re-creation attempts.
187 playing_ = false;
189 decoder_starvation_callback_.Cancel();
191 SetAudible(false);
192 DetachListener();
195 void MediaSourcePlayer::SetVolume(double volume) {
196 audio_decoder_job_->SetVolume(volume);
199 bool MediaSourcePlayer::CanPause() {
200 return Seekable();
203 bool MediaSourcePlayer::CanSeekForward() {
204 return Seekable();
207 bool MediaSourcePlayer::CanSeekBackward() {
208 return Seekable();
211 bool MediaSourcePlayer::IsPlayerReady() {
212 return audio_decoder_job_ || video_decoder_job_;
215 void MediaSourcePlayer::StartInternal() {
216 DVLOG(1) << __FUNCTION__;
217 // If there are pending events, wait for them finish.
218 if (pending_event_ != NO_EVENT_PENDING)
219 return;
221 if (!manager()->RequestPlay(player_id())) {
222 Pause(true);
223 return;
226 // When we start, we could have new demuxed data coming in. This new data
227 // could be clear (not encrypted) or encrypted with different keys. So key
228 // related info should all be cleared.
229 is_waiting_for_key_ = false;
230 key_added_while_decode_pending_ = false;
231 AttachListener(NULL);
233 SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
234 ProcessPendingEvents();
237 void MediaSourcePlayer::OnDemuxerConfigsAvailable(
238 const DemuxerConfigs& configs) {
239 DVLOG(1) << __FUNCTION__;
240 DCHECK(!HasAudio() && !HasVideo());
242 duration_ = configs.duration;
244 audio_decoder_job_->SetDemuxerConfigs(configs);
245 video_decoder_job_->SetDemuxerConfigs(configs);
246 OnDemuxerConfigsChanged();
249 void MediaSourcePlayer::OnDemuxerDataAvailable(const DemuxerData& data) {
250 DVLOG(1) << __FUNCTION__ << "(" << data.type << ")";
251 DCHECK_LT(0u, data.access_units.size());
252 CHECK_GE(1u, data.demuxer_configs.size());
254 if (data.type == DemuxerStream::AUDIO)
255 audio_decoder_job_->OnDataReceived(data);
256 else if (data.type == DemuxerStream::VIDEO)
257 video_decoder_job_->OnDataReceived(data);
260 void MediaSourcePlayer::OnDemuxerDurationChanged(base::TimeDelta duration) {
261 duration_ = duration;
264 void MediaSourcePlayer::OnMediaCryptoReady() {
265 DCHECK(!drm_bridge_->GetMediaCrypto().is_null());
266 drm_bridge_->SetMediaCryptoReadyCB(base::Closure());
268 // Retry decoder creation if the decoders are waiting for MediaCrypto.
269 RetryDecoderCreation(true, true);
272 void MediaSourcePlayer::SetCdm(BrowserCdm* cdm) {
273 // Currently we don't support DRM change during the middle of playback, even
274 // if the player is paused.
275 // TODO(qinmin): support DRM change after playback has started.
276 // http://crbug.com/253792.
277 if (GetCurrentTime() > base::TimeDelta()) {
278 VLOG(0) << "Setting DRM bridge after playback has started. "
279 << "This is not well supported!";
282 if (drm_bridge_) {
283 NOTREACHED() << "Currently we do not support resetting CDM.";
284 return;
287 // Only MediaDrmBridge will be set on MediaSourcePlayer.
288 drm_bridge_ = static_cast<MediaDrmBridge*>(cdm);
290 cdm_registration_id_ = drm_bridge_->RegisterPlayer(
291 base::Bind(&MediaSourcePlayer::OnKeyAdded, weak_this_),
292 base::Bind(&MediaSourcePlayer::OnCdmUnset, weak_this_));
294 audio_decoder_job_->SetDrmBridge(drm_bridge_);
295 video_decoder_job_->SetDrmBridge(drm_bridge_);
297 if (drm_bridge_->GetMediaCrypto().is_null()) {
298 drm_bridge_->SetMediaCryptoReadyCB(
299 base::Bind(&MediaSourcePlayer::OnMediaCryptoReady, weak_this_));
300 return;
303 // If the player is previously waiting for CDM, retry decoder creation.
304 RetryDecoderCreation(true, true);
307 void MediaSourcePlayer::OnDemuxerSeekDone(
308 base::TimeDelta actual_browser_seek_time) {
309 DVLOG(1) << __FUNCTION__;
311 ClearPendingEvent(SEEK_EVENT_PENDING);
312 if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING))
313 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
315 if (pending_seek_) {
316 DVLOG(1) << __FUNCTION__ << "processing pending seek";
317 DCHECK(doing_browser_seek_);
318 pending_seek_ = false;
319 SeekTo(pending_seek_time_);
320 return;
323 // It is possible that a browser seek to I-frame had to seek to a buffered
324 // I-frame later than the requested one due to data removal or GC. Update
325 // player clock to the actual seek target.
326 if (doing_browser_seek_) {
327 DCHECK(actual_browser_seek_time != kNoTimestamp());
328 base::TimeDelta seek_time = actual_browser_seek_time;
329 // A browser seek must not jump into the past. Ideally, it seeks to the
330 // requested time, but it might jump into the future.
331 DCHECK(seek_time >= GetCurrentTime());
332 DVLOG(1) << __FUNCTION__ << " : setting clock to actual browser seek time: "
333 << seek_time.InSecondsF();
334 interpolator_.SetBounds(seek_time, seek_time);
335 audio_decoder_job_->SetBaseTimestamp(seek_time);
336 } else {
337 DCHECK(actual_browser_seek_time == kNoTimestamp());
340 base::TimeDelta current_time = GetCurrentTime();
341 // TODO(qinmin): Simplify the logic by using |start_presentation_timestamp_|
342 // to preroll media decoder jobs. Currently |start_presentation_timestamp_|
343 // is calculated from decoder output, while preroll relies on the access
344 // unit's timestamp. There are some differences between the two.
345 preroll_timestamp_ = current_time;
346 if (HasAudio())
347 audio_decoder_job_->BeginPrerolling(preroll_timestamp_);
348 if (HasVideo())
349 video_decoder_job_->BeginPrerolling(preroll_timestamp_);
350 prerolling_ = true;
352 if (!doing_browser_seek_)
353 manager()->OnSeekComplete(player_id(), current_time);
355 ProcessPendingEvents();
358 void MediaSourcePlayer::UpdateTimestamps(
359 base::TimeDelta current_presentation_timestamp,
360 base::TimeDelta max_presentation_timestamp) {
361 interpolator_.SetBounds(current_presentation_timestamp,
362 max_presentation_timestamp);
363 manager()->OnTimeUpdate(player_id(),
364 GetCurrentTime(),
365 base::TimeTicks::Now());
368 void MediaSourcePlayer::ProcessPendingEvents() {
369 DVLOG(1) << __FUNCTION__ << " : 0x" << std::hex << pending_event_;
370 // Wait for all the decoding jobs to finish before processing pending tasks.
371 if (video_decoder_job_->is_decoding()) {
372 DVLOG(1) << __FUNCTION__ << " : A video job is still decoding.";
373 return;
376 if (audio_decoder_job_->is_decoding()) {
377 DVLOG(1) << __FUNCTION__ << " : An audio job is still decoding.";
378 return;
381 if (IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
382 DVLOG(1) << __FUNCTION__ << " : PREFETCH_DONE still pending.";
383 return;
386 if (IsEventPending(SEEK_EVENT_PENDING)) {
387 DVLOG(1) << __FUNCTION__ << " : Handling SEEK_EVENT";
388 ClearDecodingData();
389 audio_decoder_job_->SetBaseTimestamp(GetCurrentTime());
390 demuxer_->RequestDemuxerSeek(GetCurrentTime(), doing_browser_seek_);
391 return;
394 if (IsEventPending(DECODER_CREATION_EVENT_PENDING)) {
395 // Don't continue if one of the decoder is not created.
396 if (is_waiting_for_audio_decoder_ || is_waiting_for_video_decoder_)
397 return;
398 ClearPendingEvent(DECODER_CREATION_EVENT_PENDING);
401 if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING)) {
402 DVLOG(1) << __FUNCTION__ << " : Handling PREFETCH_REQUEST_EVENT.";
404 int count = (AudioFinished() ? 0 : 1) + (VideoFinished() ? 0 : 1);
406 // It is possible that all streams have finished decode, yet starvation
407 // occurred during the last stream's EOS decode. In this case, prefetch is a
408 // no-op.
409 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
410 if (count == 0)
411 return;
413 SetPendingEvent(PREFETCH_DONE_EVENT_PENDING);
414 base::Closure barrier = BarrierClosure(
415 count, base::Bind(&MediaSourcePlayer::OnPrefetchDone, weak_this_));
417 if (!AudioFinished())
418 audio_decoder_job_->Prefetch(barrier);
420 if (!VideoFinished())
421 video_decoder_job_->Prefetch(barrier);
423 return;
426 DCHECK_EQ(pending_event_, NO_EVENT_PENDING);
428 // Now that all pending events have been handled, resume decoding if we are
429 // still playing.
430 if (playing_)
431 StartInternal();
434 void MediaSourcePlayer::MediaDecoderCallback(
435 bool is_audio, MediaCodecStatus status,
436 base::TimeDelta current_presentation_timestamp,
437 base::TimeDelta max_presentation_timestamp) {
438 DVLOG(1) << __FUNCTION__ << ": " << is_audio << ", " << status;
440 // TODO(xhwang): Drop IntToString() when http://crbug.com/303899 is fixed.
441 if (is_audio) {
442 TRACE_EVENT_ASYNC_END1("media",
443 "MediaSourcePlayer::DecodeMoreAudio",
444 audio_decoder_job_.get(),
445 "MediaCodecStatus",
446 base::IntToString(status));
447 } else {
448 TRACE_EVENT_ASYNC_END1("media",
449 "MediaSourcePlayer::DecodeMoreVideo",
450 video_decoder_job_.get(),
451 "MediaCodecStatus",
452 base::IntToString(status));
455 // Let tests hook the completion of this decode cycle.
456 if (!decode_callback_for_testing_.is_null())
457 base::ResetAndReturn(&decode_callback_for_testing_).Run();
459 bool is_clock_manager = is_audio || !HasAudio();
461 if (is_clock_manager)
462 decoder_starvation_callback_.Cancel();
464 if (status == MEDIA_CODEC_ERROR) {
465 DVLOG(1) << __FUNCTION__ << " : decode error";
466 Release();
467 manager()->OnError(player_id(), MEDIA_ERROR_DECODE);
468 return;
471 DCHECK(!IsEventPending(PREFETCH_DONE_EVENT_PENDING));
473 // Let |SEEK_EVENT_PENDING| (the highest priority event outside of
474 // |PREFETCH_DONE_EVENT_PENDING|) preempt output EOS detection here. Process
475 // any other pending events only after handling EOS detection.
476 if (IsEventPending(SEEK_EVENT_PENDING)) {
477 ProcessPendingEvents();
478 return;
481 if ((status == MEDIA_CODEC_OK || status == MEDIA_CODEC_INPUT_END_OF_STREAM) &&
482 is_clock_manager && current_presentation_timestamp != kNoTimestamp()) {
483 UpdateTimestamps(current_presentation_timestamp,
484 max_presentation_timestamp);
487 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM) {
488 PlaybackCompleted(is_audio);
489 if (is_clock_manager)
490 interpolator_.StopInterpolating();
493 if (pending_event_ != NO_EVENT_PENDING) {
494 ProcessPendingEvents();
495 return;
498 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM) {
499 if (is_audio)
500 SetAudible(false);
501 return;
504 if (!playing_) {
505 if (is_clock_manager)
506 interpolator_.StopInterpolating();
508 if (is_audio)
509 SetAudible(false);
510 return;
513 if (status == MEDIA_CODEC_NO_KEY) {
514 if (key_added_while_decode_pending_) {
515 DVLOG(2) << __FUNCTION__ << ": Key was added during decoding.";
516 ResumePlaybackAfterKeyAdded();
517 } else {
518 if (is_audio)
519 SetAudible(false);
521 is_waiting_for_key_ = true;
522 manager()->OnWaitingForDecryptionKey(player_id());
524 return;
527 // If |key_added_while_decode_pending_| is true and both audio and video
528 // decoding succeeded, we should clear |key_added_while_decode_pending_| here.
529 // But that would add more complexity into this function. If we don't clear it
530 // here, the worst case would be we call ResumePlaybackAfterKeyAdded() when
531 // we don't really have a new key. This should rarely happen and the
532 // performance impact should be pretty small.
533 // TODO(qinmin/xhwang): This class is complicated because we handle both audio
534 // and video in one file. If we separate them, we should be able to remove a
535 // lot of duplication.
537 // If the status is MEDIA_CODEC_ABORT, stop decoding new data. The player is
538 // in the middle of a seek or stop event and needs to wait for the IPCs to
539 // come.
540 if (status == MEDIA_CODEC_ABORT) {
541 if (is_audio)
542 SetAudible(false);
543 return;
546 if (prerolling_ && IsPrerollFinished(is_audio)) {
547 if (IsPrerollFinished(!is_audio)) {
548 prerolling_ = false;
549 StartInternal();
551 return;
554 // We successfully decoded a frame and going to the next one.
555 // Set the audible state.
556 if (is_audio) {
557 bool is_audible = !prerolling_ && audio_decoder_job_->volume() > 0;
558 SetAudible(is_audible);
561 if (is_clock_manager) {
562 // If we have a valid timestamp, start the starvation callback. Otherwise,
563 // reset the |start_time_ticks_| so that the next frame will not suffer
564 // from the decoding delay caused by the current frame.
565 if (current_presentation_timestamp != kNoTimestamp())
566 StartStarvationCallback(current_presentation_timestamp,
567 max_presentation_timestamp);
568 else
569 start_time_ticks_ = base::TimeTicks::Now();
572 if (is_audio)
573 DecodeMoreAudio();
574 else
575 DecodeMoreVideo();
578 bool MediaSourcePlayer::IsPrerollFinished(bool is_audio) const {
579 if (is_audio)
580 return !HasAudio() || !audio_decoder_job_->prerolling();
581 return !HasVideo() || !video_decoder_job_->prerolling();
584 void MediaSourcePlayer::DecodeMoreAudio() {
585 DVLOG(1) << __FUNCTION__;
586 DCHECK(!audio_decoder_job_->is_decoding());
587 DCHECK(!AudioFinished());
589 MediaDecoderJob::MediaDecoderJobStatus status = audio_decoder_job_->Decode(
590 start_time_ticks_,
591 start_presentation_timestamp_,
592 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_, true));
594 switch (status) {
595 case MediaDecoderJob::STATUS_SUCCESS:
596 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreAudio",
597 audio_decoder_job_.get());
598 break;
599 case MediaDecoderJob::STATUS_KEY_FRAME_REQUIRED:
600 NOTREACHED();
601 break;
602 case MediaDecoderJob::STATUS_FAILURE:
603 is_waiting_for_audio_decoder_ = true;
604 if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
605 SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
606 break;
610 void MediaSourcePlayer::DecodeMoreVideo() {
611 DVLOG(1) << __FUNCTION__;
612 DCHECK(!video_decoder_job_->is_decoding());
613 DCHECK(!VideoFinished());
615 MediaDecoderJob::MediaDecoderJobStatus status = video_decoder_job_->Decode(
616 start_time_ticks_,
617 start_presentation_timestamp_,
618 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_,
619 false));
621 switch (status) {
622 case MediaDecoderJob::STATUS_SUCCESS:
623 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreVideo",
624 video_decoder_job_.get());
625 break;
626 case MediaDecoderJob::STATUS_KEY_FRAME_REQUIRED:
627 BrowserSeekToCurrentTime();
628 break;
629 case MediaDecoderJob::STATUS_FAILURE:
630 is_waiting_for_video_decoder_ = true;
631 if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
632 SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
633 break;
637 void MediaSourcePlayer::PlaybackCompleted(bool is_audio) {
638 DVLOG(1) << __FUNCTION__ << "(" << is_audio << ")";
640 if (AudioFinished() && VideoFinished()) {
641 playing_ = false;
642 start_time_ticks_ = base::TimeTicks();
643 manager()->OnPlaybackComplete(player_id());
647 void MediaSourcePlayer::ClearDecodingData() {
648 DVLOG(1) << __FUNCTION__;
649 audio_decoder_job_->Flush();
650 video_decoder_job_->Flush();
651 start_time_ticks_ = base::TimeTicks();
654 bool MediaSourcePlayer::HasVideo() const {
655 return video_decoder_job_->HasStream();
658 bool MediaSourcePlayer::HasAudio() const {
659 return audio_decoder_job_->HasStream();
662 bool MediaSourcePlayer::AudioFinished() {
663 return audio_decoder_job_->OutputEOSReached() || !HasAudio();
666 bool MediaSourcePlayer::VideoFinished() {
667 return video_decoder_job_->OutputEOSReached() || !HasVideo();
670 void MediaSourcePlayer::OnDecoderStarved() {
671 DVLOG(1) << __FUNCTION__;
673 if (HasAudio()) {
674 // If the starvation timer fired but there are no encoded frames
675 // in the queue we believe the demuxer (i.e. renderer process) froze.
676 if (!audio_decoder_job_->HasData())
677 SetAudible(false);
680 SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
681 ProcessPendingEvents();
684 void MediaSourcePlayer::StartStarvationCallback(
685 base::TimeDelta current_presentation_timestamp,
686 base::TimeDelta max_presentation_timestamp) {
687 // 20ms was chosen because it is the typical size of a compressed audio frame.
688 // Anything smaller than this would likely cause unnecessary cycling in and
689 // out of the prefetch state.
690 const base::TimeDelta kMinStarvationTimeout =
691 base::TimeDelta::FromMilliseconds(20);
693 base::TimeDelta current_timestamp = GetCurrentTime();
694 base::TimeDelta timeout;
695 if (HasAudio()) {
696 timeout = max_presentation_timestamp - current_timestamp;
697 } else {
698 DCHECK(current_timestamp <= current_presentation_timestamp);
700 // For video only streams, fps can be estimated from the difference
701 // between the previous and current presentation timestamps. The
702 // previous presentation timestamp is equal to current_timestamp.
703 // TODO(qinmin): determine whether 2 is a good coefficient for estimating
704 // video frame timeout.
705 timeout = 2 * (current_presentation_timestamp - current_timestamp);
708 timeout = std::max(timeout, kMinStarvationTimeout);
710 decoder_starvation_callback_.Reset(
711 base::Bind(&MediaSourcePlayer::OnDecoderStarved, weak_this_));
712 base::MessageLoop::current()->PostDelayedTask(
713 FROM_HERE, decoder_starvation_callback_.callback(), timeout);
716 void MediaSourcePlayer::OnPrefetchDone() {
717 DVLOG(1) << __FUNCTION__;
718 DCHECK(!audio_decoder_job_->is_decoding());
719 DCHECK(!video_decoder_job_->is_decoding());
721 // A previously posted OnPrefetchDone() could race against a Release(). If
722 // Release() won the race, we should no longer have decoder jobs.
723 // TODO(qinmin/wolenetz): Maintain channel state to not double-request data
724 // or drop data received across Release()+Start(). See http://crbug.com/306314
725 // and http://crbug.com/304234.
726 if (!IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
727 DVLOG(1) << __FUNCTION__ << " : aborting";
728 return;
731 ClearPendingEvent(PREFETCH_DONE_EVENT_PENDING);
733 if (pending_event_ != NO_EVENT_PENDING) {
734 ProcessPendingEvents();
735 return;
738 if (!playing_)
739 return;
741 start_time_ticks_ = base::TimeTicks::Now();
742 start_presentation_timestamp_ = GetCurrentTime();
743 if (!interpolator_.interpolating())
744 interpolator_.StartInterpolating();
746 if (!AudioFinished())
747 DecodeMoreAudio();
749 if (!VideoFinished())
750 DecodeMoreVideo();
753 void MediaSourcePlayer::OnDemuxerConfigsChanged() {
754 manager()->OnMediaMetadataChanged(
755 player_id(), duration_, GetVideoWidth(), GetVideoHeight(), true);
758 const char* MediaSourcePlayer::GetEventName(PendingEventFlags event) {
759 // Please keep this in sync with PendingEventFlags.
760 static const char* kPendingEventNames[] = {
761 "PREFETCH_DONE",
762 "SEEK",
763 "DECODER_CREATION",
764 "PREFETCH_REQUEST",
767 int mask = 1;
768 for (size_t i = 0; i < arraysize(kPendingEventNames); ++i, mask <<= 1) {
769 if (event & mask)
770 return kPendingEventNames[i];
773 return "UNKNOWN";
776 bool MediaSourcePlayer::IsEventPending(PendingEventFlags event) const {
777 return pending_event_ & event;
780 void MediaSourcePlayer::SetPendingEvent(PendingEventFlags event) {
781 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
782 DCHECK_NE(event, NO_EVENT_PENDING);
783 DCHECK(!IsEventPending(event));
785 pending_event_ |= event;
788 void MediaSourcePlayer::ClearPendingEvent(PendingEventFlags event) {
789 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
790 DCHECK_NE(event, NO_EVENT_PENDING);
791 DCHECK(IsEventPending(event)) << GetEventName(event);
793 pending_event_ &= ~event;
796 void MediaSourcePlayer::RetryDecoderCreation(bool audio, bool video) {
797 if (audio)
798 is_waiting_for_audio_decoder_ = false;
799 if (video)
800 is_waiting_for_video_decoder_ = false;
801 if (IsEventPending(DECODER_CREATION_EVENT_PENDING))
802 ProcessPendingEvents();
805 void MediaSourcePlayer::OnKeyAdded() {
806 DVLOG(1) << __FUNCTION__;
808 if (is_waiting_for_key_) {
809 ResumePlaybackAfterKeyAdded();
810 return;
813 if ((audio_decoder_job_->is_content_encrypted() &&
814 audio_decoder_job_->is_decoding()) ||
815 (video_decoder_job_->is_content_encrypted() &&
816 video_decoder_job_->is_decoding())) {
817 DVLOG(1) << __FUNCTION__ << ": " << "Key added during pending decode.";
818 key_added_while_decode_pending_ = true;
822 void MediaSourcePlayer::ResumePlaybackAfterKeyAdded() {
823 DVLOG(1) << __FUNCTION__;
824 DCHECK(is_waiting_for_key_ || key_added_while_decode_pending_);
826 is_waiting_for_key_ = false;
827 key_added_while_decode_pending_ = false;
829 // StartInternal() will trigger a prefetch, where in most cases we'll just
830 // use previously received data.
831 if (playing_)
832 StartInternal();
835 void MediaSourcePlayer::OnCdmUnset() {
836 DVLOG(1) << __FUNCTION__;
837 DCHECK(drm_bridge_);
838 // TODO(xhwang): Currently this is only called during teardown. Support full
839 // detachment of CDM during playback. This will be needed when we start to
840 // support setMediaKeys(0) (see http://crbug.com/330324), or when we release
841 // MediaDrm when the video is paused, or when the device goes to sleep (see
842 // http://crbug.com/272421).
843 audio_decoder_job_->SetDrmBridge(NULL);
844 video_decoder_job_->SetDrmBridge(NULL);
845 cdm_registration_id_ = 0;
846 drm_bridge_ = NULL;
849 } // namespace media