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"
12 #include "base/bind.h"
13 #include "base/callback.h"
14 #include "base/callback_helpers.h"
15 #include "base/debug/alias.h"
16 #include "base/debug/crash_logging.h"
17 #include "base/debug/trace_event.h"
18 #include "base/float_util.h"
19 #include "base/message_loop/message_loop_proxy.h"
20 #include "base/metrics/histogram.h"
21 #include "base/single_thread_task_runner.h"
22 #include "base/synchronization/waitable_event.h"
23 #include "cc/blink/web_layer_impl.h"
24 #include "cc/layers/video_layer.h"
25 #include "gpu/GLES2/gl2extchromium.h"
26 #include "gpu/command_buffer/common/mailbox_holder.h"
27 #include "media/audio/null_audio_sink.h"
28 #include "media/base/audio_hardware_config.h"
29 #include "media/base/bind_to_current_loop.h"
30 #include "media/base/limits.h"
31 #include "media/base/media_log.h"
32 #include "media/base/pipeline.h"
33 #include "media/base/text_renderer.h"
34 #include "media/base/video_frame.h"
35 #include "media/blink/buffered_data_source.h"
36 #include "media/blink/encrypted_media_player_support.h"
37 #include "media/blink/texttrack_impl.h"
38 #include "media/blink/webaudiosourceprovider_impl.h"
39 #include "media/blink/webinbandtexttrack_impl.h"
40 #include "media/blink/webmediaplayer_delegate.h"
41 #include "media/blink/webmediaplayer_params.h"
42 #include "media/blink/webmediaplayer_util.h"
43 #include "media/blink/webmediasource_impl.h"
44 #include "media/filters/audio_renderer_impl.h"
45 #include "media/filters/chunk_demuxer.h"
46 #include "media/filters/ffmpeg_audio_decoder.h"
47 #include "media/filters/ffmpeg_demuxer.h"
48 #include "media/filters/ffmpeg_video_decoder.h"
49 #include "media/filters/gpu_video_accelerator_factories.h"
50 #include "media/filters/gpu_video_decoder.h"
51 #include "media/filters/opus_audio_decoder.h"
52 #include "media/filters/renderer_impl.h"
53 #include "media/filters/video_renderer_impl.h"
54 #include "media/filters/vpx_video_decoder.h"
55 #include "third_party/WebKit/public/platform/WebMediaSource.h"
56 #include "third_party/WebKit/public/platform/WebRect.h"
57 #include "third_party/WebKit/public/platform/WebSize.h"
58 #include "third_party/WebKit/public/platform/WebString.h"
59 #include "third_party/WebKit/public/platform/WebURL.h"
60 #include "third_party/WebKit/public/web/WebLocalFrame.h"
61 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
62 #include "third_party/WebKit/public/web/WebView.h"
64 using blink::WebCanvas
;
65 using blink::WebMediaPlayer
;
68 using blink::WebString
;
72 // Limits the range of playback rate.
74 // TODO(kylep): Revisit these.
76 // Vista has substantially lower performance than XP or Windows7. If you speed
77 // up a video too much, it can't keep up, and rendering stops updating except on
78 // the time bar. For really high speeds, audio becomes a bottleneck and we just
79 // use up the data we have, which may not achieve the speed requested, but will
82 // A very slow speed, ie 0.00000001x, causes the machine to lock up. (It seems
83 // like a busy loop). It gets unresponsive, although its not completely dead.
85 // Also our timers are not very accurate (especially for ogg), which becomes
86 // evident at low speeds and on Vista. Since other speeds are risky and outside
87 // the norms, we think 1/16x to 16x is a safe and useful range for now.
88 const double kMinRate
= 0.0625;
89 const double kMaxRate
= 16.0;
91 class SyncPointClientImpl
: public media::VideoFrame::SyncPointClient
{
93 explicit SyncPointClientImpl(
94 blink::WebGraphicsContext3D
* web_graphics_context
)
95 : web_graphics_context_(web_graphics_context
) {}
96 virtual ~SyncPointClientImpl() {}
97 virtual uint32
InsertSyncPoint() override
{
98 return web_graphics_context_
->insertSyncPoint();
100 virtual void WaitSyncPoint(uint32 sync_point
) override
{
101 web_graphics_context_
->waitSyncPoint(sync_point
);
105 blink::WebGraphicsContext3D
* web_graphics_context_
;
112 class BufferedDataSourceHostImpl
;
114 #define COMPILE_ASSERT_MATCHING_ENUM(name) \
115 COMPILE_ASSERT(static_cast<int>(WebMediaPlayer::CORSMode ## name) == \
116 static_cast<int>(BufferedResourceLoader::k ## name), \
118 COMPILE_ASSERT_MATCHING_ENUM(Unspecified
);
119 COMPILE_ASSERT_MATCHING_ENUM(Anonymous
);
120 COMPILE_ASSERT_MATCHING_ENUM(UseCredentials
);
121 #undef COMPILE_ASSERT_MATCHING_ENUM
123 #define BIND_TO_RENDER_LOOP(function) \
124 (DCHECK(main_task_runner_->BelongsToCurrentThread()), \
125 BindToCurrentLoop(base::Bind(function, AsWeakPtr())))
127 #define BIND_TO_RENDER_LOOP1(function, arg1) \
128 (DCHECK(main_task_runner_->BelongsToCurrentThread()), \
129 BindToCurrentLoop(base::Bind(function, AsWeakPtr(), arg1)))
131 static void LogMediaSourceError(const scoped_refptr
<MediaLog
>& media_log
,
132 const std::string
& error
) {
133 media_log
->AddEvent(media_log
->CreateMediaSourceErrorEvent(error
));
136 WebMediaPlayerImpl::WebMediaPlayerImpl(
137 blink::WebLocalFrame
* frame
,
138 blink::WebMediaPlayerClient
* client
,
139 base::WeakPtr
<WebMediaPlayerDelegate
> delegate
,
140 const WebMediaPlayerParams
& params
)
142 network_state_(WebMediaPlayer::NetworkStateEmpty
),
143 ready_state_(WebMediaPlayer::ReadyStateHaveNothing
),
144 preload_(BufferedDataSource::AUTO
),
145 main_task_runner_(base::MessageLoopProxy::current()),
146 media_task_runner_(params
.media_task_runner()),
147 media_log_(params
.media_log()),
148 pipeline_(media_task_runner_
, media_log_
.get()),
149 load_type_(LoadTypeURL
),
153 playback_rate_(0.0f
),
155 pending_seek_(false),
156 pending_seek_seconds_(0.0f
),
157 should_notify_time_changed_(false),
160 defer_load_cb_(params
.defer_load_cb()),
161 gpu_factories_(params
.gpu_factories()),
162 supports_save_(true),
163 chunk_demuxer_(NULL
),
164 compositor_task_runner_(params
.compositor_task_runner()),
165 compositor_(new VideoFrameCompositor(
166 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNaturalSizeChanged
),
167 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnOpacityChanged
))),
168 text_track_index_(0),
169 encrypted_media_support_(
170 params
.CreateEncryptedMediaPlayerSupport(client
)),
171 audio_hardware_config_(params
.audio_hardware_config()) {
172 DCHECK(encrypted_media_support_
);
174 // Threaded compositing isn't enabled universally yet.
175 if (!compositor_task_runner_
.get())
176 compositor_task_runner_
= base::MessageLoopProxy::current();
178 media_log_
->AddEvent(
179 media_log_
->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_CREATED
));
181 // |gpu_factories_| requires that its entry points be called on its
182 // |GetTaskRunner()|. Since |pipeline_| will own decoders created from the
183 // factories, require that their message loops are identical.
184 DCHECK(!gpu_factories_
.get() ||
185 (gpu_factories_
->GetTaskRunner() == media_task_runner_
.get()));
187 // Use the null sink if no sink was provided.
188 audio_source_provider_
= new WebAudioSourceProviderImpl(
189 params
.audio_renderer_sink().get()
190 ? params
.audio_renderer_sink()
191 : new NullAudioSink(media_task_runner_
));
194 WebMediaPlayerImpl::~WebMediaPlayerImpl() {
195 client_
->setWebLayer(NULL
);
197 DCHECK(main_task_runner_
->BelongsToCurrentThread());
198 media_log_
->AddEvent(
199 media_log_
->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_DESTROYED
));
202 delegate_
->PlayerGone(this);
204 // Abort any pending IO so stopping the pipeline doesn't get blocked.
206 data_source_
->Abort();
207 if (chunk_demuxer_
) {
208 chunk_demuxer_
->Shutdown();
209 chunk_demuxer_
= NULL
;
212 gpu_factories_
= NULL
;
214 // Make sure to kill the pipeline so there's no more media threads running.
215 // Note: stopping the pipeline might block for a long time.
216 base::WaitableEvent
waiter(false, false);
218 base::Bind(&base::WaitableEvent::Signal
, base::Unretained(&waiter
)));
221 compositor_task_runner_
->DeleteSoon(FROM_HERE
, compositor_
);
224 void WebMediaPlayerImpl::load(LoadType load_type
, const blink::WebURL
& url
,
225 CORSMode cors_mode
) {
226 DVLOG(1) << __FUNCTION__
<< "(" << load_type
<< ", " << url
<< ", "
228 if (!defer_load_cb_
.is_null()) {
229 defer_load_cb_
.Run(base::Bind(
230 &WebMediaPlayerImpl::DoLoad
, AsWeakPtr(), load_type
, url
, cors_mode
));
233 DoLoad(load_type
, url
, cors_mode
);
236 void WebMediaPlayerImpl::DoLoad(LoadType load_type
,
237 const blink::WebURL
& url
,
238 CORSMode cors_mode
) {
239 DCHECK(main_task_runner_
->BelongsToCurrentThread());
242 ReportMediaSchemeUma(gurl
);
244 // Set subresource URL for crash reporting.
245 base::debug::SetCrashKeyValue("subresource_url", gurl
.spec());
247 load_type_
= load_type
;
249 SetNetworkState(WebMediaPlayer::NetworkStateLoading
);
250 SetReadyState(WebMediaPlayer::ReadyStateHaveNothing
);
251 media_log_
->AddEvent(media_log_
->CreateLoadEvent(url
.spec()));
253 // Media source pipelines can start immediately.
254 if (load_type
== LoadTypeMediaSource
) {
255 supports_save_
= false;
260 // Otherwise it's a regular request which requires resolving the URL first.
261 data_source_
.reset(new BufferedDataSource(
263 static_cast<BufferedResourceLoader::CORSMode
>(cors_mode
),
267 &buffered_data_source_host_
,
268 base::Bind(&WebMediaPlayerImpl::NotifyDownloading
, AsWeakPtr())));
269 data_source_
->Initialize(
270 base::Bind(&WebMediaPlayerImpl::DataSourceInitialized
, AsWeakPtr()));
271 data_source_
->SetPreload(preload_
);
274 void WebMediaPlayerImpl::play() {
275 DVLOG(1) << __FUNCTION__
;
276 DCHECK(main_task_runner_
->BelongsToCurrentThread());
279 pipeline_
.SetPlaybackRate(playback_rate_
);
281 data_source_
->MediaIsPlaying();
283 media_log_
->AddEvent(media_log_
->CreateEvent(MediaLogEvent::PLAY
));
285 if (delegate_
&& playback_rate_
> 0)
286 delegate_
->DidPlay(this);
289 void WebMediaPlayerImpl::pause() {
290 DVLOG(1) << __FUNCTION__
;
291 DCHECK(main_task_runner_
->BelongsToCurrentThread());
293 const bool was_already_paused
= paused_
|| playback_rate_
== 0;
295 pipeline_
.SetPlaybackRate(0.0f
);
297 data_source_
->MediaIsPaused();
298 paused_time_
= pipeline_
.GetMediaTime();
300 media_log_
->AddEvent(media_log_
->CreateEvent(MediaLogEvent::PAUSE
));
302 if (!was_already_paused
&& delegate_
)
303 delegate_
->DidPause(this);
306 bool WebMediaPlayerImpl::supportsSave() const {
307 DCHECK(main_task_runner_
->BelongsToCurrentThread());
308 return supports_save_
;
311 void WebMediaPlayerImpl::seek(double seconds
) {
312 DVLOG(1) << __FUNCTION__
<< "(" << seconds
<< ")";
313 DCHECK(main_task_runner_
->BelongsToCurrentThread());
317 if (ready_state_
> WebMediaPlayer::ReadyStateHaveMetadata
)
318 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata
);
320 base::TimeDelta seek_time
= ConvertSecondsToTimestamp(seconds
);
323 pending_seek_
= true;
324 pending_seek_seconds_
= seconds
;
326 chunk_demuxer_
->CancelPendingSeek(seek_time
);
330 media_log_
->AddEvent(media_log_
->CreateSeekEvent(seconds
));
332 // Update our paused time.
334 paused_time_
= seek_time
;
339 chunk_demuxer_
->StartWaitingForSeek(seek_time
);
341 // Kick off the asynchronous seek!
344 BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnPipelineSeeked
, true));
347 void WebMediaPlayerImpl::setRate(double rate
) {
348 DVLOG(1) << __FUNCTION__
<< "(" << rate
<< ")";
349 DCHECK(main_task_runner_
->BelongsToCurrentThread());
351 // TODO(kylep): Remove when support for negatives is added. Also, modify the
352 // following checks so rewind uses reasonable values also.
356 // Limit rates to reasonable values by clamping.
360 else if (rate
> kMaxRate
)
362 if (playback_rate_
== 0 && !paused_
&& delegate_
)
363 delegate_
->DidPlay(this);
364 } else if (playback_rate_
!= 0 && !paused_
&& delegate_
) {
365 delegate_
->DidPause(this);
368 playback_rate_
= rate
;
370 pipeline_
.SetPlaybackRate(rate
);
372 data_source_
->MediaPlaybackRateChanged(rate
);
376 void WebMediaPlayerImpl::setVolume(double volume
) {
377 DVLOG(1) << __FUNCTION__
<< "(" << volume
<< ")";
378 DCHECK(main_task_runner_
->BelongsToCurrentThread());
380 pipeline_
.SetVolume(volume
);
383 #define COMPILE_ASSERT_MATCHING_ENUM(webkit_name, chromium_name) \
384 COMPILE_ASSERT(static_cast<int>(WebMediaPlayer::webkit_name) == \
385 static_cast<int>(BufferedDataSource::chromium_name), \
387 COMPILE_ASSERT_MATCHING_ENUM(PreloadNone
, NONE
);
388 COMPILE_ASSERT_MATCHING_ENUM(PreloadMetaData
, METADATA
);
389 COMPILE_ASSERT_MATCHING_ENUM(PreloadAuto
, AUTO
);
390 #undef COMPILE_ASSERT_MATCHING_ENUM
392 void WebMediaPlayerImpl::setPreload(WebMediaPlayer::Preload preload
) {
393 DVLOG(1) << __FUNCTION__
<< "(" << preload
<< ")";
394 DCHECK(main_task_runner_
->BelongsToCurrentThread());
396 preload_
= static_cast<BufferedDataSource::Preload
>(preload
);
398 data_source_
->SetPreload(preload_
);
401 bool WebMediaPlayerImpl::hasVideo() const {
402 DCHECK(main_task_runner_
->BelongsToCurrentThread());
404 return pipeline_metadata_
.has_video
;
407 bool WebMediaPlayerImpl::hasAudio() const {
408 DCHECK(main_task_runner_
->BelongsToCurrentThread());
410 return pipeline_metadata_
.has_audio
;
413 blink::WebSize
WebMediaPlayerImpl::naturalSize() const {
414 DCHECK(main_task_runner_
->BelongsToCurrentThread());
416 return blink::WebSize(pipeline_metadata_
.natural_size
);
419 bool WebMediaPlayerImpl::paused() const {
420 DCHECK(main_task_runner_
->BelongsToCurrentThread());
422 return pipeline_
.GetPlaybackRate() == 0.0f
;
425 bool WebMediaPlayerImpl::seeking() const {
426 DCHECK(main_task_runner_
->BelongsToCurrentThread());
428 if (ready_state_
== WebMediaPlayer::ReadyStateHaveNothing
)
434 double WebMediaPlayerImpl::duration() const {
435 DCHECK(main_task_runner_
->BelongsToCurrentThread());
437 if (ready_state_
== WebMediaPlayer::ReadyStateHaveNothing
)
438 return std::numeric_limits
<double>::quiet_NaN();
440 return GetPipelineDuration();
443 double WebMediaPlayerImpl::timelineOffset() const {
444 DCHECK(main_task_runner_
->BelongsToCurrentThread());
446 if (pipeline_metadata_
.timeline_offset
.is_null())
447 return std::numeric_limits
<double>::quiet_NaN();
449 return pipeline_metadata_
.timeline_offset
.ToJsTime();
452 double WebMediaPlayerImpl::currentTime() const {
453 DCHECK(main_task_runner_
->BelongsToCurrentThread());
454 DCHECK_NE(ready_state_
, WebMediaPlayer::ReadyStateHaveNothing
);
456 // TODO(scherkus): Replace with an explicit ended signal to HTMLMediaElement,
457 // see http://crbug.com/409280
461 return (paused_
? paused_time_
: pipeline_
.GetMediaTime()).InSecondsF();
464 WebMediaPlayer::NetworkState
WebMediaPlayerImpl::networkState() const {
465 DCHECK(main_task_runner_
->BelongsToCurrentThread());
466 return network_state_
;
469 WebMediaPlayer::ReadyState
WebMediaPlayerImpl::readyState() const {
470 DCHECK(main_task_runner_
->BelongsToCurrentThread());
474 blink::WebTimeRanges
WebMediaPlayerImpl::buffered() const {
475 DCHECK(main_task_runner_
->BelongsToCurrentThread());
477 Ranges
<base::TimeDelta
> buffered_time_ranges
=
478 pipeline_
.GetBufferedTimeRanges();
480 const base::TimeDelta duration
= pipeline_
.GetMediaDuration();
481 if (duration
!= kInfiniteDuration()) {
482 buffered_data_source_host_
.AddBufferedTimeRanges(
483 &buffered_time_ranges
, duration
);
485 return ConvertToWebTimeRanges(buffered_time_ranges
);
488 blink::WebTimeRanges
WebMediaPlayerImpl::seekable() const {
489 DCHECK(main_task_runner_
->BelongsToCurrentThread());
491 // Media without duration are considered streaming and should not be seekable.
492 const double seekable_end
= duration();
493 if (!base::IsFinite(seekable_end
))
494 return blink::WebTimeRanges();
496 // If we have a finite duration then use [0, duration] as the seekable range;
497 // unless it's a streaming source, in which case only allow a seek to zero.
498 blink::WebTimeRange
seekable_range(
499 0.0, data_source_
&& data_source_
->IsStreaming() ? 0.0 : seekable_end
);
500 return blink::WebTimeRanges(&seekable_range
, 1);
503 bool WebMediaPlayerImpl::didLoadingProgress() {
504 DCHECK(main_task_runner_
->BelongsToCurrentThread());
505 bool pipeline_progress
= pipeline_
.DidLoadingProgress();
506 bool data_progress
= buffered_data_source_host_
.DidLoadingProgress();
507 return pipeline_progress
|| data_progress
;
510 void WebMediaPlayerImpl::paint(blink::WebCanvas
* canvas
,
511 const blink::WebRect
& rect
,
512 unsigned char alpha
) {
513 paint(canvas
, rect
, alpha
, SkXfermode::kSrcOver_Mode
);
516 void WebMediaPlayerImpl::paint(blink::WebCanvas
* canvas
,
517 const blink::WebRect
& rect
,
519 SkXfermode::Mode mode
) {
520 DCHECK(main_task_runner_
->BelongsToCurrentThread());
521 TRACE_EVENT0("media", "WebMediaPlayerImpl:paint");
523 // TODO(scherkus): Clarify paint() API contract to better understand when and
524 // why it's being called. For example, today paint() is called when:
525 // - We haven't reached HAVE_CURRENT_DATA and need to paint black
526 // - We're painting to a canvas
527 // See http://crbug.com/341225 http://crbug.com/342621 for details.
528 scoped_refptr
<VideoFrame
> video_frame
=
529 GetCurrentFrameFromCompositor();
531 gfx::Rect
gfx_rect(rect
);
533 skcanvas_video_renderer_
.Paint(video_frame
,
538 pipeline_metadata_
.video_rotation
);
541 bool WebMediaPlayerImpl::hasSingleSecurityOrigin() const {
543 return data_source_
->HasSingleOrigin();
547 bool WebMediaPlayerImpl::didPassCORSAccessCheck() const {
549 return data_source_
->DidPassCORSAccessCheck();
553 double WebMediaPlayerImpl::mediaTimeForTimeValue(double timeValue
) const {
554 return ConvertSecondsToTimestamp(timeValue
).InSecondsF();
557 unsigned WebMediaPlayerImpl::decodedFrameCount() const {
558 DCHECK(main_task_runner_
->BelongsToCurrentThread());
560 PipelineStatistics stats
= pipeline_
.GetStatistics();
561 return stats
.video_frames_decoded
;
564 unsigned WebMediaPlayerImpl::droppedFrameCount() const {
565 DCHECK(main_task_runner_
->BelongsToCurrentThread());
567 PipelineStatistics stats
= pipeline_
.GetStatistics();
568 return stats
.video_frames_dropped
;
571 unsigned WebMediaPlayerImpl::audioDecodedByteCount() const {
572 DCHECK(main_task_runner_
->BelongsToCurrentThread());
574 PipelineStatistics stats
= pipeline_
.GetStatistics();
575 return stats
.audio_bytes_decoded
;
578 unsigned WebMediaPlayerImpl::videoDecodedByteCount() const {
579 DCHECK(main_task_runner_
->BelongsToCurrentThread());
581 PipelineStatistics stats
= pipeline_
.GetStatistics();
582 return stats
.video_bytes_decoded
;
585 bool WebMediaPlayerImpl::copyVideoTextureToPlatformTexture(
586 blink::WebGraphicsContext3D
* web_graphics_context
,
587 unsigned int texture
,
589 unsigned int internal_format
,
591 bool premultiply_alpha
,
593 TRACE_EVENT0("media", "WebMediaPlayerImpl:copyVideoTextureToPlatformTexture");
595 scoped_refptr
<VideoFrame
> video_frame
=
596 GetCurrentFrameFromCompositor();
598 if (!video_frame
.get())
600 if (video_frame
->format() != VideoFrame::NATIVE_TEXTURE
)
603 const gpu::MailboxHolder
* mailbox_holder
= video_frame
->mailbox_holder();
604 if (mailbox_holder
->texture_target
!= GL_TEXTURE_2D
)
607 web_graphics_context
->waitSyncPoint(mailbox_holder
->sync_point
);
608 uint32 source_texture
= web_graphics_context
->createAndConsumeTextureCHROMIUM(
609 GL_TEXTURE_2D
, mailbox_holder
->mailbox
.name
);
611 // The video is stored in a unmultiplied format, so premultiply
613 web_graphics_context
->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM
,
615 // Application itself needs to take care of setting the right flip_y
616 // value down to get the expected result.
617 // flip_y==true means to reverse the video orientation while
618 // flip_y==false means to keep the intrinsic orientation.
619 web_graphics_context
->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM
, flip_y
);
620 web_graphics_context
->copyTextureCHROMIUM(GL_TEXTURE_2D
,
626 web_graphics_context
->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM
, false);
627 web_graphics_context
->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM
,
630 web_graphics_context
->deleteTexture(source_texture
);
631 web_graphics_context
->flush();
633 SyncPointClientImpl
client(web_graphics_context
);
634 video_frame
->UpdateReleaseSyncPoint(&client
);
638 WebMediaPlayer::MediaKeyException
639 WebMediaPlayerImpl::generateKeyRequest(const WebString
& key_system
,
640 const unsigned char* init_data
,
641 unsigned init_data_length
) {
642 DCHECK(main_task_runner_
->BelongsToCurrentThread());
644 return encrypted_media_support_
->GenerateKeyRequest(
645 frame_
, key_system
, init_data
, init_data_length
);
648 WebMediaPlayer::MediaKeyException
WebMediaPlayerImpl::addKey(
649 const WebString
& key_system
,
650 const unsigned char* key
,
652 const unsigned char* init_data
,
653 unsigned init_data_length
,
654 const WebString
& session_id
) {
655 DCHECK(main_task_runner_
->BelongsToCurrentThread());
657 return encrypted_media_support_
->AddKey(
658 key_system
, key
, key_length
, init_data
, init_data_length
, session_id
);
661 WebMediaPlayer::MediaKeyException
WebMediaPlayerImpl::cancelKeyRequest(
662 const WebString
& key_system
,
663 const WebString
& session_id
) {
664 DCHECK(main_task_runner_
->BelongsToCurrentThread());
666 return encrypted_media_support_
->CancelKeyRequest(key_system
, session_id
);
669 void WebMediaPlayerImpl::setContentDecryptionModule(
670 blink::WebContentDecryptionModule
* cdm
) {
671 DCHECK(main_task_runner_
->BelongsToCurrentThread());
673 encrypted_media_support_
->SetContentDecryptionModule(cdm
);
676 void WebMediaPlayerImpl::setContentDecryptionModule(
677 blink::WebContentDecryptionModule
* cdm
,
678 blink::WebContentDecryptionModuleResult result
) {
679 DCHECK(main_task_runner_
->BelongsToCurrentThread());
681 encrypted_media_support_
->SetContentDecryptionModule(cdm
, result
);
684 void WebMediaPlayerImpl::OnPipelineSeeked(bool time_changed
,
685 PipelineStatus status
) {
686 DVLOG(1) << __FUNCTION__
<< "(" << time_changed
<< ", " << status
<< ")";
687 DCHECK(main_task_runner_
->BelongsToCurrentThread());
690 pending_seek_
= false;
691 seek(pending_seek_seconds_
);
695 if (status
!= PIPELINE_OK
) {
696 OnPipelineError(status
);
700 // Update our paused time.
702 paused_time_
= pipeline_
.GetMediaTime();
704 should_notify_time_changed_
= time_changed
;
707 void WebMediaPlayerImpl::OnPipelineEnded() {
708 DVLOG(1) << __FUNCTION__
;
709 DCHECK(main_task_runner_
->BelongsToCurrentThread());
711 // Ignore state changes until we've completed all outstanding seeks.
712 if (seeking_
|| pending_seek_
)
716 client_
->timeChanged();
719 void WebMediaPlayerImpl::OnPipelineError(PipelineStatus error
) {
720 DCHECK(main_task_runner_
->BelongsToCurrentThread());
721 DCHECK_NE(error
, PIPELINE_OK
);
723 if (ready_state_
== WebMediaPlayer::ReadyStateHaveNothing
) {
724 // Any error that occurs before reaching ReadyStateHaveMetadata should
725 // be considered a format error.
726 SetNetworkState(WebMediaPlayer::NetworkStateFormatError
);
730 SetNetworkState(PipelineErrorToNetworkState(error
));
732 if (error
== PIPELINE_ERROR_DECRYPT
)
733 encrypted_media_support_
->OnPipelineDecryptError();
736 void WebMediaPlayerImpl::OnPipelineMetadata(
737 PipelineMetadata metadata
) {
738 DVLOG(1) << __FUNCTION__
;
740 pipeline_metadata_
= metadata
;
742 UMA_HISTOGRAM_ENUMERATION("Media.VideoRotation",
743 metadata
.video_rotation
,
744 VIDEO_ROTATION_MAX
+ 1);
745 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata
);
748 DCHECK(!video_weblayer_
);
749 scoped_refptr
<cc::VideoLayer
> layer
=
750 cc::VideoLayer::Create(compositor_
, pipeline_metadata_
.video_rotation
);
752 if (pipeline_metadata_
.video_rotation
== VIDEO_ROTATION_90
||
753 pipeline_metadata_
.video_rotation
== VIDEO_ROTATION_270
) {
754 gfx::Size size
= pipeline_metadata_
.natural_size
;
755 pipeline_metadata_
.natural_size
= gfx::Size(size
.height(), size
.width());
758 video_weblayer_
.reset(new cc_blink::WebLayerImpl(layer
));
759 video_weblayer_
->setOpaque(opaque_
);
760 client_
->setWebLayer(video_weblayer_
.get());
764 void WebMediaPlayerImpl::OnPipelineBufferingStateChanged(
765 BufferingState buffering_state
) {
766 DVLOG(1) << __FUNCTION__
<< "(" << buffering_state
<< ")";
768 // Ignore buffering state changes until we've completed all outstanding seeks.
769 if (seeking_
|| pending_seek_
)
772 // TODO(scherkus): Handle other buffering states when Pipeline starts using
773 // them and translate them ready state changes http://crbug.com/144683
774 DCHECK_EQ(buffering_state
, BUFFERING_HAVE_ENOUGH
);
775 SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData
);
777 // Blink expects a timeChanged() in response to a seek().
778 if (should_notify_time_changed_
)
779 client_
->timeChanged();
782 void WebMediaPlayerImpl::OnDemuxerOpened() {
783 DCHECK(main_task_runner_
->BelongsToCurrentThread());
784 client_
->mediaSourceOpened(new WebMediaSourceImpl(
785 chunk_demuxer_
, base::Bind(&LogMediaSourceError
, media_log_
)));
788 void WebMediaPlayerImpl::OnAddTextTrack(
789 const TextTrackConfig
& config
,
790 const AddTextTrackDoneCB
& done_cb
) {
791 DCHECK(main_task_runner_
->BelongsToCurrentThread());
793 const WebInbandTextTrackImpl::Kind web_kind
=
794 static_cast<WebInbandTextTrackImpl::Kind
>(config
.kind());
795 const blink::WebString web_label
=
796 blink::WebString::fromUTF8(config
.label());
797 const blink::WebString web_language
=
798 blink::WebString::fromUTF8(config
.language());
799 const blink::WebString web_id
=
800 blink::WebString::fromUTF8(config
.id());
802 scoped_ptr
<WebInbandTextTrackImpl
> web_inband_text_track(
803 new WebInbandTextTrackImpl(web_kind
, web_label
, web_language
, web_id
,
804 text_track_index_
++));
806 scoped_ptr
<TextTrack
> text_track(new TextTrackImpl(
807 main_task_runner_
, client_
, web_inband_text_track
.Pass()));
809 done_cb
.Run(text_track
.Pass());
812 void WebMediaPlayerImpl::DataSourceInitialized(bool success
) {
813 DCHECK(main_task_runner_
->BelongsToCurrentThread());
816 SetNetworkState(WebMediaPlayer::NetworkStateFormatError
);
823 void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading
) {
824 if (!is_downloading
&& network_state_
== WebMediaPlayer::NetworkStateLoading
)
825 SetNetworkState(WebMediaPlayer::NetworkStateIdle
);
826 else if (is_downloading
&& network_state_
== WebMediaPlayer::NetworkStateIdle
)
827 SetNetworkState(WebMediaPlayer::NetworkStateLoading
);
828 media_log_
->AddEvent(
829 media_log_
->CreateBooleanEvent(
830 MediaLogEvent::NETWORK_ACTIVITY_SET
,
831 "is_downloading_data", is_downloading
));
834 // TODO(xhwang): Move this to a factory class so that we can create different
836 scoped_ptr
<Renderer
> WebMediaPlayerImpl::CreateRenderer() {
837 SetDecryptorReadyCB set_decryptor_ready_cb
=
838 encrypted_media_support_
->CreateSetDecryptorReadyCB();
840 // Create our audio decoders and renderer.
841 ScopedVector
<AudioDecoder
> audio_decoders
;
843 audio_decoders
.push_back(new media::FFmpegAudioDecoder(
844 media_task_runner_
, base::Bind(&LogMediaSourceError
, media_log_
)));
845 audio_decoders
.push_back(new media::OpusAudioDecoder(media_task_runner_
));
847 scoped_ptr
<AudioRenderer
> audio_renderer(
848 new AudioRendererImpl(media_task_runner_
,
849 audio_source_provider_
.get(),
850 audio_decoders
.Pass(),
851 set_decryptor_ready_cb
,
852 audio_hardware_config_
,
855 // Create our video decoders and renderer.
856 ScopedVector
<VideoDecoder
> video_decoders
;
858 if (gpu_factories_
.get())
859 video_decoders
.push_back(new GpuVideoDecoder(gpu_factories_
));
861 #if !defined(MEDIA_DISABLE_LIBVPX)
862 video_decoders
.push_back(new VpxVideoDecoder(media_task_runner_
));
863 #endif // !defined(MEDIA_DISABLE_LIBVPX)
865 video_decoders
.push_back(new FFmpegVideoDecoder(media_task_runner_
));
867 scoped_ptr
<VideoRenderer
> video_renderer(new VideoRendererImpl(
869 video_decoders
.Pass(),
870 set_decryptor_ready_cb
,
871 base::Bind(&WebMediaPlayerImpl::FrameReady
, base::Unretained(this)),
876 return scoped_ptr
<Renderer
>(new RendererImpl(
877 media_task_runner_
, audio_renderer
.Pass(), video_renderer
.Pass()));
880 void WebMediaPlayerImpl::StartPipeline() {
881 DCHECK(main_task_runner_
->BelongsToCurrentThread());
883 // Keep track if this is a MSE or non-MSE playback.
884 UMA_HISTOGRAM_BOOLEAN("Media.MSE.Playback",
885 (load_type_
== LoadTypeMediaSource
));
888 Demuxer::NeedKeyCB need_key_cb
=
889 encrypted_media_support_
->CreateNeedKeyCB();
891 // Figure out which demuxer to use.
892 if (load_type_
!= LoadTypeMediaSource
) {
893 DCHECK(!chunk_demuxer_
);
894 DCHECK(data_source_
);
896 demuxer_
.reset(new FFmpegDemuxer(
897 media_task_runner_
, data_source_
.get(),
901 DCHECK(!chunk_demuxer_
);
902 DCHECK(!data_source_
);
904 mse_log_cb
= base::Bind(&LogMediaSourceError
, media_log_
);
906 chunk_demuxer_
= new ChunkDemuxer(
907 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDemuxerOpened
),
911 demuxer_
.reset(chunk_demuxer_
);
914 // ... and we're ready to go!
919 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineEnded
),
920 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineError
),
921 BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnPipelineSeeked
, false),
922 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineMetadata
),
923 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineBufferingStateChanged
),
924 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDurationChanged
),
925 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnAddTextTrack
));
928 void WebMediaPlayerImpl::SetNetworkState(WebMediaPlayer::NetworkState state
) {
929 DVLOG(1) << __FUNCTION__
<< "(" << state
<< ")";
930 DCHECK(main_task_runner_
->BelongsToCurrentThread());
931 network_state_
= state
;
932 // Always notify to ensure client has the latest value.
933 client_
->networkStateChanged();
936 void WebMediaPlayerImpl::SetReadyState(WebMediaPlayer::ReadyState state
) {
937 DVLOG(1) << __FUNCTION__
<< "(" << state
<< ")";
938 DCHECK(main_task_runner_
->BelongsToCurrentThread());
940 if (state
== WebMediaPlayer::ReadyStateHaveEnoughData
&& data_source_
&&
941 data_source_
->assume_fully_buffered() &&
942 network_state_
== WebMediaPlayer::NetworkStateLoading
)
943 SetNetworkState(WebMediaPlayer::NetworkStateLoaded
);
945 ready_state_
= state
;
946 // Always notify to ensure client has the latest value.
947 client_
->readyStateChanged();
950 blink::WebAudioSourceProvider
* WebMediaPlayerImpl::audioSourceProvider() {
951 return audio_source_provider_
.get();
954 double WebMediaPlayerImpl::GetPipelineDuration() const {
955 base::TimeDelta duration
= pipeline_
.GetMediaDuration();
957 // Return positive infinity if the resource is unbounded.
958 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
959 if (duration
== kInfiniteDuration())
960 return std::numeric_limits
<double>::infinity();
962 return duration
.InSecondsF();
965 void WebMediaPlayerImpl::OnDurationChanged() {
966 if (ready_state_
== WebMediaPlayer::ReadyStateHaveNothing
)
969 client_
->durationChanged();
972 void WebMediaPlayerImpl::OnNaturalSizeChanged(gfx::Size size
) {
973 DCHECK(main_task_runner_
->BelongsToCurrentThread());
974 DCHECK_NE(ready_state_
, WebMediaPlayer::ReadyStateHaveNothing
);
975 TRACE_EVENT0("media", "WebMediaPlayerImpl::OnNaturalSizeChanged");
977 media_log_
->AddEvent(
978 media_log_
->CreateVideoSizeSetEvent(size
.width(), size
.height()));
979 pipeline_metadata_
.natural_size
= size
;
981 client_
->sizeChanged();
984 void WebMediaPlayerImpl::OnOpacityChanged(bool opaque
) {
985 DCHECK(main_task_runner_
->BelongsToCurrentThread());
986 DCHECK_NE(ready_state_
, WebMediaPlayer::ReadyStateHaveNothing
);
990 video_weblayer_
->setOpaque(opaque_
);
993 void WebMediaPlayerImpl::FrameReady(
994 const scoped_refptr
<VideoFrame
>& frame
) {
995 compositor_task_runner_
->PostTask(
997 base::Bind(&VideoFrameCompositor::UpdateCurrentFrame
,
998 base::Unretained(compositor_
),
1002 static void GetCurrentFrameAndSignal(
1003 VideoFrameCompositor
* compositor
,
1004 scoped_refptr
<VideoFrame
>* video_frame_out
,
1005 base::WaitableEvent
* event
) {
1006 TRACE_EVENT0("media", "GetCurrentFrameAndSignal");
1007 *video_frame_out
= compositor
->GetCurrentFrame();
1011 scoped_refptr
<VideoFrame
>
1012 WebMediaPlayerImpl::GetCurrentFrameFromCompositor() {
1013 TRACE_EVENT0("media", "WebMediaPlayerImpl::GetCurrentFrameFromCompositor");
1014 if (compositor_task_runner_
->BelongsToCurrentThread())
1015 return compositor_
->GetCurrentFrame();
1017 // Use a posted task and waitable event instead of a lock otherwise
1018 // WebGL/Canvas can see different content than what the compositor is seeing.
1019 scoped_refptr
<VideoFrame
> video_frame
;
1020 base::WaitableEvent
event(false, false);
1021 compositor_task_runner_
->PostTask(FROM_HERE
,
1022 base::Bind(&GetCurrentFrameAndSignal
,
1023 base::Unretained(compositor_
),
1030 } // namespace media