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/compositor_bindings/web_layer_impl.h"
28 #include "content/renderer/media/buffered_data_source.h"
29 #include "content/renderer/media/crypto/key_systems.h"
30 #include "content/renderer/media/render_media_log.h"
31 #include "content/renderer/media/texttrack_impl.h"
32 #include "content/renderer/media/webaudiosourceprovider_impl.h"
33 #include "content/renderer/media/webcontentdecryptionmodule_impl.h"
34 #include "content/renderer/media/webinbandtexttrack_impl.h"
35 #include "content/renderer/media/webmediaplayer_delegate.h"
36 #include "content/renderer/media/webmediaplayer_params.h"
37 #include "content/renderer/media/webmediaplayer_util.h"
38 #include "content/renderer/media/webmediasource_impl.h"
39 #include "content/renderer/pepper/pepper_webplugin_impl.h"
40 #include "content/renderer/render_thread_impl.h"
41 #include "gpu/GLES2/gl2extchromium.h"
42 #include "gpu/command_buffer/common/mailbox_holder.h"
43 #include "media/audio/null_audio_sink.h"
44 #include "media/base/audio_hardware_config.h"
45 #include "media/base/bind_to_current_loop.h"
46 #include "media/base/filter_collection.h"
47 #include "media/base/limits.h"
48 #include "media/base/media_log.h"
49 #include "media/base/media_switches.h"
50 #include "media/base/pipeline.h"
51 #include "media/base/text_renderer.h"
52 #include "media/base/video_frame.h"
53 #include "media/filters/audio_renderer_impl.h"
54 #include "media/filters/chunk_demuxer.h"
55 #include "media/filters/ffmpeg_audio_decoder.h"
56 #include "media/filters/ffmpeg_demuxer.h"
57 #include "media/filters/ffmpeg_video_decoder.h"
58 #include "media/filters/gpu_video_accelerator_factories.h"
59 #include "media/filters/gpu_video_decoder.h"
60 #include "media/filters/opus_audio_decoder.h"
61 #include "media/filters/video_renderer_impl.h"
62 #include "media/filters/vpx_video_decoder.h"
63 #include "third_party/WebKit/public/platform/WebContentDecryptionModule.h"
64 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleResult.h"
65 #include "third_party/WebKit/public/platform/WebMediaSource.h"
66 #include "third_party/WebKit/public/platform/WebRect.h"
67 #include "third_party/WebKit/public/platform/WebSize.h"
68 #include "third_party/WebKit/public/platform/WebString.h"
69 #include "third_party/WebKit/public/platform/WebURL.h"
70 #include "third_party/WebKit/public/web/WebDocument.h"
71 #include "third_party/WebKit/public/web/WebLocalFrame.h"
72 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
73 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
74 #include "third_party/WebKit/public/web/WebView.h"
75 #include "v8/include/v8.h"
77 #if defined(ENABLE_PEPPER_CDMS)
78 #include "content/renderer/media/crypto/pepper_cdm_wrapper_impl.h"
81 using blink::WebCanvas;
82 using blink::WebMediaPlayer;
85 using blink::WebString;
86 using media::PipelineStatus;
90 // Amount of extra memory used by each player instance reported to V8.
91 // It is not exact number -- first, it differs on different platforms,
92 // and second, it is very hard to calculate. Instead, use some arbitrary
93 // value that will cause garbage collection from time to time. We don't want
94 // it to happen on every allocation, but don't want 5k players to sit in memory
95 // either. Looks that chosen constant achieves both goals, at least for audio
96 // objects. (Do not worry about video objects yet, JS programs do not create
97 // thousands of them...)
98 const int kPlayerExtraMemory = 1024 * 1024;
100 // Limits the range of playback rate.
102 // TODO(kylep): Revisit these.
104 // Vista has substantially lower performance than XP or Windows7. If you speed
105 // up a video too much, it can't keep up, and rendering stops updating except on
106 // the time bar. For really high speeds, audio becomes a bottleneck and we just
107 // use up the data we have, which may not achieve the speed requested, but will
108 // not crash the tab.
110 // A very slow speed, ie 0.00000001x, causes the machine to lock up. (It seems
111 // like a busy loop). It gets unresponsive, although its not completely dead.
113 // Also our timers are not very accurate (especially for ogg), which becomes
114 // evident at low speeds and on Vista. Since other speeds are risky and outside
115 // the norms, we think 1/16x to 16x is a safe and useful range for now.
116 const double kMinRate = 0.0625;
117 const double kMaxRate = 16.0;
119 // Prefix for histograms related to Encrypted Media Extensions.
120 const char* kMediaEme = "Media.EME.";
122 class SyncPointClientImpl : public media::VideoFrame::SyncPointClient {
124 explicit SyncPointClientImpl(
125 blink::WebGraphicsContext3D* web_graphics_context)
126 : web_graphics_context_(web_graphics_context) {}
127 virtual ~SyncPointClientImpl() {}
128 virtual uint32 InsertSyncPoint() OVERRIDE {
129 return web_graphics_context_->insertSyncPoint();
131 virtual void WaitSyncPoint(uint32 sync_point) OVERRIDE {
132 web_graphics_context_->waitSyncPoint(sync_point);
136 blink::WebGraphicsContext3D* web_graphics_context_;
139 // Used for calls to decryptor_ready_cb where the result can be ignored.
140 void DoNothing(bool) {
147 class BufferedDataSourceHostImpl;
149 #define COMPILE_ASSERT_MATCHING_ENUM(name) \
150 COMPILE_ASSERT(static_cast<int>(WebMediaPlayer::CORSMode ## name) == \
151 static_cast<int>(BufferedResourceLoader::k ## name), \
153 COMPILE_ASSERT_MATCHING_ENUM(Unspecified);
154 COMPILE_ASSERT_MATCHING_ENUM(Anonymous);
155 COMPILE_ASSERT_MATCHING_ENUM(UseCredentials);
156 #undef COMPILE_ASSERT_MATCHING_ENUM
158 #define BIND_TO_RENDER_LOOP(function) \
159 (DCHECK(main_loop_->BelongsToCurrentThread()), \
160 media::BindToCurrentLoop(base::Bind(function, AsWeakPtr())))
162 #define BIND_TO_RENDER_LOOP1(function, arg1) \
163 (DCHECK(main_loop_->BelongsToCurrentThread()), \
164 media::BindToCurrentLoop(base::Bind(function, AsWeakPtr(), arg1)))
166 static void LogMediaSourceError(const scoped_refptr<media::MediaLog>& media_log,
167 const std::string& error) {
168 media_log->AddEvent(media_log->CreateMediaSourceErrorEvent(error));
171 WebMediaPlayerImpl::WebMediaPlayerImpl(
172 blink::WebLocalFrame* frame,
173 blink::WebMediaPlayerClient* client,
174 base::WeakPtr<WebMediaPlayerDelegate> delegate,
175 const WebMediaPlayerParams& params)
177 network_state_(WebMediaPlayer::NetworkStateEmpty),
178 ready_state_(WebMediaPlayer::ReadyStateHaveNothing),
180 main_loop_(base::MessageLoopProxy::current()),
182 RenderThreadImpl::current()->GetMediaThreadMessageLoopProxy()),
183 media_log_(new RenderMediaLog()),
184 pipeline_(media_loop_, media_log_.get()),
185 load_type_(LoadTypeURL),
189 playback_rate_(0.0f),
190 pending_seek_(false),
191 pending_seek_seconds_(0.0f),
192 should_notify_time_changed_(false),
195 defer_load_cb_(params.defer_load_cb()),
196 incremented_externally_allocated_memory_(false),
197 gpu_factories_(RenderThreadImpl::current()->GetGpuFactories()),
198 supports_save_(true),
199 chunk_demuxer_(NULL),
200 // Threaded compositing isn't enabled universally yet.
201 compositor_task_runner_(
202 RenderThreadImpl::current()->compositor_message_loop_proxy()
203 ? RenderThreadImpl::current()->compositor_message_loop_proxy()
204 : base::MessageLoopProxy::current()),
205 compositor_(new VideoFrameCompositor(
206 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNaturalSizeChanged),
207 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnOpacityChanged))),
208 text_track_index_(0),
210 media_log_->AddEvent(
211 media_log_->CreateEvent(media::MediaLogEvent::WEBMEDIAPLAYER_CREATED));
213 // |gpu_factories_| requires that its entry points be called on its
214 // |GetTaskRunner()|. Since |pipeline_| will own decoders created from the
215 // factories, require that their message loops are identical.
216 DCHECK(!gpu_factories_ || (gpu_factories_->GetTaskRunner() == media_loop_));
218 // Let V8 know we started new thread if we did not do it yet.
219 // Made separate task to avoid deletion of player currently being created.
220 // Also, delaying GC until after player starts gets rid of starting lag --
221 // collection happens in parallel with playing.
223 // TODO(enal): remove when we get rid of per-audio-stream thread.
224 main_loop_->PostTask(
226 base::Bind(&WebMediaPlayerImpl::IncrementExternallyAllocatedMemory,
229 // Use the null sink if no sink was provided.
230 audio_source_provider_ = new WebAudioSourceProviderImpl(
231 params.audio_renderer_sink().get()
232 ? params.audio_renderer_sink()
233 : new media::NullAudioSink(media_loop_));
236 WebMediaPlayerImpl::~WebMediaPlayerImpl() {
237 client_->setWebLayer(NULL);
239 DCHECK(main_loop_->BelongsToCurrentThread());
240 media_log_->AddEvent(
241 media_log_->CreateEvent(media::MediaLogEvent::WEBMEDIAPLAYER_DESTROYED));
244 delegate_->PlayerGone(this);
246 // Abort any pending IO so stopping the pipeline doesn't get blocked.
248 data_source_->Abort();
249 if (chunk_demuxer_) {
250 chunk_demuxer_->Shutdown();
251 chunk_demuxer_ = NULL;
254 gpu_factories_ = NULL;
256 // Make sure to kill the pipeline so there's no more media threads running.
257 // Note: stopping the pipeline might block for a long time.
258 base::WaitableEvent waiter(false, false);
260 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&waiter)));
263 compositor_task_runner_->DeleteSoon(FROM_HERE, compositor_);
265 // Let V8 know we are not using extra resources anymore.
266 if (incremented_externally_allocated_memory_) {
267 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
268 -kPlayerExtraMemory);
269 incremented_externally_allocated_memory_ = false;
273 void WebMediaPlayerImpl::load(LoadType load_type, const blink::WebURL& url,
274 CORSMode cors_mode) {
275 DVLOG(1) << __FUNCTION__ << "(" << load_type << ", " << url << ", "
277 if (!defer_load_cb_.is_null()) {
278 defer_load_cb_.Run(base::Bind(
279 &WebMediaPlayerImpl::DoLoad, AsWeakPtr(), load_type, url, cors_mode));
282 DoLoad(load_type, url, cors_mode);
285 void WebMediaPlayerImpl::DoLoad(LoadType load_type,
286 const blink::WebURL& url,
287 CORSMode cors_mode) {
288 DCHECK(main_loop_->BelongsToCurrentThread());
291 ReportMediaSchemeUma(gurl);
293 // Set subresource URL for crash reporting.
294 base::debug::SetCrashKeyValue("subresource_url", gurl.spec());
296 load_type_ = load_type;
298 SetNetworkState(WebMediaPlayer::NetworkStateLoading);
299 SetReadyState(WebMediaPlayer::ReadyStateHaveNothing);
300 media_log_->AddEvent(media_log_->CreateLoadEvent(url.spec()));
302 // Media source pipelines can start immediately.
303 if (load_type == LoadTypeMediaSource) {
304 supports_save_ = false;
309 // Otherwise it's a regular request which requires resolving the URL first.
310 data_source_.reset(new BufferedDataSource(
312 static_cast<BufferedResourceLoader::CORSMode>(cors_mode),
316 &buffered_data_source_host_,
317 base::Bind(&WebMediaPlayerImpl::NotifyDownloading, AsWeakPtr())));
318 data_source_->Initialize(
319 base::Bind(&WebMediaPlayerImpl::DataSourceInitialized, AsWeakPtr()));
320 data_source_->SetPreload(preload_);
323 void WebMediaPlayerImpl::play() {
324 DVLOG(1) << __FUNCTION__;
325 DCHECK(main_loop_->BelongsToCurrentThread());
328 pipeline_.SetPlaybackRate(playback_rate_);
330 data_source_->MediaIsPlaying();
332 media_log_->AddEvent(media_log_->CreateEvent(media::MediaLogEvent::PLAY));
335 delegate_->DidPlay(this);
338 void WebMediaPlayerImpl::pause() {
339 DVLOG(1) << __FUNCTION__;
340 DCHECK(main_loop_->BelongsToCurrentThread());
343 pipeline_.SetPlaybackRate(0.0f);
345 data_source_->MediaIsPaused();
346 paused_time_ = pipeline_.GetMediaTime();
348 media_log_->AddEvent(media_log_->CreateEvent(media::MediaLogEvent::PAUSE));
351 delegate_->DidPause(this);
354 bool WebMediaPlayerImpl::supportsSave() const {
355 DCHECK(main_loop_->BelongsToCurrentThread());
356 return supports_save_;
359 void WebMediaPlayerImpl::seek(double seconds) {
360 DVLOG(1) << __FUNCTION__ << "(" << seconds << ")";
361 DCHECK(main_loop_->BelongsToCurrentThread());
363 if (ready_state_ > WebMediaPlayer::ReadyStateHaveMetadata)
364 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
366 base::TimeDelta seek_time = ConvertSecondsToTimestamp(seconds);
369 pending_seek_ = true;
370 pending_seek_seconds_ = seconds;
372 chunk_demuxer_->CancelPendingSeek(seek_time);
376 media_log_->AddEvent(media_log_->CreateSeekEvent(seconds));
378 // Update our paused time.
380 paused_time_ = seek_time;
385 chunk_demuxer_->StartWaitingForSeek(seek_time);
387 // Kick off the asynchronous seek!
390 BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnPipelineSeeked, true));
393 void WebMediaPlayerImpl::setRate(double rate) {
394 DVLOG(1) << __FUNCTION__ << "(" << rate << ")";
395 DCHECK(main_loop_->BelongsToCurrentThread());
397 // TODO(kylep): Remove when support for negatives is added. Also, modify the
398 // following checks so rewind uses reasonable values also.
402 // Limit rates to reasonable values by clamping.
406 else if (rate > kMaxRate)
410 playback_rate_ = rate;
412 pipeline_.SetPlaybackRate(rate);
414 data_source_->MediaPlaybackRateChanged(rate);
418 void WebMediaPlayerImpl::setVolume(double volume) {
419 DVLOG(1) << __FUNCTION__ << "(" << volume << ")";
420 DCHECK(main_loop_->BelongsToCurrentThread());
422 pipeline_.SetVolume(volume);
425 #define COMPILE_ASSERT_MATCHING_ENUM(webkit_name, chromium_name) \
426 COMPILE_ASSERT(static_cast<int>(WebMediaPlayer::webkit_name) == \
427 static_cast<int>(content::chromium_name), \
429 COMPILE_ASSERT_MATCHING_ENUM(PreloadNone, NONE);
430 COMPILE_ASSERT_MATCHING_ENUM(PreloadMetaData, METADATA);
431 COMPILE_ASSERT_MATCHING_ENUM(PreloadAuto, AUTO);
432 #undef COMPILE_ASSERT_MATCHING_ENUM
434 void WebMediaPlayerImpl::setPreload(WebMediaPlayer::Preload preload) {
435 DVLOG(1) << __FUNCTION__ << "(" << preload << ")";
436 DCHECK(main_loop_->BelongsToCurrentThread());
438 preload_ = static_cast<content::Preload>(preload);
440 data_source_->SetPreload(preload_);
443 bool WebMediaPlayerImpl::hasVideo() const {
444 DCHECK(main_loop_->BelongsToCurrentThread());
446 return pipeline_metadata_.has_video;
449 bool WebMediaPlayerImpl::hasAudio() const {
450 DCHECK(main_loop_->BelongsToCurrentThread());
452 return pipeline_metadata_.has_audio;
455 blink::WebSize WebMediaPlayerImpl::naturalSize() const {
456 DCHECK(main_loop_->BelongsToCurrentThread());
458 return blink::WebSize(pipeline_metadata_.natural_size);
461 bool WebMediaPlayerImpl::paused() const {
462 DCHECK(main_loop_->BelongsToCurrentThread());
464 return pipeline_.GetPlaybackRate() == 0.0f;
467 bool WebMediaPlayerImpl::seeking() const {
468 DCHECK(main_loop_->BelongsToCurrentThread());
470 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
476 double WebMediaPlayerImpl::duration() const {
477 DCHECK(main_loop_->BelongsToCurrentThread());
479 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
480 return std::numeric_limits<double>::quiet_NaN();
482 return GetPipelineDuration();
485 double WebMediaPlayerImpl::timelineOffset() const {
486 DCHECK(main_loop_->BelongsToCurrentThread());
488 if (pipeline_metadata_.timeline_offset.is_null())
489 return std::numeric_limits<double>::quiet_NaN();
491 return pipeline_metadata_.timeline_offset.ToJsTime();
494 double WebMediaPlayerImpl::currentTime() const {
495 DCHECK(main_loop_->BelongsToCurrentThread());
496 return (paused_ ? paused_time_ : pipeline_.GetMediaTime()).InSecondsF();
499 WebMediaPlayer::NetworkState WebMediaPlayerImpl::networkState() const {
500 DCHECK(main_loop_->BelongsToCurrentThread());
501 return network_state_;
504 WebMediaPlayer::ReadyState WebMediaPlayerImpl::readyState() const {
505 DCHECK(main_loop_->BelongsToCurrentThread());
509 blink::WebTimeRanges WebMediaPlayerImpl::buffered() const {
510 DCHECK(main_loop_->BelongsToCurrentThread());
511 media::Ranges<base::TimeDelta> buffered_time_ranges =
512 pipeline_.GetBufferedTimeRanges();
513 buffered_data_source_host_.AddBufferedTimeRanges(
514 &buffered_time_ranges, pipeline_.GetMediaDuration());
515 return ConvertToWebTimeRanges(buffered_time_ranges);
518 double WebMediaPlayerImpl::maxTimeSeekable() const {
519 DCHECK(main_loop_->BelongsToCurrentThread());
521 // If we haven't even gotten to ReadyStateHaveMetadata yet then just
522 // return 0 so that the seekable range is empty.
523 if (ready_state_ < WebMediaPlayer::ReadyStateHaveMetadata)
526 // We don't support seeking in streaming media.
527 if (data_source_ && data_source_->IsStreaming())
532 bool WebMediaPlayerImpl::didLoadingProgress() {
533 DCHECK(main_loop_->BelongsToCurrentThread());
534 bool pipeline_progress = pipeline_.DidLoadingProgress();
535 bool data_progress = buffered_data_source_host_.DidLoadingProgress();
536 return pipeline_progress || data_progress;
539 void WebMediaPlayerImpl::paint(blink::WebCanvas* canvas,
540 const blink::WebRect& rect,
541 unsigned char alpha) {
542 paint(canvas, rect, alpha, SkXfermode::kSrcOver_Mode);
545 void WebMediaPlayerImpl::paint(blink::WebCanvas* canvas,
546 const blink::WebRect& rect,
548 SkXfermode::Mode mode) {
549 DCHECK(main_loop_->BelongsToCurrentThread());
550 TRACE_EVENT0("media", "WebMediaPlayerImpl:paint");
552 // TODO(scherkus): Clarify paint() API contract to better understand when and
553 // why it's being called. For example, today paint() is called when:
554 // - We haven't reached HAVE_CURRENT_DATA and need to paint black
555 // - We're painting to a canvas
556 // See http://crbug.com/341225 http://crbug.com/342621 for details.
557 scoped_refptr<media::VideoFrame> video_frame =
558 GetCurrentFrameFromCompositor();
560 gfx::Rect gfx_rect(rect);
562 skcanvas_video_renderer_.Paint(video_frame.get(),
567 pipeline_metadata_.video_rotation);
570 bool WebMediaPlayerImpl::hasSingleSecurityOrigin() const {
572 return data_source_->HasSingleOrigin();
576 bool WebMediaPlayerImpl::didPassCORSAccessCheck() const {
578 return data_source_->DidPassCORSAccessCheck();
582 double WebMediaPlayerImpl::mediaTimeForTimeValue(double timeValue) const {
583 return ConvertSecondsToTimestamp(timeValue).InSecondsF();
586 unsigned WebMediaPlayerImpl::decodedFrameCount() const {
587 DCHECK(main_loop_->BelongsToCurrentThread());
589 media::PipelineStatistics stats = pipeline_.GetStatistics();
590 return stats.video_frames_decoded;
593 unsigned WebMediaPlayerImpl::droppedFrameCount() const {
594 DCHECK(main_loop_->BelongsToCurrentThread());
596 media::PipelineStatistics stats = pipeline_.GetStatistics();
597 return stats.video_frames_dropped;
600 unsigned WebMediaPlayerImpl::audioDecodedByteCount() const {
601 DCHECK(main_loop_->BelongsToCurrentThread());
603 media::PipelineStatistics stats = pipeline_.GetStatistics();
604 return stats.audio_bytes_decoded;
607 unsigned WebMediaPlayerImpl::videoDecodedByteCount() const {
608 DCHECK(main_loop_->BelongsToCurrentThread());
610 media::PipelineStatistics stats = pipeline_.GetStatistics();
611 return stats.video_bytes_decoded;
614 bool WebMediaPlayerImpl::copyVideoTextureToPlatformTexture(
615 blink::WebGraphicsContext3D* web_graphics_context,
616 unsigned int texture,
618 unsigned int internal_format,
620 bool premultiply_alpha,
622 TRACE_EVENT0("media", "WebMediaPlayerImpl:copyVideoTextureToPlatformTexture");
624 scoped_refptr<media::VideoFrame> video_frame =
625 GetCurrentFrameFromCompositor();
629 if (video_frame->format() != media::VideoFrame::NATIVE_TEXTURE)
632 const gpu::MailboxHolder* mailbox_holder = video_frame->mailbox_holder();
633 if (mailbox_holder->texture_target != GL_TEXTURE_2D)
636 web_graphics_context->waitSyncPoint(mailbox_holder->sync_point);
637 uint32 source_texture = web_graphics_context->createAndConsumeTextureCHROMIUM(
638 GL_TEXTURE_2D, mailbox_holder->mailbox.name);
640 // The video is stored in a unmultiplied format, so premultiply
642 web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM,
644 // Application itself needs to take care of setting the right flip_y
645 // value down to get the expected result.
646 // flip_y==true means to reverse the video orientation while
647 // flip_y==false means to keep the intrinsic orientation.
648 web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, flip_y);
649 web_graphics_context->copyTextureCHROMIUM(GL_TEXTURE_2D,
655 web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, false);
656 web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM,
659 web_graphics_context->deleteTexture(source_texture);
660 web_graphics_context->flush();
662 SyncPointClientImpl client(web_graphics_context);
663 video_frame->UpdateReleaseSyncPoint(&client);
667 // Helper functions to report media EME related stats to UMA. They follow the
668 // convention of more commonly used macros UMA_HISTOGRAM_ENUMERATION and
669 // UMA_HISTOGRAM_COUNTS. The reason that we cannot use those macros directly is
670 // that UMA_* macros require the names to be constant throughout the process'
672 static void EmeUMAHistogramEnumeration(const std::string& key_system,
673 const std::string& method,
675 int boundary_value) {
676 base::LinearHistogram::FactoryGet(
677 kMediaEme + KeySystemNameForUMA(key_system) + "." + method,
678 1, boundary_value, boundary_value + 1,
679 base::Histogram::kUmaTargetedHistogramFlag)->Add(sample);
682 static void EmeUMAHistogramCounts(const std::string& key_system,
683 const std::string& method,
685 // Use the same parameters as UMA_HISTOGRAM_COUNTS.
686 base::Histogram::FactoryGet(
687 kMediaEme + KeySystemNameForUMA(key_system) + "." + method,
688 1, 1000000, 50, base::Histogram::kUmaTargetedHistogramFlag)->Add(sample);
691 // Helper enum for reporting generateKeyRequest/addKey histograms.
692 enum MediaKeyException {
695 kKeySystemNotSupported,
697 kMaxMediaKeyException
700 static MediaKeyException MediaKeyExceptionForUMA(
701 WebMediaPlayer::MediaKeyException e) {
703 case WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported:
704 return kKeySystemNotSupported;
705 case WebMediaPlayer::MediaKeyExceptionInvalidPlayerState:
706 return kInvalidPlayerState;
707 case WebMediaPlayer::MediaKeyExceptionNoError:
710 return kUnknownResultId;
714 // Helper for converting |key_system| name and exception |e| to a pair of enum
715 // values from above, for reporting to UMA.
716 static void ReportMediaKeyExceptionToUMA(const std::string& method,
717 const std::string& key_system,
718 WebMediaPlayer::MediaKeyException e) {
719 MediaKeyException result_id = MediaKeyExceptionForUMA(e);
720 DCHECK_NE(result_id, kUnknownResultId) << e;
721 EmeUMAHistogramEnumeration(
722 key_system, method, result_id, kMaxMediaKeyException);
725 // Convert a WebString to ASCII, falling back on an empty string in the case
726 // of a non-ASCII string.
727 static std::string ToASCIIOrEmpty(const blink::WebString& string) {
728 return base::IsStringASCII(string) ? base::UTF16ToASCII(string)
732 WebMediaPlayer::MediaKeyException
733 WebMediaPlayerImpl::generateKeyRequest(const WebString& key_system,
734 const unsigned char* init_data,
735 unsigned init_data_length) {
736 DVLOG(1) << "generateKeyRequest: " << base::string16(key_system) << ": "
737 << std::string(reinterpret_cast<const char*>(init_data),
738 static_cast<size_t>(init_data_length));
740 std::string ascii_key_system =
741 GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system));
743 WebMediaPlayer::MediaKeyException e =
744 GenerateKeyRequestInternal(ascii_key_system, init_data, init_data_length);
745 ReportMediaKeyExceptionToUMA("generateKeyRequest", ascii_key_system, e);
749 // Guess the type of |init_data|. This is only used to handle some corner cases
750 // so we keep it as simple as possible without breaking major use cases.
751 static std::string GuessInitDataType(const unsigned char* init_data,
752 unsigned init_data_length) {
753 // Most WebM files use KeyId of 16 bytes. MP4 init data are always >16 bytes.
754 if (init_data_length == 16)
760 WebMediaPlayer::MediaKeyException
761 WebMediaPlayerImpl::GenerateKeyRequestInternal(const std::string& key_system,
762 const unsigned char* init_data,
763 unsigned init_data_length) {
764 DCHECK(main_loop_->BelongsToCurrentThread());
766 if (!IsConcreteSupportedKeySystem(key_system))
767 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
769 // We do not support run-time switching between key systems for now.
770 if (current_key_system_.empty()) {
771 if (!proxy_decryptor_) {
772 proxy_decryptor_.reset(new ProxyDecryptor(
773 #if defined(ENABLE_PEPPER_CDMS)
774 // Create() must be called synchronously as |frame_| may not be
776 base::Bind(&PepperCdmWrapperImpl::Create, frame_),
777 #elif defined(ENABLE_BROWSER_CDMS)
778 #error Browser side CDM in WMPI for prefixed EME API not supported yet.
780 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnKeyAdded),
781 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnKeyError),
782 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnKeyMessage)));
785 GURL security_origin(frame_->document().securityOrigin().toString());
786 if (!proxy_decryptor_->InitializeCDM(key_system, security_origin))
787 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
789 if (proxy_decryptor_ && !decryptor_ready_cb_.is_null()) {
790 base::ResetAndReturn(&decryptor_ready_cb_)
791 .Run(proxy_decryptor_->GetDecryptor(), base::Bind(DoNothing));
794 current_key_system_ = key_system;
795 } else if (key_system != current_key_system_) {
796 return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState;
799 std::string init_data_type = init_data_type_;
800 if (init_data_type.empty())
801 init_data_type = GuessInitDataType(init_data, init_data_length);
803 // TODO(xhwang): We assume all streams are from the same container (thus have
804 // the same "type") for now. In the future, the "type" should be passed down
805 // from the application.
806 if (!proxy_decryptor_->GenerateKeyRequest(
807 init_data_type, init_data, init_data_length)) {
808 current_key_system_.clear();
809 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
812 return WebMediaPlayer::MediaKeyExceptionNoError;
815 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::addKey(
816 const WebString& key_system,
817 const unsigned char* key,
819 const unsigned char* init_data,
820 unsigned init_data_length,
821 const WebString& session_id) {
822 DVLOG(1) << "addKey: " << base::string16(key_system) << ": "
823 << std::string(reinterpret_cast<const char*>(key),
824 static_cast<size_t>(key_length)) << ", "
825 << std::string(reinterpret_cast<const char*>(init_data),
826 static_cast<size_t>(init_data_length)) << " ["
827 << base::string16(session_id) << "]";
829 std::string ascii_key_system =
830 GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system));
831 std::string ascii_session_id = ToASCIIOrEmpty(session_id);
833 WebMediaPlayer::MediaKeyException e = AddKeyInternal(ascii_key_system,
839 ReportMediaKeyExceptionToUMA("addKey", ascii_key_system, e);
843 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::AddKeyInternal(
844 const std::string& key_system,
845 const unsigned char* key,
847 const unsigned char* init_data,
848 unsigned init_data_length,
849 const std::string& session_id) {
851 DCHECK_GT(key_length, 0u);
853 if (!IsConcreteSupportedKeySystem(key_system))
854 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
856 if (current_key_system_.empty() || key_system != current_key_system_)
857 return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState;
859 proxy_decryptor_->AddKey(
860 key, key_length, init_data, init_data_length, session_id);
861 return WebMediaPlayer::MediaKeyExceptionNoError;
864 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::cancelKeyRequest(
865 const WebString& key_system,
866 const WebString& session_id) {
867 DVLOG(1) << "cancelKeyRequest: " << base::string16(key_system) << ": "
868 << " [" << base::string16(session_id) << "]";
870 std::string ascii_key_system =
871 GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system));
872 std::string ascii_session_id = ToASCIIOrEmpty(session_id);
874 WebMediaPlayer::MediaKeyException e =
875 CancelKeyRequestInternal(ascii_key_system, ascii_session_id);
876 ReportMediaKeyExceptionToUMA("cancelKeyRequest", ascii_key_system, e);
880 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::CancelKeyRequestInternal(
881 const std::string& key_system,
882 const std::string& session_id) {
883 if (!IsConcreteSupportedKeySystem(key_system))
884 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
886 if (current_key_system_.empty() || key_system != current_key_system_)
887 return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState;
889 proxy_decryptor_->CancelKeyRequest(session_id);
890 return WebMediaPlayer::MediaKeyExceptionNoError;
893 void WebMediaPlayerImpl::setContentDecryptionModule(
894 blink::WebContentDecryptionModule* cdm) {
895 DCHECK(main_loop_->BelongsToCurrentThread());
897 // TODO(xhwang): Support setMediaKeys(0) if necessary: http://crbug.com/330324
901 web_cdm_ = ToWebContentDecryptionModuleImpl(cdm);
903 if (web_cdm_ && !decryptor_ready_cb_.is_null())
904 base::ResetAndReturn(&decryptor_ready_cb_)
905 .Run(web_cdm_->GetDecryptor(), base::Bind(DoNothing));
908 void WebMediaPlayerImpl::setContentDecryptionModule(
909 blink::WebContentDecryptionModule* cdm,
910 blink::WebContentDecryptionModuleResult result) {
911 DCHECK(main_loop_->BelongsToCurrentThread());
913 // TODO(xhwang): Support setMediaKeys(0) if necessary: http://crbug.com/330324
915 result.completeWithError(
916 blink::WebContentDecryptionModuleExceptionNotSupportedError,
918 "Null MediaKeys object is not supported.");
922 web_cdm_ = ToWebContentDecryptionModuleImpl(cdm);
924 if (web_cdm_ && !decryptor_ready_cb_.is_null()) {
925 base::ResetAndReturn(&decryptor_ready_cb_)
926 .Run(web_cdm_->GetDecryptor(),
927 BIND_TO_RENDER_LOOP1(
928 &WebMediaPlayerImpl::ContentDecryptionModuleAttached, result));
930 // No pipeline/decoder connected, so resolve the promise. When something
931 // is connected, setting the CDM will happen in SetDecryptorReadyCB().
932 ContentDecryptionModuleAttached(result, true);
936 void WebMediaPlayerImpl::setContentDecryptionModuleSync(
937 blink::WebContentDecryptionModule* cdm) {
938 DCHECK(main_loop_->BelongsToCurrentThread());
940 // Used when loading media and no pipeline/decoder attached yet.
941 DCHECK(decryptor_ready_cb_.is_null());
943 web_cdm_ = ToWebContentDecryptionModuleImpl(cdm);
946 void WebMediaPlayerImpl::ContentDecryptionModuleAttached(
947 blink::WebContentDecryptionModuleResult result,
954 result.completeWithError(
955 blink::WebContentDecryptionModuleExceptionNotSupportedError,
957 "Unable to set MediaKeys object");
960 void WebMediaPlayerImpl::OnPipelineSeeked(bool time_changed,
961 PipelineStatus status) {
962 DVLOG(1) << __FUNCTION__ << "(" << time_changed << ", " << status << ")";
963 DCHECK(main_loop_->BelongsToCurrentThread());
966 pending_seek_ = false;
967 seek(pending_seek_seconds_);
971 if (status != media::PIPELINE_OK) {
972 OnPipelineError(status);
976 // Update our paused time.
978 paused_time_ = pipeline_.GetMediaTime();
980 should_notify_time_changed_ = time_changed;
983 void WebMediaPlayerImpl::OnPipelineEnded() {
984 DVLOG(1) << __FUNCTION__;
985 DCHECK(main_loop_->BelongsToCurrentThread());
986 client_->timeChanged();
989 void WebMediaPlayerImpl::OnPipelineError(PipelineStatus error) {
990 DCHECK(main_loop_->BelongsToCurrentThread());
991 DCHECK_NE(error, media::PIPELINE_OK);
993 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) {
994 // Any error that occurs before reaching ReadyStateHaveMetadata should
995 // be considered a format error.
996 SetNetworkState(WebMediaPlayer::NetworkStateFormatError);
1000 SetNetworkState(PipelineErrorToNetworkState(error));
1002 if (error == media::PIPELINE_ERROR_DECRYPT)
1003 EmeUMAHistogramCounts(current_key_system_, "DecryptError", 1);
1006 void WebMediaPlayerImpl::OnPipelineMetadata(
1007 media::PipelineMetadata metadata) {
1008 DVLOG(1) << __FUNCTION__;
1010 pipeline_metadata_ = metadata;
1012 UMA_HISTOGRAM_ENUMERATION("Media.VideoRotation",
1013 metadata.video_rotation,
1014 media::VIDEO_ROTATION_MAX + 1);
1015 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
1018 DCHECK(!video_weblayer_);
1019 scoped_refptr<cc::VideoLayer> layer =
1020 cc::VideoLayer::Create(compositor_, pipeline_metadata_.video_rotation);
1022 if (pipeline_metadata_.video_rotation == media::VIDEO_ROTATION_90 ||
1023 pipeline_metadata_.video_rotation == media::VIDEO_ROTATION_270) {
1024 gfx::Size size = pipeline_metadata_.natural_size;
1025 pipeline_metadata_.natural_size = gfx::Size(size.height(), size.width());
1028 video_weblayer_.reset(new WebLayerImpl(layer));
1029 video_weblayer_->setOpaque(opaque_);
1030 client_->setWebLayer(video_weblayer_.get());
1034 void WebMediaPlayerImpl::OnPipelineBufferingStateChanged(
1035 media::BufferingState buffering_state) {
1036 DVLOG(1) << __FUNCTION__ << "(" << buffering_state << ")";
1038 // Ignore buffering state changes until we've completed all outstanding seeks.
1039 if (seeking_ || pending_seek_)
1042 // TODO(scherkus): Handle other buffering states when Pipeline starts using
1043 // them and translate them ready state changes http://crbug.com/144683
1044 DCHECK_EQ(buffering_state, media::BUFFERING_HAVE_ENOUGH);
1045 SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData);
1047 // Blink expects a timeChanged() in response to a seek().
1048 if (should_notify_time_changed_)
1049 client_->timeChanged();
1052 void WebMediaPlayerImpl::OnDemuxerOpened() {
1053 DCHECK(main_loop_->BelongsToCurrentThread());
1054 client_->mediaSourceOpened(new WebMediaSourceImpl(
1055 chunk_demuxer_, base::Bind(&LogMediaSourceError, media_log_)));
1058 void WebMediaPlayerImpl::OnKeyAdded(const std::string& session_id) {
1059 DCHECK(main_loop_->BelongsToCurrentThread());
1060 EmeUMAHistogramCounts(current_key_system_, "KeyAdded", 1);
1062 WebString::fromUTF8(GetPrefixedKeySystemName(current_key_system_)),
1063 WebString::fromUTF8(session_id));
1066 void WebMediaPlayerImpl::OnNeedKey(const std::string& type,
1067 const std::vector<uint8>& init_data) {
1068 DCHECK(main_loop_->BelongsToCurrentThread());
1070 // Do not fire NeedKey event if encrypted media is not enabled.
1071 if (!blink::WebRuntimeFeatures::isPrefixedEncryptedMediaEnabled() &&
1072 !blink::WebRuntimeFeatures::isEncryptedMediaEnabled()) {
1076 UMA_HISTOGRAM_COUNTS(kMediaEme + std::string("NeedKey"), 1);
1078 DCHECK(init_data_type_.empty() || type.empty() || type == init_data_type_);
1079 if (init_data_type_.empty())
1080 init_data_type_ = type;
1082 const uint8* init_data_ptr = init_data.empty() ? NULL : &init_data[0];
1084 WebString::fromUTF8(type), init_data_ptr, init_data.size());
1087 void WebMediaPlayerImpl::OnAddTextTrack(
1088 const media::TextTrackConfig& config,
1089 const media::AddTextTrackDoneCB& done_cb) {
1090 DCHECK(main_loop_->BelongsToCurrentThread());
1092 const WebInbandTextTrackImpl::Kind web_kind =
1093 static_cast<WebInbandTextTrackImpl::Kind>(config.kind());
1094 const blink::WebString web_label =
1095 blink::WebString::fromUTF8(config.label());
1096 const blink::WebString web_language =
1097 blink::WebString::fromUTF8(config.language());
1098 const blink::WebString web_id =
1099 blink::WebString::fromUTF8(config.id());
1101 scoped_ptr<WebInbandTextTrackImpl> web_inband_text_track(
1102 new WebInbandTextTrackImpl(web_kind, web_label, web_language, web_id,
1103 text_track_index_++));
1105 scoped_ptr<media::TextTrack> text_track(
1106 new TextTrackImpl(main_loop_, client_, web_inband_text_track.Pass()));
1108 done_cb.Run(text_track.Pass());
1111 void WebMediaPlayerImpl::OnKeyError(const std::string& session_id,
1112 media::MediaKeys::KeyError error_code,
1113 uint32 system_code) {
1114 DCHECK(main_loop_->BelongsToCurrentThread());
1116 EmeUMAHistogramEnumeration(current_key_system_, "KeyError",
1117 error_code, media::MediaKeys::kMaxKeyError);
1119 unsigned short short_system_code = 0;
1120 if (system_code > std::numeric_limits<unsigned short>::max()) {
1121 LOG(WARNING) << "system_code exceeds unsigned short limit.";
1122 short_system_code = std::numeric_limits<unsigned short>::max();
1124 short_system_code = static_cast<unsigned short>(system_code);
1128 WebString::fromUTF8(GetPrefixedKeySystemName(current_key_system_)),
1129 WebString::fromUTF8(session_id),
1130 static_cast<blink::WebMediaPlayerClient::MediaKeyErrorCode>(error_code),
1134 void WebMediaPlayerImpl::OnKeyMessage(const std::string& session_id,
1135 const std::vector<uint8>& message,
1136 const GURL& destination_url) {
1137 DCHECK(main_loop_->BelongsToCurrentThread());
1139 DCHECK(destination_url.is_empty() || destination_url.is_valid());
1141 client_->keyMessage(
1142 WebString::fromUTF8(GetPrefixedKeySystemName(current_key_system_)),
1143 WebString::fromUTF8(session_id),
1144 message.empty() ? NULL : &message[0],
1149 void WebMediaPlayerImpl::DataSourceInitialized(bool success) {
1150 DCHECK(main_loop_->BelongsToCurrentThread());
1153 SetNetworkState(WebMediaPlayer::NetworkStateFormatError);
1160 void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading) {
1161 if (!is_downloading && network_state_ == WebMediaPlayer::NetworkStateLoading)
1162 SetNetworkState(WebMediaPlayer::NetworkStateIdle);
1163 else if (is_downloading && network_state_ == WebMediaPlayer::NetworkStateIdle)
1164 SetNetworkState(WebMediaPlayer::NetworkStateLoading);
1165 media_log_->AddEvent(
1166 media_log_->CreateBooleanEvent(
1167 media::MediaLogEvent::NETWORK_ACTIVITY_SET,
1168 "is_downloading_data", is_downloading));
1171 void WebMediaPlayerImpl::StartPipeline() {
1172 DCHECK(main_loop_->BelongsToCurrentThread());
1173 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
1175 // Keep track if this is a MSE or non-MSE playback.
1176 UMA_HISTOGRAM_BOOLEAN("Media.MSE.Playback",
1177 (load_type_ == LoadTypeMediaSource));
1179 media::LogCB mse_log_cb;
1181 // Figure out which demuxer to use.
1182 if (load_type_ != LoadTypeMediaSource) {
1183 DCHECK(!chunk_demuxer_);
1184 DCHECK(data_source_);
1186 demuxer_.reset(new media::FFmpegDemuxer(
1187 media_loop_, data_source_.get(),
1188 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNeedKey),
1191 DCHECK(!chunk_demuxer_);
1192 DCHECK(!data_source_);
1194 mse_log_cb = base::Bind(&LogMediaSourceError, media_log_);
1196 chunk_demuxer_ = new media::ChunkDemuxer(
1197 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDemuxerOpened),
1198 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNeedKey),
1201 demuxer_.reset(chunk_demuxer_);
1204 scoped_ptr<media::FilterCollection> filter_collection(
1205 new media::FilterCollection());
1206 filter_collection->SetDemuxer(demuxer_.get());
1208 media::SetDecryptorReadyCB set_decryptor_ready_cb =
1209 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::SetDecryptorReadyCB);
1211 // Create our audio decoders and renderer.
1212 ScopedVector<media::AudioDecoder> audio_decoders;
1213 audio_decoders.push_back(new media::FFmpegAudioDecoder(media_loop_,
1215 audio_decoders.push_back(new media::OpusAudioDecoder(media_loop_));
1217 scoped_ptr<media::AudioRenderer> audio_renderer(new media::AudioRendererImpl(
1219 audio_source_provider_.get(),
1220 audio_decoders.Pass(),
1221 set_decryptor_ready_cb,
1222 RenderThreadImpl::current()->GetAudioHardwareConfig()));
1223 filter_collection->SetAudioRenderer(audio_renderer.Pass());
1225 // Create our video decoders and renderer.
1226 ScopedVector<media::VideoDecoder> video_decoders;
1228 if (gpu_factories_.get()) {
1229 video_decoders.push_back(
1230 new media::GpuVideoDecoder(gpu_factories_, media_log_));
1233 #if !defined(MEDIA_DISABLE_LIBVPX)
1234 video_decoders.push_back(new media::VpxVideoDecoder(media_loop_));
1235 #endif // !defined(MEDIA_DISABLE_LIBVPX)
1237 video_decoders.push_back(new media::FFmpegVideoDecoder(media_loop_));
1239 scoped_ptr<media::VideoRenderer> video_renderer(
1240 new media::VideoRendererImpl(
1242 video_decoders.Pass(),
1243 set_decryptor_ready_cb,
1244 base::Bind(&WebMediaPlayerImpl::FrameReady, base::Unretained(this)),
1246 filter_collection->SetVideoRenderer(video_renderer.Pass());
1248 if (cmd_line->HasSwitch(switches::kEnableInbandTextTracks)) {
1249 scoped_ptr<media::TextRenderer> text_renderer(
1250 new media::TextRenderer(
1252 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnAddTextTrack)));
1254 filter_collection->SetTextRenderer(text_renderer.Pass());
1257 // ... and we're ready to go!
1260 filter_collection.Pass(),
1261 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineEnded),
1262 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineError),
1263 BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnPipelineSeeked, false),
1264 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineMetadata),
1265 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineBufferingStateChanged),
1266 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDurationChanged));
1269 void WebMediaPlayerImpl::SetNetworkState(WebMediaPlayer::NetworkState state) {
1270 DVLOG(1) << __FUNCTION__ << "(" << state << ")";
1271 DCHECK(main_loop_->BelongsToCurrentThread());
1272 network_state_ = state;
1273 // Always notify to ensure client has the latest value.
1274 client_->networkStateChanged();
1277 void WebMediaPlayerImpl::SetReadyState(WebMediaPlayer::ReadyState state) {
1278 DVLOG(1) << __FUNCTION__ << "(" << state << ")";
1279 DCHECK(main_loop_->BelongsToCurrentThread());
1281 if (state == WebMediaPlayer::ReadyStateHaveEnoughData && data_source_ &&
1282 data_source_->assume_fully_buffered() &&
1283 network_state_ == WebMediaPlayer::NetworkStateLoading)
1284 SetNetworkState(WebMediaPlayer::NetworkStateLoaded);
1286 ready_state_ = state;
1287 // Always notify to ensure client has the latest value.
1288 client_->readyStateChanged();
1291 blink::WebAudioSourceProvider* WebMediaPlayerImpl::audioSourceProvider() {
1292 return audio_source_provider_.get();
1295 void WebMediaPlayerImpl::IncrementExternallyAllocatedMemory() {
1296 DCHECK(main_loop_->BelongsToCurrentThread());
1297 incremented_externally_allocated_memory_ = true;
1298 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
1299 kPlayerExtraMemory);
1302 double WebMediaPlayerImpl::GetPipelineDuration() const {
1303 base::TimeDelta duration = pipeline_.GetMediaDuration();
1305 // Return positive infinity if the resource is unbounded.
1306 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
1307 if (duration == media::kInfiniteDuration())
1308 return std::numeric_limits<double>::infinity();
1310 return duration.InSecondsF();
1313 void WebMediaPlayerImpl::OnDurationChanged() {
1314 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
1317 client_->durationChanged();
1320 void WebMediaPlayerImpl::OnNaturalSizeChanged(gfx::Size size) {
1321 DCHECK(main_loop_->BelongsToCurrentThread());
1322 DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
1323 TRACE_EVENT0("media", "WebMediaPlayerImpl::OnNaturalSizeChanged");
1325 media_log_->AddEvent(
1326 media_log_->CreateVideoSizeSetEvent(size.width(), size.height()));
1327 pipeline_metadata_.natural_size = size;
1329 client_->sizeChanged();
1332 void WebMediaPlayerImpl::OnOpacityChanged(bool opaque) {
1333 DCHECK(main_loop_->BelongsToCurrentThread());
1334 DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
1337 if (video_weblayer_)
1338 video_weblayer_->setOpaque(opaque_);
1341 void WebMediaPlayerImpl::FrameReady(
1342 const scoped_refptr<media::VideoFrame>& frame) {
1343 compositor_task_runner_->PostTask(
1345 base::Bind(&VideoFrameCompositor::UpdateCurrentFrame,
1346 base::Unretained(compositor_),
1350 void WebMediaPlayerImpl::SetDecryptorReadyCB(
1351 const media::DecryptorReadyCB& decryptor_ready_cb) {
1352 DCHECK(main_loop_->BelongsToCurrentThread());
1354 // Cancels the previous decryptor request.
1355 if (decryptor_ready_cb.is_null()) {
1356 if (!decryptor_ready_cb_.is_null()) {
1357 base::ResetAndReturn(&decryptor_ready_cb_)
1358 .Run(NULL, base::Bind(DoNothing));
1363 // TODO(xhwang): Support multiple decryptor notification request (e.g. from
1364 // video and audio). The current implementation is okay for the current
1365 // media pipeline since we initialize audio and video decoders in sequence.
1366 // But WebMediaPlayerImpl should not depend on media pipeline's implementation
1368 DCHECK(decryptor_ready_cb_.is_null());
1370 // Mixed use of prefixed and unprefixed EME APIs is disallowed by Blink.
1371 DCHECK(!proxy_decryptor_ || !web_cdm_);
1373 if (proxy_decryptor_) {
1374 decryptor_ready_cb.Run(proxy_decryptor_->GetDecryptor(),
1375 base::Bind(DoNothing));
1380 decryptor_ready_cb.Run(web_cdm_->GetDecryptor(), base::Bind(DoNothing));
1384 decryptor_ready_cb_ = decryptor_ready_cb;
1387 static void GetCurrentFrameAndSignal(
1388 VideoFrameCompositor* compositor,
1389 scoped_refptr<media::VideoFrame>* video_frame_out,
1390 base::WaitableEvent* event) {
1391 TRACE_EVENT0("media", "GetCurrentFrameAndSignal");
1392 *video_frame_out = compositor->GetCurrentFrame();
1396 scoped_refptr<media::VideoFrame>
1397 WebMediaPlayerImpl::GetCurrentFrameFromCompositor() {
1398 TRACE_EVENT0("media", "WebMediaPlayerImpl::GetCurrentFrameFromCompositor");
1399 if (compositor_task_runner_->BelongsToCurrentThread())
1400 return compositor_->GetCurrentFrame();
1402 // Use a posted task and waitable event instead of a lock otherwise
1403 // WebGL/Canvas can see different content than what the compositor is seeing.
1404 scoped_refptr<media::VideoFrame> video_frame;
1405 base::WaitableEvent event(false, false);
1406 compositor_task_runner_->PostTask(FROM_HERE,
1407 base::Bind(&GetCurrentFrameAndSignal,
1408 base::Unretained(compositor_),
1415 } // namespace content