Remove 'RemoveTrailingSeparators' function from SimpleMenuModel
[chromium-blink-merge.git] / media / blink / webmediaplayer_impl.cc
blobab62772c2b205e9920bca514ee9a677d865f4d58
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/WebEncryptedMediaTypes.h"
46 #include "third_party/WebKit/public/platform/WebMediaSource.h"
47 #include "third_party/WebKit/public/platform/WebRect.h"
48 #include "third_party/WebKit/public/platform/WebSize.h"
49 #include "third_party/WebKit/public/platform/WebString.h"
50 #include "third_party/WebKit/public/platform/WebURL.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 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::MessageLoopProxy::current()),
118 media_task_runner_(params.media_task_runner()),
119 media_log_(params.media_log()),
120 pipeline_(media_task_runner_, media_log_.get()),
121 load_type_(LoadTypeURL),
122 opaque_(false),
123 paused_(true),
124 seeking_(false),
125 playback_rate_(0.0f),
126 ended_(false),
127 pending_seek_(false),
128 pending_seek_seconds_(0.0f),
129 should_notify_time_changed_(false),
130 client_(client),
131 delegate_(delegate),
132 defer_load_cb_(params.defer_load_cb()),
133 context_3d_cb_(params.context_3d_cb()),
134 supports_save_(true),
135 chunk_demuxer_(NULL),
136 compositor_task_runner_(params.compositor_task_runner()),
137 compositor_(new VideoFrameCompositor(
138 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNaturalSizeChanged),
139 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnOpacityChanged))),
140 encrypted_media_support_(cdm_factory,
141 client,
142 params.media_permission(),
143 base::Bind(&WebMediaPlayerImpl::SetCdm,
144 AsWeakPtr(),
145 base::Bind(&IgnoreCdmAttached))),
146 renderer_factory_(renderer_factory.Pass()) {
147 // Threaded compositing isn't enabled universally yet.
148 if (!compositor_task_runner_.get())
149 compositor_task_runner_ = base::MessageLoopProxy::current();
151 media_log_->AddEvent(
152 media_log_->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_CREATED));
154 if (params.initial_cdm()) {
155 SetCdm(base::Bind(&IgnoreCdmAttached),
156 ToWebContentDecryptionModuleImpl(params.initial_cdm())
157 ->GetCdmContext());
160 // TODO(xhwang): When we use an external Renderer, many methods won't work,
161 // e.g. GetCurrentFrameFromCompositor(). See http://crbug.com/434861
163 // Use the null sink if no sink was provided.
164 audio_source_provider_ = new WebAudioSourceProviderImpl(
165 params.audio_renderer_sink().get()
166 ? params.audio_renderer_sink()
167 : new NullAudioSink(media_task_runner_));
170 WebMediaPlayerImpl::~WebMediaPlayerImpl() {
171 client_->setWebLayer(NULL);
173 DCHECK(main_task_runner_->BelongsToCurrentThread());
174 media_log_->AddEvent(
175 media_log_->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_DESTROYED));
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_);
200 void WebMediaPlayerImpl::load(LoadType load_type, const blink::WebURL& url,
201 CORSMode cors_mode) {
202 DVLOG(1) << __FUNCTION__ << "(" << load_type << ", " << url << ", "
203 << cors_mode << ")";
204 if (!defer_load_cb_.is_null()) {
205 defer_load_cb_.Run(base::Bind(
206 &WebMediaPlayerImpl::DoLoad, AsWeakPtr(), load_type, url, cors_mode));
207 return;
209 DoLoad(load_type, url, cors_mode);
212 void WebMediaPlayerImpl::DoLoad(LoadType load_type,
213 const blink::WebURL& url,
214 CORSMode cors_mode) {
215 DCHECK(main_task_runner_->BelongsToCurrentThread());
217 GURL gurl(url);
218 ReportMediaSchemeUma(gurl);
220 // Set subresource URL for crash reporting.
221 base::debug::SetCrashKeyValue("subresource_url", gurl.spec());
223 load_type_ = load_type;
225 SetNetworkState(WebMediaPlayer::NetworkStateLoading);
226 SetReadyState(WebMediaPlayer::ReadyStateHaveNothing);
227 media_log_->AddEvent(media_log_->CreateLoadEvent(url.spec()));
229 // Media source pipelines can start immediately.
230 if (load_type == LoadTypeMediaSource) {
231 supports_save_ = false;
232 StartPipeline();
233 return;
236 // Otherwise it's a regular request which requires resolving the URL first.
237 data_source_.reset(new BufferedDataSource(
238 url,
239 static_cast<BufferedResourceLoader::CORSMode>(cors_mode),
240 main_task_runner_,
241 frame_,
242 media_log_.get(),
243 &buffered_data_source_host_,
244 base::Bind(&WebMediaPlayerImpl::NotifyDownloading, AsWeakPtr())));
245 data_source_->SetPreload(preload_);
246 data_source_->Initialize(
247 base::Bind(&WebMediaPlayerImpl::DataSourceInitialized, AsWeakPtr()));
250 void WebMediaPlayerImpl::play() {
251 DVLOG(1) << __FUNCTION__;
252 DCHECK(main_task_runner_->BelongsToCurrentThread());
254 paused_ = false;
255 pipeline_.SetPlaybackRate(playback_rate_);
256 if (data_source_)
257 data_source_->MediaIsPlaying();
259 media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PLAY));
261 if (delegate_ && playback_rate_ > 0)
262 delegate_->DidPlay(this);
265 void WebMediaPlayerImpl::pause() {
266 DVLOG(1) << __FUNCTION__;
267 DCHECK(main_task_runner_->BelongsToCurrentThread());
269 const bool was_already_paused = paused_ || playback_rate_ == 0;
270 paused_ = true;
271 pipeline_.SetPlaybackRate(0.0f);
272 if (data_source_)
273 data_source_->MediaIsPaused();
274 UpdatePausedTime();
276 media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PAUSE));
278 if (!was_already_paused && delegate_)
279 delegate_->DidPause(this);
282 bool WebMediaPlayerImpl::supportsSave() const {
283 DCHECK(main_task_runner_->BelongsToCurrentThread());
284 return supports_save_;
287 void WebMediaPlayerImpl::seek(double seconds) {
288 DVLOG(1) << __FUNCTION__ << "(" << seconds << "s)";
289 DCHECK(main_task_runner_->BelongsToCurrentThread());
291 ended_ = false;
293 ReadyState old_state = ready_state_;
294 if (ready_state_ > WebMediaPlayer::ReadyStateHaveMetadata)
295 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
297 base::TimeDelta seek_time = ConvertSecondsToTimestamp(seconds);
299 if (seeking_) {
300 pending_seek_ = true;
301 pending_seek_seconds_ = seconds;
302 if (chunk_demuxer_)
303 chunk_demuxer_->CancelPendingSeek(seek_time);
304 return;
307 media_log_->AddEvent(media_log_->CreateSeekEvent(seconds));
309 // Update our paused time.
310 // In paused state ignore the seek operations to current time if the loading
311 // is completed and generate OnPipelineBufferingStateChanged event to
312 // eventually fire seeking and seeked events
313 if (paused_) {
314 if (paused_time_ != seek_time) {
315 paused_time_ = seek_time;
316 } else if (old_state == ReadyStateHaveEnoughData) {
317 main_task_runner_->PostTask(
318 FROM_HERE,
319 base::Bind(&WebMediaPlayerImpl::OnPipelineBufferingStateChanged,
320 AsWeakPtr(), BUFFERING_HAVE_ENOUGH));
321 return;
325 seeking_ = true;
327 if (chunk_demuxer_)
328 chunk_demuxer_->StartWaitingForSeek(seek_time);
330 // Kick off the asynchronous seek!
331 pipeline_.Seek(
332 seek_time,
333 BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnPipelineSeeked, true));
336 void WebMediaPlayerImpl::setRate(double rate) {
337 DVLOG(1) << __FUNCTION__ << "(" << rate << ")";
338 DCHECK(main_task_runner_->BelongsToCurrentThread());
340 // TODO(kylep): Remove when support for negatives is added. Also, modify the
341 // following checks so rewind uses reasonable values also.
342 if (rate < 0.0)
343 return;
345 // Limit rates to reasonable values by clamping.
346 if (rate != 0.0) {
347 if (rate < kMinRate)
348 rate = kMinRate;
349 else if (rate > kMaxRate)
350 rate = kMaxRate;
351 if (playback_rate_ == 0 && !paused_ && delegate_)
352 delegate_->DidPlay(this);
353 } else if (playback_rate_ != 0 && !paused_ && delegate_) {
354 delegate_->DidPause(this);
357 playback_rate_ = rate;
358 if (!paused_) {
359 pipeline_.SetPlaybackRate(rate);
360 if (data_source_)
361 data_source_->MediaPlaybackRateChanged(rate);
365 void WebMediaPlayerImpl::setVolume(double volume) {
366 DVLOG(1) << __FUNCTION__ << "(" << volume << ")";
367 DCHECK(main_task_runner_->BelongsToCurrentThread());
369 pipeline_.SetVolume(volume);
372 #define STATIC_ASSERT_MATCHING_ENUM(webkit_name, chromium_name) \
373 static_assert(static_cast<int>(WebMediaPlayer::webkit_name) == \
374 static_cast<int>(BufferedDataSource::chromium_name), \
375 "mismatching enum values: " #webkit_name)
376 STATIC_ASSERT_MATCHING_ENUM(PreloadNone, NONE);
377 STATIC_ASSERT_MATCHING_ENUM(PreloadMetaData, METADATA);
378 STATIC_ASSERT_MATCHING_ENUM(PreloadAuto, AUTO);
379 #undef STATIC_ASSERT_MATCHING_ENUM
381 void WebMediaPlayerImpl::setPreload(WebMediaPlayer::Preload preload) {
382 DVLOG(1) << __FUNCTION__ << "(" << preload << ")";
383 DCHECK(main_task_runner_->BelongsToCurrentThread());
385 preload_ = static_cast<BufferedDataSource::Preload>(preload);
386 if (data_source_)
387 data_source_->SetPreload(preload_);
390 bool WebMediaPlayerImpl::hasVideo() const {
391 DCHECK(main_task_runner_->BelongsToCurrentThread());
393 return pipeline_metadata_.has_video;
396 bool WebMediaPlayerImpl::hasAudio() const {
397 DCHECK(main_task_runner_->BelongsToCurrentThread());
399 return pipeline_metadata_.has_audio;
402 blink::WebSize WebMediaPlayerImpl::naturalSize() const {
403 DCHECK(main_task_runner_->BelongsToCurrentThread());
405 return blink::WebSize(pipeline_metadata_.natural_size);
408 bool WebMediaPlayerImpl::paused() const {
409 DCHECK(main_task_runner_->BelongsToCurrentThread());
411 return pipeline_.GetPlaybackRate() == 0.0f;
414 bool WebMediaPlayerImpl::seeking() const {
415 DCHECK(main_task_runner_->BelongsToCurrentThread());
417 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
418 return false;
420 return seeking_;
423 double WebMediaPlayerImpl::duration() const {
424 DCHECK(main_task_runner_->BelongsToCurrentThread());
426 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
427 return std::numeric_limits<double>::quiet_NaN();
429 return GetPipelineDuration();
432 double WebMediaPlayerImpl::timelineOffset() const {
433 DCHECK(main_task_runner_->BelongsToCurrentThread());
435 if (pipeline_metadata_.timeline_offset.is_null())
436 return std::numeric_limits<double>::quiet_NaN();
438 return pipeline_metadata_.timeline_offset.ToJsTime();
441 double WebMediaPlayerImpl::currentTime() const {
442 DCHECK(main_task_runner_->BelongsToCurrentThread());
443 DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
445 // TODO(scherkus): Replace with an explicit ended signal to HTMLMediaElement,
446 // see http://crbug.com/409280
447 if (ended_)
448 return duration();
450 return (paused_ ? paused_time_ : pipeline_.GetMediaTime()).InSecondsF();
453 WebMediaPlayer::NetworkState WebMediaPlayerImpl::networkState() const {
454 DCHECK(main_task_runner_->BelongsToCurrentThread());
455 return network_state_;
458 WebMediaPlayer::ReadyState WebMediaPlayerImpl::readyState() const {
459 DCHECK(main_task_runner_->BelongsToCurrentThread());
460 return ready_state_;
463 blink::WebTimeRanges WebMediaPlayerImpl::buffered() const {
464 DCHECK(main_task_runner_->BelongsToCurrentThread());
466 Ranges<base::TimeDelta> buffered_time_ranges =
467 pipeline_.GetBufferedTimeRanges();
469 const base::TimeDelta duration = pipeline_.GetMediaDuration();
470 if (duration != kInfiniteDuration()) {
471 buffered_data_source_host_.AddBufferedTimeRanges(
472 &buffered_time_ranges, duration);
474 return ConvertToWebTimeRanges(buffered_time_ranges);
477 blink::WebTimeRanges WebMediaPlayerImpl::seekable() const {
478 DCHECK(main_task_runner_->BelongsToCurrentThread());
480 if (ready_state_ < WebMediaPlayer::ReadyStateHaveMetadata)
481 return blink::WebTimeRanges();
483 const double seekable_end = duration();
485 // Allow a special exception for seeks to zero for streaming sources with a
486 // finite duration; this allows looping to work.
487 const bool allow_seek_to_zero = data_source_ && data_source_->IsStreaming() &&
488 base::IsFinite(seekable_end);
490 // TODO(dalecurtis): Technically this allows seeking on media which return an
491 // infinite duration so long as DataSource::IsStreaming() is false. While not
492 // expected, disabling this breaks semi-live players, http://crbug.com/427412.
493 const blink::WebTimeRange seekable_range(
494 0.0, allow_seek_to_zero ? 0.0 : seekable_end);
495 return blink::WebTimeRanges(&seekable_range, 1);
498 bool WebMediaPlayerImpl::didLoadingProgress() {
499 DCHECK(main_task_runner_->BelongsToCurrentThread());
500 bool pipeline_progress = pipeline_.DidLoadingProgress();
501 bool data_progress = buffered_data_source_host_.DidLoadingProgress();
502 return pipeline_progress || data_progress;
505 void WebMediaPlayerImpl::paint(blink::WebCanvas* canvas,
506 const blink::WebRect& rect,
507 unsigned char alpha,
508 SkXfermode::Mode mode) {
509 DCHECK(main_task_runner_->BelongsToCurrentThread());
510 TRACE_EVENT0("media", "WebMediaPlayerImpl:paint");
512 // TODO(scherkus): Clarify paint() API contract to better understand when and
513 // why it's being called. For example, today paint() is called when:
514 // - We haven't reached HAVE_CURRENT_DATA and need to paint black
515 // - We're painting to a canvas
516 // See http://crbug.com/341225 http://crbug.com/342621 for details.
517 scoped_refptr<VideoFrame> video_frame =
518 GetCurrentFrameFromCompositor();
520 gfx::Rect gfx_rect(rect);
521 Context3D context_3d;
522 if (video_frame.get() &&
523 video_frame->format() == VideoFrame::NATIVE_TEXTURE) {
524 if (!context_3d_cb_.is_null()) {
525 context_3d = context_3d_cb_.Run();
527 // GPU Process crashed.
528 if (!context_3d.gl)
529 return;
531 skcanvas_video_renderer_.Paint(video_frame, canvas, gfx_rect, alpha, mode,
532 pipeline_metadata_.video_rotation, context_3d);
535 bool WebMediaPlayerImpl::hasSingleSecurityOrigin() const {
536 if (data_source_)
537 return data_source_->HasSingleOrigin();
538 return true;
541 bool WebMediaPlayerImpl::didPassCORSAccessCheck() const {
542 if (data_source_)
543 return data_source_->DidPassCORSAccessCheck();
544 return false;
547 double WebMediaPlayerImpl::mediaTimeForTimeValue(double timeValue) const {
548 return ConvertSecondsToTimestamp(timeValue).InSecondsF();
551 unsigned WebMediaPlayerImpl::decodedFrameCount() const {
552 DCHECK(main_task_runner_->BelongsToCurrentThread());
554 PipelineStatistics stats = pipeline_.GetStatistics();
555 return stats.video_frames_decoded;
558 unsigned WebMediaPlayerImpl::droppedFrameCount() const {
559 DCHECK(main_task_runner_->BelongsToCurrentThread());
561 PipelineStatistics stats = pipeline_.GetStatistics();
562 return stats.video_frames_dropped;
565 unsigned WebMediaPlayerImpl::audioDecodedByteCount() const {
566 DCHECK(main_task_runner_->BelongsToCurrentThread());
568 PipelineStatistics stats = pipeline_.GetStatistics();
569 return stats.audio_bytes_decoded;
572 unsigned WebMediaPlayerImpl::videoDecodedByteCount() const {
573 DCHECK(main_task_runner_->BelongsToCurrentThread());
575 PipelineStatistics stats = pipeline_.GetStatistics();
576 return stats.video_bytes_decoded;
579 bool WebMediaPlayerImpl::copyVideoTextureToPlatformTexture(
580 blink::WebGraphicsContext3D* web_graphics_context,
581 unsigned int texture,
582 unsigned int level,
583 unsigned int internal_format,
584 unsigned int type,
585 bool premultiply_alpha,
586 bool flip_y) {
587 return copyVideoTextureToPlatformTexture(web_graphics_context, texture,
588 internal_format, type,
589 premultiply_alpha, flip_y);
592 bool WebMediaPlayerImpl::copyVideoTextureToPlatformTexture(
593 blink::WebGraphicsContext3D* web_graphics_context,
594 unsigned int texture,
595 unsigned int internal_format,
596 unsigned int type,
597 bool premultiply_alpha,
598 bool flip_y) {
599 TRACE_EVENT0("media", "WebMediaPlayerImpl:copyVideoTextureToPlatformTexture");
601 scoped_refptr<VideoFrame> video_frame =
602 GetCurrentFrameFromCompositor();
604 if (!video_frame.get() ||
605 video_frame->format() != VideoFrame::NATIVE_TEXTURE) {
606 return false;
609 // TODO(dshwang): need more elegant way to convert WebGraphicsContext3D to
610 // GLES2Interface.
611 gpu::gles2::GLES2Interface* gl =
612 static_cast<gpu_blink::WebGraphicsContext3DImpl*>(web_graphics_context)
613 ->GetGLInterface();
614 SkCanvasVideoRenderer::CopyVideoFrameTextureToGLTexture(
615 gl, video_frame.get(), texture, internal_format, type, premultiply_alpha,
616 flip_y);
617 return true;
620 WebMediaPlayer::MediaKeyException
621 WebMediaPlayerImpl::generateKeyRequest(const WebString& key_system,
622 const unsigned char* init_data,
623 unsigned init_data_length) {
624 DCHECK(main_task_runner_->BelongsToCurrentThread());
626 return encrypted_media_support_.GenerateKeyRequest(
627 frame_, key_system, init_data, init_data_length);
630 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::addKey(
631 const WebString& key_system,
632 const unsigned char* key,
633 unsigned key_length,
634 const unsigned char* init_data,
635 unsigned init_data_length,
636 const WebString& session_id) {
637 DCHECK(main_task_runner_->BelongsToCurrentThread());
639 return encrypted_media_support_.AddKey(
640 key_system, key, key_length, init_data, init_data_length, session_id);
643 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::cancelKeyRequest(
644 const WebString& key_system,
645 const WebString& session_id) {
646 DCHECK(main_task_runner_->BelongsToCurrentThread());
648 return encrypted_media_support_.CancelKeyRequest(key_system, session_id);
651 void WebMediaPlayerImpl::setContentDecryptionModule(
652 blink::WebContentDecryptionModule* cdm,
653 blink::WebContentDecryptionModuleResult result) {
654 DCHECK(main_task_runner_->BelongsToCurrentThread());
656 // TODO(xhwang): Support setMediaKeys(0) if necessary: http://crbug.com/330324
657 if (!cdm) {
658 result.completeWithError(
659 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
660 "Null MediaKeys object is not supported.");
661 return;
664 SetCdm(BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnCdmAttached, result),
665 ToWebContentDecryptionModuleImpl(cdm)->GetCdmContext());
668 void WebMediaPlayerImpl::OnEncryptedMediaInitData(
669 EmeInitDataType init_data_type,
670 const std::vector<uint8>& init_data) {
671 DCHECK(init_data_type != EmeInitDataType::UNKNOWN);
673 // Do not fire "encrypted" event if encrypted media is not enabled.
674 // TODO(xhwang): Handle this in |client_|.
675 if (!blink::WebRuntimeFeatures::isPrefixedEncryptedMediaEnabled() &&
676 !blink::WebRuntimeFeatures::isEncryptedMediaEnabled()) {
677 return;
680 // TODO(xhwang): Update this UMA name.
681 UMA_HISTOGRAM_COUNTS("Media.EME.NeedKey", 1);
683 encrypted_media_support_.SetInitDataType(init_data_type);
685 client_->encrypted(ConvertToWebInitDataType(init_data_type),
686 vector_as_array(&init_data),
687 base::saturated_cast<unsigned int>(init_data.size()));
690 void WebMediaPlayerImpl::OnWaitingForDecryptionKey() {
691 client_->didBlockPlaybackWaitingForKey();
693 // TODO(jrummell): didResumePlaybackBlockedForKey() should only be called
694 // when a key has been successfully added (e.g. OnSessionKeysChange() with
695 // |has_additional_usable_key| = true). http://crbug.com/461903
696 client_->didResumePlaybackBlockedForKey();
699 void WebMediaPlayerImpl::SetCdm(const CdmAttachedCB& cdm_attached_cb,
700 CdmContext* cdm_context) {
701 pipeline_.SetCdm(cdm_context, cdm_attached_cb);
704 void WebMediaPlayerImpl::OnCdmAttached(
705 blink::WebContentDecryptionModuleResult result,
706 bool success) {
707 if (success) {
708 result.complete();
709 return;
712 result.completeWithError(
713 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
714 "Unable to set MediaKeys object");
717 void WebMediaPlayerImpl::OnPipelineSeeked(bool time_changed,
718 PipelineStatus status) {
719 DVLOG(1) << __FUNCTION__ << "(" << time_changed << ", " << status << ")";
720 DCHECK(main_task_runner_->BelongsToCurrentThread());
721 seeking_ = false;
722 if (pending_seek_) {
723 pending_seek_ = false;
724 seek(pending_seek_seconds_);
725 return;
728 if (status != PIPELINE_OK) {
729 OnPipelineError(status);
730 return;
733 // Update our paused time.
734 if (paused_)
735 UpdatePausedTime();
737 should_notify_time_changed_ = time_changed;
740 void WebMediaPlayerImpl::OnPipelineEnded() {
741 DVLOG(1) << __FUNCTION__;
742 DCHECK(main_task_runner_->BelongsToCurrentThread());
744 // Ignore state changes until we've completed all outstanding seeks.
745 if (seeking_ || pending_seek_)
746 return;
748 ended_ = true;
749 client_->timeChanged();
752 void WebMediaPlayerImpl::OnPipelineError(PipelineStatus error) {
753 DCHECK(main_task_runner_->BelongsToCurrentThread());
754 DCHECK_NE(error, PIPELINE_OK);
756 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) {
757 // Any error that occurs before reaching ReadyStateHaveMetadata should
758 // be considered a format error.
759 SetNetworkState(WebMediaPlayer::NetworkStateFormatError);
760 return;
763 SetNetworkState(PipelineErrorToNetworkState(error));
765 if (error == PIPELINE_ERROR_DECRYPT)
766 encrypted_media_support_.OnPipelineDecryptError();
769 void WebMediaPlayerImpl::OnPipelineMetadata(
770 PipelineMetadata metadata) {
771 DVLOG(1) << __FUNCTION__;
773 pipeline_metadata_ = metadata;
775 UMA_HISTOGRAM_ENUMERATION("Media.VideoRotation", metadata.video_rotation,
776 VIDEO_ROTATION_MAX + 1);
777 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
779 if (hasVideo()) {
780 DCHECK(!video_weblayer_);
781 scoped_refptr<cc::VideoLayer> layer =
782 cc::VideoLayer::Create(compositor_, pipeline_metadata_.video_rotation);
784 if (pipeline_metadata_.video_rotation == VIDEO_ROTATION_90 ||
785 pipeline_metadata_.video_rotation == VIDEO_ROTATION_270) {
786 gfx::Size size = pipeline_metadata_.natural_size;
787 pipeline_metadata_.natural_size = gfx::Size(size.height(), size.width());
790 video_weblayer_.reset(new cc_blink::WebLayerImpl(layer));
791 video_weblayer_->setOpaque(opaque_);
792 client_->setWebLayer(video_weblayer_.get());
796 void WebMediaPlayerImpl::OnPipelineBufferingStateChanged(
797 BufferingState buffering_state) {
798 DVLOG(1) << __FUNCTION__ << "(" << buffering_state << ")";
800 // Ignore buffering state changes until we've completed all outstanding seeks.
801 if (seeking_ || pending_seek_)
802 return;
804 // TODO(scherkus): Handle other buffering states when Pipeline starts using
805 // them and translate them ready state changes http://crbug.com/144683
806 DCHECK_EQ(buffering_state, BUFFERING_HAVE_ENOUGH);
807 SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData);
809 // Let the DataSource know we have enough data. It may use this information to
810 // release unused network connections.
811 if (data_source_)
812 data_source_->OnBufferingHaveEnough();
814 // Blink expects a timeChanged() in response to a seek().
815 if (should_notify_time_changed_)
816 client_->timeChanged();
819 void WebMediaPlayerImpl::OnDemuxerOpened() {
820 DCHECK(main_task_runner_->BelongsToCurrentThread());
821 client_->mediaSourceOpened(new WebMediaSourceImpl(
822 chunk_demuxer_, base::Bind(&MediaLog::AddLogEvent, media_log_)));
825 void WebMediaPlayerImpl::OnAddTextTrack(
826 const TextTrackConfig& config,
827 const AddTextTrackDoneCB& done_cb) {
828 DCHECK(main_task_runner_->BelongsToCurrentThread());
830 const WebInbandTextTrackImpl::Kind web_kind =
831 static_cast<WebInbandTextTrackImpl::Kind>(config.kind());
832 const blink::WebString web_label =
833 blink::WebString::fromUTF8(config.label());
834 const blink::WebString web_language =
835 blink::WebString::fromUTF8(config.language());
836 const blink::WebString web_id =
837 blink::WebString::fromUTF8(config.id());
839 scoped_ptr<WebInbandTextTrackImpl> web_inband_text_track(
840 new WebInbandTextTrackImpl(web_kind, web_label, web_language, web_id));
842 scoped_ptr<TextTrack> text_track(new TextTrackImpl(
843 main_task_runner_, client_, web_inband_text_track.Pass()));
845 done_cb.Run(text_track.Pass());
848 void WebMediaPlayerImpl::DataSourceInitialized(bool success) {
849 DCHECK(main_task_runner_->BelongsToCurrentThread());
851 if (!success) {
852 SetNetworkState(WebMediaPlayer::NetworkStateFormatError);
853 return;
856 StartPipeline();
859 void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading) {
860 if (!is_downloading && network_state_ == WebMediaPlayer::NetworkStateLoading)
861 SetNetworkState(WebMediaPlayer::NetworkStateIdle);
862 else if (is_downloading && network_state_ == WebMediaPlayer::NetworkStateIdle)
863 SetNetworkState(WebMediaPlayer::NetworkStateLoading);
864 media_log_->AddEvent(
865 media_log_->CreateBooleanEvent(
866 MediaLogEvent::NETWORK_ACTIVITY_SET,
867 "is_downloading_data", is_downloading));
870 void WebMediaPlayerImpl::StartPipeline() {
871 DCHECK(main_task_runner_->BelongsToCurrentThread());
873 // Keep track if this is a MSE or non-MSE playback.
874 UMA_HISTOGRAM_BOOLEAN("Media.MSE.Playback",
875 (load_type_ == LoadTypeMediaSource));
877 LogCB mse_log_cb;
878 Demuxer::EncryptedMediaInitDataCB encrypted_media_init_data_cb =
879 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnEncryptedMediaInitData);
881 // Figure out which demuxer to use.
882 if (load_type_ != LoadTypeMediaSource) {
883 DCHECK(!chunk_demuxer_);
884 DCHECK(data_source_);
886 demuxer_.reset(new FFmpegDemuxer(media_task_runner_, data_source_.get(),
887 encrypted_media_init_data_cb, media_log_));
888 } else {
889 DCHECK(!chunk_demuxer_);
890 DCHECK(!data_source_);
892 mse_log_cb = base::Bind(&MediaLog::AddLogEvent, media_log_);
894 chunk_demuxer_ = new ChunkDemuxer(
895 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDemuxerOpened),
896 encrypted_media_init_data_cb, mse_log_cb, media_log_, true);
897 demuxer_.reset(chunk_demuxer_);
900 // ... and we're ready to go!
901 seeking_ = true;
903 pipeline_.Start(
904 demuxer_.get(),
905 renderer_factory_->CreateRenderer(media_task_runner_,
906 audio_source_provider_.get()),
907 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineEnded),
908 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineError),
909 BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnPipelineSeeked, false),
910 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineMetadata),
911 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineBufferingStateChanged),
912 base::Bind(&WebMediaPlayerImpl::FrameReady, base::Unretained(this)),
913 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDurationChanged),
914 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnAddTextTrack),
915 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnWaitingForDecryptionKey));
918 void WebMediaPlayerImpl::SetNetworkState(WebMediaPlayer::NetworkState state) {
919 DVLOG(1) << __FUNCTION__ << "(" << state << ")";
920 DCHECK(main_task_runner_->BelongsToCurrentThread());
921 network_state_ = state;
922 // Always notify to ensure client has the latest value.
923 client_->networkStateChanged();
926 void WebMediaPlayerImpl::SetReadyState(WebMediaPlayer::ReadyState state) {
927 DVLOG(1) << __FUNCTION__ << "(" << state << ")";
928 DCHECK(main_task_runner_->BelongsToCurrentThread());
930 if (state == WebMediaPlayer::ReadyStateHaveEnoughData && data_source_ &&
931 data_source_->assume_fully_buffered() &&
932 network_state_ == WebMediaPlayer::NetworkStateLoading)
933 SetNetworkState(WebMediaPlayer::NetworkStateLoaded);
935 ready_state_ = state;
936 // Always notify to ensure client has the latest value.
937 client_->readyStateChanged();
940 blink::WebAudioSourceProvider* WebMediaPlayerImpl::audioSourceProvider() {
941 return audio_source_provider_.get();
944 double WebMediaPlayerImpl::GetPipelineDuration() const {
945 base::TimeDelta duration = pipeline_.GetMediaDuration();
947 // Return positive infinity if the resource is unbounded.
948 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
949 if (duration == kInfiniteDuration())
950 return std::numeric_limits<double>::infinity();
952 return duration.InSecondsF();
955 void WebMediaPlayerImpl::OnDurationChanged() {
956 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
957 return;
959 client_->durationChanged();
962 void WebMediaPlayerImpl::OnNaturalSizeChanged(gfx::Size size) {
963 DCHECK(main_task_runner_->BelongsToCurrentThread());
964 DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
965 TRACE_EVENT0("media", "WebMediaPlayerImpl::OnNaturalSizeChanged");
967 media_log_->AddEvent(
968 media_log_->CreateVideoSizeSetEvent(size.width(), size.height()));
969 pipeline_metadata_.natural_size = size;
971 client_->sizeChanged();
974 void WebMediaPlayerImpl::OnOpacityChanged(bool opaque) {
975 DCHECK(main_task_runner_->BelongsToCurrentThread());
976 DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
978 opaque_ = opaque;
979 if (video_weblayer_)
980 video_weblayer_->setOpaque(opaque_);
983 void WebMediaPlayerImpl::FrameReady(
984 const scoped_refptr<VideoFrame>& frame) {
985 compositor_task_runner_->PostTask(
986 FROM_HERE,
987 base::Bind(&VideoFrameCompositor::UpdateCurrentFrame,
988 base::Unretained(compositor_),
989 frame));
992 static void GetCurrentFrameAndSignal(
993 VideoFrameCompositor* compositor,
994 scoped_refptr<VideoFrame>* video_frame_out,
995 base::WaitableEvent* event) {
996 TRACE_EVENT0("media", "GetCurrentFrameAndSignal");
997 *video_frame_out = compositor->GetCurrentFrame();
998 event->Signal();
1001 scoped_refptr<VideoFrame>
1002 WebMediaPlayerImpl::GetCurrentFrameFromCompositor() {
1003 TRACE_EVENT0("media", "WebMediaPlayerImpl::GetCurrentFrameFromCompositor");
1004 if (compositor_task_runner_->BelongsToCurrentThread())
1005 return compositor_->GetCurrentFrame();
1007 // Use a posted task and waitable event instead of a lock otherwise
1008 // WebGL/Canvas can see different content than what the compositor is seeing.
1009 scoped_refptr<VideoFrame> video_frame;
1010 base::WaitableEvent event(false, false);
1011 compositor_task_runner_->PostTask(FROM_HERE,
1012 base::Bind(&GetCurrentFrameAndSignal,
1013 base::Unretained(compositor_),
1014 &video_frame,
1015 &event));
1016 event.Wait();
1017 return video_frame;
1020 void WebMediaPlayerImpl::UpdatePausedTime() {
1021 DCHECK(main_task_runner_->BelongsToCurrentThread());
1023 // pause() may be called after playback has ended and the HTMLMediaElement
1024 // requires that currentTime() == duration() after ending. We want to ensure
1025 // |paused_time_| matches currentTime() in this case or a future seek() may
1026 // incorrectly discard what it thinks is a seek to the existing time.
1027 paused_time_ =
1028 ended_ ? pipeline_.GetMediaDuration() : pipeline_.GetMediaTime();
1031 } // namespace media