Update broken references to image assets
[chromium-blink-merge.git] / media / blink / webmediaplayer_impl.cc
blobfb50bc3f5a4e850e9ac4bf8dcd5f19bc5e43cb94
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/video_frame.h"
31 #include "media/blink/texttrack_impl.h"
32 #include "media/blink/webaudiosourceprovider_impl.h"
33 #include "media/blink/webcontentdecryptionmodule_impl.h"
34 #include "media/blink/webinbandtexttrack_impl.h"
35 #include "media/blink/webmediaplayer_delegate.h"
36 #include "media/blink/webmediaplayer_util.h"
37 #include "media/blink/webmediasource_impl.h"
38 #include "media/filters/chunk_demuxer.h"
39 #include "media/filters/ffmpeg_demuxer.h"
40 #include "third_party/WebKit/public/platform/WebEncryptedMediaTypes.h"
41 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h"
42 #include "third_party/WebKit/public/platform/WebMediaPlayerEncryptedMediaClient.h"
43 #include "third_party/WebKit/public/platform/WebMediaSource.h"
44 #include "third_party/WebKit/public/platform/WebRect.h"
45 #include "third_party/WebKit/public/platform/WebSize.h"
46 #include "third_party/WebKit/public/platform/WebString.h"
47 #include "third_party/WebKit/public/platform/WebURL.h"
48 #include "third_party/WebKit/public/web/WebDocument.h"
49 #include "third_party/WebKit/public/web/WebFrame.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 WebMediaPlayerImpl::WebMediaPlayerImpl(
106 blink::WebLocalFrame* frame,
107 blink::WebMediaPlayerClient* client,
108 blink::WebMediaPlayerEncryptedMediaClient* encrypted_client,
109 base::WeakPtr<WebMediaPlayerDelegate> delegate,
110 scoped_ptr<RendererFactory> renderer_factory,
111 CdmFactory* cdm_factory,
112 const WebMediaPlayerParams& params)
113 : frame_(frame),
114 network_state_(WebMediaPlayer::NetworkStateEmpty),
115 ready_state_(WebMediaPlayer::ReadyStateHaveNothing),
116 preload_(BufferedDataSource::AUTO),
117 main_task_runner_(base::ThreadTaskRunnerHandle::Get()),
118 media_task_runner_(params.media_task_runner()),
119 worker_task_runner_(params.worker_task_runner()),
120 media_log_(params.media_log()),
121 pipeline_(media_task_runner_, media_log_.get()),
122 load_type_(LoadTypeURL),
123 opaque_(false),
124 playback_rate_(0.0),
125 paused_(true),
126 seeking_(false),
127 ended_(false),
128 pending_seek_(false),
129 should_notify_time_changed_(false),
130 client_(client),
131 encrypted_client_(encrypted_client),
132 delegate_(delegate),
133 defer_load_cb_(params.defer_load_cb()),
134 context_3d_cb_(params.context_3d_cb()),
135 supports_save_(true),
136 chunk_demuxer_(NULL),
137 // Threaded compositing isn't enabled universally yet.
138 compositor_task_runner_(
139 params.compositor_task_runner()
140 ? params.compositor_task_runner()
141 : base::MessageLoop::current()->task_runner()),
142 compositor_(new VideoFrameCompositor(
143 compositor_task_runner_,
144 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNaturalSizeChanged),
145 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnOpacityChanged))),
146 encrypted_media_support_(cdm_factory,
147 encrypted_client,
148 params.media_permission(),
149 base::Bind(&WebMediaPlayerImpl::SetCdm,
150 AsWeakPtr(),
151 base::Bind(&IgnoreCdmAttached))),
152 renderer_factory_(renderer_factory.Pass()) {
153 media_log_->AddEvent(
154 media_log_->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_CREATED));
156 if (params.initial_cdm()) {
157 SetCdm(base::Bind(&IgnoreCdmAttached),
158 ToWebContentDecryptionModuleImpl(params.initial_cdm())
159 ->GetCdmContext());
162 // TODO(xhwang): When we use an external Renderer, many methods won't work,
163 // e.g. GetCurrentFrameFromCompositor(). See http://crbug.com/434861
165 // Use the null sink if no sink was provided.
166 audio_source_provider_ = new WebAudioSourceProviderImpl(
167 params.audio_renderer_sink().get()
168 ? params.audio_renderer_sink()
169 : new NullAudioSink(media_task_runner_));
172 WebMediaPlayerImpl::~WebMediaPlayerImpl() {
173 client_->setWebLayer(NULL);
175 DCHECK(main_task_runner_->BelongsToCurrentThread());
177 if (delegate_)
178 delegate_->PlayerGone(this);
180 // Abort any pending IO so stopping the pipeline doesn't get blocked.
181 if (data_source_)
182 data_source_->Abort();
183 if (chunk_demuxer_) {
184 chunk_demuxer_->Shutdown();
185 chunk_demuxer_ = NULL;
188 renderer_factory_.reset();
190 // Make sure to kill the pipeline so there's no more media threads running.
191 // Note: stopping the pipeline might block for a long time.
192 base::WaitableEvent waiter(false, false);
193 pipeline_.Stop(
194 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&waiter)));
195 waiter.Wait();
197 compositor_task_runner_->DeleteSoon(FROM_HERE, compositor_);
199 media_log_->AddEvent(
200 media_log_->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_DESTROYED));
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 ReportMetrics(load_type, gurl,
222 GURL(frame_->document().securityOrigin().toString()));
224 // Set subresource URL for crash reporting.
225 base::debug::SetCrashKeyValue("subresource_url", gurl.spec());
227 load_type_ = load_type;
229 SetNetworkState(WebMediaPlayer::NetworkStateLoading);
230 SetReadyState(WebMediaPlayer::ReadyStateHaveNothing);
231 media_log_->AddEvent(media_log_->CreateLoadEvent(url.spec()));
233 // Media source pipelines can start immediately.
234 if (load_type == LoadTypeMediaSource) {
235 supports_save_ = false;
236 StartPipeline();
237 return;
240 // Otherwise it's a regular request which requires resolving the URL first.
241 data_source_.reset(new BufferedDataSource(
242 url,
243 static_cast<BufferedResourceLoader::CORSMode>(cors_mode),
244 main_task_runner_,
245 frame_,
246 media_log_.get(),
247 &buffered_data_source_host_,
248 base::Bind(&WebMediaPlayerImpl::NotifyDownloading, AsWeakPtr())));
249 data_source_->SetPreload(preload_);
250 data_source_->Initialize(
251 base::Bind(&WebMediaPlayerImpl::DataSourceInitialized, AsWeakPtr()));
254 void WebMediaPlayerImpl::play() {
255 DVLOG(1) << __FUNCTION__;
256 DCHECK(main_task_runner_->BelongsToCurrentThread());
258 paused_ = false;
259 pipeline_.SetPlaybackRate(playback_rate_);
260 if (data_source_)
261 data_source_->MediaIsPlaying();
263 media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PLAY));
265 if (delegate_ && playback_rate_ > 0)
266 delegate_->DidPlay(this);
269 void WebMediaPlayerImpl::pause() {
270 DVLOG(1) << __FUNCTION__;
271 DCHECK(main_task_runner_->BelongsToCurrentThread());
273 const bool was_already_paused = paused_ || playback_rate_ == 0;
274 paused_ = true;
275 pipeline_.SetPlaybackRate(0.0);
276 if (data_source_)
277 data_source_->MediaIsPaused();
278 UpdatePausedTime();
280 media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PAUSE));
282 if (!was_already_paused && delegate_)
283 delegate_->DidPause(this);
286 bool WebMediaPlayerImpl::supportsSave() const {
287 DCHECK(main_task_runner_->BelongsToCurrentThread());
288 return supports_save_;
291 void WebMediaPlayerImpl::seek(double seconds) {
292 DVLOG(1) << __FUNCTION__ << "(" << seconds << "s)";
293 DCHECK(main_task_runner_->BelongsToCurrentThread());
295 ended_ = false;
297 ReadyState old_state = ready_state_;
298 if (ready_state_ > WebMediaPlayer::ReadyStateHaveMetadata)
299 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
301 base::TimeDelta new_seek_time = base::TimeDelta::FromSecondsD(seconds);
303 if (seeking_) {
304 if (new_seek_time == seek_time_) {
305 if (chunk_demuxer_) {
306 if (!pending_seek_) {
307 // If using media source demuxer, only suppress redundant seeks if
308 // there is no pending seek. This enforces that any pending seek that
309 // results in a demuxer seek is preceded by matching
310 // CancelPendingSeek() and StartWaitingForSeek() calls.
311 return;
313 } else {
314 // Suppress all redundant seeks if unrestricted by media source demuxer
315 // API.
316 pending_seek_ = false;
317 pending_seek_time_ = base::TimeDelta();
318 return;
322 pending_seek_ = true;
323 pending_seek_time_ = new_seek_time;
324 if (chunk_demuxer_)
325 chunk_demuxer_->CancelPendingSeek(pending_seek_time_);
326 return;
329 media_log_->AddEvent(media_log_->CreateSeekEvent(seconds));
331 // Update our paused time.
332 // In paused state ignore the seek operations to current time if the loading
333 // is completed and generate OnPipelineBufferingStateChanged event to
334 // eventually fire seeking and seeked events
335 if (paused_) {
336 if (paused_time_ != new_seek_time) {
337 paused_time_ = new_seek_time;
338 } else if (old_state == ReadyStateHaveEnoughData) {
339 main_task_runner_->PostTask(
340 FROM_HERE,
341 base::Bind(&WebMediaPlayerImpl::OnPipelineBufferingStateChanged,
342 AsWeakPtr(), BUFFERING_HAVE_ENOUGH));
343 return;
347 seeking_ = true;
348 seek_time_ = new_seek_time;
350 if (chunk_demuxer_)
351 chunk_demuxer_->StartWaitingForSeek(seek_time_);
353 // Kick off the asynchronous seek!
354 pipeline_.Seek(seek_time_, BIND_TO_RENDER_LOOP1(
355 &WebMediaPlayerImpl::OnPipelineSeeked, true));
358 void WebMediaPlayerImpl::setRate(double rate) {
359 DVLOG(1) << __FUNCTION__ << "(" << rate << ")";
360 DCHECK(main_task_runner_->BelongsToCurrentThread());
362 // TODO(kylep): Remove when support for negatives is added. Also, modify the
363 // following checks so rewind uses reasonable values also.
364 if (rate < 0.0)
365 return;
367 // Limit rates to reasonable values by clamping.
368 if (rate != 0.0) {
369 if (rate < kMinRate)
370 rate = kMinRate;
371 else if (rate > kMaxRate)
372 rate = kMaxRate;
373 if (playback_rate_ == 0 && !paused_ && delegate_)
374 delegate_->DidPlay(this);
375 } else if (playback_rate_ != 0 && !paused_ && delegate_) {
376 delegate_->DidPause(this);
379 playback_rate_ = rate;
380 if (!paused_) {
381 pipeline_.SetPlaybackRate(rate);
382 if (data_source_)
383 data_source_->MediaPlaybackRateChanged(rate);
387 void WebMediaPlayerImpl::setVolume(double volume) {
388 DVLOG(1) << __FUNCTION__ << "(" << volume << ")";
389 DCHECK(main_task_runner_->BelongsToCurrentThread());
391 pipeline_.SetVolume(volume);
394 void WebMediaPlayerImpl::setSinkId(const blink::WebString& device_id,
395 WebSetSinkIdCB* web_callback) {
396 DCHECK(main_task_runner_->BelongsToCurrentThread());
397 DVLOG(1) << __FUNCTION__;
398 media::SwitchOutputDeviceCB callback =
399 media::ConvertToSwitchOutputDeviceCB(web_callback);
400 OutputDevice* output_device = audio_source_provider_->GetOutputDevice();
401 if (output_device) {
402 std::string device_id_str(device_id.utf8());
403 GURL security_origin(frame_->securityOrigin().toString().utf8());
404 output_device->SwitchOutputDevice(device_id_str, security_origin, callback);
405 } else {
406 callback.Run(SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_SUPPORTED);
410 #define STATIC_ASSERT_MATCHING_ENUM(webkit_name, chromium_name) \
411 static_assert(static_cast<int>(WebMediaPlayer::webkit_name) == \
412 static_cast<int>(BufferedDataSource::chromium_name), \
413 "mismatching enum values: " #webkit_name)
414 STATIC_ASSERT_MATCHING_ENUM(PreloadNone, NONE);
415 STATIC_ASSERT_MATCHING_ENUM(PreloadMetaData, METADATA);
416 STATIC_ASSERT_MATCHING_ENUM(PreloadAuto, AUTO);
417 #undef STATIC_ASSERT_MATCHING_ENUM
419 void WebMediaPlayerImpl::setPreload(WebMediaPlayer::Preload preload) {
420 DVLOG(1) << __FUNCTION__ << "(" << preload << ")";
421 DCHECK(main_task_runner_->BelongsToCurrentThread());
423 preload_ = static_cast<BufferedDataSource::Preload>(preload);
424 if (data_source_)
425 data_source_->SetPreload(preload_);
428 bool WebMediaPlayerImpl::hasVideo() const {
429 DCHECK(main_task_runner_->BelongsToCurrentThread());
431 return pipeline_metadata_.has_video;
434 bool WebMediaPlayerImpl::hasAudio() const {
435 DCHECK(main_task_runner_->BelongsToCurrentThread());
437 return pipeline_metadata_.has_audio;
440 blink::WebSize WebMediaPlayerImpl::naturalSize() const {
441 DCHECK(main_task_runner_->BelongsToCurrentThread());
443 return blink::WebSize(pipeline_metadata_.natural_size);
446 bool WebMediaPlayerImpl::paused() const {
447 DCHECK(main_task_runner_->BelongsToCurrentThread());
449 return pipeline_.GetPlaybackRate() == 0.0f;
452 bool WebMediaPlayerImpl::seeking() const {
453 DCHECK(main_task_runner_->BelongsToCurrentThread());
455 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
456 return false;
458 return seeking_;
461 double WebMediaPlayerImpl::duration() const {
462 DCHECK(main_task_runner_->BelongsToCurrentThread());
464 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
465 return std::numeric_limits<double>::quiet_NaN();
467 return GetPipelineDuration();
470 double WebMediaPlayerImpl::timelineOffset() const {
471 DCHECK(main_task_runner_->BelongsToCurrentThread());
473 if (pipeline_metadata_.timeline_offset.is_null())
474 return std::numeric_limits<double>::quiet_NaN();
476 return pipeline_metadata_.timeline_offset.ToJsTime();
479 double WebMediaPlayerImpl::currentTime() const {
480 DCHECK(main_task_runner_->BelongsToCurrentThread());
481 DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
483 // TODO(scherkus): Replace with an explicit ended signal to HTMLMediaElement,
484 // see http://crbug.com/409280
485 if (ended_)
486 return duration();
488 // We know the current seek time better than pipeline: pipeline may processing
489 // an earlier seek before a pending seek has been started, or it might not yet
490 // have the current seek time returnable via GetMediaTime().
491 if (seeking()) {
492 return pending_seek_ ? pending_seek_time_.InSecondsF()
493 : seek_time_.InSecondsF();
496 return (paused_ ? paused_time_ : pipeline_.GetMediaTime()).InSecondsF();
499 WebMediaPlayer::NetworkState WebMediaPlayerImpl::networkState() const {
500 DCHECK(main_task_runner_->BelongsToCurrentThread());
501 return network_state_;
504 WebMediaPlayer::ReadyState WebMediaPlayerImpl::readyState() const {
505 DCHECK(main_task_runner_->BelongsToCurrentThread());
506 return ready_state_;
509 blink::WebTimeRanges WebMediaPlayerImpl::buffered() const {
510 DCHECK(main_task_runner_->BelongsToCurrentThread());
512 Ranges<base::TimeDelta> buffered_time_ranges =
513 pipeline_.GetBufferedTimeRanges();
515 const base::TimeDelta duration = pipeline_.GetMediaDuration();
516 if (duration != kInfiniteDuration()) {
517 buffered_data_source_host_.AddBufferedTimeRanges(
518 &buffered_time_ranges, duration);
520 return ConvertToWebTimeRanges(buffered_time_ranges);
523 blink::WebTimeRanges WebMediaPlayerImpl::seekable() const {
524 DCHECK(main_task_runner_->BelongsToCurrentThread());
526 if (ready_state_ < WebMediaPlayer::ReadyStateHaveMetadata)
527 return blink::WebTimeRanges();
529 const double seekable_end = duration();
531 // Allow a special exception for seeks to zero for streaming sources with a
532 // finite duration; this allows looping to work.
533 const bool allow_seek_to_zero = data_source_ && data_source_->IsStreaming() &&
534 std::isfinite(seekable_end);
536 // TODO(dalecurtis): Technically this allows seeking on media which return an
537 // infinite duration so long as DataSource::IsStreaming() is false. While not
538 // expected, disabling this breaks semi-live players, http://crbug.com/427412.
539 const blink::WebTimeRange seekable_range(
540 0.0, allow_seek_to_zero ? 0.0 : seekable_end);
541 return blink::WebTimeRanges(&seekable_range, 1);
544 bool WebMediaPlayerImpl::didLoadingProgress() {
545 DCHECK(main_task_runner_->BelongsToCurrentThread());
546 bool pipeline_progress = pipeline_.DidLoadingProgress();
547 bool data_progress = buffered_data_source_host_.DidLoadingProgress();
548 return pipeline_progress || data_progress;
551 void WebMediaPlayerImpl::paint(blink::WebCanvas* canvas,
552 const blink::WebRect& rect,
553 unsigned char alpha,
554 SkXfermode::Mode mode) {
555 DCHECK(main_task_runner_->BelongsToCurrentThread());
556 TRACE_EVENT0("media", "WebMediaPlayerImpl:paint");
558 // TODO(scherkus): Clarify paint() API contract to better understand when and
559 // why it's being called. For example, today paint() is called when:
560 // - We haven't reached HAVE_CURRENT_DATA and need to paint black
561 // - We're painting to a canvas
562 // See http://crbug.com/341225 http://crbug.com/342621 for details.
563 scoped_refptr<VideoFrame> video_frame = GetCurrentFrameFromCompositor();
565 gfx::Rect gfx_rect(rect);
566 Context3D context_3d;
567 if (video_frame.get() && video_frame->HasTextures()) {
568 if (!context_3d_cb_.is_null())
569 context_3d = context_3d_cb_.Run();
570 // GPU Process crashed.
571 if (!context_3d.gl)
572 return;
574 skcanvas_video_renderer_.Paint(video_frame, canvas, gfx_rect, alpha, mode,
575 pipeline_metadata_.video_rotation, context_3d);
578 bool WebMediaPlayerImpl::hasSingleSecurityOrigin() const {
579 if (data_source_)
580 return data_source_->HasSingleOrigin();
581 return true;
584 bool WebMediaPlayerImpl::didPassCORSAccessCheck() const {
585 if (data_source_)
586 return data_source_->DidPassCORSAccessCheck();
587 return false;
590 double WebMediaPlayerImpl::mediaTimeForTimeValue(double timeValue) const {
591 return base::TimeDelta::FromSecondsD(timeValue).InSecondsF();
594 unsigned WebMediaPlayerImpl::decodedFrameCount() const {
595 DCHECK(main_task_runner_->BelongsToCurrentThread());
597 PipelineStatistics stats = pipeline_.GetStatistics();
598 return stats.video_frames_decoded;
601 unsigned WebMediaPlayerImpl::droppedFrameCount() const {
602 DCHECK(main_task_runner_->BelongsToCurrentThread());
604 PipelineStatistics stats = pipeline_.GetStatistics();
605 return stats.video_frames_dropped;
608 unsigned WebMediaPlayerImpl::audioDecodedByteCount() const {
609 DCHECK(main_task_runner_->BelongsToCurrentThread());
611 PipelineStatistics stats = pipeline_.GetStatistics();
612 return stats.audio_bytes_decoded;
615 unsigned WebMediaPlayerImpl::videoDecodedByteCount() const {
616 DCHECK(main_task_runner_->BelongsToCurrentThread());
618 PipelineStatistics stats = pipeline_.GetStatistics();
619 return stats.video_bytes_decoded;
622 bool WebMediaPlayerImpl::copyVideoTextureToPlatformTexture(
623 blink::WebGraphicsContext3D* web_graphics_context,
624 unsigned int texture,
625 unsigned int internal_format,
626 unsigned int type,
627 bool premultiply_alpha,
628 bool flip_y) {
629 TRACE_EVENT0("media", "WebMediaPlayerImpl:copyVideoTextureToPlatformTexture");
631 scoped_refptr<VideoFrame> video_frame = GetCurrentFrameFromCompositor();
633 if (!video_frame.get() || !video_frame->HasTextures() ||
634 media::VideoFrame::NumPlanes(video_frame->format()) != 1) {
635 return false;
638 // TODO(dshwang): need more elegant way to convert WebGraphicsContext3D to
639 // GLES2Interface.
640 gpu::gles2::GLES2Interface* gl =
641 static_cast<gpu_blink::WebGraphicsContext3DImpl*>(web_graphics_context)
642 ->GetGLInterface();
643 SkCanvasVideoRenderer::CopyVideoFrameSingleTextureToGLTexture(
644 gl, video_frame.get(), texture, internal_format, type, premultiply_alpha,
645 flip_y);
646 return true;
649 WebMediaPlayer::MediaKeyException
650 WebMediaPlayerImpl::generateKeyRequest(const WebString& key_system,
651 const unsigned char* init_data,
652 unsigned init_data_length) {
653 DCHECK(main_task_runner_->BelongsToCurrentThread());
655 return encrypted_media_support_.GenerateKeyRequest(
656 frame_, key_system, init_data, init_data_length);
659 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::addKey(
660 const WebString& key_system,
661 const unsigned char* key,
662 unsigned key_length,
663 const unsigned char* init_data,
664 unsigned init_data_length,
665 const WebString& session_id) {
666 DCHECK(main_task_runner_->BelongsToCurrentThread());
668 return encrypted_media_support_.AddKey(
669 key_system, key, key_length, init_data, init_data_length, session_id);
672 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::cancelKeyRequest(
673 const WebString& key_system,
674 const WebString& session_id) {
675 DCHECK(main_task_runner_->BelongsToCurrentThread());
677 return encrypted_media_support_.CancelKeyRequest(key_system, session_id);
680 void WebMediaPlayerImpl::setContentDecryptionModule(
681 blink::WebContentDecryptionModule* cdm,
682 blink::WebContentDecryptionModuleResult result) {
683 DCHECK(main_task_runner_->BelongsToCurrentThread());
685 // Once the CDM is set it can't be cleared as there may be frames being
686 // decrypted on other threads. So fail this request.
687 // http://crbug.com/462365#c7.
688 if (!cdm) {
689 result.completeWithError(
690 blink::WebContentDecryptionModuleExceptionInvalidStateError, 0,
691 "The existing MediaKeys object cannot be removed at this time.");
692 return;
695 // Although unlikely, it is possible that multiple calls happen
696 // simultaneously, so fail this call if there is already one pending.
697 if (set_cdm_result_) {
698 result.completeWithError(
699 blink::WebContentDecryptionModuleExceptionInvalidStateError, 0,
700 "Unable to set MediaKeys object at this time.");
701 return;
704 // Create a local copy of |result| to avoid problems with the callback
705 // getting passed to the media thread and causing |result| to be destructed
706 // on the wrong thread in some failure conditions.
707 set_cdm_result_.reset(new blink::WebContentDecryptionModuleResult(result));
709 SetCdm(BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnCdmAttached),
710 ToWebContentDecryptionModuleImpl(cdm)->GetCdmContext());
713 void WebMediaPlayerImpl::OnEncryptedMediaInitData(
714 EmeInitDataType init_data_type,
715 const std::vector<uint8>& init_data) {
716 DCHECK(init_data_type != EmeInitDataType::UNKNOWN);
718 // Do not fire "encrypted" event if encrypted media is not enabled.
719 // TODO(xhwang): Handle this in |client_|.
720 if (!blink::WebRuntimeFeatures::isPrefixedEncryptedMediaEnabled() &&
721 !blink::WebRuntimeFeatures::isEncryptedMediaEnabled()) {
722 return;
725 // TODO(xhwang): Update this UMA name.
726 UMA_HISTOGRAM_COUNTS("Media.EME.NeedKey", 1);
728 encrypted_media_support_.SetInitDataType(init_data_type);
730 encrypted_client_->encrypted(
731 ConvertToWebInitDataType(init_data_type), vector_as_array(&init_data),
732 base::saturated_cast<unsigned int>(init_data.size()));
735 void WebMediaPlayerImpl::OnWaitingForDecryptionKey() {
736 encrypted_client_->didBlockPlaybackWaitingForKey();
738 // TODO(jrummell): didResumePlaybackBlockedForKey() should only be called
739 // when a key has been successfully added (e.g. OnSessionKeysChange() with
740 // |has_additional_usable_key| = true). http://crbug.com/461903
741 encrypted_client_->didResumePlaybackBlockedForKey();
744 void WebMediaPlayerImpl::SetCdm(const CdmAttachedCB& cdm_attached_cb,
745 CdmContext* cdm_context) {
746 // If CDM initialization succeeded, tell the pipeline about it.
747 if (cdm_context)
748 pipeline_.SetCdm(cdm_context, cdm_attached_cb);
751 void WebMediaPlayerImpl::OnCdmAttached(bool success) {
752 if (success) {
753 set_cdm_result_->complete();
754 set_cdm_result_.reset();
755 return;
758 set_cdm_result_->completeWithError(
759 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
760 "Unable to set MediaKeys object");
761 set_cdm_result_.reset();
764 void WebMediaPlayerImpl::OnPipelineSeeked(bool time_changed,
765 PipelineStatus status) {
766 DVLOG(1) << __FUNCTION__ << "(" << time_changed << ", " << status << ")";
767 DCHECK(main_task_runner_->BelongsToCurrentThread());
768 seeking_ = false;
769 seek_time_ = base::TimeDelta();
770 if (pending_seek_) {
771 double pending_seek_seconds = pending_seek_time_.InSecondsF();
772 pending_seek_ = false;
773 pending_seek_time_ = base::TimeDelta();
774 seek(pending_seek_seconds);
775 return;
778 if (status != PIPELINE_OK) {
779 OnPipelineError(status);
780 return;
783 // Update our paused time.
784 if (paused_)
785 UpdatePausedTime();
787 should_notify_time_changed_ = time_changed;
790 void WebMediaPlayerImpl::OnPipelineEnded() {
791 DVLOG(1) << __FUNCTION__;
792 DCHECK(main_task_runner_->BelongsToCurrentThread());
794 // Ignore state changes until we've completed all outstanding seeks.
795 if (seeking_ || pending_seek_)
796 return;
798 ended_ = true;
799 client_->timeChanged();
802 void WebMediaPlayerImpl::OnPipelineError(PipelineStatus error) {
803 DCHECK(main_task_runner_->BelongsToCurrentThread());
804 DCHECK_NE(error, PIPELINE_OK);
806 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) {
807 // Any error that occurs before reaching ReadyStateHaveMetadata should
808 // be considered a format error.
809 SetNetworkState(WebMediaPlayer::NetworkStateFormatError);
810 return;
813 SetNetworkState(PipelineErrorToNetworkState(error));
816 void WebMediaPlayerImpl::OnPipelineMetadata(
817 PipelineMetadata metadata) {
818 DVLOG(1) << __FUNCTION__;
820 pipeline_metadata_ = metadata;
822 UMA_HISTOGRAM_ENUMERATION("Media.VideoRotation", metadata.video_rotation,
823 VIDEO_ROTATION_MAX + 1);
824 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
826 if (hasVideo()) {
827 DCHECK(!video_weblayer_);
828 scoped_refptr<cc::VideoLayer> layer =
829 cc::VideoLayer::Create(cc_blink::WebLayerImpl::LayerSettings(),
830 compositor_, pipeline_metadata_.video_rotation);
832 if (pipeline_metadata_.video_rotation == VIDEO_ROTATION_90 ||
833 pipeline_metadata_.video_rotation == VIDEO_ROTATION_270) {
834 gfx::Size size = pipeline_metadata_.natural_size;
835 pipeline_metadata_.natural_size = gfx::Size(size.height(), size.width());
838 video_weblayer_.reset(new cc_blink::WebLayerImpl(layer));
839 video_weblayer_->setOpaque(opaque_);
840 client_->setWebLayer(video_weblayer_.get());
844 void WebMediaPlayerImpl::OnPipelineBufferingStateChanged(
845 BufferingState buffering_state) {
846 DVLOG(1) << __FUNCTION__ << "(" << buffering_state << ")";
848 // Ignore buffering state changes until we've completed all outstanding seeks.
849 if (seeking_ || pending_seek_)
850 return;
852 // TODO(scherkus): Handle other buffering states when Pipeline starts using
853 // them and translate them ready state changes http://crbug.com/144683
854 DCHECK_EQ(buffering_state, BUFFERING_HAVE_ENOUGH);
855 SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData);
857 // Let the DataSource know we have enough data. It may use this information to
858 // release unused network connections.
859 if (data_source_)
860 data_source_->OnBufferingHaveEnough();
862 // Blink expects a timeChanged() in response to a seek().
863 if (should_notify_time_changed_)
864 client_->timeChanged();
867 void WebMediaPlayerImpl::OnDemuxerOpened() {
868 DCHECK(main_task_runner_->BelongsToCurrentThread());
869 client_->mediaSourceOpened(
870 new WebMediaSourceImpl(chunk_demuxer_, media_log_));
873 void WebMediaPlayerImpl::OnAddTextTrack(
874 const TextTrackConfig& config,
875 const AddTextTrackDoneCB& done_cb) {
876 DCHECK(main_task_runner_->BelongsToCurrentThread());
878 const WebInbandTextTrackImpl::Kind web_kind =
879 static_cast<WebInbandTextTrackImpl::Kind>(config.kind());
880 const blink::WebString web_label =
881 blink::WebString::fromUTF8(config.label());
882 const blink::WebString web_language =
883 blink::WebString::fromUTF8(config.language());
884 const blink::WebString web_id =
885 blink::WebString::fromUTF8(config.id());
887 scoped_ptr<WebInbandTextTrackImpl> web_inband_text_track(
888 new WebInbandTextTrackImpl(web_kind, web_label, web_language, web_id));
890 scoped_ptr<TextTrack> text_track(new TextTrackImpl(
891 main_task_runner_, client_, web_inband_text_track.Pass()));
893 done_cb.Run(text_track.Pass());
896 void WebMediaPlayerImpl::DataSourceInitialized(bool success) {
897 DCHECK(main_task_runner_->BelongsToCurrentThread());
899 if (!success) {
900 SetNetworkState(WebMediaPlayer::NetworkStateFormatError);
901 return;
904 StartPipeline();
907 void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading) {
908 if (!is_downloading && network_state_ == WebMediaPlayer::NetworkStateLoading)
909 SetNetworkState(WebMediaPlayer::NetworkStateIdle);
910 else if (is_downloading && network_state_ == WebMediaPlayer::NetworkStateIdle)
911 SetNetworkState(WebMediaPlayer::NetworkStateLoading);
912 media_log_->AddEvent(
913 media_log_->CreateBooleanEvent(
914 MediaLogEvent::NETWORK_ACTIVITY_SET,
915 "is_downloading_data", is_downloading));
918 void WebMediaPlayerImpl::StartPipeline() {
919 DCHECK(main_task_runner_->BelongsToCurrentThread());
921 Demuxer::EncryptedMediaInitDataCB encrypted_media_init_data_cb =
922 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnEncryptedMediaInitData);
924 // Figure out which demuxer to use.
925 if (load_type_ != LoadTypeMediaSource) {
926 DCHECK(!chunk_demuxer_);
927 DCHECK(data_source_);
929 demuxer_.reset(new FFmpegDemuxer(media_task_runner_, data_source_.get(),
930 encrypted_media_init_data_cb, media_log_));
931 } else {
932 DCHECK(!chunk_demuxer_);
933 DCHECK(!data_source_);
935 chunk_demuxer_ = new ChunkDemuxer(
936 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDemuxerOpened),
937 encrypted_media_init_data_cb, media_log_, true);
938 demuxer_.reset(chunk_demuxer_);
941 // ... and we're ready to go!
942 seeking_ = true;
944 pipeline_.Start(
945 demuxer_.get(), renderer_factory_->CreateRenderer(
946 media_task_runner_, worker_task_runner_,
947 audio_source_provider_.get(), compositor_),
948 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineEnded),
949 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineError),
950 BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnPipelineSeeked, false),
951 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineMetadata),
952 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineBufferingStateChanged),
953 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDurationChanged),
954 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnAddTextTrack),
955 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnWaitingForDecryptionKey));
958 void WebMediaPlayerImpl::SetNetworkState(WebMediaPlayer::NetworkState state) {
959 DVLOG(1) << __FUNCTION__ << "(" << state << ")";
960 DCHECK(main_task_runner_->BelongsToCurrentThread());
961 network_state_ = state;
962 // Always notify to ensure client has the latest value.
963 client_->networkStateChanged();
966 void WebMediaPlayerImpl::SetReadyState(WebMediaPlayer::ReadyState state) {
967 DVLOG(1) << __FUNCTION__ << "(" << state << ")";
968 DCHECK(main_task_runner_->BelongsToCurrentThread());
970 if (state == WebMediaPlayer::ReadyStateHaveEnoughData && data_source_ &&
971 data_source_->assume_fully_buffered() &&
972 network_state_ == WebMediaPlayer::NetworkStateLoading)
973 SetNetworkState(WebMediaPlayer::NetworkStateLoaded);
975 ready_state_ = state;
976 // Always notify to ensure client has the latest value.
977 client_->readyStateChanged();
980 blink::WebAudioSourceProvider* WebMediaPlayerImpl::audioSourceProvider() {
981 return audio_source_provider_.get();
984 double WebMediaPlayerImpl::GetPipelineDuration() const {
985 base::TimeDelta duration = pipeline_.GetMediaDuration();
987 // Return positive infinity if the resource is unbounded.
988 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
989 if (duration == kInfiniteDuration())
990 return std::numeric_limits<double>::infinity();
992 return duration.InSecondsF();
995 void WebMediaPlayerImpl::OnDurationChanged() {
996 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
997 return;
999 client_->durationChanged();
1002 void WebMediaPlayerImpl::OnNaturalSizeChanged(gfx::Size size) {
1003 DCHECK(main_task_runner_->BelongsToCurrentThread());
1004 DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
1005 TRACE_EVENT0("media", "WebMediaPlayerImpl::OnNaturalSizeChanged");
1007 media_log_->AddEvent(
1008 media_log_->CreateVideoSizeSetEvent(size.width(), size.height()));
1009 pipeline_metadata_.natural_size = size;
1011 client_->sizeChanged();
1014 void WebMediaPlayerImpl::OnOpacityChanged(bool opaque) {
1015 DCHECK(main_task_runner_->BelongsToCurrentThread());
1016 DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
1018 opaque_ = opaque;
1019 if (video_weblayer_)
1020 video_weblayer_->setOpaque(opaque_);
1023 static void GetCurrentFrameAndSignal(
1024 VideoFrameCompositor* compositor,
1025 scoped_refptr<VideoFrame>* video_frame_out,
1026 base::WaitableEvent* event) {
1027 TRACE_EVENT0("media", "GetCurrentFrameAndSignal");
1028 *video_frame_out = compositor->GetCurrentFrameAndUpdateIfStale();
1029 event->Signal();
1032 scoped_refptr<VideoFrame>
1033 WebMediaPlayerImpl::GetCurrentFrameFromCompositor() {
1034 TRACE_EVENT0("media", "WebMediaPlayerImpl::GetCurrentFrameFromCompositor");
1035 if (compositor_task_runner_->BelongsToCurrentThread())
1036 return compositor_->GetCurrentFrameAndUpdateIfStale();
1038 // Use a posted task and waitable event instead of a lock otherwise
1039 // WebGL/Canvas can see different content than what the compositor is seeing.
1040 scoped_refptr<VideoFrame> video_frame;
1041 base::WaitableEvent event(false, false);
1042 compositor_task_runner_->PostTask(FROM_HERE,
1043 base::Bind(&GetCurrentFrameAndSignal,
1044 base::Unretained(compositor_),
1045 &video_frame,
1046 &event));
1047 event.Wait();
1048 return video_frame;
1051 void WebMediaPlayerImpl::UpdatePausedTime() {
1052 DCHECK(main_task_runner_->BelongsToCurrentThread());
1054 // pause() may be called after playback has ended and the HTMLMediaElement
1055 // requires that currentTime() == duration() after ending. We want to ensure
1056 // |paused_time_| matches currentTime() in this case or a future seek() may
1057 // incorrectly discard what it thinks is a seek to the existing time.
1058 paused_time_ =
1059 ended_ ? pipeline_.GetMediaDuration() : pipeline_.GetMediaTime();
1062 } // namespace media