OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 // Delegate calls from WebCore::MediaPlayerPrivate to Chrome's video player. |
| 6 // It contains Pipeline which is the actual media player pipeline, it glues |
| 7 // the media player pipeline, data source, audio renderer and renderer. |
| 8 // Pipeline would creates multiple threads and access some public methods |
| 9 // of this class, so we need to be extra careful about concurrent access of |
| 10 // methods and members. |
| 11 // |
| 12 // WebMediaPlayerMS works with multiple objects, the most important ones are: |
| 13 // |
| 14 // media::Pipeline |
| 15 // The media playback pipeline. |
| 16 // |
| 17 // VideoRendererBase |
| 18 // Video renderer object. |
| 19 // |
| 20 // WebKit::WebMediaPlayerClient |
| 21 // WebKit client of this media player object. |
| 22 // |
| 23 // The following diagram shows the relationship of these objects: |
| 24 // (note: ref-counted reference is marked by a "r".) |
| 25 // |
| 26 // WebMediaPlayerClient (WebKit object) |
| 27 // ^ |
| 28 // | |
| 29 // WebMediaPlayerMS ---> Pipeline |
| 30 // | ^ | |
| 31 // | | v r |
| 32 // | | VideoRendererBase |
| 33 // | | | ^ r |
| 34 // | r | v r | |
| 35 // '---> WebMediaPlayerProxy --' |
| 36 // |
| 37 // Notice that WebMediaPlayerProxy and VideoRendererBase are referencing each |
| 38 // other. This interdependency has to be treated carefully. |
| 39 // |
| 40 // Other issues: |
| 41 // During tear down of the whole browser or a tab, the DOM tree may not be |
| 42 // destructed nicely, and there will be some dangling media threads trying to |
| 43 // the main thread, so we need this class to listen to destruction event of the |
| 44 // main thread and cleanup the media threads when the even is received. Also |
| 45 // at destruction of this class we will need to unhook it from destruction event |
| 46 // list of the main thread. |
| 47 |
| 48 #ifndef WEBKIT_MEDIA_WEBMEDIAPLAYER_MS_H_ |
| 49 #define WEBKIT_MEDIA_WEBMEDIAPLAYER_MS_H_ |
| 50 |
| 51 #include "base/memory/ref_counted.h" |
| 52 #include "base/memory/scoped_ptr.h" |
| 53 #include "base/memory/weak_ptr.h" |
| 54 #include "base/message_loop.h" |
| 55 #include "googleurl/src/gurl.h" |
| 56 #include "media/base/audio_renderer_sink.h" |
| 57 #include "media/base/filters.h" |
| 58 #include "media/base/message_loop_factory.h" |
| 59 #include "media/base/pipeline.h" |
| 60 #include "skia/ext/platform_canvas.h" |
| 61 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAudioSourceProvide
r.h" |
| 62 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaPlayer.h" |
| 63 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaPlayerClient.
h" |
| 64 |
| 65 class RenderAudioSourceProvider; |
| 66 class SkCanvas; |
| 67 |
| 68 namespace WebKit { |
| 69 class WebAudioSourceProvider; |
| 70 class WebFrame; |
| 71 } |
| 72 |
| 73 namespace gfx { |
| 74 class Rect; |
| 75 } |
| 76 |
| 77 namespace media { |
| 78 class MediaLog; |
| 79 } |
| 80 |
| 81 namespace webkit_media { |
| 82 |
| 83 class MediaStreamClient; |
| 84 class VideoFrameProvider; |
| 85 class WebMediaPlayerDelegate; |
| 86 class WebMediaPlayerMS; |
| 87 |
| 88 class WebMediaPlayerMSProxy |
| 89 : public base::RefCountedThreadSafe<WebMediaPlayerMSProxy> { |
| 90 public: |
| 91 WebMediaPlayerMSProxy(); |
| 92 |
| 93 void SetListener(WebMediaPlayerMS* listener) { |
| 94 listener_ = listener; |
| 95 } |
| 96 |
| 97 void Repaint(); |
| 98 void Paint(SkCanvas* canvas, const gfx::Rect& dest_rect, uint8_t alpha); |
| 99 |
| 100 private: |
| 101 friend class base::RefCountedThreadSafe<WebMediaPlayerMSProxy>; |
| 102 virtual ~WebMediaPlayerMSProxy(); |
| 103 |
| 104 WebMediaPlayerMS* listener_; |
| 105 scoped_refptr<base::MessageLoopProxy> render_message_loop_; |
| 106 |
| 107 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerMSProxy); |
| 108 }; |
| 109 |
| 110 class WebMediaPlayerMS |
| 111 : public WebKit::WebMediaPlayer, |
| 112 public MessageLoop::DestructionObserver, |
| 113 public base::SupportsWeakPtr<WebMediaPlayerMS> { |
| 114 public: |
| 115 // Construct a WebMediaPlayerMS with reference to the client, and media |
| 116 // filter collection. By providing the filter collection the implementor can |
| 117 // provide more specific media filters that does resource loading and |
| 118 // rendering. |
| 119 // |
| 120 // WebMediaPlayerMS comes packaged with the following media filters: |
| 121 // - URL fetching |
| 122 // - Demuxing |
| 123 // - Software audio/video decoding |
| 124 // - Video rendering |
| 125 // |
| 126 // Clients are expected to add their platform-specific audio rendering media |
| 127 // filter if they wish to hear any sound coming out the speakers, otherwise |
| 128 // audio data is discarded and media plays back based on wall clock time. |
| 129 // |
| 130 WebMediaPlayerMS(WebKit::WebFrame* frame, |
| 131 WebKit::WebMediaPlayerClient* client, |
| 132 base::WeakPtr<WebMediaPlayerDelegate> delegate, |
| 133 media::MessageLoopFactory* message_loop_factory, |
| 134 MediaStreamClient* media_stream_client, |
| 135 media::MediaLog* media_log); |
| 136 virtual ~WebMediaPlayerMS(); |
| 137 |
| 138 virtual void load(const WebKit::WebURL& url); |
| 139 virtual void cancelLoad(); |
| 140 |
| 141 // Playback controls. |
| 142 virtual void play(); |
| 143 virtual void pause(); |
| 144 virtual bool supportsFullscreen() const; |
| 145 virtual bool supportsSave() const; |
| 146 virtual void seek(float seconds); |
| 147 virtual void setEndTime(float seconds); |
| 148 virtual void setRate(float rate); |
| 149 virtual void setVolume(float volume); |
| 150 virtual void setVisible(bool visible); |
| 151 virtual void setPreload(WebKit::WebMediaPlayer::Preload preload); |
| 152 virtual bool totalBytesKnown(); |
| 153 virtual const WebKit::WebTimeRanges& buffered(); |
| 154 virtual float maxTimeSeekable() const; |
| 155 |
| 156 // Methods for painting. |
| 157 virtual void setSize(const WebKit::WebSize& size); |
| 158 |
| 159 #if WEBKIT_USING_SKIA |
| 160 // This variant (without alpha) is just present during staging of this API |
| 161 // change. Later we will again only have one virtual paint(). |
| 162 virtual void paint(WebKit::WebCanvas* canvas, const WebKit::WebRect& rect); |
| 163 virtual void paint(WebKit::WebCanvas* canvas, |
| 164 const WebKit::WebRect& rect, |
| 165 uint8_t alpha); |
| 166 #else |
| 167 virtual void paint(WebKit::WebCanvas* canvas, const WebKit::WebRect& rect); |
| 168 #endif |
| 169 |
| 170 // True if the loaded media has a playable video/audio track. |
| 171 virtual bool hasVideo() const; |
| 172 virtual bool hasAudio() const; |
| 173 |
| 174 // Dimensions of the video. |
| 175 virtual WebKit::WebSize naturalSize() const; |
| 176 |
| 177 // Getters of playback state. |
| 178 virtual bool paused() const; |
| 179 virtual bool seeking() const; |
| 180 virtual float duration() const; |
| 181 virtual float currentTime() const; |
| 182 |
| 183 // Get rate of loading the resource. |
| 184 virtual int32 dataRate() const; |
| 185 |
| 186 // Internal states of loading and network. |
| 187 // TODO(hclam): Ask the pipeline about the state rather than having reading |
| 188 // them from members which would cause race conditions. |
| 189 virtual WebKit::WebMediaPlayer::NetworkState networkState() const; |
| 190 virtual WebKit::WebMediaPlayer::ReadyState readyState() const; |
| 191 |
| 192 virtual unsigned long long bytesLoaded() const; |
| 193 virtual unsigned long long totalBytes() const; |
| 194 |
| 195 virtual bool hasSingleSecurityOrigin() const; |
| 196 virtual WebKit::WebMediaPlayer::MovieLoadType movieLoadType() const; |
| 197 |
| 198 virtual float mediaTimeForTimeValue(float timeValue) const; |
| 199 |
| 200 virtual unsigned decodedFrameCount() const; |
| 201 virtual unsigned droppedFrameCount() const; |
| 202 virtual unsigned audioDecodedByteCount() const; |
| 203 virtual unsigned videoDecodedByteCount() const; |
| 204 |
| 205 virtual WebKit::WebVideoFrame* getCurrentFrame(); |
| 206 virtual void putCurrentFrame(WebKit::WebVideoFrame* web_video_frame); |
| 207 |
| 208 // As we are closing the tab or even the browser, |main_loop_| is destroyed |
| 209 // even before this object gets destructed, so we need to know when |
| 210 // |main_loop_| is being destroyed and we can stop posting repaint task |
| 211 // to it. |
| 212 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
| 213 |
| 214 void Repaint(); |
| 215 |
| 216 private: |
| 217 // Helpers that set the network/ready state and notifies the client if |
| 218 // they've changed. |
| 219 void SetNetworkState(WebKit::WebMediaPlayer::NetworkState state); |
| 220 void SetReadyState(WebKit::WebMediaPlayer::ReadyState state); |
| 221 |
| 222 // Destroy resources held. |
| 223 void Destroy(); |
| 224 |
| 225 // Getter method to |client_|. |
| 226 WebKit::WebMediaPlayerClient* GetClient(); |
| 227 |
| 228 // Lets V8 know that player uses extra resources not managed by V8. |
| 229 void IncrementExternallyAllocatedMemory(); |
| 230 |
| 231 WebKit::WebFrame* frame_; |
| 232 |
| 233 // TODO(hclam): get rid of these members and read from the pipeline directly. |
| 234 WebKit::WebMediaPlayer::NetworkState network_state_; |
| 235 WebKit::WebMediaPlayer::ReadyState ready_state_; |
| 236 |
| 237 // Keep a list of buffered time ranges. |
| 238 WebKit::WebTimeRanges buffered_; |
| 239 |
| 240 // Message loops for posting tasks between Chrome's main thread. Also used |
| 241 // for DCHECKs so methods calls won't execute in the wrong thread. |
| 242 MessageLoop* main_loop_; |
| 243 |
| 244 // The media pipeline and a bool tracking whether we have started it yet. |
| 245 // |
| 246 // TODO(scherkus): replace |started_| with a pointer check for |pipeline_| and |
| 247 // have WebMediaPlayerMS return the default values to WebKit instead of |
| 248 // relying on Pipeline to take care of default values. |
| 249 |
| 250 scoped_ptr<media::MessageLoopFactory> message_loop_factory_; |
| 251 |
| 252 WebKit::WebMediaPlayerClient* client_; |
| 253 |
| 254 scoped_refptr<WebMediaPlayerMSProxy> proxy_; |
| 255 |
| 256 base::WeakPtr<WebMediaPlayerDelegate> delegate_; |
| 257 |
| 258 MediaStreamClient* media_stream_client_; |
| 259 scoped_refptr<VideoFrameProvider> frame_provider_; |
| 260 bool frame_provider_paused_; |
| 261 |
| 262 #if WEBKIT_USING_CG |
| 263 scoped_ptr<skia::PlatformCanvas> skia_canvas_; |
| 264 #endif |
| 265 |
| 266 scoped_refptr<media::MediaLog> media_log_; |
| 267 |
| 268 // Since accelerated compositing status is only known after the first layout, |
| 269 // we delay reporting it to UMA until that time. |
| 270 bool accelerated_compositing_reported_; |
| 271 |
| 272 bool incremented_externally_allocated_memory_; |
| 273 |
| 274 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerMS); |
| 275 }; |
| 276 |
| 277 } // namespace webkit_media |
| 278 |
| 279 #endif // WEBKIT_MEDIA_WEBMEDIAPLAYER_MS_H_ |
OLD | NEW |