Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / media / base / android / media_source_player.cc
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       interpolator_(&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   interpolator_.SetUpperBound(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   interpolator_.SetBounds(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 std::min(interpolator_.GetInterpolatedTime(), duration_);
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
247   audio_decoder_job_->SetDemuxerConfigs(configs);
248   video_decoder_job_->SetDemuxerConfigs(configs);
249   OnDemuxerConfigsChanged();
250 }
251
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());
256
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);
261 }
262
263 void MediaSourcePlayer::OnDemuxerDurationChanged(base::TimeDelta duration) {
264   duration_ = duration;
265 }
266
267 void MediaSourcePlayer::OnMediaCryptoReady() {
268   DCHECK(!drm_bridge_->GetMediaCrypto().is_null());
269   drm_bridge_->SetMediaCryptoReadyCB(base::Closure());
270
271   // Retry decoder creation if the decoders are waiting for MediaCrypto.
272   RetryDecoderCreation(true, true);
273 }
274
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!";
283   }
284
285   if (drm_bridge_) {
286     NOTREACHED() << "Currently we do not support resetting CDM.";
287     return;
288   }
289
290   // Only MediaDrmBridge will be set on MediaSourcePlayer.
291   drm_bridge_ = static_cast<MediaDrmBridge*>(cdm);
292
293   cdm_registration_id_ = drm_bridge_->RegisterPlayer(
294       base::Bind(&MediaSourcePlayer::OnKeyAdded, weak_this_),
295       base::Bind(&MediaSourcePlayer::OnCdmUnset, weak_this_));
296
297   audio_decoder_job_->SetDrmBridge(drm_bridge_);
298   video_decoder_job_->SetDrmBridge(drm_bridge_);
299
300   if (drm_bridge_->GetMediaCrypto().is_null()) {
301     drm_bridge_->SetMediaCryptoReadyCB(
302         base::Bind(&MediaSourcePlayer::OnMediaCryptoReady, weak_this_));
303     return;
304   }
305
306   // If the player is previously waiting for CDM, retry decoder creation.
307   RetryDecoderCreation(true, true);
308 }
309
310 void MediaSourcePlayer::OnDemuxerSeekDone(
311     base::TimeDelta actual_browser_seek_time) {
312   DVLOG(1) << __FUNCTION__;
313
314   ClearPendingEvent(SEEK_EVENT_PENDING);
315   if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING))
316     ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
317
318   if (pending_seek_) {
319     DVLOG(1) << __FUNCTION__ << "processing pending seek";
320     DCHECK(doing_browser_seek_);
321     pending_seek_ = false;
322     SeekTo(pending_seek_time_);
323     return;
324   }
325
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);
339   } else {
340     DCHECK(actual_browser_seek_time == kNoTimestamp());
341   }
342
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;
349   if (HasAudio())
350     audio_decoder_job_->BeginPrerolling(preroll_timestamp_);
351   if (HasVideo())
352     video_decoder_job_->BeginPrerolling(preroll_timestamp_);
353
354   if (!doing_browser_seek_)
355     manager()->OnSeekComplete(player_id(), current_time);
356
357   ProcessPendingEvents();
358 }
359
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());
366 }
367
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.";
373     return;
374   }
375
376   if (audio_decoder_job_->is_decoding()) {
377     DVLOG(1) << __FUNCTION__ << " : An audio job is still decoding.";
378     return;
379   }
380
381   if (IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
382     DVLOG(1) << __FUNCTION__ << " : PREFETCH_DONE still pending.";
383     return;
384   }
385
386   if (IsEventPending(SEEK_EVENT_PENDING)) {
387     DVLOG(1) << __FUNCTION__ << " : Handling SEEK_EVENT";
388     ClearDecodingData();
389     audio_decoder_job_->SetBaseTimestamp(GetCurrentTime());
390     demuxer_->RequestDemuxerSeek(GetCurrentTime(), doing_browser_seek_);
391     return;
392   }
393
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_)
397       return;
398     ClearPendingEvent(DECODER_CREATION_EVENT_PENDING);
399   }
400
401   if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING)) {
402     DVLOG(1) << __FUNCTION__ << " : Handling PREFETCH_REQUEST_EVENT.";
403     int count = (AudioFinished() ? 0 : 1) + (VideoFinished() ? 0 : 1);
404
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
407     // no-op.
408     ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
409     if (count == 0)
410       return;
411
412     SetPendingEvent(PREFETCH_DONE_EVENT_PENDING);
413     base::Closure barrier = BarrierClosure(
414         count, base::Bind(&MediaSourcePlayer::OnPrefetchDone, weak_this_));
415
416     if (!AudioFinished())
417       audio_decoder_job_->Prefetch(barrier);
418
419     if (!VideoFinished())
420       video_decoder_job_->Prefetch(barrier);
421
422     return;
423   }
424
425   DCHECK_EQ(pending_event_, NO_EVENT_PENDING);
426
427   // Now that all pending events have been handled, resume decoding if we are
428   // still playing.
429   if (playing_)
430     StartInternal();
431 }
432
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;
438
439   // TODO(xhwang): Drop IntToString() when http://crbug.com/303899 is fixed.
440   if (is_audio) {
441     TRACE_EVENT_ASYNC_END1("media",
442                            "MediaSourcePlayer::DecodeMoreAudio",
443                            audio_decoder_job_.get(),
444                            "MediaCodecStatus",
445                            base::IntToString(status));
446   } else {
447     TRACE_EVENT_ASYNC_END1("media",
448                            "MediaSourcePlayer::DecodeMoreVideo",
449                            video_decoder_job_.get(),
450                            "MediaCodecStatus",
451                            base::IntToString(status));
452   }
453
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();
457
458   bool is_clock_manager = is_audio || !HasAudio();
459
460   if (is_clock_manager)
461     decoder_starvation_callback_.Cancel();
462
463   if (status == MEDIA_CODEC_ERROR) {
464     DVLOG(1) << __FUNCTION__ << " : decode error";
465     Release();
466     manager()->OnError(player_id(), MEDIA_ERROR_DECODE);
467     return;
468   }
469
470   DCHECK(!IsEventPending(PREFETCH_DONE_EVENT_PENDING));
471
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();
477     return;
478   }
479
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);
484   }
485
486   if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM)
487     PlaybackCompleted(is_audio);
488
489   if (pending_event_ != NO_EVENT_PENDING) {
490     ProcessPendingEvents();
491     return;
492   }
493
494   if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM)
495     return;
496
497   if (!playing_) {
498     if (is_clock_manager)
499       interpolator_.StopInterpolating();
500     return;
501   }
502
503   if (status == MEDIA_CODEC_NO_KEY) {
504     is_waiting_for_key_ = true;
505     return;
506   }
507
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
510   // come.
511   if (status == MEDIA_CODEC_STOPPED)
512     return;
513
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);
521     else
522       start_time_ticks_ = base::TimeTicks::Now();
523   }
524
525   if (is_audio)
526     DecodeMoreAudio();
527   else
528     DecodeMoreVideo();
529 }
530
531 void MediaSourcePlayer::DecodeMoreAudio() {
532   DVLOG(1) << __FUNCTION__;
533   DCHECK(!audio_decoder_job_->is_decoding());
534   DCHECK(!AudioFinished());
535
536   if (audio_decoder_job_->Decode(
537       start_time_ticks_,
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());
542     return;
543   }
544
545   is_waiting_for_audio_decoder_ = true;
546   if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
547     SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
548 }
549
550 void MediaSourcePlayer::DecodeMoreVideo() {
551   DVLOG(1) << __FUNCTION__;
552   DCHECK(!video_decoder_job_->is_decoding());
553   DCHECK(!VideoFinished());
554
555   if (video_decoder_job_->Decode(
556       start_time_ticks_,
557       start_presentation_timestamp_,
558       base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_,
559                  false))) {
560     TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreVideo",
561                              video_decoder_job_.get());
562     return;
563   }
564
565   // If the decoder is waiting for iframe, trigger a browser seek.
566   if (!video_decoder_job_->next_video_data_is_iframe()) {
567     BrowserSeekToCurrentTime();
568     return;
569   }
570
571   is_waiting_for_video_decoder_ = true;
572   if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
573     SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
574 }
575
576 void MediaSourcePlayer::PlaybackCompleted(bool is_audio) {
577   DVLOG(1) << __FUNCTION__ << "(" << is_audio << ")";
578
579   if (AudioFinished() && VideoFinished()) {
580     playing_ = false;
581     interpolator_.StopInterpolating();
582     start_time_ticks_ = base::TimeTicks();
583     manager()->OnPlaybackComplete(player_id());
584   }
585 }
586
587 void MediaSourcePlayer::ClearDecodingData() {
588   DVLOG(1) << __FUNCTION__;
589   audio_decoder_job_->Flush();
590   video_decoder_job_->Flush();
591   start_time_ticks_ = base::TimeTicks();
592 }
593
594 bool MediaSourcePlayer::HasVideo() {
595   return video_decoder_job_->HasStream();
596 }
597
598 bool MediaSourcePlayer::HasAudio() {
599   return audio_decoder_job_->HasStream();
600 }
601
602 bool MediaSourcePlayer::AudioFinished() {
603   return audio_decoder_job_->OutputEOSReached() || !HasAudio();
604 }
605
606 bool MediaSourcePlayer::VideoFinished() {
607   return video_decoder_job_->OutputEOSReached() || !HasVideo();
608 }
609
610 void MediaSourcePlayer::OnDecoderStarved() {
611   DVLOG(1) << __FUNCTION__;
612   SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
613   ProcessPendingEvents();
614 }
615
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);
624
625   base::TimeDelta current_timestamp = GetCurrentTime();
626   base::TimeDelta timeout;
627   if (HasAudio()) {
628     timeout = max_presentation_timestamp - current_timestamp;
629   } else {
630     DCHECK(current_timestamp <= current_presentation_timestamp);
631
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);
638   }
639
640   timeout = std::max(timeout, kMinStarvationTimeout);
641
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);
646 }
647
648 bool MediaSourcePlayer::IsProtectedSurfaceRequired() {
649   return video_decoder_job_->is_content_encrypted() &&
650       drm_bridge_ && drm_bridge_->IsProtectedSurfaceRequired();
651 }
652
653 void MediaSourcePlayer::OnPrefetchDone() {
654   DVLOG(1) << __FUNCTION__;
655   DCHECK(!audio_decoder_job_->is_decoding());
656   DCHECK(!video_decoder_job_->is_decoding());
657
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";
665     return;
666   }
667
668   ClearPendingEvent(PREFETCH_DONE_EVENT_PENDING);
669
670   if (pending_event_ != NO_EVENT_PENDING) {
671     ProcessPendingEvents();
672     return;
673   }
674
675   if (!playing_)
676     return;
677
678   start_time_ticks_ = base::TimeTicks::Now();
679   start_presentation_timestamp_ = GetCurrentTime();
680   if (!interpolator_.interpolating())
681     interpolator_.StartInterpolating();
682
683   if (!AudioFinished())
684     DecodeMoreAudio();
685
686   if (!VideoFinished())
687     DecodeMoreVideo();
688 }
689
690 void MediaSourcePlayer::OnDemuxerConfigsChanged() {
691   manager()->OnMediaMetadataChanged(
692       player_id(), duration_, GetVideoWidth(), GetVideoHeight(), true);
693 }
694
695 const char* MediaSourcePlayer::GetEventName(PendingEventFlags event) {
696   // Please keep this in sync with PendingEventFlags.
697   static const char* kPendingEventNames[] = {
698     "PREFETCH_DONE",
699     "SEEK",
700     "DECODER_CREATION",
701     "PREFETCH_REQUEST",
702   };
703
704   int mask = 1;
705   for (size_t i = 0; i < arraysize(kPendingEventNames); ++i, mask <<= 1) {
706     if (event & mask)
707       return kPendingEventNames[i];
708   }
709
710   return "UNKNOWN";
711 }
712
713 bool MediaSourcePlayer::IsEventPending(PendingEventFlags event) const {
714   return pending_event_ & event;
715 }
716
717 void MediaSourcePlayer::SetPendingEvent(PendingEventFlags event) {
718   DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
719   DCHECK_NE(event, NO_EVENT_PENDING);
720   DCHECK(!IsEventPending(event));
721
722   pending_event_ |= event;
723 }
724
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);
729
730   pending_event_ &= ~event;
731 }
732
733 void MediaSourcePlayer::RetryDecoderCreation(bool audio, bool video) {
734   if (audio)
735     is_waiting_for_audio_decoder_ = false;
736   if (video)
737     is_waiting_for_video_decoder_ = false;
738   if (IsEventPending(DECODER_CREATION_EVENT_PENDING))
739     ProcessPendingEvents();
740 }
741
742 void MediaSourcePlayer::OnKeyAdded() {
743   DVLOG(1) << __FUNCTION__;
744   if (!is_waiting_for_key_)
745     return;
746
747   is_waiting_for_key_ = false;
748   if (playing_)
749     StartInternal();
750 }
751
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.";
759   DCHECK(drm_bridge_);
760   audio_decoder_job_->SetDrmBridge(NULL);
761   video_decoder_job_->SetDrmBridge(NULL);
762   drm_bridge_ = NULL;
763 }
764
765 }  // namespace media