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 "media/blink/webmediaplayer_impl.h"
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/callback_helpers.h"
14 #include "base/debug/alias.h"
15 #include "base/debug/crash_logging.h"
16 #include "base/metrics/histogram.h"
17 #include "base/single_thread_task_runner.h"
18 #include "base/synchronization/waitable_event.h"
19 #include "base/thread_task_runner_handle.h"
20 #include "base/trace_event/trace_event.h"
21 #include "cc/blink/web_layer_impl.h"
22 #include "cc/layers/video_layer.h"
23 #include "gpu/blink/webgraphicscontext3d_impl.h"
24 #include "media/audio/null_audio_sink.h"
25 #include "media/base/bind_to_current_loop.h"
26 #include "media/base/cdm_context.h"
27 #include "media/base/limits.h"
28 #include "media/base/media_log.h"
29 #include "media/base/text_renderer.h"
30 #include "media/base/video_frame.h"
31 #include "media/blink/texttrack_impl.h"
32 #include "media/blink/webaudiosourceprovider_impl.h"
33 #include "media/blink/webcontentdecryptionmodule_impl.h"
34 #include "media/blink/webinbandtexttrack_impl.h"
35 #include "media/blink/webmediaplayer_delegate.h"
36 #include "media/blink/webmediaplayer_util.h"
37 #include "media/blink/webmediasource_impl.h"
38 #include "media/filters/chunk_demuxer.h"
39 #include "media/filters/ffmpeg_demuxer.h"
40 #include "third_party/WebKit/public/platform/WebEncryptedMediaTypes.h"
41 #include "third_party/WebKit/public/platform/WebMediaSource.h"
42 #include "third_party/WebKit/public/platform/WebRect.h"
43 #include "third_party/WebKit/public/platform/WebSize.h"
44 #include "third_party/WebKit/public/platform/WebString.h"
45 #include "third_party/WebKit/public/platform/WebURL.h"
46 #include "third_party/WebKit/public/web/WebLocalFrame.h"
47 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
48 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
49 #include "third_party/WebKit/public/web/WebView.h"
51 using blink::WebCanvas
;
52 using blink::WebMediaPlayer
;
55 using blink::WebString
;
59 // Limits the range of playback rate.
61 // TODO(kylep): Revisit these.
63 // Vista has substantially lower performance than XP or Windows7. If you speed
64 // up a video too much, it can't keep up, and rendering stops updating except on
65 // the time bar. For really high speeds, audio becomes a bottleneck and we just
66 // use up the data we have, which may not achieve the speed requested, but will
69 // A very slow speed, ie 0.00000001x, causes the machine to lock up. (It seems
70 // like a busy loop). It gets unresponsive, although its not completely dead.
72 // Also our timers are not very accurate (especially for ogg), which becomes
73 // evident at low speeds and on Vista. Since other speeds are risky and outside
74 // the norms, we think 1/16x to 16x is a safe and useful range for now.
75 const double kMinRate
= 0.0625;
76 const double kMaxRate
= 16.0;
82 class BufferedDataSourceHostImpl
;
84 #define STATIC_ASSERT_MATCHING_ENUM(name) \
85 static_assert(static_cast<int>(WebMediaPlayer::CORSMode ## name) == \
86 static_cast<int>(BufferedResourceLoader::k ## name), \
87 "mismatching enum values: " #name)
88 STATIC_ASSERT_MATCHING_ENUM(Unspecified
);
89 STATIC_ASSERT_MATCHING_ENUM(Anonymous
);
90 STATIC_ASSERT_MATCHING_ENUM(UseCredentials
);
91 #undef STATIC_ASSERT_MATCHING_ENUM
93 #define BIND_TO_RENDER_LOOP(function) \
94 (DCHECK(main_task_runner_->BelongsToCurrentThread()), \
95 BindToCurrentLoop(base::Bind(function, AsWeakPtr())))
97 #define BIND_TO_RENDER_LOOP1(function, arg1) \
98 (DCHECK(main_task_runner_->BelongsToCurrentThread()), \
99 BindToCurrentLoop(base::Bind(function, AsWeakPtr(), arg1)))
101 WebMediaPlayerImpl::WebMediaPlayerImpl(
102 blink::WebLocalFrame
* frame
,
103 blink::WebMediaPlayerClient
* client
,
104 base::WeakPtr
<WebMediaPlayerDelegate
> delegate
,
105 scoped_ptr
<RendererFactory
> renderer_factory
,
106 CdmFactory
* cdm_factory
,
107 const WebMediaPlayerParams
& params
)
109 network_state_(WebMediaPlayer::NetworkStateEmpty
),
110 ready_state_(WebMediaPlayer::ReadyStateHaveNothing
),
111 preload_(BufferedDataSource::AUTO
),
112 main_task_runner_(base::ThreadTaskRunnerHandle::Get()),
113 media_task_runner_(params
.media_task_runner()),
114 media_log_(params
.media_log()),
115 pipeline_(media_task_runner_
, media_log_
.get()),
116 load_type_(LoadTypeURL
),
122 pending_seek_(false),
123 pending_seek_seconds_(0.0f
),
124 should_notify_time_changed_(false),
127 defer_load_cb_(params
.defer_load_cb()),
128 context_3d_cb_(params
.context_3d_cb()),
129 supports_save_(true),
130 chunk_demuxer_(NULL
),
131 // Threaded compositing isn't enabled universally yet.
132 compositor_task_runner_(
133 params
.compositor_task_runner()
134 ? params
.compositor_task_runner()
135 : base::MessageLoop::current()->task_runner()),
136 compositor_(new VideoFrameCompositor(
137 compositor_task_runner_
,
138 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNaturalSizeChanged
),
139 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnOpacityChanged
))),
140 encrypted_media_support_(cdm_factory
,
142 params
.media_permission(),
143 base::Bind(&WebMediaPlayerImpl::SetCdm
,
145 base::Bind(&IgnoreCdmAttached
))),
146 renderer_factory_(renderer_factory
.Pass()) {
147 media_log_
->AddEvent(
148 media_log_
->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_CREATED
));
150 if (params
.initial_cdm()) {
151 SetCdm(base::Bind(&IgnoreCdmAttached
),
152 ToWebContentDecryptionModuleImpl(params
.initial_cdm())
156 // TODO(xhwang): When we use an external Renderer, many methods won't work,
157 // e.g. GetCurrentFrameFromCompositor(). See http://crbug.com/434861
159 // Use the null sink if no sink was provided.
160 audio_source_provider_
= new WebAudioSourceProviderImpl(
161 params
.audio_renderer_sink().get()
162 ? params
.audio_renderer_sink()
163 : new NullAudioSink(media_task_runner_
));
166 WebMediaPlayerImpl::~WebMediaPlayerImpl() {
167 client_
->setWebLayer(NULL
);
169 DCHECK(main_task_runner_
->BelongsToCurrentThread());
170 media_log_
->AddEvent(
171 media_log_
->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_DESTROYED
));
174 delegate_
->PlayerGone(this);
176 // Abort any pending IO so stopping the pipeline doesn't get blocked.
178 data_source_
->Abort();
179 if (chunk_demuxer_
) {
180 chunk_demuxer_
->Shutdown();
181 chunk_demuxer_
= NULL
;
184 renderer_factory_
.reset();
186 // Make sure to kill the pipeline so there's no more media threads running.
187 // Note: stopping the pipeline might block for a long time.
188 base::WaitableEvent
waiter(false, false);
190 base::Bind(&base::WaitableEvent::Signal
, base::Unretained(&waiter
)));
193 compositor_task_runner_
->DeleteSoon(FROM_HERE
, compositor_
);
196 void WebMediaPlayerImpl::load(LoadType load_type
, const blink::WebURL
& url
,
197 CORSMode cors_mode
) {
198 DVLOG(1) << __FUNCTION__
<< "(" << load_type
<< ", " << url
<< ", "
200 if (!defer_load_cb_
.is_null()) {
201 defer_load_cb_
.Run(base::Bind(
202 &WebMediaPlayerImpl::DoLoad
, AsWeakPtr(), load_type
, url
, cors_mode
));
205 DoLoad(load_type
, url
, cors_mode
);
208 void WebMediaPlayerImpl::DoLoad(LoadType load_type
,
209 const blink::WebURL
& url
,
210 CORSMode cors_mode
) {
211 DCHECK(main_task_runner_
->BelongsToCurrentThread());
214 ReportMediaSchemeUma(gurl
);
216 // Set subresource URL for crash reporting.
217 base::debug::SetCrashKeyValue("subresource_url", gurl
.spec());
219 load_type_
= load_type
;
221 SetNetworkState(WebMediaPlayer::NetworkStateLoading
);
222 SetReadyState(WebMediaPlayer::ReadyStateHaveNothing
);
223 media_log_
->AddEvent(media_log_
->CreateLoadEvent(url
.spec()));
225 // Media source pipelines can start immediately.
226 if (load_type
== LoadTypeMediaSource
) {
227 supports_save_
= false;
232 // Otherwise it's a regular request which requires resolving the URL first.
233 data_source_
.reset(new BufferedDataSource(
235 static_cast<BufferedResourceLoader::CORSMode
>(cors_mode
),
239 &buffered_data_source_host_
,
240 base::Bind(&WebMediaPlayerImpl::NotifyDownloading
, AsWeakPtr())));
241 data_source_
->SetPreload(preload_
);
242 data_source_
->Initialize(
243 base::Bind(&WebMediaPlayerImpl::DataSourceInitialized
, AsWeakPtr()));
246 void WebMediaPlayerImpl::play() {
247 DVLOG(1) << __FUNCTION__
;
248 DCHECK(main_task_runner_
->BelongsToCurrentThread());
251 pipeline_
.SetPlaybackRate(playback_rate_
);
253 data_source_
->MediaIsPlaying();
255 media_log_
->AddEvent(media_log_
->CreateEvent(MediaLogEvent::PLAY
));
257 if (delegate_
&& playback_rate_
> 0)
258 delegate_
->DidPlay(this);
261 void WebMediaPlayerImpl::pause() {
262 DVLOG(1) << __FUNCTION__
;
263 DCHECK(main_task_runner_
->BelongsToCurrentThread());
265 const bool was_already_paused
= paused_
|| playback_rate_
== 0;
267 pipeline_
.SetPlaybackRate(0.0);
269 data_source_
->MediaIsPaused();
272 media_log_
->AddEvent(media_log_
->CreateEvent(MediaLogEvent::PAUSE
));
274 if (!was_already_paused
&& delegate_
)
275 delegate_
->DidPause(this);
278 bool WebMediaPlayerImpl::supportsSave() const {
279 DCHECK(main_task_runner_
->BelongsToCurrentThread());
280 return supports_save_
;
283 void WebMediaPlayerImpl::seek(double seconds
) {
284 DVLOG(1) << __FUNCTION__
<< "(" << seconds
<< "s)";
285 DCHECK(main_task_runner_
->BelongsToCurrentThread());
289 ReadyState old_state
= ready_state_
;
290 if (ready_state_
> WebMediaPlayer::ReadyStateHaveMetadata
)
291 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata
);
293 base::TimeDelta seek_time
= ConvertSecondsToTimestamp(seconds
);
296 pending_seek_
= true;
297 pending_seek_seconds_
= seconds
;
299 chunk_demuxer_
->CancelPendingSeek(seek_time
);
303 media_log_
->AddEvent(media_log_
->CreateSeekEvent(seconds
));
305 // Update our paused time.
306 // In paused state ignore the seek operations to current time if the loading
307 // is completed and generate OnPipelineBufferingStateChanged event to
308 // eventually fire seeking and seeked events
310 if (paused_time_
!= seek_time
) {
311 paused_time_
= seek_time
;
312 } else if (old_state
== ReadyStateHaveEnoughData
) {
313 main_task_runner_
->PostTask(
315 base::Bind(&WebMediaPlayerImpl::OnPipelineBufferingStateChanged
,
316 AsWeakPtr(), BUFFERING_HAVE_ENOUGH
));
324 chunk_demuxer_
->StartWaitingForSeek(seek_time
);
326 // Kick off the asynchronous seek!
329 BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnPipelineSeeked
, true));
332 void WebMediaPlayerImpl::setRate(double rate
) {
333 DVLOG(1) << __FUNCTION__
<< "(" << rate
<< ")";
334 DCHECK(main_task_runner_
->BelongsToCurrentThread());
336 // TODO(kylep): Remove when support for negatives is added. Also, modify the
337 // following checks so rewind uses reasonable values also.
341 // Limit rates to reasonable values by clamping.
345 else if (rate
> kMaxRate
)
347 if (playback_rate_
== 0 && !paused_
&& delegate_
)
348 delegate_
->DidPlay(this);
349 } else if (playback_rate_
!= 0 && !paused_
&& delegate_
) {
350 delegate_
->DidPause(this);
353 playback_rate_
= rate
;
355 pipeline_
.SetPlaybackRate(rate
);
357 data_source_
->MediaPlaybackRateChanged(rate
);
361 void WebMediaPlayerImpl::setVolume(double volume
) {
362 DVLOG(1) << __FUNCTION__
<< "(" << volume
<< ")";
363 DCHECK(main_task_runner_
->BelongsToCurrentThread());
365 pipeline_
.SetVolume(volume
);
368 #define STATIC_ASSERT_MATCHING_ENUM(webkit_name, chromium_name) \
369 static_assert(static_cast<int>(WebMediaPlayer::webkit_name) == \
370 static_cast<int>(BufferedDataSource::chromium_name), \
371 "mismatching enum values: " #webkit_name)
372 STATIC_ASSERT_MATCHING_ENUM(PreloadNone
, NONE
);
373 STATIC_ASSERT_MATCHING_ENUM(PreloadMetaData
, METADATA
);
374 STATIC_ASSERT_MATCHING_ENUM(PreloadAuto
, AUTO
);
375 #undef STATIC_ASSERT_MATCHING_ENUM
377 void WebMediaPlayerImpl::setPreload(WebMediaPlayer::Preload preload
) {
378 DVLOG(1) << __FUNCTION__
<< "(" << preload
<< ")";
379 DCHECK(main_task_runner_
->BelongsToCurrentThread());
381 preload_
= static_cast<BufferedDataSource::Preload
>(preload
);
383 data_source_
->SetPreload(preload_
);
386 bool WebMediaPlayerImpl::hasVideo() const {
387 DCHECK(main_task_runner_
->BelongsToCurrentThread());
389 return pipeline_metadata_
.has_video
;
392 bool WebMediaPlayerImpl::hasAudio() const {
393 DCHECK(main_task_runner_
->BelongsToCurrentThread());
395 return pipeline_metadata_
.has_audio
;
398 blink::WebSize
WebMediaPlayerImpl::naturalSize() const {
399 DCHECK(main_task_runner_
->BelongsToCurrentThread());
401 return blink::WebSize(pipeline_metadata_
.natural_size
);
404 bool WebMediaPlayerImpl::paused() const {
405 DCHECK(main_task_runner_
->BelongsToCurrentThread());
407 return pipeline_
.GetPlaybackRate() == 0.0f
;
410 bool WebMediaPlayerImpl::seeking() const {
411 DCHECK(main_task_runner_
->BelongsToCurrentThread());
413 if (ready_state_
== WebMediaPlayer::ReadyStateHaveNothing
)
419 double WebMediaPlayerImpl::duration() const {
420 DCHECK(main_task_runner_
->BelongsToCurrentThread());
422 if (ready_state_
== WebMediaPlayer::ReadyStateHaveNothing
)
423 return std::numeric_limits
<double>::quiet_NaN();
425 return GetPipelineDuration();
428 double WebMediaPlayerImpl::timelineOffset() const {
429 DCHECK(main_task_runner_
->BelongsToCurrentThread());
431 if (pipeline_metadata_
.timeline_offset
.is_null())
432 return std::numeric_limits
<double>::quiet_NaN();
434 return pipeline_metadata_
.timeline_offset
.ToJsTime();
437 double WebMediaPlayerImpl::currentTime() const {
438 DCHECK(main_task_runner_
->BelongsToCurrentThread());
439 DCHECK_NE(ready_state_
, WebMediaPlayer::ReadyStateHaveNothing
);
441 // TODO(scherkus): Replace with an explicit ended signal to HTMLMediaElement,
442 // see http://crbug.com/409280
446 return (paused_
? paused_time_
: pipeline_
.GetMediaTime()).InSecondsF();
449 WebMediaPlayer::NetworkState
WebMediaPlayerImpl::networkState() const {
450 DCHECK(main_task_runner_
->BelongsToCurrentThread());
451 return network_state_
;
454 WebMediaPlayer::ReadyState
WebMediaPlayerImpl::readyState() const {
455 DCHECK(main_task_runner_
->BelongsToCurrentThread());
459 blink::WebTimeRanges
WebMediaPlayerImpl::buffered() const {
460 DCHECK(main_task_runner_
->BelongsToCurrentThread());
462 Ranges
<base::TimeDelta
> buffered_time_ranges
=
463 pipeline_
.GetBufferedTimeRanges();
465 const base::TimeDelta duration
= pipeline_
.GetMediaDuration();
466 if (duration
!= kInfiniteDuration()) {
467 buffered_data_source_host_
.AddBufferedTimeRanges(
468 &buffered_time_ranges
, duration
);
470 return ConvertToWebTimeRanges(buffered_time_ranges
);
473 blink::WebTimeRanges
WebMediaPlayerImpl::seekable() const {
474 DCHECK(main_task_runner_
->BelongsToCurrentThread());
476 if (ready_state_
< WebMediaPlayer::ReadyStateHaveMetadata
)
477 return blink::WebTimeRanges();
479 const double seekable_end
= duration();
481 // Allow a special exception for seeks to zero for streaming sources with a
482 // finite duration; this allows looping to work.
483 const bool allow_seek_to_zero
= data_source_
&& data_source_
->IsStreaming() &&
484 std::isfinite(seekable_end
);
486 // TODO(dalecurtis): Technically this allows seeking on media which return an
487 // infinite duration so long as DataSource::IsStreaming() is false. While not
488 // expected, disabling this breaks semi-live players, http://crbug.com/427412.
489 const blink::WebTimeRange
seekable_range(
490 0.0, allow_seek_to_zero
? 0.0 : seekable_end
);
491 return blink::WebTimeRanges(&seekable_range
, 1);
494 bool WebMediaPlayerImpl::didLoadingProgress() {
495 DCHECK(main_task_runner_
->BelongsToCurrentThread());
496 bool pipeline_progress
= pipeline_
.DidLoadingProgress();
497 bool data_progress
= buffered_data_source_host_
.DidLoadingProgress();
498 return pipeline_progress
|| data_progress
;
501 void WebMediaPlayerImpl::paint(blink::WebCanvas
* canvas
,
502 const blink::WebRect
& rect
,
504 SkXfermode::Mode mode
) {
505 DCHECK(main_task_runner_
->BelongsToCurrentThread());
506 TRACE_EVENT0("media", "WebMediaPlayerImpl:paint");
508 // TODO(scherkus): Clarify paint() API contract to better understand when and
509 // why it's being called. For example, today paint() is called when:
510 // - We haven't reached HAVE_CURRENT_DATA and need to paint black
511 // - We're painting to a canvas
512 // See http://crbug.com/341225 http://crbug.com/342621 for details.
513 scoped_refptr
<VideoFrame
> video_frame
=
514 GetCurrentFrameFromCompositor();
516 gfx::Rect
gfx_rect(rect
);
517 Context3D context_3d
;
518 if (video_frame
.get() &&
519 video_frame
->format() == VideoFrame::NATIVE_TEXTURE
) {
520 if (!context_3d_cb_
.is_null()) {
521 context_3d
= context_3d_cb_
.Run();
523 // GPU Process crashed.
527 skcanvas_video_renderer_
.Paint(video_frame
, canvas
, gfx_rect
, alpha
, mode
,
528 pipeline_metadata_
.video_rotation
, context_3d
);
531 bool WebMediaPlayerImpl::hasSingleSecurityOrigin() const {
533 return data_source_
->HasSingleOrigin();
537 bool WebMediaPlayerImpl::didPassCORSAccessCheck() const {
539 return data_source_
->DidPassCORSAccessCheck();
543 double WebMediaPlayerImpl::mediaTimeForTimeValue(double timeValue
) const {
544 return ConvertSecondsToTimestamp(timeValue
).InSecondsF();
547 unsigned WebMediaPlayerImpl::decodedFrameCount() const {
548 DCHECK(main_task_runner_
->BelongsToCurrentThread());
550 PipelineStatistics stats
= pipeline_
.GetStatistics();
551 return stats
.video_frames_decoded
;
554 unsigned WebMediaPlayerImpl::droppedFrameCount() const {
555 DCHECK(main_task_runner_
->BelongsToCurrentThread());
557 PipelineStatistics stats
= pipeline_
.GetStatistics();
558 return stats
.video_frames_dropped
;
561 unsigned WebMediaPlayerImpl::audioDecodedByteCount() const {
562 DCHECK(main_task_runner_
->BelongsToCurrentThread());
564 PipelineStatistics stats
= pipeline_
.GetStatistics();
565 return stats
.audio_bytes_decoded
;
568 unsigned WebMediaPlayerImpl::videoDecodedByteCount() const {
569 DCHECK(main_task_runner_
->BelongsToCurrentThread());
571 PipelineStatistics stats
= pipeline_
.GetStatistics();
572 return stats
.video_bytes_decoded
;
575 bool WebMediaPlayerImpl::copyVideoTextureToPlatformTexture(
576 blink::WebGraphicsContext3D
* web_graphics_context
,
577 unsigned int texture
,
579 unsigned int internal_format
,
581 bool premultiply_alpha
,
583 return copyVideoTextureToPlatformTexture(web_graphics_context
, texture
,
584 internal_format
, type
,
585 premultiply_alpha
, flip_y
);
588 bool WebMediaPlayerImpl::copyVideoTextureToPlatformTexture(
589 blink::WebGraphicsContext3D
* web_graphics_context
,
590 unsigned int texture
,
591 unsigned int internal_format
,
593 bool premultiply_alpha
,
595 TRACE_EVENT0("media", "WebMediaPlayerImpl:copyVideoTextureToPlatformTexture");
597 scoped_refptr
<VideoFrame
> video_frame
=
598 GetCurrentFrameFromCompositor();
600 if (!video_frame
.get() ||
601 video_frame
->format() != VideoFrame::NATIVE_TEXTURE
) {
605 // TODO(dshwang): need more elegant way to convert WebGraphicsContext3D to
607 gpu::gles2::GLES2Interface
* gl
=
608 static_cast<gpu_blink::WebGraphicsContext3DImpl
*>(web_graphics_context
)
610 SkCanvasVideoRenderer::CopyVideoFrameTextureToGLTexture(
611 gl
, video_frame
.get(), texture
, internal_format
, type
, premultiply_alpha
,
616 WebMediaPlayer::MediaKeyException
617 WebMediaPlayerImpl::generateKeyRequest(const WebString
& key_system
,
618 const unsigned char* init_data
,
619 unsigned init_data_length
) {
620 DCHECK(main_task_runner_
->BelongsToCurrentThread());
622 return encrypted_media_support_
.GenerateKeyRequest(
623 frame_
, key_system
, init_data
, init_data_length
);
626 WebMediaPlayer::MediaKeyException
WebMediaPlayerImpl::addKey(
627 const WebString
& key_system
,
628 const unsigned char* key
,
630 const unsigned char* init_data
,
631 unsigned init_data_length
,
632 const WebString
& session_id
) {
633 DCHECK(main_task_runner_
->BelongsToCurrentThread());
635 return encrypted_media_support_
.AddKey(
636 key_system
, key
, key_length
, init_data
, init_data_length
, session_id
);
639 WebMediaPlayer::MediaKeyException
WebMediaPlayerImpl::cancelKeyRequest(
640 const WebString
& key_system
,
641 const WebString
& session_id
) {
642 DCHECK(main_task_runner_
->BelongsToCurrentThread());
644 return encrypted_media_support_
.CancelKeyRequest(key_system
, session_id
);
647 void WebMediaPlayerImpl::setContentDecryptionModule(
648 blink::WebContentDecryptionModule
* cdm
,
649 blink::WebContentDecryptionModuleResult result
) {
650 DCHECK(main_task_runner_
->BelongsToCurrentThread());
652 // TODO(xhwang): Support setMediaKeys(0) if necessary: http://crbug.com/330324
654 result
.completeWithError(
655 blink::WebContentDecryptionModuleExceptionNotSupportedError
, 0,
656 "Null MediaKeys object is not supported.");
660 SetCdm(BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnCdmAttached
, result
),
661 ToWebContentDecryptionModuleImpl(cdm
)->GetCdmContext());
664 void WebMediaPlayerImpl::OnEncryptedMediaInitData(
665 EmeInitDataType init_data_type
,
666 const std::vector
<uint8
>& init_data
) {
667 DCHECK(init_data_type
!= EmeInitDataType::UNKNOWN
);
669 // Do not fire "encrypted" event if encrypted media is not enabled.
670 // TODO(xhwang): Handle this in |client_|.
671 if (!blink::WebRuntimeFeatures::isPrefixedEncryptedMediaEnabled() &&
672 !blink::WebRuntimeFeatures::isEncryptedMediaEnabled()) {
676 // TODO(xhwang): Update this UMA name.
677 UMA_HISTOGRAM_COUNTS("Media.EME.NeedKey", 1);
679 encrypted_media_support_
.SetInitDataType(init_data_type
);
681 client_
->encrypted(ConvertToWebInitDataType(init_data_type
),
682 vector_as_array(&init_data
),
683 base::saturated_cast
<unsigned int>(init_data
.size()));
686 void WebMediaPlayerImpl::OnWaitingForDecryptionKey() {
687 client_
->didBlockPlaybackWaitingForKey();
689 // TODO(jrummell): didResumePlaybackBlockedForKey() should only be called
690 // when a key has been successfully added (e.g. OnSessionKeysChange() with
691 // |has_additional_usable_key| = true). http://crbug.com/461903
692 client_
->didResumePlaybackBlockedForKey();
695 void WebMediaPlayerImpl::SetCdm(const CdmAttachedCB
& cdm_attached_cb
,
696 CdmContext
* cdm_context
) {
697 // If CDM initialization succeeded, tell the pipeline about it.
699 pipeline_
.SetCdm(cdm_context
, cdm_attached_cb
);
702 void WebMediaPlayerImpl::OnCdmAttached(
703 blink::WebContentDecryptionModuleResult result
,
710 result
.completeWithError(
711 blink::WebContentDecryptionModuleExceptionNotSupportedError
, 0,
712 "Unable to set MediaKeys object");
715 void WebMediaPlayerImpl::OnPipelineSeeked(bool time_changed
,
716 PipelineStatus status
) {
717 DVLOG(1) << __FUNCTION__
<< "(" << time_changed
<< ", " << status
<< ")";
718 DCHECK(main_task_runner_
->BelongsToCurrentThread());
721 pending_seek_
= false;
722 seek(pending_seek_seconds_
);
726 if (status
!= PIPELINE_OK
) {
727 OnPipelineError(status
);
731 // Update our paused time.
735 should_notify_time_changed_
= time_changed
;
738 void WebMediaPlayerImpl::OnPipelineEnded() {
739 DVLOG(1) << __FUNCTION__
;
740 DCHECK(main_task_runner_
->BelongsToCurrentThread());
742 // Ignore state changes until we've completed all outstanding seeks.
743 if (seeking_
|| pending_seek_
)
747 client_
->timeChanged();
750 void WebMediaPlayerImpl::OnPipelineError(PipelineStatus error
) {
751 DCHECK(main_task_runner_
->BelongsToCurrentThread());
752 DCHECK_NE(error
, PIPELINE_OK
);
754 if (ready_state_
== WebMediaPlayer::ReadyStateHaveNothing
) {
755 // Any error that occurs before reaching ReadyStateHaveMetadata should
756 // be considered a format error.
757 SetNetworkState(WebMediaPlayer::NetworkStateFormatError
);
761 SetNetworkState(PipelineErrorToNetworkState(error
));
763 if (error
== PIPELINE_ERROR_DECRYPT
)
764 encrypted_media_support_
.OnPipelineDecryptError();
767 void WebMediaPlayerImpl::OnPipelineMetadata(
768 PipelineMetadata metadata
) {
769 DVLOG(1) << __FUNCTION__
;
771 pipeline_metadata_
= metadata
;
773 UMA_HISTOGRAM_ENUMERATION("Media.VideoRotation", metadata
.video_rotation
,
774 VIDEO_ROTATION_MAX
+ 1);
775 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata
);
778 DCHECK(!video_weblayer_
);
779 scoped_refptr
<cc::VideoLayer
> layer
=
780 cc::VideoLayer::Create(cc_blink::WebLayerImpl::LayerSettings(),
781 compositor_
, pipeline_metadata_
.video_rotation
);
783 if (pipeline_metadata_
.video_rotation
== VIDEO_ROTATION_90
||
784 pipeline_metadata_
.video_rotation
== VIDEO_ROTATION_270
) {
785 gfx::Size size
= pipeline_metadata_
.natural_size
;
786 pipeline_metadata_
.natural_size
= gfx::Size(size
.height(), size
.width());
789 video_weblayer_
.reset(new cc_blink::WebLayerImpl(layer
));
790 video_weblayer_
->setOpaque(opaque_
);
791 client_
->setWebLayer(video_weblayer_
.get());
795 void WebMediaPlayerImpl::OnPipelineBufferingStateChanged(
796 BufferingState buffering_state
) {
797 DVLOG(1) << __FUNCTION__
<< "(" << buffering_state
<< ")";
799 // Ignore buffering state changes until we've completed all outstanding seeks.
800 if (seeking_
|| pending_seek_
)
803 // TODO(scherkus): Handle other buffering states when Pipeline starts using
804 // them and translate them ready state changes http://crbug.com/144683
805 DCHECK_EQ(buffering_state
, BUFFERING_HAVE_ENOUGH
);
806 SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData
);
808 // Let the DataSource know we have enough data. It may use this information to
809 // release unused network connections.
811 data_source_
->OnBufferingHaveEnough();
813 // Blink expects a timeChanged() in response to a seek().
814 if (should_notify_time_changed_
)
815 client_
->timeChanged();
818 void WebMediaPlayerImpl::OnDemuxerOpened() {
819 DCHECK(main_task_runner_
->BelongsToCurrentThread());
820 client_
->mediaSourceOpened(new WebMediaSourceImpl(
821 chunk_demuxer_
, base::Bind(&MediaLog::AddLogEvent
, media_log_
)));
824 void WebMediaPlayerImpl::OnAddTextTrack(
825 const TextTrackConfig
& config
,
826 const AddTextTrackDoneCB
& done_cb
) {
827 DCHECK(main_task_runner_
->BelongsToCurrentThread());
829 const WebInbandTextTrackImpl::Kind web_kind
=
830 static_cast<WebInbandTextTrackImpl::Kind
>(config
.kind());
831 const blink::WebString web_label
=
832 blink::WebString::fromUTF8(config
.label());
833 const blink::WebString web_language
=
834 blink::WebString::fromUTF8(config
.language());
835 const blink::WebString web_id
=
836 blink::WebString::fromUTF8(config
.id());
838 scoped_ptr
<WebInbandTextTrackImpl
> web_inband_text_track(
839 new WebInbandTextTrackImpl(web_kind
, web_label
, web_language
, web_id
));
841 scoped_ptr
<TextTrack
> text_track(new TextTrackImpl(
842 main_task_runner_
, client_
, web_inband_text_track
.Pass()));
844 done_cb
.Run(text_track
.Pass());
847 void WebMediaPlayerImpl::DataSourceInitialized(bool success
) {
848 DCHECK(main_task_runner_
->BelongsToCurrentThread());
851 SetNetworkState(WebMediaPlayer::NetworkStateFormatError
);
858 void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading
) {
859 if (!is_downloading
&& network_state_
== WebMediaPlayer::NetworkStateLoading
)
860 SetNetworkState(WebMediaPlayer::NetworkStateIdle
);
861 else if (is_downloading
&& network_state_
== WebMediaPlayer::NetworkStateIdle
)
862 SetNetworkState(WebMediaPlayer::NetworkStateLoading
);
863 media_log_
->AddEvent(
864 media_log_
->CreateBooleanEvent(
865 MediaLogEvent::NETWORK_ACTIVITY_SET
,
866 "is_downloading_data", is_downloading
));
869 void WebMediaPlayerImpl::StartPipeline() {
870 DCHECK(main_task_runner_
->BelongsToCurrentThread());
872 // Keep track if this is a MSE or non-MSE playback.
873 UMA_HISTOGRAM_BOOLEAN("Media.MSE.Playback",
874 (load_type_
== LoadTypeMediaSource
));
877 Demuxer::EncryptedMediaInitDataCB encrypted_media_init_data_cb
=
878 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnEncryptedMediaInitData
);
880 // Figure out which demuxer to use.
881 if (load_type_
!= LoadTypeMediaSource
) {
882 DCHECK(!chunk_demuxer_
);
883 DCHECK(data_source_
);
885 demuxer_
.reset(new FFmpegDemuxer(media_task_runner_
, data_source_
.get(),
886 encrypted_media_init_data_cb
, media_log_
));
888 DCHECK(!chunk_demuxer_
);
889 DCHECK(!data_source_
);
891 mse_log_cb
= base::Bind(&MediaLog::AddLogEvent
, media_log_
);
893 chunk_demuxer_
= new ChunkDemuxer(
894 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDemuxerOpened
),
895 encrypted_media_init_data_cb
, mse_log_cb
, media_log_
, true);
896 demuxer_
.reset(chunk_demuxer_
);
899 // ... and we're ready to go!
904 renderer_factory_
->CreateRenderer(
905 media_task_runner_
, audio_source_provider_
.get(), compositor_
),
906 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineEnded
),
907 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineError
),
908 BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnPipelineSeeked
, false),
909 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineMetadata
),
910 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineBufferingStateChanged
),
911 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDurationChanged
),
912 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnAddTextTrack
),
913 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnWaitingForDecryptionKey
));
916 void WebMediaPlayerImpl::SetNetworkState(WebMediaPlayer::NetworkState state
) {
917 DVLOG(1) << __FUNCTION__
<< "(" << state
<< ")";
918 DCHECK(main_task_runner_
->BelongsToCurrentThread());
919 network_state_
= state
;
920 // Always notify to ensure client has the latest value.
921 client_
->networkStateChanged();
924 void WebMediaPlayerImpl::SetReadyState(WebMediaPlayer::ReadyState state
) {
925 DVLOG(1) << __FUNCTION__
<< "(" << state
<< ")";
926 DCHECK(main_task_runner_
->BelongsToCurrentThread());
928 if (state
== WebMediaPlayer::ReadyStateHaveEnoughData
&& data_source_
&&
929 data_source_
->assume_fully_buffered() &&
930 network_state_
== WebMediaPlayer::NetworkStateLoading
)
931 SetNetworkState(WebMediaPlayer::NetworkStateLoaded
);
933 ready_state_
= state
;
934 // Always notify to ensure client has the latest value.
935 client_
->readyStateChanged();
938 blink::WebAudioSourceProvider
* WebMediaPlayerImpl::audioSourceProvider() {
939 return audio_source_provider_
.get();
942 double WebMediaPlayerImpl::GetPipelineDuration() const {
943 base::TimeDelta duration
= pipeline_
.GetMediaDuration();
945 // Return positive infinity if the resource is unbounded.
946 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
947 if (duration
== kInfiniteDuration())
948 return std::numeric_limits
<double>::infinity();
950 return duration
.InSecondsF();
953 void WebMediaPlayerImpl::OnDurationChanged() {
954 if (ready_state_
== WebMediaPlayer::ReadyStateHaveNothing
)
957 client_
->durationChanged();
960 void WebMediaPlayerImpl::OnNaturalSizeChanged(gfx::Size size
) {
961 DCHECK(main_task_runner_
->BelongsToCurrentThread());
962 DCHECK_NE(ready_state_
, WebMediaPlayer::ReadyStateHaveNothing
);
963 TRACE_EVENT0("media", "WebMediaPlayerImpl::OnNaturalSizeChanged");
965 media_log_
->AddEvent(
966 media_log_
->CreateVideoSizeSetEvent(size
.width(), size
.height()));
967 pipeline_metadata_
.natural_size
= size
;
969 client_
->sizeChanged();
972 void WebMediaPlayerImpl::OnOpacityChanged(bool opaque
) {
973 DCHECK(main_task_runner_
->BelongsToCurrentThread());
974 DCHECK_NE(ready_state_
, WebMediaPlayer::ReadyStateHaveNothing
);
978 video_weblayer_
->setOpaque(opaque_
);
981 static void GetCurrentFrameAndSignal(
982 VideoFrameCompositor
* compositor
,
983 scoped_refptr
<VideoFrame
>* video_frame_out
,
984 base::WaitableEvent
* event
) {
985 TRACE_EVENT0("media", "GetCurrentFrameAndSignal");
986 *video_frame_out
= compositor
->GetCurrentFrameAndUpdateIfStale();
990 scoped_refptr
<VideoFrame
>
991 WebMediaPlayerImpl::GetCurrentFrameFromCompositor() {
992 TRACE_EVENT0("media", "WebMediaPlayerImpl::GetCurrentFrameFromCompositor");
993 if (compositor_task_runner_
->BelongsToCurrentThread())
994 return compositor_
->GetCurrentFrameAndUpdateIfStale();
996 // Use a posted task and waitable event instead of a lock otherwise
997 // WebGL/Canvas can see different content than what the compositor is seeing.
998 scoped_refptr
<VideoFrame
> video_frame
;
999 base::WaitableEvent
event(false, false);
1000 compositor_task_runner_
->PostTask(FROM_HERE
,
1001 base::Bind(&GetCurrentFrameAndSignal
,
1002 base::Unretained(compositor_
),
1009 void WebMediaPlayerImpl::UpdatePausedTime() {
1010 DCHECK(main_task_runner_
->BelongsToCurrentThread());
1012 // pause() may be called after playback has ended and the HTMLMediaElement
1013 // requires that currentTime() == duration() after ending. We want to ensure
1014 // |paused_time_| matches currentTime() in this case or a future seek() may
1015 // incorrectly discard what it thinks is a seek to the existing time.
1017 ended_
? pipeline_
.GetMediaDuration() : pipeline_
.GetMediaTime();
1020 } // namespace media