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"
26 MediaSourcePlayer::MediaSourcePlayer(
28 MediaPlayerManager* manager,
29 const RequestMediaResourcesCB& request_media_resources_cb,
30 const ReleaseMediaResourcesCB& release_media_resources_cb,
31 scoped_ptr<DemuxerAndroid> demuxer,
32 const GURL& frame_url)
33 : MediaPlayerAndroid(player_id,
35 request_media_resources_cb,
36 release_media_resources_cb,
38 demuxer_(demuxer.Pass()),
39 pending_event_(NO_EVENT_PENDING),
41 interpolator_(&default_tick_clock_),
42 doing_browser_seek_(false),
45 cdm_registration_id_(0),
46 is_waiting_for_key_(false),
47 is_waiting_for_audio_decoder_(false),
48 is_waiting_for_video_decoder_(false),
50 audio_decoder_job_.reset(new AudioDecoderJob(
51 base::Bind(&DemuxerAndroid::RequestDemuxerData,
52 base::Unretained(demuxer_.get()),
53 DemuxerStream::AUDIO),
54 base::Bind(&MediaSourcePlayer::OnDemuxerConfigsChanged,
55 weak_factory_.GetWeakPtr())));
56 video_decoder_job_.reset(new VideoDecoderJob(
57 base::Bind(&DemuxerAndroid::RequestDemuxerData,
58 base::Unretained(demuxer_.get()),
59 DemuxerStream::VIDEO),
60 base::Bind(request_media_resources_cb_, player_id),
61 base::Bind(release_media_resources_cb_, player_id),
62 base::Bind(&MediaSourcePlayer::OnDemuxerConfigsChanged,
63 weak_factory_.GetWeakPtr())));
64 demuxer_->Initialize(this);
65 interpolator_.SetUpperBound(base::TimeDelta());
66 weak_this_ = weak_factory_.GetWeakPtr();
69 MediaSourcePlayer::~MediaSourcePlayer() {
71 DCHECK_EQ(!drm_bridge_, !cdm_registration_id_);
73 drm_bridge_->UnregisterPlayer(cdm_registration_id_);
74 cdm_registration_id_ = 0;
78 void MediaSourcePlayer::SetVideoSurface(gfx::ScopedJavaSurface surface) {
79 DVLOG(1) << __FUNCTION__;
80 if (!video_decoder_job_->SetVideoSurface(surface.Pass()))
82 // Retry video decoder creation.
83 RetryDecoderCreation(false, true);
86 void MediaSourcePlayer::ScheduleSeekEventAndStopDecoding(
87 base::TimeDelta seek_time) {
88 DVLOG(1) << __FUNCTION__ << "(" << seek_time.InSecondsF() << ")";
89 DCHECK(!IsEventPending(SEEK_EVENT_PENDING));
91 pending_seek_ = false;
93 interpolator_.SetBounds(seek_time, seek_time);
95 if (audio_decoder_job_->is_decoding())
96 audio_decoder_job_->StopDecode();
97 if (video_decoder_job_->is_decoding())
98 video_decoder_job_->StopDecode();
100 SetPendingEvent(SEEK_EVENT_PENDING);
101 ProcessPendingEvents();
104 void MediaSourcePlayer::BrowserSeekToCurrentTime() {
105 DVLOG(1) << __FUNCTION__;
107 DCHECK(!IsEventPending(SEEK_EVENT_PENDING));
108 doing_browser_seek_ = true;
109 ScheduleSeekEventAndStopDecoding(GetCurrentTime());
112 bool MediaSourcePlayer::Seekable() {
113 // If the duration TimeDelta, converted to milliseconds from microseconds,
114 // is >= 2^31, then the media is assumed to be unbounded and unseekable.
115 // 2^31 is the bound due to java player using 32-bit integer for time
116 // values at millisecond resolution.
118 base::TimeDelta::FromMilliseconds(std::numeric_limits<int32>::max());
121 void MediaSourcePlayer::Start() {
122 DVLOG(1) << __FUNCTION__;
126 bool request_fullscreen = IsProtectedSurfaceRequired();
127 #if defined(VIDEO_HOLE)
128 // Skip to request fullscreen when hole-punching is used.
129 request_fullscreen = request_fullscreen &&
130 !manager()->ShouldUseVideoOverlayForEmbeddedEncryptedVideo();
131 #endif // defined(VIDEO_HOLE)
132 if (request_fullscreen)
133 manager()->RequestFullScreen(player_id());
138 void MediaSourcePlayer::Pause(bool is_media_related_action) {
139 DVLOG(1) << __FUNCTION__;
141 // Since decoder jobs have their own thread, decoding is not fully paused
142 // until all the decoder jobs call MediaDecoderCallback(). It is possible
143 // that Start() is called while the player is waiting for
144 // MediaDecoderCallback(). In that case, decoding will continue when
145 // MediaDecoderCallback() is called.
147 start_time_ticks_ = base::TimeTicks();
150 bool MediaSourcePlayer::IsPlaying() {
154 int MediaSourcePlayer::GetVideoWidth() {
155 return video_decoder_job_->width();
158 int MediaSourcePlayer::GetVideoHeight() {
159 return video_decoder_job_->height();
162 void MediaSourcePlayer::SeekTo(base::TimeDelta timestamp) {
163 DVLOG(1) << __FUNCTION__ << "(" << timestamp.InSecondsF() << ")";
165 if (IsEventPending(SEEK_EVENT_PENDING)) {
166 DCHECK(doing_browser_seek_) << "SeekTo while SeekTo in progress";
167 DCHECK(!pending_seek_) << "SeekTo while SeekTo pending browser seek";
169 // There is a browser seek currently in progress to obtain I-frame to feed
170 // a newly constructed video decoder. Remember this real seek request so
171 // it can be initiated once OnDemuxerSeekDone() occurs for the browser seek.
172 pending_seek_ = true;
173 pending_seek_time_ = timestamp;
177 doing_browser_seek_ = false;
178 ScheduleSeekEventAndStopDecoding(timestamp);
181 base::TimeDelta MediaSourcePlayer::GetCurrentTime() {
182 return std::min(interpolator_.GetInterpolatedTime(), duration_);
185 base::TimeDelta MediaSourcePlayer::GetDuration() {
189 void MediaSourcePlayer::Release() {
190 DVLOG(1) << __FUNCTION__;
192 is_surface_in_use_ = false;
193 audio_decoder_job_->ReleaseDecoderResources();
194 video_decoder_job_->ReleaseDecoderResources();
196 // Prevent player restart, including job re-creation attempts.
199 decoder_starvation_callback_.Cancel();
202 void MediaSourcePlayer::SetVolume(double volume) {
203 audio_decoder_job_->SetVolume(volume);
206 bool MediaSourcePlayer::IsSurfaceInUse() const {
207 return is_surface_in_use_;
210 bool MediaSourcePlayer::CanPause() {
214 bool MediaSourcePlayer::CanSeekForward() {
218 bool MediaSourcePlayer::CanSeekBackward() {
222 bool MediaSourcePlayer::IsPlayerReady() {
223 return audio_decoder_job_ || video_decoder_job_;
226 void MediaSourcePlayer::StartInternal() {
227 DVLOG(1) << __FUNCTION__;
228 // If there are pending events, wait for them finish.
229 if (pending_event_ != NO_EVENT_PENDING)
232 // When we start, we'll have new demuxed data coming in. This new data could
233 // be clear (not encrypted) or encrypted with different keys. So
234 // |is_waiting_for_key_| condition may not be true anymore.
235 is_waiting_for_key_ = false;
237 SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
238 ProcessPendingEvents();
241 void MediaSourcePlayer::OnDemuxerConfigsAvailable(
242 const DemuxerConfigs& configs) {
243 DVLOG(1) << __FUNCTION__;
244 DCHECK(!HasAudio() && !HasVideo());
245 duration_ = configs.duration;
247 audio_decoder_job_->SetDemuxerConfigs(configs);
248 video_decoder_job_->SetDemuxerConfigs(configs);
249 OnDemuxerConfigsChanged();
252 void MediaSourcePlayer::OnDemuxerDataAvailable(const DemuxerData& data) {
253 DVLOG(1) << __FUNCTION__ << "(" << data.type << ")";
254 DCHECK_LT(0u, data.access_units.size());
255 CHECK_GE(1u, data.demuxer_configs.size());
257 if (data.type == DemuxerStream::AUDIO)
258 audio_decoder_job_->OnDataReceived(data);
259 else if (data.type == DemuxerStream::VIDEO)
260 video_decoder_job_->OnDataReceived(data);
263 void MediaSourcePlayer::OnDemuxerDurationChanged(base::TimeDelta duration) {
264 duration_ = duration;
267 void MediaSourcePlayer::OnMediaCryptoReady() {
268 DCHECK(!drm_bridge_->GetMediaCrypto().is_null());
269 drm_bridge_->SetMediaCryptoReadyCB(base::Closure());
271 // Retry decoder creation if the decoders are waiting for MediaCrypto.
272 RetryDecoderCreation(true, true);
275 void MediaSourcePlayer::SetCdm(BrowserCdm* cdm) {
276 // Currently we don't support DRM change during the middle of playback, even
277 // if the player is paused.
278 // TODO(qinmin): support DRM change after playback has started.
279 // http://crbug.com/253792.
280 if (GetCurrentTime() > base::TimeDelta()) {
281 VLOG(0) << "Setting DRM bridge after playback has started. "
282 << "This is not well supported!";
286 NOTREACHED() << "Currently we do not support resetting CDM.";
290 // Only MediaDrmBridge will be set on MediaSourcePlayer.
291 drm_bridge_ = static_cast<MediaDrmBridge*>(cdm);
293 cdm_registration_id_ = drm_bridge_->RegisterPlayer(
294 base::Bind(&MediaSourcePlayer::OnKeyAdded, weak_this_),
295 base::Bind(&MediaSourcePlayer::OnCdmUnset, weak_this_));
297 audio_decoder_job_->SetDrmBridge(drm_bridge_);
298 video_decoder_job_->SetDrmBridge(drm_bridge_);
300 if (drm_bridge_->GetMediaCrypto().is_null()) {
301 drm_bridge_->SetMediaCryptoReadyCB(
302 base::Bind(&MediaSourcePlayer::OnMediaCryptoReady, weak_this_));
306 // If the player is previously waiting for CDM, retry decoder creation.
307 RetryDecoderCreation(true, true);
310 void MediaSourcePlayer::OnDemuxerSeekDone(
311 base::TimeDelta actual_browser_seek_time) {
312 DVLOG(1) << __FUNCTION__;
314 ClearPendingEvent(SEEK_EVENT_PENDING);
315 if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING))
316 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
319 DVLOG(1) << __FUNCTION__ << "processing pending seek";
320 DCHECK(doing_browser_seek_);
321 pending_seek_ = false;
322 SeekTo(pending_seek_time_);
326 // It is possible that a browser seek to I-frame had to seek to a buffered
327 // I-frame later than the requested one due to data removal or GC. Update
328 // player clock to the actual seek target.
329 if (doing_browser_seek_) {
330 DCHECK(actual_browser_seek_time != kNoTimestamp());
331 base::TimeDelta seek_time = actual_browser_seek_time;
332 // A browser seek must not jump into the past. Ideally, it seeks to the
333 // requested time, but it might jump into the future.
334 DCHECK(seek_time >= GetCurrentTime());
335 DVLOG(1) << __FUNCTION__ << " : setting clock to actual browser seek time: "
336 << seek_time.InSecondsF();
337 interpolator_.SetBounds(seek_time, seek_time);
338 audio_decoder_job_->SetBaseTimestamp(seek_time);
340 DCHECK(actual_browser_seek_time == kNoTimestamp());
343 base::TimeDelta current_time = GetCurrentTime();
344 // TODO(qinmin): Simplify the logic by using |start_presentation_timestamp_|
345 // to preroll media decoder jobs. Currently |start_presentation_timestamp_|
346 // is calculated from decoder output, while preroll relies on the access
347 // unit's timestamp. There are some differences between the two.
348 preroll_timestamp_ = current_time;
350 audio_decoder_job_->BeginPrerolling(preroll_timestamp_);
352 video_decoder_job_->BeginPrerolling(preroll_timestamp_);
354 if (!doing_browser_seek_)
355 manager()->OnSeekComplete(player_id(), current_time);
357 ProcessPendingEvents();
360 void MediaSourcePlayer::UpdateTimestamps(
361 base::TimeDelta current_presentation_timestamp,
362 base::TimeDelta max_presentation_timestamp) {
363 interpolator_.SetBounds(current_presentation_timestamp,
364 max_presentation_timestamp);
365 manager()->OnTimeUpdate(player_id(), GetCurrentTime());
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.";
376 if (audio_decoder_job_->is_decoding()) {
377 DVLOG(1) << __FUNCTION__ << " : An audio job is still decoding.";
381 if (IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
382 DVLOG(1) << __FUNCTION__ << " : PREFETCH_DONE still pending.";
386 if (IsEventPending(SEEK_EVENT_PENDING)) {
387 DVLOG(1) << __FUNCTION__ << " : Handling SEEK_EVENT";
389 audio_decoder_job_->SetBaseTimestamp(GetCurrentTime());
390 demuxer_->RequestDemuxerSeek(GetCurrentTime(), doing_browser_seek_);
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_)
398 ClearPendingEvent(DECODER_CREATION_EVENT_PENDING);
401 if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING)) {
402 DVLOG(1) << __FUNCTION__ << " : Handling PREFETCH_REQUEST_EVENT.";
403 int count = (AudioFinished() ? 0 : 1) + (VideoFinished() ? 0 : 1);
405 // It is possible that all streams have finished decode, yet starvation
406 // occurred during the last stream's EOS decode. In this case, prefetch is a
408 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
412 SetPendingEvent(PREFETCH_DONE_EVENT_PENDING);
413 base::Closure barrier = BarrierClosure(
414 count, base::Bind(&MediaSourcePlayer::OnPrefetchDone, weak_this_));
416 if (!AudioFinished())
417 audio_decoder_job_->Prefetch(barrier);
419 if (!VideoFinished())
420 video_decoder_job_->Prefetch(barrier);
425 DCHECK_EQ(pending_event_, NO_EVENT_PENDING);
427 // Now that all pending events have been handled, resume decoding if we are
433 void MediaSourcePlayer::MediaDecoderCallback(
434 bool is_audio, MediaCodecStatus status,
435 base::TimeDelta current_presentation_timestamp,
436 base::TimeDelta max_presentation_timestamp) {
437 DVLOG(1) << __FUNCTION__ << ": " << is_audio << ", " << status;
439 // TODO(xhwang): Drop IntToString() when http://crbug.com/303899 is fixed.
441 TRACE_EVENT_ASYNC_END1("media",
442 "MediaSourcePlayer::DecodeMoreAudio",
443 audio_decoder_job_.get(),
445 base::IntToString(status));
447 TRACE_EVENT_ASYNC_END1("media",
448 "MediaSourcePlayer::DecodeMoreVideo",
449 video_decoder_job_.get(),
451 base::IntToString(status));
454 // Let tests hook the completion of this decode cycle.
455 if (!decode_callback_for_testing_.is_null())
456 base::ResetAndReturn(&decode_callback_for_testing_).Run();
458 bool is_clock_manager = is_audio || !HasAudio();
460 if (is_clock_manager)
461 decoder_starvation_callback_.Cancel();
463 if (status == MEDIA_CODEC_ERROR) {
464 DVLOG(1) << __FUNCTION__ << " : decode error";
466 manager()->OnError(player_id(), MEDIA_ERROR_DECODE);
470 DCHECK(!IsEventPending(PREFETCH_DONE_EVENT_PENDING));
472 // Let |SEEK_EVENT_PENDING| (the highest priority event outside of
473 // |PREFETCH_DONE_EVENT_PENDING|) preempt output EOS detection here. Process
474 // any other pending events only after handling EOS detection.
475 if (IsEventPending(SEEK_EVENT_PENDING)) {
476 ProcessPendingEvents();
480 if ((status == MEDIA_CODEC_OK || status == MEDIA_CODEC_INPUT_END_OF_STREAM) &&
481 is_clock_manager && current_presentation_timestamp != kNoTimestamp()) {
482 UpdateTimestamps(current_presentation_timestamp,
483 max_presentation_timestamp);
486 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM)
487 PlaybackCompleted(is_audio);
489 if (pending_event_ != NO_EVENT_PENDING) {
490 ProcessPendingEvents();
494 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM)
498 if (is_clock_manager)
499 interpolator_.StopInterpolating();
503 if (status == MEDIA_CODEC_NO_KEY) {
504 is_waiting_for_key_ = true;
508 // If the status is MEDIA_CODEC_STOPPED, stop decoding new data. The player is
509 // in the middle of a seek or stop event and needs to wait for the IPCs to
511 if (status == MEDIA_CODEC_STOPPED)
514 if (is_clock_manager) {
515 // If we have a valid timestamp, start the starvation callback. Otherwise,
516 // reset the |start_time_ticks_| so that the next frame will not suffer
517 // from the decoding delay caused by the current frame.
518 if (current_presentation_timestamp != kNoTimestamp())
519 StartStarvationCallback(current_presentation_timestamp,
520 max_presentation_timestamp);
522 start_time_ticks_ = base::TimeTicks::Now();
531 void MediaSourcePlayer::DecodeMoreAudio() {
532 DVLOG(1) << __FUNCTION__;
533 DCHECK(!audio_decoder_job_->is_decoding());
534 DCHECK(!AudioFinished());
536 if (audio_decoder_job_->Decode(
538 start_presentation_timestamp_,
539 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_, true))) {
540 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreAudio",
541 audio_decoder_job_.get());
545 is_waiting_for_audio_decoder_ = true;
546 if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
547 SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
550 void MediaSourcePlayer::DecodeMoreVideo() {
551 DVLOG(1) << __FUNCTION__;
552 DCHECK(!video_decoder_job_->is_decoding());
553 DCHECK(!VideoFinished());
555 if (video_decoder_job_->Decode(
557 start_presentation_timestamp_,
558 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_,
560 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreVideo",
561 video_decoder_job_.get());
565 // If the decoder is waiting for iframe, trigger a browser seek.
566 if (!video_decoder_job_->next_video_data_is_iframe()) {
567 BrowserSeekToCurrentTime();
571 is_waiting_for_video_decoder_ = true;
572 if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
573 SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
576 void MediaSourcePlayer::PlaybackCompleted(bool is_audio) {
577 DVLOG(1) << __FUNCTION__ << "(" << is_audio << ")";
579 if (AudioFinished() && VideoFinished()) {
581 interpolator_.StopInterpolating();
582 start_time_ticks_ = base::TimeTicks();
583 manager()->OnPlaybackComplete(player_id());
587 void MediaSourcePlayer::ClearDecodingData() {
588 DVLOG(1) << __FUNCTION__;
589 audio_decoder_job_->Flush();
590 video_decoder_job_->Flush();
591 start_time_ticks_ = base::TimeTicks();
594 bool MediaSourcePlayer::HasVideo() {
595 return video_decoder_job_->HasStream();
598 bool MediaSourcePlayer::HasAudio() {
599 return audio_decoder_job_->HasStream();
602 bool MediaSourcePlayer::AudioFinished() {
603 return audio_decoder_job_->OutputEOSReached() || !HasAudio();
606 bool MediaSourcePlayer::VideoFinished() {
607 return video_decoder_job_->OutputEOSReached() || !HasVideo();
610 void MediaSourcePlayer::OnDecoderStarved() {
611 DVLOG(1) << __FUNCTION__;
612 SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
613 ProcessPendingEvents();
616 void MediaSourcePlayer::StartStarvationCallback(
617 base::TimeDelta current_presentation_timestamp,
618 base::TimeDelta max_presentation_timestamp) {
619 // 20ms was chosen because it is the typical size of a compressed audio frame.
620 // Anything smaller than this would likely cause unnecessary cycling in and
621 // out of the prefetch state.
622 const base::TimeDelta kMinStarvationTimeout =
623 base::TimeDelta::FromMilliseconds(20);
625 base::TimeDelta current_timestamp = GetCurrentTime();
626 base::TimeDelta timeout;
628 timeout = max_presentation_timestamp - current_timestamp;
630 DCHECK(current_timestamp <= current_presentation_timestamp);
632 // For video only streams, fps can be estimated from the difference
633 // between the previous and current presentation timestamps. The
634 // previous presentation timestamp is equal to current_timestamp.
635 // TODO(qinmin): determine whether 2 is a good coefficient for estimating
636 // video frame timeout.
637 timeout = 2 * (current_presentation_timestamp - current_timestamp);
640 timeout = std::max(timeout, kMinStarvationTimeout);
642 decoder_starvation_callback_.Reset(
643 base::Bind(&MediaSourcePlayer::OnDecoderStarved, weak_this_));
644 base::MessageLoop::current()->PostDelayedTask(
645 FROM_HERE, decoder_starvation_callback_.callback(), timeout);
648 bool MediaSourcePlayer::IsProtectedSurfaceRequired() {
649 return video_decoder_job_->is_content_encrypted() &&
650 drm_bridge_ && drm_bridge_->IsProtectedSurfaceRequired();
653 void MediaSourcePlayer::OnPrefetchDone() {
654 DVLOG(1) << __FUNCTION__;
655 DCHECK(!audio_decoder_job_->is_decoding());
656 DCHECK(!video_decoder_job_->is_decoding());
658 // A previously posted OnPrefetchDone() could race against a Release(). If
659 // Release() won the race, we should no longer have decoder jobs.
660 // TODO(qinmin/wolenetz): Maintain channel state to not double-request data
661 // or drop data received across Release()+Start(). See http://crbug.com/306314
662 // and http://crbug.com/304234.
663 if (!IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
664 DVLOG(1) << __FUNCTION__ << " : aborting";
668 ClearPendingEvent(PREFETCH_DONE_EVENT_PENDING);
670 if (pending_event_ != NO_EVENT_PENDING) {
671 ProcessPendingEvents();
678 start_time_ticks_ = base::TimeTicks::Now();
679 start_presentation_timestamp_ = GetCurrentTime();
680 if (!interpolator_.interpolating())
681 interpolator_.StartInterpolating();
683 if (!AudioFinished())
686 if (!VideoFinished())
690 void MediaSourcePlayer::OnDemuxerConfigsChanged() {
691 manager()->OnMediaMetadataChanged(
692 player_id(), duration_, GetVideoWidth(), GetVideoHeight(), true);
695 const char* MediaSourcePlayer::GetEventName(PendingEventFlags event) {
696 // Please keep this in sync with PendingEventFlags.
697 static const char* kPendingEventNames[] = {
705 for (size_t i = 0; i < arraysize(kPendingEventNames); ++i, mask <<= 1) {
707 return kPendingEventNames[i];
713 bool MediaSourcePlayer::IsEventPending(PendingEventFlags event) const {
714 return pending_event_ & event;
717 void MediaSourcePlayer::SetPendingEvent(PendingEventFlags event) {
718 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
719 DCHECK_NE(event, NO_EVENT_PENDING);
720 DCHECK(!IsEventPending(event));
722 pending_event_ |= event;
725 void MediaSourcePlayer::ClearPendingEvent(PendingEventFlags event) {
726 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
727 DCHECK_NE(event, NO_EVENT_PENDING);
728 DCHECK(IsEventPending(event)) << GetEventName(event);
730 pending_event_ &= ~event;
733 void MediaSourcePlayer::RetryDecoderCreation(bool audio, bool video) {
735 is_waiting_for_audio_decoder_ = false;
737 is_waiting_for_video_decoder_ = false;
738 if (IsEventPending(DECODER_CREATION_EVENT_PENDING))
739 ProcessPendingEvents();
742 void MediaSourcePlayer::OnKeyAdded() {
743 DVLOG(1) << __FUNCTION__;
744 if (!is_waiting_for_key_)
747 is_waiting_for_key_ = false;
752 void MediaSourcePlayer::OnCdmUnset() {
753 DVLOG(1) << __FUNCTION__;
754 // TODO(xhwang): Support detachment of CDM. This will be needed when we start
755 // to support setMediaKeys(0) (see http://crbug.com/330324), or when we
756 // release MediaDrm when the video is paused, or when the device goes to
757 // sleep (see http://crbug.com/272421).
758 NOTREACHED() << "CDM detachment not supported.";
760 audio_decoder_job_->SetDrmBridge(NULL);
761 video_decoder_job_->SetDrmBridge(NULL);