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 is_local_source_(false),
171 supports_save_(true),
173 chunk_demuxer_(NULL),
174 // Threaded compositing isn't enabled universally yet.
175 compositor_task_runner_(
176 RenderThreadImpl::current()->compositor_message_loop_proxy()
177 ? RenderThreadImpl::current()->compositor_message_loop_proxy()
178 : base::MessageLoopProxy::current()),
179 compositor_(new VideoFrameCompositor(
180 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNaturalSizeChanged),
181 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnOpacityChanged))),
182 text_track_index_(0),
184 media_log_->AddEvent(
185 media_log_->CreateEvent(media::MediaLogEvent::WEBMEDIAPLAYER_CREATED));
187 // |gpu_factories_| requires that its entry points be called on its
188 // |GetTaskRunner()|. Since |pipeline_| will own decoders created from the
189 // factories, require that their message loops are identical.
190 DCHECK(!gpu_factories_ || (gpu_factories_->GetTaskRunner() == media_loop_));
192 // Let V8 know we started new thread if we did not do it yet.
193 // Made separate task to avoid deletion of player currently being created.
194 // Also, delaying GC until after player starts gets rid of starting lag --
195 // collection happens in parallel with playing.
197 // TODO(enal): remove when we get rid of per-audio-stream thread.
198 main_loop_->PostTask(
200 base::Bind(&WebMediaPlayerImpl::IncrementExternallyAllocatedMemory,
203 // Use the null sink if no sink was provided.
204 audio_source_provider_ = new WebAudioSourceProviderImpl(
205 params.audio_renderer_sink().get()
206 ? params.audio_renderer_sink()
207 : new media::NullAudioSink(media_loop_));
210 WebMediaPlayerImpl::~WebMediaPlayerImpl() {
211 client_->setWebLayer(NULL);
213 DCHECK(main_loop_->BelongsToCurrentThread());
214 media_log_->AddEvent(
215 media_log_->CreateEvent(media::MediaLogEvent::WEBMEDIAPLAYER_DESTROYED));
218 delegate_->PlayerGone(this);
220 // Abort any pending IO so stopping the pipeline doesn't get blocked.
222 data_source_->Abort();
223 if (chunk_demuxer_) {
224 chunk_demuxer_->Shutdown();
225 chunk_demuxer_ = NULL;
228 gpu_factories_ = NULL;
230 // Make sure to kill the pipeline so there's no more media threads running.
231 // Note: stopping the pipeline might block for a long time.
232 base::WaitableEvent waiter(false, false);
234 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&waiter)));
237 compositor_task_runner_->DeleteSoon(FROM_HERE, compositor_);
239 // Let V8 know we are not using extra resources anymore.
240 if (incremented_externally_allocated_memory_) {
241 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
242 -kPlayerExtraMemory);
243 incremented_externally_allocated_memory_ = false;
249 // Helper enum for reporting scheme histograms.
250 enum URLSchemeForHistogram {
256 kChromeExtensionURLScheme,
257 kJavascriptURLScheme,
262 kMaxURLScheme = kFileSystemScheme // Must be equal to highest enum value.
265 URLSchemeForHistogram URLScheme(const GURL& url) {
266 if (!url.has_scheme()) return kMissingURLScheme;
267 if (url.SchemeIs("http")) return kHttpURLScheme;
268 if (url.SchemeIs("https")) return kHttpsURLScheme;
269 if (url.SchemeIs("ftp")) return kFtpURLScheme;
270 if (url.SchemeIs("chrome-extension")) return kChromeExtensionURLScheme;
271 if (url.SchemeIs("javascript")) return kJavascriptURLScheme;
272 if (url.SchemeIs("file")) return kFileURLScheme;
273 if (url.SchemeIs("blob")) return kBlobURLScheme;
274 if (url.SchemeIs("data")) return kDataURLScheme;
275 if (url.SchemeIs("filesystem")) return kFileSystemScheme;
276 return kUnknownURLScheme;
281 void WebMediaPlayerImpl::load(LoadType load_type, const blink::WebURL& url,
282 CORSMode cors_mode) {
283 if (!defer_load_cb_.is_null()) {
284 defer_load_cb_.Run(base::Bind(
285 &WebMediaPlayerImpl::DoLoad, AsWeakPtr(), load_type, url, cors_mode));
288 DoLoad(load_type, url, cors_mode);
291 void WebMediaPlayerImpl::DoLoad(LoadType load_type,
292 const blink::WebURL& url,
293 CORSMode cors_mode) {
294 DCHECK(main_loop_->BelongsToCurrentThread());
297 UMA_HISTOGRAM_ENUMERATION("Media.URLScheme", URLScheme(gurl), kMaxURLScheme);
299 // Set subresource URL for crash reporting.
300 base::debug::SetCrashKeyValue("subresource_url", gurl.spec());
302 load_type_ = load_type;
304 // Handle any volume/preload changes that occurred before load().
305 setVolume(client_->volume());
306 setPreload(client_->preload());
308 SetNetworkState(WebMediaPlayer::NetworkStateLoading);
309 SetReadyState(WebMediaPlayer::ReadyStateHaveNothing);
310 media_log_->AddEvent(media_log_->CreateLoadEvent(url.spec()));
312 // Media source pipelines can start immediately.
313 if (load_type == LoadTypeMediaSource) {
314 supports_save_ = false;
319 // Otherwise it's a regular request which requires resolving the URL first.
320 data_source_.reset(new BufferedDataSource(
324 &buffered_data_source_host_,
325 base::Bind(&WebMediaPlayerImpl::NotifyDownloading, AsWeakPtr())));
326 data_source_->Initialize(
327 url, static_cast<BufferedResourceLoader::CORSMode>(cors_mode),
329 &WebMediaPlayerImpl::DataSourceInitialized,
332 is_local_source_ = !gurl.SchemeIsHTTPOrHTTPS();
335 void WebMediaPlayerImpl::play() {
336 DCHECK(main_loop_->BelongsToCurrentThread());
339 pipeline_.SetPlaybackRate(playback_rate_);
341 data_source_->MediaIsPlaying();
343 media_log_->AddEvent(media_log_->CreateEvent(media::MediaLogEvent::PLAY));
346 delegate_->DidPlay(this);
349 void WebMediaPlayerImpl::pause() {
350 DCHECK(main_loop_->BelongsToCurrentThread());
353 pipeline_.SetPlaybackRate(0.0f);
355 data_source_->MediaIsPaused();
356 paused_time_ = pipeline_.GetMediaTime();
358 media_log_->AddEvent(media_log_->CreateEvent(media::MediaLogEvent::PAUSE));
361 delegate_->DidPause(this);
364 bool WebMediaPlayerImpl::supportsSave() const {
365 DCHECK(main_loop_->BelongsToCurrentThread());
366 return supports_save_;
369 void WebMediaPlayerImpl::seek(double seconds) {
370 DCHECK(main_loop_->BelongsToCurrentThread());
372 if (ready_state_ > WebMediaPlayer::ReadyStateHaveMetadata)
373 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
375 base::TimeDelta seek_time = ConvertSecondsToTimestamp(seconds);
377 if (starting_ || seeking_) {
378 pending_seek_ = true;
379 pending_seek_seconds_ = seconds;
381 chunk_demuxer_->CancelPendingSeek(seek_time);
385 media_log_->AddEvent(media_log_->CreateSeekEvent(seconds));
387 // Update our paused time.
389 paused_time_ = seek_time;
394 chunk_demuxer_->StartWaitingForSeek(seek_time);
396 // Kick off the asynchronous seek!
399 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineSeek));
402 void WebMediaPlayerImpl::setRate(double rate) {
403 DCHECK(main_loop_->BelongsToCurrentThread());
405 // TODO(kylep): Remove when support for negatives is added. Also, modify the
406 // following checks so rewind uses reasonable values also.
410 // Limit rates to reasonable values by clamping.
414 else if (rate > kMaxRate)
418 playback_rate_ = rate;
420 pipeline_.SetPlaybackRate(rate);
422 data_source_->MediaPlaybackRateChanged(rate);
426 void WebMediaPlayerImpl::setVolume(double volume) {
427 DCHECK(main_loop_->BelongsToCurrentThread());
429 pipeline_.SetVolume(volume);
432 #define COMPILE_ASSERT_MATCHING_ENUM(webkit_name, chromium_name) \
433 COMPILE_ASSERT(static_cast<int>(WebMediaPlayer::webkit_name) == \
434 static_cast<int>(content::chromium_name), \
436 COMPILE_ASSERT_MATCHING_ENUM(PreloadNone, NONE);
437 COMPILE_ASSERT_MATCHING_ENUM(PreloadMetaData, METADATA);
438 COMPILE_ASSERT_MATCHING_ENUM(PreloadAuto, AUTO);
439 #undef COMPILE_ASSERT_MATCHING_ENUM
441 void WebMediaPlayerImpl::setPreload(WebMediaPlayer::Preload preload) {
442 DCHECK(main_loop_->BelongsToCurrentThread());
445 data_source_->SetPreload(static_cast<content::Preload>(preload));
448 bool WebMediaPlayerImpl::hasVideo() const {
449 DCHECK(main_loop_->BelongsToCurrentThread());
451 return pipeline_metadata_.has_video;
454 bool WebMediaPlayerImpl::hasAudio() const {
455 DCHECK(main_loop_->BelongsToCurrentThread());
457 return pipeline_metadata_.has_audio;
460 blink::WebSize WebMediaPlayerImpl::naturalSize() const {
461 DCHECK(main_loop_->BelongsToCurrentThread());
463 return blink::WebSize(pipeline_metadata_.natural_size);
466 bool WebMediaPlayerImpl::paused() const {
467 DCHECK(main_loop_->BelongsToCurrentThread());
469 return pipeline_.GetPlaybackRate() == 0.0f;
472 bool WebMediaPlayerImpl::seeking() const {
473 DCHECK(main_loop_->BelongsToCurrentThread());
475 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
481 double WebMediaPlayerImpl::duration() const {
482 DCHECK(main_loop_->BelongsToCurrentThread());
484 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
485 return std::numeric_limits<double>::quiet_NaN();
487 return GetPipelineDuration();
490 double WebMediaPlayerImpl::timelineOffset() const {
491 DCHECK(main_loop_->BelongsToCurrentThread());
493 if (pipeline_metadata_.timeline_offset.is_null())
494 return std::numeric_limits<double>::quiet_NaN();
496 return pipeline_metadata_.timeline_offset.ToJsTime();
499 double WebMediaPlayerImpl::currentTime() const {
500 DCHECK(main_loop_->BelongsToCurrentThread());
501 return (paused_ ? paused_time_ : pipeline_.GetMediaTime()).InSecondsF();
504 WebMediaPlayer::NetworkState WebMediaPlayerImpl::networkState() const {
505 DCHECK(main_loop_->BelongsToCurrentThread());
506 return network_state_;
509 WebMediaPlayer::ReadyState WebMediaPlayerImpl::readyState() const {
510 DCHECK(main_loop_->BelongsToCurrentThread());
514 const blink::WebTimeRanges& WebMediaPlayerImpl::buffered() {
515 DCHECK(main_loop_->BelongsToCurrentThread());
516 media::Ranges<base::TimeDelta> buffered_time_ranges =
517 pipeline_.GetBufferedTimeRanges();
518 buffered_data_source_host_.AddBufferedTimeRanges(
519 &buffered_time_ranges, pipeline_.GetMediaDuration());
520 blink::WebTimeRanges buffered_web_time_ranges(
521 ConvertToWebTimeRanges(buffered_time_ranges));
522 buffered_web_time_ranges_.swap(buffered_web_time_ranges);
523 return buffered_web_time_ranges_;
526 double WebMediaPlayerImpl::maxTimeSeekable() const {
527 DCHECK(main_loop_->BelongsToCurrentThread());
529 // If we haven't even gotten to ReadyStateHaveMetadata yet then just
530 // return 0 so that the seekable range is empty.
531 if (ready_state_ < WebMediaPlayer::ReadyStateHaveMetadata)
534 // We don't support seeking in streaming media.
535 if (data_source_ && data_source_->IsStreaming())
540 bool WebMediaPlayerImpl::didLoadingProgress() const {
541 DCHECK(main_loop_->BelongsToCurrentThread());
542 bool pipeline_progress = pipeline_.DidLoadingProgress();
543 bool data_progress = buffered_data_source_host_.DidLoadingProgress();
544 return pipeline_progress || data_progress;
547 void WebMediaPlayerImpl::paint(WebCanvas* canvas,
549 unsigned char alpha) {
550 DCHECK(main_loop_->BelongsToCurrentThread());
551 TRACE_EVENT0("media", "WebMediaPlayerImpl:paint");
553 if (!accelerated_compositing_reported_) {
554 accelerated_compositing_reported_ = true;
555 // Normally paint() is only called in non-accelerated rendering, but there
556 // are exceptions such as webgl where compositing is used in the WebView but
557 // video frames are still rendered to a canvas.
558 UMA_HISTOGRAM_BOOLEAN(
559 "Media.AcceleratedCompositingActive",
560 frame_->view()->isAcceleratedCompositingActive());
563 // TODO(scherkus): Clarify paint() API contract to better understand when and
564 // why it's being called. For example, today paint() is called when:
565 // - We haven't reached HAVE_CURRENT_DATA and need to paint black
566 // - We're painting to a canvas
567 // See http://crbug.com/341225 http://crbug.com/342621 for details.
568 scoped_refptr<media::VideoFrame> video_frame =
569 GetCurrentFrameFromCompositor();
571 gfx::Rect gfx_rect(rect);
572 skcanvas_video_renderer_.Paint(video_frame.get(), canvas, gfx_rect, alpha);
575 bool WebMediaPlayerImpl::hasSingleSecurityOrigin() const {
577 return data_source_->HasSingleOrigin();
581 bool WebMediaPlayerImpl::didPassCORSAccessCheck() const {
583 return data_source_->DidPassCORSAccessCheck();
587 double WebMediaPlayerImpl::mediaTimeForTimeValue(double timeValue) const {
588 return ConvertSecondsToTimestamp(timeValue).InSecondsF();
591 unsigned WebMediaPlayerImpl::decodedFrameCount() const {
592 DCHECK(main_loop_->BelongsToCurrentThread());
594 media::PipelineStatistics stats = pipeline_.GetStatistics();
595 return stats.video_frames_decoded;
598 unsigned WebMediaPlayerImpl::droppedFrameCount() const {
599 DCHECK(main_loop_->BelongsToCurrentThread());
601 media::PipelineStatistics stats = pipeline_.GetStatistics();
602 return stats.video_frames_dropped;
605 unsigned WebMediaPlayerImpl::audioDecodedByteCount() const {
606 DCHECK(main_loop_->BelongsToCurrentThread());
608 media::PipelineStatistics stats = pipeline_.GetStatistics();
609 return stats.audio_bytes_decoded;
612 unsigned WebMediaPlayerImpl::videoDecodedByteCount() const {
613 DCHECK(main_loop_->BelongsToCurrentThread());
615 media::PipelineStatistics stats = pipeline_.GetStatistics();
616 return stats.video_bytes_decoded;
619 bool WebMediaPlayerImpl::copyVideoTextureToPlatformTexture(
620 blink::WebGraphicsContext3D* web_graphics_context,
621 unsigned int texture,
623 unsigned int internal_format,
625 bool premultiply_alpha,
627 TRACE_EVENT0("media", "WebMediaPlayerImpl:copyVideoTextureToPlatformTexture");
629 scoped_refptr<media::VideoFrame> video_frame =
630 GetCurrentFrameFromCompositor();
634 if (video_frame->format() != media::VideoFrame::NATIVE_TEXTURE)
637 const gpu::MailboxHolder* mailbox_holder = video_frame->mailbox_holder();
638 if (mailbox_holder->texture_target != GL_TEXTURE_2D)
641 // Since this method changes which texture is bound to the TEXTURE_2D target,
642 // ideally it would restore the currently-bound texture before returning.
643 // The cost of getIntegerv is sufficiently high, however, that we want to
644 // avoid it in user builds. As a result assume (below) that |texture| is
645 // bound when this method is called, and only verify this fact when
648 GLint bound_texture = 0;
649 web_graphics_context->getIntegerv(GL_TEXTURE_BINDING_2D, &bound_texture);
650 DCHECK_EQ(static_cast<GLuint>(bound_texture), texture);
653 uint32 source_texture = web_graphics_context->createTexture();
655 web_graphics_context->waitSyncPoint(mailbox_holder->sync_point);
656 web_graphics_context->bindTexture(GL_TEXTURE_2D, source_texture);
657 web_graphics_context->consumeTextureCHROMIUM(GL_TEXTURE_2D,
658 mailbox_holder->mailbox.name);
660 // The video is stored in a unmultiplied format, so premultiply
662 web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM,
664 // Application itself needs to take care of setting the right flip_y
665 // value down to get the expected result.
666 // flip_y==true means to reverse the video orientation while
667 // flip_y==false means to keep the intrinsic orientation.
668 web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, flip_y);
669 web_graphics_context->copyTextureCHROMIUM(GL_TEXTURE_2D,
675 web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, false);
676 web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM,
679 // Restore the state for TEXTURE_2D binding point as mentioned above.
680 web_graphics_context->bindTexture(GL_TEXTURE_2D, texture);
682 web_graphics_context->deleteTexture(source_texture);
683 web_graphics_context->flush();
684 video_frame->AppendReleaseSyncPoint(web_graphics_context->insertSyncPoint());
688 // Helper functions to report media EME related stats to UMA. They follow the
689 // convention of more commonly used macros UMA_HISTOGRAM_ENUMERATION and
690 // UMA_HISTOGRAM_COUNTS. The reason that we cannot use those macros directly is
691 // that UMA_* macros require the names to be constant throughout the process'
693 static void EmeUMAHistogramEnumeration(const std::string& key_system,
694 const std::string& method,
696 int boundary_value) {
697 base::LinearHistogram::FactoryGet(
698 kMediaEme + KeySystemNameForUMA(key_system) + "." + method,
699 1, boundary_value, boundary_value + 1,
700 base::Histogram::kUmaTargetedHistogramFlag)->Add(sample);
703 static void EmeUMAHistogramCounts(const std::string& key_system,
704 const std::string& method,
706 // Use the same parameters as UMA_HISTOGRAM_COUNTS.
707 base::Histogram::FactoryGet(
708 kMediaEme + KeySystemNameForUMA(key_system) + "." + method,
709 1, 1000000, 50, base::Histogram::kUmaTargetedHistogramFlag)->Add(sample);
712 // Helper enum for reporting generateKeyRequest/addKey histograms.
713 enum MediaKeyException {
716 kKeySystemNotSupported,
718 kMaxMediaKeyException
721 static MediaKeyException MediaKeyExceptionForUMA(
722 WebMediaPlayer::MediaKeyException e) {
724 case WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported:
725 return kKeySystemNotSupported;
726 case WebMediaPlayer::MediaKeyExceptionInvalidPlayerState:
727 return kInvalidPlayerState;
728 case WebMediaPlayer::MediaKeyExceptionNoError:
731 return kUnknownResultId;
735 // Helper for converting |key_system| name and exception |e| to a pair of enum
736 // values from above, for reporting to UMA.
737 static void ReportMediaKeyExceptionToUMA(const std::string& method,
738 const std::string& key_system,
739 WebMediaPlayer::MediaKeyException e) {
740 MediaKeyException result_id = MediaKeyExceptionForUMA(e);
741 DCHECK_NE(result_id, kUnknownResultId) << e;
742 EmeUMAHistogramEnumeration(
743 key_system, method, result_id, kMaxMediaKeyException);
746 // Convert a WebString to ASCII, falling back on an empty string in the case
747 // of a non-ASCII string.
748 static std::string ToASCIIOrEmpty(const blink::WebString& string) {
749 return base::IsStringASCII(string) ? base::UTF16ToASCII(string)
753 WebMediaPlayer::MediaKeyException
754 WebMediaPlayerImpl::generateKeyRequest(const WebString& key_system,
755 const unsigned char* init_data,
756 unsigned init_data_length) {
757 DVLOG(1) << "generateKeyRequest: " << base::string16(key_system) << ": "
758 << std::string(reinterpret_cast<const char*>(init_data),
759 static_cast<size_t>(init_data_length));
761 std::string ascii_key_system =
762 GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system));
764 WebMediaPlayer::MediaKeyException e =
765 GenerateKeyRequestInternal(ascii_key_system, init_data, init_data_length);
766 ReportMediaKeyExceptionToUMA("generateKeyRequest", ascii_key_system, e);
770 // Guess the type of |init_data|. This is only used to handle some corner cases
771 // so we keep it as simple as possible without breaking major use cases.
772 static std::string GuessInitDataType(const unsigned char* init_data,
773 unsigned init_data_length) {
774 // Most WebM files use KeyId of 16 bytes. MP4 init data are always >16 bytes.
775 if (init_data_length == 16)
781 WebMediaPlayer::MediaKeyException
782 WebMediaPlayerImpl::GenerateKeyRequestInternal(const std::string& key_system,
783 const unsigned char* init_data,
784 unsigned init_data_length) {
785 DCHECK(main_loop_->BelongsToCurrentThread());
787 if (!IsConcreteSupportedKeySystem(key_system))
788 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
790 // We do not support run-time switching between key systems for now.
791 if (current_key_system_.empty()) {
792 if (!proxy_decryptor_) {
793 proxy_decryptor_.reset(new ProxyDecryptor(
794 #if defined(ENABLE_PEPPER_CDMS)
795 // Create() must be called synchronously as |frame_| may not be
797 base::Bind(&PepperCdmWrapperImpl::Create, frame_),
799 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnKeyAdded),
800 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnKeyError),
801 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnKeyMessage)));
804 GURL security_origin(frame_->document().securityOrigin().toString());
805 if (!proxy_decryptor_->InitializeCDM(key_system, security_origin))
806 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
808 if (proxy_decryptor_ && !decryptor_ready_cb_.is_null()) {
809 base::ResetAndReturn(&decryptor_ready_cb_)
810 .Run(proxy_decryptor_->GetDecryptor());
813 current_key_system_ = key_system;
814 } else if (key_system != current_key_system_) {
815 return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState;
818 std::string init_data_type = init_data_type_;
819 if (init_data_type.empty())
820 init_data_type = GuessInitDataType(init_data, init_data_length);
822 // TODO(xhwang): We assume all streams are from the same container (thus have
823 // the same "type") for now. In the future, the "type" should be passed down
824 // from the application.
825 if (!proxy_decryptor_->GenerateKeyRequest(
826 init_data_type, init_data, init_data_length)) {
827 current_key_system_.clear();
828 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
831 return WebMediaPlayer::MediaKeyExceptionNoError;
834 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::addKey(
835 const WebString& key_system,
836 const unsigned char* key,
838 const unsigned char* init_data,
839 unsigned init_data_length,
840 const WebString& session_id) {
841 DVLOG(1) << "addKey: " << base::string16(key_system) << ": "
842 << std::string(reinterpret_cast<const char*>(key),
843 static_cast<size_t>(key_length)) << ", "
844 << std::string(reinterpret_cast<const char*>(init_data),
845 static_cast<size_t>(init_data_length)) << " ["
846 << base::string16(session_id) << "]";
848 std::string ascii_key_system =
849 GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system));
850 std::string ascii_session_id = ToASCIIOrEmpty(session_id);
852 WebMediaPlayer::MediaKeyException e = AddKeyInternal(ascii_key_system,
858 ReportMediaKeyExceptionToUMA("addKey", ascii_key_system, e);
862 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::AddKeyInternal(
863 const std::string& key_system,
864 const unsigned char* key,
866 const unsigned char* init_data,
867 unsigned init_data_length,
868 const std::string& session_id) {
870 DCHECK_GT(key_length, 0u);
872 if (!IsConcreteSupportedKeySystem(key_system))
873 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
875 if (current_key_system_.empty() || key_system != current_key_system_)
876 return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState;
878 proxy_decryptor_->AddKey(
879 key, key_length, init_data, init_data_length, session_id);
880 return WebMediaPlayer::MediaKeyExceptionNoError;
883 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::cancelKeyRequest(
884 const WebString& key_system,
885 const WebString& session_id) {
886 DVLOG(1) << "cancelKeyRequest: " << base::string16(key_system) << ": "
887 << " [" << base::string16(session_id) << "]";
889 std::string ascii_key_system =
890 GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system));
891 std::string ascii_session_id = ToASCIIOrEmpty(session_id);
893 WebMediaPlayer::MediaKeyException e =
894 CancelKeyRequestInternal(ascii_key_system, ascii_session_id);
895 ReportMediaKeyExceptionToUMA("cancelKeyRequest", ascii_key_system, e);
899 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::CancelKeyRequestInternal(
900 const std::string& key_system,
901 const std::string& session_id) {
902 if (!IsConcreteSupportedKeySystem(key_system))
903 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
905 if (current_key_system_.empty() || key_system != current_key_system_)
906 return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState;
908 proxy_decryptor_->CancelKeyRequest(session_id);
909 return WebMediaPlayer::MediaKeyExceptionNoError;
912 void WebMediaPlayerImpl::setContentDecryptionModule(
913 blink::WebContentDecryptionModule* cdm) {
914 DCHECK(main_loop_->BelongsToCurrentThread());
916 // TODO(xhwang): Support setMediaKeys(0) if necessary: http://crbug.com/330324
920 web_cdm_ = ToWebContentDecryptionModuleImpl(cdm);
922 if (web_cdm_ && !decryptor_ready_cb_.is_null())
923 base::ResetAndReturn(&decryptor_ready_cb_).Run(web_cdm_->GetDecryptor());
926 void WebMediaPlayerImpl::InvalidateOnMainThread() {
927 DCHECK(main_loop_->BelongsToCurrentThread());
928 TRACE_EVENT0("media", "WebMediaPlayerImpl::InvalidateOnMainThread");
933 void WebMediaPlayerImpl::OnPipelineSeek(PipelineStatus status) {
934 DCHECK(main_loop_->BelongsToCurrentThread());
938 pending_seek_ = false;
939 seek(pending_seek_seconds_);
943 if (status != media::PIPELINE_OK) {
944 OnPipelineError(status);
948 // Update our paused time.
950 paused_time_ = pipeline_.GetMediaTime();
952 client_->timeChanged();
955 void WebMediaPlayerImpl::OnPipelineEnded() {
956 DCHECK(main_loop_->BelongsToCurrentThread());
957 client_->timeChanged();
960 void WebMediaPlayerImpl::OnPipelineError(PipelineStatus error) {
961 DCHECK(main_loop_->BelongsToCurrentThread());
962 DCHECK_NE(error, media::PIPELINE_OK);
964 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) {
965 // Any error that occurs before reaching ReadyStateHaveMetadata should
966 // be considered a format error.
967 SetNetworkState(WebMediaPlayer::NetworkStateFormatError);
969 // TODO(scherkus): This should be handled by HTMLMediaElement and controls
970 // should know when to invalidate themselves http://crbug.com/337015
971 InvalidateOnMainThread();
975 SetNetworkState(PipelineErrorToNetworkState(error));
977 if (error == media::PIPELINE_ERROR_DECRYPT)
978 EmeUMAHistogramCounts(current_key_system_, "DecryptError", 1);
980 // TODO(scherkus): This should be handled by HTMLMediaElement and controls
981 // should know when to invalidate themselves http://crbug.com/337015
982 InvalidateOnMainThread();
985 void WebMediaPlayerImpl::OnPipelineMetadata(
986 media::PipelineMetadata metadata) {
987 DVLOG(1) << "OnPipelineMetadata";
989 pipeline_metadata_ = metadata;
991 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
994 DCHECK(!video_weblayer_);
995 video_weblayer_.reset(
996 new webkit::WebLayerImpl(cc::VideoLayer::Create(compositor_)));
997 video_weblayer_->setOpaque(opaque_);
998 client_->setWebLayer(video_weblayer_.get());
1001 // TODO(scherkus): This should be handled by HTMLMediaElement and controls
1002 // should know when to invalidate themselves http://crbug.com/337015
1003 InvalidateOnMainThread();
1006 void WebMediaPlayerImpl::OnPipelinePrerollCompleted() {
1007 DVLOG(1) << "OnPipelinePrerollCompleted";
1009 // Only transition to ReadyStateHaveEnoughData if we don't have
1010 // any pending seeks because the transition can cause Blink to
1011 // report that the most recent seek has completed.
1012 if (!pending_seek_) {
1013 SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData);
1015 // TODO(scherkus): This should be handled by HTMLMediaElement and controls
1016 // should know when to invalidate themselves http://crbug.com/337015
1017 InvalidateOnMainThread();
1021 void WebMediaPlayerImpl::OnDemuxerOpened() {
1022 DCHECK(main_loop_->BelongsToCurrentThread());
1023 client_->mediaSourceOpened(new WebMediaSourceImpl(
1024 chunk_demuxer_, base::Bind(&LogMediaSourceError, media_log_)));
1027 void WebMediaPlayerImpl::OnKeyAdded(const std::string& session_id) {
1028 DCHECK(main_loop_->BelongsToCurrentThread());
1029 EmeUMAHistogramCounts(current_key_system_, "KeyAdded", 1);
1031 WebString::fromUTF8(GetPrefixedKeySystemName(current_key_system_)),
1032 WebString::fromUTF8(session_id));
1035 void WebMediaPlayerImpl::OnNeedKey(const std::string& type,
1036 const std::vector<uint8>& init_data) {
1037 DCHECK(main_loop_->BelongsToCurrentThread());
1039 // Do not fire NeedKey event if encrypted media is not enabled.
1040 if (!blink::WebRuntimeFeatures::isPrefixedEncryptedMediaEnabled() &&
1041 !blink::WebRuntimeFeatures::isEncryptedMediaEnabled()) {
1045 UMA_HISTOGRAM_COUNTS(kMediaEme + std::string("NeedKey"), 1);
1047 DCHECK(init_data_type_.empty() || type.empty() || type == init_data_type_);
1048 if (init_data_type_.empty())
1049 init_data_type_ = type;
1051 const uint8* init_data_ptr = init_data.empty() ? NULL : &init_data[0];
1053 WebString::fromUTF8(type), init_data_ptr, init_data.size());
1056 void WebMediaPlayerImpl::OnAddTextTrack(
1057 const media::TextTrackConfig& config,
1058 const media::AddTextTrackDoneCB& done_cb) {
1059 DCHECK(main_loop_->BelongsToCurrentThread());
1061 const WebInbandTextTrackImpl::Kind web_kind =
1062 static_cast<WebInbandTextTrackImpl::Kind>(config.kind());
1063 const blink::WebString web_label =
1064 blink::WebString::fromUTF8(config.label());
1065 const blink::WebString web_language =
1066 blink::WebString::fromUTF8(config.language());
1067 const blink::WebString web_id =
1068 blink::WebString::fromUTF8(config.id());
1070 scoped_ptr<WebInbandTextTrackImpl> web_inband_text_track(
1071 new WebInbandTextTrackImpl(web_kind, web_label, web_language, web_id,
1072 text_track_index_++));
1074 scoped_ptr<media::TextTrack> text_track(
1075 new TextTrackImpl(main_loop_, client_, web_inband_text_track.Pass()));
1077 done_cb.Run(text_track.Pass());
1080 void WebMediaPlayerImpl::OnKeyError(const std::string& session_id,
1081 media::MediaKeys::KeyError error_code,
1082 uint32 system_code) {
1083 DCHECK(main_loop_->BelongsToCurrentThread());
1085 EmeUMAHistogramEnumeration(current_key_system_, "KeyError",
1086 error_code, media::MediaKeys::kMaxKeyError);
1088 unsigned short short_system_code = 0;
1089 if (system_code > std::numeric_limits<unsigned short>::max()) {
1090 LOG(WARNING) << "system_code exceeds unsigned short limit.";
1091 short_system_code = std::numeric_limits<unsigned short>::max();
1093 short_system_code = static_cast<unsigned short>(system_code);
1097 WebString::fromUTF8(GetPrefixedKeySystemName(current_key_system_)),
1098 WebString::fromUTF8(session_id),
1099 static_cast<blink::WebMediaPlayerClient::MediaKeyErrorCode>(error_code),
1103 void WebMediaPlayerImpl::OnKeyMessage(const std::string& session_id,
1104 const std::vector<uint8>& message,
1105 const std::string& default_url) {
1106 DCHECK(main_loop_->BelongsToCurrentThread());
1108 const GURL default_url_gurl(default_url);
1109 DLOG_IF(WARNING, !default_url.empty() && !default_url_gurl.is_valid())
1110 << "Invalid URL in default_url: " << default_url;
1112 client_->keyMessage(
1113 WebString::fromUTF8(GetPrefixedKeySystemName(current_key_system_)),
1114 WebString::fromUTF8(session_id),
1115 message.empty() ? NULL : &message[0],
1120 void WebMediaPlayerImpl::DataSourceInitialized(const GURL& gurl, bool success) {
1121 DCHECK(main_loop_->BelongsToCurrentThread());
1124 SetNetworkState(WebMediaPlayer::NetworkStateFormatError);
1126 // TODO(scherkus): This should be handled by HTMLMediaElement and controls
1127 // should know when to invalidate themselves http://crbug.com/337015
1128 InvalidateOnMainThread();
1135 void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading) {
1136 if (!is_downloading && network_state_ == WebMediaPlayer::NetworkStateLoading)
1137 SetNetworkState(WebMediaPlayer::NetworkStateIdle);
1138 else if (is_downloading && network_state_ == WebMediaPlayer::NetworkStateIdle)
1139 SetNetworkState(WebMediaPlayer::NetworkStateLoading);
1140 media_log_->AddEvent(
1141 media_log_->CreateBooleanEvent(
1142 media::MediaLogEvent::NETWORK_ACTIVITY_SET,
1143 "is_downloading_data", is_downloading));
1146 void WebMediaPlayerImpl::StartPipeline() {
1147 DCHECK(main_loop_->BelongsToCurrentThread());
1148 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
1150 // Keep track if this is a MSE or non-MSE playback.
1151 UMA_HISTOGRAM_BOOLEAN("Media.MSE.Playback",
1152 (load_type_ == LoadTypeMediaSource));
1154 media::LogCB mse_log_cb;
1156 // Figure out which demuxer to use.
1157 if (load_type_ != LoadTypeMediaSource) {
1158 DCHECK(!chunk_demuxer_);
1159 DCHECK(data_source_);
1161 demuxer_.reset(new media::FFmpegDemuxer(
1162 media_loop_, data_source_.get(),
1163 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNeedKey),
1166 DCHECK(!chunk_demuxer_);
1167 DCHECK(!data_source_);
1169 mse_log_cb = base::Bind(&LogMediaSourceError, media_log_);
1171 chunk_demuxer_ = new media::ChunkDemuxer(
1172 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDemuxerOpened),
1173 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNeedKey),
1176 demuxer_.reset(chunk_demuxer_);
1179 scoped_ptr<media::FilterCollection> filter_collection(
1180 new media::FilterCollection());
1181 filter_collection->SetDemuxer(demuxer_.get());
1183 media::SetDecryptorReadyCB set_decryptor_ready_cb =
1184 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::SetDecryptorReadyCB);
1186 // Create our audio decoders and renderer.
1187 ScopedVector<media::AudioDecoder> audio_decoders;
1188 audio_decoders.push_back(new media::FFmpegAudioDecoder(media_loop_,
1190 audio_decoders.push_back(new media::OpusAudioDecoder(media_loop_));
1192 scoped_ptr<media::AudioRenderer> audio_renderer(new media::AudioRendererImpl(
1194 audio_source_provider_.get(),
1195 audio_decoders.Pass(),
1196 set_decryptor_ready_cb,
1197 RenderThreadImpl::current()->GetAudioHardwareConfig()));
1198 filter_collection->SetAudioRenderer(audio_renderer.Pass());
1200 // Create our video decoders and renderer.
1201 ScopedVector<media::VideoDecoder> video_decoders;
1203 if (gpu_factories_.get()) {
1204 video_decoders.push_back(
1205 new media::GpuVideoDecoder(gpu_factories_, media_log_));
1208 #if !defined(MEDIA_DISABLE_LIBVPX)
1209 video_decoders.push_back(new media::VpxVideoDecoder(media_loop_));
1210 #endif // !defined(MEDIA_DISABLE_LIBVPX)
1212 video_decoders.push_back(new media::FFmpegVideoDecoder(media_loop_));
1214 scoped_ptr<media::VideoRenderer> video_renderer(
1215 new media::VideoRendererImpl(
1217 video_decoders.Pass(),
1218 set_decryptor_ready_cb,
1219 base::Bind(&WebMediaPlayerImpl::FrameReady, base::Unretained(this)),
1221 filter_collection->SetVideoRenderer(video_renderer.Pass());
1223 if (cmd_line->HasSwitch(switches::kEnableInbandTextTracks)) {
1224 scoped_ptr<media::TextRenderer> text_renderer(
1225 new media::TextRenderer(
1227 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnAddTextTrack)));
1229 filter_collection->SetTextRenderer(text_renderer.Pass());
1232 // ... and we're ready to go!
1235 filter_collection.Pass(),
1236 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineEnded),
1237 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineError),
1238 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineSeek),
1239 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineMetadata),
1240 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelinePrerollCompleted),
1241 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDurationChanged));
1244 void WebMediaPlayerImpl::SetNetworkState(WebMediaPlayer::NetworkState state) {
1245 DCHECK(main_loop_->BelongsToCurrentThread());
1246 DVLOG(1) << "SetNetworkState: " << state;
1247 network_state_ = state;
1248 // Always notify to ensure client has the latest value.
1249 client_->networkStateChanged();
1252 void WebMediaPlayerImpl::SetReadyState(WebMediaPlayer::ReadyState state) {
1253 DCHECK(main_loop_->BelongsToCurrentThread());
1254 DVLOG(1) << "SetReadyState: " << state;
1256 if (state == WebMediaPlayer::ReadyStateHaveEnoughData &&
1258 network_state_ == WebMediaPlayer::NetworkStateLoading)
1259 SetNetworkState(WebMediaPlayer::NetworkStateLoaded);
1261 ready_state_ = state;
1262 // Always notify to ensure client has the latest value.
1263 client_->readyStateChanged();
1266 blink::WebAudioSourceProvider* WebMediaPlayerImpl::audioSourceProvider() {
1267 return audio_source_provider_.get();
1270 void WebMediaPlayerImpl::IncrementExternallyAllocatedMemory() {
1271 DCHECK(main_loop_->BelongsToCurrentThread());
1272 incremented_externally_allocated_memory_ = true;
1273 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
1274 kPlayerExtraMemory);
1277 double WebMediaPlayerImpl::GetPipelineDuration() const {
1278 base::TimeDelta duration = pipeline_.GetMediaDuration();
1280 // Return positive infinity if the resource is unbounded.
1281 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
1282 if (duration == media::kInfiniteDuration())
1283 return std::numeric_limits<double>::infinity();
1285 return duration.InSecondsF();
1288 void WebMediaPlayerImpl::OnDurationChanged() {
1289 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
1292 client_->durationChanged();
1295 void WebMediaPlayerImpl::OnNaturalSizeChanged(gfx::Size size) {
1296 DCHECK(main_loop_->BelongsToCurrentThread());
1297 DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
1298 TRACE_EVENT0("media", "WebMediaPlayerImpl::OnNaturalSizeChanged");
1300 media_log_->AddEvent(
1301 media_log_->CreateVideoSizeSetEvent(size.width(), size.height()));
1302 pipeline_metadata_.natural_size = size;
1304 client_->sizeChanged();
1307 void WebMediaPlayerImpl::OnOpacityChanged(bool opaque) {
1308 DCHECK(main_loop_->BelongsToCurrentThread());
1309 DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
1312 if (video_weblayer_)
1313 video_weblayer_->setOpaque(opaque_);
1316 void WebMediaPlayerImpl::FrameReady(
1317 const scoped_refptr<media::VideoFrame>& frame) {
1318 compositor_task_runner_->PostTask(
1320 base::Bind(&VideoFrameCompositor::UpdateCurrentFrame,
1321 base::Unretained(compositor_),
1325 void WebMediaPlayerImpl::SetDecryptorReadyCB(
1326 const media::DecryptorReadyCB& decryptor_ready_cb) {
1327 DCHECK(main_loop_->BelongsToCurrentThread());
1329 // Cancels the previous decryptor request.
1330 if (decryptor_ready_cb.is_null()) {
1331 if (!decryptor_ready_cb_.is_null())
1332 base::ResetAndReturn(&decryptor_ready_cb_).Run(NULL);
1336 // TODO(xhwang): Support multiple decryptor notification request (e.g. from
1337 // video and audio). The current implementation is okay for the current
1338 // media pipeline since we initialize audio and video decoders in sequence.
1339 // But WebMediaPlayerImpl should not depend on media pipeline's implementation
1341 DCHECK(decryptor_ready_cb_.is_null());
1343 // Mixed use of prefixed and unprefixed EME APIs is disallowed by Blink.
1344 DCHECK(!proxy_decryptor_ || !web_cdm_);
1346 if (proxy_decryptor_) {
1347 decryptor_ready_cb.Run(proxy_decryptor_->GetDecryptor());
1352 decryptor_ready_cb.Run(web_cdm_->GetDecryptor());
1356 decryptor_ready_cb_ = decryptor_ready_cb;
1359 static void GetCurrentFrameAndSignal(
1360 VideoFrameCompositor* compositor,
1361 scoped_refptr<media::VideoFrame>* video_frame_out,
1362 base::WaitableEvent* event) {
1363 TRACE_EVENT0("media", "GetCurrentFrameAndSignal");
1364 *video_frame_out = compositor->GetCurrentFrame();
1368 scoped_refptr<media::VideoFrame>
1369 WebMediaPlayerImpl::GetCurrentFrameFromCompositor() {
1370 TRACE_EVENT0("media", "WebMediaPlayerImpl::GetCurrentFrameFromCompositor");
1371 if (compositor_task_runner_->BelongsToCurrentThread())
1372 return compositor_->GetCurrentFrame();
1374 // Use a posted task and waitable event instead of a lock otherwise
1375 // WebGL/Canvas can see different content than what the compositor is seeing.
1376 scoped_refptr<media::VideoFrame> video_frame;
1377 base::WaitableEvent event(false, false);
1378 compositor_task_runner_->PostTask(FROM_HERE,
1379 base::Bind(&GetCurrentFrameAndSignal,
1380 base::Unretained(compositor_),
1387 } // namespace content