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