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 clock_(&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 clock_.SetMaxTime(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 clock_.SetTime(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 clock_.Elapsed();
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;
246 clock_.SetDuration(duration_);
248 audio_decoder_job_->SetDemuxerConfigs(configs);
249 video_decoder_job_->SetDemuxerConfigs(configs);
250 OnDemuxerConfigsChanged();
253 void MediaSourcePlayer::OnDemuxerDataAvailable(const DemuxerData& data) {
254 DVLOG(1) << __FUNCTION__ << "(" << data.type << ")";
255 DCHECK_LT(0u, data.access_units.size());
256 CHECK_GE(1u, data.demuxer_configs.size());
258 if (data.type == DemuxerStream::AUDIO)
259 audio_decoder_job_->OnDataReceived(data);
260 else if (data.type == DemuxerStream::VIDEO)
261 video_decoder_job_->OnDataReceived(data);
264 void MediaSourcePlayer::OnDemuxerDurationChanged(base::TimeDelta duration) {
265 duration_ = duration;
266 clock_.SetDuration(duration_);
269 void MediaSourcePlayer::OnMediaCryptoReady() {
270 DCHECK(!drm_bridge_->GetMediaCrypto().is_null());
271 drm_bridge_->SetMediaCryptoReadyCB(base::Closure());
273 // Retry decoder creation if the decoders are waiting for MediaCrypto.
274 RetryDecoderCreation(true, true);
277 void MediaSourcePlayer::SetCdm(BrowserCdm* cdm) {
278 // Currently we don't support DRM change during the middle of playback, even
279 // if the player is paused.
280 // TODO(qinmin): support DRM change after playback has started.
281 // http://crbug.com/253792.
282 if (GetCurrentTime() > base::TimeDelta()) {
283 VLOG(0) << "Setting DRM bridge after playback has started. "
284 << "This is not well supported!";
288 NOTREACHED() << "Currently we do not support resetting CDM.";
292 // Only MediaDrmBridge will be set on MediaSourcePlayer.
293 drm_bridge_ = static_cast<MediaDrmBridge*>(cdm);
295 cdm_registration_id_ = drm_bridge_->RegisterPlayer(
296 base::Bind(&MediaSourcePlayer::OnKeyAdded, weak_this_),
297 base::Bind(&MediaSourcePlayer::OnCdmUnset, weak_this_));
299 audio_decoder_job_->SetDrmBridge(drm_bridge_);
300 video_decoder_job_->SetDrmBridge(drm_bridge_);
302 if (drm_bridge_->GetMediaCrypto().is_null()) {
303 drm_bridge_->SetMediaCryptoReadyCB(
304 base::Bind(&MediaSourcePlayer::OnMediaCryptoReady, weak_this_));
308 // If the player is previously waiting for CDM, retry decoder creation.
309 RetryDecoderCreation(true, true);
312 void MediaSourcePlayer::OnDemuxerSeekDone(
313 base::TimeDelta actual_browser_seek_time) {
314 DVLOG(1) << __FUNCTION__;
316 ClearPendingEvent(SEEK_EVENT_PENDING);
317 if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING))
318 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
321 DVLOG(1) << __FUNCTION__ << "processing pending seek";
322 DCHECK(doing_browser_seek_);
323 pending_seek_ = false;
324 SeekTo(pending_seek_time_);
328 // It is possible that a browser seek to I-frame had to seek to a buffered
329 // I-frame later than the requested one due to data removal or GC. Update
330 // player clock to the actual seek target.
331 if (doing_browser_seek_) {
332 DCHECK(actual_browser_seek_time != kNoTimestamp());
333 base::TimeDelta seek_time = actual_browser_seek_time;
334 // A browser seek must not jump into the past. Ideally, it seeks to the
335 // requested time, but it might jump into the future.
336 DCHECK(seek_time >= GetCurrentTime());
337 DVLOG(1) << __FUNCTION__ << " : setting clock to actual browser seek time: "
338 << seek_time.InSecondsF();
339 clock_.SetTime(seek_time, seek_time);
340 audio_decoder_job_->SetBaseTimestamp(seek_time);
342 DCHECK(actual_browser_seek_time == kNoTimestamp());
345 base::TimeDelta current_time = GetCurrentTime();
346 // TODO(qinmin): Simplify the logic by using |start_presentation_timestamp_|
347 // to preroll media decoder jobs. Currently |start_presentation_timestamp_|
348 // is calculated from decoder output, while preroll relies on the access
349 // unit's timestamp. There are some differences between the two.
350 preroll_timestamp_ = current_time;
352 audio_decoder_job_->BeginPrerolling(preroll_timestamp_);
354 video_decoder_job_->BeginPrerolling(preroll_timestamp_);
356 if (!doing_browser_seek_)
357 manager()->OnSeekComplete(player_id(), current_time);
359 ProcessPendingEvents();
362 void MediaSourcePlayer::UpdateTimestamps(
363 base::TimeDelta current_presentation_timestamp,
364 base::TimeDelta max_presentation_timestamp) {
365 clock_.SetTime(current_presentation_timestamp, max_presentation_timestamp);
366 manager()->OnTimeUpdate(player_id(), GetCurrentTime());
369 void MediaSourcePlayer::ProcessPendingEvents() {
370 DVLOG(1) << __FUNCTION__ << " : 0x" << std::hex << pending_event_;
371 // Wait for all the decoding jobs to finish before processing pending tasks.
372 if (video_decoder_job_->is_decoding()) {
373 DVLOG(1) << __FUNCTION__ << " : A video job is still decoding.";
377 if (audio_decoder_job_->is_decoding()) {
378 DVLOG(1) << __FUNCTION__ << " : An audio job is still decoding.";
382 if (IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
383 DVLOG(1) << __FUNCTION__ << " : PREFETCH_DONE still pending.";
387 if (IsEventPending(SEEK_EVENT_PENDING)) {
388 DVLOG(1) << __FUNCTION__ << " : Handling SEEK_EVENT";
390 audio_decoder_job_->SetBaseTimestamp(GetCurrentTime());
391 demuxer_->RequestDemuxerSeek(GetCurrentTime(), doing_browser_seek_);
395 if (IsEventPending(DECODER_CREATION_EVENT_PENDING)) {
396 // Don't continue if one of the decoder is not created.
397 if (is_waiting_for_audio_decoder_ || is_waiting_for_video_decoder_)
399 ClearPendingEvent(DECODER_CREATION_EVENT_PENDING);
402 if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING)) {
403 DVLOG(1) << __FUNCTION__ << " : Handling PREFETCH_REQUEST_EVENT.";
404 int count = (AudioFinished() ? 0 : 1) + (VideoFinished() ? 0 : 1);
406 // It is possible that all streams have finished decode, yet starvation
407 // occurred during the last stream's EOS decode. In this case, prefetch is a
409 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
413 SetPendingEvent(PREFETCH_DONE_EVENT_PENDING);
414 base::Closure barrier = BarrierClosure(
415 count, base::Bind(&MediaSourcePlayer::OnPrefetchDone, weak_this_));
417 if (!AudioFinished())
418 audio_decoder_job_->Prefetch(barrier);
420 if (!VideoFinished())
421 video_decoder_job_->Prefetch(barrier);
426 DCHECK_EQ(pending_event_, NO_EVENT_PENDING);
428 // Now that all pending events have been handled, resume decoding if we are
434 void MediaSourcePlayer::MediaDecoderCallback(
435 bool is_audio, MediaCodecStatus status,
436 base::TimeDelta current_presentation_timestamp,
437 base::TimeDelta max_presentation_timestamp) {
438 DVLOG(1) << __FUNCTION__ << ": " << is_audio << ", " << status;
440 // TODO(xhwang): Drop IntToString() when http://crbug.com/303899 is fixed.
442 TRACE_EVENT_ASYNC_END1("media",
443 "MediaSourcePlayer::DecodeMoreAudio",
444 audio_decoder_job_.get(),
446 base::IntToString(status));
448 TRACE_EVENT_ASYNC_END1("media",
449 "MediaSourcePlayer::DecodeMoreVideo",
450 video_decoder_job_.get(),
452 base::IntToString(status));
455 // Let tests hook the completion of this decode cycle.
456 if (!decode_callback_for_testing_.is_null())
457 base::ResetAndReturn(&decode_callback_for_testing_).Run();
459 bool is_clock_manager = is_audio || !HasAudio();
461 if (is_clock_manager)
462 decoder_starvation_callback_.Cancel();
464 if (status == MEDIA_CODEC_ERROR) {
465 DVLOG(1) << __FUNCTION__ << " : decode error";
467 manager()->OnError(player_id(), MEDIA_ERROR_DECODE);
471 DCHECK(!IsEventPending(PREFETCH_DONE_EVENT_PENDING));
473 // Let |SEEK_EVENT_PENDING| (the highest priority event outside of
474 // |PREFETCH_DONE_EVENT_PENDING|) preempt output EOS detection here. Process
475 // any other pending events only after handling EOS detection.
476 if (IsEventPending(SEEK_EVENT_PENDING)) {
477 ProcessPendingEvents();
481 if ((status == MEDIA_CODEC_OK || status == MEDIA_CODEC_INPUT_END_OF_STREAM) &&
482 is_clock_manager && current_presentation_timestamp != kNoTimestamp()) {
483 UpdateTimestamps(current_presentation_timestamp,
484 max_presentation_timestamp);
487 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM)
488 PlaybackCompleted(is_audio);
490 if (pending_event_ != NO_EVENT_PENDING) {
491 ProcessPendingEvents();
495 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM)
499 if (is_clock_manager)
504 if (status == MEDIA_CODEC_NO_KEY) {
505 is_waiting_for_key_ = true;
509 // If the status is MEDIA_CODEC_STOPPED, stop decoding new data. The player is
510 // in the middle of a seek or stop event and needs to wait for the IPCs to
512 if (status == MEDIA_CODEC_STOPPED)
515 if (is_clock_manager) {
516 // If we have a valid timestamp, start the starvation callback. Otherwise,
517 // reset the |start_time_ticks_| so that the next frame will not suffer
518 // from the decoding delay caused by the current frame.
519 if (current_presentation_timestamp != kNoTimestamp())
520 StartStarvationCallback(current_presentation_timestamp,
521 max_presentation_timestamp);
523 start_time_ticks_ = base::TimeTicks::Now();
532 void MediaSourcePlayer::DecodeMoreAudio() {
533 DVLOG(1) << __FUNCTION__;
534 DCHECK(!audio_decoder_job_->is_decoding());
535 DCHECK(!AudioFinished());
537 if (audio_decoder_job_->Decode(
539 start_presentation_timestamp_,
540 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_, true))) {
541 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreAudio",
542 audio_decoder_job_.get());
546 is_waiting_for_audio_decoder_ = true;
547 if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
548 SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
551 void MediaSourcePlayer::DecodeMoreVideo() {
552 DVLOG(1) << __FUNCTION__;
553 DCHECK(!video_decoder_job_->is_decoding());
554 DCHECK(!VideoFinished());
556 if (video_decoder_job_->Decode(
558 start_presentation_timestamp_,
559 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_,
561 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreVideo",
562 video_decoder_job_.get());
566 // If the decoder is waiting for iframe, trigger a browser seek.
567 if (!video_decoder_job_->next_video_data_is_iframe()) {
568 BrowserSeekToCurrentTime();
572 is_waiting_for_video_decoder_ = true;
573 if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
574 SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
577 void MediaSourcePlayer::PlaybackCompleted(bool is_audio) {
578 DVLOG(1) << __FUNCTION__ << "(" << is_audio << ")";
580 if (AudioFinished() && VideoFinished()) {
583 start_time_ticks_ = base::TimeTicks();
584 manager()->OnPlaybackComplete(player_id());
588 void MediaSourcePlayer::ClearDecodingData() {
589 DVLOG(1) << __FUNCTION__;
590 audio_decoder_job_->Flush();
591 video_decoder_job_->Flush();
592 start_time_ticks_ = base::TimeTicks();
595 bool MediaSourcePlayer::HasVideo() {
596 return video_decoder_job_->HasStream();
599 bool MediaSourcePlayer::HasAudio() {
600 return audio_decoder_job_->HasStream();
603 bool MediaSourcePlayer::AudioFinished() {
604 return audio_decoder_job_->OutputEOSReached() || !HasAudio();
607 bool MediaSourcePlayer::VideoFinished() {
608 return video_decoder_job_->OutputEOSReached() || !HasVideo();
611 void MediaSourcePlayer::OnDecoderStarved() {
612 DVLOG(1) << __FUNCTION__;
613 SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
614 ProcessPendingEvents();
617 void MediaSourcePlayer::StartStarvationCallback(
618 base::TimeDelta current_presentation_timestamp,
619 base::TimeDelta max_presentation_timestamp) {
620 // 20ms was chosen because it is the typical size of a compressed audio frame.
621 // Anything smaller than this would likely cause unnecessary cycling in and
622 // out of the prefetch state.
623 const base::TimeDelta kMinStarvationTimeout =
624 base::TimeDelta::FromMilliseconds(20);
626 base::TimeDelta current_timestamp = GetCurrentTime();
627 base::TimeDelta timeout;
629 timeout = max_presentation_timestamp - current_timestamp;
631 DCHECK(current_timestamp <= current_presentation_timestamp);
633 // For video only streams, fps can be estimated from the difference
634 // between the previous and current presentation timestamps. The
635 // previous presentation timestamp is equal to current_timestamp.
636 // TODO(qinmin): determine whether 2 is a good coefficient for estimating
637 // video frame timeout.
638 timeout = 2 * (current_presentation_timestamp - current_timestamp);
641 timeout = std::max(timeout, kMinStarvationTimeout);
643 decoder_starvation_callback_.Reset(
644 base::Bind(&MediaSourcePlayer::OnDecoderStarved, weak_this_));
645 base::MessageLoop::current()->PostDelayedTask(
646 FROM_HERE, decoder_starvation_callback_.callback(), timeout);
649 bool MediaSourcePlayer::IsProtectedSurfaceRequired() {
650 return video_decoder_job_->is_content_encrypted() &&
651 drm_bridge_ && drm_bridge_->IsProtectedSurfaceRequired();
654 void MediaSourcePlayer::OnPrefetchDone() {
655 DVLOG(1) << __FUNCTION__;
656 DCHECK(!audio_decoder_job_->is_decoding());
657 DCHECK(!video_decoder_job_->is_decoding());
659 // A previously posted OnPrefetchDone() could race against a Release(). If
660 // Release() won the race, we should no longer have decoder jobs.
661 // TODO(qinmin/wolenetz): Maintain channel state to not double-request data
662 // or drop data received across Release()+Start(). See http://crbug.com/306314
663 // and http://crbug.com/304234.
664 if (!IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
665 DVLOG(1) << __FUNCTION__ << " : aborting";
669 ClearPendingEvent(PREFETCH_DONE_EVENT_PENDING);
671 if (pending_event_ != NO_EVENT_PENDING) {
672 ProcessPendingEvents();
679 start_time_ticks_ = base::TimeTicks::Now();
680 start_presentation_timestamp_ = GetCurrentTime();
681 if (!clock_.IsPlaying())
684 if (!AudioFinished())
687 if (!VideoFinished())
691 void MediaSourcePlayer::OnDemuxerConfigsChanged() {
692 manager()->OnMediaMetadataChanged(
693 player_id(), duration_, GetVideoWidth(), GetVideoHeight(), true);
696 const char* MediaSourcePlayer::GetEventName(PendingEventFlags event) {
697 // Please keep this in sync with PendingEventFlags.
698 static const char* kPendingEventNames[] = {
706 for (size_t i = 0; i < arraysize(kPendingEventNames); ++i, mask <<= 1) {
708 return kPendingEventNames[i];
714 bool MediaSourcePlayer::IsEventPending(PendingEventFlags event) const {
715 return pending_event_ & event;
718 void MediaSourcePlayer::SetPendingEvent(PendingEventFlags event) {
719 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
720 DCHECK_NE(event, NO_EVENT_PENDING);
721 DCHECK(!IsEventPending(event));
723 pending_event_ |= event;
726 void MediaSourcePlayer::ClearPendingEvent(PendingEventFlags event) {
727 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
728 DCHECK_NE(event, NO_EVENT_PENDING);
729 DCHECK(IsEventPending(event)) << GetEventName(event);
731 pending_event_ &= ~event;
734 void MediaSourcePlayer::RetryDecoderCreation(bool audio, bool video) {
736 is_waiting_for_audio_decoder_ = false;
738 is_waiting_for_video_decoder_ = false;
739 if (IsEventPending(DECODER_CREATION_EVENT_PENDING))
740 ProcessPendingEvents();
743 void MediaSourcePlayer::OnKeyAdded() {
744 DVLOG(1) << __FUNCTION__;
745 if (!is_waiting_for_key_)
748 is_waiting_for_key_ = false;
753 void MediaSourcePlayer::OnCdmUnset() {
754 DVLOG(1) << __FUNCTION__;
755 // TODO(xhwang): Support detachment of CDM. This will be needed when we start
756 // to support setMediaKeys(0) (see http://crbug.com/330324), or when we
757 // release MediaDrm when the video is paused, or when the device goes to
758 // sleep (see http://crbug.com/272421).
759 NOTREACHED() << "CDM detachment not supported.";
761 audio_decoder_job_->SetDrmBridge(NULL);
762 video_decoder_job_->SetDrmBridge(NULL);