Home | History | Annotate | Download | only in android
      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.
      4 
      5 #include "media/base/android/media_source_player.h"
      6 
      7 #include <limits>
      8 
      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"
     22 
     23 
     24 namespace media {
     25 
     26 MediaSourcePlayer::MediaSourcePlayer(
     27     int player_id,
     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,
     34                          manager,
     35                          request_media_resources_cb,
     36                          release_media_resources_cb,
     37                          frame_url),
     38       demuxer_(demuxer.Pass()),
     39       pending_event_(NO_EVENT_PENDING),
     40       playing_(false),
     41       clock_(&default_tick_clock_),
     42       doing_browser_seek_(false),
     43       pending_seek_(false),
     44       drm_bridge_(NULL),
     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),
     49       weak_factory_(this) {
     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();
     67 }
     68 
     69 MediaSourcePlayer::~MediaSourcePlayer() {
     70   Release();
     71   DCHECK_EQ(!drm_bridge_, !cdm_registration_id_);
     72   if (drm_bridge_) {
     73     drm_bridge_->UnregisterPlayer(cdm_registration_id_);
     74     cdm_registration_id_ = 0;
     75   }
     76 }
     77 
     78 void MediaSourcePlayer::SetVideoSurface(gfx::ScopedJavaSurface surface) {
     79   DVLOG(1) << __FUNCTION__;
     80   if (!video_decoder_job_->SetVideoSurface(surface.Pass()))
     81     return;
     82   // Retry video decoder creation.
     83   RetryDecoderCreation(false, true);
     84 }
     85 
     86 void MediaSourcePlayer::ScheduleSeekEventAndStopDecoding(
     87     base::TimeDelta seek_time) {
     88   DVLOG(1) << __FUNCTION__ << "(" << seek_time.InSecondsF() << ")";
     89   DCHECK(!IsEventPending(SEEK_EVENT_PENDING));
     90 
     91   pending_seek_ = false;
     92 
     93   clock_.SetTime(seek_time, seek_time);
     94 
     95   if (audio_decoder_job_->is_decoding())
     96     audio_decoder_job_->StopDecode();
     97   if (video_decoder_job_->is_decoding())
     98     video_decoder_job_->StopDecode();
     99 
    100   SetPendingEvent(SEEK_EVENT_PENDING);
    101   ProcessPendingEvents();
    102 }
    103 
    104 void MediaSourcePlayer::BrowserSeekToCurrentTime() {
    105   DVLOG(1) << __FUNCTION__;
    106 
    107   DCHECK(!IsEventPending(SEEK_EVENT_PENDING));
    108   doing_browser_seek_ = true;
    109   ScheduleSeekEventAndStopDecoding(GetCurrentTime());
    110 }
    111 
    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.
    117   return duration_ <
    118          base::TimeDelta::FromMilliseconds(std::numeric_limits<int32>::max());
    119 }
    120 
    121 void MediaSourcePlayer::Start() {
    122   DVLOG(1) << __FUNCTION__;
    123 
    124   playing_ = true;
    125 
    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());
    134 
    135   StartInternal();
    136 }
    137 
    138 void MediaSourcePlayer::Pause(bool is_media_related_action) {
    139   DVLOG(1) << __FUNCTION__;
    140 
    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.
    146   playing_ = false;
    147   start_time_ticks_ = base::TimeTicks();
    148 }
    149 
    150 bool MediaSourcePlayer::IsPlaying() {
    151   return playing_;
    152 }
    153 
    154 int MediaSourcePlayer::GetVideoWidth() {
    155   return video_decoder_job_->width();
    156 }
    157 
    158 int MediaSourcePlayer::GetVideoHeight() {
    159   return video_decoder_job_->height();
    160 }
    161 
    162 void MediaSourcePlayer::SeekTo(base::TimeDelta timestamp) {
    163   DVLOG(1) << __FUNCTION__ << "(" << timestamp.InSecondsF() << ")";
    164 
    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";
    168 
    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;
    174     return;
    175   }
    176 
    177   doing_browser_seek_ = false;
    178   ScheduleSeekEventAndStopDecoding(timestamp);
    179 }
    180 
    181 base::TimeDelta MediaSourcePlayer::GetCurrentTime() {
    182   return clock_.Elapsed();
    183 }
    184 
    185 base::TimeDelta MediaSourcePlayer::GetDuration() {
    186   return duration_;
    187 }
    188 
    189 void MediaSourcePlayer::Release() {
    190   DVLOG(1) << __FUNCTION__;
    191 
    192   is_surface_in_use_ = false;
    193   audio_decoder_job_->ReleaseDecoderResources();
    194   video_decoder_job_->ReleaseDecoderResources();
    195 
    196   // Prevent player restart, including job re-creation attempts.
    197   playing_ = false;
    198 
    199   decoder_starvation_callback_.Cancel();
    200 }
    201 
    202 void MediaSourcePlayer::SetVolume(double volume) {
    203   audio_decoder_job_->SetVolume(volume);
    204 }
    205 
    206 bool MediaSourcePlayer::IsSurfaceInUse() const {
    207   return is_surface_in_use_;
    208 }
    209 
    210 bool MediaSourcePlayer::CanPause() {
    211   return Seekable();
    212 }
    213 
    214 bool MediaSourcePlayer::CanSeekForward() {
    215   return Seekable();
    216 }
    217 
    218 bool MediaSourcePlayer::CanSeekBackward() {
    219   return Seekable();
    220 }
    221 
    222 bool MediaSourcePlayer::IsPlayerReady() {
    223   return audio_decoder_job_ || video_decoder_job_;
    224 }
    225 
    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)
    230     return;
    231 
    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;
    236 
    237   SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
    238   ProcessPendingEvents();
    239 }
    240 
    241 void MediaSourcePlayer::OnDemuxerConfigsAvailable(
    242     const DemuxerConfigs& configs) {
    243   DVLOG(1) << __FUNCTION__;
    244   DCHECK(!HasAudio() && !HasVideo());
    245   duration_ = configs.duration;
    246   clock_.SetDuration(duration_);
    247 
    248   audio_decoder_job_->SetDemuxerConfigs(configs);
    249   video_decoder_job_->SetDemuxerConfigs(configs);
    250   OnDemuxerConfigsChanged();
    251 }
    252 
    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());
    257 
    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);
    262 }
    263 
    264 void MediaSourcePlayer::OnDemuxerDurationChanged(base::TimeDelta duration) {
    265   duration_ = duration;
    266   clock_.SetDuration(duration_);
    267 }
    268 
    269 void MediaSourcePlayer::OnMediaCryptoReady() {
    270   DCHECK(!drm_bridge_->GetMediaCrypto().is_null());
    271   drm_bridge_->SetMediaCryptoReadyCB(base::Closure());
    272 
    273   // Retry decoder creation if the decoders are waiting for MediaCrypto.
    274   RetryDecoderCreation(true, true);
    275 }
    276 
    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!";
    285   }
    286 
    287   if (drm_bridge_) {
    288     NOTREACHED() << "Currently we do not support resetting CDM.";
    289     return;
    290   }
    291 
    292   // Only MediaDrmBridge will be set on MediaSourcePlayer.
    293   drm_bridge_ = static_cast<MediaDrmBridge*>(cdm);
    294 
    295   cdm_registration_id_ = drm_bridge_->RegisterPlayer(
    296       base::Bind(&MediaSourcePlayer::OnKeyAdded, weak_this_),
    297       base::Bind(&MediaSourcePlayer::OnCdmUnset, weak_this_));
    298 
    299   audio_decoder_job_->SetDrmBridge(drm_bridge_);
    300   video_decoder_job_->SetDrmBridge(drm_bridge_);
    301 
    302   if (drm_bridge_->GetMediaCrypto().is_null()) {
    303     drm_bridge_->SetMediaCryptoReadyCB(
    304         base::Bind(&MediaSourcePlayer::OnMediaCryptoReady, weak_this_));
    305     return;
    306   }
    307 
    308   // If the player is previously waiting for CDM, retry decoder creation.
    309   RetryDecoderCreation(true, true);
    310 }
    311 
    312 void MediaSourcePlayer::OnDemuxerSeekDone(
    313     base::TimeDelta actual_browser_seek_time) {
    314   DVLOG(1) << __FUNCTION__;
    315 
    316   ClearPendingEvent(SEEK_EVENT_PENDING);
    317   if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING))
    318     ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
    319 
    320   if (pending_seek_) {
    321     DVLOG(1) << __FUNCTION__ << "processing pending seek";
    322     DCHECK(doing_browser_seek_);
    323     pending_seek_ = false;
    324     SeekTo(pending_seek_time_);
    325     return;
    326   }
    327 
    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);
    341   } else {
    342     DCHECK(actual_browser_seek_time == kNoTimestamp());
    343   }
    344 
    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;
    351   if (HasAudio())
    352     audio_decoder_job_->BeginPrerolling(preroll_timestamp_);
    353   if (HasVideo())
    354     video_decoder_job_->BeginPrerolling(preroll_timestamp_);
    355 
    356   if (!doing_browser_seek_)
    357     manager()->OnSeekComplete(player_id(), current_time);
    358 
    359   ProcessPendingEvents();
    360 }
    361 
    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());
    367 }
    368 
    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.";
    374     return;
    375   }
    376 
    377   if (audio_decoder_job_->is_decoding()) {
    378     DVLOG(1) << __FUNCTION__ << " : An audio job is still decoding.";
    379     return;
    380   }
    381 
    382   if (IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
    383     DVLOG(1) << __FUNCTION__ << " : PREFETCH_DONE still pending.";
    384     return;
    385   }
    386 
    387   if (IsEventPending(SEEK_EVENT_PENDING)) {
    388     DVLOG(1) << __FUNCTION__ << " : Handling SEEK_EVENT";
    389     ClearDecodingData();
    390     audio_decoder_job_->SetBaseTimestamp(GetCurrentTime());
    391     demuxer_->RequestDemuxerSeek(GetCurrentTime(), doing_browser_seek_);
    392     return;
    393   }
    394 
    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_)
    398       return;
    399     ClearPendingEvent(DECODER_CREATION_EVENT_PENDING);
    400   }
    401 
    402   if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING)) {
    403     DVLOG(1) << __FUNCTION__ << " : Handling PREFETCH_REQUEST_EVENT.";
    404     int count = (AudioFinished() ? 0 : 1) + (VideoFinished() ? 0 : 1);
    405 
    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
    408     // no-op.
    409     ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
    410     if (count == 0)
    411       return;
    412 
    413     SetPendingEvent(PREFETCH_DONE_EVENT_PENDING);
    414     base::Closure barrier = BarrierClosure(
    415         count, base::Bind(&MediaSourcePlayer::OnPrefetchDone, weak_this_));
    416 
    417     if (!AudioFinished())
    418       audio_decoder_job_->Prefetch(barrier);
    419 
    420     if (!VideoFinished())
    421       video_decoder_job_->Prefetch(barrier);
    422 
    423     return;
    424   }
    425 
    426   DCHECK_EQ(pending_event_, NO_EVENT_PENDING);
    427 
    428   // Now that all pending events have been handled, resume decoding if we are
    429   // still playing.
    430   if (playing_)
    431     StartInternal();
    432 }
    433 
    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;
    439 
    440   // TODO(xhwang): Drop IntToString() when http://crbug.com/303899 is fixed.
    441   if (is_audio) {
    442     TRACE_EVENT_ASYNC_END1("media",
    443                            "MediaSourcePlayer::DecodeMoreAudio",
    444                            audio_decoder_job_.get(),
    445                            "MediaCodecStatus",
    446                            base::IntToString(status));
    447   } else {
    448     TRACE_EVENT_ASYNC_END1("media",
    449                            "MediaSourcePlayer::DecodeMoreVideo",
    450                            video_decoder_job_.get(),
    451                            "MediaCodecStatus",
    452                            base::IntToString(status));
    453   }
    454 
    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();
    458 
    459   bool is_clock_manager = is_audio || !HasAudio();
    460 
    461   if (is_clock_manager)
    462     decoder_starvation_callback_.Cancel();
    463 
    464   if (status == MEDIA_CODEC_ERROR) {
    465     DVLOG(1) << __FUNCTION__ << " : decode error";
    466     Release();
    467     manager()->OnError(player_id(), MEDIA_ERROR_DECODE);
    468     return;
    469   }
    470 
    471   DCHECK(!IsEventPending(PREFETCH_DONE_EVENT_PENDING));
    472 
    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();
    478     return;
    479   }
    480 
    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);
    485   }
    486 
    487   if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM)
    488     PlaybackCompleted(is_audio);
    489 
    490   if (pending_event_ != NO_EVENT_PENDING) {
    491     ProcessPendingEvents();
    492     return;
    493   }
    494 
    495   if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM)
    496     return;
    497 
    498   if (!playing_) {
    499     if (is_clock_manager)
    500       clock_.Pause();
    501     return;
    502   }
    503 
    504   if (status == MEDIA_CODEC_NO_KEY) {
    505     is_waiting_for_key_ = true;
    506     return;
    507   }
    508 
    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
    511   // come.
    512   if (status == MEDIA_CODEC_STOPPED)
    513     return;
    514 
    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);
    522     else
    523       start_time_ticks_ = base::TimeTicks::Now();
    524   }
    525 
    526   if (is_audio)
    527     DecodeMoreAudio();
    528   else
    529     DecodeMoreVideo();
    530 }
    531 
    532 void MediaSourcePlayer::DecodeMoreAudio() {
    533   DVLOG(1) << __FUNCTION__;
    534   DCHECK(!audio_decoder_job_->is_decoding());
    535   DCHECK(!AudioFinished());
    536 
    537   if (audio_decoder_job_->Decode(
    538       start_time_ticks_,
    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());
    543     return;
    544   }
    545 
    546   is_waiting_for_audio_decoder_ = true;
    547   if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
    548     SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
    549 }
    550 
    551 void MediaSourcePlayer::DecodeMoreVideo() {
    552   DVLOG(1) << __FUNCTION__;
    553   DCHECK(!video_decoder_job_->is_decoding());
    554   DCHECK(!VideoFinished());
    555 
    556   if (video_decoder_job_->Decode(
    557       start_time_ticks_,
    558       start_presentation_timestamp_,
    559       base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_,
    560                  false))) {
    561     TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreVideo",
    562                              video_decoder_job_.get());
    563     return;
    564   }
    565 
    566   // If the decoder is waiting for iframe, trigger a browser seek.
    567   if (!video_decoder_job_->next_video_data_is_iframe()) {
    568     BrowserSeekToCurrentTime();
    569     return;
    570   }
    571 
    572   is_waiting_for_video_decoder_ = true;
    573   if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
    574     SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
    575 }
    576 
    577 void MediaSourcePlayer::PlaybackCompleted(bool is_audio) {
    578   DVLOG(1) << __FUNCTION__ << "(" << is_audio << ")";
    579 
    580   if (AudioFinished() && VideoFinished()) {
    581     playing_ = false;
    582     clock_.Pause();
    583     start_time_ticks_ = base::TimeTicks();
    584     manager()->OnPlaybackComplete(player_id());
    585   }
    586 }
    587 
    588 void MediaSourcePlayer::ClearDecodingData() {
    589   DVLOG(1) << __FUNCTION__;
    590   audio_decoder_job_->Flush();
    591   video_decoder_job_->Flush();
    592   start_time_ticks_ = base::TimeTicks();
    593 }
    594 
    595 bool MediaSourcePlayer::HasVideo() {
    596   return video_decoder_job_->HasStream();
    597 }
    598 
    599 bool MediaSourcePlayer::HasAudio() {
    600   return audio_decoder_job_->HasStream();
    601 }
    602 
    603 bool MediaSourcePlayer::AudioFinished() {
    604   return audio_decoder_job_->OutputEOSReached() || !HasAudio();
    605 }
    606 
    607 bool MediaSourcePlayer::VideoFinished() {
    608   return video_decoder_job_->OutputEOSReached() || !HasVideo();
    609 }
    610 
    611 void MediaSourcePlayer::OnDecoderStarved() {
    612   DVLOG(1) << __FUNCTION__;
    613   SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
    614   ProcessPendingEvents();
    615 }
    616 
    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);
    625 
    626   base::TimeDelta current_timestamp = GetCurrentTime();
    627   base::TimeDelta timeout;
    628   if (HasAudio()) {
    629     timeout = max_presentation_timestamp - current_timestamp;
    630   } else {
    631     DCHECK(current_timestamp <= current_presentation_timestamp);
    632 
    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);
    639   }
    640 
    641   timeout = std::max(timeout, kMinStarvationTimeout);
    642 
    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);
    647 }
    648 
    649 bool MediaSourcePlayer::IsProtectedSurfaceRequired() {
    650   return video_decoder_job_->is_content_encrypted() &&
    651       drm_bridge_ && drm_bridge_->IsProtectedSurfaceRequired();
    652 }
    653 
    654 void MediaSourcePlayer::OnPrefetchDone() {
    655   DVLOG(1) << __FUNCTION__;
    656   DCHECK(!audio_decoder_job_->is_decoding());
    657   DCHECK(!video_decoder_job_->is_decoding());
    658 
    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";
    666     return;
    667   }
    668 
    669   ClearPendingEvent(PREFETCH_DONE_EVENT_PENDING);
    670 
    671   if (pending_event_ != NO_EVENT_PENDING) {
    672     ProcessPendingEvents();
    673     return;
    674   }
    675 
    676   if (!playing_)
    677     return;
    678 
    679   start_time_ticks_ = base::TimeTicks::Now();
    680   start_presentation_timestamp_ = GetCurrentTime();
    681   if (!clock_.IsPlaying())
    682     clock_.Play();
    683 
    684   if (!AudioFinished())
    685     DecodeMoreAudio();
    686 
    687   if (!VideoFinished())
    688     DecodeMoreVideo();
    689 }
    690 
    691 void MediaSourcePlayer::OnDemuxerConfigsChanged() {
    692   manager()->OnMediaMetadataChanged(
    693       player_id(), duration_, GetVideoWidth(), GetVideoHeight(), true);
    694 }
    695 
    696 const char* MediaSourcePlayer::GetEventName(PendingEventFlags event) {
    697   // Please keep this in sync with PendingEventFlags.
    698   static const char* kPendingEventNames[] = {
    699     "PREFETCH_DONE",
    700     "SEEK",
    701     "DECODER_CREATION",
    702     "PREFETCH_REQUEST",
    703   };
    704 
    705   int mask = 1;
    706   for (size_t i = 0; i < arraysize(kPendingEventNames); ++i, mask <<= 1) {
    707     if (event & mask)
    708       return kPendingEventNames[i];
    709   }
    710 
    711   return "UNKNOWN";
    712 }
    713 
    714 bool MediaSourcePlayer::IsEventPending(PendingEventFlags event) const {
    715   return pending_event_ & event;
    716 }
    717 
    718 void MediaSourcePlayer::SetPendingEvent(PendingEventFlags event) {
    719   DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
    720   DCHECK_NE(event, NO_EVENT_PENDING);
    721   DCHECK(!IsEventPending(event));
    722 
    723   pending_event_ |= event;
    724 }
    725 
    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);
    730 
    731   pending_event_ &= ~event;
    732 }
    733 
    734 void MediaSourcePlayer::RetryDecoderCreation(bool audio, bool video) {
    735   if (audio)
    736     is_waiting_for_audio_decoder_ = false;
    737   if (video)
    738     is_waiting_for_video_decoder_ = false;
    739   if (IsEventPending(DECODER_CREATION_EVENT_PENDING))
    740     ProcessPendingEvents();
    741 }
    742 
    743 void MediaSourcePlayer::OnKeyAdded() {
    744   DVLOG(1) << __FUNCTION__;
    745   if (!is_waiting_for_key_)
    746     return;
    747 
    748   is_waiting_for_key_ = false;
    749   if (playing_)
    750     StartInternal();
    751 }
    752 
    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.";
    760   DCHECK(drm_bridge_);
    761   audio_decoder_job_->SetDrmBridge(NULL);
    762   video_decoder_job_->SetDrmBridge(NULL);
    763   drm_bridge_ = NULL;
    764 }
    765 
    766 }  // namespace media
    767