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"
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/debug/trace_event.h"
16 #include "base/logging.h"
17 #include "base/strings/string_number_conversions.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"
25 MediaSourcePlayer::MediaSourcePlayer(
27 MediaPlayerManager* manager,
28 const RequestMediaResourcesCB& request_media_resources_cb,
29 scoped_ptr<DemuxerAndroid> demuxer,
30 const GURL& frame_url)
31 : MediaPlayerAndroid(player_id,
33 request_media_resources_cb,
35 demuxer_(demuxer.Pass()),
36 pending_event_(NO_EVENT_PENDING),
38 interpolator_(&default_tick_clock_),
39 doing_browser_seek_(false),
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),
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() {
69 DCHECK_EQ(!drm_bridge_, !cdm_registration_id_);
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()))
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.
116 base::TimeDelta::FromMilliseconds(std::numeric_limits<int32>::max());
119 void MediaSourcePlayer::Start() {
120 DVLOG(1) << __FUNCTION__;
124 bool request_fullscreen = IsProtectedSurfaceRequired();
125 #if defined(VIDEO_HOLE)
126 // Skip to request fullscreen when hole-punching is used.
127 request_fullscreen = request_fullscreen &&
128 !manager()->ShouldUseVideoOverlayForEmbeddedEncryptedVideo();
129 #endif // defined(VIDEO_HOLE)
130 if (request_fullscreen)
131 manager()->RequestFullScreen(player_id());
136 void MediaSourcePlayer::Pause(bool is_media_related_action) {
137 DVLOG(1) << __FUNCTION__;
139 // Since decoder jobs have their own thread, decoding is not fully paused
140 // until all the decoder jobs call MediaDecoderCallback(). It is possible
141 // that Start() is called while the player is waiting for
142 // MediaDecoderCallback(). In that case, decoding will continue when
143 // MediaDecoderCallback() is called.
145 start_time_ticks_ = base::TimeTicks();
148 bool MediaSourcePlayer::IsPlaying() {
152 int MediaSourcePlayer::GetVideoWidth() {
153 return video_decoder_job_->output_width();
156 int MediaSourcePlayer::GetVideoHeight() {
157 return video_decoder_job_->output_height();
160 void MediaSourcePlayer::SeekTo(base::TimeDelta timestamp) {
161 DVLOG(1) << __FUNCTION__ << "(" << timestamp.InSecondsF() << ")";
163 if (IsEventPending(SEEK_EVENT_PENDING)) {
164 DCHECK(doing_browser_seek_) << "SeekTo while SeekTo in progress";
165 DCHECK(!pending_seek_) << "SeekTo while SeekTo pending browser seek";
167 // There is a browser seek currently in progress to obtain I-frame to feed
168 // a newly constructed video decoder. Remember this real seek request so
169 // it can be initiated once OnDemuxerSeekDone() occurs for the browser seek.
170 pending_seek_ = true;
171 pending_seek_time_ = timestamp;
175 doing_browser_seek_ = false;
176 ScheduleSeekEventAndStopDecoding(timestamp);
179 base::TimeDelta MediaSourcePlayer::GetCurrentTime() {
180 return std::min(interpolator_.GetInterpolatedTime(), duration_);
183 base::TimeDelta MediaSourcePlayer::GetDuration() {
187 void MediaSourcePlayer::Release() {
188 DVLOG(1) << __FUNCTION__;
190 audio_decoder_job_->ReleaseDecoderResources();
191 video_decoder_job_->ReleaseDecoderResources();
193 // Prevent player restart, including job re-creation attempts.
196 decoder_starvation_callback_.Cancel();
200 void MediaSourcePlayer::SetVolume(double volume) {
201 audio_decoder_job_->SetVolume(volume);
204 bool MediaSourcePlayer::CanPause() {
208 bool MediaSourcePlayer::CanSeekForward() {
212 bool MediaSourcePlayer::CanSeekBackward() {
216 bool MediaSourcePlayer::IsPlayerReady() {
217 return audio_decoder_job_ || video_decoder_job_;
220 void MediaSourcePlayer::StartInternal() {
221 DVLOG(1) << __FUNCTION__;
222 // If there are pending events, wait for them finish.
223 if (pending_event_ != NO_EVENT_PENDING)
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());
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!";
282 NOTREACHED() << "Currently we do not support resetting CDM.";
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_));
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);
315 DVLOG(1) << __FUNCTION__ << "processing pending seek";
316 DCHECK(doing_browser_seek_);
317 pending_seek_ = false;
318 SeekTo(pending_seek_time_);
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);
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;
346 audio_decoder_job_->BeginPrerolling(preroll_timestamp_);
348 video_decoder_job_->BeginPrerolling(preroll_timestamp_);
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(),
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.";
375 if (audio_decoder_job_->is_decoding()) {
376 DVLOG(1) << __FUNCTION__ << " : An audio job is still decoding.";
380 if (IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
381 DVLOG(1) << __FUNCTION__ << " : PREFETCH_DONE still pending.";
385 if (IsEventPending(SEEK_EVENT_PENDING)) {
386 DVLOG(1) << __FUNCTION__ << " : Handling SEEK_EVENT";
388 audio_decoder_job_->SetBaseTimestamp(GetCurrentTime());
389 demuxer_->RequestDemuxerSeek(GetCurrentTime(), doing_browser_seek_);
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_)
397 ClearPendingEvent(DECODER_CREATION_EVENT_PENDING);
400 if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING)) {
401 DVLOG(1) << __FUNCTION__ << " : Handling PREFETCH_REQUEST_EVENT.";
402 int count = (AudioFinished() ? 0 : 1) + (VideoFinished() ? 0 : 1);
404 // It is possible that all streams have finished decode, yet starvation
405 // occurred during the last stream's EOS decode. In this case, prefetch is a
407 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
411 SetPendingEvent(PREFETCH_DONE_EVENT_PENDING);
412 base::Closure barrier = BarrierClosure(
413 count, base::Bind(&MediaSourcePlayer::OnPrefetchDone, weak_this_));
415 if (!AudioFinished())
416 audio_decoder_job_->Prefetch(barrier);
418 if (!VideoFinished())
419 video_decoder_job_->Prefetch(barrier);
424 DCHECK_EQ(pending_event_, NO_EVENT_PENDING);
426 // Now that all pending events have been handled, resume decoding if we are
432 void MediaSourcePlayer::MediaDecoderCallback(
433 bool is_audio, MediaCodecStatus status,
434 base::TimeDelta current_presentation_timestamp,
435 base::TimeDelta max_presentation_timestamp) {
436 DVLOG(1) << __FUNCTION__ << ": " << is_audio << ", " << status;
438 // TODO(xhwang): Drop IntToString() when http://crbug.com/303899 is fixed.
440 TRACE_EVENT_ASYNC_END1("media",
441 "MediaSourcePlayer::DecodeMoreAudio",
442 audio_decoder_job_.get(),
444 base::IntToString(status));
446 TRACE_EVENT_ASYNC_END1("media",
447 "MediaSourcePlayer::DecodeMoreVideo",
448 video_decoder_job_.get(),
450 base::IntToString(status));
453 // Let tests hook the completion of this decode cycle.
454 if (!decode_callback_for_testing_.is_null())
455 base::ResetAndReturn(&decode_callback_for_testing_).Run();
457 bool is_clock_manager = is_audio || !HasAudio();
459 if (is_clock_manager)
460 decoder_starvation_callback_.Cancel();
462 if (status == MEDIA_CODEC_ERROR) {
463 DVLOG(1) << __FUNCTION__ << " : decode error";
465 manager()->OnError(player_id(), MEDIA_ERROR_DECODE);
469 DCHECK(!IsEventPending(PREFETCH_DONE_EVENT_PENDING));
471 // Let |SEEK_EVENT_PENDING| (the highest priority event outside of
472 // |PREFETCH_DONE_EVENT_PENDING|) preempt output EOS detection here. Process
473 // any other pending events only after handling EOS detection.
474 if (IsEventPending(SEEK_EVENT_PENDING)) {
475 ProcessPendingEvents();
479 if ((status == MEDIA_CODEC_OK || status == MEDIA_CODEC_INPUT_END_OF_STREAM) &&
480 is_clock_manager && current_presentation_timestamp != kNoTimestamp()) {
481 UpdateTimestamps(current_presentation_timestamp,
482 max_presentation_timestamp);
485 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM) {
486 PlaybackCompleted(is_audio);
487 if (is_clock_manager)
488 interpolator_.StopInterpolating();
491 if (pending_event_ != NO_EVENT_PENDING) {
492 ProcessPendingEvents();
496 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM)
500 if (is_clock_manager)
501 interpolator_.StopInterpolating();
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();
510 is_waiting_for_key_ = true;
515 // If |key_added_while_decode_pending_| is true and both audio and video
516 // decoding succeeded, we should clear |key_added_while_decode_pending_| here.
517 // But that would add more complexity into this function. If we don't clear it
518 // here, the worst case would be we call ResumePlaybackAfterKeyAdded() when
519 // we don't really have a new key. This should rarely happen and the
520 // performance impact should be pretty small.
521 // TODO(qinmin/xhwang): This class is complicated because we handle both audio
522 // and video in one file. If we separate them, we should be able to remove a
523 // lot of duplication.
525 // If the status is MEDIA_CODEC_STOPPED, stop decoding new data. The player is
526 // in the middle of a seek or stop event and needs to wait for the IPCs to
528 if (status == MEDIA_CODEC_STOPPED)
531 if (prerolling_ && IsPrerollFinished(is_audio)) {
532 if (IsPrerollFinished(!is_audio)) {
539 if (is_clock_manager) {
540 // If we have a valid timestamp, start the starvation callback. Otherwise,
541 // reset the |start_time_ticks_| so that the next frame will not suffer
542 // from the decoding delay caused by the current frame.
543 if (current_presentation_timestamp != kNoTimestamp())
544 StartStarvationCallback(current_presentation_timestamp,
545 max_presentation_timestamp);
547 start_time_ticks_ = base::TimeTicks::Now();
556 bool MediaSourcePlayer::IsPrerollFinished(bool is_audio) const {
558 return !HasAudio() || !audio_decoder_job_->prerolling();
559 return !HasVideo() || !video_decoder_job_->prerolling();
562 void MediaSourcePlayer::DecodeMoreAudio() {
563 DVLOG(1) << __FUNCTION__;
564 DCHECK(!audio_decoder_job_->is_decoding());
565 DCHECK(!AudioFinished());
567 if (audio_decoder_job_->Decode(
569 start_presentation_timestamp_,
570 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_, true))) {
571 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreAudio",
572 audio_decoder_job_.get());
576 is_waiting_for_audio_decoder_ = true;
577 if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
578 SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
581 void MediaSourcePlayer::DecodeMoreVideo() {
582 DVLOG(1) << __FUNCTION__;
583 DCHECK(!video_decoder_job_->is_decoding());
584 DCHECK(!VideoFinished());
586 if (video_decoder_job_->Decode(
588 start_presentation_timestamp_,
589 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_,
591 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreVideo",
592 video_decoder_job_.get());
596 // If the decoder is waiting for iframe, trigger a browser seek.
597 if (!video_decoder_job_->next_video_data_is_iframe()) {
598 BrowserSeekToCurrentTime();
602 is_waiting_for_video_decoder_ = true;
603 if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
604 SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
607 void MediaSourcePlayer::PlaybackCompleted(bool is_audio) {
608 DVLOG(1) << __FUNCTION__ << "(" << is_audio << ")";
610 if (AudioFinished() && VideoFinished()) {
612 start_time_ticks_ = base::TimeTicks();
613 manager()->OnPlaybackComplete(player_id());
617 void MediaSourcePlayer::ClearDecodingData() {
618 DVLOG(1) << __FUNCTION__;
619 audio_decoder_job_->Flush();
620 video_decoder_job_->Flush();
621 start_time_ticks_ = base::TimeTicks();
624 bool MediaSourcePlayer::HasVideo() const {
625 return video_decoder_job_->HasStream();
628 bool MediaSourcePlayer::HasAudio() const {
629 return audio_decoder_job_->HasStream();
632 bool MediaSourcePlayer::AudioFinished() {
633 return audio_decoder_job_->OutputEOSReached() || !HasAudio();
636 bool MediaSourcePlayer::VideoFinished() {
637 return video_decoder_job_->OutputEOSReached() || !HasVideo();
640 void MediaSourcePlayer::OnDecoderStarved() {
641 DVLOG(1) << __FUNCTION__;
642 SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
643 ProcessPendingEvents();
646 void MediaSourcePlayer::StartStarvationCallback(
647 base::TimeDelta current_presentation_timestamp,
648 base::TimeDelta max_presentation_timestamp) {
649 // 20ms was chosen because it is the typical size of a compressed audio frame.
650 // Anything smaller than this would likely cause unnecessary cycling in and
651 // out of the prefetch state.
652 const base::TimeDelta kMinStarvationTimeout =
653 base::TimeDelta::FromMilliseconds(20);
655 base::TimeDelta current_timestamp = GetCurrentTime();
656 base::TimeDelta timeout;
658 timeout = max_presentation_timestamp - current_timestamp;
660 DCHECK(current_timestamp <= current_presentation_timestamp);
662 // For video only streams, fps can be estimated from the difference
663 // between the previous and current presentation timestamps. The
664 // previous presentation timestamp is equal to current_timestamp.
665 // TODO(qinmin): determine whether 2 is a good coefficient for estimating
666 // video frame timeout.
667 timeout = 2 * (current_presentation_timestamp - current_timestamp);
670 timeout = std::max(timeout, kMinStarvationTimeout);
672 decoder_starvation_callback_.Reset(
673 base::Bind(&MediaSourcePlayer::OnDecoderStarved, weak_this_));
674 base::MessageLoop::current()->PostDelayedTask(
675 FROM_HERE, decoder_starvation_callback_.callback(), timeout);
678 bool MediaSourcePlayer::IsProtectedSurfaceRequired() {
679 return video_decoder_job_->is_content_encrypted() &&
680 drm_bridge_ && drm_bridge_->IsProtectedSurfaceRequired();
683 void MediaSourcePlayer::OnPrefetchDone() {
684 DVLOG(1) << __FUNCTION__;
685 DCHECK(!audio_decoder_job_->is_decoding());
686 DCHECK(!video_decoder_job_->is_decoding());
688 // A previously posted OnPrefetchDone() could race against a Release(). If
689 // Release() won the race, we should no longer have decoder jobs.
690 // TODO(qinmin/wolenetz): Maintain channel state to not double-request data
691 // or drop data received across Release()+Start(). See http://crbug.com/306314
692 // and http://crbug.com/304234.
693 if (!IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
694 DVLOG(1) << __FUNCTION__ << " : aborting";
698 ClearPendingEvent(PREFETCH_DONE_EVENT_PENDING);
700 if (pending_event_ != NO_EVENT_PENDING) {
701 ProcessPendingEvents();
708 start_time_ticks_ = base::TimeTicks::Now();
709 start_presentation_timestamp_ = GetCurrentTime();
710 if (!interpolator_.interpolating())
711 interpolator_.StartInterpolating();
713 if (!AudioFinished())
716 if (!VideoFinished())
720 void MediaSourcePlayer::OnDemuxerConfigsChanged() {
721 manager()->OnMediaMetadataChanged(
722 player_id(), duration_, GetVideoWidth(), GetVideoHeight(), true);
725 const char* MediaSourcePlayer::GetEventName(PendingEventFlags event) {
726 // Please keep this in sync with PendingEventFlags.
727 static const char* kPendingEventNames[] = {
735 for (size_t i = 0; i < arraysize(kPendingEventNames); ++i, mask <<= 1) {
737 return kPendingEventNames[i];
743 bool MediaSourcePlayer::IsEventPending(PendingEventFlags event) const {
744 return pending_event_ & event;
747 void MediaSourcePlayer::SetPendingEvent(PendingEventFlags event) {
748 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
749 DCHECK_NE(event, NO_EVENT_PENDING);
750 DCHECK(!IsEventPending(event));
752 pending_event_ |= event;
755 void MediaSourcePlayer::ClearPendingEvent(PendingEventFlags event) {
756 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
757 DCHECK_NE(event, NO_EVENT_PENDING);
758 DCHECK(IsEventPending(event)) << GetEventName(event);
760 pending_event_ &= ~event;
763 void MediaSourcePlayer::RetryDecoderCreation(bool audio, bool video) {
765 is_waiting_for_audio_decoder_ = false;
767 is_waiting_for_video_decoder_ = false;
768 if (IsEventPending(DECODER_CREATION_EVENT_PENDING))
769 ProcessPendingEvents();
772 void MediaSourcePlayer::OnKeyAdded() {
773 DVLOG(1) << __FUNCTION__;
775 if (is_waiting_for_key_) {
776 ResumePlaybackAfterKeyAdded();
780 if ((audio_decoder_job_->is_content_encrypted() &&
781 audio_decoder_job_->is_decoding()) ||
782 (video_decoder_job_->is_content_encrypted() &&
783 video_decoder_job_->is_decoding())) {
784 DVLOG(1) << __FUNCTION__ << ": " << "Key added during pending decode.";
785 key_added_while_decode_pending_ = true;
789 void MediaSourcePlayer::ResumePlaybackAfterKeyAdded() {
790 DVLOG(1) << __FUNCTION__;
791 DCHECK(is_waiting_for_key_ || key_added_while_decode_pending_);
793 is_waiting_for_key_ = false;
794 key_added_while_decode_pending_ = false;
796 // StartInternal() will trigger a prefetch, where in most cases we'll just
797 // use previously received data.
802 void MediaSourcePlayer::OnCdmUnset() {
803 DVLOG(1) << __FUNCTION__;
805 // TODO(xhwang): Currently this is only called during teardown. Support full
806 // detachment of CDM during playback. This will be needed when we start to
807 // support setMediaKeys(0) (see http://crbug.com/330324), or when we release
808 // MediaDrm when the video is paused, or when the device goes to sleep (see
809 // http://crbug.com/272421).
810 audio_decoder_job_->SetDrmBridge(NULL);
811 video_decoder_job_->SetDrmBridge(NULL);
812 cdm_registration_id_ = 0;