1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/media/webmediaplayer_impl.h"
12 #include "base/bind.h"
13 #include "base/callback.h"
14 #include "base/callback_helpers.h"
15 #include "base/command_line.h"
16 #include "base/debug/alias.h"
17 #include "base/debug/crash_logging.h"
18 #include "base/debug/trace_event.h"
19 #include "base/message_loop/message_loop_proxy.h"
20 #include "base/metrics/histogram.h"
21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "base/synchronization/waitable_event.h"
24 #include "cc/layers/video_layer.h"
25 #include "content/public/common/content_switches.h"
26 #include "content/public/renderer/render_frame.h"
27 #include "content/renderer/media/buffered_data_source.h"
28 #include "content/renderer/media/crypto/key_systems.h"
29 #include "content/renderer/media/render_media_log.h"
30 #include "content/renderer/media/texttrack_impl.h"
31 #include "content/renderer/media/webaudiosourceprovider_impl.h"
32 #include "content/renderer/media/webcontentdecryptionmodule_impl.h"
33 #include "content/renderer/media/webinbandtexttrack_impl.h"
34 #include "content/renderer/media/webmediaplayer_delegate.h"
35 #include "content/renderer/media/webmediaplayer_params.h"
36 #include "content/renderer/media/webmediaplayer_util.h"
37 #include "content/renderer/media/webmediasource_impl.h"
38 #include "content/renderer/pepper/pepper_webplugin_impl.h"
39 #include "content/renderer/render_thread_impl.h"
40 #include "gpu/GLES2/gl2extchromium.h"
41 #include "gpu/command_buffer/common/mailbox_holder.h"
42 #include "media/audio/null_audio_sink.h"
43 #include "media/base/audio_hardware_config.h"
44 #include "media/base/bind_to_current_loop.h"
45 #include "media/base/filter_collection.h"
46 #include "media/base/limits.h"
47 #include "media/base/media_log.h"
48 #include "media/base/media_switches.h"
49 #include "media/base/pipeline.h"
50 #include "media/base/text_renderer.h"
51 #include "media/base/video_frame.h"
52 #include "media/filters/audio_renderer_impl.h"
53 #include "media/filters/chunk_demuxer.h"
54 #include "media/filters/ffmpeg_audio_decoder.h"
55 #include "media/filters/ffmpeg_demuxer.h"
56 #include "media/filters/ffmpeg_video_decoder.h"
57 #include "media/filters/gpu_video_accelerator_factories.h"
58 #include "media/filters/gpu_video_decoder.h"
59 #include "media/filters/opus_audio_decoder.h"
60 #include "media/filters/video_renderer_impl.h"
61 #include "media/filters/vpx_video_decoder.h"
62 #include "third_party/WebKit/public/platform/WebContentDecryptionModule.h"
63 #include "third_party/WebKit/public/platform/WebMediaSource.h"
64 #include "third_party/WebKit/public/platform/WebRect.h"
65 #include "third_party/WebKit/public/platform/WebSize.h"
66 #include "third_party/WebKit/public/platform/WebString.h"
67 #include "third_party/WebKit/public/platform/WebURL.h"
68 #include "third_party/WebKit/public/web/WebDocument.h"
69 #include "third_party/WebKit/public/web/WebLocalFrame.h"
70 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
71 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
72 #include "third_party/WebKit/public/web/WebView.h"
73 #include "v8/include/v8.h"
74 #include "webkit/renderer/compositor_bindings/web_layer_impl.h"
76 #if defined(ENABLE_PEPPER_CDMS)
77 #include "content/renderer/media/crypto/pepper_cdm_wrapper_impl.h"
80 using blink::WebCanvas
;
81 using blink::WebMediaPlayer
;
84 using blink::WebString
;
85 using media::PipelineStatus
;
89 // Amount of extra memory used by each player instance reported to V8.
90 // It is not exact number -- first, it differs on different platforms,
91 // and second, it is very hard to calculate. Instead, use some arbitrary
92 // value that will cause garbage collection from time to time. We don't want
93 // it to happen on every allocation, but don't want 5k players to sit in memory
94 // either. Looks that chosen constant achieves both goals, at least for audio
95 // objects. (Do not worry about video objects yet, JS programs do not create
96 // thousands of them...)
97 const int kPlayerExtraMemory
= 1024 * 1024;
99 // Limits the range of playback rate.
101 // TODO(kylep): Revisit these.
103 // Vista has substantially lower performance than XP or Windows7. If you speed
104 // up a video too much, it can't keep up, and rendering stops updating except on
105 // the time bar. For really high speeds, audio becomes a bottleneck and we just
106 // use up the data we have, which may not achieve the speed requested, but will
107 // not crash the tab.
109 // A very slow speed, ie 0.00000001x, causes the machine to lock up. (It seems
110 // like a busy loop). It gets unresponsive, although its not completely dead.
112 // Also our timers are not very accurate (especially for ogg), which becomes
113 // evident at low speeds and on Vista. Since other speeds are risky and outside
114 // the norms, we think 1/16x to 16x is a safe and useful range for now.
115 const double kMinRate
= 0.0625;
116 const double kMaxRate
= 16.0;
118 // Prefix for histograms related to Encrypted Media Extensions.
119 const char* kMediaEme
= "Media.EME.";
125 class BufferedDataSourceHostImpl
;
127 #define COMPILE_ASSERT_MATCHING_ENUM(name) \
128 COMPILE_ASSERT(static_cast<int>(WebMediaPlayer::CORSMode ## name) == \
129 static_cast<int>(BufferedResourceLoader::k ## name), \
131 COMPILE_ASSERT_MATCHING_ENUM(Unspecified
);
132 COMPILE_ASSERT_MATCHING_ENUM(Anonymous
);
133 COMPILE_ASSERT_MATCHING_ENUM(UseCredentials
);
134 #undef COMPILE_ASSERT_MATCHING_ENUM
136 #define BIND_TO_RENDER_LOOP(function) \
137 (DCHECK(main_loop_->BelongsToCurrentThread()), \
138 media::BindToCurrentLoop(base::Bind(function, AsWeakPtr())))
140 static void LogMediaSourceError(const scoped_refptr
<media::MediaLog
>& media_log
,
141 const std::string
& error
) {
142 media_log
->AddEvent(media_log
->CreateMediaSourceErrorEvent(error
));
145 WebMediaPlayerImpl::WebMediaPlayerImpl(
146 blink::WebLocalFrame
* frame
,
147 blink::WebMediaPlayerClient
* client
,
148 base::WeakPtr
<WebMediaPlayerDelegate
> delegate
,
149 const WebMediaPlayerParams
& params
)
151 network_state_(WebMediaPlayer::NetworkStateEmpty
),
152 ready_state_(WebMediaPlayer::ReadyStateHaveNothing
),
153 main_loop_(base::MessageLoopProxy::current()),
155 RenderThreadImpl::current()->GetMediaThreadMessageLoopProxy()),
156 media_log_(new RenderMediaLog()),
157 pipeline_(media_loop_
, media_log_
.get()),
161 playback_rate_(0.0f
),
162 pending_seek_(false),
163 pending_seek_seconds_(0.0f
),
166 defer_load_cb_(params
.defer_load_cb()),
167 accelerated_compositing_reported_(false),
168 incremented_externally_allocated_memory_(false),
169 gpu_factories_(RenderThreadImpl::current()->GetGpuFactories()),
170 supports_save_(true),
172 chunk_demuxer_(NULL
),
173 // Threaded compositing isn't enabled universally yet.
174 compositor_task_runner_(
175 RenderThreadImpl::current()->compositor_message_loop_proxy()
176 ? RenderThreadImpl::current()->compositor_message_loop_proxy()
177 : base::MessageLoopProxy::current()),
178 compositor_(new VideoFrameCompositor(
179 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNaturalSizeChanged
),
180 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnOpacityChanged
))),
181 text_track_index_(0),
183 media_log_
->AddEvent(
184 media_log_
->CreateEvent(media::MediaLogEvent::WEBMEDIAPLAYER_CREATED
));
186 // |gpu_factories_| requires that its entry points be called on its
187 // |GetTaskRunner()|. Since |pipeline_| will own decoders created from the
188 // factories, require that their message loops are identical.
189 DCHECK(!gpu_factories_
|| (gpu_factories_
->GetTaskRunner() == media_loop_
));
191 // Let V8 know we started new thread if we did not do it yet.
192 // Made separate task to avoid deletion of player currently being created.
193 // Also, delaying GC until after player starts gets rid of starting lag --
194 // collection happens in parallel with playing.
196 // TODO(enal): remove when we get rid of per-audio-stream thread.
197 main_loop_
->PostTask(
199 base::Bind(&WebMediaPlayerImpl::IncrementExternallyAllocatedMemory
,
202 // Use the null sink if no sink was provided.
203 audio_source_provider_
= new WebAudioSourceProviderImpl(
204 params
.audio_renderer_sink().get()
205 ? params
.audio_renderer_sink()
206 : new media::NullAudioSink(media_loop_
));
209 WebMediaPlayerImpl::~WebMediaPlayerImpl() {
210 client_
->setWebLayer(NULL
);
212 DCHECK(main_loop_
->BelongsToCurrentThread());
213 media_log_
->AddEvent(
214 media_log_
->CreateEvent(media::MediaLogEvent::WEBMEDIAPLAYER_DESTROYED
));
217 delegate_
->PlayerGone(this);
219 // Abort any pending IO so stopping the pipeline doesn't get blocked.
221 data_source_
->Abort();
222 if (chunk_demuxer_
) {
223 chunk_demuxer_
->Shutdown();
224 chunk_demuxer_
= NULL
;
227 gpu_factories_
= NULL
;
229 // Make sure to kill the pipeline so there's no more media threads running.
230 // Note: stopping the pipeline might block for a long time.
231 base::WaitableEvent
waiter(false, false);
233 base::Bind(&base::WaitableEvent::Signal
, base::Unretained(&waiter
)));
236 compositor_task_runner_
->DeleteSoon(FROM_HERE
, compositor_
);
238 // Let V8 know we are not using extra resources anymore.
239 if (incremented_externally_allocated_memory_
) {
240 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
241 -kPlayerExtraMemory
);
242 incremented_externally_allocated_memory_
= false;
246 void WebMediaPlayerImpl::load(LoadType load_type
, const blink::WebURL
& url
,
247 CORSMode cors_mode
) {
248 DVLOG(1) << __FUNCTION__
<< "(" << load_type
<< ", " << url
<< ", "
250 if (!defer_load_cb_
.is_null()) {
251 defer_load_cb_
.Run(base::Bind(
252 &WebMediaPlayerImpl::DoLoad
, AsWeakPtr(), load_type
, url
, cors_mode
));
255 DoLoad(load_type
, url
, cors_mode
);
258 void WebMediaPlayerImpl::DoLoad(LoadType load_type
,
259 const blink::WebURL
& url
,
260 CORSMode cors_mode
) {
261 DCHECK(main_loop_
->BelongsToCurrentThread());
264 ReportMediaSchemeUma(gurl
);
266 // Set subresource URL for crash reporting.
267 base::debug::SetCrashKeyValue("subresource_url", gurl
.spec());
269 load_type_
= load_type
;
271 // Handle any volume/preload changes that occurred before load().
272 setVolume(client_
->volume());
273 setPreload(client_
->preload());
275 SetNetworkState(WebMediaPlayer::NetworkStateLoading
);
276 SetReadyState(WebMediaPlayer::ReadyStateHaveNothing
);
277 media_log_
->AddEvent(media_log_
->CreateLoadEvent(url
.spec()));
279 // Media source pipelines can start immediately.
280 if (load_type
== LoadTypeMediaSource
) {
281 supports_save_
= false;
286 // Otherwise it's a regular request which requires resolving the URL first.
287 data_source_
.reset(new BufferedDataSource(
289 static_cast<BufferedResourceLoader::CORSMode
>(cors_mode
),
293 &buffered_data_source_host_
,
294 base::Bind(&WebMediaPlayerImpl::NotifyDownloading
, AsWeakPtr())));
295 data_source_
->Initialize(
296 base::Bind(&WebMediaPlayerImpl::DataSourceInitialized
, AsWeakPtr()));
299 void WebMediaPlayerImpl::play() {
300 DVLOG(1) << __FUNCTION__
;
301 DCHECK(main_loop_
->BelongsToCurrentThread());
304 pipeline_
.SetPlaybackRate(playback_rate_
);
306 data_source_
->MediaIsPlaying();
308 media_log_
->AddEvent(media_log_
->CreateEvent(media::MediaLogEvent::PLAY
));
311 delegate_
->DidPlay(this);
314 void WebMediaPlayerImpl::pause() {
315 DVLOG(1) << __FUNCTION__
;
316 DCHECK(main_loop_
->BelongsToCurrentThread());
319 pipeline_
.SetPlaybackRate(0.0f
);
321 data_source_
->MediaIsPaused();
322 paused_time_
= pipeline_
.GetMediaTime();
324 media_log_
->AddEvent(media_log_
->CreateEvent(media::MediaLogEvent::PAUSE
));
327 delegate_
->DidPause(this);
330 bool WebMediaPlayerImpl::supportsSave() const {
331 DCHECK(main_loop_
->BelongsToCurrentThread());
332 return supports_save_
;
335 void WebMediaPlayerImpl::seek(double seconds
) {
336 DVLOG(1) << __FUNCTION__
<< "(" << seconds
<< ")";
337 DCHECK(main_loop_
->BelongsToCurrentThread());
339 if (ready_state_
> WebMediaPlayer::ReadyStateHaveMetadata
)
340 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata
);
342 base::TimeDelta seek_time
= ConvertSecondsToTimestamp(seconds
);
344 if (starting_
|| seeking_
) {
345 pending_seek_
= true;
346 pending_seek_seconds_
= seconds
;
348 chunk_demuxer_
->CancelPendingSeek(seek_time
);
352 media_log_
->AddEvent(media_log_
->CreateSeekEvent(seconds
));
354 // Update our paused time.
356 paused_time_
= seek_time
;
361 chunk_demuxer_
->StartWaitingForSeek(seek_time
);
363 // Kick off the asynchronous seek!
366 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineSeek
));
369 void WebMediaPlayerImpl::setRate(double rate
) {
370 DVLOG(1) << __FUNCTION__
<< "(" << rate
<< ")";
371 DCHECK(main_loop_
->BelongsToCurrentThread());
373 // TODO(kylep): Remove when support for negatives is added. Also, modify the
374 // following checks so rewind uses reasonable values also.
378 // Limit rates to reasonable values by clamping.
382 else if (rate
> kMaxRate
)
386 playback_rate_
= rate
;
388 pipeline_
.SetPlaybackRate(rate
);
390 data_source_
->MediaPlaybackRateChanged(rate
);
394 void WebMediaPlayerImpl::setVolume(double volume
) {
395 DVLOG(1) << __FUNCTION__
<< "(" << volume
<< ")";
396 DCHECK(main_loop_
->BelongsToCurrentThread());
398 pipeline_
.SetVolume(volume
);
401 #define COMPILE_ASSERT_MATCHING_ENUM(webkit_name, chromium_name) \
402 COMPILE_ASSERT(static_cast<int>(WebMediaPlayer::webkit_name) == \
403 static_cast<int>(content::chromium_name), \
405 COMPILE_ASSERT_MATCHING_ENUM(PreloadNone
, NONE
);
406 COMPILE_ASSERT_MATCHING_ENUM(PreloadMetaData
, METADATA
);
407 COMPILE_ASSERT_MATCHING_ENUM(PreloadAuto
, AUTO
);
408 #undef COMPILE_ASSERT_MATCHING_ENUM
410 void WebMediaPlayerImpl::setPreload(WebMediaPlayer::Preload preload
) {
411 DVLOG(1) << __FUNCTION__
<< "(" << preload
<< ")";
412 DCHECK(main_loop_
->BelongsToCurrentThread());
415 data_source_
->SetPreload(static_cast<content::Preload
>(preload
));
418 bool WebMediaPlayerImpl::hasVideo() const {
419 DCHECK(main_loop_
->BelongsToCurrentThread());
421 return pipeline_metadata_
.has_video
;
424 bool WebMediaPlayerImpl::hasAudio() const {
425 DCHECK(main_loop_
->BelongsToCurrentThread());
427 return pipeline_metadata_
.has_audio
;
430 blink::WebSize
WebMediaPlayerImpl::naturalSize() const {
431 DCHECK(main_loop_
->BelongsToCurrentThread());
433 return blink::WebSize(pipeline_metadata_
.natural_size
);
436 bool WebMediaPlayerImpl::paused() const {
437 DCHECK(main_loop_
->BelongsToCurrentThread());
439 return pipeline_
.GetPlaybackRate() == 0.0f
;
442 bool WebMediaPlayerImpl::seeking() const {
443 DCHECK(main_loop_
->BelongsToCurrentThread());
445 if (ready_state_
== WebMediaPlayer::ReadyStateHaveNothing
)
451 double WebMediaPlayerImpl::duration() const {
452 DCHECK(main_loop_
->BelongsToCurrentThread());
454 if (ready_state_
== WebMediaPlayer::ReadyStateHaveNothing
)
455 return std::numeric_limits
<double>::quiet_NaN();
457 return GetPipelineDuration();
460 double WebMediaPlayerImpl::timelineOffset() const {
461 DCHECK(main_loop_
->BelongsToCurrentThread());
463 if (pipeline_metadata_
.timeline_offset
.is_null())
464 return std::numeric_limits
<double>::quiet_NaN();
466 return pipeline_metadata_
.timeline_offset
.ToJsTime();
469 double WebMediaPlayerImpl::currentTime() const {
470 DCHECK(main_loop_
->BelongsToCurrentThread());
471 return (paused_
? paused_time_
: pipeline_
.GetMediaTime()).InSecondsF();
474 WebMediaPlayer::NetworkState
WebMediaPlayerImpl::networkState() const {
475 DCHECK(main_loop_
->BelongsToCurrentThread());
476 return network_state_
;
479 WebMediaPlayer::ReadyState
WebMediaPlayerImpl::readyState() const {
480 DCHECK(main_loop_
->BelongsToCurrentThread());
484 blink::WebTimeRanges
WebMediaPlayerImpl::buffered() const {
485 DCHECK(main_loop_
->BelongsToCurrentThread());
486 media::Ranges
<base::TimeDelta
> buffered_time_ranges
=
487 pipeline_
.GetBufferedTimeRanges();
488 buffered_data_source_host_
.AddBufferedTimeRanges(
489 &buffered_time_ranges
, pipeline_
.GetMediaDuration());
490 return ConvertToWebTimeRanges(buffered_time_ranges
);
493 double WebMediaPlayerImpl::maxTimeSeekable() const {
494 DCHECK(main_loop_
->BelongsToCurrentThread());
496 // If we haven't even gotten to ReadyStateHaveMetadata yet then just
497 // return 0 so that the seekable range is empty.
498 if (ready_state_
< WebMediaPlayer::ReadyStateHaveMetadata
)
501 // We don't support seeking in streaming media.
502 if (data_source_
&& data_source_
->IsStreaming())
507 bool WebMediaPlayerImpl::didLoadingProgress() {
508 DCHECK(main_loop_
->BelongsToCurrentThread());
509 bool pipeline_progress
= pipeline_
.DidLoadingProgress();
510 bool data_progress
= buffered_data_source_host_
.DidLoadingProgress();
511 return pipeline_progress
|| data_progress
;
514 void WebMediaPlayerImpl::paint(WebCanvas
* canvas
,
516 unsigned char alpha
) {
517 DCHECK(main_loop_
->BelongsToCurrentThread());
518 TRACE_EVENT0("media", "WebMediaPlayerImpl:paint");
520 if (!accelerated_compositing_reported_
) {
521 accelerated_compositing_reported_
= true;
522 // Normally paint() is only called in non-accelerated rendering, but there
523 // are exceptions such as webgl where compositing is used in the WebView but
524 // video frames are still rendered to a canvas.
525 UMA_HISTOGRAM_BOOLEAN(
526 "Media.AcceleratedCompositingActive",
527 frame_
->view()->isAcceleratedCompositingActive());
530 // TODO(scherkus): Clarify paint() API contract to better understand when and
531 // why it's being called. For example, today paint() is called when:
532 // - We haven't reached HAVE_CURRENT_DATA and need to paint black
533 // - We're painting to a canvas
534 // See http://crbug.com/341225 http://crbug.com/342621 for details.
535 scoped_refptr
<media::VideoFrame
> video_frame
=
536 GetCurrentFrameFromCompositor();
538 gfx::Rect
gfx_rect(rect
);
539 skcanvas_video_renderer_
.Paint(video_frame
.get(), canvas
, gfx_rect
, alpha
);
542 bool WebMediaPlayerImpl::hasSingleSecurityOrigin() const {
544 return data_source_
->HasSingleOrigin();
548 bool WebMediaPlayerImpl::didPassCORSAccessCheck() const {
550 return data_source_
->DidPassCORSAccessCheck();
554 double WebMediaPlayerImpl::mediaTimeForTimeValue(double timeValue
) const {
555 return ConvertSecondsToTimestamp(timeValue
).InSecondsF();
558 unsigned WebMediaPlayerImpl::decodedFrameCount() const {
559 DCHECK(main_loop_
->BelongsToCurrentThread());
561 media::PipelineStatistics stats
= pipeline_
.GetStatistics();
562 return stats
.video_frames_decoded
;
565 unsigned WebMediaPlayerImpl::droppedFrameCount() const {
566 DCHECK(main_loop_
->BelongsToCurrentThread());
568 media::PipelineStatistics stats
= pipeline_
.GetStatistics();
569 return stats
.video_frames_dropped
;
572 unsigned WebMediaPlayerImpl::audioDecodedByteCount() const {
573 DCHECK(main_loop_
->BelongsToCurrentThread());
575 media::PipelineStatistics stats
= pipeline_
.GetStatistics();
576 return stats
.audio_bytes_decoded
;
579 unsigned WebMediaPlayerImpl::videoDecodedByteCount() const {
580 DCHECK(main_loop_
->BelongsToCurrentThread());
582 media::PipelineStatistics stats
= pipeline_
.GetStatistics();
583 return stats
.video_bytes_decoded
;
586 bool WebMediaPlayerImpl::copyVideoTextureToPlatformTexture(
587 blink::WebGraphicsContext3D
* web_graphics_context
,
588 unsigned int texture
,
590 unsigned int internal_format
,
592 bool premultiply_alpha
,
594 TRACE_EVENT0("media", "WebMediaPlayerImpl:copyVideoTextureToPlatformTexture");
596 scoped_refptr
<media::VideoFrame
> video_frame
=
597 GetCurrentFrameFromCompositor();
601 if (video_frame
->format() != media::VideoFrame::NATIVE_TEXTURE
)
604 const gpu::MailboxHolder
* mailbox_holder
= video_frame
->mailbox_holder();
605 if (mailbox_holder
->texture_target
!= GL_TEXTURE_2D
)
608 // Since this method changes which texture is bound to the TEXTURE_2D target,
609 // ideally it would restore the currently-bound texture before returning.
610 // The cost of getIntegerv is sufficiently high, however, that we want to
611 // avoid it in user builds. As a result assume (below) that |texture| is
612 // bound when this method is called, and only verify this fact when
615 GLint bound_texture
= 0;
616 web_graphics_context
->getIntegerv(GL_TEXTURE_BINDING_2D
, &bound_texture
);
617 DCHECK_EQ(static_cast<GLuint
>(bound_texture
), texture
);
620 uint32 source_texture
= web_graphics_context
->createTexture();
622 web_graphics_context
->waitSyncPoint(mailbox_holder
->sync_point
);
623 web_graphics_context
->bindTexture(GL_TEXTURE_2D
, source_texture
);
624 web_graphics_context
->consumeTextureCHROMIUM(GL_TEXTURE_2D
,
625 mailbox_holder
->mailbox
.name
);
627 // The video is stored in a unmultiplied format, so premultiply
629 web_graphics_context
->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM
,
631 // Application itself needs to take care of setting the right flip_y
632 // value down to get the expected result.
633 // flip_y==true means to reverse the video orientation while
634 // flip_y==false means to keep the intrinsic orientation.
635 web_graphics_context
->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM
, flip_y
);
636 web_graphics_context
->copyTextureCHROMIUM(GL_TEXTURE_2D
,
642 web_graphics_context
->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM
, false);
643 web_graphics_context
->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM
,
646 // Restore the state for TEXTURE_2D binding point as mentioned above.
647 web_graphics_context
->bindTexture(GL_TEXTURE_2D
, texture
);
649 web_graphics_context
->deleteTexture(source_texture
);
650 web_graphics_context
->flush();
651 video_frame
->AppendReleaseSyncPoint(web_graphics_context
->insertSyncPoint());
655 // Helper functions to report media EME related stats to UMA. They follow the
656 // convention of more commonly used macros UMA_HISTOGRAM_ENUMERATION and
657 // UMA_HISTOGRAM_COUNTS. The reason that we cannot use those macros directly is
658 // that UMA_* macros require the names to be constant throughout the process'
660 static void EmeUMAHistogramEnumeration(const std::string
& key_system
,
661 const std::string
& method
,
663 int boundary_value
) {
664 base::LinearHistogram::FactoryGet(
665 kMediaEme
+ KeySystemNameForUMA(key_system
) + "." + method
,
666 1, boundary_value
, boundary_value
+ 1,
667 base::Histogram::kUmaTargetedHistogramFlag
)->Add(sample
);
670 static void EmeUMAHistogramCounts(const std::string
& key_system
,
671 const std::string
& method
,
673 // Use the same parameters as UMA_HISTOGRAM_COUNTS.
674 base::Histogram::FactoryGet(
675 kMediaEme
+ KeySystemNameForUMA(key_system
) + "." + method
,
676 1, 1000000, 50, base::Histogram::kUmaTargetedHistogramFlag
)->Add(sample
);
679 // Helper enum for reporting generateKeyRequest/addKey histograms.
680 enum MediaKeyException
{
683 kKeySystemNotSupported
,
685 kMaxMediaKeyException
688 static MediaKeyException
MediaKeyExceptionForUMA(
689 WebMediaPlayer::MediaKeyException e
) {
691 case WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported
:
692 return kKeySystemNotSupported
;
693 case WebMediaPlayer::MediaKeyExceptionInvalidPlayerState
:
694 return kInvalidPlayerState
;
695 case WebMediaPlayer::MediaKeyExceptionNoError
:
698 return kUnknownResultId
;
702 // Helper for converting |key_system| name and exception |e| to a pair of enum
703 // values from above, for reporting to UMA.
704 static void ReportMediaKeyExceptionToUMA(const std::string
& method
,
705 const std::string
& key_system
,
706 WebMediaPlayer::MediaKeyException e
) {
707 MediaKeyException result_id
= MediaKeyExceptionForUMA(e
);
708 DCHECK_NE(result_id
, kUnknownResultId
) << e
;
709 EmeUMAHistogramEnumeration(
710 key_system
, method
, result_id
, kMaxMediaKeyException
);
713 // Convert a WebString to ASCII, falling back on an empty string in the case
714 // of a non-ASCII string.
715 static std::string
ToASCIIOrEmpty(const blink::WebString
& string
) {
716 return base::IsStringASCII(string
) ? base::UTF16ToASCII(string
)
720 WebMediaPlayer::MediaKeyException
721 WebMediaPlayerImpl::generateKeyRequest(const WebString
& key_system
,
722 const unsigned char* init_data
,
723 unsigned init_data_length
) {
724 DVLOG(1) << "generateKeyRequest: " << base::string16(key_system
) << ": "
725 << std::string(reinterpret_cast<const char*>(init_data
),
726 static_cast<size_t>(init_data_length
));
728 std::string ascii_key_system
=
729 GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system
));
731 WebMediaPlayer::MediaKeyException e
=
732 GenerateKeyRequestInternal(ascii_key_system
, init_data
, init_data_length
);
733 ReportMediaKeyExceptionToUMA("generateKeyRequest", ascii_key_system
, e
);
737 // Guess the type of |init_data|. This is only used to handle some corner cases
738 // so we keep it as simple as possible without breaking major use cases.
739 static std::string
GuessInitDataType(const unsigned char* init_data
,
740 unsigned init_data_length
) {
741 // Most WebM files use KeyId of 16 bytes. MP4 init data are always >16 bytes.
742 if (init_data_length
== 16)
748 WebMediaPlayer::MediaKeyException
749 WebMediaPlayerImpl::GenerateKeyRequestInternal(const std::string
& key_system
,
750 const unsigned char* init_data
,
751 unsigned init_data_length
) {
752 DCHECK(main_loop_
->BelongsToCurrentThread());
754 if (!IsConcreteSupportedKeySystem(key_system
))
755 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported
;
757 // We do not support run-time switching between key systems for now.
758 if (current_key_system_
.empty()) {
759 if (!proxy_decryptor_
) {
760 proxy_decryptor_
.reset(new ProxyDecryptor(
761 #if defined(ENABLE_PEPPER_CDMS)
762 // Create() must be called synchronously as |frame_| may not be
764 base::Bind(&PepperCdmWrapperImpl::Create
, frame_
),
766 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnKeyAdded
),
767 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnKeyError
),
768 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnKeyMessage
)));
771 GURL
security_origin(frame_
->document().securityOrigin().toString());
772 if (!proxy_decryptor_
->InitializeCDM(key_system
, security_origin
))
773 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported
;
775 if (proxy_decryptor_
&& !decryptor_ready_cb_
.is_null()) {
776 base::ResetAndReturn(&decryptor_ready_cb_
)
777 .Run(proxy_decryptor_
->GetDecryptor());
780 current_key_system_
= key_system
;
781 } else if (key_system
!= current_key_system_
) {
782 return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState
;
785 std::string init_data_type
= init_data_type_
;
786 if (init_data_type
.empty())
787 init_data_type
= GuessInitDataType(init_data
, init_data_length
);
789 // TODO(xhwang): We assume all streams are from the same container (thus have
790 // the same "type") for now. In the future, the "type" should be passed down
791 // from the application.
792 if (!proxy_decryptor_
->GenerateKeyRequest(
793 init_data_type
, init_data
, init_data_length
)) {
794 current_key_system_
.clear();
795 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported
;
798 return WebMediaPlayer::MediaKeyExceptionNoError
;
801 WebMediaPlayer::MediaKeyException
WebMediaPlayerImpl::addKey(
802 const WebString
& key_system
,
803 const unsigned char* key
,
805 const unsigned char* init_data
,
806 unsigned init_data_length
,
807 const WebString
& session_id
) {
808 DVLOG(1) << "addKey: " << base::string16(key_system
) << ": "
809 << std::string(reinterpret_cast<const char*>(key
),
810 static_cast<size_t>(key_length
)) << ", "
811 << std::string(reinterpret_cast<const char*>(init_data
),
812 static_cast<size_t>(init_data_length
)) << " ["
813 << base::string16(session_id
) << "]";
815 std::string ascii_key_system
=
816 GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system
));
817 std::string ascii_session_id
= ToASCIIOrEmpty(session_id
);
819 WebMediaPlayer::MediaKeyException e
= AddKeyInternal(ascii_key_system
,
825 ReportMediaKeyExceptionToUMA("addKey", ascii_key_system
, e
);
829 WebMediaPlayer::MediaKeyException
WebMediaPlayerImpl::AddKeyInternal(
830 const std::string
& key_system
,
831 const unsigned char* key
,
833 const unsigned char* init_data
,
834 unsigned init_data_length
,
835 const std::string
& session_id
) {
837 DCHECK_GT(key_length
, 0u);
839 if (!IsConcreteSupportedKeySystem(key_system
))
840 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported
;
842 if (current_key_system_
.empty() || key_system
!= current_key_system_
)
843 return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState
;
845 proxy_decryptor_
->AddKey(
846 key
, key_length
, init_data
, init_data_length
, session_id
);
847 return WebMediaPlayer::MediaKeyExceptionNoError
;
850 WebMediaPlayer::MediaKeyException
WebMediaPlayerImpl::cancelKeyRequest(
851 const WebString
& key_system
,
852 const WebString
& session_id
) {
853 DVLOG(1) << "cancelKeyRequest: " << base::string16(key_system
) << ": "
854 << " [" << base::string16(session_id
) << "]";
856 std::string ascii_key_system
=
857 GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system
));
858 std::string ascii_session_id
= ToASCIIOrEmpty(session_id
);
860 WebMediaPlayer::MediaKeyException e
=
861 CancelKeyRequestInternal(ascii_key_system
, ascii_session_id
);
862 ReportMediaKeyExceptionToUMA("cancelKeyRequest", ascii_key_system
, e
);
866 WebMediaPlayer::MediaKeyException
WebMediaPlayerImpl::CancelKeyRequestInternal(
867 const std::string
& key_system
,
868 const std::string
& session_id
) {
869 if (!IsConcreteSupportedKeySystem(key_system
))
870 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported
;
872 if (current_key_system_
.empty() || key_system
!= current_key_system_
)
873 return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState
;
875 proxy_decryptor_
->CancelKeyRequest(session_id
);
876 return WebMediaPlayer::MediaKeyExceptionNoError
;
879 void WebMediaPlayerImpl::setContentDecryptionModule(
880 blink::WebContentDecryptionModule
* cdm
) {
881 DCHECK(main_loop_
->BelongsToCurrentThread());
883 // TODO(xhwang): Support setMediaKeys(0) if necessary: http://crbug.com/330324
887 web_cdm_
= ToWebContentDecryptionModuleImpl(cdm
);
889 if (web_cdm_
&& !decryptor_ready_cb_
.is_null())
890 base::ResetAndReturn(&decryptor_ready_cb_
).Run(web_cdm_
->GetDecryptor());
893 void WebMediaPlayerImpl::InvalidateOnMainThread() {
894 DCHECK(main_loop_
->BelongsToCurrentThread());
895 TRACE_EVENT0("media", "WebMediaPlayerImpl::InvalidateOnMainThread");
900 void WebMediaPlayerImpl::OnPipelineSeek(PipelineStatus status
) {
901 DVLOG(1) << __FUNCTION__
<< "(" << status
<< ")";
902 DCHECK(main_loop_
->BelongsToCurrentThread());
906 pending_seek_
= false;
907 seek(pending_seek_seconds_
);
911 if (status
!= media::PIPELINE_OK
) {
912 OnPipelineError(status
);
916 // Update our paused time.
918 paused_time_
= pipeline_
.GetMediaTime();
920 client_
->timeChanged();
923 void WebMediaPlayerImpl::OnPipelineEnded() {
924 DVLOG(1) << __FUNCTION__
;
925 DCHECK(main_loop_
->BelongsToCurrentThread());
926 client_
->timeChanged();
929 void WebMediaPlayerImpl::OnPipelineError(PipelineStatus error
) {
930 DCHECK(main_loop_
->BelongsToCurrentThread());
931 DCHECK_NE(error
, media::PIPELINE_OK
);
933 if (ready_state_
== WebMediaPlayer::ReadyStateHaveNothing
) {
934 // Any error that occurs before reaching ReadyStateHaveMetadata should
935 // be considered a format error.
936 SetNetworkState(WebMediaPlayer::NetworkStateFormatError
);
938 // TODO(scherkus): This should be handled by HTMLMediaElement and controls
939 // should know when to invalidate themselves http://crbug.com/337015
940 InvalidateOnMainThread();
944 SetNetworkState(PipelineErrorToNetworkState(error
));
946 if (error
== media::PIPELINE_ERROR_DECRYPT
)
947 EmeUMAHistogramCounts(current_key_system_
, "DecryptError", 1);
949 // TODO(scherkus): This should be handled by HTMLMediaElement and controls
950 // should know when to invalidate themselves http://crbug.com/337015
951 InvalidateOnMainThread();
954 void WebMediaPlayerImpl::OnPipelineMetadata(
955 media::PipelineMetadata metadata
) {
956 DVLOG(1) << __FUNCTION__
;
958 pipeline_metadata_
= metadata
;
960 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata
);
963 DCHECK(!video_weblayer_
);
964 video_weblayer_
.reset(
965 new webkit::WebLayerImpl(cc::VideoLayer::Create(compositor_
)));
966 video_weblayer_
->setOpaque(opaque_
);
967 client_
->setWebLayer(video_weblayer_
.get());
970 // TODO(scherkus): This should be handled by HTMLMediaElement and controls
971 // should know when to invalidate themselves http://crbug.com/337015
972 InvalidateOnMainThread();
975 void WebMediaPlayerImpl::OnPipelinePrerollCompleted() {
976 DVLOG(1) << __FUNCTION__
;
978 // Only transition to ReadyStateHaveEnoughData if we don't have
979 // any pending seeks because the transition can cause Blink to
980 // report that the most recent seek has completed.
981 if (!pending_seek_
) {
982 SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData
);
984 // TODO(scherkus): This should be handled by HTMLMediaElement and controls
985 // should know when to invalidate themselves http://crbug.com/337015
986 InvalidateOnMainThread();
990 void WebMediaPlayerImpl::OnDemuxerOpened() {
991 DCHECK(main_loop_
->BelongsToCurrentThread());
992 client_
->mediaSourceOpened(new WebMediaSourceImpl(
993 chunk_demuxer_
, base::Bind(&LogMediaSourceError
, media_log_
)));
996 void WebMediaPlayerImpl::OnKeyAdded(const std::string
& session_id
) {
997 DCHECK(main_loop_
->BelongsToCurrentThread());
998 EmeUMAHistogramCounts(current_key_system_
, "KeyAdded", 1);
1000 WebString::fromUTF8(GetPrefixedKeySystemName(current_key_system_
)),
1001 WebString::fromUTF8(session_id
));
1004 void WebMediaPlayerImpl::OnNeedKey(const std::string
& type
,
1005 const std::vector
<uint8
>& init_data
) {
1006 DCHECK(main_loop_
->BelongsToCurrentThread());
1008 // Do not fire NeedKey event if encrypted media is not enabled.
1009 if (!blink::WebRuntimeFeatures::isPrefixedEncryptedMediaEnabled() &&
1010 !blink::WebRuntimeFeatures::isEncryptedMediaEnabled()) {
1014 UMA_HISTOGRAM_COUNTS(kMediaEme
+ std::string("NeedKey"), 1);
1016 DCHECK(init_data_type_
.empty() || type
.empty() || type
== init_data_type_
);
1017 if (init_data_type_
.empty())
1018 init_data_type_
= type
;
1020 const uint8
* init_data_ptr
= init_data
.empty() ? NULL
: &init_data
[0];
1022 WebString::fromUTF8(type
), init_data_ptr
, init_data
.size());
1025 void WebMediaPlayerImpl::OnAddTextTrack(
1026 const media::TextTrackConfig
& config
,
1027 const media::AddTextTrackDoneCB
& done_cb
) {
1028 DCHECK(main_loop_
->BelongsToCurrentThread());
1030 const WebInbandTextTrackImpl::Kind web_kind
=
1031 static_cast<WebInbandTextTrackImpl::Kind
>(config
.kind());
1032 const blink::WebString web_label
=
1033 blink::WebString::fromUTF8(config
.label());
1034 const blink::WebString web_language
=
1035 blink::WebString::fromUTF8(config
.language());
1036 const blink::WebString web_id
=
1037 blink::WebString::fromUTF8(config
.id());
1039 scoped_ptr
<WebInbandTextTrackImpl
> web_inband_text_track(
1040 new WebInbandTextTrackImpl(web_kind
, web_label
, web_language
, web_id
,
1041 text_track_index_
++));
1043 scoped_ptr
<media::TextTrack
> text_track(
1044 new TextTrackImpl(main_loop_
, client_
, web_inband_text_track
.Pass()));
1046 done_cb
.Run(text_track
.Pass());
1049 void WebMediaPlayerImpl::OnKeyError(const std::string
& session_id
,
1050 media::MediaKeys::KeyError error_code
,
1051 uint32 system_code
) {
1052 DCHECK(main_loop_
->BelongsToCurrentThread());
1054 EmeUMAHistogramEnumeration(current_key_system_
, "KeyError",
1055 error_code
, media::MediaKeys::kMaxKeyError
);
1057 unsigned short short_system_code
= 0;
1058 if (system_code
> std::numeric_limits
<unsigned short>::max()) {
1059 LOG(WARNING
) << "system_code exceeds unsigned short limit.";
1060 short_system_code
= std::numeric_limits
<unsigned short>::max();
1062 short_system_code
= static_cast<unsigned short>(system_code
);
1066 WebString::fromUTF8(GetPrefixedKeySystemName(current_key_system_
)),
1067 WebString::fromUTF8(session_id
),
1068 static_cast<blink::WebMediaPlayerClient::MediaKeyErrorCode
>(error_code
),
1072 void WebMediaPlayerImpl::OnKeyMessage(const std::string
& session_id
,
1073 const std::vector
<uint8
>& message
,
1074 const GURL
& destination_url
) {
1075 DCHECK(main_loop_
->BelongsToCurrentThread());
1077 DCHECK(destination_url
.is_empty() || destination_url
.is_valid());
1079 client_
->keyMessage(
1080 WebString::fromUTF8(GetPrefixedKeySystemName(current_key_system_
)),
1081 WebString::fromUTF8(session_id
),
1082 message
.empty() ? NULL
: &message
[0],
1087 void WebMediaPlayerImpl::DataSourceInitialized(bool success
) {
1088 DCHECK(main_loop_
->BelongsToCurrentThread());
1091 SetNetworkState(WebMediaPlayer::NetworkStateFormatError
);
1093 // TODO(scherkus): This should be handled by HTMLMediaElement and controls
1094 // should know when to invalidate themselves http://crbug.com/337015
1095 InvalidateOnMainThread();
1102 void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading
) {
1103 if (!is_downloading
&& network_state_
== WebMediaPlayer::NetworkStateLoading
)
1104 SetNetworkState(WebMediaPlayer::NetworkStateIdle
);
1105 else if (is_downloading
&& network_state_
== WebMediaPlayer::NetworkStateIdle
)
1106 SetNetworkState(WebMediaPlayer::NetworkStateLoading
);
1107 media_log_
->AddEvent(
1108 media_log_
->CreateBooleanEvent(
1109 media::MediaLogEvent::NETWORK_ACTIVITY_SET
,
1110 "is_downloading_data", is_downloading
));
1113 void WebMediaPlayerImpl::StartPipeline() {
1114 DCHECK(main_loop_
->BelongsToCurrentThread());
1115 const CommandLine
* cmd_line
= CommandLine::ForCurrentProcess();
1117 // Keep track if this is a MSE or non-MSE playback.
1118 UMA_HISTOGRAM_BOOLEAN("Media.MSE.Playback",
1119 (load_type_
== LoadTypeMediaSource
));
1121 media::LogCB mse_log_cb
;
1123 // Figure out which demuxer to use.
1124 if (load_type_
!= LoadTypeMediaSource
) {
1125 DCHECK(!chunk_demuxer_
);
1126 DCHECK(data_source_
);
1128 demuxer_
.reset(new media::FFmpegDemuxer(
1129 media_loop_
, data_source_
.get(),
1130 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNeedKey
),
1133 DCHECK(!chunk_demuxer_
);
1134 DCHECK(!data_source_
);
1136 mse_log_cb
= base::Bind(&LogMediaSourceError
, media_log_
);
1138 chunk_demuxer_
= new media::ChunkDemuxer(
1139 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDemuxerOpened
),
1140 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNeedKey
),
1143 demuxer_
.reset(chunk_demuxer_
);
1146 scoped_ptr
<media::FilterCollection
> filter_collection(
1147 new media::FilterCollection());
1148 filter_collection
->SetDemuxer(demuxer_
.get());
1150 media::SetDecryptorReadyCB set_decryptor_ready_cb
=
1151 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::SetDecryptorReadyCB
);
1153 // Create our audio decoders and renderer.
1154 ScopedVector
<media::AudioDecoder
> audio_decoders
;
1155 audio_decoders
.push_back(new media::FFmpegAudioDecoder(media_loop_
,
1157 audio_decoders
.push_back(new media::OpusAudioDecoder(media_loop_
));
1159 scoped_ptr
<media::AudioRenderer
> audio_renderer(new media::AudioRendererImpl(
1161 audio_source_provider_
.get(),
1162 audio_decoders
.Pass(),
1163 set_decryptor_ready_cb
,
1164 RenderThreadImpl::current()->GetAudioHardwareConfig()));
1165 filter_collection
->SetAudioRenderer(audio_renderer
.Pass());
1167 // Create our video decoders and renderer.
1168 ScopedVector
<media::VideoDecoder
> video_decoders
;
1170 if (gpu_factories_
.get()) {
1171 video_decoders
.push_back(
1172 new media::GpuVideoDecoder(gpu_factories_
, media_log_
));
1175 #if !defined(MEDIA_DISABLE_LIBVPX)
1176 video_decoders
.push_back(new media::VpxVideoDecoder(media_loop_
));
1177 #endif // !defined(MEDIA_DISABLE_LIBVPX)
1179 video_decoders
.push_back(new media::FFmpegVideoDecoder(media_loop_
));
1181 scoped_ptr
<media::VideoRenderer
> video_renderer(
1182 new media::VideoRendererImpl(
1184 video_decoders
.Pass(),
1185 set_decryptor_ready_cb
,
1186 base::Bind(&WebMediaPlayerImpl::FrameReady
, base::Unretained(this)),
1188 filter_collection
->SetVideoRenderer(video_renderer
.Pass());
1190 if (cmd_line
->HasSwitch(switches::kEnableInbandTextTracks
)) {
1191 scoped_ptr
<media::TextRenderer
> text_renderer(
1192 new media::TextRenderer(
1194 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnAddTextTrack
)));
1196 filter_collection
->SetTextRenderer(text_renderer
.Pass());
1199 // ... and we're ready to go!
1202 filter_collection
.Pass(),
1203 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineEnded
),
1204 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineError
),
1205 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineSeek
),
1206 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineMetadata
),
1207 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelinePrerollCompleted
),
1208 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDurationChanged
));
1211 void WebMediaPlayerImpl::SetNetworkState(WebMediaPlayer::NetworkState state
) {
1212 DVLOG(1) << __FUNCTION__
<< "(" << state
<< ")";
1213 DCHECK(main_loop_
->BelongsToCurrentThread());
1214 network_state_
= state
;
1215 // Always notify to ensure client has the latest value.
1216 client_
->networkStateChanged();
1219 void WebMediaPlayerImpl::SetReadyState(WebMediaPlayer::ReadyState state
) {
1220 DVLOG(1) << __FUNCTION__
<< "(" << state
<< ")";
1221 DCHECK(main_loop_
->BelongsToCurrentThread());
1223 if (state
== WebMediaPlayer::ReadyStateHaveEnoughData
&& data_source_
&&
1224 data_source_
->assume_fully_buffered() &&
1225 network_state_
== WebMediaPlayer::NetworkStateLoading
)
1226 SetNetworkState(WebMediaPlayer::NetworkStateLoaded
);
1228 ready_state_
= state
;
1229 // Always notify to ensure client has the latest value.
1230 client_
->readyStateChanged();
1233 blink::WebAudioSourceProvider
* WebMediaPlayerImpl::audioSourceProvider() {
1234 return audio_source_provider_
.get();
1237 void WebMediaPlayerImpl::IncrementExternallyAllocatedMemory() {
1238 DCHECK(main_loop_
->BelongsToCurrentThread());
1239 incremented_externally_allocated_memory_
= true;
1240 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
1241 kPlayerExtraMemory
);
1244 double WebMediaPlayerImpl::GetPipelineDuration() const {
1245 base::TimeDelta duration
= pipeline_
.GetMediaDuration();
1247 // Return positive infinity if the resource is unbounded.
1248 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
1249 if (duration
== media::kInfiniteDuration())
1250 return std::numeric_limits
<double>::infinity();
1252 return duration
.InSecondsF();
1255 void WebMediaPlayerImpl::OnDurationChanged() {
1256 if (ready_state_
== WebMediaPlayer::ReadyStateHaveNothing
)
1259 client_
->durationChanged();
1262 void WebMediaPlayerImpl::OnNaturalSizeChanged(gfx::Size size
) {
1263 DCHECK(main_loop_
->BelongsToCurrentThread());
1264 DCHECK_NE(ready_state_
, WebMediaPlayer::ReadyStateHaveNothing
);
1265 TRACE_EVENT0("media", "WebMediaPlayerImpl::OnNaturalSizeChanged");
1267 media_log_
->AddEvent(
1268 media_log_
->CreateVideoSizeSetEvent(size
.width(), size
.height()));
1269 pipeline_metadata_
.natural_size
= size
;
1271 client_
->sizeChanged();
1274 void WebMediaPlayerImpl::OnOpacityChanged(bool opaque
) {
1275 DCHECK(main_loop_
->BelongsToCurrentThread());
1276 DCHECK_NE(ready_state_
, WebMediaPlayer::ReadyStateHaveNothing
);
1279 if (video_weblayer_
)
1280 video_weblayer_
->setOpaque(opaque_
);
1283 void WebMediaPlayerImpl::FrameReady(
1284 const scoped_refptr
<media::VideoFrame
>& frame
) {
1285 compositor_task_runner_
->PostTask(
1287 base::Bind(&VideoFrameCompositor::UpdateCurrentFrame
,
1288 base::Unretained(compositor_
),
1292 void WebMediaPlayerImpl::SetDecryptorReadyCB(
1293 const media::DecryptorReadyCB
& decryptor_ready_cb
) {
1294 DCHECK(main_loop_
->BelongsToCurrentThread());
1296 // Cancels the previous decryptor request.
1297 if (decryptor_ready_cb
.is_null()) {
1298 if (!decryptor_ready_cb_
.is_null())
1299 base::ResetAndReturn(&decryptor_ready_cb_
).Run(NULL
);
1303 // TODO(xhwang): Support multiple decryptor notification request (e.g. from
1304 // video and audio). The current implementation is okay for the current
1305 // media pipeline since we initialize audio and video decoders in sequence.
1306 // But WebMediaPlayerImpl should not depend on media pipeline's implementation
1308 DCHECK(decryptor_ready_cb_
.is_null());
1310 // Mixed use of prefixed and unprefixed EME APIs is disallowed by Blink.
1311 DCHECK(!proxy_decryptor_
|| !web_cdm_
);
1313 if (proxy_decryptor_
) {
1314 decryptor_ready_cb
.Run(proxy_decryptor_
->GetDecryptor());
1319 decryptor_ready_cb
.Run(web_cdm_
->GetDecryptor());
1323 decryptor_ready_cb_
= decryptor_ready_cb
;
1326 static void GetCurrentFrameAndSignal(
1327 VideoFrameCompositor
* compositor
,
1328 scoped_refptr
<media::VideoFrame
>* video_frame_out
,
1329 base::WaitableEvent
* event
) {
1330 TRACE_EVENT0("media", "GetCurrentFrameAndSignal");
1331 *video_frame_out
= compositor
->GetCurrentFrame();
1335 scoped_refptr
<media::VideoFrame
>
1336 WebMediaPlayerImpl::GetCurrentFrameFromCompositor() {
1337 TRACE_EVENT0("media", "WebMediaPlayerImpl::GetCurrentFrameFromCompositor");
1338 if (compositor_task_runner_
->BelongsToCurrentThread())
1339 return compositor_
->GetCurrentFrame();
1341 // Use a posted task and waitable event instead of a lock otherwise
1342 // WebGL/Canvas can see different content than what the compositor is seeing.
1343 scoped_refptr
<media::VideoFrame
> video_frame
;
1344 base::WaitableEvent
event(false, false);
1345 compositor_task_runner_
->PostTask(FROM_HERE
,
1346 base::Bind(&GetCurrentFrameAndSignal
,
1347 base::Unretained(compositor_
),
1354 } // namespace content