Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / webmediaplayer_impl.cc
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.
4
5 #include "content/renderer/media/webmediaplayer_impl.h"
6
7 #include <algorithm>
8 #include <limits>
9 #include <string>
10 #include <vector>
11
12 #include "base/bind.h"
13 #include "base/callback.h"
14 #include "base/callback_helpers.h"
15 #include "base/command_line.h"
16 #include "base/debug/alias.h"
17 #include "base/debug/crash_logging.h"
18 #include "base/debug/trace_event.h"
19 #include "base/message_loop/message_loop_proxy.h"
20 #include "base/metrics/histogram.h"
21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "base/synchronization/waitable_event.h"
24 #include "cc/layers/video_layer.h"
25 #include "content/public/common/content_switches.h"
26 #include "content/public/renderer/render_frame.h"
27 #include "content/renderer/media/buffered_data_source.h"
28 #include "content/renderer/media/crypto/key_systems.h"
29 #include "content/renderer/media/render_media_log.h"
30 #include "content/renderer/media/texttrack_impl.h"
31 #include "content/renderer/media/webaudiosourceprovider_impl.h"
32 #include "content/renderer/media/webcontentdecryptionmodule_impl.h"
33 #include "content/renderer/media/webinbandtexttrack_impl.h"
34 #include "content/renderer/media/webmediaplayer_delegate.h"
35 #include "content/renderer/media/webmediaplayer_params.h"
36 #include "content/renderer/media/webmediaplayer_util.h"
37 #include "content/renderer/media/webmediasource_impl.h"
38 #include "content/renderer/pepper/pepper_webplugin_impl.h"
39 #include "content/renderer/render_thread_impl.h"
40 #include "gpu/GLES2/gl2extchromium.h"
41 #include "gpu/command_buffer/common/mailbox_holder.h"
42 #include "media/audio/null_audio_sink.h"
43 #include "media/base/audio_hardware_config.h"
44 #include "media/base/bind_to_current_loop.h"
45 #include "media/base/filter_collection.h"
46 #include "media/base/limits.h"
47 #include "media/base/media_log.h"
48 #include "media/base/media_switches.h"
49 #include "media/base/pipeline.h"
50 #include "media/base/text_renderer.h"
51 #include "media/base/video_frame.h"
52 #include "media/filters/audio_renderer_impl.h"
53 #include "media/filters/chunk_demuxer.h"
54 #include "media/filters/ffmpeg_audio_decoder.h"
55 #include "media/filters/ffmpeg_demuxer.h"
56 #include "media/filters/ffmpeg_video_decoder.h"
57 #include "media/filters/gpu_video_accelerator_factories.h"
58 #include "media/filters/gpu_video_decoder.h"
59 #include "media/filters/opus_audio_decoder.h"
60 #include "media/filters/video_renderer_impl.h"
61 #include "media/filters/vpx_video_decoder.h"
62 #include "third_party/WebKit/public/platform/WebContentDecryptionModule.h"
63 #include "third_party/WebKit/public/platform/WebMediaSource.h"
64 #include "third_party/WebKit/public/platform/WebRect.h"
65 #include "third_party/WebKit/public/platform/WebSize.h"
66 #include "third_party/WebKit/public/platform/WebString.h"
67 #include "third_party/WebKit/public/platform/WebURL.h"
68 #include "third_party/WebKit/public/web/WebDocument.h"
69 #include "third_party/WebKit/public/web/WebLocalFrame.h"
70 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
71 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
72 #include "third_party/WebKit/public/web/WebView.h"
73 #include "v8/include/v8.h"
74 #include "webkit/renderer/compositor_bindings/web_layer_impl.h"
75
76 #if defined(ENABLE_PEPPER_CDMS)
77 #include "content/renderer/media/crypto/pepper_cdm_wrapper_impl.h"
78 #endif
79
80 using blink::WebCanvas;
81 using blink::WebMediaPlayer;
82 using blink::WebRect;
83 using blink::WebSize;
84 using blink::WebString;
85 using media::PipelineStatus;
86
87 namespace {
88
89 // Amount of extra memory used by each player instance reported to V8.
90 // It is not exact number -- first, it differs on different platforms,
91 // and second, it is very hard to calculate. Instead, use some arbitrary
92 // value that will cause garbage collection from time to time. We don't want
93 // it to happen on every allocation, but don't want 5k players to sit in memory
94 // either. Looks that chosen constant achieves both goals, at least for audio
95 // objects. (Do not worry about video objects yet, JS programs do not create
96 // thousands of them...)
97 const int kPlayerExtraMemory = 1024 * 1024;
98
99 // Limits the range of playback rate.
100 //
101 // TODO(kylep): Revisit these.
102 //
103 // Vista has substantially lower performance than XP or Windows7.  If you speed
104 // up a video too much, it can't keep up, and rendering stops updating except on
105 // the time bar. For really high speeds, audio becomes a bottleneck and we just
106 // use up the data we have, which may not achieve the speed requested, but will
107 // not crash the tab.
108 //
109 // A very slow speed, ie 0.00000001x, causes the machine to lock up. (It seems
110 // like a busy loop). It gets unresponsive, although its not completely dead.
111 //
112 // Also our timers are not very accurate (especially for ogg), which becomes
113 // evident at low speeds and on Vista. Since other speeds are risky and outside
114 // the norms, we think 1/16x to 16x is a safe and useful range for now.
115 const double kMinRate = 0.0625;
116 const double kMaxRate = 16.0;
117
118 // Prefix for histograms related to Encrypted Media Extensions.
119 const char* kMediaEme = "Media.EME.";
120
121 }  // namespace
122
123 namespace content {
124
125 class BufferedDataSourceHostImpl;
126
127 #define COMPILE_ASSERT_MATCHING_ENUM(name) \
128   COMPILE_ASSERT(static_cast<int>(WebMediaPlayer::CORSMode ## name) == \
129                  static_cast<int>(BufferedResourceLoader::k ## name), \
130                  mismatching_enums)
131 COMPILE_ASSERT_MATCHING_ENUM(Unspecified);
132 COMPILE_ASSERT_MATCHING_ENUM(Anonymous);
133 COMPILE_ASSERT_MATCHING_ENUM(UseCredentials);
134 #undef COMPILE_ASSERT_MATCHING_ENUM
135
136 #define BIND_TO_RENDER_LOOP(function) \
137   (DCHECK(main_loop_->BelongsToCurrentThread()), \
138   media::BindToCurrentLoop(base::Bind(function, AsWeakPtr())))
139
140 static void LogMediaSourceError(const scoped_refptr<media::MediaLog>& media_log,
141                                 const std::string& error) {
142   media_log->AddEvent(media_log->CreateMediaSourceErrorEvent(error));
143 }
144
145 WebMediaPlayerImpl::WebMediaPlayerImpl(
146     blink::WebLocalFrame* frame,
147     blink::WebMediaPlayerClient* client,
148     base::WeakPtr<WebMediaPlayerDelegate> delegate,
149     const WebMediaPlayerParams& params)
150     : frame_(frame),
151       network_state_(WebMediaPlayer::NetworkStateEmpty),
152       ready_state_(WebMediaPlayer::ReadyStateHaveNothing),
153       main_loop_(base::MessageLoopProxy::current()),
154       media_loop_(
155           RenderThreadImpl::current()->GetMediaThreadMessageLoopProxy()),
156       media_log_(new RenderMediaLog()),
157       pipeline_(media_loop_, media_log_.get()),
158       opaque_(false),
159       paused_(true),
160       seeking_(false),
161       playback_rate_(0.0f),
162       pending_seek_(false),
163       pending_seek_seconds_(0.0f),
164       client_(client),
165       delegate_(delegate),
166       defer_load_cb_(params.defer_load_cb()),
167       accelerated_compositing_reported_(false),
168       incremented_externally_allocated_memory_(false),
169       gpu_factories_(RenderThreadImpl::current()->GetGpuFactories()),
170       is_local_source_(false),
171       supports_save_(true),
172       starting_(false),
173       chunk_demuxer_(NULL),
174       // Threaded compositing isn't enabled universally yet.
175       compositor_task_runner_(
176           RenderThreadImpl::current()->compositor_message_loop_proxy()
177               ? RenderThreadImpl::current()->compositor_message_loop_proxy()
178               : base::MessageLoopProxy::current()),
179       compositor_(new VideoFrameCompositor(
180           BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNaturalSizeChanged),
181           BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnOpacityChanged))),
182       text_track_index_(0),
183       web_cdm_(NULL) {
184   media_log_->AddEvent(
185       media_log_->CreateEvent(media::MediaLogEvent::WEBMEDIAPLAYER_CREATED));
186
187   // |gpu_factories_| requires that its entry points be called on its
188   // |GetTaskRunner()|.  Since |pipeline_| will own decoders created from the
189   // factories, require that their message loops are identical.
190   DCHECK(!gpu_factories_ || (gpu_factories_->GetTaskRunner() == media_loop_));
191
192   // Let V8 know we started new thread if we did not do it yet.
193   // Made separate task to avoid deletion of player currently being created.
194   // Also, delaying GC until after player starts gets rid of starting lag --
195   // collection happens in parallel with playing.
196   //
197   // TODO(enal): remove when we get rid of per-audio-stream thread.
198   main_loop_->PostTask(
199       FROM_HERE,
200       base::Bind(&WebMediaPlayerImpl::IncrementExternallyAllocatedMemory,
201                  AsWeakPtr()));
202
203   // Use the null sink if no sink was provided.
204   audio_source_provider_ = new WebAudioSourceProviderImpl(
205       params.audio_renderer_sink().get()
206           ? params.audio_renderer_sink()
207           : new media::NullAudioSink(media_loop_));
208 }
209
210 WebMediaPlayerImpl::~WebMediaPlayerImpl() {
211   client_->setWebLayer(NULL);
212
213   DCHECK(main_loop_->BelongsToCurrentThread());
214   media_log_->AddEvent(
215       media_log_->CreateEvent(media::MediaLogEvent::WEBMEDIAPLAYER_DESTROYED));
216
217   if (delegate_.get())
218     delegate_->PlayerGone(this);
219
220   // Abort any pending IO so stopping the pipeline doesn't get blocked.
221   if (data_source_)
222     data_source_->Abort();
223   if (chunk_demuxer_) {
224     chunk_demuxer_->Shutdown();
225     chunk_demuxer_ = NULL;
226   }
227
228   gpu_factories_ = NULL;
229
230   // Make sure to kill the pipeline so there's no more media threads running.
231   // Note: stopping the pipeline might block for a long time.
232   base::WaitableEvent waiter(false, false);
233   pipeline_.Stop(
234       base::Bind(&base::WaitableEvent::Signal, base::Unretained(&waiter)));
235   waiter.Wait();
236
237   compositor_task_runner_->DeleteSoon(FROM_HERE, compositor_);
238
239   // Let V8 know we are not using extra resources anymore.
240   if (incremented_externally_allocated_memory_) {
241     v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
242         -kPlayerExtraMemory);
243     incremented_externally_allocated_memory_ = false;
244   }
245 }
246
247 namespace {
248
249 // Helper enum for reporting scheme histograms.
250 enum URLSchemeForHistogram {
251   kUnknownURLScheme,
252   kMissingURLScheme,
253   kHttpURLScheme,
254   kHttpsURLScheme,
255   kFtpURLScheme,
256   kChromeExtensionURLScheme,
257   kJavascriptURLScheme,
258   kFileURLScheme,
259   kBlobURLScheme,
260   kDataURLScheme,
261   kFileSystemScheme,
262   kMaxURLScheme = kFileSystemScheme  // Must be equal to highest enum value.
263 };
264
265 URLSchemeForHistogram URLScheme(const GURL& url) {
266   if (!url.has_scheme()) return kMissingURLScheme;
267   if (url.SchemeIs("http")) return kHttpURLScheme;
268   if (url.SchemeIs("https")) return kHttpsURLScheme;
269   if (url.SchemeIs("ftp")) return kFtpURLScheme;
270   if (url.SchemeIs("chrome-extension")) return kChromeExtensionURLScheme;
271   if (url.SchemeIs("javascript")) return kJavascriptURLScheme;
272   if (url.SchemeIs("file")) return kFileURLScheme;
273   if (url.SchemeIs("blob")) return kBlobURLScheme;
274   if (url.SchemeIs("data")) return kDataURLScheme;
275   if (url.SchemeIs("filesystem")) return kFileSystemScheme;
276   return kUnknownURLScheme;
277 }
278
279 }  // namespace
280
281 void WebMediaPlayerImpl::load(LoadType load_type, const blink::WebURL& url,
282                               CORSMode cors_mode) {
283   if (!defer_load_cb_.is_null()) {
284     defer_load_cb_.Run(base::Bind(
285         &WebMediaPlayerImpl::DoLoad, AsWeakPtr(), load_type, url, cors_mode));
286     return;
287   }
288   DoLoad(load_type, url, cors_mode);
289 }
290
291 void WebMediaPlayerImpl::DoLoad(LoadType load_type,
292                                 const blink::WebURL& url,
293                                 CORSMode cors_mode) {
294   DCHECK(main_loop_->BelongsToCurrentThread());
295
296   GURL gurl(url);
297   UMA_HISTOGRAM_ENUMERATION("Media.URLScheme", URLScheme(gurl), kMaxURLScheme);
298
299   // Set subresource URL for crash reporting.
300   base::debug::SetCrashKeyValue("subresource_url", gurl.spec());
301
302   load_type_ = load_type;
303
304   // Handle any volume/preload changes that occurred before load().
305   setVolume(client_->volume());
306   setPreload(client_->preload());
307
308   SetNetworkState(WebMediaPlayer::NetworkStateLoading);
309   SetReadyState(WebMediaPlayer::ReadyStateHaveNothing);
310   media_log_->AddEvent(media_log_->CreateLoadEvent(url.spec()));
311
312   // Media source pipelines can start immediately.
313   if (load_type == LoadTypeMediaSource) {
314     supports_save_ = false;
315     StartPipeline();
316     return;
317   }
318
319   // Otherwise it's a regular request which requires resolving the URL first.
320   data_source_.reset(new BufferedDataSource(
321       main_loop_,
322       frame_,
323       media_log_.get(),
324       &buffered_data_source_host_,
325       base::Bind(&WebMediaPlayerImpl::NotifyDownloading, AsWeakPtr())));
326   data_source_->Initialize(
327       url, static_cast<BufferedResourceLoader::CORSMode>(cors_mode),
328       base::Bind(
329           &WebMediaPlayerImpl::DataSourceInitialized,
330           AsWeakPtr(), gurl));
331
332   is_local_source_ = !gurl.SchemeIsHTTPOrHTTPS();
333 }
334
335 void WebMediaPlayerImpl::play() {
336   DCHECK(main_loop_->BelongsToCurrentThread());
337
338   paused_ = false;
339   pipeline_.SetPlaybackRate(playback_rate_);
340   if (data_source_)
341     data_source_->MediaIsPlaying();
342
343   media_log_->AddEvent(media_log_->CreateEvent(media::MediaLogEvent::PLAY));
344
345   if (delegate_.get())
346     delegate_->DidPlay(this);
347 }
348
349 void WebMediaPlayerImpl::pause() {
350   DCHECK(main_loop_->BelongsToCurrentThread());
351
352   paused_ = true;
353   pipeline_.SetPlaybackRate(0.0f);
354   if (data_source_)
355     data_source_->MediaIsPaused();
356   paused_time_ = pipeline_.GetMediaTime();
357
358   media_log_->AddEvent(media_log_->CreateEvent(media::MediaLogEvent::PAUSE));
359
360   if (delegate_.get())
361     delegate_->DidPause(this);
362 }
363
364 bool WebMediaPlayerImpl::supportsSave() const {
365   DCHECK(main_loop_->BelongsToCurrentThread());
366   return supports_save_;
367 }
368
369 void WebMediaPlayerImpl::seek(double seconds) {
370   DCHECK(main_loop_->BelongsToCurrentThread());
371
372   if (ready_state_ > WebMediaPlayer::ReadyStateHaveMetadata)
373     SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
374
375   base::TimeDelta seek_time = ConvertSecondsToTimestamp(seconds);
376
377   if (starting_ || seeking_) {
378     pending_seek_ = true;
379     pending_seek_seconds_ = seconds;
380     if (chunk_demuxer_)
381       chunk_demuxer_->CancelPendingSeek(seek_time);
382     return;
383   }
384
385   media_log_->AddEvent(media_log_->CreateSeekEvent(seconds));
386
387   // Update our paused time.
388   if (paused_)
389     paused_time_ = seek_time;
390
391   seeking_ = true;
392
393   if (chunk_demuxer_)
394     chunk_demuxer_->StartWaitingForSeek(seek_time);
395
396   // Kick off the asynchronous seek!
397   pipeline_.Seek(
398       seek_time,
399       BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineSeek));
400 }
401
402 void WebMediaPlayerImpl::setRate(double rate) {
403   DCHECK(main_loop_->BelongsToCurrentThread());
404
405   // TODO(kylep): Remove when support for negatives is added. Also, modify the
406   // following checks so rewind uses reasonable values also.
407   if (rate < 0.0)
408     return;
409
410   // Limit rates to reasonable values by clamping.
411   if (rate != 0.0) {
412     if (rate < kMinRate)
413       rate = kMinRate;
414     else if (rate > kMaxRate)
415       rate = kMaxRate;
416   }
417
418   playback_rate_ = rate;
419   if (!paused_) {
420     pipeline_.SetPlaybackRate(rate);
421     if (data_source_)
422       data_source_->MediaPlaybackRateChanged(rate);
423   }
424 }
425
426 void WebMediaPlayerImpl::setVolume(double volume) {
427   DCHECK(main_loop_->BelongsToCurrentThread());
428
429   pipeline_.SetVolume(volume);
430 }
431
432 #define COMPILE_ASSERT_MATCHING_ENUM(webkit_name, chromium_name) \
433     COMPILE_ASSERT(static_cast<int>(WebMediaPlayer::webkit_name) == \
434                    static_cast<int>(content::chromium_name), \
435                    mismatching_enums)
436 COMPILE_ASSERT_MATCHING_ENUM(PreloadNone, NONE);
437 COMPILE_ASSERT_MATCHING_ENUM(PreloadMetaData, METADATA);
438 COMPILE_ASSERT_MATCHING_ENUM(PreloadAuto, AUTO);
439 #undef COMPILE_ASSERT_MATCHING_ENUM
440
441 void WebMediaPlayerImpl::setPreload(WebMediaPlayer::Preload preload) {
442   DCHECK(main_loop_->BelongsToCurrentThread());
443
444   if (data_source_)
445     data_source_->SetPreload(static_cast<content::Preload>(preload));
446 }
447
448 bool WebMediaPlayerImpl::hasVideo() const {
449   DCHECK(main_loop_->BelongsToCurrentThread());
450
451   return pipeline_metadata_.has_video;
452 }
453
454 bool WebMediaPlayerImpl::hasAudio() const {
455   DCHECK(main_loop_->BelongsToCurrentThread());
456
457   return pipeline_metadata_.has_audio;
458 }
459
460 blink::WebSize WebMediaPlayerImpl::naturalSize() const {
461   DCHECK(main_loop_->BelongsToCurrentThread());
462
463   return blink::WebSize(pipeline_metadata_.natural_size);
464 }
465
466 bool WebMediaPlayerImpl::paused() const {
467   DCHECK(main_loop_->BelongsToCurrentThread());
468
469   return pipeline_.GetPlaybackRate() == 0.0f;
470 }
471
472 bool WebMediaPlayerImpl::seeking() const {
473   DCHECK(main_loop_->BelongsToCurrentThread());
474
475   if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
476     return false;
477
478   return seeking_;
479 }
480
481 double WebMediaPlayerImpl::duration() const {
482   DCHECK(main_loop_->BelongsToCurrentThread());
483
484   if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
485     return std::numeric_limits<double>::quiet_NaN();
486
487   return GetPipelineDuration();
488 }
489
490 double WebMediaPlayerImpl::timelineOffset() const {
491   DCHECK(main_loop_->BelongsToCurrentThread());
492
493   if (pipeline_metadata_.timeline_offset.is_null())
494     return std::numeric_limits<double>::quiet_NaN();
495
496   return pipeline_metadata_.timeline_offset.ToJsTime();
497 }
498
499 double WebMediaPlayerImpl::currentTime() const {
500   DCHECK(main_loop_->BelongsToCurrentThread());
501   return (paused_ ? paused_time_ : pipeline_.GetMediaTime()).InSecondsF();
502 }
503
504 WebMediaPlayer::NetworkState WebMediaPlayerImpl::networkState() const {
505   DCHECK(main_loop_->BelongsToCurrentThread());
506   return network_state_;
507 }
508
509 WebMediaPlayer::ReadyState WebMediaPlayerImpl::readyState() const {
510   DCHECK(main_loop_->BelongsToCurrentThread());
511   return ready_state_;
512 }
513
514 const blink::WebTimeRanges& WebMediaPlayerImpl::buffered() {
515   DCHECK(main_loop_->BelongsToCurrentThread());
516   media::Ranges<base::TimeDelta> buffered_time_ranges =
517       pipeline_.GetBufferedTimeRanges();
518   buffered_data_source_host_.AddBufferedTimeRanges(
519       &buffered_time_ranges, pipeline_.GetMediaDuration());
520   blink::WebTimeRanges buffered_web_time_ranges(
521       ConvertToWebTimeRanges(buffered_time_ranges));
522   buffered_web_time_ranges_.swap(buffered_web_time_ranges);
523   return buffered_web_time_ranges_;
524 }
525
526 double WebMediaPlayerImpl::maxTimeSeekable() const {
527   DCHECK(main_loop_->BelongsToCurrentThread());
528
529   // If we haven't even gotten to ReadyStateHaveMetadata yet then just
530   // return 0 so that the seekable range is empty.
531   if (ready_state_ < WebMediaPlayer::ReadyStateHaveMetadata)
532     return 0.0;
533
534   // We don't support seeking in streaming media.
535   if (data_source_ && data_source_->IsStreaming())
536     return 0.0;
537   return duration();
538 }
539
540 bool WebMediaPlayerImpl::didLoadingProgress() const {
541   DCHECK(main_loop_->BelongsToCurrentThread());
542   bool pipeline_progress = pipeline_.DidLoadingProgress();
543   bool data_progress = buffered_data_source_host_.DidLoadingProgress();
544   return pipeline_progress || data_progress;
545 }
546
547 void WebMediaPlayerImpl::paint(WebCanvas* canvas,
548                                const WebRect& rect,
549                                unsigned char alpha) {
550   DCHECK(main_loop_->BelongsToCurrentThread());
551   TRACE_EVENT0("media", "WebMediaPlayerImpl:paint");
552
553   if (!accelerated_compositing_reported_) {
554     accelerated_compositing_reported_ = true;
555     // Normally paint() is only called in non-accelerated rendering, but there
556     // are exceptions such as webgl where compositing is used in the WebView but
557     // video frames are still rendered to a canvas.
558     UMA_HISTOGRAM_BOOLEAN(
559         "Media.AcceleratedCompositingActive",
560         frame_->view()->isAcceleratedCompositingActive());
561   }
562
563   // TODO(scherkus): Clarify paint() API contract to better understand when and
564   // why it's being called. For example, today paint() is called when:
565   //   - We haven't reached HAVE_CURRENT_DATA and need to paint black
566   //   - We're painting to a canvas
567   // See http://crbug.com/341225 http://crbug.com/342621 for details.
568   scoped_refptr<media::VideoFrame> video_frame =
569       GetCurrentFrameFromCompositor();
570
571   gfx::Rect gfx_rect(rect);
572   skcanvas_video_renderer_.Paint(video_frame.get(), canvas, gfx_rect, alpha);
573 }
574
575 bool WebMediaPlayerImpl::hasSingleSecurityOrigin() const {
576   if (data_source_)
577     return data_source_->HasSingleOrigin();
578   return true;
579 }
580
581 bool WebMediaPlayerImpl::didPassCORSAccessCheck() const {
582   if (data_source_)
583     return data_source_->DidPassCORSAccessCheck();
584   return false;
585 }
586
587 double WebMediaPlayerImpl::mediaTimeForTimeValue(double timeValue) const {
588   return ConvertSecondsToTimestamp(timeValue).InSecondsF();
589 }
590
591 unsigned WebMediaPlayerImpl::decodedFrameCount() const {
592   DCHECK(main_loop_->BelongsToCurrentThread());
593
594   media::PipelineStatistics stats = pipeline_.GetStatistics();
595   return stats.video_frames_decoded;
596 }
597
598 unsigned WebMediaPlayerImpl::droppedFrameCount() const {
599   DCHECK(main_loop_->BelongsToCurrentThread());
600
601   media::PipelineStatistics stats = pipeline_.GetStatistics();
602   return stats.video_frames_dropped;
603 }
604
605 unsigned WebMediaPlayerImpl::audioDecodedByteCount() const {
606   DCHECK(main_loop_->BelongsToCurrentThread());
607
608   media::PipelineStatistics stats = pipeline_.GetStatistics();
609   return stats.audio_bytes_decoded;
610 }
611
612 unsigned WebMediaPlayerImpl::videoDecodedByteCount() const {
613   DCHECK(main_loop_->BelongsToCurrentThread());
614
615   media::PipelineStatistics stats = pipeline_.GetStatistics();
616   return stats.video_bytes_decoded;
617 }
618
619 bool WebMediaPlayerImpl::copyVideoTextureToPlatformTexture(
620     blink::WebGraphicsContext3D* web_graphics_context,
621     unsigned int texture,
622     unsigned int level,
623     unsigned int internal_format,
624     unsigned int type,
625     bool premultiply_alpha,
626     bool flip_y) {
627   TRACE_EVENT0("media", "WebMediaPlayerImpl:copyVideoTextureToPlatformTexture");
628
629   scoped_refptr<media::VideoFrame> video_frame =
630       GetCurrentFrameFromCompositor();
631
632   if (!video_frame)
633     return false;
634   if (video_frame->format() != media::VideoFrame::NATIVE_TEXTURE)
635     return false;
636
637   const gpu::MailboxHolder* mailbox_holder = video_frame->mailbox_holder();
638   if (mailbox_holder->texture_target != GL_TEXTURE_2D)
639     return false;
640
641   // Since this method changes which texture is bound to the TEXTURE_2D target,
642   // ideally it would restore the currently-bound texture before returning.
643   // The cost of getIntegerv is sufficiently high, however, that we want to
644   // avoid it in user builds. As a result assume (below) that |texture| is
645   // bound when this method is called, and only verify this fact when
646   // DCHECK_IS_ON.
647 #if DCHECK_IS_ON
648   GLint bound_texture = 0;
649   web_graphics_context->getIntegerv(GL_TEXTURE_BINDING_2D, &bound_texture);
650   DCHECK_EQ(static_cast<GLuint>(bound_texture), texture);
651 #endif
652
653   uint32 source_texture = web_graphics_context->createTexture();
654
655   web_graphics_context->waitSyncPoint(mailbox_holder->sync_point);
656   web_graphics_context->bindTexture(GL_TEXTURE_2D, source_texture);
657   web_graphics_context->consumeTextureCHROMIUM(GL_TEXTURE_2D,
658                                                mailbox_holder->mailbox.name);
659
660   // The video is stored in a unmultiplied format, so premultiply
661   // if necessary.
662   web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM,
663                                     premultiply_alpha);
664   // Application itself needs to take care of setting the right flip_y
665   // value down to get the expected result.
666   // flip_y==true means to reverse the video orientation while
667   // flip_y==false means to keep the intrinsic orientation.
668   web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, flip_y);
669   web_graphics_context->copyTextureCHROMIUM(GL_TEXTURE_2D,
670                                             source_texture,
671                                             texture,
672                                             level,
673                                             internal_format,
674                                             type);
675   web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, false);
676   web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM,
677                                     false);
678
679   // Restore the state for TEXTURE_2D binding point as mentioned above.
680   web_graphics_context->bindTexture(GL_TEXTURE_2D, texture);
681
682   web_graphics_context->deleteTexture(source_texture);
683   web_graphics_context->flush();
684   video_frame->AppendReleaseSyncPoint(web_graphics_context->insertSyncPoint());
685   return true;
686 }
687
688 // Helper functions to report media EME related stats to UMA. They follow the
689 // convention of more commonly used macros UMA_HISTOGRAM_ENUMERATION and
690 // UMA_HISTOGRAM_COUNTS. The reason that we cannot use those macros directly is
691 // that UMA_* macros require the names to be constant throughout the process'
692 // lifetime.
693 static void EmeUMAHistogramEnumeration(const std::string& key_system,
694                                        const std::string& method,
695                                        int sample,
696                                        int boundary_value) {
697   base::LinearHistogram::FactoryGet(
698       kMediaEme + KeySystemNameForUMA(key_system) + "." + method,
699       1, boundary_value, boundary_value + 1,
700       base::Histogram::kUmaTargetedHistogramFlag)->Add(sample);
701 }
702
703 static void EmeUMAHistogramCounts(const std::string& key_system,
704                                   const std::string& method,
705                                   int sample) {
706   // Use the same parameters as UMA_HISTOGRAM_COUNTS.
707   base::Histogram::FactoryGet(
708       kMediaEme + KeySystemNameForUMA(key_system) + "." + method,
709       1, 1000000, 50, base::Histogram::kUmaTargetedHistogramFlag)->Add(sample);
710 }
711
712 // Helper enum for reporting generateKeyRequest/addKey histograms.
713 enum MediaKeyException {
714   kUnknownResultId,
715   kSuccess,
716   kKeySystemNotSupported,
717   kInvalidPlayerState,
718   kMaxMediaKeyException
719 };
720
721 static MediaKeyException MediaKeyExceptionForUMA(
722     WebMediaPlayer::MediaKeyException e) {
723   switch (e) {
724     case WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported:
725       return kKeySystemNotSupported;
726     case WebMediaPlayer::MediaKeyExceptionInvalidPlayerState:
727       return kInvalidPlayerState;
728     case WebMediaPlayer::MediaKeyExceptionNoError:
729       return kSuccess;
730     default:
731       return kUnknownResultId;
732   }
733 }
734
735 // Helper for converting |key_system| name and exception |e| to a pair of enum
736 // values from above, for reporting to UMA.
737 static void ReportMediaKeyExceptionToUMA(const std::string& method,
738                                          const std::string& key_system,
739                                          WebMediaPlayer::MediaKeyException e) {
740   MediaKeyException result_id = MediaKeyExceptionForUMA(e);
741   DCHECK_NE(result_id, kUnknownResultId) << e;
742   EmeUMAHistogramEnumeration(
743       key_system, method, result_id, kMaxMediaKeyException);
744 }
745
746 // Convert a WebString to ASCII, falling back on an empty string in the case
747 // of a non-ASCII string.
748 static std::string ToASCIIOrEmpty(const blink::WebString& string) {
749   return base::IsStringASCII(string) ? base::UTF16ToASCII(string)
750                                      : std::string();
751 }
752
753 WebMediaPlayer::MediaKeyException
754 WebMediaPlayerImpl::generateKeyRequest(const WebString& key_system,
755                                        const unsigned char* init_data,
756                                        unsigned init_data_length) {
757   DVLOG(1) << "generateKeyRequest: " << base::string16(key_system) << ": "
758            << std::string(reinterpret_cast<const char*>(init_data),
759                           static_cast<size_t>(init_data_length));
760
761   std::string ascii_key_system =
762       GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system));
763
764   WebMediaPlayer::MediaKeyException e =
765       GenerateKeyRequestInternal(ascii_key_system, init_data, init_data_length);
766   ReportMediaKeyExceptionToUMA("generateKeyRequest", ascii_key_system, e);
767   return e;
768 }
769
770 // Guess the type of |init_data|. This is only used to handle some corner cases
771 // so we keep it as simple as possible without breaking major use cases.
772 static std::string GuessInitDataType(const unsigned char* init_data,
773                                      unsigned init_data_length) {
774   // Most WebM files use KeyId of 16 bytes. MP4 init data are always >16 bytes.
775   if (init_data_length == 16)
776     return "video/webm";
777
778   return "video/mp4";
779 }
780
781 WebMediaPlayer::MediaKeyException
782 WebMediaPlayerImpl::GenerateKeyRequestInternal(const std::string& key_system,
783                                                const unsigned char* init_data,
784                                                unsigned init_data_length) {
785   DCHECK(main_loop_->BelongsToCurrentThread());
786
787   if (!IsConcreteSupportedKeySystem(key_system))
788     return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
789
790   // We do not support run-time switching between key systems for now.
791   if (current_key_system_.empty()) {
792     if (!proxy_decryptor_) {
793       proxy_decryptor_.reset(new ProxyDecryptor(
794 #if defined(ENABLE_PEPPER_CDMS)
795           // Create() must be called synchronously as |frame_| may not be
796           // valid afterwards.
797           base::Bind(&PepperCdmWrapperImpl::Create, frame_),
798 #endif
799           BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnKeyAdded),
800           BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnKeyError),
801           BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnKeyMessage)));
802     }
803
804     GURL security_origin(frame_->document().securityOrigin().toString());
805     if (!proxy_decryptor_->InitializeCDM(key_system, security_origin))
806       return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
807
808     if (proxy_decryptor_ && !decryptor_ready_cb_.is_null()) {
809       base::ResetAndReturn(&decryptor_ready_cb_)
810           .Run(proxy_decryptor_->GetDecryptor());
811     }
812
813     current_key_system_ = key_system;
814   } else if (key_system != current_key_system_) {
815     return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState;
816   }
817
818   std::string init_data_type = init_data_type_;
819   if (init_data_type.empty())
820     init_data_type = GuessInitDataType(init_data, init_data_length);
821
822   // TODO(xhwang): We assume all streams are from the same container (thus have
823   // the same "type") for now. In the future, the "type" should be passed down
824   // from the application.
825   if (!proxy_decryptor_->GenerateKeyRequest(
826            init_data_type, init_data, init_data_length)) {
827     current_key_system_.clear();
828     return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
829   }
830
831   return WebMediaPlayer::MediaKeyExceptionNoError;
832 }
833
834 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::addKey(
835     const WebString& key_system,
836     const unsigned char* key,
837     unsigned key_length,
838     const unsigned char* init_data,
839     unsigned init_data_length,
840     const WebString& session_id) {
841   DVLOG(1) << "addKey: " << base::string16(key_system) << ": "
842            << std::string(reinterpret_cast<const char*>(key),
843                           static_cast<size_t>(key_length)) << ", "
844            << std::string(reinterpret_cast<const char*>(init_data),
845                           static_cast<size_t>(init_data_length)) << " ["
846            << base::string16(session_id) << "]";
847
848   std::string ascii_key_system =
849       GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system));
850   std::string ascii_session_id = ToASCIIOrEmpty(session_id);
851
852   WebMediaPlayer::MediaKeyException e = AddKeyInternal(ascii_key_system,
853                                                        key,
854                                                        key_length,
855                                                        init_data,
856                                                        init_data_length,
857                                                        ascii_session_id);
858   ReportMediaKeyExceptionToUMA("addKey", ascii_key_system, e);
859   return e;
860 }
861
862 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::AddKeyInternal(
863     const std::string& key_system,
864     const unsigned char* key,
865     unsigned key_length,
866     const unsigned char* init_data,
867     unsigned init_data_length,
868     const std::string& session_id) {
869   DCHECK(key);
870   DCHECK_GT(key_length, 0u);
871
872   if (!IsConcreteSupportedKeySystem(key_system))
873     return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
874
875   if (current_key_system_.empty() || key_system != current_key_system_)
876     return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState;
877
878   proxy_decryptor_->AddKey(
879       key, key_length, init_data, init_data_length, session_id);
880   return WebMediaPlayer::MediaKeyExceptionNoError;
881 }
882
883 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::cancelKeyRequest(
884     const WebString& key_system,
885     const WebString& session_id) {
886   DVLOG(1) << "cancelKeyRequest: " << base::string16(key_system) << ": "
887            << " [" << base::string16(session_id) << "]";
888
889   std::string ascii_key_system =
890       GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system));
891   std::string ascii_session_id = ToASCIIOrEmpty(session_id);
892
893   WebMediaPlayer::MediaKeyException e =
894       CancelKeyRequestInternal(ascii_key_system, ascii_session_id);
895   ReportMediaKeyExceptionToUMA("cancelKeyRequest", ascii_key_system, e);
896   return e;
897 }
898
899 WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::CancelKeyRequestInternal(
900     const std::string& key_system,
901     const std::string& session_id) {
902   if (!IsConcreteSupportedKeySystem(key_system))
903     return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
904
905   if (current_key_system_.empty() || key_system != current_key_system_)
906     return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState;
907
908   proxy_decryptor_->CancelKeyRequest(session_id);
909   return WebMediaPlayer::MediaKeyExceptionNoError;
910 }
911
912 void WebMediaPlayerImpl::setContentDecryptionModule(
913     blink::WebContentDecryptionModule* cdm) {
914   DCHECK(main_loop_->BelongsToCurrentThread());
915
916   // TODO(xhwang): Support setMediaKeys(0) if necessary: http://crbug.com/330324
917   if (!cdm)
918     return;
919
920   web_cdm_ = ToWebContentDecryptionModuleImpl(cdm);
921
922   if (web_cdm_ && !decryptor_ready_cb_.is_null())
923     base::ResetAndReturn(&decryptor_ready_cb_).Run(web_cdm_->GetDecryptor());
924 }
925
926 void WebMediaPlayerImpl::InvalidateOnMainThread() {
927   DCHECK(main_loop_->BelongsToCurrentThread());
928   TRACE_EVENT0("media", "WebMediaPlayerImpl::InvalidateOnMainThread");
929
930   client_->repaint();
931 }
932
933 void WebMediaPlayerImpl::OnPipelineSeek(PipelineStatus status) {
934   DCHECK(main_loop_->BelongsToCurrentThread());
935   starting_ = false;
936   seeking_ = false;
937   if (pending_seek_) {
938     pending_seek_ = false;
939     seek(pending_seek_seconds_);
940     return;
941   }
942
943   if (status != media::PIPELINE_OK) {
944     OnPipelineError(status);
945     return;
946   }
947
948   // Update our paused time.
949   if (paused_)
950     paused_time_ = pipeline_.GetMediaTime();
951
952   client_->timeChanged();
953 }
954
955 void WebMediaPlayerImpl::OnPipelineEnded() {
956   DCHECK(main_loop_->BelongsToCurrentThread());
957   client_->timeChanged();
958 }
959
960 void WebMediaPlayerImpl::OnPipelineError(PipelineStatus error) {
961   DCHECK(main_loop_->BelongsToCurrentThread());
962   DCHECK_NE(error, media::PIPELINE_OK);
963
964   if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) {
965     // Any error that occurs before reaching ReadyStateHaveMetadata should
966     // be considered a format error.
967     SetNetworkState(WebMediaPlayer::NetworkStateFormatError);
968
969     // TODO(scherkus): This should be handled by HTMLMediaElement and controls
970     // should know when to invalidate themselves http://crbug.com/337015
971     InvalidateOnMainThread();
972     return;
973   }
974
975   SetNetworkState(PipelineErrorToNetworkState(error));
976
977   if (error == media::PIPELINE_ERROR_DECRYPT)
978     EmeUMAHistogramCounts(current_key_system_, "DecryptError", 1);
979
980   // TODO(scherkus): This should be handled by HTMLMediaElement and controls
981   // should know when to invalidate themselves http://crbug.com/337015
982   InvalidateOnMainThread();
983 }
984
985 void WebMediaPlayerImpl::OnPipelineMetadata(
986     media::PipelineMetadata metadata) {
987   DVLOG(1) << "OnPipelineMetadata";
988
989   pipeline_metadata_ = metadata;
990
991   SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
992
993   if (hasVideo()) {
994     DCHECK(!video_weblayer_);
995     video_weblayer_.reset(
996         new webkit::WebLayerImpl(cc::VideoLayer::Create(compositor_)));
997     video_weblayer_->setOpaque(opaque_);
998     client_->setWebLayer(video_weblayer_.get());
999   }
1000
1001   // TODO(scherkus): This should be handled by HTMLMediaElement and controls
1002   // should know when to invalidate themselves http://crbug.com/337015
1003   InvalidateOnMainThread();
1004 }
1005
1006 void WebMediaPlayerImpl::OnPipelinePrerollCompleted() {
1007   DVLOG(1) << "OnPipelinePrerollCompleted";
1008
1009   // Only transition to ReadyStateHaveEnoughData if we don't have
1010   // any pending seeks because the transition can cause Blink to
1011   // report that the most recent seek has completed.
1012   if (!pending_seek_) {
1013     SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData);
1014
1015     // TODO(scherkus): This should be handled by HTMLMediaElement and controls
1016     // should know when to invalidate themselves http://crbug.com/337015
1017     InvalidateOnMainThread();
1018   }
1019 }
1020
1021 void WebMediaPlayerImpl::OnDemuxerOpened() {
1022   DCHECK(main_loop_->BelongsToCurrentThread());
1023   client_->mediaSourceOpened(new WebMediaSourceImpl(
1024       chunk_demuxer_, base::Bind(&LogMediaSourceError, media_log_)));
1025 }
1026
1027 void WebMediaPlayerImpl::OnKeyAdded(const std::string& session_id) {
1028   DCHECK(main_loop_->BelongsToCurrentThread());
1029   EmeUMAHistogramCounts(current_key_system_, "KeyAdded", 1);
1030   client_->keyAdded(
1031       WebString::fromUTF8(GetPrefixedKeySystemName(current_key_system_)),
1032       WebString::fromUTF8(session_id));
1033 }
1034
1035 void WebMediaPlayerImpl::OnNeedKey(const std::string& type,
1036                                    const std::vector<uint8>& init_data) {
1037   DCHECK(main_loop_->BelongsToCurrentThread());
1038
1039   // Do not fire NeedKey event if encrypted media is not enabled.
1040   if (!blink::WebRuntimeFeatures::isPrefixedEncryptedMediaEnabled() &&
1041       !blink::WebRuntimeFeatures::isEncryptedMediaEnabled()) {
1042     return;
1043   }
1044
1045   UMA_HISTOGRAM_COUNTS(kMediaEme + std::string("NeedKey"), 1);
1046
1047   DCHECK(init_data_type_.empty() || type.empty() || type == init_data_type_);
1048   if (init_data_type_.empty())
1049     init_data_type_ = type;
1050
1051   const uint8* init_data_ptr = init_data.empty() ? NULL : &init_data[0];
1052   client_->keyNeeded(
1053       WebString::fromUTF8(type), init_data_ptr, init_data.size());
1054 }
1055
1056 void WebMediaPlayerImpl::OnAddTextTrack(
1057     const media::TextTrackConfig& config,
1058     const media::AddTextTrackDoneCB& done_cb) {
1059   DCHECK(main_loop_->BelongsToCurrentThread());
1060
1061   const WebInbandTextTrackImpl::Kind web_kind =
1062       static_cast<WebInbandTextTrackImpl::Kind>(config.kind());
1063   const blink::WebString web_label =
1064       blink::WebString::fromUTF8(config.label());
1065   const blink::WebString web_language =
1066       blink::WebString::fromUTF8(config.language());
1067   const blink::WebString web_id =
1068       blink::WebString::fromUTF8(config.id());
1069
1070   scoped_ptr<WebInbandTextTrackImpl> web_inband_text_track(
1071       new WebInbandTextTrackImpl(web_kind, web_label, web_language, web_id,
1072                                  text_track_index_++));
1073
1074   scoped_ptr<media::TextTrack> text_track(
1075       new TextTrackImpl(main_loop_, client_, web_inband_text_track.Pass()));
1076
1077   done_cb.Run(text_track.Pass());
1078 }
1079
1080 void WebMediaPlayerImpl::OnKeyError(const std::string& session_id,
1081                                     media::MediaKeys::KeyError error_code,
1082                                     uint32 system_code) {
1083   DCHECK(main_loop_->BelongsToCurrentThread());
1084
1085   EmeUMAHistogramEnumeration(current_key_system_, "KeyError",
1086                              error_code, media::MediaKeys::kMaxKeyError);
1087
1088   unsigned short short_system_code = 0;
1089   if (system_code > std::numeric_limits<unsigned short>::max()) {
1090     LOG(WARNING) << "system_code exceeds unsigned short limit.";
1091     short_system_code = std::numeric_limits<unsigned short>::max();
1092   } else {
1093     short_system_code = static_cast<unsigned short>(system_code);
1094   }
1095
1096   client_->keyError(
1097       WebString::fromUTF8(GetPrefixedKeySystemName(current_key_system_)),
1098       WebString::fromUTF8(session_id),
1099       static_cast<blink::WebMediaPlayerClient::MediaKeyErrorCode>(error_code),
1100       short_system_code);
1101 }
1102
1103 void WebMediaPlayerImpl::OnKeyMessage(const std::string& session_id,
1104                                       const std::vector<uint8>& message,
1105                                       const std::string& default_url) {
1106   DCHECK(main_loop_->BelongsToCurrentThread());
1107
1108   const GURL default_url_gurl(default_url);
1109   DLOG_IF(WARNING, !default_url.empty() && !default_url_gurl.is_valid())
1110       << "Invalid URL in default_url: " << default_url;
1111
1112   client_->keyMessage(
1113       WebString::fromUTF8(GetPrefixedKeySystemName(current_key_system_)),
1114       WebString::fromUTF8(session_id),
1115       message.empty() ? NULL : &message[0],
1116       message.size(),
1117       default_url_gurl);
1118 }
1119
1120 void WebMediaPlayerImpl::DataSourceInitialized(const GURL& gurl, bool success) {
1121   DCHECK(main_loop_->BelongsToCurrentThread());
1122
1123   if (!success) {
1124     SetNetworkState(WebMediaPlayer::NetworkStateFormatError);
1125
1126     // TODO(scherkus): This should be handled by HTMLMediaElement and controls
1127     // should know when to invalidate themselves http://crbug.com/337015
1128     InvalidateOnMainThread();
1129     return;
1130   }
1131
1132   StartPipeline();
1133 }
1134
1135 void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading) {
1136   if (!is_downloading && network_state_ == WebMediaPlayer::NetworkStateLoading)
1137     SetNetworkState(WebMediaPlayer::NetworkStateIdle);
1138   else if (is_downloading && network_state_ == WebMediaPlayer::NetworkStateIdle)
1139     SetNetworkState(WebMediaPlayer::NetworkStateLoading);
1140   media_log_->AddEvent(
1141       media_log_->CreateBooleanEvent(
1142           media::MediaLogEvent::NETWORK_ACTIVITY_SET,
1143           "is_downloading_data", is_downloading));
1144 }
1145
1146 void WebMediaPlayerImpl::StartPipeline() {
1147   DCHECK(main_loop_->BelongsToCurrentThread());
1148   const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
1149
1150   // Keep track if this is a MSE or non-MSE playback.
1151   UMA_HISTOGRAM_BOOLEAN("Media.MSE.Playback",
1152                         (load_type_ == LoadTypeMediaSource));
1153
1154   media::LogCB mse_log_cb;
1155
1156   // Figure out which demuxer to use.
1157   if (load_type_ != LoadTypeMediaSource) {
1158     DCHECK(!chunk_demuxer_);
1159     DCHECK(data_source_);
1160
1161     demuxer_.reset(new media::FFmpegDemuxer(
1162         media_loop_, data_source_.get(),
1163         BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNeedKey),
1164         media_log_));
1165   } else {
1166     DCHECK(!chunk_demuxer_);
1167     DCHECK(!data_source_);
1168
1169     mse_log_cb = base::Bind(&LogMediaSourceError, media_log_);
1170
1171     chunk_demuxer_ = new media::ChunkDemuxer(
1172         BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDemuxerOpened),
1173         BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNeedKey),
1174         mse_log_cb,
1175         true);
1176     demuxer_.reset(chunk_demuxer_);
1177   }
1178
1179   scoped_ptr<media::FilterCollection> filter_collection(
1180       new media::FilterCollection());
1181   filter_collection->SetDemuxer(demuxer_.get());
1182
1183   media::SetDecryptorReadyCB set_decryptor_ready_cb =
1184       BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::SetDecryptorReadyCB);
1185
1186   // Create our audio decoders and renderer.
1187   ScopedVector<media::AudioDecoder> audio_decoders;
1188   audio_decoders.push_back(new media::FFmpegAudioDecoder(media_loop_,
1189                                                          mse_log_cb));
1190   audio_decoders.push_back(new media::OpusAudioDecoder(media_loop_));
1191
1192   scoped_ptr<media::AudioRenderer> audio_renderer(new media::AudioRendererImpl(
1193       media_loop_,
1194       audio_source_provider_.get(),
1195       audio_decoders.Pass(),
1196       set_decryptor_ready_cb,
1197       RenderThreadImpl::current()->GetAudioHardwareConfig()));
1198   filter_collection->SetAudioRenderer(audio_renderer.Pass());
1199
1200   // Create our video decoders and renderer.
1201   ScopedVector<media::VideoDecoder> video_decoders;
1202
1203   if (gpu_factories_.get()) {
1204     video_decoders.push_back(
1205         new media::GpuVideoDecoder(gpu_factories_, media_log_));
1206   }
1207
1208 #if !defined(MEDIA_DISABLE_LIBVPX)
1209   video_decoders.push_back(new media::VpxVideoDecoder(media_loop_));
1210 #endif  // !defined(MEDIA_DISABLE_LIBVPX)
1211
1212   video_decoders.push_back(new media::FFmpegVideoDecoder(media_loop_));
1213
1214   scoped_ptr<media::VideoRenderer> video_renderer(
1215       new media::VideoRendererImpl(
1216           media_loop_,
1217           video_decoders.Pass(),
1218           set_decryptor_ready_cb,
1219           base::Bind(&WebMediaPlayerImpl::FrameReady, base::Unretained(this)),
1220           true));
1221   filter_collection->SetVideoRenderer(video_renderer.Pass());
1222
1223   if (cmd_line->HasSwitch(switches::kEnableInbandTextTracks)) {
1224     scoped_ptr<media::TextRenderer> text_renderer(
1225         new media::TextRenderer(
1226             media_loop_,
1227             BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnAddTextTrack)));
1228
1229     filter_collection->SetTextRenderer(text_renderer.Pass());
1230   }
1231
1232   // ... and we're ready to go!
1233   starting_ = true;
1234   pipeline_.Start(
1235       filter_collection.Pass(),
1236       BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineEnded),
1237       BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineError),
1238       BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineSeek),
1239       BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineMetadata),
1240       BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelinePrerollCompleted),
1241       BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDurationChanged));
1242 }
1243
1244 void WebMediaPlayerImpl::SetNetworkState(WebMediaPlayer::NetworkState state) {
1245   DCHECK(main_loop_->BelongsToCurrentThread());
1246   DVLOG(1) << "SetNetworkState: " << state;
1247   network_state_ = state;
1248   // Always notify to ensure client has the latest value.
1249   client_->networkStateChanged();
1250 }
1251
1252 void WebMediaPlayerImpl::SetReadyState(WebMediaPlayer::ReadyState state) {
1253   DCHECK(main_loop_->BelongsToCurrentThread());
1254   DVLOG(1) << "SetReadyState: " << state;
1255
1256   if (state == WebMediaPlayer::ReadyStateHaveEnoughData &&
1257       is_local_source_ &&
1258       network_state_ == WebMediaPlayer::NetworkStateLoading)
1259     SetNetworkState(WebMediaPlayer::NetworkStateLoaded);
1260
1261   ready_state_ = state;
1262   // Always notify to ensure client has the latest value.
1263   client_->readyStateChanged();
1264 }
1265
1266 blink::WebAudioSourceProvider* WebMediaPlayerImpl::audioSourceProvider() {
1267   return audio_source_provider_.get();
1268 }
1269
1270 void WebMediaPlayerImpl::IncrementExternallyAllocatedMemory() {
1271   DCHECK(main_loop_->BelongsToCurrentThread());
1272   incremented_externally_allocated_memory_ = true;
1273   v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
1274       kPlayerExtraMemory);
1275 }
1276
1277 double WebMediaPlayerImpl::GetPipelineDuration() const {
1278   base::TimeDelta duration = pipeline_.GetMediaDuration();
1279
1280   // Return positive infinity if the resource is unbounded.
1281   // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
1282   if (duration == media::kInfiniteDuration())
1283     return std::numeric_limits<double>::infinity();
1284
1285   return duration.InSecondsF();
1286 }
1287
1288 void WebMediaPlayerImpl::OnDurationChanged() {
1289   if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
1290     return;
1291
1292   client_->durationChanged();
1293 }
1294
1295 void WebMediaPlayerImpl::OnNaturalSizeChanged(gfx::Size size) {
1296   DCHECK(main_loop_->BelongsToCurrentThread());
1297   DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
1298   TRACE_EVENT0("media", "WebMediaPlayerImpl::OnNaturalSizeChanged");
1299
1300   media_log_->AddEvent(
1301       media_log_->CreateVideoSizeSetEvent(size.width(), size.height()));
1302   pipeline_metadata_.natural_size = size;
1303
1304   client_->sizeChanged();
1305 }
1306
1307 void WebMediaPlayerImpl::OnOpacityChanged(bool opaque) {
1308   DCHECK(main_loop_->BelongsToCurrentThread());
1309   DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
1310
1311   opaque_ = opaque;
1312   if (video_weblayer_)
1313     video_weblayer_->setOpaque(opaque_);
1314 }
1315
1316 void WebMediaPlayerImpl::FrameReady(
1317     const scoped_refptr<media::VideoFrame>& frame) {
1318   compositor_task_runner_->PostTask(
1319       FROM_HERE,
1320       base::Bind(&VideoFrameCompositor::UpdateCurrentFrame,
1321                  base::Unretained(compositor_),
1322                  frame));
1323 }
1324
1325 void WebMediaPlayerImpl::SetDecryptorReadyCB(
1326      const media::DecryptorReadyCB& decryptor_ready_cb) {
1327   DCHECK(main_loop_->BelongsToCurrentThread());
1328
1329   // Cancels the previous decryptor request.
1330   if (decryptor_ready_cb.is_null()) {
1331     if (!decryptor_ready_cb_.is_null())
1332       base::ResetAndReturn(&decryptor_ready_cb_).Run(NULL);
1333     return;
1334   }
1335
1336   // TODO(xhwang): Support multiple decryptor notification request (e.g. from
1337   // video and audio). The current implementation is okay for the current
1338   // media pipeline since we initialize audio and video decoders in sequence.
1339   // But WebMediaPlayerImpl should not depend on media pipeline's implementation
1340   // detail.
1341   DCHECK(decryptor_ready_cb_.is_null());
1342
1343   // Mixed use of prefixed and unprefixed EME APIs is disallowed by Blink.
1344   DCHECK(!proxy_decryptor_ || !web_cdm_);
1345
1346   if (proxy_decryptor_) {
1347     decryptor_ready_cb.Run(proxy_decryptor_->GetDecryptor());
1348     return;
1349   }
1350
1351   if (web_cdm_) {
1352     decryptor_ready_cb.Run(web_cdm_->GetDecryptor());
1353     return;
1354   }
1355
1356   decryptor_ready_cb_ = decryptor_ready_cb;
1357 }
1358
1359 static void GetCurrentFrameAndSignal(
1360     VideoFrameCompositor* compositor,
1361     scoped_refptr<media::VideoFrame>* video_frame_out,
1362     base::WaitableEvent* event) {
1363   TRACE_EVENT0("media", "GetCurrentFrameAndSignal");
1364   *video_frame_out = compositor->GetCurrentFrame();
1365   event->Signal();
1366 }
1367
1368 scoped_refptr<media::VideoFrame>
1369 WebMediaPlayerImpl::GetCurrentFrameFromCompositor() {
1370   TRACE_EVENT0("media", "WebMediaPlayerImpl::GetCurrentFrameFromCompositor");
1371   if (compositor_task_runner_->BelongsToCurrentThread())
1372     return compositor_->GetCurrentFrame();
1373
1374   // Use a posted task and waitable event instead of a lock otherwise
1375   // WebGL/Canvas can see different content than what the compositor is seeing.
1376   scoped_refptr<media::VideoFrame> video_frame;
1377   base::WaitableEvent event(false, false);
1378   compositor_task_runner_->PostTask(FROM_HERE,
1379                                     base::Bind(&GetCurrentFrameAndSignal,
1380                                                base::Unretained(compositor_),
1381                                                &video_frame,
1382                                                &event));
1383   event.Wait();
1384   return video_frame;
1385 }
1386
1387 }  // namespace content