Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / blink / webmediaplayer_impl.cc
blob2b7cd1ab3e385e2d7a951b4ccdd3c8f77e3c734b
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 <cmath>
9 #include <limits>
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/callback_helpers.h"
14 #include "base/debug/alias.h"
15 #include "base/debug/crash_logging.h"
16 #include "base/metrics/histogram.h"
17 #include "base/single_thread_task_runner.h"
18 #include "base/synchronization/waitable_event.h"
19 #include "base/thread_task_runner_handle.h"
20 #include "base/trace_event/trace_event.h"
21 #include "cc/blink/web_layer_impl.h"
22 #include "cc/layers/video_layer.h"
23 #include "gpu/blink/webgraphicscontext3d_impl.h"
24 #include "media/audio/null_audio_sink.h"
25 #include "media/base/bind_to_current_loop.h"
26 #include "media/base/cdm_context.h"
27 #include "media/base/limits.h"
28 #include "media/base/media_log.h"
29 #include "media/base/text_renderer.h"
30 #include "media/base/timestamp_constants.h"
31 #include "media/base/video_frame.h"
32 #include "media/blink/texttrack_impl.h"
33 #include "media/blink/webaudiosourceprovider_impl.h"
34 #include "media/blink/webcontentdecryptionmodule_impl.h"
35 #include "media/blink/webinbandtexttrack_impl.h"
36 #include "media/blink/webmediaplayer_delegate.h"
37 #include "media/blink/webmediaplayer_util.h"
38 #include "media/blink/webmediasource_impl.h"
39 #include "media/filters/chunk_demuxer.h"
40 #include "media/filters/ffmpeg_demuxer.h"
41 #include "third_party/WebKit/public/platform/WebEncryptedMediaTypes.h"
42 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h"
43 #include "third_party/WebKit/public/platform/WebMediaPlayerEncryptedMediaClient.h"
44 #include "third_party/WebKit/public/platform/WebMediaSource.h"
45 #include "third_party/WebKit/public/platform/WebRect.h"
46 #include "third_party/WebKit/public/platform/WebSize.h"
47 #include "third_party/WebKit/public/platform/WebString.h"
48 #include "third_party/WebKit/public/platform/WebURL.h"
49 #include "third_party/WebKit/public/web/WebDocument.h"
50 #include "third_party/WebKit/public/web/WebFrame.h"
51 #include "third_party/WebKit/public/web/WebLocalFrame.h"
52 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
53 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
54 #include "third_party/WebKit/public/web/WebView.h"
56 using blink::WebCanvas;
57 using blink::WebMediaPlayer;
58 using blink::WebRect;
59 using blink::WebSize;
60 using blink::WebString;
62 namespace {
64 // Limits the range of playback rate.
66 // TODO(kylep): Revisit these.
68 // Vista has substantially lower performance than XP or Windows7. If you speed
69 // up a video too much, it can't keep up, and rendering stops updating except on
70 // the time bar. For really high speeds, audio becomes a bottleneck and we just
71 // use up the data we have, which may not achieve the speed requested, but will
72 // not crash the tab.
74 // A very slow speed, ie 0.00000001x, causes the machine to lock up. (It seems
75 // like a busy loop). It gets unresponsive, although its not completely dead.
77 // Also our timers are not very accurate (especially for ogg), which becomes
78 // evident at low speeds and on Vista. Since other speeds are risky and outside
79 // the norms, we think 1/16x to 16x is a safe and useful range for now.
80 const double kMinRate = 0.0625;
81 const double kMaxRate = 16.0;
83 } // namespace
85 namespace media {
87 class BufferedDataSourceHostImpl;
89 #define STATIC_ASSERT_MATCHING_ENUM(name) \
90 static_assert(static_cast<int>(WebMediaPlayer::CORSMode ## name) == \
91 static_cast<int>(BufferedResourceLoader::k ## name), \
92 "mismatching enum values: " #name)
93 STATIC_ASSERT_MATCHING_ENUM(Unspecified);
94 STATIC_ASSERT_MATCHING_ENUM(Anonymous);
95 STATIC_ASSERT_MATCHING_ENUM(UseCredentials);
96 #undef STATIC_ASSERT_MATCHING_ENUM
98 #define BIND_TO_RENDER_LOOP(function) \
99 (DCHECK(main_task_runner_->BelongsToCurrentThread()), \
100 BindToCurrentLoop(base::Bind(function, AsWeakPtr())))
102 #define BIND_TO_RENDER_LOOP1(function, arg1) \
103 (DCHECK(main_task_runner_->BelongsToCurrentThread()), \
104 BindToCurrentLoop(base::Bind(function, AsWeakPtr(), arg1)))
106 WebMediaPlayerImpl::WebMediaPlayerImpl(
107 blink::WebLocalFrame* frame,
108 blink::WebMediaPlayerClient* client,
109 blink::WebMediaPlayerEncryptedMediaClient* encrypted_client,
110 base::WeakPtr<WebMediaPlayerDelegate> delegate,
111 scoped_ptr<RendererFactory> renderer_factory,
112 CdmFactory* cdm_factory,
113 const WebMediaPlayerParams& params)
114 : frame_(frame),
115 network_state_(WebMediaPlayer::NetworkStateEmpty),
116 ready_state_(WebMediaPlayer::ReadyStateHaveNothing),
117 preload_(BufferedDataSource::AUTO),
118 main_task_runner_(base::ThreadTaskRunnerHandle::Get()),
119 media_task_runner_(params.media_task_runner()),
120 worker_task_runner_(params.worker_task_runner()),
121 media_log_(params.media_log()),
122 pipeline_(media_task_runner_, media_log_.get()),
123 load_type_(LoadTypeURL),
124 opaque_(false),
125 playback_rate_(0.0),
126 paused_(true),
127 seeking_(false),
128 ended_(false),
129 pending_seek_(false),
130 should_notify_time_changed_(false),
131 client_(client),
132 encrypted_client_(encrypted_client),
133 delegate_(delegate),
134 defer_load_cb_(params.defer_load_cb()),
135 context_3d_cb_(params.context_3d_cb()),
136 supports_save_(true),
137 chunk_demuxer_(NULL),
138 // Threaded compositing isn't enabled universally yet.
139 compositor_task_runner_(
140 params.compositor_task_runner()
141 ? params.compositor_task_runner()
142 : base::MessageLoop::current()->task_runner()),
143 compositor_(new VideoFrameCompositor(
144 compositor_task_runner_,
145 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNaturalSizeChanged),
146 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnOpacityChanged))),
147 encrypted_media_support_(cdm_factory,
148 encrypted_client,
149 params.media_permission(),
150 base::Bind(&WebMediaPlayerImpl::SetCdm,
151 AsWeakPtr(),
152 base::Bind(&IgnoreCdmAttached))),
153 renderer_factory_(renderer_factory.Pass()) {
154 media_log_->AddEvent(
155 media_log_->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_CREATED));
157 if (params.initial_cdm()) {
158 SetCdm(base::Bind(&IgnoreCdmAttached),
159 ToWebContentDecryptionModuleImpl(params.initial_cdm())
160 ->GetCdmContext());
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());
178 if (delegate_)
179 delegate_->PlayerGone(this);
181 // Abort any pending IO so stopping the pipeline doesn't get blocked.
182 if (data_source_)
183 data_source_->Abort();
184 if (chunk_demuxer_) {
185 chunk_demuxer_->Shutdown();
186 chunk_demuxer_ = NULL;
189 renderer_factory_.reset();
191 // Make sure to kill the pipeline so there's no more media threads running.
192 // Note: stopping the pipeline might block for a long time.
193 base::WaitableEvent waiter(false, false);
194 pipeline_.Stop(
195 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&waiter)));
196 waiter.Wait();
198 compositor_task_runner_->DeleteSoon(FROM_HERE, compositor_);
200 media_log_->AddEvent(
201 media_log_->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_DESTROYED));
204 void WebMediaPlayerImpl::load(LoadType load_type, const blink::WebURL& url,
205 CORSMode cors_mode) {
206 DVLOG(1) << __FUNCTION__ << "(" << load_type << ", " << url << ", "
207 << cors_mode << ")";
208 if (!defer_load_cb_.is_null()) {
209 defer_load_cb_.Run(base::Bind(
210 &WebMediaPlayerImpl::DoLoad, AsWeakPtr(), load_type, url, cors_mode));
211 return;
213 DoLoad(load_type, url, cors_mode);
216 void WebMediaPlayerImpl::DoLoad(LoadType load_type,
217 const blink::WebURL& url,
218 CORSMode cors_mode) {
219 DCHECK(main_task_runner_->BelongsToCurrentThread());
221 GURL gurl(url);
222 ReportMetrics(load_type, gurl,
223 GURL(frame_->document().securityOrigin().toString()));
225 // Set subresource URL for crash reporting.
226 base::debug::SetCrashKeyValue("subresource_url", gurl.spec());
228 load_type_ = load_type;
230 SetNetworkState(WebMediaPlayer::NetworkStateLoading);
231 SetReadyState(WebMediaPlayer::ReadyStateHaveNothing);
232 media_log_->AddEvent(media_log_->CreateLoadEvent(url.spec()));
234 // Media source pipelines can start immediately.
235 if (load_type == LoadTypeMediaSource) {
236 supports_save_ = false;
237 StartPipeline();
238 return;
241 // Otherwise it's a regular request which requires resolving the URL first.
242 data_source_.reset(new BufferedDataSource(
243 url,
244 static_cast<BufferedResourceLoader::CORSMode>(cors_mode),
245 main_task_runner_,
246 frame_,
247 media_log_.get(),
248 &buffered_data_source_host_,
249 base::Bind(&WebMediaPlayerImpl::NotifyDownloading, AsWeakPtr())));
250 data_source_->SetPreload(preload_);
251 data_source_->Initialize(
252 base::Bind(&WebMediaPlayerImpl::DataSourceInitialized, AsWeakPtr()));
255 void WebMediaPlayerImpl::play() {
256 DVLOG(1) << __FUNCTION__;
257 DCHECK(main_task_runner_->BelongsToCurrentThread());
259 paused_ = false;
260 pipeline_.SetPlaybackRate(playback_rate_);
261 if (data_source_)
262 data_source_->MediaIsPlaying();
264 media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PLAY));
266 if (delegate_ && playback_rate_ > 0)
267 delegate_->DidPlay(this);
270 void WebMediaPlayerImpl::pause() {
271 DVLOG(1) << __FUNCTION__;
272 DCHECK(main_task_runner_->BelongsToCurrentThread());
274 const bool was_already_paused = paused_ || playback_rate_ == 0;
275 paused_ = true;
276 pipeline_.SetPlaybackRate(0.0);
277 if (data_source_)
278 data_source_->MediaIsPaused();
279 UpdatePausedTime();
281 media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PAUSE));
283 if (!was_already_paused && delegate_)
284 delegate_->DidPause(this);
287 bool WebMediaPlayerImpl::supportsSave() const {
288 DCHECK(main_task_runner_->BelongsToCurrentThread());
289 return supports_save_;
292 void WebMediaPlayerImpl::seek(double seconds) {
293 DVLOG(1) << __FUNCTION__ << "(" << seconds << "s)";
294 DCHECK(main_task_runner_->BelongsToCurrentThread());
296 ended_ = false;
298 ReadyState old_state = ready_state_;
299 if (ready_state_ > WebMediaPlayer::ReadyStateHaveMetadata)
300 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
302 base::TimeDelta new_seek_time = base::TimeDelta::FromSecondsD(seconds);
304 if (seeking_) {
305 if (new_seek_time == seek_time_) {
306 if (chunk_demuxer_) {
307 // Don't suppress any redundant in-progress MSE seek. There could have
308 // been changes to the underlying buffers after seeking the demuxer and
309 // before receiving OnPipelineSeeked() for the currently in-progress
310 // seek.
311 MEDIA_LOG(DEBUG, media_log_)
312 << "Detected MediaSource seek to same time as in-progress seek to "
313 << seek_time_ << ".";
314 } else {
315 // Suppress all redundant seeks if unrestricted by media source demuxer
316 // API.
317 pending_seek_ = false;
318 pending_seek_time_ = base::TimeDelta();
319 return;
323 pending_seek_ = true;
324 pending_seek_time_ = new_seek_time;
325 if (chunk_demuxer_)
326 chunk_demuxer_->CancelPendingSeek(pending_seek_time_);
327 return;
330 media_log_->AddEvent(media_log_->CreateSeekEvent(seconds));
332 // Update our paused time.
333 // For non-MSE playbacks, in paused state ignore the seek operations to
334 // current time if the loading is completed and generate
335 // OnPipelineBufferingStateChanged event to eventually fire seeking and seeked
336 // events. We don't short-circuit MSE seeks in this logic because the
337 // underlying buffers around the seek time might have changed (or even been
338 // removed) since previous seek/preroll/pause action, and the pipeline might
339 // need to flush so the new buffers are decoded and rendered instead of the
340 // old ones.
341 if (paused_) {
342 if (paused_time_ != new_seek_time || chunk_demuxer_) {
343 paused_time_ = new_seek_time;
344 } else if (old_state == ReadyStateHaveEnoughData) {
345 main_task_runner_->PostTask(
346 FROM_HERE,
347 base::Bind(&WebMediaPlayerImpl::OnPipelineBufferingStateChanged,
348 AsWeakPtr(), BUFFERING_HAVE_ENOUGH));
349 return;
353 seeking_ = true;
354 seek_time_ = new_seek_time;
356 if (chunk_demuxer_)
357 chunk_demuxer_->StartWaitingForSeek(seek_time_);
359 // Kick off the asynchronous seek!
360 pipeline_.Seek(seek_time_, BIND_TO_RENDER_LOOP1(
361 &WebMediaPlayerImpl::OnPipelineSeeked, true));
364 void WebMediaPlayerImpl::setRate(double rate) {
365 DVLOG(1) << __FUNCTION__ << "(" << rate << ")";
366 DCHECK(main_task_runner_->BelongsToCurrentThread());
368 // TODO(kylep): Remove when support for negatives is added. Also, modify the
369 // following checks so rewind uses reasonable values also.
370 if (rate < 0.0)
371 return;
373 // Limit rates to reasonable values by clamping.
374 if (rate != 0.0) {
375 if (rate < kMinRate)
376 rate = kMinRate;
377 else if (rate > kMaxRate)
378 rate = kMaxRate;
379 if (playback_rate_ == 0 && !paused_ && delegate_)
380 delegate_->DidPlay(this);
381 } else if (playback_rate_ != 0 && !paused_ && delegate_) {
382 delegate_->DidPause(this);
385 playback_rate_ = rate;
386 if (!paused_) {
387 pipeline_.SetPlaybackRate(rate);
388 if (data_source_)
389 data_source_->MediaPlaybackRateChanged(rate);
393 void WebMediaPlayerImpl::setVolume(double volume) {
394 DVLOG(1) << __FUNCTION__ << "(" << volume << ")";
395 DCHECK(main_task_runner_->BelongsToCurrentThread());
397 pipeline_.SetVolume(volume);
400 void WebMediaPlayerImpl::setSinkId(const blink::WebString& device_id,
401 WebSetSinkIdCB* web_callback) {
402 DCHECK(main_task_runner_->BelongsToCurrentThread());
403 DVLOG(1) << __FUNCTION__;
404 media::SwitchOutputDeviceCB callback =
405 media::ConvertToSwitchOutputDeviceCB(web_callback);
406 OutputDevice* output_device = audio_source_provider_->GetOutputDevice();
407 if (output_device) {
408 std::string device_id_str(device_id.utf8());
409 url::Origin security_origin(
410 GURL(frame_->securityOrigin().toString().utf8()));
411 output_device->SwitchOutputDevice(device_id_str, security_origin, callback);
412 } else {
413 callback.Run(SWITCH_OUTPUT_DEVICE_RESULT_ERROR_INTERNAL);
417 #define STATIC_ASSERT_MATCHING_ENUM(webkit_name, chromium_name) \
418 static_assert(static_cast<int>(WebMediaPlayer::webkit_name) == \
419 static_cast<int>(BufferedDataSource::chromium_name), \
420 "mismatching enum values: " #webkit_name)
421 STATIC_ASSERT_MATCHING_ENUM(PreloadNone, NONE);
422 STATIC_ASSERT_MATCHING_ENUM(PreloadMetaData, METADATA);
423 STATIC_ASSERT_MATCHING_ENUM(PreloadAuto, AUTO);
424 #undef STATIC_ASSERT_MATCHING_ENUM
426 void WebMediaPlayerImpl::setPreload(WebMediaPlayer::Preload preload) {
427 DVLOG(1) << __FUNCTION__ << "(" << preload << ")";
428 DCHECK(main_task_runner_->BelongsToCurrentThread());
430 preload_ = static_cast<BufferedDataSource::Preload>(preload);
431 if (data_source_)
432 data_source_->SetPreload(preload_);
435 bool WebMediaPlayerImpl::hasVideo() const {
436 DCHECK(main_task_runner_->BelongsToCurrentThread());
438 return pipeline_metadata_.has_video;
441 bool WebMediaPlayerImpl::hasAudio() const {
442 DCHECK(main_task_runner_->BelongsToCurrentThread());
444 return pipeline_metadata_.has_audio;
447 blink::WebSize WebMediaPlayerImpl::naturalSize() const {
448 DCHECK(main_task_runner_->BelongsToCurrentThread());
450 return blink::WebSize(pipeline_metadata_.natural_size);
453 bool WebMediaPlayerImpl::paused() const {
454 DCHECK(main_task_runner_->BelongsToCurrentThread());
456 return pipeline_.GetPlaybackRate() == 0.0f;
459 bool WebMediaPlayerImpl::seeking() const {
460 DCHECK(main_task_runner_->BelongsToCurrentThread());
462 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
463 return false;
465 return seeking_;
468 double WebMediaPlayerImpl::duration() const {
469 DCHECK(main_task_runner_->BelongsToCurrentThread());
471 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
472 return std::numeric_limits<double>::quiet_NaN();
474 return GetPipelineDuration();
477 double WebMediaPlayerImpl::timelineOffset() const {
478 DCHECK(main_task_runner_->BelongsToCurrentThread());
480 if (pipeline_metadata_.timeline_offset.is_null())
481 return std::numeric_limits<double>::quiet_NaN();
483 return pipeline_metadata_.timeline_offset.ToJsTime();
486 double WebMediaPlayerImpl::currentTime() const {
487 DCHECK(main_task_runner_->BelongsToCurrentThread());
488 DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
490 // TODO(scherkus): Replace with an explicit ended signal to HTMLMediaElement,
491 // see http://crbug.com/409280
492 if (ended_)
493 return duration();
495 // We know the current seek time better than pipeline: pipeline may processing
496 // an earlier seek before a pending seek has been started, or it might not yet
497 // have the current seek time returnable via GetMediaTime().
498 if (seeking()) {
499 return pending_seek_ ? pending_seek_time_.InSecondsF()
500 : seek_time_.InSecondsF();
503 return (paused_ ? paused_time_ : pipeline_.GetMediaTime()).InSecondsF();
506 WebMediaPlayer::NetworkState WebMediaPlayerImpl::networkState() const {
507 DCHECK(main_task_runner_->BelongsToCurrentThread());
508 return network_state_;
511 WebMediaPlayer::ReadyState WebMediaPlayerImpl::readyState() const {
512 DCHECK(main_task_runner_->BelongsToCurrentThread());
513 return ready_state_;
516 blink::WebTimeRanges WebMediaPlayerImpl::buffered() const {
517 DCHECK(main_task_runner_->BelongsToCurrentThread());
519 Ranges<base::TimeDelta> buffered_time_ranges =
520 pipeline_.GetBufferedTimeRanges();
522 const base::TimeDelta duration = pipeline_.GetMediaDuration();
523 if (duration != kInfiniteDuration()) {
524 buffered_data_source_host_.AddBufferedTimeRanges(
525 &buffered_time_ranges, duration);
527 return ConvertToWebTimeRanges(buffered_time_ranges);
530 blink::WebTimeRanges WebMediaPlayerImpl::seekable() const {
531 DCHECK(main_task_runner_->BelongsToCurrentThread());
533 if (ready_state_ < WebMediaPlayer::ReadyStateHaveMetadata)
534 return blink::WebTimeRanges();
536 const double seekable_end = duration();
538 // Allow a special exception for seeks to zero for streaming sources with a
539 // finite duration; this allows looping to work.
540 const bool allow_seek_to_zero = data_source_ && data_source_->IsStreaming() &&
541 std::isfinite(seekable_end);
543 // TODO(dalecurtis): Technically this allows seeking on media which return an
544 // infinite duration so long as DataSource::IsStreaming() is false. While not
545 // expected, disabling this breaks semi-live players, http://crbug.com/427412.
546 const blink::WebTimeRange seekable_range(
547 0.0, allow_seek_to_zero ? 0.0 : seekable_end);
548 return blink::WebTimeRanges(&seekable_range, 1);
551 bool WebMediaPlayerImpl::didLoadingProgress() {
552 DCHECK(main_task_runner_->BelongsToCurrentThread());
553 bool pipeline_progress = pipeline_.DidLoadingProgress();
554 bool data_progress = buffered_data_source_host_.DidLoadingProgress();
555 return pipeline_progress || data_progress;
558 void WebMediaPlayerImpl::paint(blink::WebCanvas* canvas,
559 const blink::WebRect& rect,
560 unsigned char alpha,
561 SkXfermode::Mode mode) {
562 DCHECK(main_task_runner_->BelongsToCurrentThread());
563 TRACE_EVENT0("media", "WebMediaPlayerImpl:paint");
565 // TODO(scherkus): Clarify paint() API contract to better understand when and
566 // why it's being called. For example, today paint() is called when:
567 // - We haven't reached HAVE_CURRENT_DATA and need to paint black
568 // - We're painting to a canvas
569 // See http://crbug.com/341225 http://crbug.com/342621 for details.
570 scoped_refptr<VideoFrame> video_frame = GetCurrentFrameFromCompositor();
572 gfx::Rect gfx_rect(rect);
573 Context3D context_3d;
574 if (video_frame.get() && video_frame->HasTextures()) {
575 if (!context_3d_cb_.is_null())
576 context_3d = context_3d_cb_.Run();
577 // GPU Process crashed.
578 if (!context_3d.gl)
579 return;
581 skcanvas_video_renderer_.Paint(video_frame, canvas, gfx::RectF(gfx_rect),
582 alpha, mode, pipeline_metadata_.video_rotation,
583 context_3d);
586 bool WebMediaPlayerImpl::hasSingleSecurityOrigin() const {
587 if (data_source_)
588 return data_source_->HasSingleOrigin();
589 return true;
592 bool WebMediaPlayerImpl::didPassCORSAccessCheck() const {
593 if (data_source_)
594 return data_source_->DidPassCORSAccessCheck();
595 return false;
598 double WebMediaPlayerImpl::mediaTimeForTimeValue(double timeValue) const {
599 return base::TimeDelta::FromSecondsD(timeValue).InSecondsF();
602 unsigned WebMediaPlayerImpl::decodedFrameCount() const {
603 DCHECK(main_task_runner_->BelongsToCurrentThread());
605 PipelineStatistics stats = pipeline_.GetStatistics();
606 return stats.video_frames_decoded;
609 unsigned WebMediaPlayerImpl::droppedFrameCount() const {
610 DCHECK(main_task_runner_->BelongsToCurrentThread());
612 PipelineStatistics stats = pipeline_.GetStatistics();
613 return stats.video_frames_dropped;
616 unsigned WebMediaPlayerImpl::audioDecodedByteCount() const {
617 DCHECK(main_task_runner_->BelongsToCurrentThread());
619 PipelineStatistics stats = pipeline_.GetStatistics();
620 return stats.audio_bytes_decoded;
623 unsigned WebMediaPlayerImpl::videoDecodedByteCount() const {
624 DCHECK(main_task_runner_->BelongsToCurrentThread());
626 PipelineStatistics stats = pipeline_.GetStatistics();
627 return stats.video_bytes_decoded;
630 bool WebMediaPlayerImpl::copyVideoTextureToPlatformTexture(
631 blink::WebGraphicsContext3D* web_graphics_context,
632 unsigned int texture,
633 unsigned int internal_format,
634 unsigned int type,
635 bool premultiply_alpha,
636 bool flip_y) {
637 TRACE_EVENT0("media", "WebMediaPlayerImpl:copyVideoTextureToPlatformTexture");
639 scoped_refptr<VideoFrame> video_frame = GetCurrentFrameFromCompositor();
641 if (!video_frame.get() || !video_frame->HasTextures() ||
642 media::VideoFrame::NumPlanes(video_frame->format()) != 1) {
643 return false;
646 // TODO(dshwang): need more elegant way to convert WebGraphicsContext3D to
647 // GLES2Interface.
648 gpu::gles2::GLES2Interface* gl =
649 static_cast<gpu_blink::WebGraphicsContext3DImpl*>(web_graphics_context)
650 ->GetGLInterface();
651 SkCanvasVideoRenderer::CopyVideoFrameSingleTextureToGLTexture(
652 gl, video_frame.get(), texture, internal_format, type, premultiply_alpha,
653 flip_y);
654 return true;
657 WebMediaPlayer::MediaKeyException
658 WebMediaPlayerImpl::generateKeyRequest(const WebString& key_system,
659 const unsigned char* init_data,
660 unsigned init_data_length) {
661 DCHECK(main_task_runner_->BelongsToCurrentThread());
663 return encrypted_media_support_.GenerateKeyRequest(
664 frame_, key_system, init_data, init_data_length);
667 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::addKey(
668 const WebString& key_system,
669 const unsigned char* key,
670 unsigned key_length,
671 const unsigned char* init_data,
672 unsigned init_data_length,
673 const WebString& session_id) {
674 DCHECK(main_task_runner_->BelongsToCurrentThread());
676 return encrypted_media_support_.AddKey(
677 key_system, key, key_length, init_data, init_data_length, session_id);
680 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::cancelKeyRequest(
681 const WebString& key_system,
682 const WebString& session_id) {
683 DCHECK(main_task_runner_->BelongsToCurrentThread());
685 return encrypted_media_support_.CancelKeyRequest(key_system, session_id);
688 void WebMediaPlayerImpl::setContentDecryptionModule(
689 blink::WebContentDecryptionModule* cdm,
690 blink::WebContentDecryptionModuleResult result) {
691 DCHECK(main_task_runner_->BelongsToCurrentThread());
693 // Once the CDM is set it can't be cleared as there may be frames being
694 // decrypted on other threads. So fail this request.
695 // http://crbug.com/462365#c7.
696 if (!cdm) {
697 result.completeWithError(
698 blink::WebContentDecryptionModuleExceptionInvalidStateError, 0,
699 "The existing MediaKeys object cannot be removed at this time.");
700 return;
703 // Although unlikely, it is possible that multiple calls happen
704 // simultaneously, so fail this call if there is already one pending.
705 if (set_cdm_result_) {
706 result.completeWithError(
707 blink::WebContentDecryptionModuleExceptionInvalidStateError, 0,
708 "Unable to set MediaKeys object at this time.");
709 return;
712 // Create a local copy of |result| to avoid problems with the callback
713 // getting passed to the media thread and causing |result| to be destructed
714 // on the wrong thread in some failure conditions.
715 set_cdm_result_.reset(new blink::WebContentDecryptionModuleResult(result));
717 SetCdm(BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnCdmAttached),
718 ToWebContentDecryptionModuleImpl(cdm)->GetCdmContext());
721 void WebMediaPlayerImpl::OnEncryptedMediaInitData(
722 EmeInitDataType init_data_type,
723 const std::vector<uint8>& init_data) {
724 DCHECK(init_data_type != EmeInitDataType::UNKNOWN);
726 // Do not fire "encrypted" event if encrypted media is not enabled.
727 // TODO(xhwang): Handle this in |client_|.
728 if (!blink::WebRuntimeFeatures::isPrefixedEncryptedMediaEnabled() &&
729 !blink::WebRuntimeFeatures::isEncryptedMediaEnabled()) {
730 return;
733 // TODO(xhwang): Update this UMA name.
734 UMA_HISTOGRAM_COUNTS("Media.EME.NeedKey", 1);
736 encrypted_media_support_.SetInitDataType(init_data_type);
738 encrypted_client_->encrypted(
739 ConvertToWebInitDataType(init_data_type), vector_as_array(&init_data),
740 base::saturated_cast<unsigned int>(init_data.size()));
743 void WebMediaPlayerImpl::OnWaitingForDecryptionKey() {
744 encrypted_client_->didBlockPlaybackWaitingForKey();
746 // TODO(jrummell): didResumePlaybackBlockedForKey() should only be called
747 // when a key has been successfully added (e.g. OnSessionKeysChange() with
748 // |has_additional_usable_key| = true). http://crbug.com/461903
749 encrypted_client_->didResumePlaybackBlockedForKey();
752 void WebMediaPlayerImpl::SetCdm(const CdmAttachedCB& cdm_attached_cb,
753 CdmContext* cdm_context) {
754 // If CDM initialization succeeded, tell the pipeline about it.
755 if (cdm_context)
756 pipeline_.SetCdm(cdm_context, cdm_attached_cb);
759 void WebMediaPlayerImpl::OnCdmAttached(bool success) {
760 if (success) {
761 set_cdm_result_->complete();
762 set_cdm_result_.reset();
763 return;
766 set_cdm_result_->completeWithError(
767 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
768 "Unable to set MediaKeys object");
769 set_cdm_result_.reset();
772 void WebMediaPlayerImpl::OnPipelineSeeked(bool time_changed,
773 PipelineStatus status) {
774 DVLOG(1) << __FUNCTION__ << "(" << time_changed << ", " << status << ")";
775 DCHECK(main_task_runner_->BelongsToCurrentThread());
776 seeking_ = false;
777 seek_time_ = base::TimeDelta();
778 if (pending_seek_) {
779 double pending_seek_seconds = pending_seek_time_.InSecondsF();
780 pending_seek_ = false;
781 pending_seek_time_ = base::TimeDelta();
782 seek(pending_seek_seconds);
783 return;
786 if (status != PIPELINE_OK) {
787 OnPipelineError(status);
788 return;
791 // Update our paused time.
792 if (paused_)
793 UpdatePausedTime();
795 should_notify_time_changed_ = time_changed;
798 void WebMediaPlayerImpl::OnPipelineEnded() {
799 DVLOG(1) << __FUNCTION__;
800 DCHECK(main_task_runner_->BelongsToCurrentThread());
802 // Ignore state changes until we've completed all outstanding seeks.
803 if (seeking_ || pending_seek_)
804 return;
806 ended_ = true;
807 client_->timeChanged();
810 void WebMediaPlayerImpl::OnPipelineError(PipelineStatus error) {
811 DCHECK(main_task_runner_->BelongsToCurrentThread());
812 DCHECK_NE(error, PIPELINE_OK);
814 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) {
815 // Any error that occurs before reaching ReadyStateHaveMetadata should
816 // be considered a format error.
817 SetNetworkState(WebMediaPlayer::NetworkStateFormatError);
818 return;
821 SetNetworkState(PipelineErrorToNetworkState(error));
824 void WebMediaPlayerImpl::OnPipelineMetadata(
825 PipelineMetadata metadata) {
826 DVLOG(1) << __FUNCTION__;
828 pipeline_metadata_ = metadata;
830 UMA_HISTOGRAM_ENUMERATION("Media.VideoRotation", metadata.video_rotation,
831 VIDEO_ROTATION_MAX + 1);
832 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
834 if (hasVideo()) {
835 DCHECK(!video_weblayer_);
836 scoped_refptr<cc::VideoLayer> layer =
837 cc::VideoLayer::Create(cc_blink::WebLayerImpl::LayerSettings(),
838 compositor_, pipeline_metadata_.video_rotation);
840 if (pipeline_metadata_.video_rotation == VIDEO_ROTATION_90 ||
841 pipeline_metadata_.video_rotation == VIDEO_ROTATION_270) {
842 gfx::Size size = pipeline_metadata_.natural_size;
843 pipeline_metadata_.natural_size = gfx::Size(size.height(), size.width());
846 video_weblayer_.reset(new cc_blink::WebLayerImpl(layer));
847 video_weblayer_->layer()->SetContentsOpaque(opaque_);
848 video_weblayer_->SetContentsOpaqueIsFixed(true);
849 client_->setWebLayer(video_weblayer_.get());
853 void WebMediaPlayerImpl::OnPipelineBufferingStateChanged(
854 BufferingState buffering_state) {
855 DVLOG(1) << __FUNCTION__ << "(" << buffering_state << ")";
857 // Ignore buffering state changes until we've completed all outstanding seeks.
858 if (seeking_ || pending_seek_)
859 return;
861 // TODO(scherkus): Handle other buffering states when Pipeline starts using
862 // them and translate them ready state changes http://crbug.com/144683
863 DCHECK_EQ(buffering_state, BUFFERING_HAVE_ENOUGH);
864 SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData);
866 // Let the DataSource know we have enough data. It may use this information to
867 // release unused network connections.
868 if (data_source_)
869 data_source_->OnBufferingHaveEnough();
871 // Blink expects a timeChanged() in response to a seek().
872 if (should_notify_time_changed_)
873 client_->timeChanged();
876 void WebMediaPlayerImpl::OnDemuxerOpened() {
877 DCHECK(main_task_runner_->BelongsToCurrentThread());
878 client_->mediaSourceOpened(
879 new WebMediaSourceImpl(chunk_demuxer_, media_log_));
882 void WebMediaPlayerImpl::OnAddTextTrack(
883 const TextTrackConfig& config,
884 const AddTextTrackDoneCB& done_cb) {
885 DCHECK(main_task_runner_->BelongsToCurrentThread());
887 const WebInbandTextTrackImpl::Kind web_kind =
888 static_cast<WebInbandTextTrackImpl::Kind>(config.kind());
889 const blink::WebString web_label =
890 blink::WebString::fromUTF8(config.label());
891 const blink::WebString web_language =
892 blink::WebString::fromUTF8(config.language());
893 const blink::WebString web_id =
894 blink::WebString::fromUTF8(config.id());
896 scoped_ptr<WebInbandTextTrackImpl> web_inband_text_track(
897 new WebInbandTextTrackImpl(web_kind, web_label, web_language, web_id));
899 scoped_ptr<TextTrack> text_track(new TextTrackImpl(
900 main_task_runner_, client_, web_inband_text_track.Pass()));
902 done_cb.Run(text_track.Pass());
905 void WebMediaPlayerImpl::DataSourceInitialized(bool success) {
906 DCHECK(main_task_runner_->BelongsToCurrentThread());
908 if (!success) {
909 SetNetworkState(WebMediaPlayer::NetworkStateFormatError);
910 return;
913 StartPipeline();
916 void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading) {
917 if (!is_downloading && network_state_ == WebMediaPlayer::NetworkStateLoading)
918 SetNetworkState(WebMediaPlayer::NetworkStateIdle);
919 else if (is_downloading && network_state_ == WebMediaPlayer::NetworkStateIdle)
920 SetNetworkState(WebMediaPlayer::NetworkStateLoading);
921 media_log_->AddEvent(
922 media_log_->CreateBooleanEvent(
923 MediaLogEvent::NETWORK_ACTIVITY_SET,
924 "is_downloading_data", is_downloading));
927 void WebMediaPlayerImpl::StartPipeline() {
928 DCHECK(main_task_runner_->BelongsToCurrentThread());
930 Demuxer::EncryptedMediaInitDataCB encrypted_media_init_data_cb =
931 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnEncryptedMediaInitData);
933 // Figure out which demuxer to use.
934 if (load_type_ != LoadTypeMediaSource) {
935 DCHECK(!chunk_demuxer_);
936 DCHECK(data_source_);
938 demuxer_.reset(new FFmpegDemuxer(media_task_runner_, data_source_.get(),
939 encrypted_media_init_data_cb, media_log_));
940 } else {
941 DCHECK(!chunk_demuxer_);
942 DCHECK(!data_source_);
944 chunk_demuxer_ = new ChunkDemuxer(
945 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDemuxerOpened),
946 encrypted_media_init_data_cb, media_log_, true);
947 demuxer_.reset(chunk_demuxer_);
950 // ... and we're ready to go!
951 seeking_ = true;
953 pipeline_.Start(
954 demuxer_.get(), renderer_factory_->CreateRenderer(
955 media_task_runner_, worker_task_runner_,
956 audio_source_provider_.get(), compositor_),
957 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineEnded),
958 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineError),
959 BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnPipelineSeeked, false),
960 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineMetadata),
961 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineBufferingStateChanged),
962 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDurationChanged),
963 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnAddTextTrack),
964 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnWaitingForDecryptionKey));
967 void WebMediaPlayerImpl::SetNetworkState(WebMediaPlayer::NetworkState state) {
968 DVLOG(1) << __FUNCTION__ << "(" << state << ")";
969 DCHECK(main_task_runner_->BelongsToCurrentThread());
970 network_state_ = state;
971 // Always notify to ensure client has the latest value.
972 client_->networkStateChanged();
975 void WebMediaPlayerImpl::SetReadyState(WebMediaPlayer::ReadyState state) {
976 DVLOG(1) << __FUNCTION__ << "(" << state << ")";
977 DCHECK(main_task_runner_->BelongsToCurrentThread());
979 if (state == WebMediaPlayer::ReadyStateHaveEnoughData && data_source_ &&
980 data_source_->assume_fully_buffered() &&
981 network_state_ == WebMediaPlayer::NetworkStateLoading)
982 SetNetworkState(WebMediaPlayer::NetworkStateLoaded);
984 ready_state_ = state;
985 // Always notify to ensure client has the latest value.
986 client_->readyStateChanged();
989 blink::WebAudioSourceProvider* WebMediaPlayerImpl::audioSourceProvider() {
990 return audio_source_provider_.get();
993 double WebMediaPlayerImpl::GetPipelineDuration() const {
994 base::TimeDelta duration = pipeline_.GetMediaDuration();
996 // Return positive infinity if the resource is unbounded.
997 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
998 if (duration == kInfiniteDuration())
999 return std::numeric_limits<double>::infinity();
1001 return duration.InSecondsF();
1004 void WebMediaPlayerImpl::OnDurationChanged() {
1005 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
1006 return;
1008 client_->durationChanged();
1011 void WebMediaPlayerImpl::OnNaturalSizeChanged(gfx::Size size) {
1012 DCHECK(main_task_runner_->BelongsToCurrentThread());
1013 DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
1014 TRACE_EVENT0("media", "WebMediaPlayerImpl::OnNaturalSizeChanged");
1016 media_log_->AddEvent(
1017 media_log_->CreateVideoSizeSetEvent(size.width(), size.height()));
1018 pipeline_metadata_.natural_size = size;
1020 client_->sizeChanged();
1023 void WebMediaPlayerImpl::OnOpacityChanged(bool opaque) {
1024 DCHECK(main_task_runner_->BelongsToCurrentThread());
1025 DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
1027 opaque_ = opaque;
1028 // Modify content opaqueness of cc::Layer directly so that
1029 // SetContentsOpaqueIsFixed is ignored.
1030 if (video_weblayer_)
1031 video_weblayer_->layer()->SetContentsOpaque(opaque_);
1034 static void GetCurrentFrameAndSignal(
1035 VideoFrameCompositor* compositor,
1036 scoped_refptr<VideoFrame>* video_frame_out,
1037 base::WaitableEvent* event) {
1038 TRACE_EVENT0("media", "GetCurrentFrameAndSignal");
1039 *video_frame_out = compositor->GetCurrentFrameAndUpdateIfStale();
1040 event->Signal();
1043 scoped_refptr<VideoFrame>
1044 WebMediaPlayerImpl::GetCurrentFrameFromCompositor() {
1045 TRACE_EVENT0("media", "WebMediaPlayerImpl::GetCurrentFrameFromCompositor");
1046 if (compositor_task_runner_->BelongsToCurrentThread())
1047 return compositor_->GetCurrentFrameAndUpdateIfStale();
1049 // Use a posted task and waitable event instead of a lock otherwise
1050 // WebGL/Canvas can see different content than what the compositor is seeing.
1051 scoped_refptr<VideoFrame> video_frame;
1052 base::WaitableEvent event(false, false);
1053 compositor_task_runner_->PostTask(FROM_HERE,
1054 base::Bind(&GetCurrentFrameAndSignal,
1055 base::Unretained(compositor_),
1056 &video_frame,
1057 &event));
1058 event.Wait();
1059 return video_frame;
1062 void WebMediaPlayerImpl::UpdatePausedTime() {
1063 DCHECK(main_task_runner_->BelongsToCurrentThread());
1065 // pause() may be called after playback has ended and the HTMLMediaElement
1066 // requires that currentTime() == duration() after ending. We want to ensure
1067 // |paused_time_| matches currentTime() in this case or a future seek() may
1068 // incorrectly discard what it thinks is a seek to the existing time.
1069 paused_time_ =
1070 ended_ ? pipeline_.GetMediaDuration() : pipeline_.GetMediaTime();
1073 } // namespace media