Update To 11.40.268.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 namespace media {
24
25 MediaSourcePlayer::MediaSourcePlayer(
26     int player_id,
27     MediaPlayerManager* manager,
28     const RequestMediaResourcesCB& request_media_resources_cb,
29     scoped_ptr<DemuxerAndroid> demuxer,
30     const GURL& frame_url)
31     : MediaPlayerAndroid(player_id,
32                          manager,
33                          request_media_resources_cb,
34                          frame_url),
35       demuxer_(demuxer.Pass()),
36       pending_event_(NO_EVENT_PENDING),
37       playing_(false),
38       interpolator_(&default_tick_clock_),
39       doing_browser_seek_(false),
40       pending_seek_(false),
41       drm_bridge_(NULL),
42       cdm_registration_id_(0),
43       is_waiting_for_key_(false),
44       key_added_while_decode_pending_(false),
45       is_waiting_for_audio_decoder_(false),
46       is_waiting_for_video_decoder_(false),
47       prerolling_(true),
48       weak_factory_(this) {
49   audio_decoder_job_.reset(new AudioDecoderJob(
50       base::Bind(&DemuxerAndroid::RequestDemuxerData,
51                  base::Unretained(demuxer_.get()),
52                  DemuxerStream::AUDIO),
53       base::Bind(&MediaSourcePlayer::OnDemuxerConfigsChanged,
54                  weak_factory_.GetWeakPtr())));
55   video_decoder_job_.reset(new VideoDecoderJob(
56       base::Bind(&DemuxerAndroid::RequestDemuxerData,
57                  base::Unretained(demuxer_.get()),
58                  DemuxerStream::VIDEO),
59       base::Bind(request_media_resources_cb_, player_id),
60       base::Bind(&MediaSourcePlayer::OnDemuxerConfigsChanged,
61                  weak_factory_.GetWeakPtr())));
62   demuxer_->Initialize(this);
63   interpolator_.SetUpperBound(base::TimeDelta());
64   weak_this_ = weak_factory_.GetWeakPtr();
65 }
66
67 MediaSourcePlayer::~MediaSourcePlayer() {
68   Release();
69   DCHECK_EQ(!drm_bridge_, !cdm_registration_id_);
70   if (drm_bridge_) {
71     drm_bridge_->UnregisterPlayer(cdm_registration_id_);
72     cdm_registration_id_ = 0;
73   }
74 }
75
76 void MediaSourcePlayer::SetVideoSurface(gfx::ScopedJavaSurface surface) {
77   DVLOG(1) << __FUNCTION__;
78   if (!video_decoder_job_->SetVideoSurface(surface.Pass()))
79     return;
80   // Retry video decoder creation.
81   RetryDecoderCreation(false, true);
82 }
83
84 void MediaSourcePlayer::ScheduleSeekEventAndStopDecoding(
85     base::TimeDelta seek_time) {
86   DVLOG(1) << __FUNCTION__ << "(" << seek_time.InSecondsF() << ")";
87   DCHECK(!IsEventPending(SEEK_EVENT_PENDING));
88
89   pending_seek_ = false;
90
91   interpolator_.SetBounds(seek_time, seek_time);
92
93   if (audio_decoder_job_->is_decoding())
94     audio_decoder_job_->StopDecode();
95   if (video_decoder_job_->is_decoding())
96     video_decoder_job_->StopDecode();
97
98   SetPendingEvent(SEEK_EVENT_PENDING);
99   ProcessPendingEvents();
100 }
101
102 void MediaSourcePlayer::BrowserSeekToCurrentTime() {
103   DVLOG(1) << __FUNCTION__;
104
105   DCHECK(!IsEventPending(SEEK_EVENT_PENDING));
106   doing_browser_seek_ = true;
107   ScheduleSeekEventAndStopDecoding(GetCurrentTime());
108 }
109
110 bool MediaSourcePlayer::Seekable() {
111   // If the duration TimeDelta, converted to milliseconds from microseconds,
112   // is >= 2^31, then the media is assumed to be unbounded and unseekable.
113   // 2^31 is the bound due to java player using 32-bit integer for time
114   // values at millisecond resolution.
115   return duration_ <
116          base::TimeDelta::FromMilliseconds(std::numeric_limits<int32>::max());
117 }
118
119 void MediaSourcePlayer::Start() {
120   DVLOG(1) << __FUNCTION__;
121
122   playing_ = true;
123
124   bool request_fullscreen = IsProtectedSurfaceRequired();
125 #if defined(VIDEO_HOLE)
126   // Skip to request fullscreen when hole-punching is used.
127   request_fullscreen = request_fullscreen &&
128       !manager()->ShouldUseVideoOverlayForEmbeddedEncryptedVideo();
129 #endif  // defined(VIDEO_HOLE)
130   if (request_fullscreen)
131     manager()->RequestFullScreen(player_id());
132
133   StartInternal();
134 }
135
136 void MediaSourcePlayer::Pause(bool is_media_related_action) {
137   DVLOG(1) << __FUNCTION__;
138
139   // Since decoder jobs have their own thread, decoding is not fully paused
140   // until all the decoder jobs call MediaDecoderCallback(). It is possible
141   // that Start() is called while the player is waiting for
142   // MediaDecoderCallback(). In that case, decoding will continue when
143   // MediaDecoderCallback() is called.
144   playing_ = false;
145   start_time_ticks_ = base::TimeTicks();
146 }
147
148 bool MediaSourcePlayer::IsPlaying() {
149   return playing_;
150 }
151
152 int MediaSourcePlayer::GetVideoWidth() {
153   return video_decoder_job_->output_width();
154 }
155
156 int MediaSourcePlayer::GetVideoHeight() {
157   return video_decoder_job_->output_height();
158 }
159
160 void MediaSourcePlayer::SeekTo(base::TimeDelta timestamp) {
161   DVLOG(1) << __FUNCTION__ << "(" << timestamp.InSecondsF() << ")";
162
163   if (IsEventPending(SEEK_EVENT_PENDING)) {
164     DCHECK(doing_browser_seek_) << "SeekTo while SeekTo in progress";
165     DCHECK(!pending_seek_) << "SeekTo while SeekTo pending browser seek";
166
167     // There is a browser seek currently in progress to obtain I-frame to feed
168     // a newly constructed video decoder. Remember this real seek request so
169     // it can be initiated once OnDemuxerSeekDone() occurs for the browser seek.
170     pending_seek_ = true;
171     pending_seek_time_ = timestamp;
172     return;
173   }
174
175   doing_browser_seek_ = false;
176   ScheduleSeekEventAndStopDecoding(timestamp);
177 }
178
179 base::TimeDelta MediaSourcePlayer::GetCurrentTime() {
180   return std::min(interpolator_.GetInterpolatedTime(), duration_);
181 }
182
183 base::TimeDelta MediaSourcePlayer::GetDuration() {
184   return duration_;
185 }
186
187 void MediaSourcePlayer::Release() {
188   DVLOG(1) << __FUNCTION__;
189
190   audio_decoder_job_->ReleaseDecoderResources();
191   video_decoder_job_->ReleaseDecoderResources();
192
193   // Prevent player restart, including job re-creation attempts.
194   playing_ = false;
195
196   decoder_starvation_callback_.Cancel();
197   DetachListener();
198 }
199
200 void MediaSourcePlayer::SetVolume(double volume) {
201   audio_decoder_job_->SetVolume(volume);
202 }
203
204 bool MediaSourcePlayer::CanPause() {
205   return Seekable();
206 }
207
208 bool MediaSourcePlayer::CanSeekForward() {
209   return Seekable();
210 }
211
212 bool MediaSourcePlayer::CanSeekBackward() {
213   return Seekable();
214 }
215
216 bool MediaSourcePlayer::IsPlayerReady() {
217   return audio_decoder_job_ || video_decoder_job_;
218 }
219
220 void MediaSourcePlayer::StartInternal() {
221   DVLOG(1) << __FUNCTION__;
222   // If there are pending events, wait for them finish.
223   if (pending_event_ != NO_EVENT_PENDING)
224     return;
225
226   // When we start, we could have new demuxed data coming in. This new data
227   // could be clear (not encrypted) or encrypted with different keys. So key
228   // related info should all be cleared.
229   is_waiting_for_key_ = false;
230   key_added_while_decode_pending_ = false;
231   AttachListener(NULL);
232
233   SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
234   ProcessPendingEvents();
235 }
236
237 void MediaSourcePlayer::OnDemuxerConfigsAvailable(
238     const DemuxerConfigs& configs) {
239   DVLOG(1) << __FUNCTION__;
240   DCHECK(!HasAudio() && !HasVideo());
241   duration_ = configs.duration;
242
243   audio_decoder_job_->SetDemuxerConfigs(configs);
244   video_decoder_job_->SetDemuxerConfigs(configs);
245   OnDemuxerConfigsChanged();
246 }
247
248 void MediaSourcePlayer::OnDemuxerDataAvailable(const DemuxerData& data) {
249   DVLOG(1) << __FUNCTION__ << "(" << data.type << ")";
250   DCHECK_LT(0u, data.access_units.size());
251   CHECK_GE(1u, data.demuxer_configs.size());
252
253   if (data.type == DemuxerStream::AUDIO)
254     audio_decoder_job_->OnDataReceived(data);
255   else if (data.type == DemuxerStream::VIDEO)
256     video_decoder_job_->OnDataReceived(data);
257 }
258
259 void MediaSourcePlayer::OnDemuxerDurationChanged(base::TimeDelta duration) {
260   duration_ = duration;
261 }
262
263 void MediaSourcePlayer::OnMediaCryptoReady() {
264   DCHECK(!drm_bridge_->GetMediaCrypto().is_null());
265   drm_bridge_->SetMediaCryptoReadyCB(base::Closure());
266
267   // Retry decoder creation if the decoders are waiting for MediaCrypto.
268   RetryDecoderCreation(true, true);
269 }
270
271 void MediaSourcePlayer::SetCdm(BrowserCdm* cdm) {
272   // Currently we don't support DRM change during the middle of playback, even
273   // if the player is paused.
274   // TODO(qinmin): support DRM change after playback has started.
275   // http://crbug.com/253792.
276   if (GetCurrentTime() > base::TimeDelta()) {
277     VLOG(0) << "Setting DRM bridge after playback has started. "
278             << "This is not well supported!";
279   }
280
281   if (drm_bridge_) {
282     NOTREACHED() << "Currently we do not support resetting CDM.";
283     return;
284   }
285
286   // Only MediaDrmBridge will be set on MediaSourcePlayer.
287   drm_bridge_ = static_cast<MediaDrmBridge*>(cdm);
288
289   cdm_registration_id_ = drm_bridge_->RegisterPlayer(
290       base::Bind(&MediaSourcePlayer::OnKeyAdded, weak_this_),
291       base::Bind(&MediaSourcePlayer::OnCdmUnset, weak_this_));
292
293   audio_decoder_job_->SetDrmBridge(drm_bridge_);
294   video_decoder_job_->SetDrmBridge(drm_bridge_);
295
296   if (drm_bridge_->GetMediaCrypto().is_null()) {
297     drm_bridge_->SetMediaCryptoReadyCB(
298         base::Bind(&MediaSourcePlayer::OnMediaCryptoReady, weak_this_));
299     return;
300   }
301
302   // If the player is previously waiting for CDM, retry decoder creation.
303   RetryDecoderCreation(true, true);
304 }
305
306 void MediaSourcePlayer::OnDemuxerSeekDone(
307     base::TimeDelta actual_browser_seek_time) {
308   DVLOG(1) << __FUNCTION__;
309
310   ClearPendingEvent(SEEK_EVENT_PENDING);
311   if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING))
312     ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
313
314   if (pending_seek_) {
315     DVLOG(1) << __FUNCTION__ << "processing pending seek";
316     DCHECK(doing_browser_seek_);
317     pending_seek_ = false;
318     SeekTo(pending_seek_time_);
319     return;
320   }
321
322   // It is possible that a browser seek to I-frame had to seek to a buffered
323   // I-frame later than the requested one due to data removal or GC. Update
324   // player clock to the actual seek target.
325   if (doing_browser_seek_) {
326     DCHECK(actual_browser_seek_time != kNoTimestamp());
327     base::TimeDelta seek_time = actual_browser_seek_time;
328     // A browser seek must not jump into the past. Ideally, it seeks to the
329     // requested time, but it might jump into the future.
330     DCHECK(seek_time >= GetCurrentTime());
331     DVLOG(1) << __FUNCTION__ << " : setting clock to actual browser seek time: "
332              << seek_time.InSecondsF();
333     interpolator_.SetBounds(seek_time, seek_time);
334     audio_decoder_job_->SetBaseTimestamp(seek_time);
335   } else {
336     DCHECK(actual_browser_seek_time == kNoTimestamp());
337   }
338
339   base::TimeDelta current_time = GetCurrentTime();
340   // TODO(qinmin): Simplify the logic by using |start_presentation_timestamp_|
341   // to preroll media decoder jobs. Currently |start_presentation_timestamp_|
342   // is calculated from decoder output, while preroll relies on the access
343   // unit's timestamp. There are some differences between the two.
344   preroll_timestamp_ = current_time;
345   if (HasAudio())
346     audio_decoder_job_->BeginPrerolling(preroll_timestamp_);
347   if (HasVideo())
348     video_decoder_job_->BeginPrerolling(preroll_timestamp_);
349   prerolling_ = true;
350
351   if (!doing_browser_seek_)
352     manager()->OnSeekComplete(player_id(), current_time);
353
354   ProcessPendingEvents();
355 }
356
357 void MediaSourcePlayer::UpdateTimestamps(
358     base::TimeDelta current_presentation_timestamp,
359     base::TimeDelta max_presentation_timestamp) {
360   interpolator_.SetBounds(current_presentation_timestamp,
361                           max_presentation_timestamp);
362   manager()->OnTimeUpdate(player_id(),
363                           GetCurrentTime(),
364                           base::TimeTicks::Now());
365 }
366
367 void MediaSourcePlayer::ProcessPendingEvents() {
368   DVLOG(1) << __FUNCTION__ << " : 0x" << std::hex << pending_event_;
369   // Wait for all the decoding jobs to finish before processing pending tasks.
370   if (video_decoder_job_->is_decoding()) {
371     DVLOG(1) << __FUNCTION__ << " : A video job is still decoding.";
372     return;
373   }
374
375   if (audio_decoder_job_->is_decoding()) {
376     DVLOG(1) << __FUNCTION__ << " : An audio job is still decoding.";
377     return;
378   }
379
380   if (IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
381     DVLOG(1) << __FUNCTION__ << " : PREFETCH_DONE still pending.";
382     return;
383   }
384
385   if (IsEventPending(SEEK_EVENT_PENDING)) {
386     DVLOG(1) << __FUNCTION__ << " : Handling SEEK_EVENT";
387     ClearDecodingData();
388     audio_decoder_job_->SetBaseTimestamp(GetCurrentTime());
389     demuxer_->RequestDemuxerSeek(GetCurrentTime(), doing_browser_seek_);
390     return;
391   }
392
393   if (IsEventPending(DECODER_CREATION_EVENT_PENDING)) {
394     // Don't continue if one of the decoder is not created.
395     if (is_waiting_for_audio_decoder_ || is_waiting_for_video_decoder_)
396       return;
397     ClearPendingEvent(DECODER_CREATION_EVENT_PENDING);
398   }
399
400   if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING)) {
401     DVLOG(1) << __FUNCTION__ << " : Handling PREFETCH_REQUEST_EVENT.";
402     int count = (AudioFinished() ? 0 : 1) + (VideoFinished() ? 0 : 1);
403
404     // It is possible that all streams have finished decode, yet starvation
405     // occurred during the last stream's EOS decode. In this case, prefetch is a
406     // no-op.
407     ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
408     if (count == 0)
409       return;
410
411     SetPendingEvent(PREFETCH_DONE_EVENT_PENDING);
412     base::Closure barrier = BarrierClosure(
413         count, base::Bind(&MediaSourcePlayer::OnPrefetchDone, weak_this_));
414
415     if (!AudioFinished())
416       audio_decoder_job_->Prefetch(barrier);
417
418     if (!VideoFinished())
419       video_decoder_job_->Prefetch(barrier);
420
421     return;
422   }
423
424   DCHECK_EQ(pending_event_, NO_EVENT_PENDING);
425
426   // Now that all pending events have been handled, resume decoding if we are
427   // still playing.
428   if (playing_)
429     StartInternal();
430 }
431
432 void MediaSourcePlayer::MediaDecoderCallback(
433     bool is_audio, MediaCodecStatus status,
434     base::TimeDelta current_presentation_timestamp,
435     base::TimeDelta max_presentation_timestamp) {
436   DVLOG(1) << __FUNCTION__ << ": " << is_audio << ", " << status;
437
438   // TODO(xhwang): Drop IntToString() when http://crbug.com/303899 is fixed.
439   if (is_audio) {
440     TRACE_EVENT_ASYNC_END1("media",
441                            "MediaSourcePlayer::DecodeMoreAudio",
442                            audio_decoder_job_.get(),
443                            "MediaCodecStatus",
444                            base::IntToString(status));
445   } else {
446     TRACE_EVENT_ASYNC_END1("media",
447                            "MediaSourcePlayer::DecodeMoreVideo",
448                            video_decoder_job_.get(),
449                            "MediaCodecStatus",
450                            base::IntToString(status));
451   }
452
453   // Let tests hook the completion of this decode cycle.
454   if (!decode_callback_for_testing_.is_null())
455     base::ResetAndReturn(&decode_callback_for_testing_).Run();
456
457   bool is_clock_manager = is_audio || !HasAudio();
458
459   if (is_clock_manager)
460     decoder_starvation_callback_.Cancel();
461
462   if (status == MEDIA_CODEC_ERROR) {
463     DVLOG(1) << __FUNCTION__ << " : decode error";
464     Release();
465     manager()->OnError(player_id(), MEDIA_ERROR_DECODE);
466     return;
467   }
468
469   DCHECK(!IsEventPending(PREFETCH_DONE_EVENT_PENDING));
470
471   // Let |SEEK_EVENT_PENDING| (the highest priority event outside of
472   // |PREFETCH_DONE_EVENT_PENDING|) preempt output EOS detection here. Process
473   // any other pending events only after handling EOS detection.
474   if (IsEventPending(SEEK_EVENT_PENDING)) {
475     ProcessPendingEvents();
476     return;
477   }
478
479   if ((status == MEDIA_CODEC_OK || status == MEDIA_CODEC_INPUT_END_OF_STREAM) &&
480       is_clock_manager && current_presentation_timestamp != kNoTimestamp()) {
481     UpdateTimestamps(current_presentation_timestamp,
482                      max_presentation_timestamp);
483   }
484
485   if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM) {
486     PlaybackCompleted(is_audio);
487     if (is_clock_manager)
488       interpolator_.StopInterpolating();
489   }
490
491   if (pending_event_ != NO_EVENT_PENDING) {
492     ProcessPendingEvents();
493     return;
494   }
495
496   if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM)
497     return;
498
499   if (!playing_) {
500     if (is_clock_manager)
501       interpolator_.StopInterpolating();
502     return;
503   }
504
505   if (status == MEDIA_CODEC_NO_KEY) {
506     if (key_added_while_decode_pending_) {
507       DVLOG(2) << __FUNCTION__ << ": Key was added during decoding.";
508       ResumePlaybackAfterKeyAdded();
509     } else {
510       is_waiting_for_key_ = true;
511     }
512     return;
513   }
514
515   // If |key_added_while_decode_pending_| is true and both audio and video
516   // decoding succeeded, we should clear |key_added_while_decode_pending_| here.
517   // But that would add more complexity into this function. If we don't clear it
518   // here, the worst case would be we call ResumePlaybackAfterKeyAdded() when
519   // we don't really have a new key. This should rarely happen and the
520   // performance impact should be pretty small.
521   // TODO(qinmin/xhwang): This class is complicated because we handle both audio
522   // and video in one file. If we separate them, we should be able to remove a
523   // lot of duplication.
524
525   // If the status is MEDIA_CODEC_STOPPED, stop decoding new data. The player is
526   // in the middle of a seek or stop event and needs to wait for the IPCs to
527   // come.
528   if (status == MEDIA_CODEC_STOPPED)
529     return;
530
531   if (prerolling_ && IsPrerollFinished(is_audio)) {
532     if (IsPrerollFinished(!is_audio)) {
533       prerolling_ = false;
534       StartInternal();
535     }
536     return;
537   }
538
539   if (is_clock_manager) {
540     // If we have a valid timestamp, start the starvation callback. Otherwise,
541     // reset the |start_time_ticks_| so that the next frame will not suffer
542     // from the decoding delay caused by the current frame.
543     if (current_presentation_timestamp != kNoTimestamp())
544       StartStarvationCallback(current_presentation_timestamp,
545                               max_presentation_timestamp);
546     else
547       start_time_ticks_ = base::TimeTicks::Now();
548   }
549
550   if (is_audio)
551     DecodeMoreAudio();
552   else
553     DecodeMoreVideo();
554 }
555
556 bool MediaSourcePlayer::IsPrerollFinished(bool is_audio) const {
557   if (is_audio)
558     return !HasAudio() || !audio_decoder_job_->prerolling();
559   return !HasVideo() || !video_decoder_job_->prerolling();
560 }
561
562 void MediaSourcePlayer::DecodeMoreAudio() {
563   DVLOG(1) << __FUNCTION__;
564   DCHECK(!audio_decoder_job_->is_decoding());
565   DCHECK(!AudioFinished());
566
567   if (audio_decoder_job_->Decode(
568       start_time_ticks_,
569       start_presentation_timestamp_,
570       base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_, true))) {
571     TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreAudio",
572                              audio_decoder_job_.get());
573     return;
574   }
575
576   is_waiting_for_audio_decoder_ = true;
577   if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
578     SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
579 }
580
581 void MediaSourcePlayer::DecodeMoreVideo() {
582   DVLOG(1) << __FUNCTION__;
583   DCHECK(!video_decoder_job_->is_decoding());
584   DCHECK(!VideoFinished());
585
586   if (video_decoder_job_->Decode(
587       start_time_ticks_,
588       start_presentation_timestamp_,
589       base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_,
590                  false))) {
591     TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreVideo",
592                              video_decoder_job_.get());
593     return;
594   }
595
596   // If the decoder is waiting for iframe, trigger a browser seek.
597   if (!video_decoder_job_->next_video_data_is_iframe()) {
598     BrowserSeekToCurrentTime();
599     return;
600   }
601
602   is_waiting_for_video_decoder_ = true;
603   if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
604     SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
605 }
606
607 void MediaSourcePlayer::PlaybackCompleted(bool is_audio) {
608   DVLOG(1) << __FUNCTION__ << "(" << is_audio << ")";
609
610   if (AudioFinished() && VideoFinished()) {
611     playing_ = false;
612     start_time_ticks_ = base::TimeTicks();
613     manager()->OnPlaybackComplete(player_id());
614   }
615 }
616
617 void MediaSourcePlayer::ClearDecodingData() {
618   DVLOG(1) << __FUNCTION__;
619   audio_decoder_job_->Flush();
620   video_decoder_job_->Flush();
621   start_time_ticks_ = base::TimeTicks();
622 }
623
624 bool MediaSourcePlayer::HasVideo() const {
625   return video_decoder_job_->HasStream();
626 }
627
628 bool MediaSourcePlayer::HasAudio() const {
629   return audio_decoder_job_->HasStream();
630 }
631
632 bool MediaSourcePlayer::AudioFinished() {
633   return audio_decoder_job_->OutputEOSReached() || !HasAudio();
634 }
635
636 bool MediaSourcePlayer::VideoFinished() {
637   return video_decoder_job_->OutputEOSReached() || !HasVideo();
638 }
639
640 void MediaSourcePlayer::OnDecoderStarved() {
641   DVLOG(1) << __FUNCTION__;
642   SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
643   ProcessPendingEvents();
644 }
645
646 void MediaSourcePlayer::StartStarvationCallback(
647     base::TimeDelta current_presentation_timestamp,
648     base::TimeDelta max_presentation_timestamp) {
649   // 20ms was chosen because it is the typical size of a compressed audio frame.
650   // Anything smaller than this would likely cause unnecessary cycling in and
651   // out of the prefetch state.
652   const base::TimeDelta kMinStarvationTimeout =
653       base::TimeDelta::FromMilliseconds(20);
654
655   base::TimeDelta current_timestamp = GetCurrentTime();
656   base::TimeDelta timeout;
657   if (HasAudio()) {
658     timeout = max_presentation_timestamp - current_timestamp;
659   } else {
660     DCHECK(current_timestamp <= current_presentation_timestamp);
661
662     // For video only streams, fps can be estimated from the difference
663     // between the previous and current presentation timestamps. The
664     // previous presentation timestamp is equal to current_timestamp.
665     // TODO(qinmin): determine whether 2 is a good coefficient for estimating
666     // video frame timeout.
667     timeout = 2 * (current_presentation_timestamp - current_timestamp);
668   }
669
670   timeout = std::max(timeout, kMinStarvationTimeout);
671
672   decoder_starvation_callback_.Reset(
673       base::Bind(&MediaSourcePlayer::OnDecoderStarved, weak_this_));
674   base::MessageLoop::current()->PostDelayedTask(
675       FROM_HERE, decoder_starvation_callback_.callback(), timeout);
676 }
677
678 bool MediaSourcePlayer::IsProtectedSurfaceRequired() {
679   return video_decoder_job_->is_content_encrypted() &&
680       drm_bridge_ && drm_bridge_->IsProtectedSurfaceRequired();
681 }
682
683 void MediaSourcePlayer::OnPrefetchDone() {
684   DVLOG(1) << __FUNCTION__;
685   DCHECK(!audio_decoder_job_->is_decoding());
686   DCHECK(!video_decoder_job_->is_decoding());
687
688   // A previously posted OnPrefetchDone() could race against a Release(). If
689   // Release() won the race, we should no longer have decoder jobs.
690   // TODO(qinmin/wolenetz): Maintain channel state to not double-request data
691   // or drop data received across Release()+Start(). See http://crbug.com/306314
692   // and http://crbug.com/304234.
693   if (!IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
694     DVLOG(1) << __FUNCTION__ << " : aborting";
695     return;
696   }
697
698   ClearPendingEvent(PREFETCH_DONE_EVENT_PENDING);
699
700   if (pending_event_ != NO_EVENT_PENDING) {
701     ProcessPendingEvents();
702     return;
703   }
704
705   if (!playing_)
706     return;
707
708   start_time_ticks_ = base::TimeTicks::Now();
709   start_presentation_timestamp_ = GetCurrentTime();
710   if (!interpolator_.interpolating())
711     interpolator_.StartInterpolating();
712
713   if (!AudioFinished())
714     DecodeMoreAudio();
715
716   if (!VideoFinished())
717     DecodeMoreVideo();
718 }
719
720 void MediaSourcePlayer::OnDemuxerConfigsChanged() {
721   manager()->OnMediaMetadataChanged(
722       player_id(), duration_, GetVideoWidth(), GetVideoHeight(), true);
723 }
724
725 const char* MediaSourcePlayer::GetEventName(PendingEventFlags event) {
726   // Please keep this in sync with PendingEventFlags.
727   static const char* kPendingEventNames[] = {
728     "PREFETCH_DONE",
729     "SEEK",
730     "DECODER_CREATION",
731     "PREFETCH_REQUEST",
732   };
733
734   int mask = 1;
735   for (size_t i = 0; i < arraysize(kPendingEventNames); ++i, mask <<= 1) {
736     if (event & mask)
737       return kPendingEventNames[i];
738   }
739
740   return "UNKNOWN";
741 }
742
743 bool MediaSourcePlayer::IsEventPending(PendingEventFlags event) const {
744   return pending_event_ & event;
745 }
746
747 void MediaSourcePlayer::SetPendingEvent(PendingEventFlags event) {
748   DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
749   DCHECK_NE(event, NO_EVENT_PENDING);
750   DCHECK(!IsEventPending(event));
751
752   pending_event_ |= event;
753 }
754
755 void MediaSourcePlayer::ClearPendingEvent(PendingEventFlags event) {
756   DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
757   DCHECK_NE(event, NO_EVENT_PENDING);
758   DCHECK(IsEventPending(event)) << GetEventName(event);
759
760   pending_event_ &= ~event;
761 }
762
763 void MediaSourcePlayer::RetryDecoderCreation(bool audio, bool video) {
764   if (audio)
765     is_waiting_for_audio_decoder_ = false;
766   if (video)
767     is_waiting_for_video_decoder_ = false;
768   if (IsEventPending(DECODER_CREATION_EVENT_PENDING))
769     ProcessPendingEvents();
770 }
771
772 void MediaSourcePlayer::OnKeyAdded() {
773   DVLOG(1) << __FUNCTION__;
774
775   if (is_waiting_for_key_) {
776     ResumePlaybackAfterKeyAdded();
777     return;
778   }
779
780   if ((audio_decoder_job_->is_content_encrypted() &&
781        audio_decoder_job_->is_decoding()) ||
782       (video_decoder_job_->is_content_encrypted() &&
783        video_decoder_job_->is_decoding())) {
784     DVLOG(1) << __FUNCTION__ << ": " << "Key added during pending decode.";
785     key_added_while_decode_pending_ = true;
786   }
787 }
788
789 void MediaSourcePlayer::ResumePlaybackAfterKeyAdded() {
790   DVLOG(1) << __FUNCTION__;
791   DCHECK(is_waiting_for_key_ || key_added_while_decode_pending_);
792
793   is_waiting_for_key_ = false;
794   key_added_while_decode_pending_ = false;
795
796   // StartInternal() will trigger a prefetch, where in most cases we'll just
797   // use previously received data.
798   if (playing_)
799     StartInternal();
800 }
801
802 void MediaSourcePlayer::OnCdmUnset() {
803   DVLOG(1) << __FUNCTION__;
804   DCHECK(drm_bridge_);
805   // TODO(xhwang): Currently this is only called during teardown. Support full
806   // detachment of CDM during playback. This will be needed when we start to
807   // support setMediaKeys(0) (see http://crbug.com/330324), or when we release
808   // MediaDrm when the video is paused, or when the device goes to sleep (see
809   // http://crbug.com/272421).
810   audio_decoder_job_->SetDrmBridge(NULL);
811   video_decoder_job_->SetDrmBridge(NULL);
812   cdm_registration_id_ = 0;
813   drm_bridge_ = NULL;
814 }
815
816 }  // namespace media