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/WebMediaPlayerClient.h"
42 #include "third_party/WebKit/public/platform/WebMediaPlayerEncryptedMediaClient.h"
43 #include "third_party/WebKit/public/platform/WebMediaSource.h"
44 #include "third_party/WebKit/public/platform/WebRect.h"
45 #include "third_party/WebKit/public/platform/WebSize.h"
46 #include "third_party/WebKit/public/platform/WebString.h"
47 #include "third_party/WebKit/public/platform/WebURL.h"
48 #include "third_party/WebKit/public/web/WebDocument.h"
49 #include "third_party/WebKit/public/web/WebFrame.h"
50 #include "third_party/WebKit/public/web/WebLocalFrame.h"
51 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
52 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
53 #include "third_party/WebKit/public/web/WebView.h"
55 using blink::WebCanvas
;
56 using blink::WebMediaPlayer
;
59 using blink::WebString
;
63 // Limits the range of playback rate.
65 // TODO(kylep): Revisit these.
67 // Vista has substantially lower performance than XP or Windows7. If you speed
68 // up a video too much, it can't keep up, and rendering stops updating except on
69 // the time bar. For really high speeds, audio becomes a bottleneck and we just
70 // use up the data we have, which may not achieve the speed requested, but will
73 // A very slow speed, ie 0.00000001x, causes the machine to lock up. (It seems
74 // like a busy loop). It gets unresponsive, although its not completely dead.
76 // Also our timers are not very accurate (especially for ogg), which becomes
77 // evident at low speeds and on Vista. Since other speeds are risky and outside
78 // the norms, we think 1/16x to 16x is a safe and useful range for now.
79 const double kMinRate
= 0.0625;
80 const double kMaxRate
= 16.0;
86 class BufferedDataSourceHostImpl
;
88 #define STATIC_ASSERT_MATCHING_ENUM(name) \
89 static_assert(static_cast<int>(WebMediaPlayer::CORSMode ## name) == \
90 static_cast<int>(BufferedResourceLoader::k ## name), \
91 "mismatching enum values: " #name)
92 STATIC_ASSERT_MATCHING_ENUM(Unspecified
);
93 STATIC_ASSERT_MATCHING_ENUM(Anonymous
);
94 STATIC_ASSERT_MATCHING_ENUM(UseCredentials
);
95 #undef STATIC_ASSERT_MATCHING_ENUM
97 #define BIND_TO_RENDER_LOOP(function) \
98 (DCHECK(main_task_runner_->BelongsToCurrentThread()), \
99 BindToCurrentLoop(base::Bind(function, AsWeakPtr())))
101 #define BIND_TO_RENDER_LOOP1(function, arg1) \
102 (DCHECK(main_task_runner_->BelongsToCurrentThread()), \
103 BindToCurrentLoop(base::Bind(function, AsWeakPtr(), arg1)))
105 WebMediaPlayerImpl::WebMediaPlayerImpl(
106 blink::WebLocalFrame
* frame
,
107 blink::WebMediaPlayerClient
* client
,
108 blink::WebMediaPlayerEncryptedMediaClient
* encrypted_client
,
109 base::WeakPtr
<WebMediaPlayerDelegate
> delegate
,
110 scoped_ptr
<RendererFactory
> renderer_factory
,
111 CdmFactory
* cdm_factory
,
112 const WebMediaPlayerParams
& params
)
114 network_state_(WebMediaPlayer::NetworkStateEmpty
),
115 ready_state_(WebMediaPlayer::ReadyStateHaveNothing
),
116 preload_(BufferedDataSource::AUTO
),
117 main_task_runner_(base::ThreadTaskRunnerHandle::Get()),
118 media_task_runner_(params
.media_task_runner()),
119 media_log_(params
.media_log()),
120 pipeline_(media_task_runner_
, media_log_
.get()),
121 load_type_(LoadTypeURL
),
127 pending_seek_(false),
128 should_notify_time_changed_(false),
130 encrypted_client_(encrypted_client
),
132 defer_load_cb_(params
.defer_load_cb()),
133 context_3d_cb_(params
.context_3d_cb()),
134 supports_save_(true),
135 chunk_demuxer_(NULL
),
136 // Threaded compositing isn't enabled universally yet.
137 compositor_task_runner_(
138 params
.compositor_task_runner()
139 ? params
.compositor_task_runner()
140 : base::MessageLoop::current()->task_runner()),
141 compositor_(new VideoFrameCompositor(
142 compositor_task_runner_
,
143 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNaturalSizeChanged
),
144 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnOpacityChanged
))),
145 encrypted_media_support_(cdm_factory
,
147 params
.media_permission(),
148 base::Bind(&WebMediaPlayerImpl::SetCdm
,
150 base::Bind(&IgnoreCdmAttached
))),
151 renderer_factory_(renderer_factory
.Pass()) {
152 media_log_
->AddEvent(
153 media_log_
->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_CREATED
));
155 if (params
.initial_cdm()) {
156 SetCdm(base::Bind(&IgnoreCdmAttached
),
157 ToWebContentDecryptionModuleImpl(params
.initial_cdm())
161 // TODO(xhwang): When we use an external Renderer, many methods won't work,
162 // e.g. GetCurrentFrameFromCompositor(). See http://crbug.com/434861
164 // Use the null sink if no sink was provided.
165 audio_source_provider_
= new WebAudioSourceProviderImpl(
166 params
.audio_renderer_sink().get()
167 ? params
.audio_renderer_sink()
168 : new NullAudioSink(media_task_runner_
));
171 WebMediaPlayerImpl::~WebMediaPlayerImpl() {
172 client_
->setWebLayer(NULL
);
174 DCHECK(main_task_runner_
->BelongsToCurrentThread());
177 delegate_
->PlayerGone(this);
179 // Abort any pending IO so stopping the pipeline doesn't get blocked.
181 data_source_
->Abort();
182 if (chunk_demuxer_
) {
183 chunk_demuxer_
->Shutdown();
184 chunk_demuxer_
= NULL
;
187 renderer_factory_
.reset();
189 // Make sure to kill the pipeline so there's no more media threads running.
190 // Note: stopping the pipeline might block for a long time.
191 base::WaitableEvent
waiter(false, false);
193 base::Bind(&base::WaitableEvent::Signal
, base::Unretained(&waiter
)));
196 compositor_task_runner_
->DeleteSoon(FROM_HERE
, compositor_
);
198 media_log_
->AddEvent(
199 media_log_
->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_DESTROYED
));
202 void WebMediaPlayerImpl::load(LoadType load_type
, const blink::WebURL
& url
,
203 CORSMode cors_mode
) {
204 DVLOG(1) << __FUNCTION__
<< "(" << load_type
<< ", " << url
<< ", "
206 if (!defer_load_cb_
.is_null()) {
207 defer_load_cb_
.Run(base::Bind(
208 &WebMediaPlayerImpl::DoLoad
, AsWeakPtr(), load_type
, url
, cors_mode
));
211 DoLoad(load_type
, url
, cors_mode
);
214 void WebMediaPlayerImpl::DoLoad(LoadType load_type
,
215 const blink::WebURL
& url
,
216 CORSMode cors_mode
) {
217 DCHECK(main_task_runner_
->BelongsToCurrentThread());
220 ReportMetrics(load_type
, gurl
,
221 GURL(frame_
->document().securityOrigin().toString()));
223 // Set subresource URL for crash reporting.
224 base::debug::SetCrashKeyValue("subresource_url", gurl
.spec());
226 load_type_
= load_type
;
228 SetNetworkState(WebMediaPlayer::NetworkStateLoading
);
229 SetReadyState(WebMediaPlayer::ReadyStateHaveNothing
);
230 media_log_
->AddEvent(media_log_
->CreateLoadEvent(url
.spec()));
232 // Media source pipelines can start immediately.
233 if (load_type
== LoadTypeMediaSource
) {
234 supports_save_
= false;
239 // Otherwise it's a regular request which requires resolving the URL first.
240 data_source_
.reset(new BufferedDataSource(
242 static_cast<BufferedResourceLoader::CORSMode
>(cors_mode
),
246 &buffered_data_source_host_
,
247 base::Bind(&WebMediaPlayerImpl::NotifyDownloading
, AsWeakPtr())));
248 data_source_
->SetPreload(preload_
);
249 data_source_
->Initialize(
250 base::Bind(&WebMediaPlayerImpl::DataSourceInitialized
, AsWeakPtr()));
253 void WebMediaPlayerImpl::play() {
254 DVLOG(1) << __FUNCTION__
;
255 DCHECK(main_task_runner_
->BelongsToCurrentThread());
258 pipeline_
.SetPlaybackRate(playback_rate_
);
260 data_source_
->MediaIsPlaying();
262 media_log_
->AddEvent(media_log_
->CreateEvent(MediaLogEvent::PLAY
));
264 if (delegate_
&& playback_rate_
> 0)
265 delegate_
->DidPlay(this);
268 void WebMediaPlayerImpl::pause() {
269 DVLOG(1) << __FUNCTION__
;
270 DCHECK(main_task_runner_
->BelongsToCurrentThread());
272 const bool was_already_paused
= paused_
|| playback_rate_
== 0;
274 pipeline_
.SetPlaybackRate(0.0);
276 data_source_
->MediaIsPaused();
279 media_log_
->AddEvent(media_log_
->CreateEvent(MediaLogEvent::PAUSE
));
281 if (!was_already_paused
&& delegate_
)
282 delegate_
->DidPause(this);
285 bool WebMediaPlayerImpl::supportsSave() const {
286 DCHECK(main_task_runner_
->BelongsToCurrentThread());
287 return supports_save_
;
290 void WebMediaPlayerImpl::seek(double seconds
) {
291 DVLOG(1) << __FUNCTION__
<< "(" << seconds
<< "s)";
292 DCHECK(main_task_runner_
->BelongsToCurrentThread());
296 ReadyState old_state
= ready_state_
;
297 if (ready_state_
> WebMediaPlayer::ReadyStateHaveMetadata
)
298 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata
);
300 base::TimeDelta new_seek_time
= ConvertSecondsToTimestamp(seconds
);
303 if (new_seek_time
== seek_time_
) {
304 if (chunk_demuxer_
) {
305 if (!pending_seek_
) {
306 // If using media source demuxer, only suppress redundant seeks if
307 // there is no pending seek. This enforces that any pending seek that
308 // results in a demuxer seek is preceded by matching
309 // CancelPendingSeek() and StartWaitingForSeek() calls.
313 // Suppress all redundant seeks if unrestricted by media source demuxer
315 pending_seek_
= false;
316 pending_seek_time_
= base::TimeDelta();
321 pending_seek_
= true;
322 pending_seek_time_
= new_seek_time
;
324 chunk_demuxer_
->CancelPendingSeek(pending_seek_time_
);
328 media_log_
->AddEvent(media_log_
->CreateSeekEvent(seconds
));
330 // Update our paused time.
331 // In paused state ignore the seek operations to current time if the loading
332 // is completed and generate OnPipelineBufferingStateChanged event to
333 // eventually fire seeking and seeked events
335 if (paused_time_
!= new_seek_time
) {
336 paused_time_
= new_seek_time
;
337 } else if (old_state
== ReadyStateHaveEnoughData
) {
338 main_task_runner_
->PostTask(
340 base::Bind(&WebMediaPlayerImpl::OnPipelineBufferingStateChanged
,
341 AsWeakPtr(), BUFFERING_HAVE_ENOUGH
));
347 seek_time_
= new_seek_time
;
350 chunk_demuxer_
->StartWaitingForSeek(seek_time_
);
352 // Kick off the asynchronous seek!
353 pipeline_
.Seek(seek_time_
, BIND_TO_RENDER_LOOP1(
354 &WebMediaPlayerImpl::OnPipelineSeeked
, true));
357 void WebMediaPlayerImpl::setRate(double rate
) {
358 DVLOG(1) << __FUNCTION__
<< "(" << rate
<< ")";
359 DCHECK(main_task_runner_
->BelongsToCurrentThread());
361 // TODO(kylep): Remove when support for negatives is added. Also, modify the
362 // following checks so rewind uses reasonable values also.
366 // Limit rates to reasonable values by clamping.
370 else if (rate
> kMaxRate
)
372 if (playback_rate_
== 0 && !paused_
&& delegate_
)
373 delegate_
->DidPlay(this);
374 } else if (playback_rate_
!= 0 && !paused_
&& delegate_
) {
375 delegate_
->DidPause(this);
378 playback_rate_
= rate
;
380 pipeline_
.SetPlaybackRate(rate
);
382 data_source_
->MediaPlaybackRateChanged(rate
);
386 void WebMediaPlayerImpl::setVolume(double volume
) {
387 DVLOG(1) << __FUNCTION__
<< "(" << volume
<< ")";
388 DCHECK(main_task_runner_
->BelongsToCurrentThread());
390 pipeline_
.SetVolume(volume
);
393 void WebMediaPlayerImpl::setSinkId(const blink::WebString
& device_id
,
394 WebSetSinkIdCB
* web_callback
) {
395 DCHECK(main_task_runner_
->BelongsToCurrentThread());
396 DVLOG(1) << __FUNCTION__
;
397 media::SwitchOutputDeviceCB callback
=
398 media::ConvertToSwitchOutputDeviceCB(web_callback
);
399 OutputDevice
* output_device
= audio_source_provider_
->GetOutputDevice();
401 std::string
device_id_str(device_id
.utf8());
402 GURL
security_origin(frame_
->securityOrigin().toString().utf8());
403 output_device
->SwitchOutputDevice(device_id_str
, security_origin
, callback
);
405 callback
.Run(SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_SUPPORTED
);
409 #define STATIC_ASSERT_MATCHING_ENUM(webkit_name, chromium_name) \
410 static_assert(static_cast<int>(WebMediaPlayer::webkit_name) == \
411 static_cast<int>(BufferedDataSource::chromium_name), \
412 "mismatching enum values: " #webkit_name)
413 STATIC_ASSERT_MATCHING_ENUM(PreloadNone
, NONE
);
414 STATIC_ASSERT_MATCHING_ENUM(PreloadMetaData
, METADATA
);
415 STATIC_ASSERT_MATCHING_ENUM(PreloadAuto
, AUTO
);
416 #undef STATIC_ASSERT_MATCHING_ENUM
418 void WebMediaPlayerImpl::setPreload(WebMediaPlayer::Preload preload
) {
419 DVLOG(1) << __FUNCTION__
<< "(" << preload
<< ")";
420 DCHECK(main_task_runner_
->BelongsToCurrentThread());
422 preload_
= static_cast<BufferedDataSource::Preload
>(preload
);
424 data_source_
->SetPreload(preload_
);
427 bool WebMediaPlayerImpl::hasVideo() const {
428 DCHECK(main_task_runner_
->BelongsToCurrentThread());
430 return pipeline_metadata_
.has_video
;
433 bool WebMediaPlayerImpl::hasAudio() const {
434 DCHECK(main_task_runner_
->BelongsToCurrentThread());
436 return pipeline_metadata_
.has_audio
;
439 blink::WebSize
WebMediaPlayerImpl::naturalSize() const {
440 DCHECK(main_task_runner_
->BelongsToCurrentThread());
442 return blink::WebSize(pipeline_metadata_
.natural_size
);
445 bool WebMediaPlayerImpl::paused() const {
446 DCHECK(main_task_runner_
->BelongsToCurrentThread());
448 return pipeline_
.GetPlaybackRate() == 0.0f
;
451 bool WebMediaPlayerImpl::seeking() const {
452 DCHECK(main_task_runner_
->BelongsToCurrentThread());
454 if (ready_state_
== WebMediaPlayer::ReadyStateHaveNothing
)
460 double WebMediaPlayerImpl::duration() const {
461 DCHECK(main_task_runner_
->BelongsToCurrentThread());
463 if (ready_state_
== WebMediaPlayer::ReadyStateHaveNothing
)
464 return std::numeric_limits
<double>::quiet_NaN();
466 return GetPipelineDuration();
469 double WebMediaPlayerImpl::timelineOffset() const {
470 DCHECK(main_task_runner_
->BelongsToCurrentThread());
472 if (pipeline_metadata_
.timeline_offset
.is_null())
473 return std::numeric_limits
<double>::quiet_NaN();
475 return pipeline_metadata_
.timeline_offset
.ToJsTime();
478 double WebMediaPlayerImpl::currentTime() const {
479 DCHECK(main_task_runner_
->BelongsToCurrentThread());
480 DCHECK_NE(ready_state_
, WebMediaPlayer::ReadyStateHaveNothing
);
482 // TODO(scherkus): Replace with an explicit ended signal to HTMLMediaElement,
483 // see http://crbug.com/409280
487 // We know the current seek time better than pipeline: pipeline may processing
488 // an earlier seek before a pending seek has been started, or it might not yet
489 // have the current seek time returnable via GetMediaTime().
491 return pending_seek_
? pending_seek_time_
.InSecondsF()
492 : seek_time_
.InSecondsF();
495 return (paused_
? paused_time_
: pipeline_
.GetMediaTime()).InSecondsF();
498 WebMediaPlayer::NetworkState
WebMediaPlayerImpl::networkState() const {
499 DCHECK(main_task_runner_
->BelongsToCurrentThread());
500 return network_state_
;
503 WebMediaPlayer::ReadyState
WebMediaPlayerImpl::readyState() const {
504 DCHECK(main_task_runner_
->BelongsToCurrentThread());
508 blink::WebTimeRanges
WebMediaPlayerImpl::buffered() const {
509 DCHECK(main_task_runner_
->BelongsToCurrentThread());
511 Ranges
<base::TimeDelta
> buffered_time_ranges
=
512 pipeline_
.GetBufferedTimeRanges();
514 const base::TimeDelta duration
= pipeline_
.GetMediaDuration();
515 if (duration
!= kInfiniteDuration()) {
516 buffered_data_source_host_
.AddBufferedTimeRanges(
517 &buffered_time_ranges
, duration
);
519 return ConvertToWebTimeRanges(buffered_time_ranges
);
522 blink::WebTimeRanges
WebMediaPlayerImpl::seekable() const {
523 DCHECK(main_task_runner_
->BelongsToCurrentThread());
525 if (ready_state_
< WebMediaPlayer::ReadyStateHaveMetadata
)
526 return blink::WebTimeRanges();
528 const double seekable_end
= duration();
530 // Allow a special exception for seeks to zero for streaming sources with a
531 // finite duration; this allows looping to work.
532 const bool allow_seek_to_zero
= data_source_
&& data_source_
->IsStreaming() &&
533 std::isfinite(seekable_end
);
535 // TODO(dalecurtis): Technically this allows seeking on media which return an
536 // infinite duration so long as DataSource::IsStreaming() is false. While not
537 // expected, disabling this breaks semi-live players, http://crbug.com/427412.
538 const blink::WebTimeRange
seekable_range(
539 0.0, allow_seek_to_zero
? 0.0 : seekable_end
);
540 return blink::WebTimeRanges(&seekable_range
, 1);
543 bool WebMediaPlayerImpl::didLoadingProgress() {
544 DCHECK(main_task_runner_
->BelongsToCurrentThread());
545 bool pipeline_progress
= pipeline_
.DidLoadingProgress();
546 bool data_progress
= buffered_data_source_host_
.DidLoadingProgress();
547 return pipeline_progress
|| data_progress
;
550 void WebMediaPlayerImpl::paint(blink::WebCanvas
* canvas
,
551 const blink::WebRect
& rect
,
553 SkXfermode::Mode mode
) {
554 DCHECK(main_task_runner_
->BelongsToCurrentThread());
555 TRACE_EVENT0("media", "WebMediaPlayerImpl:paint");
557 // TODO(scherkus): Clarify paint() API contract to better understand when and
558 // why it's being called. For example, today paint() is called when:
559 // - We haven't reached HAVE_CURRENT_DATA and need to paint black
560 // - We're painting to a canvas
561 // See http://crbug.com/341225 http://crbug.com/342621 for details.
562 scoped_refptr
<VideoFrame
> video_frame
= GetCurrentFrameFromCompositor();
564 gfx::Rect
gfx_rect(rect
);
565 Context3D context_3d
;
566 if (video_frame
.get() && video_frame
->HasTextures()) {
567 if (!context_3d_cb_
.is_null())
568 context_3d
= context_3d_cb_
.Run();
569 // GPU Process crashed.
573 skcanvas_video_renderer_
.Paint(video_frame
, canvas
, gfx_rect
, alpha
, mode
,
574 pipeline_metadata_
.video_rotation
, context_3d
);
577 bool WebMediaPlayerImpl::hasSingleSecurityOrigin() const {
579 return data_source_
->HasSingleOrigin();
583 bool WebMediaPlayerImpl::didPassCORSAccessCheck() const {
585 return data_source_
->DidPassCORSAccessCheck();
589 double WebMediaPlayerImpl::mediaTimeForTimeValue(double timeValue
) const {
590 return ConvertSecondsToTimestamp(timeValue
).InSecondsF();
593 unsigned WebMediaPlayerImpl::decodedFrameCount() const {
594 DCHECK(main_task_runner_
->BelongsToCurrentThread());
596 PipelineStatistics stats
= pipeline_
.GetStatistics();
597 return stats
.video_frames_decoded
;
600 unsigned WebMediaPlayerImpl::droppedFrameCount() const {
601 DCHECK(main_task_runner_
->BelongsToCurrentThread());
603 PipelineStatistics stats
= pipeline_
.GetStatistics();
604 return stats
.video_frames_dropped
;
607 unsigned WebMediaPlayerImpl::audioDecodedByteCount() const {
608 DCHECK(main_task_runner_
->BelongsToCurrentThread());
610 PipelineStatistics stats
= pipeline_
.GetStatistics();
611 return stats
.audio_bytes_decoded
;
614 unsigned WebMediaPlayerImpl::videoDecodedByteCount() const {
615 DCHECK(main_task_runner_
->BelongsToCurrentThread());
617 PipelineStatistics stats
= pipeline_
.GetStatistics();
618 return stats
.video_bytes_decoded
;
621 bool WebMediaPlayerImpl::copyVideoTextureToPlatformTexture(
622 blink::WebGraphicsContext3D
* web_graphics_context
,
623 unsigned int texture
,
624 unsigned int internal_format
,
626 bool premultiply_alpha
,
628 TRACE_EVENT0("media", "WebMediaPlayerImpl:copyVideoTextureToPlatformTexture");
630 scoped_refptr
<VideoFrame
> video_frame
= GetCurrentFrameFromCompositor();
632 if (!video_frame
.get() || !video_frame
->HasTextures() ||
633 media::VideoFrame::NumPlanes(video_frame
->format()) != 1) {
637 // TODO(dshwang): need more elegant way to convert WebGraphicsContext3D to
639 gpu::gles2::GLES2Interface
* gl
=
640 static_cast<gpu_blink::WebGraphicsContext3DImpl
*>(web_graphics_context
)
642 SkCanvasVideoRenderer::CopyVideoFrameSingleTextureToGLTexture(
643 gl
, video_frame
.get(), texture
, internal_format
, type
, premultiply_alpha
,
648 WebMediaPlayer::MediaKeyException
649 WebMediaPlayerImpl::generateKeyRequest(const WebString
& key_system
,
650 const unsigned char* init_data
,
651 unsigned init_data_length
) {
652 DCHECK(main_task_runner_
->BelongsToCurrentThread());
654 return encrypted_media_support_
.GenerateKeyRequest(
655 frame_
, key_system
, init_data
, init_data_length
);
658 WebMediaPlayer::MediaKeyException
WebMediaPlayerImpl::addKey(
659 const WebString
& key_system
,
660 const unsigned char* key
,
662 const unsigned char* init_data
,
663 unsigned init_data_length
,
664 const WebString
& session_id
) {
665 DCHECK(main_task_runner_
->BelongsToCurrentThread());
667 return encrypted_media_support_
.AddKey(
668 key_system
, key
, key_length
, init_data
, init_data_length
, session_id
);
671 WebMediaPlayer::MediaKeyException
WebMediaPlayerImpl::cancelKeyRequest(
672 const WebString
& key_system
,
673 const WebString
& session_id
) {
674 DCHECK(main_task_runner_
->BelongsToCurrentThread());
676 return encrypted_media_support_
.CancelKeyRequest(key_system
, session_id
);
679 void WebMediaPlayerImpl::setContentDecryptionModule(
680 blink::WebContentDecryptionModule
* cdm
,
681 blink::WebContentDecryptionModuleResult result
) {
682 DCHECK(main_task_runner_
->BelongsToCurrentThread());
684 // Once the CDM is set it can't be cleared as there may be frames being
685 // decrypted on other threads. So fail this request.
686 // http://crbug.com/462365#c7.
688 result
.completeWithError(
689 blink::WebContentDecryptionModuleExceptionInvalidStateError
, 0,
690 "The existing MediaKeys object cannot be removed at this time.");
694 // Although unlikely, it is possible that multiple calls happen
695 // simultaneously, so fail this call if there is already one pending.
696 if (set_cdm_result_
) {
697 result
.completeWithError(
698 blink::WebContentDecryptionModuleExceptionInvalidStateError
, 0,
699 "Unable to set MediaKeys object at this time.");
703 // Create a local copy of |result| to avoid problems with the callback
704 // getting passed to the media thread and causing |result| to be destructed
705 // on the wrong thread in some failure conditions.
706 set_cdm_result_
.reset(new blink::WebContentDecryptionModuleResult(result
));
708 SetCdm(BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnCdmAttached
),
709 ToWebContentDecryptionModuleImpl(cdm
)->GetCdmContext());
712 void WebMediaPlayerImpl::OnEncryptedMediaInitData(
713 EmeInitDataType init_data_type
,
714 const std::vector
<uint8
>& init_data
) {
715 DCHECK(init_data_type
!= EmeInitDataType::UNKNOWN
);
717 // Do not fire "encrypted" event if encrypted media is not enabled.
718 // TODO(xhwang): Handle this in |client_|.
719 if (!blink::WebRuntimeFeatures::isPrefixedEncryptedMediaEnabled() &&
720 !blink::WebRuntimeFeatures::isEncryptedMediaEnabled()) {
724 // TODO(xhwang): Update this UMA name.
725 UMA_HISTOGRAM_COUNTS("Media.EME.NeedKey", 1);
727 encrypted_media_support_
.SetInitDataType(init_data_type
);
729 encrypted_client_
->encrypted(
730 ConvertToWebInitDataType(init_data_type
), vector_as_array(&init_data
),
731 base::saturated_cast
<unsigned int>(init_data
.size()));
734 void WebMediaPlayerImpl::OnWaitingForDecryptionKey() {
735 encrypted_client_
->didBlockPlaybackWaitingForKey();
737 // TODO(jrummell): didResumePlaybackBlockedForKey() should only be called
738 // when a key has been successfully added (e.g. OnSessionKeysChange() with
739 // |has_additional_usable_key| = true). http://crbug.com/461903
740 encrypted_client_
->didResumePlaybackBlockedForKey();
743 void WebMediaPlayerImpl::SetCdm(const CdmAttachedCB
& cdm_attached_cb
,
744 CdmContext
* cdm_context
) {
745 // If CDM initialization succeeded, tell the pipeline about it.
747 pipeline_
.SetCdm(cdm_context
, cdm_attached_cb
);
750 void WebMediaPlayerImpl::OnCdmAttached(bool success
) {
752 set_cdm_result_
->complete();
753 set_cdm_result_
.reset();
757 set_cdm_result_
->completeWithError(
758 blink::WebContentDecryptionModuleExceptionNotSupportedError
, 0,
759 "Unable to set MediaKeys object");
760 set_cdm_result_
.reset();
763 void WebMediaPlayerImpl::OnPipelineSeeked(bool time_changed
,
764 PipelineStatus status
) {
765 DVLOG(1) << __FUNCTION__
<< "(" << time_changed
<< ", " << status
<< ")";
766 DCHECK(main_task_runner_
->BelongsToCurrentThread());
768 seek_time_
= base::TimeDelta();
770 double pending_seek_seconds
= pending_seek_time_
.InSecondsF();
771 pending_seek_
= false;
772 pending_seek_time_
= base::TimeDelta();
773 seek(pending_seek_seconds
);
777 if (status
!= PIPELINE_OK
) {
778 OnPipelineError(status
);
782 // Update our paused time.
786 should_notify_time_changed_
= time_changed
;
789 void WebMediaPlayerImpl::OnPipelineEnded() {
790 DVLOG(1) << __FUNCTION__
;
791 DCHECK(main_task_runner_
->BelongsToCurrentThread());
793 // Ignore state changes until we've completed all outstanding seeks.
794 if (seeking_
|| pending_seek_
)
798 client_
->timeChanged();
801 void WebMediaPlayerImpl::OnPipelineError(PipelineStatus error
) {
802 DCHECK(main_task_runner_
->BelongsToCurrentThread());
803 DCHECK_NE(error
, PIPELINE_OK
);
805 if (ready_state_
== WebMediaPlayer::ReadyStateHaveNothing
) {
806 // Any error that occurs before reaching ReadyStateHaveMetadata should
807 // be considered a format error.
808 SetNetworkState(WebMediaPlayer::NetworkStateFormatError
);
812 SetNetworkState(PipelineErrorToNetworkState(error
));
815 void WebMediaPlayerImpl::OnPipelineMetadata(
816 PipelineMetadata metadata
) {
817 DVLOG(1) << __FUNCTION__
;
819 pipeline_metadata_
= metadata
;
821 UMA_HISTOGRAM_ENUMERATION("Media.VideoRotation", metadata
.video_rotation
,
822 VIDEO_ROTATION_MAX
+ 1);
823 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata
);
826 DCHECK(!video_weblayer_
);
827 scoped_refptr
<cc::VideoLayer
> layer
=
828 cc::VideoLayer::Create(cc_blink::WebLayerImpl::LayerSettings(),
829 compositor_
, pipeline_metadata_
.video_rotation
);
831 if (pipeline_metadata_
.video_rotation
== VIDEO_ROTATION_90
||
832 pipeline_metadata_
.video_rotation
== VIDEO_ROTATION_270
) {
833 gfx::Size size
= pipeline_metadata_
.natural_size
;
834 pipeline_metadata_
.natural_size
= gfx::Size(size
.height(), size
.width());
837 video_weblayer_
.reset(new cc_blink::WebLayerImpl(layer
));
838 video_weblayer_
->setOpaque(opaque_
);
839 client_
->setWebLayer(video_weblayer_
.get());
843 void WebMediaPlayerImpl::OnPipelineBufferingStateChanged(
844 BufferingState buffering_state
) {
845 DVLOG(1) << __FUNCTION__
<< "(" << buffering_state
<< ")";
847 // Ignore buffering state changes until we've completed all outstanding seeks.
848 if (seeking_
|| pending_seek_
)
851 // TODO(scherkus): Handle other buffering states when Pipeline starts using
852 // them and translate them ready state changes http://crbug.com/144683
853 DCHECK_EQ(buffering_state
, BUFFERING_HAVE_ENOUGH
);
854 SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData
);
856 // Let the DataSource know we have enough data. It may use this information to
857 // release unused network connections.
859 data_source_
->OnBufferingHaveEnough();
861 // Blink expects a timeChanged() in response to a seek().
862 if (should_notify_time_changed_
)
863 client_
->timeChanged();
866 void WebMediaPlayerImpl::OnDemuxerOpened() {
867 DCHECK(main_task_runner_
->BelongsToCurrentThread());
868 client_
->mediaSourceOpened(
869 new WebMediaSourceImpl(chunk_demuxer_
, media_log_
));
872 void WebMediaPlayerImpl::OnAddTextTrack(
873 const TextTrackConfig
& config
,
874 const AddTextTrackDoneCB
& done_cb
) {
875 DCHECK(main_task_runner_
->BelongsToCurrentThread());
877 const WebInbandTextTrackImpl::Kind web_kind
=
878 static_cast<WebInbandTextTrackImpl::Kind
>(config
.kind());
879 const blink::WebString web_label
=
880 blink::WebString::fromUTF8(config
.label());
881 const blink::WebString web_language
=
882 blink::WebString::fromUTF8(config
.language());
883 const blink::WebString web_id
=
884 blink::WebString::fromUTF8(config
.id());
886 scoped_ptr
<WebInbandTextTrackImpl
> web_inband_text_track(
887 new WebInbandTextTrackImpl(web_kind
, web_label
, web_language
, web_id
));
889 scoped_ptr
<TextTrack
> text_track(new TextTrackImpl(
890 main_task_runner_
, client_
, web_inband_text_track
.Pass()));
892 done_cb
.Run(text_track
.Pass());
895 void WebMediaPlayerImpl::DataSourceInitialized(bool success
) {
896 DCHECK(main_task_runner_
->BelongsToCurrentThread());
899 SetNetworkState(WebMediaPlayer::NetworkStateFormatError
);
906 void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading
) {
907 if (!is_downloading
&& network_state_
== WebMediaPlayer::NetworkStateLoading
)
908 SetNetworkState(WebMediaPlayer::NetworkStateIdle
);
909 else if (is_downloading
&& network_state_
== WebMediaPlayer::NetworkStateIdle
)
910 SetNetworkState(WebMediaPlayer::NetworkStateLoading
);
911 media_log_
->AddEvent(
912 media_log_
->CreateBooleanEvent(
913 MediaLogEvent::NETWORK_ACTIVITY_SET
,
914 "is_downloading_data", is_downloading
));
917 void WebMediaPlayerImpl::StartPipeline() {
918 DCHECK(main_task_runner_
->BelongsToCurrentThread());
920 Demuxer::EncryptedMediaInitDataCB encrypted_media_init_data_cb
=
921 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnEncryptedMediaInitData
);
923 // Figure out which demuxer to use.
924 if (load_type_
!= LoadTypeMediaSource
) {
925 DCHECK(!chunk_demuxer_
);
926 DCHECK(data_source_
);
928 demuxer_
.reset(new FFmpegDemuxer(media_task_runner_
, data_source_
.get(),
929 encrypted_media_init_data_cb
, media_log_
));
931 DCHECK(!chunk_demuxer_
);
932 DCHECK(!data_source_
);
934 chunk_demuxer_
= new ChunkDemuxer(
935 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDemuxerOpened
),
936 encrypted_media_init_data_cb
, media_log_
, true);
937 demuxer_
.reset(chunk_demuxer_
);
940 // ... and we're ready to go!
945 renderer_factory_
->CreateRenderer(
946 media_task_runner_
, audio_source_provider_
.get(), compositor_
),
947 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineEnded
),
948 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineError
),
949 BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnPipelineSeeked
, false),
950 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineMetadata
),
951 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineBufferingStateChanged
),
952 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDurationChanged
),
953 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnAddTextTrack
),
954 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnWaitingForDecryptionKey
));
957 void WebMediaPlayerImpl::SetNetworkState(WebMediaPlayer::NetworkState state
) {
958 DVLOG(1) << __FUNCTION__
<< "(" << state
<< ")";
959 DCHECK(main_task_runner_
->BelongsToCurrentThread());
960 network_state_
= state
;
961 // Always notify to ensure client has the latest value.
962 client_
->networkStateChanged();
965 void WebMediaPlayerImpl::SetReadyState(WebMediaPlayer::ReadyState state
) {
966 DVLOG(1) << __FUNCTION__
<< "(" << state
<< ")";
967 DCHECK(main_task_runner_
->BelongsToCurrentThread());
969 if (state
== WebMediaPlayer::ReadyStateHaveEnoughData
&& data_source_
&&
970 data_source_
->assume_fully_buffered() &&
971 network_state_
== WebMediaPlayer::NetworkStateLoading
)
972 SetNetworkState(WebMediaPlayer::NetworkStateLoaded
);
974 ready_state_
= state
;
975 // Always notify to ensure client has the latest value.
976 client_
->readyStateChanged();
979 blink::WebAudioSourceProvider
* WebMediaPlayerImpl::audioSourceProvider() {
980 return audio_source_provider_
.get();
983 double WebMediaPlayerImpl::GetPipelineDuration() const {
984 base::TimeDelta duration
= pipeline_
.GetMediaDuration();
986 // Return positive infinity if the resource is unbounded.
987 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
988 if (duration
== kInfiniteDuration())
989 return std::numeric_limits
<double>::infinity();
991 return duration
.InSecondsF();
994 void WebMediaPlayerImpl::OnDurationChanged() {
995 if (ready_state_
== WebMediaPlayer::ReadyStateHaveNothing
)
998 client_
->durationChanged();
1001 void WebMediaPlayerImpl::OnNaturalSizeChanged(gfx::Size size
) {
1002 DCHECK(main_task_runner_
->BelongsToCurrentThread());
1003 DCHECK_NE(ready_state_
, WebMediaPlayer::ReadyStateHaveNothing
);
1004 TRACE_EVENT0("media", "WebMediaPlayerImpl::OnNaturalSizeChanged");
1006 media_log_
->AddEvent(
1007 media_log_
->CreateVideoSizeSetEvent(size
.width(), size
.height()));
1008 pipeline_metadata_
.natural_size
= size
;
1010 client_
->sizeChanged();
1013 void WebMediaPlayerImpl::OnOpacityChanged(bool opaque
) {
1014 DCHECK(main_task_runner_
->BelongsToCurrentThread());
1015 DCHECK_NE(ready_state_
, WebMediaPlayer::ReadyStateHaveNothing
);
1018 if (video_weblayer_
)
1019 video_weblayer_
->setOpaque(opaque_
);
1022 static void GetCurrentFrameAndSignal(
1023 VideoFrameCompositor
* compositor
,
1024 scoped_refptr
<VideoFrame
>* video_frame_out
,
1025 base::WaitableEvent
* event
) {
1026 TRACE_EVENT0("media", "GetCurrentFrameAndSignal");
1027 *video_frame_out
= compositor
->GetCurrentFrameAndUpdateIfStale();
1031 scoped_refptr
<VideoFrame
>
1032 WebMediaPlayerImpl::GetCurrentFrameFromCompositor() {
1033 TRACE_EVENT0("media", "WebMediaPlayerImpl::GetCurrentFrameFromCompositor");
1034 if (compositor_task_runner_
->BelongsToCurrentThread())
1035 return compositor_
->GetCurrentFrameAndUpdateIfStale();
1037 // Use a posted task and waitable event instead of a lock otherwise
1038 // WebGL/Canvas can see different content than what the compositor is seeing.
1039 scoped_refptr
<VideoFrame
> video_frame
;
1040 base::WaitableEvent
event(false, false);
1041 compositor_task_runner_
->PostTask(FROM_HERE
,
1042 base::Bind(&GetCurrentFrameAndSignal
,
1043 base::Unretained(compositor_
),
1050 void WebMediaPlayerImpl::UpdatePausedTime() {
1051 DCHECK(main_task_runner_
->BelongsToCurrentThread());
1053 // pause() may be called after playback has ended and the HTMLMediaElement
1054 // requires that currentTime() == duration() after ending. We want to ensure
1055 // |paused_time_| matches currentTime() in this case or a future seek() may
1056 // incorrectly discard what it thinks is a seek to the existing time.
1058 ended_
? pipeline_
.GetMediaDuration() : pipeline_
.GetMediaTime();
1061 } // namespace media