Update V8 to version 4.7.56.
[chromium-blink-merge.git] / media / base / android / media_source_player.cc
blob4ed14aa478a9fd20013d99a9bf5b23de398a8916
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/timestamp_constants.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();
139 bool MediaSourcePlayer::IsPlaying() {
140 return playing_;
143 int MediaSourcePlayer::GetVideoWidth() {
144 return video_decoder_job_->output_width();
147 int MediaSourcePlayer::GetVideoHeight() {
148 return video_decoder_job_->output_height();
151 void MediaSourcePlayer::SeekTo(base::TimeDelta timestamp) {
152 DVLOG(1) << __FUNCTION__ << "(" << timestamp.InSecondsF() << ")";
154 if (IsEventPending(SEEK_EVENT_PENDING)) {
155 DCHECK(doing_browser_seek_) << "SeekTo while SeekTo in progress";
156 DCHECK(!pending_seek_) << "SeekTo while SeekTo pending browser seek";
158 // There is a browser seek currently in progress to obtain I-frame to feed
159 // a newly constructed video decoder. Remember this real seek request so
160 // it can be initiated once OnDemuxerSeekDone() occurs for the browser seek.
161 pending_seek_ = true;
162 pending_seek_time_ = timestamp;
163 return;
166 doing_browser_seek_ = false;
167 ScheduleSeekEventAndStopDecoding(timestamp);
170 base::TimeDelta MediaSourcePlayer::GetCurrentTime() {
171 return std::min(interpolator_.GetInterpolatedTime(), duration_);
174 base::TimeDelta MediaSourcePlayer::GetDuration() {
175 return duration_;
178 void MediaSourcePlayer::Release() {
179 DVLOG(1) << __FUNCTION__;
181 audio_decoder_job_->ReleaseDecoderResources();
182 video_decoder_job_->ReleaseDecoderResources();
184 // Prevent player restart, including job re-creation attempts.
185 playing_ = false;
187 decoder_starvation_callback_.Cancel();
189 DetachListener();
192 void MediaSourcePlayer::SetVolume(double volume) {
193 audio_decoder_job_->SetVolume(volume);
196 bool MediaSourcePlayer::CanPause() {
197 return Seekable();
200 bool MediaSourcePlayer::CanSeekForward() {
201 return Seekable();
204 bool MediaSourcePlayer::CanSeekBackward() {
205 return Seekable();
208 bool MediaSourcePlayer::IsPlayerReady() {
209 return audio_decoder_job_ || video_decoder_job_;
212 void MediaSourcePlayer::StartInternal() {
213 DVLOG(1) << __FUNCTION__;
214 // If there are pending events, wait for them finish.
215 if (pending_event_ != NO_EVENT_PENDING)
216 return;
218 if (!manager()->RequestPlay(player_id())) {
219 Pause(true);
220 return;
223 // When we start, we could have new demuxed data coming in. This new data
224 // could be clear (not encrypted) or encrypted with different keys. So key
225 // related info should all be cleared.
226 is_waiting_for_key_ = false;
227 key_added_while_decode_pending_ = false;
228 AttachListener(NULL);
230 SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
231 ProcessPendingEvents();
234 void MediaSourcePlayer::OnDemuxerConfigsAvailable(
235 const DemuxerConfigs& configs) {
236 DVLOG(1) << __FUNCTION__;
237 DCHECK(!HasAudio() && !HasVideo());
239 duration_ = configs.duration;
241 audio_decoder_job_->SetDemuxerConfigs(configs);
242 video_decoder_job_->SetDemuxerConfigs(configs);
243 OnDemuxerConfigsChanged();
246 void MediaSourcePlayer::OnDemuxerDataAvailable(const DemuxerData& data) {
247 DVLOG(1) << __FUNCTION__ << "(" << data.type << ")";
248 DCHECK_LT(0u, data.access_units.size());
249 CHECK_GE(1u, data.demuxer_configs.size());
251 if (data.type == DemuxerStream::AUDIO)
252 audio_decoder_job_->OnDataReceived(data);
253 else if (data.type == DemuxerStream::VIDEO)
254 video_decoder_job_->OnDataReceived(data);
257 void MediaSourcePlayer::OnDemuxerDurationChanged(base::TimeDelta duration) {
258 duration_ = duration;
261 void MediaSourcePlayer::OnMediaCryptoReady() {
262 DCHECK(!drm_bridge_->GetMediaCrypto().is_null());
263 drm_bridge_->SetMediaCryptoReadyCB(base::Closure());
265 // Retry decoder creation if the decoders are waiting for MediaCrypto.
266 RetryDecoderCreation(true, true);
269 void MediaSourcePlayer::SetCdm(BrowserCdm* cdm) {
270 // Currently we don't support DRM change during the middle of playback, even
271 // if the player is paused.
272 // TODO(qinmin): support DRM change after playback has started.
273 // http://crbug.com/253792.
274 if (GetCurrentTime() > base::TimeDelta()) {
275 VLOG(0) << "Setting DRM bridge after playback has started. "
276 << "This is not well supported!";
279 if (drm_bridge_) {
280 NOTREACHED() << "Currently we do not support resetting CDM.";
281 return;
284 // Only MediaDrmBridge will be set on MediaSourcePlayer.
285 drm_bridge_ = static_cast<MediaDrmBridge*>(cdm);
287 cdm_registration_id_ = drm_bridge_->RegisterPlayer(
288 base::Bind(&MediaSourcePlayer::OnKeyAdded, weak_this_),
289 base::Bind(&MediaSourcePlayer::OnCdmUnset, weak_this_));
291 audio_decoder_job_->SetDrmBridge(drm_bridge_);
292 video_decoder_job_->SetDrmBridge(drm_bridge_);
294 if (drm_bridge_->GetMediaCrypto().is_null()) {
295 drm_bridge_->SetMediaCryptoReadyCB(
296 base::Bind(&MediaSourcePlayer::OnMediaCryptoReady, weak_this_));
297 return;
300 // If the player is previously waiting for CDM, retry decoder creation.
301 RetryDecoderCreation(true, true);
304 void MediaSourcePlayer::OnDemuxerSeekDone(
305 base::TimeDelta actual_browser_seek_time) {
306 DVLOG(1) << __FUNCTION__;
308 ClearPendingEvent(SEEK_EVENT_PENDING);
309 if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING))
310 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
312 if (pending_seek_) {
313 DVLOG(1) << __FUNCTION__ << "processing pending seek";
314 DCHECK(doing_browser_seek_);
315 pending_seek_ = false;
316 SeekTo(pending_seek_time_);
317 return;
320 // It is possible that a browser seek to I-frame had to seek to a buffered
321 // I-frame later than the requested one due to data removal or GC. Update
322 // player clock to the actual seek target.
323 if (doing_browser_seek_) {
324 DCHECK(actual_browser_seek_time != kNoTimestamp());
325 base::TimeDelta seek_time = actual_browser_seek_time;
326 // A browser seek must not jump into the past. Ideally, it seeks to the
327 // requested time, but it might jump into the future.
328 DCHECK(seek_time >= GetCurrentTime());
329 DVLOG(1) << __FUNCTION__ << " : setting clock to actual browser seek time: "
330 << seek_time.InSecondsF();
331 interpolator_.SetBounds(seek_time, seek_time);
332 audio_decoder_job_->SetBaseTimestamp(seek_time);
333 } else {
334 DCHECK(actual_browser_seek_time == kNoTimestamp());
337 base::TimeDelta current_time = GetCurrentTime();
338 // TODO(qinmin): Simplify the logic by using |start_presentation_timestamp_|
339 // to preroll media decoder jobs. Currently |start_presentation_timestamp_|
340 // is calculated from decoder output, while preroll relies on the access
341 // unit's timestamp. There are some differences between the two.
342 preroll_timestamp_ = current_time;
343 if (HasAudio())
344 audio_decoder_job_->BeginPrerolling(preroll_timestamp_);
345 if (HasVideo())
346 video_decoder_job_->BeginPrerolling(preroll_timestamp_);
347 prerolling_ = true;
349 if (!doing_browser_seek_)
350 manager()->OnSeekComplete(player_id(), current_time);
352 ProcessPendingEvents();
355 void MediaSourcePlayer::UpdateTimestamps(
356 base::TimeDelta current_presentation_timestamp,
357 base::TimeDelta max_presentation_timestamp) {
358 interpolator_.SetBounds(current_presentation_timestamp,
359 max_presentation_timestamp);
360 manager()->OnTimeUpdate(player_id(),
361 GetCurrentTime(),
362 base::TimeTicks::Now());
365 void MediaSourcePlayer::ProcessPendingEvents() {
366 DVLOG(1) << __FUNCTION__ << " : 0x" << std::hex << pending_event_;
367 // Wait for all the decoding jobs to finish before processing pending tasks.
368 if (video_decoder_job_->is_decoding()) {
369 DVLOG(1) << __FUNCTION__ << " : A video job is still decoding.";
370 return;
373 if (audio_decoder_job_->is_decoding()) {
374 DVLOG(1) << __FUNCTION__ << " : An audio job is still decoding.";
375 return;
378 if (IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
379 DVLOG(1) << __FUNCTION__ << " : PREFETCH_DONE still pending.";
380 return;
383 if (IsEventPending(SEEK_EVENT_PENDING)) {
384 DVLOG(1) << __FUNCTION__ << " : Handling SEEK_EVENT";
385 ClearDecodingData();
386 audio_decoder_job_->SetBaseTimestamp(GetCurrentTime());
387 demuxer_->RequestDemuxerSeek(GetCurrentTime(), doing_browser_seek_);
388 return;
391 if (IsEventPending(DECODER_CREATION_EVENT_PENDING)) {
392 // Don't continue if one of the decoder is not created.
393 if (is_waiting_for_audio_decoder_ || is_waiting_for_video_decoder_)
394 return;
395 ClearPendingEvent(DECODER_CREATION_EVENT_PENDING);
398 if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING)) {
399 DVLOG(1) << __FUNCTION__ << " : Handling PREFETCH_REQUEST_EVENT.";
401 int count = (AudioFinished() ? 0 : 1) + (VideoFinished() ? 0 : 1);
403 // It is possible that all streams have finished decode, yet starvation
404 // occurred during the last stream's EOS decode. In this case, prefetch is a
405 // no-op.
406 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
407 if (count == 0)
408 return;
410 SetPendingEvent(PREFETCH_DONE_EVENT_PENDING);
411 base::Closure barrier = BarrierClosure(
412 count, base::Bind(&MediaSourcePlayer::OnPrefetchDone, weak_this_));
414 if (!AudioFinished())
415 audio_decoder_job_->Prefetch(barrier);
417 if (!VideoFinished())
418 video_decoder_job_->Prefetch(barrier);
420 return;
423 DCHECK_EQ(pending_event_, NO_EVENT_PENDING);
425 // Now that all pending events have been handled, resume decoding if we are
426 // still playing.
427 if (playing_)
428 StartInternal();
431 void MediaSourcePlayer::MediaDecoderCallback(
432 bool is_audio, MediaCodecStatus status,
433 base::TimeDelta current_presentation_timestamp,
434 base::TimeDelta max_presentation_timestamp) {
435 DVLOG(1) << __FUNCTION__ << ": " << is_audio << ", " << status;
437 // TODO(xhwang): Drop IntToString() when http://crbug.com/303899 is fixed.
438 if (is_audio) {
439 TRACE_EVENT_ASYNC_END1("media",
440 "MediaSourcePlayer::DecodeMoreAudio",
441 audio_decoder_job_.get(),
442 "MediaCodecStatus",
443 base::IntToString(status));
444 } else {
445 TRACE_EVENT_ASYNC_END1("media",
446 "MediaSourcePlayer::DecodeMoreVideo",
447 video_decoder_job_.get(),
448 "MediaCodecStatus",
449 base::IntToString(status));
452 // Let tests hook the completion of this decode cycle.
453 if (!decode_callback_for_testing_.is_null())
454 base::ResetAndReturn(&decode_callback_for_testing_).Run();
456 bool is_clock_manager = is_audio || !HasAudio();
458 if (is_clock_manager)
459 decoder_starvation_callback_.Cancel();
461 if (status == MEDIA_CODEC_ERROR) {
462 DVLOG(1) << __FUNCTION__ << " : decode error";
463 Release();
464 manager()->OnError(player_id(), MEDIA_ERROR_DECODE);
465 return;
468 DCHECK(!IsEventPending(PREFETCH_DONE_EVENT_PENDING));
470 // Let |SEEK_EVENT_PENDING| (the highest priority event outside of
471 // |PREFETCH_DONE_EVENT_PENDING|) preempt output EOS detection here. Process
472 // any other pending events only after handling EOS detection.
473 if (IsEventPending(SEEK_EVENT_PENDING)) {
474 ProcessPendingEvents();
475 return;
478 if ((status == MEDIA_CODEC_OK || status == MEDIA_CODEC_INPUT_END_OF_STREAM) &&
479 is_clock_manager && current_presentation_timestamp != kNoTimestamp()) {
480 UpdateTimestamps(current_presentation_timestamp,
481 max_presentation_timestamp);
484 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM) {
485 PlaybackCompleted(is_audio);
486 if (is_clock_manager)
487 interpolator_.StopInterpolating();
490 if (pending_event_ != NO_EVENT_PENDING) {
491 ProcessPendingEvents();
492 return;
495 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM)
496 return;
498 if (!playing_) {
499 if (is_clock_manager)
500 interpolator_.StopInterpolating();
502 return;
505 if (status == MEDIA_CODEC_NO_KEY) {
506 if (key_added_while_decode_pending_) {
507 DVLOG(2) << __FUNCTION__ << ": Key was added during decoding.";
508 ResumePlaybackAfterKeyAdded();
509 } else {
510 is_waiting_for_key_ = true;
511 manager()->OnWaitingForDecryptionKey(player_id());
513 return;
516 // If |key_added_while_decode_pending_| is true and both audio and video
517 // decoding succeeded, we should clear |key_added_while_decode_pending_| here.
518 // But that would add more complexity into this function. If we don't clear it
519 // here, the worst case would be we call ResumePlaybackAfterKeyAdded() when
520 // we don't really have a new key. This should rarely happen and the
521 // performance impact should be pretty small.
522 // TODO(qinmin/xhwang): This class is complicated because we handle both audio
523 // and video in one file. If we separate them, we should be able to remove a
524 // lot of duplication.
526 // If the status is MEDIA_CODEC_ABORT, stop decoding new data. The player is
527 // in the middle of a seek or stop event and needs to wait for the IPCs to
528 // come.
529 if (status == MEDIA_CODEC_ABORT)
530 return;
532 if (prerolling_ && IsPrerollFinished(is_audio)) {
533 if (IsPrerollFinished(!is_audio)) {
534 prerolling_ = false;
535 StartInternal();
537 return;
540 if (is_clock_manager) {
541 // If we have a valid timestamp, start the starvation callback. Otherwise,
542 // reset the |start_time_ticks_| so that the next frame will not suffer
543 // from the decoding delay caused by the current frame.
544 if (current_presentation_timestamp != kNoTimestamp())
545 StartStarvationCallback(current_presentation_timestamp,
546 max_presentation_timestamp);
547 else
548 start_time_ticks_ = base::TimeTicks::Now();
551 if (is_audio)
552 DecodeMoreAudio();
553 else
554 DecodeMoreVideo();
557 bool MediaSourcePlayer::IsPrerollFinished(bool is_audio) const {
558 if (is_audio)
559 return !HasAudio() || !audio_decoder_job_->prerolling();
560 return !HasVideo() || !video_decoder_job_->prerolling();
563 void MediaSourcePlayer::DecodeMoreAudio() {
564 DVLOG(1) << __FUNCTION__;
565 DCHECK(!audio_decoder_job_->is_decoding());
566 DCHECK(!AudioFinished());
568 MediaDecoderJob::MediaDecoderJobStatus status = audio_decoder_job_->Decode(
569 start_time_ticks_,
570 start_presentation_timestamp_,
571 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_, true));
573 switch (status) {
574 case MediaDecoderJob::STATUS_SUCCESS:
575 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreAudio",
576 audio_decoder_job_.get());
577 break;
578 case MediaDecoderJob::STATUS_KEY_FRAME_REQUIRED:
579 NOTREACHED();
580 break;
581 case MediaDecoderJob::STATUS_FAILURE:
582 is_waiting_for_audio_decoder_ = true;
583 if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
584 SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
585 break;
589 void MediaSourcePlayer::DecodeMoreVideo() {
590 DVLOG(1) << __FUNCTION__;
591 DCHECK(!video_decoder_job_->is_decoding());
592 DCHECK(!VideoFinished());
594 MediaDecoderJob::MediaDecoderJobStatus status = video_decoder_job_->Decode(
595 start_time_ticks_,
596 start_presentation_timestamp_,
597 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_,
598 false));
600 switch (status) {
601 case MediaDecoderJob::STATUS_SUCCESS:
602 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreVideo",
603 video_decoder_job_.get());
604 break;
605 case MediaDecoderJob::STATUS_KEY_FRAME_REQUIRED:
606 BrowserSeekToCurrentTime();
607 break;
608 case MediaDecoderJob::STATUS_FAILURE:
609 is_waiting_for_video_decoder_ = true;
610 if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
611 SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
612 break;
616 void MediaSourcePlayer::PlaybackCompleted(bool is_audio) {
617 DVLOG(1) << __FUNCTION__ << "(" << is_audio << ")";
619 if (AudioFinished() && VideoFinished()) {
620 playing_ = false;
621 start_time_ticks_ = base::TimeTicks();
622 manager()->OnPlaybackComplete(player_id());
626 void MediaSourcePlayer::ClearDecodingData() {
627 DVLOG(1) << __FUNCTION__;
628 audio_decoder_job_->Flush();
629 video_decoder_job_->Flush();
630 start_time_ticks_ = base::TimeTicks();
633 bool MediaSourcePlayer::HasVideo() const {
634 return video_decoder_job_->HasStream();
637 bool MediaSourcePlayer::HasAudio() const {
638 return audio_decoder_job_->HasStream();
641 bool MediaSourcePlayer::AudioFinished() {
642 return audio_decoder_job_->OutputEOSReached() || !HasAudio();
645 bool MediaSourcePlayer::VideoFinished() {
646 return video_decoder_job_->OutputEOSReached() || !HasVideo();
649 void MediaSourcePlayer::OnDecoderStarved() {
650 DVLOG(1) << __FUNCTION__;
652 SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
653 ProcessPendingEvents();
656 void MediaSourcePlayer::StartStarvationCallback(
657 base::TimeDelta current_presentation_timestamp,
658 base::TimeDelta max_presentation_timestamp) {
659 // 20ms was chosen because it is the typical size of a compressed audio frame.
660 // Anything smaller than this would likely cause unnecessary cycling in and
661 // out of the prefetch state.
662 const base::TimeDelta kMinStarvationTimeout =
663 base::TimeDelta::FromMilliseconds(20);
665 base::TimeDelta current_timestamp = GetCurrentTime();
666 base::TimeDelta timeout;
667 if (HasAudio()) {
668 timeout = max_presentation_timestamp - current_timestamp;
669 } else {
670 DCHECK(current_timestamp <= current_presentation_timestamp);
672 // For video only streams, fps can be estimated from the difference
673 // between the previous and current presentation timestamps. The
674 // previous presentation timestamp is equal to current_timestamp.
675 // TODO(qinmin): determine whether 2 is a good coefficient for estimating
676 // video frame timeout.
677 timeout = 2 * (current_presentation_timestamp - current_timestamp);
680 timeout = std::max(timeout, kMinStarvationTimeout);
682 decoder_starvation_callback_.Reset(
683 base::Bind(&MediaSourcePlayer::OnDecoderStarved, weak_this_));
684 base::MessageLoop::current()->PostDelayedTask(
685 FROM_HERE, decoder_starvation_callback_.callback(), timeout);
688 void MediaSourcePlayer::OnPrefetchDone() {
689 DVLOG(1) << __FUNCTION__;
690 DCHECK(!audio_decoder_job_->is_decoding());
691 DCHECK(!video_decoder_job_->is_decoding());
693 // A previously posted OnPrefetchDone() could race against a Release(). If
694 // Release() won the race, we should no longer have decoder jobs.
695 // TODO(qinmin/wolenetz): Maintain channel state to not double-request data
696 // or drop data received across Release()+Start(). See http://crbug.com/306314
697 // and http://crbug.com/304234.
698 if (!IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
699 DVLOG(1) << __FUNCTION__ << " : aborting";
700 return;
703 ClearPendingEvent(PREFETCH_DONE_EVENT_PENDING);
705 if (pending_event_ != NO_EVENT_PENDING) {
706 ProcessPendingEvents();
707 return;
710 if (!playing_)
711 return;
713 start_time_ticks_ = base::TimeTicks::Now();
714 start_presentation_timestamp_ = GetCurrentTime();
715 if (!interpolator_.interpolating())
716 interpolator_.StartInterpolating();
718 if (!AudioFinished())
719 DecodeMoreAudio();
721 if (!VideoFinished())
722 DecodeMoreVideo();
725 void MediaSourcePlayer::OnDemuxerConfigsChanged() {
726 manager()->OnMediaMetadataChanged(
727 player_id(), duration_, GetVideoWidth(), GetVideoHeight(), true);
730 const char* MediaSourcePlayer::GetEventName(PendingEventFlags event) {
731 // Please keep this in sync with PendingEventFlags.
732 static const char* kPendingEventNames[] = {
733 "PREFETCH_DONE",
734 "SEEK",
735 "DECODER_CREATION",
736 "PREFETCH_REQUEST",
739 int mask = 1;
740 for (size_t i = 0; i < arraysize(kPendingEventNames); ++i, mask <<= 1) {
741 if (event & mask)
742 return kPendingEventNames[i];
745 return "UNKNOWN";
748 bool MediaSourcePlayer::IsEventPending(PendingEventFlags event) const {
749 return pending_event_ & event;
752 void MediaSourcePlayer::SetPendingEvent(PendingEventFlags event) {
753 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
754 DCHECK_NE(event, NO_EVENT_PENDING);
755 DCHECK(!IsEventPending(event));
757 pending_event_ |= event;
760 void MediaSourcePlayer::ClearPendingEvent(PendingEventFlags event) {
761 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
762 DCHECK_NE(event, NO_EVENT_PENDING);
763 DCHECK(IsEventPending(event)) << GetEventName(event);
765 pending_event_ &= ~event;
768 void MediaSourcePlayer::RetryDecoderCreation(bool audio, bool video) {
769 if (audio)
770 is_waiting_for_audio_decoder_ = false;
771 if (video)
772 is_waiting_for_video_decoder_ = false;
773 if (IsEventPending(DECODER_CREATION_EVENT_PENDING))
774 ProcessPendingEvents();
777 void MediaSourcePlayer::OnKeyAdded() {
778 DVLOG(1) << __FUNCTION__;
780 if (is_waiting_for_key_) {
781 ResumePlaybackAfterKeyAdded();
782 return;
785 if ((audio_decoder_job_->is_content_encrypted() &&
786 audio_decoder_job_->is_decoding()) ||
787 (video_decoder_job_->is_content_encrypted() &&
788 video_decoder_job_->is_decoding())) {
789 DVLOG(1) << __FUNCTION__ << ": " << "Key added during pending decode.";
790 key_added_while_decode_pending_ = true;
794 void MediaSourcePlayer::ResumePlaybackAfterKeyAdded() {
795 DVLOG(1) << __FUNCTION__;
796 DCHECK(is_waiting_for_key_ || key_added_while_decode_pending_);
798 is_waiting_for_key_ = false;
799 key_added_while_decode_pending_ = false;
801 // StartInternal() will trigger a prefetch, where in most cases we'll just
802 // use previously received data.
803 if (playing_)
804 StartInternal();
807 void MediaSourcePlayer::OnCdmUnset() {
808 DVLOG(1) << __FUNCTION__;
809 DCHECK(drm_bridge_);
810 // TODO(xhwang): Currently this is only called during teardown. Support full
811 // detachment of CDM during playback. This will be needed when we start to
812 // support setMediaKeys(0) (see http://crbug.com/330324), or when we release
813 // MediaDrm when the video is paused, or when the device goes to sleep (see
814 // http://crbug.com/272421).
815 audio_decoder_job_->SetDrmBridge(NULL);
816 video_decoder_job_->SetDrmBridge(NULL);
817 cdm_registration_id_ = 0;
818 drm_bridge_ = NULL;
821 } // namespace media