[ServiceWorker] Implement WebServiceWorkerContextClient::openWindow().
[chromium-blink-merge.git] / content / renderer / media / android / media_source_delegate.cc
blobf4732a0cdb82a257de8ac04e8f92737195c40294
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/media/android/media_source_delegate.h"
7 #include <limits>
8 #include <string>
9 #include <vector>
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "content/renderer/media/android/renderer_demuxer_android.h"
14 #include "media/base/android/demuxer_stream_player_params.h"
15 #include "media/base/bind_to_current_loop.h"
16 #include "media/base/demuxer_stream.h"
17 #include "media/base/media_log.h"
18 #include "media/blink/webmediaplayer_util.h"
19 #include "media/blink/webmediasource_impl.h"
20 #include "media/filters/chunk_demuxer.h"
21 #include "media/filters/decrypting_demuxer_stream.h"
22 #include "third_party/WebKit/public/platform/WebString.h"
23 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
25 using media::DemuxerStream;
26 using media::DemuxerConfigs;
27 using media::DemuxerData;
28 using blink::WebMediaPlayer;
29 using blink::WebString;
31 namespace {
33 // The size of the access unit to transfer in an IPC in case of MediaSource.
34 // 4: approximately 64ms of content in 60 fps movies.
35 const size_t kAccessUnitSizeForMediaSource = 4;
37 const uint8 kVorbisPadding[] = { 0xff, 0xff, 0xff, 0xff };
39 } // namespace
41 namespace content {
43 static void LogMediaSourceError(const scoped_refptr<media::MediaLog>& media_log,
44 const std::string& error) {
45 media_log->AddEvent(media_log->CreateMediaSourceErrorEvent(error));
48 MediaSourceDelegate::MediaSourceDelegate(
49 RendererDemuxerAndroid* demuxer_client,
50 int demuxer_client_id,
51 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner,
52 const scoped_refptr<media::MediaLog> media_log)
53 : demuxer_client_(demuxer_client),
54 demuxer_client_id_(demuxer_client_id),
55 media_log_(media_log),
56 is_demuxer_ready_(false),
57 audio_stream_(NULL),
58 video_stream_(NULL),
59 seeking_(false),
60 is_video_encrypted_(false),
61 doing_browser_seek_(false),
62 browser_seek_time_(media::kNoTimestamp()),
63 expecting_regular_seek_(false),
64 access_unit_size_(0),
65 main_task_runner_(base::MessageLoopProxy::current()),
66 media_task_runner_(media_task_runner),
67 main_weak_factory_(this),
68 media_weak_factory_(this) {
69 main_weak_this_ = main_weak_factory_.GetWeakPtr();
70 DCHECK(main_task_runner_->BelongsToCurrentThread());
73 MediaSourceDelegate::~MediaSourceDelegate() {
74 DCHECK(main_task_runner_->BelongsToCurrentThread());
75 DVLOG(1) << __FUNCTION__ << " : " << demuxer_client_id_;
76 DCHECK(!chunk_demuxer_);
77 DCHECK(!demuxer_client_);
78 DCHECK(!audio_decrypting_demuxer_stream_);
79 DCHECK(!video_decrypting_demuxer_stream_);
80 DCHECK(!audio_stream_);
81 DCHECK(!video_stream_);
84 void MediaSourceDelegate::Stop(const base::Closure& stop_cb) {
85 DCHECK(main_task_runner_->BelongsToCurrentThread());
86 DVLOG(1) << __FUNCTION__ << " : " << demuxer_client_id_;
88 if (!chunk_demuxer_) {
89 DCHECK(!demuxer_client_);
90 return;
93 duration_change_cb_.Reset();
94 update_network_state_cb_.Reset();
95 media_source_opened_cb_.Reset();
97 main_weak_factory_.InvalidateWeakPtrs();
98 DCHECK(!main_weak_factory_.HasWeakPtrs());
100 chunk_demuxer_->Shutdown();
102 // Continue to stop objects on the media thread.
103 media_task_runner_->PostTask(
104 FROM_HERE,
105 base::Bind(
106 &MediaSourceDelegate::StopDemuxer, base::Unretained(this), stop_cb));
109 bool MediaSourceDelegate::IsVideoEncrypted() {
110 DCHECK(main_task_runner_->BelongsToCurrentThread());
111 base::AutoLock auto_lock(is_video_encrypted_lock_);
112 return is_video_encrypted_;
115 base::Time MediaSourceDelegate::GetTimelineOffset() const {
116 DCHECK(main_task_runner_->BelongsToCurrentThread());
117 if (!chunk_demuxer_)
118 return base::Time();
120 return chunk_demuxer_->GetTimelineOffset();
123 void MediaSourceDelegate::StopDemuxer(const base::Closure& stop_cb) {
124 DVLOG(2) << __FUNCTION__;
125 DCHECK(media_task_runner_->BelongsToCurrentThread());
126 DCHECK(chunk_demuxer_);
128 demuxer_client_->RemoveDelegate(demuxer_client_id_);
129 demuxer_client_ = NULL;
131 audio_stream_ = NULL;
132 video_stream_ = NULL;
133 // TODO(xhwang): Figure out if we need to Reset the DDSs after Seeking or
134 // before destroying them.
135 audio_decrypting_demuxer_stream_.reset();
136 video_decrypting_demuxer_stream_.reset();
138 media_weak_factory_.InvalidateWeakPtrs();
139 DCHECK(!media_weak_factory_.HasWeakPtrs());
141 chunk_demuxer_->Stop();
142 chunk_demuxer_.reset();
144 // |this| may be destroyed at this point in time as a result of running
145 // |stop_cb|.
146 stop_cb.Run();
149 void MediaSourceDelegate::InitializeMediaSource(
150 const MediaSourceOpenedCB& media_source_opened_cb,
151 const media::Demuxer::EncryptedMediaInitDataCB&
152 encrypted_media_init_data_cb,
153 const media::SetDecryptorReadyCB& set_decryptor_ready_cb,
154 const UpdateNetworkStateCB& update_network_state_cb,
155 const DurationChangeCB& duration_change_cb) {
156 DCHECK(main_task_runner_->BelongsToCurrentThread());
157 DCHECK(!media_source_opened_cb.is_null());
158 media_source_opened_cb_ = media_source_opened_cb;
159 encrypted_media_init_data_cb_ = encrypted_media_init_data_cb;
160 set_decryptor_ready_cb_ = media::BindToCurrentLoop(set_decryptor_ready_cb);
161 update_network_state_cb_ = media::BindToCurrentLoop(update_network_state_cb);
162 duration_change_cb_ = duration_change_cb;
163 access_unit_size_ = kAccessUnitSizeForMediaSource;
165 chunk_demuxer_.reset(new media::ChunkDemuxer(
166 media::BindToCurrentLoop(
167 base::Bind(&MediaSourceDelegate::OnDemuxerOpened, main_weak_this_)),
168 media::BindToCurrentLoop(base::Bind(
169 &MediaSourceDelegate::OnEncryptedMediaInitData, main_weak_this_)),
170 base::Bind(&LogMediaSourceError, media_log_), media_log_, false));
172 // |this| will be retained until StopDemuxer() is posted, so Unretained() is
173 // safe here.
174 media_task_runner_->PostTask(FROM_HERE,
175 base::Bind(&MediaSourceDelegate::InitializeDemuxer,
176 base::Unretained(this)));
179 void MediaSourceDelegate::InitializeDemuxer() {
180 DCHECK(media_task_runner_->BelongsToCurrentThread());
181 demuxer_client_->AddDelegate(demuxer_client_id_, this);
182 chunk_demuxer_->Initialize(this,
183 base::Bind(&MediaSourceDelegate::OnDemuxerInitDone,
184 media_weak_factory_.GetWeakPtr()),
185 false);
188 blink::WebTimeRanges MediaSourceDelegate::Buffered() const {
189 return media::ConvertToWebTimeRanges(buffered_time_ranges_);
192 size_t MediaSourceDelegate::DecodedFrameCount() const {
193 return statistics_.video_frames_decoded;
196 size_t MediaSourceDelegate::DroppedFrameCount() const {
197 return statistics_.video_frames_dropped;
200 size_t MediaSourceDelegate::AudioDecodedByteCount() const {
201 return statistics_.audio_bytes_decoded;
204 size_t MediaSourceDelegate::VideoDecodedByteCount() const {
205 return statistics_.video_bytes_decoded;
208 void MediaSourceDelegate::CancelPendingSeek(const base::TimeDelta& seek_time) {
209 DCHECK(main_task_runner_->BelongsToCurrentThread());
210 DVLOG(1) << __FUNCTION__ << "(" << seek_time.InSecondsF() << ") : "
211 << demuxer_client_id_;
213 if (!chunk_demuxer_)
214 return;
217 // Remember to trivially finish any newly arriving browser seek requests
218 // that may arrive prior to the next regular seek request.
219 base::AutoLock auto_lock(seeking_lock_);
220 expecting_regular_seek_ = true;
223 // Cancel any previously expected or in-progress regular or browser seek.
224 // It is possible that we have just finished the seek, but caller does
225 // not know this yet. It is still safe to cancel in this case because the
226 // caller will always call StartWaitingForSeek() when it is notified of
227 // the finished seek.
228 chunk_demuxer_->CancelPendingSeek(seek_time);
231 void MediaSourceDelegate::StartWaitingForSeek(
232 const base::TimeDelta& seek_time) {
233 DCHECK(main_task_runner_->BelongsToCurrentThread());
234 DVLOG(1) << __FUNCTION__ << "(" << seek_time.InSecondsF() << ") : "
235 << demuxer_client_id_;
237 if (!chunk_demuxer_)
238 return;
240 bool cancel_browser_seek = false;
242 // Remember to trivially finish any newly arriving browser seek requests
243 // that may arrive prior to the next regular seek request.
244 base::AutoLock auto_lock(seeking_lock_);
245 expecting_regular_seek_ = true;
247 // Remember to cancel any in-progress browser seek.
248 if (seeking_) {
249 DCHECK(doing_browser_seek_);
250 cancel_browser_seek = true;
254 if (cancel_browser_seek)
255 chunk_demuxer_->CancelPendingSeek(seek_time);
256 chunk_demuxer_->StartWaitingForSeek(seek_time);
259 void MediaSourceDelegate::Seek(
260 const base::TimeDelta& seek_time, bool is_browser_seek) {
261 DCHECK(media_task_runner_->BelongsToCurrentThread());
262 DVLOG(1) << __FUNCTION__ << "(" << seek_time.InSecondsF() << ", "
263 << (is_browser_seek ? "browser seek" : "regular seek") << ") : "
264 << demuxer_client_id_;
266 base::TimeDelta internal_seek_time = seek_time;
268 base::AutoLock auto_lock(seeking_lock_);
269 DCHECK(!seeking_);
270 seeking_ = true;
271 doing_browser_seek_ = is_browser_seek;
273 if (doing_browser_seek_ && (!chunk_demuxer_ || expecting_regular_seek_)) {
274 // Trivially finish the browser seek without actually doing it. Reads will
275 // continue to be |kAborted| until the next regular seek is done. Browser
276 // seeking is not supported unless using a ChunkDemuxer; browser seeks are
277 // trivially finished if |chunk_demuxer_| is NULL.
278 seeking_ = false;
279 doing_browser_seek_ = false;
280 demuxer_client_->DemuxerSeekDone(demuxer_client_id_, seek_time);
281 return;
284 if (doing_browser_seek_) {
285 internal_seek_time = FindBufferedBrowserSeekTime_Locked(seek_time);
286 browser_seek_time_ = internal_seek_time;
287 } else {
288 expecting_regular_seek_ = false;
289 browser_seek_time_ = media::kNoTimestamp();
293 // Prepare |chunk_demuxer_| for browser seek.
294 if (is_browser_seek) {
295 chunk_demuxer_->CancelPendingSeek(internal_seek_time);
296 chunk_demuxer_->StartWaitingForSeek(internal_seek_time);
299 SeekInternal(internal_seek_time);
302 void MediaSourceDelegate::SeekInternal(const base::TimeDelta& seek_time) {
303 DCHECK(media_task_runner_->BelongsToCurrentThread());
304 DCHECK(IsSeeking());
305 chunk_demuxer_->Seek(seek_time, base::Bind(
306 &MediaSourceDelegate::OnDemuxerSeekDone,
307 media_weak_factory_.GetWeakPtr()));
310 void MediaSourceDelegate::AddBufferedTimeRange(base::TimeDelta start,
311 base::TimeDelta end) {
312 buffered_time_ranges_.Add(start, end);
315 void MediaSourceDelegate::SetDuration(base::TimeDelta duration) {
316 DCHECK(main_task_runner_->BelongsToCurrentThread());
317 DVLOG(1) << __FUNCTION__ << "(" << duration.InSecondsF() << ") : "
318 << demuxer_client_id_;
320 // Force duration change notification to be async to avoid reentrancy into
321 // ChunkDemxuer.
322 main_task_runner_->PostTask(FROM_HERE, base::Bind(
323 &MediaSourceDelegate::OnDurationChanged, main_weak_this_, duration));
326 void MediaSourceDelegate::OnDurationChanged(const base::TimeDelta& duration) {
327 DCHECK(main_task_runner_->BelongsToCurrentThread());
328 if (demuxer_client_)
329 demuxer_client_->DurationChanged(demuxer_client_id_, duration);
330 if (!duration_change_cb_.is_null())
331 duration_change_cb_.Run(duration);
334 void MediaSourceDelegate::OnReadFromDemuxer(media::DemuxerStream::Type type) {
335 DCHECK(media_task_runner_->BelongsToCurrentThread());
336 DVLOG(1) << __FUNCTION__ << "(" << type << ") : " << demuxer_client_id_;
337 if (IsSeeking())
338 return; // Drop the request during seeking.
340 DCHECK(type == DemuxerStream::AUDIO || type == DemuxerStream::VIDEO);
341 // The access unit size should have been initialized properly at this stage.
342 DCHECK_GT(access_unit_size_, 0u);
343 scoped_ptr<DemuxerData> data(new DemuxerData());
344 data->type = type;
345 data->access_units.resize(access_unit_size_);
346 ReadFromDemuxerStream(type, data.Pass(), 0);
349 void MediaSourceDelegate::ReadFromDemuxerStream(media::DemuxerStream::Type type,
350 scoped_ptr<DemuxerData> data,
351 size_t index) {
352 DCHECK(media_task_runner_->BelongsToCurrentThread());
353 // DemuxerStream::Read() always returns the read callback asynchronously.
354 DemuxerStream* stream =
355 (type == DemuxerStream::AUDIO) ? audio_stream_ : video_stream_;
356 stream->Read(base::Bind(
357 &MediaSourceDelegate::OnBufferReady,
358 media_weak_factory_.GetWeakPtr(), type, base::Passed(&data), index));
361 void MediaSourceDelegate::OnBufferReady(
362 media::DemuxerStream::Type type,
363 scoped_ptr<DemuxerData> data,
364 size_t index,
365 DemuxerStream::Status status,
366 const scoped_refptr<media::DecoderBuffer>& buffer) {
367 DCHECK(media_task_runner_->BelongsToCurrentThread());
368 DVLOG(1) << __FUNCTION__ << "(" << index << ", " << status << ", "
369 << ((!buffer.get() || buffer->end_of_stream())
370 ? -1
371 : buffer->timestamp().InMilliseconds())
372 << ") : " << demuxer_client_id_;
373 DCHECK(chunk_demuxer_);
375 // No new OnReadFromDemuxer() will be called during seeking. So this callback
376 // must be from previous OnReadFromDemuxer() call and should be ignored.
377 if (IsSeeking()) {
378 DVLOG(1) << __FUNCTION__ << ": Ignore previous read during seeking.";
379 return;
382 bool is_audio = (type == DemuxerStream::AUDIO);
383 if (status != DemuxerStream::kAborted &&
384 index >= data->access_units.size()) {
385 LOG(ERROR) << "The internal state inconsistency onBufferReady: "
386 << (is_audio ? "Audio" : "Video") << ", index " << index
387 << ", size " << data->access_units.size()
388 << ", status " << static_cast<int>(status);
389 NOTREACHED();
390 return;
393 switch (status) {
394 case DemuxerStream::kAborted:
395 DVLOG(1) << __FUNCTION__ << " : Aborted";
396 data->access_units[index].status = status;
397 data->access_units.resize(index + 1);
398 break;
400 case DemuxerStream::kConfigChanged:
401 CHECK((is_audio && audio_stream_) || (!is_audio && video_stream_));
402 data->demuxer_configs.resize(1);
403 CHECK(GetDemuxerConfigFromStream(&data->demuxer_configs[0], is_audio));
404 if (!is_audio) {
405 gfx::Size size = data->demuxer_configs[0].video_size;
406 DVLOG(1) << "Video config is changed: " << size.width() << "x"
407 << size.height();
409 data->access_units[index].status = status;
410 data->access_units.resize(index + 1);
411 break;
413 case DemuxerStream::kOk:
414 data->access_units[index].status = status;
415 if (buffer->end_of_stream()) {
416 data->access_units[index].is_end_of_stream = true;
417 data->access_units.resize(index + 1);
418 break;
420 data->access_units[index].is_key_frame = buffer->is_key_frame();
421 // TODO(ycheo): We assume that the inputed stream will be decoded
422 // right away.
423 // Need to implement this properly using MediaPlayer.OnInfoListener.
424 if (is_audio) {
425 statistics_.audio_bytes_decoded += buffer->data_size();
426 } else {
427 statistics_.video_bytes_decoded += buffer->data_size();
428 statistics_.video_frames_decoded++;
430 data->access_units[index].timestamp = buffer->timestamp();
432 data->access_units[index].data.assign(
433 buffer->data(), buffer->data() + buffer->data_size());
434 // Vorbis needs 4 extra bytes padding on Android. Check
435 // NuMediaExtractor.cpp in Android source code.
436 if (is_audio && media::kCodecVorbis ==
437 audio_stream_->audio_decoder_config().codec()) {
438 data->access_units[index].data.insert(
439 data->access_units[index].data.end(), kVorbisPadding,
440 kVorbisPadding + 4);
442 if (buffer->decrypt_config()) {
443 data->access_units[index].key_id = std::vector<char>(
444 buffer->decrypt_config()->key_id().begin(),
445 buffer->decrypt_config()->key_id().end());
446 data->access_units[index].iv = std::vector<char>(
447 buffer->decrypt_config()->iv().begin(),
448 buffer->decrypt_config()->iv().end());
449 data->access_units[index].subsamples =
450 buffer->decrypt_config()->subsamples();
452 if (++index < data->access_units.size()) {
453 ReadFromDemuxerStream(type, data.Pass(), index);
454 return;
456 break;
458 default:
459 NOTREACHED();
462 if (!IsSeeking() && demuxer_client_)
463 demuxer_client_->ReadFromDemuxerAck(demuxer_client_id_, *data);
466 void MediaSourceDelegate::OnDemuxerError(media::PipelineStatus status) {
467 DVLOG(1) << __FUNCTION__ << "(" << status << ") : " << demuxer_client_id_;
468 // |update_network_state_cb_| is bound to the main thread.
469 if (status != media::PIPELINE_OK && !update_network_state_cb_.is_null())
470 update_network_state_cb_.Run(PipelineErrorToNetworkState(status));
473 void MediaSourceDelegate::AddTextStream(
474 media::DemuxerStream* /* text_stream */ ,
475 const media::TextTrackConfig& /* config */ ) {
476 // TODO(matthewjheaney): add text stream (http://crbug/322115).
477 NOTIMPLEMENTED();
480 void MediaSourceDelegate::RemoveTextStream(
481 media::DemuxerStream* /* text_stream */ ) {
482 // TODO(matthewjheaney): remove text stream (http://crbug/322115).
483 NOTIMPLEMENTED();
486 void MediaSourceDelegate::OnDemuxerInitDone(media::PipelineStatus status) {
487 DCHECK(media_task_runner_->BelongsToCurrentThread());
488 DVLOG(1) << __FUNCTION__ << "(" << status << ") : " << demuxer_client_id_;
489 DCHECK(chunk_demuxer_);
491 if (status != media::PIPELINE_OK) {
492 OnDemuxerError(status);
493 return;
496 audio_stream_ = chunk_demuxer_->GetStream(DemuxerStream::AUDIO);
497 video_stream_ = chunk_demuxer_->GetStream(DemuxerStream::VIDEO);
499 if (audio_stream_ && audio_stream_->audio_decoder_config().is_encrypted() &&
500 !set_decryptor_ready_cb_.is_null()) {
501 InitAudioDecryptingDemuxerStream();
502 // InitVideoDecryptingDemuxerStream() will be called in
503 // OnAudioDecryptingDemuxerStreamInitDone().
504 return;
507 if (video_stream_ && video_stream_->video_decoder_config().is_encrypted() &&
508 !set_decryptor_ready_cb_.is_null()) {
509 InitVideoDecryptingDemuxerStream();
510 return;
513 // Notify demuxer ready when both streams are not encrypted.
514 is_demuxer_ready_ = true;
515 NotifyDemuxerReady();
518 void MediaSourceDelegate::InitAudioDecryptingDemuxerStream() {
519 DCHECK(media_task_runner_->BelongsToCurrentThread());
520 DVLOG(1) << __FUNCTION__ << " : " << demuxer_client_id_;
521 DCHECK(!set_decryptor_ready_cb_.is_null());
523 audio_decrypting_demuxer_stream_.reset(new media::DecryptingDemuxerStream(
524 media_task_runner_, set_decryptor_ready_cb_));
525 audio_decrypting_demuxer_stream_->Initialize(
526 audio_stream_,
527 base::Bind(&MediaSourceDelegate::OnAudioDecryptingDemuxerStreamInitDone,
528 media_weak_factory_.GetWeakPtr()));
531 void MediaSourceDelegate::InitVideoDecryptingDemuxerStream() {
532 DCHECK(media_task_runner_->BelongsToCurrentThread());
533 DVLOG(1) << __FUNCTION__ << " : " << demuxer_client_id_;
534 DCHECK(!set_decryptor_ready_cb_.is_null());
536 video_decrypting_demuxer_stream_.reset(new media::DecryptingDemuxerStream(
537 media_task_runner_, set_decryptor_ready_cb_));
538 video_decrypting_demuxer_stream_->Initialize(
539 video_stream_,
540 base::Bind(&MediaSourceDelegate::OnVideoDecryptingDemuxerStreamInitDone,
541 media_weak_factory_.GetWeakPtr()));
544 void MediaSourceDelegate::OnAudioDecryptingDemuxerStreamInitDone(
545 media::PipelineStatus status) {
546 DCHECK(media_task_runner_->BelongsToCurrentThread());
547 DVLOG(1) << __FUNCTION__ << "(" << status << ") : " << demuxer_client_id_;
548 DCHECK(chunk_demuxer_);
550 if (status != media::PIPELINE_OK)
551 audio_decrypting_demuxer_stream_.reset();
552 else
553 audio_stream_ = audio_decrypting_demuxer_stream_.get();
555 if (video_stream_ && video_stream_->video_decoder_config().is_encrypted()) {
556 InitVideoDecryptingDemuxerStream();
557 return;
560 // Try to notify demuxer ready when audio DDS initialization finished and
561 // video is not encrypted.
562 is_demuxer_ready_ = true;
563 NotifyDemuxerReady();
566 void MediaSourceDelegate::OnVideoDecryptingDemuxerStreamInitDone(
567 media::PipelineStatus status) {
568 DCHECK(media_task_runner_->BelongsToCurrentThread());
569 DVLOG(1) << __FUNCTION__ << "(" << status << ") : " << demuxer_client_id_;
570 DCHECK(chunk_demuxer_);
572 if (status != media::PIPELINE_OK)
573 video_decrypting_demuxer_stream_.reset();
574 else
575 video_stream_ = video_decrypting_demuxer_stream_.get();
577 // Try to notify demuxer ready when video DDS initialization finished.
578 is_demuxer_ready_ = true;
579 NotifyDemuxerReady();
582 void MediaSourceDelegate::OnDemuxerSeekDone(media::PipelineStatus status) {
583 DCHECK(media_task_runner_->BelongsToCurrentThread());
584 DVLOG(1) << __FUNCTION__ << "(" << status << ") : " << demuxer_client_id_;
585 DCHECK(IsSeeking());
587 if (status != media::PIPELINE_OK) {
588 OnDemuxerError(status);
589 return;
592 ResetAudioDecryptingDemuxerStream();
595 void MediaSourceDelegate::ResetAudioDecryptingDemuxerStream() {
596 DCHECK(media_task_runner_->BelongsToCurrentThread());
597 DVLOG(1) << __FUNCTION__ << " : " << demuxer_client_id_;
598 if (audio_decrypting_demuxer_stream_) {
599 audio_decrypting_demuxer_stream_->Reset(
600 base::Bind(&MediaSourceDelegate::ResetVideoDecryptingDemuxerStream,
601 media_weak_factory_.GetWeakPtr()));
602 return;
605 ResetVideoDecryptingDemuxerStream();
608 void MediaSourceDelegate::ResetVideoDecryptingDemuxerStream() {
609 DCHECK(media_task_runner_->BelongsToCurrentThread());
610 DVLOG(1) << __FUNCTION__ << " : " << demuxer_client_id_;
611 if (video_decrypting_demuxer_stream_) {
612 video_decrypting_demuxer_stream_->Reset(base::Bind(
613 &MediaSourceDelegate::FinishResettingDecryptingDemuxerStreams,
614 media_weak_factory_.GetWeakPtr()));
615 return;
618 FinishResettingDecryptingDemuxerStreams();
621 void MediaSourceDelegate::FinishResettingDecryptingDemuxerStreams() {
622 DCHECK(media_task_runner_->BelongsToCurrentThread());
623 DVLOG(1) << __FUNCTION__ << " : " << demuxer_client_id_;
625 base::AutoLock auto_lock(seeking_lock_);
626 DCHECK(seeking_);
627 seeking_ = false;
628 doing_browser_seek_ = false;
629 demuxer_client_->DemuxerSeekDone(demuxer_client_id_, browser_seek_time_);
632 void MediaSourceDelegate::NotifyDemuxerReady() {
633 DCHECK(media_task_runner_->BelongsToCurrentThread());
634 DVLOG(1) << __FUNCTION__ << " : " << demuxer_client_id_;
635 DCHECK(is_demuxer_ready_);
637 scoped_ptr<DemuxerConfigs> configs(new DemuxerConfigs());
638 GetDemuxerConfigFromStream(configs.get(), true);
639 GetDemuxerConfigFromStream(configs.get(), false);
640 configs->duration = GetDuration();
642 if (demuxer_client_)
643 demuxer_client_->DemuxerReady(demuxer_client_id_, *configs);
645 base::AutoLock auto_lock(is_video_encrypted_lock_);
646 is_video_encrypted_ = configs->is_video_encrypted;
649 base::TimeDelta MediaSourceDelegate::GetDuration() const {
650 DCHECK(media_task_runner_->BelongsToCurrentThread());
651 if (!chunk_demuxer_)
652 return media::kNoTimestamp();
654 double duration = chunk_demuxer_->GetDuration();
655 if (duration == std::numeric_limits<double>::infinity())
656 return media::kInfiniteDuration();
658 return media::ConvertSecondsToTimestamp(duration);
661 void MediaSourceDelegate::OnDemuxerOpened() {
662 DCHECK(main_task_runner_->BelongsToCurrentThread());
663 if (media_source_opened_cb_.is_null())
664 return;
666 media_source_opened_cb_.Run(new media::WebMediaSourceImpl(
667 chunk_demuxer_.get(), base::Bind(&LogMediaSourceError, media_log_)));
670 void MediaSourceDelegate::OnEncryptedMediaInitData(
671 const std::string& init_data_type,
672 const std::vector<uint8>& init_data) {
673 DCHECK(main_task_runner_->BelongsToCurrentThread());
674 if (encrypted_media_init_data_cb_.is_null())
675 return;
677 encrypted_media_init_data_cb_.Run(init_data_type, init_data);
680 bool MediaSourceDelegate::IsSeeking() const {
681 base::AutoLock auto_lock(seeking_lock_);
682 return seeking_;
685 base::TimeDelta MediaSourceDelegate::FindBufferedBrowserSeekTime_Locked(
686 const base::TimeDelta& seek_time) const {
687 seeking_lock_.AssertAcquired();
688 DCHECK(seeking_);
689 DCHECK(doing_browser_seek_);
690 DCHECK(chunk_demuxer_) << "Browser seek requested, but no chunk demuxer";
692 media::Ranges<base::TimeDelta> buffered =
693 chunk_demuxer_->GetBufferedRanges();
695 for (size_t i = 0; i < buffered.size(); ++i) {
696 base::TimeDelta range_start = buffered.start(i);
697 base::TimeDelta range_end = buffered.end(i);
698 if (range_start <= seek_time) {
699 if (range_end >= seek_time)
700 return seek_time;
701 continue;
704 // If the start of the next buffered range after |seek_time| is too far
705 // into the future, do not jump forward.
706 if ((range_start - seek_time) > base::TimeDelta::FromMilliseconds(100))
707 break;
709 // TODO(wolenetz): Remove possibility that this browser seek jumps
710 // into future when the requested range is unbuffered but there is some
711 // other buffered range after it. See http://crbug.com/304234.
712 return range_start;
715 // We found no range containing |seek_time| or beginning shortly after
716 // |seek_time|. While possible that such data at and beyond the player's
717 // current time have been garbage collected or removed by the web app, this is
718 // unlikely. This may cause unexpected playback stall due to seek pending an
719 // append for a GOP prior to the last GOP demuxed.
720 // TODO(wolenetz): Remove the possibility for this seek to cause unexpected
721 // player stall by replaying cached data since last keyframe in browser player
722 // rather than issuing browser seek. See http://crbug.com/304234.
723 return seek_time;
726 bool MediaSourceDelegate::GetDemuxerConfigFromStream(
727 media::DemuxerConfigs* configs, bool is_audio) {
728 DCHECK(media_task_runner_->BelongsToCurrentThread());
729 if (!is_demuxer_ready_)
730 return false;
731 if (is_audio && audio_stream_) {
732 media::AudioDecoderConfig config = audio_stream_->audio_decoder_config();
733 configs->audio_codec = config.codec();
734 configs->audio_channels =
735 media::ChannelLayoutToChannelCount(config.channel_layout());
736 configs->audio_sampling_rate = config.samples_per_second();
737 configs->is_audio_encrypted = config.is_encrypted();
738 configs->audio_extra_data = std::vector<uint8>(
739 config.extra_data(), config.extra_data() + config.extra_data_size());
740 return true;
742 if (!is_audio && video_stream_) {
743 media::VideoDecoderConfig config = video_stream_->video_decoder_config();
744 configs->video_codec = config.codec();
745 configs->video_size = config.natural_size();
746 configs->is_video_encrypted = config.is_encrypted();
747 configs->video_extra_data = std::vector<uint8>(
748 config.extra_data(), config.extra_data() + config.extra_data_size());
749 return true;
751 return false;
754 } // namespace content