Remove obsolete for_web_contents parameter in FontRenderParamsQuery.
[chromium-blink-merge.git] / media / base / android / media_source_player.cc
blob52ea7db11af5e240a43ecd86a598cb99d6320db6
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_drm_bridge.h"
20 #include "media/base/android/media_player_manager.h"
21 #include "media/base/android/video_decoder_job.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 SetAudible(false);
190 DetachListener();
193 void MediaSourcePlayer::SetVolume(double volume) {
194 audio_decoder_job_->SetVolume(volume);
197 bool MediaSourcePlayer::CanPause() {
198 return Seekable();
201 bool MediaSourcePlayer::CanSeekForward() {
202 return Seekable();
205 bool MediaSourcePlayer::CanSeekBackward() {
206 return Seekable();
209 bool MediaSourcePlayer::IsPlayerReady() {
210 return audio_decoder_job_ || video_decoder_job_;
213 void MediaSourcePlayer::StartInternal() {
214 DVLOG(1) << __FUNCTION__;
215 // If there are pending events, wait for them finish.
216 if (pending_event_ != NO_EVENT_PENDING)
217 return;
219 // When we start, we could have new demuxed data coming in. This new data
220 // could be clear (not encrypted) or encrypted with different keys. So key
221 // related info should all be cleared.
222 is_waiting_for_key_ = false;
223 key_added_while_decode_pending_ = false;
224 AttachListener(NULL);
226 SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
227 ProcessPendingEvents();
230 void MediaSourcePlayer::OnDemuxerConfigsAvailable(
231 const DemuxerConfigs& configs) {
232 DVLOG(1) << __FUNCTION__;
233 DCHECK(!HasAudio() && !HasVideo());
235 duration_ = configs.duration;
237 audio_decoder_job_->SetDemuxerConfigs(configs);
238 video_decoder_job_->SetDemuxerConfigs(configs);
239 OnDemuxerConfigsChanged();
242 void MediaSourcePlayer::OnDemuxerDataAvailable(const DemuxerData& data) {
243 DVLOG(1) << __FUNCTION__ << "(" << data.type << ")";
244 DCHECK_LT(0u, data.access_units.size());
245 CHECK_GE(1u, data.demuxer_configs.size());
247 if (data.type == DemuxerStream::AUDIO)
248 audio_decoder_job_->OnDataReceived(data);
249 else if (data.type == DemuxerStream::VIDEO)
250 video_decoder_job_->OnDataReceived(data);
253 void MediaSourcePlayer::OnDemuxerDurationChanged(base::TimeDelta duration) {
254 duration_ = duration;
257 void MediaSourcePlayer::OnMediaCryptoReady() {
258 DCHECK(!drm_bridge_->GetMediaCrypto().is_null());
259 drm_bridge_->SetMediaCryptoReadyCB(base::Closure());
261 // Retry decoder creation if the decoders are waiting for MediaCrypto.
262 RetryDecoderCreation(true, true);
265 void MediaSourcePlayer::SetCdm(BrowserCdm* cdm) {
266 // Currently we don't support DRM change during the middle of playback, even
267 // if the player is paused.
268 // TODO(qinmin): support DRM change after playback has started.
269 // http://crbug.com/253792.
270 if (GetCurrentTime() > base::TimeDelta()) {
271 VLOG(0) << "Setting DRM bridge after playback has started. "
272 << "This is not well supported!";
275 if (drm_bridge_) {
276 NOTREACHED() << "Currently we do not support resetting CDM.";
277 return;
280 // Only MediaDrmBridge will be set on MediaSourcePlayer.
281 drm_bridge_ = static_cast<MediaDrmBridge*>(cdm);
283 cdm_registration_id_ = drm_bridge_->RegisterPlayer(
284 base::Bind(&MediaSourcePlayer::OnKeyAdded, weak_this_),
285 base::Bind(&MediaSourcePlayer::OnCdmUnset, weak_this_));
287 audio_decoder_job_->SetDrmBridge(drm_bridge_);
288 video_decoder_job_->SetDrmBridge(drm_bridge_);
290 if (drm_bridge_->GetMediaCrypto().is_null()) {
291 drm_bridge_->SetMediaCryptoReadyCB(
292 base::Bind(&MediaSourcePlayer::OnMediaCryptoReady, weak_this_));
293 return;
296 // If the player is previously waiting for CDM, retry decoder creation.
297 RetryDecoderCreation(true, true);
300 void MediaSourcePlayer::OnDemuxerSeekDone(
301 base::TimeDelta actual_browser_seek_time) {
302 DVLOG(1) << __FUNCTION__;
304 ClearPendingEvent(SEEK_EVENT_PENDING);
305 if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING))
306 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
308 if (pending_seek_) {
309 DVLOG(1) << __FUNCTION__ << "processing pending seek";
310 DCHECK(doing_browser_seek_);
311 pending_seek_ = false;
312 SeekTo(pending_seek_time_);
313 return;
316 // It is possible that a browser seek to I-frame had to seek to a buffered
317 // I-frame later than the requested one due to data removal or GC. Update
318 // player clock to the actual seek target.
319 if (doing_browser_seek_) {
320 DCHECK(actual_browser_seek_time != kNoTimestamp());
321 base::TimeDelta seek_time = actual_browser_seek_time;
322 // A browser seek must not jump into the past. Ideally, it seeks to the
323 // requested time, but it might jump into the future.
324 DCHECK(seek_time >= GetCurrentTime());
325 DVLOG(1) << __FUNCTION__ << " : setting clock to actual browser seek time: "
326 << seek_time.InSecondsF();
327 interpolator_.SetBounds(seek_time, seek_time);
328 audio_decoder_job_->SetBaseTimestamp(seek_time);
329 } else {
330 DCHECK(actual_browser_seek_time == kNoTimestamp());
333 base::TimeDelta current_time = GetCurrentTime();
334 // TODO(qinmin): Simplify the logic by using |start_presentation_timestamp_|
335 // to preroll media decoder jobs. Currently |start_presentation_timestamp_|
336 // is calculated from decoder output, while preroll relies on the access
337 // unit's timestamp. There are some differences between the two.
338 preroll_timestamp_ = current_time;
339 if (HasAudio())
340 audio_decoder_job_->BeginPrerolling(preroll_timestamp_);
341 if (HasVideo())
342 video_decoder_job_->BeginPrerolling(preroll_timestamp_);
343 prerolling_ = true;
345 if (!doing_browser_seek_)
346 manager()->OnSeekComplete(player_id(), current_time);
348 ProcessPendingEvents();
351 void MediaSourcePlayer::UpdateTimestamps(
352 base::TimeDelta current_presentation_timestamp,
353 base::TimeDelta max_presentation_timestamp) {
354 interpolator_.SetBounds(current_presentation_timestamp,
355 max_presentation_timestamp);
356 manager()->OnTimeUpdate(player_id(),
357 GetCurrentTime(),
358 base::TimeTicks::Now());
361 void MediaSourcePlayer::ProcessPendingEvents() {
362 DVLOG(1) << __FUNCTION__ << " : 0x" << std::hex << pending_event_;
363 // Wait for all the decoding jobs to finish before processing pending tasks.
364 if (video_decoder_job_->is_decoding()) {
365 DVLOG(1) << __FUNCTION__ << " : A video job is still decoding.";
366 return;
369 if (audio_decoder_job_->is_decoding()) {
370 DVLOG(1) << __FUNCTION__ << " : An audio job is still decoding.";
371 return;
374 if (IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
375 DVLOG(1) << __FUNCTION__ << " : PREFETCH_DONE still pending.";
376 return;
379 if (IsEventPending(SEEK_EVENT_PENDING)) {
380 DVLOG(1) << __FUNCTION__ << " : Handling SEEK_EVENT";
381 ClearDecodingData();
382 audio_decoder_job_->SetBaseTimestamp(GetCurrentTime());
383 demuxer_->RequestDemuxerSeek(GetCurrentTime(), doing_browser_seek_);
384 return;
387 if (IsEventPending(DECODER_CREATION_EVENT_PENDING)) {
388 // Don't continue if one of the decoder is not created.
389 if (is_waiting_for_audio_decoder_ || is_waiting_for_video_decoder_)
390 return;
391 ClearPendingEvent(DECODER_CREATION_EVENT_PENDING);
394 if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING)) {
395 DVLOG(1) << __FUNCTION__ << " : Handling PREFETCH_REQUEST_EVENT.";
397 int count = (AudioFinished() ? 0 : 1) + (VideoFinished() ? 0 : 1);
399 // It is possible that all streams have finished decode, yet starvation
400 // occurred during the last stream's EOS decode. In this case, prefetch is a
401 // no-op.
402 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
403 if (count == 0)
404 return;
406 SetPendingEvent(PREFETCH_DONE_EVENT_PENDING);
407 base::Closure barrier = BarrierClosure(
408 count, base::Bind(&MediaSourcePlayer::OnPrefetchDone, weak_this_));
410 if (!AudioFinished())
411 audio_decoder_job_->Prefetch(barrier);
413 if (!VideoFinished())
414 video_decoder_job_->Prefetch(barrier);
416 return;
419 DCHECK_EQ(pending_event_, NO_EVENT_PENDING);
421 // Now that all pending events have been handled, resume decoding if we are
422 // still playing.
423 if (playing_)
424 StartInternal();
427 void MediaSourcePlayer::MediaDecoderCallback(
428 bool is_audio, MediaCodecStatus status,
429 base::TimeDelta current_presentation_timestamp,
430 base::TimeDelta max_presentation_timestamp) {
431 DVLOG(1) << __FUNCTION__ << ": " << is_audio << ", " << status;
433 // TODO(xhwang): Drop IntToString() when http://crbug.com/303899 is fixed.
434 if (is_audio) {
435 TRACE_EVENT_ASYNC_END1("media",
436 "MediaSourcePlayer::DecodeMoreAudio",
437 audio_decoder_job_.get(),
438 "MediaCodecStatus",
439 base::IntToString(status));
440 } else {
441 TRACE_EVENT_ASYNC_END1("media",
442 "MediaSourcePlayer::DecodeMoreVideo",
443 video_decoder_job_.get(),
444 "MediaCodecStatus",
445 base::IntToString(status));
448 // Let tests hook the completion of this decode cycle.
449 if (!decode_callback_for_testing_.is_null())
450 base::ResetAndReturn(&decode_callback_for_testing_).Run();
452 bool is_clock_manager = is_audio || !HasAudio();
454 if (is_clock_manager)
455 decoder_starvation_callback_.Cancel();
457 if (status == MEDIA_CODEC_ERROR) {
458 DVLOG(1) << __FUNCTION__ << " : decode error";
459 Release();
460 manager()->OnError(player_id(), MEDIA_ERROR_DECODE);
461 return;
464 DCHECK(!IsEventPending(PREFETCH_DONE_EVENT_PENDING));
466 // Let |SEEK_EVENT_PENDING| (the highest priority event outside of
467 // |PREFETCH_DONE_EVENT_PENDING|) preempt output EOS detection here. Process
468 // any other pending events only after handling EOS detection.
469 if (IsEventPending(SEEK_EVENT_PENDING)) {
470 ProcessPendingEvents();
471 return;
474 if ((status == MEDIA_CODEC_OK || status == MEDIA_CODEC_INPUT_END_OF_STREAM) &&
475 is_clock_manager && current_presentation_timestamp != kNoTimestamp()) {
476 UpdateTimestamps(current_presentation_timestamp,
477 max_presentation_timestamp);
480 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM) {
481 PlaybackCompleted(is_audio);
482 if (is_clock_manager)
483 interpolator_.StopInterpolating();
486 if (pending_event_ != NO_EVENT_PENDING) {
487 ProcessPendingEvents();
488 return;
491 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM) {
492 if (is_audio)
493 SetAudible(false);
494 return;
497 if (!playing_) {
498 if (is_clock_manager)
499 interpolator_.StopInterpolating();
501 if (is_audio)
502 SetAudible(false);
503 return;
506 if (status == MEDIA_CODEC_NO_KEY) {
507 if (key_added_while_decode_pending_) {
508 DVLOG(2) << __FUNCTION__ << ": Key was added during decoding.";
509 ResumePlaybackAfterKeyAdded();
510 } else {
511 if (is_audio)
512 SetAudible(false);
514 is_waiting_for_key_ = true;
515 manager()->OnWaitingForDecryptionKey(player_id());
517 return;
520 // If |key_added_while_decode_pending_| is true and both audio and video
521 // decoding succeeded, we should clear |key_added_while_decode_pending_| here.
522 // But that would add more complexity into this function. If we don't clear it
523 // here, the worst case would be we call ResumePlaybackAfterKeyAdded() when
524 // we don't really have a new key. This should rarely happen and the
525 // performance impact should be pretty small.
526 // TODO(qinmin/xhwang): This class is complicated because we handle both audio
527 // and video in one file. If we separate them, we should be able to remove a
528 // lot of duplication.
530 // If the status is MEDIA_CODEC_ABORT, stop decoding new data. The player is
531 // in the middle of a seek or stop event and needs to wait for the IPCs to
532 // come.
533 if (status == MEDIA_CODEC_ABORT) {
534 if (is_audio)
535 SetAudible(false);
536 return;
539 if (prerolling_ && IsPrerollFinished(is_audio)) {
540 if (IsPrerollFinished(!is_audio)) {
541 prerolling_ = false;
542 StartInternal();
544 return;
547 // We successfully decoded a frame and going to the next one.
548 // Set the audible state.
549 if (is_audio) {
550 bool is_audible = !prerolling_ && audio_decoder_job_->volume() > 0;
551 SetAudible(is_audible);
554 if (is_clock_manager) {
555 // If we have a valid timestamp, start the starvation callback. Otherwise,
556 // reset the |start_time_ticks_| so that the next frame will not suffer
557 // from the decoding delay caused by the current frame.
558 if (current_presentation_timestamp != kNoTimestamp())
559 StartStarvationCallback(current_presentation_timestamp,
560 max_presentation_timestamp);
561 else
562 start_time_ticks_ = base::TimeTicks::Now();
565 if (is_audio)
566 DecodeMoreAudio();
567 else
568 DecodeMoreVideo();
571 bool MediaSourcePlayer::IsPrerollFinished(bool is_audio) const {
572 if (is_audio)
573 return !HasAudio() || !audio_decoder_job_->prerolling();
574 return !HasVideo() || !video_decoder_job_->prerolling();
577 void MediaSourcePlayer::DecodeMoreAudio() {
578 DVLOG(1) << __FUNCTION__;
579 DCHECK(!audio_decoder_job_->is_decoding());
580 DCHECK(!AudioFinished());
582 MediaDecoderJob::MediaDecoderJobStatus status = audio_decoder_job_->Decode(
583 start_time_ticks_,
584 start_presentation_timestamp_,
585 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_, true));
587 switch (status) {
588 case MediaDecoderJob::STATUS_SUCCESS:
589 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreAudio",
590 audio_decoder_job_.get());
591 break;
592 case MediaDecoderJob::STATUS_KEY_FRAME_REQUIRED:
593 NOTREACHED();
594 break;
595 case MediaDecoderJob::STATUS_FAILURE:
596 is_waiting_for_audio_decoder_ = true;
597 if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
598 SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
599 break;
603 void MediaSourcePlayer::DecodeMoreVideo() {
604 DVLOG(1) << __FUNCTION__;
605 DCHECK(!video_decoder_job_->is_decoding());
606 DCHECK(!VideoFinished());
608 MediaDecoderJob::MediaDecoderJobStatus status = video_decoder_job_->Decode(
609 start_time_ticks_,
610 start_presentation_timestamp_,
611 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_,
612 false));
614 switch (status) {
615 case MediaDecoderJob::STATUS_SUCCESS:
616 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreVideo",
617 video_decoder_job_.get());
618 break;
619 case MediaDecoderJob::STATUS_KEY_FRAME_REQUIRED:
620 BrowserSeekToCurrentTime();
621 break;
622 case MediaDecoderJob::STATUS_FAILURE:
623 is_waiting_for_video_decoder_ = true;
624 if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
625 SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
626 break;
630 void MediaSourcePlayer::PlaybackCompleted(bool is_audio) {
631 DVLOG(1) << __FUNCTION__ << "(" << is_audio << ")";
633 if (AudioFinished() && VideoFinished()) {
634 playing_ = false;
635 start_time_ticks_ = base::TimeTicks();
636 manager()->OnPlaybackComplete(player_id());
640 void MediaSourcePlayer::ClearDecodingData() {
641 DVLOG(1) << __FUNCTION__;
642 audio_decoder_job_->Flush();
643 video_decoder_job_->Flush();
644 start_time_ticks_ = base::TimeTicks();
647 bool MediaSourcePlayer::HasVideo() const {
648 return video_decoder_job_->HasStream();
651 bool MediaSourcePlayer::HasAudio() const {
652 return audio_decoder_job_->HasStream();
655 bool MediaSourcePlayer::AudioFinished() {
656 return audio_decoder_job_->OutputEOSReached() || !HasAudio();
659 bool MediaSourcePlayer::VideoFinished() {
660 return video_decoder_job_->OutputEOSReached() || !HasVideo();
663 void MediaSourcePlayer::OnDecoderStarved() {
664 DVLOG(1) << __FUNCTION__;
666 if (HasAudio()) {
667 // If the starvation timer fired but there are no encoded frames
668 // in the queue we believe the demuxer (i.e. renderer process) froze.
669 if (!audio_decoder_job_->HasData())
670 SetAudible(false);
673 SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
674 ProcessPendingEvents();
677 void MediaSourcePlayer::StartStarvationCallback(
678 base::TimeDelta current_presentation_timestamp,
679 base::TimeDelta max_presentation_timestamp) {
680 // 20ms was chosen because it is the typical size of a compressed audio frame.
681 // Anything smaller than this would likely cause unnecessary cycling in and
682 // out of the prefetch state.
683 const base::TimeDelta kMinStarvationTimeout =
684 base::TimeDelta::FromMilliseconds(20);
686 base::TimeDelta current_timestamp = GetCurrentTime();
687 base::TimeDelta timeout;
688 if (HasAudio()) {
689 timeout = max_presentation_timestamp - current_timestamp;
690 } else {
691 DCHECK(current_timestamp <= current_presentation_timestamp);
693 // For video only streams, fps can be estimated from the difference
694 // between the previous and current presentation timestamps. The
695 // previous presentation timestamp is equal to current_timestamp.
696 // TODO(qinmin): determine whether 2 is a good coefficient for estimating
697 // video frame timeout.
698 timeout = 2 * (current_presentation_timestamp - current_timestamp);
701 timeout = std::max(timeout, kMinStarvationTimeout);
703 decoder_starvation_callback_.Reset(
704 base::Bind(&MediaSourcePlayer::OnDecoderStarved, weak_this_));
705 base::MessageLoop::current()->PostDelayedTask(
706 FROM_HERE, decoder_starvation_callback_.callback(), timeout);
709 void MediaSourcePlayer::OnPrefetchDone() {
710 DVLOG(1) << __FUNCTION__;
711 DCHECK(!audio_decoder_job_->is_decoding());
712 DCHECK(!video_decoder_job_->is_decoding());
714 // A previously posted OnPrefetchDone() could race against a Release(). If
715 // Release() won the race, we should no longer have decoder jobs.
716 // TODO(qinmin/wolenetz): Maintain channel state to not double-request data
717 // or drop data received across Release()+Start(). See http://crbug.com/306314
718 // and http://crbug.com/304234.
719 if (!IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
720 DVLOG(1) << __FUNCTION__ << " : aborting";
721 return;
724 ClearPendingEvent(PREFETCH_DONE_EVENT_PENDING);
726 if (pending_event_ != NO_EVENT_PENDING) {
727 ProcessPendingEvents();
728 return;
731 if (!playing_)
732 return;
734 start_time_ticks_ = base::TimeTicks::Now();
735 start_presentation_timestamp_ = GetCurrentTime();
736 if (!interpolator_.interpolating())
737 interpolator_.StartInterpolating();
739 if (!AudioFinished())
740 DecodeMoreAudio();
742 if (!VideoFinished())
743 DecodeMoreVideo();
746 void MediaSourcePlayer::OnDemuxerConfigsChanged() {
747 manager()->OnMediaMetadataChanged(
748 player_id(), duration_, GetVideoWidth(), GetVideoHeight(), true);
751 const char* MediaSourcePlayer::GetEventName(PendingEventFlags event) {
752 // Please keep this in sync with PendingEventFlags.
753 static const char* kPendingEventNames[] = {
754 "PREFETCH_DONE",
755 "SEEK",
756 "DECODER_CREATION",
757 "PREFETCH_REQUEST",
760 int mask = 1;
761 for (size_t i = 0; i < arraysize(kPendingEventNames); ++i, mask <<= 1) {
762 if (event & mask)
763 return kPendingEventNames[i];
766 return "UNKNOWN";
769 bool MediaSourcePlayer::IsEventPending(PendingEventFlags event) const {
770 return pending_event_ & event;
773 void MediaSourcePlayer::SetPendingEvent(PendingEventFlags event) {
774 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
775 DCHECK_NE(event, NO_EVENT_PENDING);
776 DCHECK(!IsEventPending(event));
778 pending_event_ |= event;
781 void MediaSourcePlayer::ClearPendingEvent(PendingEventFlags event) {
782 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
783 DCHECK_NE(event, NO_EVENT_PENDING);
784 DCHECK(IsEventPending(event)) << GetEventName(event);
786 pending_event_ &= ~event;
789 void MediaSourcePlayer::RetryDecoderCreation(bool audio, bool video) {
790 if (audio)
791 is_waiting_for_audio_decoder_ = false;
792 if (video)
793 is_waiting_for_video_decoder_ = false;
794 if (IsEventPending(DECODER_CREATION_EVENT_PENDING))
795 ProcessPendingEvents();
798 void MediaSourcePlayer::OnKeyAdded() {
799 DVLOG(1) << __FUNCTION__;
801 if (is_waiting_for_key_) {
802 ResumePlaybackAfterKeyAdded();
803 return;
806 if ((audio_decoder_job_->is_content_encrypted() &&
807 audio_decoder_job_->is_decoding()) ||
808 (video_decoder_job_->is_content_encrypted() &&
809 video_decoder_job_->is_decoding())) {
810 DVLOG(1) << __FUNCTION__ << ": " << "Key added during pending decode.";
811 key_added_while_decode_pending_ = true;
815 void MediaSourcePlayer::ResumePlaybackAfterKeyAdded() {
816 DVLOG(1) << __FUNCTION__;
817 DCHECK(is_waiting_for_key_ || key_added_while_decode_pending_);
819 is_waiting_for_key_ = false;
820 key_added_while_decode_pending_ = false;
822 // StartInternal() will trigger a prefetch, where in most cases we'll just
823 // use previously received data.
824 if (playing_)
825 StartInternal();
828 void MediaSourcePlayer::OnCdmUnset() {
829 DVLOG(1) << __FUNCTION__;
830 DCHECK(drm_bridge_);
831 // TODO(xhwang): Currently this is only called during teardown. Support full
832 // detachment of CDM during playback. This will be needed when we start to
833 // support setMediaKeys(0) (see http://crbug.com/330324), or when we release
834 // MediaDrm when the video is paused, or when the device goes to sleep (see
835 // http://crbug.com/272421).
836 audio_decoder_job_->SetDrmBridge(NULL);
837 video_decoder_job_->SetDrmBridge(NULL);
838 cdm_registration_id_ = 0;
839 drm_bridge_ = NULL;
842 } // namespace media