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#ifndef CONTENT_RENDERER_MEDIA_WEBMEDIAPLAYER_MS_H_ 6#define CONTENT_RENDERER_MEDIA_WEBMEDIAPLAYER_MS_H_ 7 8#include "base/memory/ref_counted.h" 9#include "base/memory/scoped_ptr.h" 10#include "base/memory/weak_ptr.h" 11#include "base/synchronization/lock.h" 12#include "base/threading/thread_checker.h" 13#include "cc/layers/video_frame_provider.h" 14#include "media/filters/skcanvas_video_renderer.h" 15#include "skia/ext/platform_canvas.h" 16#include "third_party/WebKit/public/platform/WebMediaPlayer.h" 17#include "url/gurl.h" 18 19namespace blink { 20class WebFrame; 21class WebMediaPlayerClient; 22} 23 24namespace media { 25class MediaLog; 26class WebMediaPlayerDelegate; 27} 28 29namespace cc_blink { 30class WebLayerImpl; 31} 32 33namespace content { 34class MediaStreamAudioRenderer; 35class MediaStreamRendererFactory; 36class VideoFrameProvider; 37 38// WebMediaPlayerMS delegates calls from WebCore::MediaPlayerPrivate to 39// Chrome's media player when "src" is from media stream. 40// 41// WebMediaPlayerMS works with multiple objects, the most important ones are: 42// 43// VideoFrameProvider 44// provides video frames for rendering. 45// 46// TODO(wjia): add AudioPlayer. 47// AudioPlayer 48// plays audio streams. 49// 50// blink::WebMediaPlayerClient 51// WebKit client of this media player object. 52class WebMediaPlayerMS 53 : public blink::WebMediaPlayer, 54 public cc::VideoFrameProvider, 55 public base::SupportsWeakPtr<WebMediaPlayerMS> { 56 public: 57 // Construct a WebMediaPlayerMS with reference to the client, and 58 // a MediaStreamClient which provides VideoFrameProvider. 59 WebMediaPlayerMS(blink::WebFrame* frame, 60 blink::WebMediaPlayerClient* client, 61 base::WeakPtr<media::WebMediaPlayerDelegate> delegate, 62 media::MediaLog* media_log, 63 scoped_ptr<MediaStreamRendererFactory> factory); 64 virtual ~WebMediaPlayerMS(); 65 66 virtual void load(LoadType load_type, 67 const blink::WebURL& url, 68 CORSMode cors_mode); 69 70 // Playback controls. 71 virtual void play(); 72 virtual void pause(); 73 virtual bool supportsSave() const; 74 virtual void seek(double seconds); 75 virtual void setRate(double rate); 76 virtual void setVolume(double volume); 77 virtual void setPreload(blink::WebMediaPlayer::Preload preload); 78 virtual blink::WebTimeRanges buffered() const; 79 virtual double maxTimeSeekable() const; 80 81 // Methods for painting. 82 virtual void paint(blink::WebCanvas* canvas, 83 const blink::WebRect& rect, 84 unsigned char alpha, 85 SkXfermode::Mode mode); 86 // TODO(dshwang): remove it because above method replaces. crbug.com/401027 87 virtual void paint(blink::WebCanvas* canvas, 88 const blink::WebRect& rect, 89 unsigned char alpha); 90 91 // True if the loaded media has a playable video/audio track. 92 virtual bool hasVideo() const; 93 virtual bool hasAudio() const; 94 95 // Dimensions of the video. 96 virtual blink::WebSize naturalSize() const; 97 98 // Getters of playback state. 99 virtual bool paused() const; 100 virtual bool seeking() const; 101 virtual double duration() const; 102 virtual double currentTime() const; 103 104 // Internal states of loading and network. 105 virtual blink::WebMediaPlayer::NetworkState networkState() const; 106 virtual blink::WebMediaPlayer::ReadyState readyState() const; 107 108 virtual bool didLoadingProgress(); 109 110 virtual bool hasSingleSecurityOrigin() const; 111 virtual bool didPassCORSAccessCheck() const; 112 113 virtual double mediaTimeForTimeValue(double timeValue) const; 114 115 virtual unsigned decodedFrameCount() const; 116 virtual unsigned droppedFrameCount() const; 117 virtual unsigned audioDecodedByteCount() const; 118 virtual unsigned videoDecodedByteCount() const; 119 120 // VideoFrameProvider implementation. 121 virtual void SetVideoFrameProviderClient( 122 cc::VideoFrameProvider::Client* client) OVERRIDE; 123 virtual scoped_refptr<media::VideoFrame> GetCurrentFrame() OVERRIDE; 124 virtual void PutCurrentFrame(const scoped_refptr<media::VideoFrame>& frame) 125 OVERRIDE; 126 127 private: 128 // The callback for VideoFrameProvider to signal a new frame is available. 129 void OnFrameAvailable(const scoped_refptr<media::VideoFrame>& frame); 130 // Need repaint due to state change. 131 void RepaintInternal(); 132 133 // The callback for source to report error. 134 void OnSourceError(); 135 136 // Helpers that set the network/ready state and notifies the client if 137 // they've changed. 138 void SetNetworkState(blink::WebMediaPlayer::NetworkState state); 139 void SetReadyState(blink::WebMediaPlayer::ReadyState state); 140 141 // Getter method to |client_|. 142 blink::WebMediaPlayerClient* GetClient(); 143 144 blink::WebFrame* frame_; 145 146 blink::WebMediaPlayer::NetworkState network_state_; 147 blink::WebMediaPlayer::ReadyState ready_state_; 148 149 blink::WebTimeRanges buffered_; 150 151 float volume_; 152 153 // Used for DCHECKs to ensure methods calls executed in the correct thread. 154 base::ThreadChecker thread_checker_; 155 156 blink::WebMediaPlayerClient* client_; 157 158 base::WeakPtr<media::WebMediaPlayerDelegate> delegate_; 159 160 // Specify content:: to disambiguate from cc::. 161 scoped_refptr<content::VideoFrameProvider> video_frame_provider_; 162 bool paused_; 163 164 // |current_frame_| is updated only on main thread. The object it holds 165 // can be freed on the compositor thread if it is the last to hold a 166 // reference but media::VideoFrame is a thread-safe ref-pointer. It is 167 // however read on the compositing thread so locking is required around all 168 // modifications on the main thread, and all reads on the compositing thread. 169 scoped_refptr<media::VideoFrame> current_frame_; 170 // |current_frame_used_| is updated on both main and compositing thread. 171 // It's used to track whether |current_frame_| was painted for detecting 172 // when to increase |dropped_frame_count_|. 173 bool current_frame_used_; 174 // |current_frame_lock_| protects |current_frame_used_| and |current_frame_|. 175 base::Lock current_frame_lock_; 176 bool pending_repaint_; 177 178 scoped_ptr<cc_blink::WebLayerImpl> video_weblayer_; 179 180 // A pointer back to the compositor to inform it about state changes. This is 181 // not NULL while the compositor is actively using this webmediaplayer. 182 cc::VideoFrameProvider::Client* video_frame_provider_client_; 183 184 bool received_first_frame_; 185 base::TimeDelta current_time_; 186 unsigned total_frame_count_; 187 unsigned dropped_frame_count_; 188 media::SkCanvasVideoRenderer video_renderer_; 189 190 scoped_refptr<MediaStreamAudioRenderer> audio_renderer_; 191 192 scoped_refptr<media::MediaLog> media_log_; 193 194 scoped_ptr<MediaStreamRendererFactory> renderer_factory_; 195 196 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerMS); 197}; 198 199} // namespace content 200 201#endif // CONTENT_RENDERER_MEDIA_WEBMEDIAPLAYER_MS_H_ 202