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