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
MediaSourcePlayer(int player_id,MediaPlayerManager * manager,const RequestMediaResourcesCB & request_media_resources_cb,scoped_ptr<DemuxerAndroid> demuxer,const GURL & frame_url)26 MediaSourcePlayer::MediaSourcePlayer(
27 int player_id,
28 MediaPlayerManager* manager,
29 const RequestMediaResourcesCB& request_media_resources_cb,
30 scoped_ptr<DemuxerAndroid> demuxer,
31 const GURL& frame_url)
32 : MediaPlayerAndroid(player_id,
33 manager,
34 request_media_resources_cb,
35 frame_url),
36 demuxer_(demuxer.Pass()),
37 pending_event_(NO_EVENT_PENDING),
38 playing_(false),
39 interpolator_(&default_tick_clock_),
40 doing_browser_seek_(false),
41 pending_seek_(false),
42 drm_bridge_(NULL),
43 cdm_registration_id_(0),
44 is_waiting_for_key_(false),
45 is_waiting_for_audio_decoder_(false),
46 is_waiting_for_video_decoder_(false),
47 prerolling_(false),
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
~MediaSourcePlayer()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
SetVideoSurface(gfx::ScopedJavaSurface surface)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
ScheduleSeekEventAndStopDecoding(base::TimeDelta seek_time)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
BrowserSeekToCurrentTime()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
Seekable()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
Start()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
Pause(bool is_media_related_action)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
IsPlaying()148 bool MediaSourcePlayer::IsPlaying() {
149 return playing_;
150 }
151
GetVideoWidth()152 int MediaSourcePlayer::GetVideoWidth() {
153 return video_decoder_job_->output_width();
154 }
155
GetVideoHeight()156 int MediaSourcePlayer::GetVideoHeight() {
157 return video_decoder_job_->output_height();
158 }
159
SeekTo(base::TimeDelta timestamp)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
GetCurrentTime()179 base::TimeDelta MediaSourcePlayer::GetCurrentTime() {
180 return std::min(interpolator_.GetInterpolatedTime(), duration_);
181 }
182
GetDuration()183 base::TimeDelta MediaSourcePlayer::GetDuration() {
184 return duration_;
185 }
186
Release()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
SetVolume(double volume)200 void MediaSourcePlayer::SetVolume(double volume) {
201 audio_decoder_job_->SetVolume(volume);
202 }
203
CanPause()204 bool MediaSourcePlayer::CanPause() {
205 return Seekable();
206 }
207
CanSeekForward()208 bool MediaSourcePlayer::CanSeekForward() {
209 return Seekable();
210 }
211
CanSeekBackward()212 bool MediaSourcePlayer::CanSeekBackward() {
213 return Seekable();
214 }
215
IsPlayerReady()216 bool MediaSourcePlayer::IsPlayerReady() {
217 return audio_decoder_job_ || video_decoder_job_;
218 }
219
StartInternal()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'll have new demuxed data coming in. This new data could
227 // be clear (not encrypted) or encrypted with different keys. So
228 // |is_waiting_for_key_| condition may not be true anymore.
229 is_waiting_for_key_ = false;
230 AttachListener(NULL);
231
232 SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
233 ProcessPendingEvents();
234 }
235
OnDemuxerConfigsAvailable(const DemuxerConfigs & configs)236 void MediaSourcePlayer::OnDemuxerConfigsAvailable(
237 const DemuxerConfigs& configs) {
238 DVLOG(1) << __FUNCTION__;
239 DCHECK(!HasAudio() && !HasVideo());
240 duration_ = configs.duration;
241
242 audio_decoder_job_->SetDemuxerConfigs(configs);
243 video_decoder_job_->SetDemuxerConfigs(configs);
244 OnDemuxerConfigsChanged();
245 }
246
OnDemuxerDataAvailable(const DemuxerData & data)247 void MediaSourcePlayer::OnDemuxerDataAvailable(const DemuxerData& data) {
248 DVLOG(1) << __FUNCTION__ << "(" << data.type << ")";
249 DCHECK_LT(0u, data.access_units.size());
250 CHECK_GE(1u, data.demuxer_configs.size());
251
252 if (data.type == DemuxerStream::AUDIO)
253 audio_decoder_job_->OnDataReceived(data);
254 else if (data.type == DemuxerStream::VIDEO)
255 video_decoder_job_->OnDataReceived(data);
256 }
257
OnDemuxerDurationChanged(base::TimeDelta duration)258 void MediaSourcePlayer::OnDemuxerDurationChanged(base::TimeDelta duration) {
259 duration_ = duration;
260 }
261
OnMediaCryptoReady()262 void MediaSourcePlayer::OnMediaCryptoReady() {
263 DCHECK(!drm_bridge_->GetMediaCrypto().is_null());
264 drm_bridge_->SetMediaCryptoReadyCB(base::Closure());
265
266 // Retry decoder creation if the decoders are waiting for MediaCrypto.
267 RetryDecoderCreation(true, true);
268 }
269
SetCdm(BrowserCdm * cdm)270 void MediaSourcePlayer::SetCdm(BrowserCdm* cdm) {
271 // Currently we don't support DRM change during the middle of playback, even
272 // if the player is paused.
273 // TODO(qinmin): support DRM change after playback has started.
274 // http://crbug.com/253792.
275 if (GetCurrentTime() > base::TimeDelta()) {
276 VLOG(0) << "Setting DRM bridge after playback has started. "
277 << "This is not well supported!";
278 }
279
280 if (drm_bridge_) {
281 NOTREACHED() << "Currently we do not support resetting CDM.";
282 return;
283 }
284
285 // Only MediaDrmBridge will be set on MediaSourcePlayer.
286 drm_bridge_ = static_cast<MediaDrmBridge*>(cdm);
287
288 cdm_registration_id_ = drm_bridge_->RegisterPlayer(
289 base::Bind(&MediaSourcePlayer::OnKeyAdded, weak_this_),
290 base::Bind(&MediaSourcePlayer::OnCdmUnset, weak_this_));
291
292 audio_decoder_job_->SetDrmBridge(drm_bridge_);
293 video_decoder_job_->SetDrmBridge(drm_bridge_);
294
295 if (drm_bridge_->GetMediaCrypto().is_null()) {
296 drm_bridge_->SetMediaCryptoReadyCB(
297 base::Bind(&MediaSourcePlayer::OnMediaCryptoReady, weak_this_));
298 return;
299 }
300
301 // If the player is previously waiting for CDM, retry decoder creation.
302 RetryDecoderCreation(true, true);
303 }
304
OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time)305 void MediaSourcePlayer::OnDemuxerSeekDone(
306 base::TimeDelta actual_browser_seek_time) {
307 DVLOG(1) << __FUNCTION__;
308
309 ClearPendingEvent(SEEK_EVENT_PENDING);
310 if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING))
311 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
312
313 if (pending_seek_) {
314 DVLOG(1) << __FUNCTION__ << "processing pending seek";
315 DCHECK(doing_browser_seek_);
316 pending_seek_ = false;
317 SeekTo(pending_seek_time_);
318 return;
319 }
320
321 // It is possible that a browser seek to I-frame had to seek to a buffered
322 // I-frame later than the requested one due to data removal or GC. Update
323 // player clock to the actual seek target.
324 if (doing_browser_seek_) {
325 DCHECK(actual_browser_seek_time != kNoTimestamp());
326 base::TimeDelta seek_time = actual_browser_seek_time;
327 // A browser seek must not jump into the past. Ideally, it seeks to the
328 // requested time, but it might jump into the future.
329 DCHECK(seek_time >= GetCurrentTime());
330 DVLOG(1) << __FUNCTION__ << " : setting clock to actual browser seek time: "
331 << seek_time.InSecondsF();
332 interpolator_.SetBounds(seek_time, seek_time);
333 audio_decoder_job_->SetBaseTimestamp(seek_time);
334 } else {
335 DCHECK(actual_browser_seek_time == kNoTimestamp());
336 }
337
338 base::TimeDelta current_time = GetCurrentTime();
339 // TODO(qinmin): Simplify the logic by using |start_presentation_timestamp_|
340 // to preroll media decoder jobs. Currently |start_presentation_timestamp_|
341 // is calculated from decoder output, while preroll relies on the access
342 // unit's timestamp. There are some differences between the two.
343 preroll_timestamp_ = current_time;
344 if (HasAudio())
345 audio_decoder_job_->BeginPrerolling(preroll_timestamp_);
346 if (HasVideo())
347 video_decoder_job_->BeginPrerolling(preroll_timestamp_);
348 prerolling_ = true;
349
350 if (!doing_browser_seek_)
351 manager()->OnSeekComplete(player_id(), current_time);
352
353 ProcessPendingEvents();
354 }
355
UpdateTimestamps(base::TimeDelta current_presentation_timestamp,base::TimeDelta max_presentation_timestamp)356 void MediaSourcePlayer::UpdateTimestamps(
357 base::TimeDelta current_presentation_timestamp,
358 base::TimeDelta max_presentation_timestamp) {
359 interpolator_.SetBounds(current_presentation_timestamp,
360 max_presentation_timestamp);
361 manager()->OnTimeUpdate(player_id(),
362 GetCurrentTime(),
363 base::TimeTicks::Now());
364 }
365
ProcessPendingEvents()366 void MediaSourcePlayer::ProcessPendingEvents() {
367 DVLOG(1) << __FUNCTION__ << " : 0x" << std::hex << pending_event_;
368 // Wait for all the decoding jobs to finish before processing pending tasks.
369 if (video_decoder_job_->is_decoding()) {
370 DVLOG(1) << __FUNCTION__ << " : A video job is still decoding.";
371 return;
372 }
373
374 if (audio_decoder_job_->is_decoding()) {
375 DVLOG(1) << __FUNCTION__ << " : An audio job is still decoding.";
376 return;
377 }
378
379 if (IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
380 DVLOG(1) << __FUNCTION__ << " : PREFETCH_DONE still pending.";
381 return;
382 }
383
384 if (IsEventPending(SEEK_EVENT_PENDING)) {
385 DVLOG(1) << __FUNCTION__ << " : Handling SEEK_EVENT";
386 ClearDecodingData();
387 audio_decoder_job_->SetBaseTimestamp(GetCurrentTime());
388 demuxer_->RequestDemuxerSeek(GetCurrentTime(), doing_browser_seek_);
389 return;
390 }
391
392 if (IsEventPending(DECODER_CREATION_EVENT_PENDING)) {
393 // Don't continue if one of the decoder is not created.
394 if (is_waiting_for_audio_decoder_ || is_waiting_for_video_decoder_)
395 return;
396 ClearPendingEvent(DECODER_CREATION_EVENT_PENDING);
397 }
398
399 if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING)) {
400 DVLOG(1) << __FUNCTION__ << " : Handling PREFETCH_REQUEST_EVENT.";
401 int count = (AudioFinished() ? 0 : 1) + (VideoFinished() ? 0 : 1);
402
403 // It is possible that all streams have finished decode, yet starvation
404 // occurred during the last stream's EOS decode. In this case, prefetch is a
405 // no-op.
406 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
407 if (count == 0)
408 return;
409
410 SetPendingEvent(PREFETCH_DONE_EVENT_PENDING);
411 base::Closure barrier = BarrierClosure(
412 count, base::Bind(&MediaSourcePlayer::OnPrefetchDone, weak_this_));
413
414 if (!AudioFinished())
415 audio_decoder_job_->Prefetch(barrier);
416
417 if (!VideoFinished())
418 video_decoder_job_->Prefetch(barrier);
419
420 return;
421 }
422
423 DCHECK_EQ(pending_event_, NO_EVENT_PENDING);
424
425 // Now that all pending events have been handled, resume decoding if we are
426 // still playing.
427 if (playing_)
428 StartInternal();
429 }
430
MediaDecoderCallback(bool is_audio,MediaCodecStatus status,base::TimeDelta current_presentation_timestamp,base::TimeDelta max_presentation_timestamp)431 void MediaSourcePlayer::MediaDecoderCallback(
432 bool is_audio, MediaCodecStatus status,
433 base::TimeDelta current_presentation_timestamp,
434 base::TimeDelta max_presentation_timestamp) {
435 DVLOG(1) << __FUNCTION__ << ": " << is_audio << ", " << status;
436
437 // TODO(xhwang): Drop IntToString() when http://crbug.com/303899 is fixed.
438 if (is_audio) {
439 TRACE_EVENT_ASYNC_END1("media",
440 "MediaSourcePlayer::DecodeMoreAudio",
441 audio_decoder_job_.get(),
442 "MediaCodecStatus",
443 base::IntToString(status));
444 } else {
445 TRACE_EVENT_ASYNC_END1("media",
446 "MediaSourcePlayer::DecodeMoreVideo",
447 video_decoder_job_.get(),
448 "MediaCodecStatus",
449 base::IntToString(status));
450 }
451
452 // Let tests hook the completion of this decode cycle.
453 if (!decode_callback_for_testing_.is_null())
454 base::ResetAndReturn(&decode_callback_for_testing_).Run();
455
456 bool is_clock_manager = is_audio || !HasAudio();
457
458 if (is_clock_manager)
459 decoder_starvation_callback_.Cancel();
460
461 if (status == MEDIA_CODEC_ERROR) {
462 DVLOG(1) << __FUNCTION__ << " : decode error";
463 Release();
464 manager()->OnError(player_id(), MEDIA_ERROR_DECODE);
465 return;
466 }
467
468 DCHECK(!IsEventPending(PREFETCH_DONE_EVENT_PENDING));
469
470 // Let |SEEK_EVENT_PENDING| (the highest priority event outside of
471 // |PREFETCH_DONE_EVENT_PENDING|) preempt output EOS detection here. Process
472 // any other pending events only after handling EOS detection.
473 if (IsEventPending(SEEK_EVENT_PENDING)) {
474 ProcessPendingEvents();
475 return;
476 }
477
478 if ((status == MEDIA_CODEC_OK || status == MEDIA_CODEC_INPUT_END_OF_STREAM) &&
479 is_clock_manager && current_presentation_timestamp != kNoTimestamp()) {
480 UpdateTimestamps(current_presentation_timestamp,
481 max_presentation_timestamp);
482 }
483
484 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM) {
485 PlaybackCompleted(is_audio);
486 if (is_clock_manager)
487 interpolator_.StopInterpolating();
488 }
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 interpolator_.StopInterpolating();
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 (prerolling_ && IsPrerollFinished(is_audio)) {
516 if (IsPrerollFinished(!is_audio)) {
517 prerolling_ = false;
518 StartInternal();
519 }
520 return;
521 }
522
523 if (is_clock_manager) {
524 // If we have a valid timestamp, start the starvation callback. Otherwise,
525 // reset the |start_time_ticks_| so that the next frame will not suffer
526 // from the decoding delay caused by the current frame.
527 if (current_presentation_timestamp != kNoTimestamp())
528 StartStarvationCallback(current_presentation_timestamp,
529 max_presentation_timestamp);
530 else
531 start_time_ticks_ = base::TimeTicks::Now();
532 }
533
534 if (is_audio)
535 DecodeMoreAudio();
536 else
537 DecodeMoreVideo();
538 }
539
IsPrerollFinished(bool is_audio) const540 bool MediaSourcePlayer::IsPrerollFinished(bool is_audio) const {
541 if (is_audio)
542 return !HasAudio() || !audio_decoder_job_->prerolling();
543 return !HasVideo() || !video_decoder_job_->prerolling();
544 }
545
DecodeMoreAudio()546 void MediaSourcePlayer::DecodeMoreAudio() {
547 DVLOG(1) << __FUNCTION__;
548 DCHECK(!audio_decoder_job_->is_decoding());
549 DCHECK(!AudioFinished());
550
551 if (audio_decoder_job_->Decode(
552 start_time_ticks_,
553 start_presentation_timestamp_,
554 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_, true))) {
555 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreAudio",
556 audio_decoder_job_.get());
557 return;
558 }
559
560 is_waiting_for_audio_decoder_ = true;
561 if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
562 SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
563 }
564
DecodeMoreVideo()565 void MediaSourcePlayer::DecodeMoreVideo() {
566 DVLOG(1) << __FUNCTION__;
567 DCHECK(!video_decoder_job_->is_decoding());
568 DCHECK(!VideoFinished());
569
570 if (video_decoder_job_->Decode(
571 start_time_ticks_,
572 start_presentation_timestamp_,
573 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_,
574 false))) {
575 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreVideo",
576 video_decoder_job_.get());
577 return;
578 }
579
580 // If the decoder is waiting for iframe, trigger a browser seek.
581 if (!video_decoder_job_->next_video_data_is_iframe()) {
582 BrowserSeekToCurrentTime();
583 return;
584 }
585
586 is_waiting_for_video_decoder_ = true;
587 if (!IsEventPending(DECODER_CREATION_EVENT_PENDING))
588 SetPendingEvent(DECODER_CREATION_EVENT_PENDING);
589 }
590
PlaybackCompleted(bool is_audio)591 void MediaSourcePlayer::PlaybackCompleted(bool is_audio) {
592 DVLOG(1) << __FUNCTION__ << "(" << is_audio << ")";
593
594 if (AudioFinished() && VideoFinished()) {
595 playing_ = false;
596 start_time_ticks_ = base::TimeTicks();
597 manager()->OnPlaybackComplete(player_id());
598 }
599 }
600
ClearDecodingData()601 void MediaSourcePlayer::ClearDecodingData() {
602 DVLOG(1) << __FUNCTION__;
603 audio_decoder_job_->Flush();
604 video_decoder_job_->Flush();
605 start_time_ticks_ = base::TimeTicks();
606 }
607
HasVideo() const608 bool MediaSourcePlayer::HasVideo() const {
609 return video_decoder_job_->HasStream();
610 }
611
HasAudio() const612 bool MediaSourcePlayer::HasAudio() const {
613 return audio_decoder_job_->HasStream();
614 }
615
AudioFinished()616 bool MediaSourcePlayer::AudioFinished() {
617 return audio_decoder_job_->OutputEOSReached() || !HasAudio();
618 }
619
VideoFinished()620 bool MediaSourcePlayer::VideoFinished() {
621 return video_decoder_job_->OutputEOSReached() || !HasVideo();
622 }
623
OnDecoderStarved()624 void MediaSourcePlayer::OnDecoderStarved() {
625 DVLOG(1) << __FUNCTION__;
626 SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING);
627 ProcessPendingEvents();
628 }
629
StartStarvationCallback(base::TimeDelta current_presentation_timestamp,base::TimeDelta max_presentation_timestamp)630 void MediaSourcePlayer::StartStarvationCallback(
631 base::TimeDelta current_presentation_timestamp,
632 base::TimeDelta max_presentation_timestamp) {
633 // 20ms was chosen because it is the typical size of a compressed audio frame.
634 // Anything smaller than this would likely cause unnecessary cycling in and
635 // out of the prefetch state.
636 const base::TimeDelta kMinStarvationTimeout =
637 base::TimeDelta::FromMilliseconds(20);
638
639 base::TimeDelta current_timestamp = GetCurrentTime();
640 base::TimeDelta timeout;
641 if (HasAudio()) {
642 timeout = max_presentation_timestamp - current_timestamp;
643 } else {
644 DCHECK(current_timestamp <= current_presentation_timestamp);
645
646 // For video only streams, fps can be estimated from the difference
647 // between the previous and current presentation timestamps. The
648 // previous presentation timestamp is equal to current_timestamp.
649 // TODO(qinmin): determine whether 2 is a good coefficient for estimating
650 // video frame timeout.
651 timeout = 2 * (current_presentation_timestamp - current_timestamp);
652 }
653
654 timeout = std::max(timeout, kMinStarvationTimeout);
655
656 decoder_starvation_callback_.Reset(
657 base::Bind(&MediaSourcePlayer::OnDecoderStarved, weak_this_));
658 base::MessageLoop::current()->PostDelayedTask(
659 FROM_HERE, decoder_starvation_callback_.callback(), timeout);
660 }
661
IsProtectedSurfaceRequired()662 bool MediaSourcePlayer::IsProtectedSurfaceRequired() {
663 return video_decoder_job_->is_content_encrypted() &&
664 drm_bridge_ && drm_bridge_->IsProtectedSurfaceRequired();
665 }
666
OnPrefetchDone()667 void MediaSourcePlayer::OnPrefetchDone() {
668 DVLOG(1) << __FUNCTION__;
669 DCHECK(!audio_decoder_job_->is_decoding());
670 DCHECK(!video_decoder_job_->is_decoding());
671
672 // A previously posted OnPrefetchDone() could race against a Release(). If
673 // Release() won the race, we should no longer have decoder jobs.
674 // TODO(qinmin/wolenetz): Maintain channel state to not double-request data
675 // or drop data received across Release()+Start(). See http://crbug.com/306314
676 // and http://crbug.com/304234.
677 if (!IsEventPending(PREFETCH_DONE_EVENT_PENDING)) {
678 DVLOG(1) << __FUNCTION__ << " : aborting";
679 return;
680 }
681
682 ClearPendingEvent(PREFETCH_DONE_EVENT_PENDING);
683
684 if (pending_event_ != NO_EVENT_PENDING) {
685 ProcessPendingEvents();
686 return;
687 }
688
689 if (!playing_)
690 return;
691
692 start_time_ticks_ = base::TimeTicks::Now();
693 start_presentation_timestamp_ = GetCurrentTime();
694 if (!interpolator_.interpolating())
695 interpolator_.StartInterpolating();
696
697 if (!AudioFinished())
698 DecodeMoreAudio();
699
700 if (!VideoFinished())
701 DecodeMoreVideo();
702 }
703
OnDemuxerConfigsChanged()704 void MediaSourcePlayer::OnDemuxerConfigsChanged() {
705 manager()->OnMediaMetadataChanged(
706 player_id(), duration_, GetVideoWidth(), GetVideoHeight(), true);
707 }
708
GetEventName(PendingEventFlags event)709 const char* MediaSourcePlayer::GetEventName(PendingEventFlags event) {
710 // Please keep this in sync with PendingEventFlags.
711 static const char* kPendingEventNames[] = {
712 "PREFETCH_DONE",
713 "SEEK",
714 "DECODER_CREATION",
715 "PREFETCH_REQUEST",
716 };
717
718 int mask = 1;
719 for (size_t i = 0; i < arraysize(kPendingEventNames); ++i, mask <<= 1) {
720 if (event & mask)
721 return kPendingEventNames[i];
722 }
723
724 return "UNKNOWN";
725 }
726
IsEventPending(PendingEventFlags event) const727 bool MediaSourcePlayer::IsEventPending(PendingEventFlags event) const {
728 return pending_event_ & event;
729 }
730
SetPendingEvent(PendingEventFlags event)731 void MediaSourcePlayer::SetPendingEvent(PendingEventFlags event) {
732 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
733 DCHECK_NE(event, NO_EVENT_PENDING);
734 DCHECK(!IsEventPending(event));
735
736 pending_event_ |= event;
737 }
738
ClearPendingEvent(PendingEventFlags event)739 void MediaSourcePlayer::ClearPendingEvent(PendingEventFlags event) {
740 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")";
741 DCHECK_NE(event, NO_EVENT_PENDING);
742 DCHECK(IsEventPending(event)) << GetEventName(event);
743
744 pending_event_ &= ~event;
745 }
746
RetryDecoderCreation(bool audio,bool video)747 void MediaSourcePlayer::RetryDecoderCreation(bool audio, bool video) {
748 if (audio)
749 is_waiting_for_audio_decoder_ = false;
750 if (video)
751 is_waiting_for_video_decoder_ = false;
752 if (IsEventPending(DECODER_CREATION_EVENT_PENDING))
753 ProcessPendingEvents();
754 }
755
OnKeyAdded()756 void MediaSourcePlayer::OnKeyAdded() {
757 DVLOG(1) << __FUNCTION__;
758 if (!is_waiting_for_key_)
759 return;
760
761 is_waiting_for_key_ = false;
762 if (playing_)
763 StartInternal();
764 }
765
OnCdmUnset()766 void MediaSourcePlayer::OnCdmUnset() {
767 DVLOG(1) << __FUNCTION__;
768 DCHECK(drm_bridge_);
769 // TODO(xhwang): Currently this is only called during teardown. Support full
770 // detachment of CDM during playback. This will be needed when we start to
771 // support setMediaKeys(0) (see http://crbug.com/330324), or when we release
772 // MediaDrm when the video is paused, or when the device goes to sleep (see
773 // http://crbug.com/272421).
774 audio_decoder_job_->SetDrmBridge(NULL);
775 video_decoder_job_->SetDrmBridge(NULL);
776 cdm_registration_id_ = 0;
777 drm_bridge_ = NULL;
778 }
779
780 } // namespace media
781