1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
57dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "content/renderer/media/webmediaplayer_ms.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <limits>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/callback.h"
119ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/histogram.h"
1303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "cc/blink/web_layer_impl.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/layers/video_layer.h"
15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/renderer/render_view.h"
167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "content/renderer/media/media_stream_audio_renderer.h"
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/renderer/media/media_stream_renderer_factory.h"
187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "content/renderer/media/video_frame_provider.h"
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/renderer/render_frame_impl.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "media/base/media_log.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "media/base/video_frame.h"
226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "media/base/video_rotation.h"
23effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "media/base/video_util.h"
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "media/blink/webmediaplayer_delegate.h"
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "media/blink/webmediaplayer_util.h"
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "third_party/WebKit/public/platform/WebMediaPlayerClient.h"
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "third_party/WebKit/public/platform/WebRect.h"
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "third_party/WebKit/public/platform/WebSize.h"
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "third_party/WebKit/public/platform/WebURL.h"
307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "third_party/WebKit/public/web/WebFrame.h"
317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "third_party/WebKit/public/web/WebView.h"
32effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "third_party/skia/include/core/SkBitmap.h"
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)using blink::WebCanvas;
35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)using blink::WebMediaPlayer;
36f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)using blink::WebRect;
37f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)using blink::WebSize;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
39effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochnamespace {
40effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
41effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// This function copies a YV12 or NATIVE_TEXTURE to a new YV12
42effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// media::VideoFrame.
43effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochscoped_refptr<media::VideoFrame> CopyFrameToYV12(
44effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    const scoped_refptr<media::VideoFrame>& frame) {
45effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DCHECK(frame->format() == media::VideoFrame::YV12 ||
46c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch         frame->format() == media::VideoFrame::I420 ||
47effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch         frame->format() == media::VideoFrame::NATIVE_TEXTURE);
48effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  scoped_refptr<media::VideoFrame> new_frame =
49effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      media::VideoFrame::CreateFrame(media::VideoFrame::YV12,
50effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                     frame->coded_size(),
51effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                     frame->visible_rect(),
52effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                     frame->natural_size(),
53c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                     frame->timestamp());
54effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
55effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (frame->format() == media::VideoFrame::NATIVE_TEXTURE) {
56effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    SkBitmap bitmap;
57effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    bitmap.allocN32Pixels(frame->visible_rect().width(),
58effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                          frame->visible_rect().height());
59effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    frame->ReadPixelsFromNativeTexture(bitmap);
60effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
61effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    media::CopyRGBToVideoFrame(
62effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        reinterpret_cast<uint8*>(bitmap.getPixels()),
63effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        bitmap.rowBytes(),
64effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        frame->visible_rect(),
65effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        new_frame.get());
66effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  } else {
67effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    size_t number_of_planes =
68effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        media::VideoFrame::NumPlanes(frame->format());
69effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    for (size_t i = 0; i < number_of_planes; ++i) {
70effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      media::CopyPlane(i, frame->data(i), frame->stride(i),
71effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                       frame->rows(i), new_frame.get());
72effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    }
73effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
74effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return new_frame;
75effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
76effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
77effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}  // anonymous namespace
78effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochnamespace content {
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WebMediaPlayerMS::WebMediaPlayerMS(
82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    blink::WebFrame* frame,
83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    blink::WebMediaPlayerClient* client,
841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    base::WeakPtr<media::WebMediaPlayerDelegate> delegate,
85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    media::MediaLog* media_log,
86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    scoped_ptr<MediaStreamRendererFactory> factory)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : frame_(frame),
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      network_state_(WebMediaPlayer::NetworkStateEmpty),
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ready_state_(WebMediaPlayer::ReadyStateHaveNothing),
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      buffered_(static_cast<size_t>(1)),
916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      volume_(1.0f),
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      client_(client),
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      delegate_(delegate),
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      paused_(true),
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      current_frame_used_(false),
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pending_repaint_(false),
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      video_frame_provider_client_(NULL),
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      received_first_frame_(false),
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      total_frame_count_(0),
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      dropped_frame_count_(0),
101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      media_log_(media_log),
102cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      renderer_factory_(factory.Pass()) {
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DVLOG(1) << "WebMediaPlayerMS::ctor";
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  media_log_->AddEvent(
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      media_log_->CreateEvent(media::MediaLogEvent::WEBMEDIAPLAYER_CREATED));
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WebMediaPlayerMS::~WebMediaPlayerMS() {
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DVLOG(1) << "WebMediaPlayerMS::dtor";
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SetVideoFrameProviderClient(NULL);
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetClient()->setWebLayer(NULL);
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (video_frame_provider_.get())
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    video_frame_provider_->Stop();
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (audio_renderer_.get())
11958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    audio_renderer_->Stop();
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  media_log_->AddEvent(
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      media_log_->CreateEvent(media::MediaLogEvent::WEBMEDIAPLAYER_DESTROYED));
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (delegate_.get())
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delegate_->PlayerGone(this);
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void WebMediaPlayerMS::load(LoadType load_type,
129f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                            const blink::WebURL& url,
1303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                            CORSMode cors_mode) {
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DVLOG(1) << "WebMediaPlayerMS::load";
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // TODO(acolwell): Change this to DCHECK_EQ(load_type,
1353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // LoadTypeMediaStream) once Blink-side changes land.
1363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK_NE(load_type, LoadTypeMediaSource);
1373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL gurl(url);
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetNetworkState(WebMediaPlayer::NetworkStateLoading);
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetReadyState(WebMediaPlayer::ReadyStateHaveNothing);
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  media_log_->AddEvent(media_log_->CreateLoadEvent(url.spec()));
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
144cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  video_frame_provider_ = renderer_factory_->GetVideoFrameProvider(
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      url,
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&WebMediaPlayerMS::OnSourceError, AsWeakPtr()),
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&WebMediaPlayerMS::OnFrameAvailable, AsWeakPtr()));
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
149cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  RenderFrame* frame = RenderFrame::FromWebFrame(frame_);
150cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  audio_renderer_ = renderer_factory_->GetAudioRenderer(
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    url,
152cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    frame->GetRenderView()->GetRoutingID(),
153cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    frame->GetRoutingID());
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (video_frame_provider_.get() || audio_renderer_.get()) {
1566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    if (audio_renderer_.get()) {
1576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      audio_renderer_->SetVolume(volume_);
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      audio_renderer_->Start();
1596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    }
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (video_frame_provider_.get()) {
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      video_frame_provider_->Start();
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // This is audio-only mode.
165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DCHECK(audio_renderer_.get());
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData);
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SetNetworkState(WebMediaPlayer::NetworkStateNetworkError);
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void WebMediaPlayerMS::play() {
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DVLOG(1) << "WebMediaPlayerMS::play";
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
17890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (paused_) {
179868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (video_frame_provider_.get())
18090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      video_frame_provider_->Play();
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
182868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (audio_renderer_.get())
18390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      audio_renderer_->Play();
18490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
185868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (delegate_.get())
18690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      delegate_->DidPlay(this);
18790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  paused_ = false;
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  media_log_->AddEvent(media_log_->CreateEvent(media::MediaLogEvent::PLAY));
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void WebMediaPlayerMS::pause() {
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DVLOG(1) << "WebMediaPlayerMS::pause";
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
198868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (video_frame_provider_.get())
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    video_frame_provider_->Pause();
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
20190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!paused_) {
202868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (audio_renderer_.get())
20390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      audio_renderer_->Pause();
20490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
205868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (delegate_.get())
20690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      delegate_->DidPause(this);
20790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  paused_ = true;
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  media_log_->AddEvent(media_log_->CreateEvent(media::MediaLogEvent::PAUSE));
212effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
2131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!current_frame_.get())
214effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
215effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
216effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Copy the frame so that rendering can show the last received frame.
217effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // The original frame must not be referenced when the player is paused since
218effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // there might be a finite number of available buffers. E.g, video that
219effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // originates from a video camera.
220effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  scoped_refptr<media::VideoFrame> new_frame = CopyFrameToYV12(current_frame_);
221effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  base::AutoLock auto_lock(current_frame_lock_);
222effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  current_frame_ = new_frame;
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool WebMediaPlayerMS::supportsSave() const {
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
230c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void WebMediaPlayerMS::seek(double seconds) {
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
234c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void WebMediaPlayerMS::setRate(double rate) {
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
238c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void WebMediaPlayerMS::setVolume(double volume) {
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DVLOG(1) << "WebMediaPlayerMS::setVolume(volume=" << volume << ")";
2416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  volume_ = volume;
2426e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (audio_renderer_.get())
2436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    audio_renderer_->SetVolume(volume_);
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void WebMediaPlayerMS::setPreload(WebMediaPlayer::Preload preload) {
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool WebMediaPlayerMS::hasVideo() const {
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
252868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return (video_frame_provider_.get() != NULL);
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool WebMediaPlayerMS::hasAudio() const {
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
257868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return (audio_renderer_.get() != NULL);
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
260f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)blink::WebSize WebMediaPlayerMS::naturalSize() const {
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Size size;
264868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (current_frame_.get())
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    size = current_frame_->natural_size();
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DVLOG(3) << "WebMediaPlayerMS::naturalSize, " << size.ToString();
267f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return blink::WebSize(size);
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool WebMediaPlayerMS::paused() const {
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return paused_;
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool WebMediaPlayerMS::seeking() const {
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
280c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)double WebMediaPlayerMS::duration() const {
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
282c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return std::numeric_limits<double>::infinity();
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)double WebMediaPlayerMS::currentTime() const {
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
287effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (current_time_.ToInternalValue() != 0) {
288effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return current_time_.InSecondsF();
289868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  } else if (audio_renderer_.get()) {
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return audio_renderer_->GetCurrentRenderTime().InSecondsF();
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
292c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return 0.0;
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WebMediaPlayer::NetworkState WebMediaPlayerMS::networkState() const {
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DVLOG(1) << "WebMediaPlayerMS::networkState, state:" << network_state_;
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return network_state_;
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WebMediaPlayer::ReadyState WebMediaPlayerMS::readyState() const {
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DVLOG(1) << "WebMediaPlayerMS::readyState, state:" << ready_state_;
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return ready_state_;
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
307cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)blink::WebTimeRanges WebMediaPlayerMS::buffered() const {
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return buffered_;
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
312c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)double WebMediaPlayerMS::maxTimeSeekable() const {
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
314c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return 0.0;
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
317cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool WebMediaPlayerMS::didLoadingProgress() {
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return true;
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
32203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)void WebMediaPlayerMS::paint(blink::WebCanvas* canvas,
32303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                             const blink::WebRect& rect,
32490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                             unsigned char alpha) {
32503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  paint(canvas, rect, alpha, SkXfermode::kSrcOver_Mode);
32603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
32703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
32803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)void WebMediaPlayerMS::paint(blink::WebCanvas* canvas,
32903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                             const blink::WebRect& rect,
33003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                             unsigned char alpha,
33103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                             SkXfermode::Mode mode) {
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DVLOG(3) << "WebMediaPlayerMS::paint";
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::RectF dest_rect(rect.x, rect.y, rect.width, rect.height);
3361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  video_renderer_.Paint(
3371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      current_frame_, canvas, dest_rect, alpha, mode, media::VIDEO_ROTATION_0);
3382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  {
3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::AutoLock auto_lock(current_frame_lock_);
341868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (current_frame_.get())
3422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      current_frame_used_ = true;
3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool WebMediaPlayerMS::hasSingleSecurityOrigin() const {
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool WebMediaPlayerMS::didPassCORSAccessCheck() const {
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
356c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)double WebMediaPlayerMS::mediaTimeForTimeValue(double timeValue) const {
3571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return media::ConvertSecondsToTimestamp(timeValue).InSecondsF();
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)unsigned WebMediaPlayerMS::decodedFrameCount() const {
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DVLOG(1) << "WebMediaPlayerMS::decodedFrameCount, " << total_frame_count_;
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return total_frame_count_;
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)unsigned WebMediaPlayerMS::droppedFrameCount() const {
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DVLOG(1) << "WebMediaPlayerMS::droppedFrameCount, " << dropped_frame_count_;
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return dropped_frame_count_;
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)unsigned WebMediaPlayerMS::audioDecodedByteCount() const {
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NOTIMPLEMENTED();
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return 0;
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)unsigned WebMediaPlayerMS::videoDecodedByteCount() const {
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NOTIMPLEMENTED();
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return 0;
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void WebMediaPlayerMS::SetVideoFrameProviderClient(
3852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    cc::VideoFrameProvider::Client* client) {
3862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This is called from both the main renderer thread and the compositor
3872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // thread (when the main thread is blocked).
3882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (video_frame_provider_client_)
3892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    video_frame_provider_client_->StopUsingProvider();
3902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  video_frame_provider_client_ = client;
3912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)scoped_refptr<media::VideoFrame> WebMediaPlayerMS::GetCurrentFrame() {
3942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DVLOG(3) << "WebMediaPlayerMS::GetCurrentFrame";
3952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::AutoLock auto_lock(current_frame_lock_);
3962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!pending_repaint_);
397868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!current_frame_.get())
3982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return NULL;
3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  pending_repaint_ = true;
4002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  current_frame_used_ = true;
4012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return current_frame_;
4022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void WebMediaPlayerMS::PutCurrentFrame(
4052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const scoped_refptr<media::VideoFrame>& frame) {
4062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DVLOG(3) << "WebMediaPlayerMS::PutCurrentFrame";
4072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(pending_repaint_);
4082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  pending_repaint_ = false;
4092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void WebMediaPlayerMS::OnFrameAvailable(
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const scoped_refptr<media::VideoFrame>& frame) {
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DVLOG(3) << "WebMediaPlayerMS::OnFrameAvailable";
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ++total_frame_count_;
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!received_first_frame_) {
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    received_first_frame_ = true;
4182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    {
4192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::AutoLock auto_lock(current_frame_lock_);
4202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DCHECK(!current_frame_used_);
4217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      current_frame_ = frame;
4222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData);
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GetClient()->sizeChanged();
4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (video_frame_provider_.get()) {
42803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      video_weblayer_.reset(new cc_blink::WebLayerImpl(
4296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          cc::VideoLayer::Create(this, media::VIDEO_ROTATION_0)));
430a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      video_weblayer_->setOpaque(true);
4312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      GetClient()->setWebLayer(video_weblayer_.get());
4322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Do not update |current_frame_| when paused.
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (paused_)
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
439868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool size_changed = !current_frame_.get() ||
440868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                      current_frame_->natural_size() != frame->natural_size();
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  {
4432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::AutoLock auto_lock(current_frame_lock_);
444868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (!current_frame_used_ && current_frame_.get())
4452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ++dropped_frame_count_;
4462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    current_frame_ = frame;
447f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    current_time_ = frame->timestamp();
4482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    current_frame_used_ = false;
4492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (size_changed)
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GetClient()->sizeChanged();
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetClient()->repaint();
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void WebMediaPlayerMS::RepaintInternal() {
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DVLOG(1) << "WebMediaPlayerMS::RepaintInternal";
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
4602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetClient()->repaint();
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void WebMediaPlayerMS::OnSourceError() {
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DVLOG(1) << "WebMediaPlayerMS::OnSourceError";
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetNetworkState(WebMediaPlayer::NetworkStateFormatError);
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RepaintInternal();
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void WebMediaPlayerMS::SetNetworkState(WebMediaPlayer::NetworkState state) {
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  network_state_ = state;
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Always notify to ensure client has the latest value.
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GetClient()->networkStateChanged();
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void WebMediaPlayerMS::SetReadyState(WebMediaPlayer::ReadyState state) {
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ready_state_ = state;
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Always notify to ensure client has the latest value.
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GetClient()->readyStateChanged();
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
484f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)blink::WebMediaPlayerClient* WebMediaPlayerMS::GetClient() {
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(thread_checker_.CalledOnValidThread());
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(client_);
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return client_;
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}  // namespace content
491