Disable FileSystemProviderApiTest.BigFile
[chromium-blink-merge.git] / media / blink / webmediaplayer_impl.cc
blob12839eb06eeceecbc38ef54299c90695a5cd0ae6
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/blink/webmediaplayer_impl.h"
7 #include <algorithm>
8 #include <limits>
9 #include <string>
10 #include <vector>
12 #include "base/bind.h"
13 #include "base/callback.h"
14 #include "base/callback_helpers.h"
15 #include "base/debug/alias.h"
16 #include "base/debug/crash_logging.h"
17 #include "base/float_util.h"
18 #include "base/message_loop/message_loop_proxy.h"
19 #include "base/metrics/histogram.h"
20 #include "base/single_thread_task_runner.h"
21 #include "base/synchronization/waitable_event.h"
22 #include "base/trace_event/trace_event.h"
23 #include "cc/blink/web_layer_impl.h"
24 #include "cc/layers/video_layer.h"
25 #include "gpu/blink/webgraphicscontext3d_impl.h"
26 #include "media/audio/null_audio_sink.h"
27 #include "media/base/bind_to_current_loop.h"
28 #include "media/base/cdm_context.h"
29 #include "media/base/limits.h"
30 #include "media/base/media_log.h"
31 #include "media/base/pipeline.h"
32 #include "media/base/text_renderer.h"
33 #include "media/base/video_frame.h"
34 #include "media/blink/buffered_data_source.h"
35 #include "media/blink/encrypted_media_player_support.h"
36 #include "media/blink/texttrack_impl.h"
37 #include "media/blink/webaudiosourceprovider_impl.h"
38 #include "media/blink/webcontentdecryptionmodule_impl.h"
39 #include "media/blink/webinbandtexttrack_impl.h"
40 #include "media/blink/webmediaplayer_delegate.h"
41 #include "media/blink/webmediaplayer_util.h"
42 #include "media/blink/webmediasource_impl.h"
43 #include "media/filters/chunk_demuxer.h"
44 #include "media/filters/ffmpeg_demuxer.h"
45 #include "third_party/WebKit/public/platform/WebMediaSource.h"
46 #include "third_party/WebKit/public/platform/WebRect.h"
47 #include "third_party/WebKit/public/platform/WebSize.h"
48 #include "third_party/WebKit/public/platform/WebString.h"
49 #include "third_party/WebKit/public/platform/WebURL.h"
50 #include "third_party/WebKit/public/web/WebLocalFrame.h"
51 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
52 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
53 #include "third_party/WebKit/public/web/WebView.h"
55 using blink::WebCanvas;
56 using blink::WebMediaPlayer;
57 using blink::WebRect;
58 using blink::WebSize;
59 using blink::WebString;
61 namespace {
63 // Limits the range of playback rate.
65 // TODO(kylep): Revisit these.
67 // Vista has substantially lower performance than XP or Windows7. If you speed
68 // up a video too much, it can't keep up, and rendering stops updating except on
69 // the time bar. For really high speeds, audio becomes a bottleneck and we just
70 // use up the data we have, which may not achieve the speed requested, but will
71 // not crash the tab.
73 // A very slow speed, ie 0.00000001x, causes the machine to lock up. (It seems
74 // like a busy loop). It gets unresponsive, although its not completely dead.
76 // Also our timers are not very accurate (especially for ogg), which becomes
77 // evident at low speeds and on Vista. Since other speeds are risky and outside
78 // the norms, we think 1/16x to 16x is a safe and useful range for now.
79 const double kMinRate = 0.0625;
80 const double kMaxRate = 16.0;
82 } // namespace
84 namespace media {
86 class BufferedDataSourceHostImpl;
88 #define STATIC_ASSERT_MATCHING_ENUM(name) \
89 static_assert(static_cast<int>(WebMediaPlayer::CORSMode ## name) == \
90 static_cast<int>(BufferedResourceLoader::k ## name), \
91 "mismatching enum values: " #name)
92 STATIC_ASSERT_MATCHING_ENUM(Unspecified);
93 STATIC_ASSERT_MATCHING_ENUM(Anonymous);
94 STATIC_ASSERT_MATCHING_ENUM(UseCredentials);
95 #undef STATIC_ASSERT_MATCHING_ENUM
97 #define BIND_TO_RENDER_LOOP(function) \
98 (DCHECK(main_task_runner_->BelongsToCurrentThread()), \
99 BindToCurrentLoop(base::Bind(function, AsWeakPtr())))
101 #define BIND_TO_RENDER_LOOP1(function, arg1) \
102 (DCHECK(main_task_runner_->BelongsToCurrentThread()), \
103 BindToCurrentLoop(base::Bind(function, AsWeakPtr(), arg1)))
105 static void LogMediaSourceError(const scoped_refptr<MediaLog>& media_log,
106 const std::string& error) {
107 media_log->AddEvent(media_log->CreateMediaSourceErrorEvent(error));
110 WebMediaPlayerImpl::WebMediaPlayerImpl(
111 blink::WebLocalFrame* frame,
112 blink::WebMediaPlayerClient* client,
113 base::WeakPtr<WebMediaPlayerDelegate> delegate,
114 scoped_ptr<RendererFactory> renderer_factory,
115 scoped_ptr<CdmFactory> cdm_factory,
116 const WebMediaPlayerParams& params)
117 : frame_(frame),
118 network_state_(WebMediaPlayer::NetworkStateEmpty),
119 ready_state_(WebMediaPlayer::ReadyStateHaveNothing),
120 preload_(BufferedDataSource::AUTO),
121 main_task_runner_(base::MessageLoopProxy::current()),
122 media_task_runner_(params.media_task_runner()),
123 media_log_(params.media_log()),
124 pipeline_(media_task_runner_, media_log_.get()),
125 load_type_(LoadTypeURL),
126 opaque_(false),
127 paused_(true),
128 seeking_(false),
129 playback_rate_(0.0f),
130 ended_(false),
131 pending_seek_(false),
132 pending_seek_seconds_(0.0f),
133 should_notify_time_changed_(false),
134 client_(client),
135 delegate_(delegate),
136 defer_load_cb_(params.defer_load_cb()),
137 context_3d_cb_(params.context_3d_cb()),
138 supports_save_(true),
139 chunk_demuxer_(NULL),
140 compositor_task_runner_(params.compositor_task_runner()),
141 compositor_(new VideoFrameCompositor(
142 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNaturalSizeChanged),
143 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnOpacityChanged))),
144 encrypted_media_support_(
145 cdm_factory.Pass(),
146 client,
147 params.media_permission(),
148 base::Bind(&WebMediaPlayerImpl::SetCdm, AsWeakPtr())),
149 renderer_factory_(renderer_factory.Pass()) {
150 // Threaded compositing isn't enabled universally yet.
151 if (!compositor_task_runner_.get())
152 compositor_task_runner_ = base::MessageLoopProxy::current();
154 media_log_->AddEvent(
155 media_log_->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_CREATED));
157 if (params.initial_cdm()) {
158 SetCdm(
159 ToWebContentDecryptionModuleImpl(params.initial_cdm())->GetCdmContext(),
160 base::Bind(&IgnoreCdmAttached));
163 // TODO(xhwang): When we use an external Renderer, many methods won't work,
164 // e.g. GetCurrentFrameFromCompositor(). See http://crbug.com/434861
166 // Use the null sink if no sink was provided.
167 audio_source_provider_ = new WebAudioSourceProviderImpl(
168 params.audio_renderer_sink().get()
169 ? params.audio_renderer_sink()
170 : new NullAudioSink(media_task_runner_));
173 WebMediaPlayerImpl::~WebMediaPlayerImpl() {
174 client_->setWebLayer(NULL);
176 DCHECK(main_task_runner_->BelongsToCurrentThread());
177 media_log_->AddEvent(
178 media_log_->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_DESTROYED));
180 if (delegate_)
181 delegate_->PlayerGone(this);
183 // Abort any pending IO so stopping the pipeline doesn't get blocked.
184 if (data_source_)
185 data_source_->Abort();
186 if (chunk_demuxer_) {
187 chunk_demuxer_->Shutdown();
188 chunk_demuxer_ = NULL;
191 renderer_factory_.reset();
193 // Make sure to kill the pipeline so there's no more media threads running.
194 // Note: stopping the pipeline might block for a long time.
195 base::WaitableEvent waiter(false, false);
196 pipeline_.Stop(
197 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&waiter)));
198 waiter.Wait();
200 compositor_task_runner_->DeleteSoon(FROM_HERE, compositor_);
203 void WebMediaPlayerImpl::load(LoadType load_type, const blink::WebURL& url,
204 CORSMode cors_mode) {
205 DVLOG(1) << __FUNCTION__ << "(" << load_type << ", " << url << ", "
206 << cors_mode << ")";
207 if (!defer_load_cb_.is_null()) {
208 defer_load_cb_.Run(base::Bind(
209 &WebMediaPlayerImpl::DoLoad, AsWeakPtr(), load_type, url, cors_mode));
210 return;
212 DoLoad(load_type, url, cors_mode);
215 void WebMediaPlayerImpl::DoLoad(LoadType load_type,
216 const blink::WebURL& url,
217 CORSMode cors_mode) {
218 DCHECK(main_task_runner_->BelongsToCurrentThread());
220 GURL gurl(url);
221 ReportMediaSchemeUma(gurl);
223 // Set subresource URL for crash reporting.
224 base::debug::SetCrashKeyValue("subresource_url", gurl.spec());
226 load_type_ = load_type;
228 SetNetworkState(WebMediaPlayer::NetworkStateLoading);
229 SetReadyState(WebMediaPlayer::ReadyStateHaveNothing);
230 media_log_->AddEvent(media_log_->CreateLoadEvent(url.spec()));
232 // Media source pipelines can start immediately.
233 if (load_type == LoadTypeMediaSource) {
234 supports_save_ = false;
235 StartPipeline();
236 return;
239 // Otherwise it's a regular request which requires resolving the URL first.
240 data_source_.reset(new BufferedDataSource(
241 url,
242 static_cast<BufferedResourceLoader::CORSMode>(cors_mode),
243 main_task_runner_,
244 frame_,
245 media_log_.get(),
246 &buffered_data_source_host_,
247 base::Bind(&WebMediaPlayerImpl::NotifyDownloading, AsWeakPtr())));
248 data_source_->SetPreload(preload_);
249 data_source_->Initialize(
250 base::Bind(&WebMediaPlayerImpl::DataSourceInitialized, AsWeakPtr()));
253 void WebMediaPlayerImpl::play() {
254 DVLOG(1) << __FUNCTION__;
255 DCHECK(main_task_runner_->BelongsToCurrentThread());
257 paused_ = false;
258 pipeline_.SetPlaybackRate(playback_rate_);
259 if (data_source_)
260 data_source_->MediaIsPlaying();
262 media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PLAY));
264 if (delegate_ && playback_rate_ > 0)
265 delegate_->DidPlay(this);
268 void WebMediaPlayerImpl::pause() {
269 DVLOG(1) << __FUNCTION__;
270 DCHECK(main_task_runner_->BelongsToCurrentThread());
272 const bool was_already_paused = paused_ || playback_rate_ == 0;
273 paused_ = true;
274 pipeline_.SetPlaybackRate(0.0f);
275 if (data_source_)
276 data_source_->MediaIsPaused();
277 UpdatePausedTime();
279 media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PAUSE));
281 if (!was_already_paused && delegate_)
282 delegate_->DidPause(this);
285 bool WebMediaPlayerImpl::supportsSave() const {
286 DCHECK(main_task_runner_->BelongsToCurrentThread());
287 return supports_save_;
290 void WebMediaPlayerImpl::seek(double seconds) {
291 DVLOG(1) << __FUNCTION__ << "(" << seconds << "s)";
292 DCHECK(main_task_runner_->BelongsToCurrentThread());
294 ended_ = false;
296 ReadyState old_state = ready_state_;
297 if (ready_state_ > WebMediaPlayer::ReadyStateHaveMetadata)
298 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
300 base::TimeDelta seek_time = ConvertSecondsToTimestamp(seconds);
302 if (seeking_) {
303 pending_seek_ = true;
304 pending_seek_seconds_ = seconds;
305 if (chunk_demuxer_)
306 chunk_demuxer_->CancelPendingSeek(seek_time);
307 return;
310 media_log_->AddEvent(media_log_->CreateSeekEvent(seconds));
312 // Update our paused time.
313 // In paused state ignore the seek operations to current time if the loading
314 // is completed and generate OnPipelineBufferingStateChanged event to
315 // eventually fire seeking and seeked events
316 if (paused_) {
317 if (paused_time_ != seek_time) {
318 paused_time_ = seek_time;
319 } else if (old_state == ReadyStateHaveEnoughData) {
320 main_task_runner_->PostTask(
321 FROM_HERE,
322 base::Bind(&WebMediaPlayerImpl::OnPipelineBufferingStateChanged,
323 AsWeakPtr(), BUFFERING_HAVE_ENOUGH));
324 return;
328 seeking_ = true;
330 if (chunk_demuxer_)
331 chunk_demuxer_->StartWaitingForSeek(seek_time);
333 // Kick off the asynchronous seek!
334 pipeline_.Seek(
335 seek_time,
336 BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnPipelineSeeked, true));
339 void WebMediaPlayerImpl::setRate(double rate) {
340 DVLOG(1) << __FUNCTION__ << "(" << rate << ")";
341 DCHECK(main_task_runner_->BelongsToCurrentThread());
343 // TODO(kylep): Remove when support for negatives is added. Also, modify the
344 // following checks so rewind uses reasonable values also.
345 if (rate < 0.0)
346 return;
348 // Limit rates to reasonable values by clamping.
349 if (rate != 0.0) {
350 if (rate < kMinRate)
351 rate = kMinRate;
352 else if (rate > kMaxRate)
353 rate = kMaxRate;
354 if (playback_rate_ == 0 && !paused_ && delegate_)
355 delegate_->DidPlay(this);
356 } else if (playback_rate_ != 0 && !paused_ && delegate_) {
357 delegate_->DidPause(this);
360 playback_rate_ = rate;
361 if (!paused_) {
362 pipeline_.SetPlaybackRate(rate);
363 if (data_source_)
364 data_source_->MediaPlaybackRateChanged(rate);
368 void WebMediaPlayerImpl::setVolume(double volume) {
369 DVLOG(1) << __FUNCTION__ << "(" << volume << ")";
370 DCHECK(main_task_runner_->BelongsToCurrentThread());
372 pipeline_.SetVolume(volume);
375 #define STATIC_ASSERT_MATCHING_ENUM(webkit_name, chromium_name) \
376 static_assert(static_cast<int>(WebMediaPlayer::webkit_name) == \
377 static_cast<int>(BufferedDataSource::chromium_name), \
378 "mismatching enum values: " #webkit_name)
379 STATIC_ASSERT_MATCHING_ENUM(PreloadNone, NONE);
380 STATIC_ASSERT_MATCHING_ENUM(PreloadMetaData, METADATA);
381 STATIC_ASSERT_MATCHING_ENUM(PreloadAuto, AUTO);
382 #undef STATIC_ASSERT_MATCHING_ENUM
384 void WebMediaPlayerImpl::setPreload(WebMediaPlayer::Preload preload) {
385 DVLOG(1) << __FUNCTION__ << "(" << preload << ")";
386 DCHECK(main_task_runner_->BelongsToCurrentThread());
388 preload_ = static_cast<BufferedDataSource::Preload>(preload);
389 if (data_source_)
390 data_source_->SetPreload(preload_);
393 bool WebMediaPlayerImpl::hasVideo() const {
394 DCHECK(main_task_runner_->BelongsToCurrentThread());
396 return pipeline_metadata_.has_video;
399 bool WebMediaPlayerImpl::hasAudio() const {
400 DCHECK(main_task_runner_->BelongsToCurrentThread());
402 return pipeline_metadata_.has_audio;
405 blink::WebSize WebMediaPlayerImpl::naturalSize() const {
406 DCHECK(main_task_runner_->BelongsToCurrentThread());
408 return blink::WebSize(pipeline_metadata_.natural_size);
411 bool WebMediaPlayerImpl::paused() const {
412 DCHECK(main_task_runner_->BelongsToCurrentThread());
414 return pipeline_.GetPlaybackRate() == 0.0f;
417 bool WebMediaPlayerImpl::seeking() const {
418 DCHECK(main_task_runner_->BelongsToCurrentThread());
420 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
421 return false;
423 return seeking_;
426 double WebMediaPlayerImpl::duration() const {
427 DCHECK(main_task_runner_->BelongsToCurrentThread());
429 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
430 return std::numeric_limits<double>::quiet_NaN();
432 return GetPipelineDuration();
435 double WebMediaPlayerImpl::timelineOffset() const {
436 DCHECK(main_task_runner_->BelongsToCurrentThread());
438 if (pipeline_metadata_.timeline_offset.is_null())
439 return std::numeric_limits<double>::quiet_NaN();
441 return pipeline_metadata_.timeline_offset.ToJsTime();
444 double WebMediaPlayerImpl::currentTime() const {
445 DCHECK(main_task_runner_->BelongsToCurrentThread());
446 DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
448 // TODO(scherkus): Replace with an explicit ended signal to HTMLMediaElement,
449 // see http://crbug.com/409280
450 if (ended_)
451 return duration();
453 return (paused_ ? paused_time_ : pipeline_.GetMediaTime()).InSecondsF();
456 WebMediaPlayer::NetworkState WebMediaPlayerImpl::networkState() const {
457 DCHECK(main_task_runner_->BelongsToCurrentThread());
458 return network_state_;
461 WebMediaPlayer::ReadyState WebMediaPlayerImpl::readyState() const {
462 DCHECK(main_task_runner_->BelongsToCurrentThread());
463 return ready_state_;
466 blink::WebTimeRanges WebMediaPlayerImpl::buffered() const {
467 DCHECK(main_task_runner_->BelongsToCurrentThread());
469 Ranges<base::TimeDelta> buffered_time_ranges =
470 pipeline_.GetBufferedTimeRanges();
472 const base::TimeDelta duration = pipeline_.GetMediaDuration();
473 if (duration != kInfiniteDuration()) {
474 buffered_data_source_host_.AddBufferedTimeRanges(
475 &buffered_time_ranges, duration);
477 return ConvertToWebTimeRanges(buffered_time_ranges);
480 blink::WebTimeRanges WebMediaPlayerImpl::seekable() const {
481 DCHECK(main_task_runner_->BelongsToCurrentThread());
483 if (ready_state_ < WebMediaPlayer::ReadyStateHaveMetadata)
484 return blink::WebTimeRanges();
486 const double seekable_end = duration();
488 // Allow a special exception for seeks to zero for streaming sources with a
489 // finite duration; this allows looping to work.
490 const bool allow_seek_to_zero = data_source_ && data_source_->IsStreaming() &&
491 base::IsFinite(seekable_end);
493 // TODO(dalecurtis): Technically this allows seeking on media which return an
494 // infinite duration so long as DataSource::IsStreaming() is false. While not
495 // expected, disabling this breaks semi-live players, http://crbug.com/427412.
496 const blink::WebTimeRange seekable_range(
497 0.0, allow_seek_to_zero ? 0.0 : seekable_end);
498 return blink::WebTimeRanges(&seekable_range, 1);
501 bool WebMediaPlayerImpl::didLoadingProgress() {
502 DCHECK(main_task_runner_->BelongsToCurrentThread());
503 bool pipeline_progress = pipeline_.DidLoadingProgress();
504 bool data_progress = buffered_data_source_host_.DidLoadingProgress();
505 return pipeline_progress || data_progress;
508 void WebMediaPlayerImpl::paint(blink::WebCanvas* canvas,
509 const blink::WebRect& rect,
510 unsigned char alpha,
511 SkXfermode::Mode mode) {
512 DCHECK(main_task_runner_->BelongsToCurrentThread());
513 TRACE_EVENT0("media", "WebMediaPlayerImpl:paint");
515 // TODO(scherkus): Clarify paint() API contract to better understand when and
516 // why it's being called. For example, today paint() is called when:
517 // - We haven't reached HAVE_CURRENT_DATA and need to paint black
518 // - We're painting to a canvas
519 // See http://crbug.com/341225 http://crbug.com/342621 for details.
520 scoped_refptr<VideoFrame> video_frame =
521 GetCurrentFrameFromCompositor();
523 gfx::Rect gfx_rect(rect);
524 Context3D context_3d;
525 if (video_frame.get() &&
526 video_frame->format() == VideoFrame::NATIVE_TEXTURE) {
527 if (!context_3d_cb_.is_null()) {
528 context_3d = context_3d_cb_.Run();
530 // GPU Process crashed.
531 if (!context_3d.gl)
532 return;
534 skcanvas_video_renderer_.Paint(video_frame, canvas, gfx_rect, alpha, mode,
535 pipeline_metadata_.video_rotation, context_3d);
538 bool WebMediaPlayerImpl::hasSingleSecurityOrigin() const {
539 if (data_source_)
540 return data_source_->HasSingleOrigin();
541 return true;
544 bool WebMediaPlayerImpl::didPassCORSAccessCheck() const {
545 if (data_source_)
546 return data_source_->DidPassCORSAccessCheck();
547 return false;
550 double WebMediaPlayerImpl::mediaTimeForTimeValue(double timeValue) const {
551 return ConvertSecondsToTimestamp(timeValue).InSecondsF();
554 unsigned WebMediaPlayerImpl::decodedFrameCount() const {
555 DCHECK(main_task_runner_->BelongsToCurrentThread());
557 PipelineStatistics stats = pipeline_.GetStatistics();
558 return stats.video_frames_decoded;
561 unsigned WebMediaPlayerImpl::droppedFrameCount() const {
562 DCHECK(main_task_runner_->BelongsToCurrentThread());
564 PipelineStatistics stats = pipeline_.GetStatistics();
565 return stats.video_frames_dropped;
568 unsigned WebMediaPlayerImpl::audioDecodedByteCount() const {
569 DCHECK(main_task_runner_->BelongsToCurrentThread());
571 PipelineStatistics stats = pipeline_.GetStatistics();
572 return stats.audio_bytes_decoded;
575 unsigned WebMediaPlayerImpl::videoDecodedByteCount() const {
576 DCHECK(main_task_runner_->BelongsToCurrentThread());
578 PipelineStatistics stats = pipeline_.GetStatistics();
579 return stats.video_bytes_decoded;
582 bool WebMediaPlayerImpl::copyVideoTextureToPlatformTexture(
583 blink::WebGraphicsContext3D* web_graphics_context,
584 unsigned int texture,
585 unsigned int level,
586 unsigned int internal_format,
587 unsigned int type,
588 bool premultiply_alpha,
589 bool flip_y) {
590 TRACE_EVENT0("media", "WebMediaPlayerImpl:copyVideoTextureToPlatformTexture");
592 scoped_refptr<VideoFrame> video_frame =
593 GetCurrentFrameFromCompositor();
595 if (!video_frame.get() ||
596 video_frame->format() != VideoFrame::NATIVE_TEXTURE) {
597 return false;
600 // TODO(dshwang): need more elegant way to convert WebGraphicsContext3D to
601 // GLES2Interface.
602 gpu::gles2::GLES2Interface* gl =
603 static_cast<gpu_blink::WebGraphicsContext3DImpl*>(web_graphics_context)
604 ->GetGLInterface();
605 SkCanvasVideoRenderer::CopyVideoFrameTextureToGLTexture(
606 gl, video_frame.get(), texture, level, internal_format, type,
607 premultiply_alpha, flip_y);
608 return true;
611 WebMediaPlayer::MediaKeyException
612 WebMediaPlayerImpl::generateKeyRequest(const WebString& key_system,
613 const unsigned char* init_data,
614 unsigned init_data_length) {
615 DCHECK(main_task_runner_->BelongsToCurrentThread());
617 return encrypted_media_support_.GenerateKeyRequest(
618 frame_, key_system, init_data, init_data_length);
621 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::addKey(
622 const WebString& key_system,
623 const unsigned char* key,
624 unsigned key_length,
625 const unsigned char* init_data,
626 unsigned init_data_length,
627 const WebString& session_id) {
628 DCHECK(main_task_runner_->BelongsToCurrentThread());
630 return encrypted_media_support_.AddKey(
631 key_system, key, key_length, init_data, init_data_length, session_id);
634 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::cancelKeyRequest(
635 const WebString& key_system,
636 const WebString& session_id) {
637 DCHECK(main_task_runner_->BelongsToCurrentThread());
639 return encrypted_media_support_.CancelKeyRequest(key_system, session_id);
642 void WebMediaPlayerImpl::setContentDecryptionModule(
643 blink::WebContentDecryptionModule* cdm,
644 blink::WebContentDecryptionModuleResult result) {
645 DCHECK(main_task_runner_->BelongsToCurrentThread());
647 // TODO(xhwang): Support setMediaKeys(0) if necessary: http://crbug.com/330324
648 if (!cdm) {
649 result.completeWithError(
650 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
651 "Null MediaKeys object is not supported.");
652 return;
655 SetCdm(ToWebContentDecryptionModuleImpl(cdm)->GetCdmContext(),
656 BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnCdmAttached, result));
659 void WebMediaPlayerImpl::OnEncryptedMediaInitData(
660 const std::string& init_data_type,
661 const std::vector<uint8>& init_data) {
662 DCHECK(!init_data_type.empty());
664 // Do not fire "encrypted" event if encrypted media is not enabled.
665 // TODO(xhwang): Handle this in |client_|.
666 if (!blink::WebRuntimeFeatures::isPrefixedEncryptedMediaEnabled() &&
667 !blink::WebRuntimeFeatures::isEncryptedMediaEnabled()) {
668 return;
671 // TODO(xhwang): Update this UMA name.
672 UMA_HISTOGRAM_COUNTS("Media.EME.NeedKey", 1);
674 encrypted_media_support_.SetInitDataType(init_data_type);
676 const uint8* init_data_ptr = init_data.empty() ? nullptr : &init_data[0];
677 client_->encrypted(WebString::fromUTF8(init_data_type), init_data_ptr,
678 base::saturated_cast<unsigned int>(init_data.size()));
681 void WebMediaPlayerImpl::SetCdm(CdmContext* cdm_context,
682 const CdmAttachedCB& cdm_attached_cb) {
683 pipeline_.SetCdm(cdm_context, cdm_attached_cb);
686 void WebMediaPlayerImpl::OnCdmAttached(
687 blink::WebContentDecryptionModuleResult result,
688 bool success) {
689 if (success) {
690 result.complete();
691 return;
694 result.completeWithError(
695 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
696 "Unable to set MediaKeys object");
699 void WebMediaPlayerImpl::OnPipelineSeeked(bool time_changed,
700 PipelineStatus status) {
701 DVLOG(1) << __FUNCTION__ << "(" << time_changed << ", " << status << ")";
702 DCHECK(main_task_runner_->BelongsToCurrentThread());
703 seeking_ = false;
704 if (pending_seek_) {
705 pending_seek_ = false;
706 seek(pending_seek_seconds_);
707 return;
710 if (status != PIPELINE_OK) {
711 OnPipelineError(status);
712 return;
715 // Update our paused time.
716 if (paused_)
717 UpdatePausedTime();
719 should_notify_time_changed_ = time_changed;
722 void WebMediaPlayerImpl::OnPipelineEnded() {
723 DVLOG(1) << __FUNCTION__;
724 DCHECK(main_task_runner_->BelongsToCurrentThread());
726 // Ignore state changes until we've completed all outstanding seeks.
727 if (seeking_ || pending_seek_)
728 return;
730 ended_ = true;
731 client_->timeChanged();
734 void WebMediaPlayerImpl::OnPipelineError(PipelineStatus error) {
735 DCHECK(main_task_runner_->BelongsToCurrentThread());
736 DCHECK_NE(error, PIPELINE_OK);
738 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) {
739 // Any error that occurs before reaching ReadyStateHaveMetadata should
740 // be considered a format error.
741 SetNetworkState(WebMediaPlayer::NetworkStateFormatError);
742 return;
745 SetNetworkState(PipelineErrorToNetworkState(error));
747 if (error == PIPELINE_ERROR_DECRYPT)
748 encrypted_media_support_.OnPipelineDecryptError();
751 void WebMediaPlayerImpl::OnPipelineMetadata(
752 PipelineMetadata metadata) {
753 DVLOG(1) << __FUNCTION__;
755 pipeline_metadata_ = metadata;
757 UMA_HISTOGRAM_ENUMERATION("Media.VideoRotation",
758 metadata.video_rotation,
759 VIDEO_ROTATION_MAX + 1);
760 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
762 if (hasVideo()) {
763 DCHECK(!video_weblayer_);
764 scoped_refptr<cc::VideoLayer> layer =
765 cc::VideoLayer::Create(compositor_, pipeline_metadata_.video_rotation);
767 if (pipeline_metadata_.video_rotation == VIDEO_ROTATION_90 ||
768 pipeline_metadata_.video_rotation == VIDEO_ROTATION_270) {
769 gfx::Size size = pipeline_metadata_.natural_size;
770 pipeline_metadata_.natural_size = gfx::Size(size.height(), size.width());
773 video_weblayer_.reset(new cc_blink::WebLayerImpl(layer));
774 video_weblayer_->setOpaque(opaque_);
775 client_->setWebLayer(video_weblayer_.get());
779 void WebMediaPlayerImpl::OnPipelineBufferingStateChanged(
780 BufferingState buffering_state) {
781 DVLOG(1) << __FUNCTION__ << "(" << buffering_state << ")";
783 // Ignore buffering state changes until we've completed all outstanding seeks.
784 if (seeking_ || pending_seek_)
785 return;
787 // TODO(scherkus): Handle other buffering states when Pipeline starts using
788 // them and translate them ready state changes http://crbug.com/144683
789 DCHECK_EQ(buffering_state, BUFFERING_HAVE_ENOUGH);
790 SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData);
792 // Blink expects a timeChanged() in response to a seek().
793 if (should_notify_time_changed_)
794 client_->timeChanged();
797 void WebMediaPlayerImpl::OnDemuxerOpened() {
798 DCHECK(main_task_runner_->BelongsToCurrentThread());
799 client_->mediaSourceOpened(new WebMediaSourceImpl(
800 chunk_demuxer_, base::Bind(&LogMediaSourceError, media_log_)));
803 void WebMediaPlayerImpl::OnAddTextTrack(
804 const TextTrackConfig& config,
805 const AddTextTrackDoneCB& done_cb) {
806 DCHECK(main_task_runner_->BelongsToCurrentThread());
808 const WebInbandTextTrackImpl::Kind web_kind =
809 static_cast<WebInbandTextTrackImpl::Kind>(config.kind());
810 const blink::WebString web_label =
811 blink::WebString::fromUTF8(config.label());
812 const blink::WebString web_language =
813 blink::WebString::fromUTF8(config.language());
814 const blink::WebString web_id =
815 blink::WebString::fromUTF8(config.id());
817 scoped_ptr<WebInbandTextTrackImpl> web_inband_text_track(
818 new WebInbandTextTrackImpl(web_kind, web_label, web_language, web_id));
820 scoped_ptr<TextTrack> text_track(new TextTrackImpl(
821 main_task_runner_, client_, web_inband_text_track.Pass()));
823 done_cb.Run(text_track.Pass());
826 void WebMediaPlayerImpl::DataSourceInitialized(bool success) {
827 DCHECK(main_task_runner_->BelongsToCurrentThread());
829 if (!success) {
830 SetNetworkState(WebMediaPlayer::NetworkStateFormatError);
831 return;
834 StartPipeline();
837 void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading) {
838 if (!is_downloading && network_state_ == WebMediaPlayer::NetworkStateLoading)
839 SetNetworkState(WebMediaPlayer::NetworkStateIdle);
840 else if (is_downloading && network_state_ == WebMediaPlayer::NetworkStateIdle)
841 SetNetworkState(WebMediaPlayer::NetworkStateLoading);
842 media_log_->AddEvent(
843 media_log_->CreateBooleanEvent(
844 MediaLogEvent::NETWORK_ACTIVITY_SET,
845 "is_downloading_data", is_downloading));
848 void WebMediaPlayerImpl::StartPipeline() {
849 DCHECK(main_task_runner_->BelongsToCurrentThread());
851 // Keep track if this is a MSE or non-MSE playback.
852 UMA_HISTOGRAM_BOOLEAN("Media.MSE.Playback",
853 (load_type_ == LoadTypeMediaSource));
855 LogCB mse_log_cb;
856 Demuxer::EncryptedMediaInitDataCB encrypted_media_init_data_cb =
857 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnEncryptedMediaInitData);
859 // Figure out which demuxer to use.
860 if (load_type_ != LoadTypeMediaSource) {
861 DCHECK(!chunk_demuxer_);
862 DCHECK(data_source_);
864 demuxer_.reset(new FFmpegDemuxer(media_task_runner_, data_source_.get(),
865 encrypted_media_init_data_cb, media_log_));
866 } else {
867 DCHECK(!chunk_demuxer_);
868 DCHECK(!data_source_);
870 mse_log_cb = base::Bind(&LogMediaSourceError, media_log_);
872 chunk_demuxer_ = new ChunkDemuxer(
873 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDemuxerOpened),
874 encrypted_media_init_data_cb, mse_log_cb, media_log_, true);
875 demuxer_.reset(chunk_demuxer_);
878 // ... and we're ready to go!
879 seeking_ = true;
881 pipeline_.Start(
882 demuxer_.get(),
883 renderer_factory_->CreateRenderer(media_task_runner_,
884 audio_source_provider_.get()),
885 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineEnded),
886 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineError),
887 BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnPipelineSeeked, false),
888 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineMetadata),
889 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineBufferingStateChanged),
890 base::Bind(&WebMediaPlayerImpl::FrameReady, base::Unretained(this)),
891 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDurationChanged),
892 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnAddTextTrack));
895 void WebMediaPlayerImpl::SetNetworkState(WebMediaPlayer::NetworkState state) {
896 DVLOG(1) << __FUNCTION__ << "(" << state << ")";
897 DCHECK(main_task_runner_->BelongsToCurrentThread());
898 network_state_ = state;
899 // Always notify to ensure client has the latest value.
900 client_->networkStateChanged();
903 void WebMediaPlayerImpl::SetReadyState(WebMediaPlayer::ReadyState state) {
904 DVLOG(1) << __FUNCTION__ << "(" << state << ")";
905 DCHECK(main_task_runner_->BelongsToCurrentThread());
907 if (state == WebMediaPlayer::ReadyStateHaveEnoughData && data_source_ &&
908 data_source_->assume_fully_buffered() &&
909 network_state_ == WebMediaPlayer::NetworkStateLoading)
910 SetNetworkState(WebMediaPlayer::NetworkStateLoaded);
912 ready_state_ = state;
913 // Always notify to ensure client has the latest value.
914 client_->readyStateChanged();
917 blink::WebAudioSourceProvider* WebMediaPlayerImpl::audioSourceProvider() {
918 return audio_source_provider_.get();
921 double WebMediaPlayerImpl::GetPipelineDuration() const {
922 base::TimeDelta duration = pipeline_.GetMediaDuration();
924 // Return positive infinity if the resource is unbounded.
925 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
926 if (duration == kInfiniteDuration())
927 return std::numeric_limits<double>::infinity();
929 return duration.InSecondsF();
932 void WebMediaPlayerImpl::OnDurationChanged() {
933 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
934 return;
936 client_->durationChanged();
939 void WebMediaPlayerImpl::OnNaturalSizeChanged(gfx::Size size) {
940 DCHECK(main_task_runner_->BelongsToCurrentThread());
941 DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
942 TRACE_EVENT0("media", "WebMediaPlayerImpl::OnNaturalSizeChanged");
944 media_log_->AddEvent(
945 media_log_->CreateVideoSizeSetEvent(size.width(), size.height()));
946 pipeline_metadata_.natural_size = size;
948 client_->sizeChanged();
951 void WebMediaPlayerImpl::OnOpacityChanged(bool opaque) {
952 DCHECK(main_task_runner_->BelongsToCurrentThread());
953 DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
955 opaque_ = opaque;
956 if (video_weblayer_)
957 video_weblayer_->setOpaque(opaque_);
960 void WebMediaPlayerImpl::FrameReady(
961 const scoped_refptr<VideoFrame>& frame) {
962 compositor_task_runner_->PostTask(
963 FROM_HERE,
964 base::Bind(&VideoFrameCompositor::UpdateCurrentFrame,
965 base::Unretained(compositor_),
966 frame));
969 static void GetCurrentFrameAndSignal(
970 VideoFrameCompositor* compositor,
971 scoped_refptr<VideoFrame>* video_frame_out,
972 base::WaitableEvent* event) {
973 TRACE_EVENT0("media", "GetCurrentFrameAndSignal");
974 *video_frame_out = compositor->GetCurrentFrame();
975 event->Signal();
978 scoped_refptr<VideoFrame>
979 WebMediaPlayerImpl::GetCurrentFrameFromCompositor() {
980 TRACE_EVENT0("media", "WebMediaPlayerImpl::GetCurrentFrameFromCompositor");
981 if (compositor_task_runner_->BelongsToCurrentThread())
982 return compositor_->GetCurrentFrame();
984 // Use a posted task and waitable event instead of a lock otherwise
985 // WebGL/Canvas can see different content than what the compositor is seeing.
986 scoped_refptr<VideoFrame> video_frame;
987 base::WaitableEvent event(false, false);
988 compositor_task_runner_->PostTask(FROM_HERE,
989 base::Bind(&GetCurrentFrameAndSignal,
990 base::Unretained(compositor_),
991 &video_frame,
992 &event));
993 event.Wait();
994 return video_frame;
997 void WebMediaPlayerImpl::UpdatePausedTime() {
998 DCHECK(main_task_runner_->BelongsToCurrentThread());
1000 // pause() may be called after playback has ended and the HTMLMediaElement
1001 // requires that currentTime() == duration() after ending. We want to ensure
1002 // |paused_time_| matches currentTime() in this case or a future seek() may
1003 // incorrectly discard what it thinks is a seek to the existing time.
1004 paused_time_ =
1005 ended_ ? pipeline_.GetMediaDuration() : pipeline_.GetMediaTime();
1008 } // namespace media