Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / webkit / media / webmediaplayer_ms.h
blob3140a529aab64e6a82816c00ef4abf8c84f93b0d
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.
5 #ifndef WEBKIT_MEDIA_WEBMEDIAPLAYER_MS_H_
6 #define WEBKIT_MEDIA_WEBMEDIAPLAYER_MS_H_
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 "cc/layers/video_frame_provider.h"
13 #include "googleurl/src/gurl.h"
14 #include "media/filters/skcanvas_video_renderer.h"
15 #include "skia/ext/platform_canvas.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaPlayer.h"
18 namespace WebKit {
19 class WebFrame;
20 class WebMediaPlayerClient;
23 namespace media {
24 class MediaLog;
27 namespace webkit {
28 class WebLayerImpl;
31 namespace webkit_media {
33 class MediaStreamAudioRenderer;
34 class MediaStreamClient;
35 class VideoFrameProvider;
36 class WebMediaPlayerDelegate;
38 // WebMediaPlayerMS delegates calls from WebCore::MediaPlayerPrivate to
39 // Chrome's media player when "src" is from media stream.
41 // WebMediaPlayerMS works with multiple objects, the most important ones are:
43 // VideoFrameProvider
44 // provides video frames for rendering.
46 // TODO(wjia): add AudioPlayer.
47 // AudioPlayer
48 // plays audio streams.
50 // WebKit::WebMediaPlayerClient
51 // WebKit client of this media player object.
52 class WebMediaPlayerMS
53 : public WebKit::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(WebKit::WebFrame* frame,
60 WebKit::WebMediaPlayerClient* client,
61 base::WeakPtr<WebMediaPlayerDelegate> delegate,
62 MediaStreamClient* media_stream_client,
63 media::MediaLog* media_log);
64 virtual ~WebMediaPlayerMS();
66 virtual void load(const WebKit::WebURL& url, CORSMode cors_mode) OVERRIDE;
67 virtual void load(const WebKit::WebURL& url,
68 WebKit::WebMediaSource* media_source,
69 CORSMode cors_mode) OVERRIDE;
70 virtual void cancelLoad() OVERRIDE;
72 // Playback controls.
73 virtual void play() OVERRIDE;
74 virtual void pause() OVERRIDE;
75 virtual bool supportsFullscreen() const OVERRIDE;
76 virtual bool supportsSave() const OVERRIDE;
77 virtual void seekFloat(float seconds);
78 virtual void seek(float seconds);
79 virtual void setEndTimeFloat(float seconds);
80 virtual void setEndTime(float seconds);
81 virtual void setRateFloat(float rate);
82 virtual void setRate(float rate);
83 virtual void setVolumeFloat(float volume);
84 virtual void setVolume(float volume);
85 virtual void setVisible(bool visible) OVERRIDE;
86 virtual void setPreload(WebKit::WebMediaPlayer::Preload preload) OVERRIDE;
87 virtual bool totalBytesKnown() OVERRIDE;
88 virtual const WebKit::WebTimeRanges& buffered() OVERRIDE;
89 virtual float maxTimeSeekableFloat() const;
90 virtual float maxTimeSeekable() const;
92 // Methods for painting.
93 virtual void setSize(const WebKit::WebSize& size) OVERRIDE;
95 virtual void paint(WebKit::WebCanvas* canvas,
96 const WebKit::WebRect& rect,
97 uint8_t alpha) OVERRIDE;
99 // True if the loaded media has a playable video/audio track.
100 virtual bool hasVideo() const OVERRIDE;
101 virtual bool hasAudio() const OVERRIDE;
103 // Dimensions of the video.
104 virtual WebKit::WebSize naturalSize() const OVERRIDE;
106 // Getters of playback state.
107 virtual bool paused() const OVERRIDE;
108 virtual bool seeking() const OVERRIDE;
109 virtual float durationFloat() const;
110 virtual float duration() const;
111 virtual float currentTimeFloat() const;
112 virtual float currentTime() const;
114 // Get rate of loading the resource.
115 virtual int32 dataRate() const OVERRIDE;
117 // Internal states of loading and network.
118 virtual WebKit::WebMediaPlayer::NetworkState networkState() const OVERRIDE;
119 virtual WebKit::WebMediaPlayer::ReadyState readyState() const OVERRIDE;
121 virtual bool didLoadingProgress() const OVERRIDE;
122 virtual unsigned long long totalBytes() const OVERRIDE;
124 virtual bool hasSingleSecurityOrigin() const OVERRIDE;
125 virtual bool didPassCORSAccessCheck() const OVERRIDE;
126 virtual WebKit::WebMediaPlayer::MovieLoadType movieLoadType() const OVERRIDE;
128 virtual float mediaTimeForTimeValueFloat(float timeValue) const;
129 virtual float mediaTimeForTimeValue(float timeValue) const;
131 virtual unsigned decodedFrameCount() const OVERRIDE;
132 virtual unsigned droppedFrameCount() const OVERRIDE;
133 virtual unsigned audioDecodedByteCount() const OVERRIDE;
134 virtual unsigned videoDecodedByteCount() const OVERRIDE;
136 // VideoFrameProvider implementation.
137 virtual void SetVideoFrameProviderClient(
138 cc::VideoFrameProvider::Client* client) OVERRIDE;
139 virtual scoped_refptr<media::VideoFrame> GetCurrentFrame() OVERRIDE;
140 virtual void PutCurrentFrame(const scoped_refptr<media::VideoFrame>& frame)
141 OVERRIDE;
143 private:
144 // The callback for VideoFrameProvider to signal a new frame is available.
145 void OnFrameAvailable(const scoped_refptr<media::VideoFrame>& frame);
146 // Need repaint due to state change.
147 void RepaintInternal();
149 // The callback for source to report error.
150 void OnSourceError();
152 // Helpers that set the network/ready state and notifies the client if
153 // they've changed.
154 void SetNetworkState(WebKit::WebMediaPlayer::NetworkState state);
155 void SetReadyState(WebKit::WebMediaPlayer::ReadyState state);
157 // Getter method to |client_|.
158 WebKit::WebMediaPlayerClient* GetClient();
160 WebKit::WebFrame* frame_;
162 WebKit::WebMediaPlayer::NetworkState network_state_;
163 WebKit::WebMediaPlayer::ReadyState ready_state_;
165 WebKit::WebTimeRanges buffered_;
167 // Used for DCHECKs to ensure methods calls executed in the correct thread.
168 base::ThreadChecker thread_checker_;
170 WebKit::WebMediaPlayerClient* client_;
172 base::WeakPtr<WebMediaPlayerDelegate> delegate_;
174 MediaStreamClient* media_stream_client_;
175 scoped_refptr<webkit_media::VideoFrameProvider> video_frame_provider_;
176 bool paused_;
178 // |current_frame_| is updated only on main thread. The object it holds
179 // can be freed on the compositor thread if it is the last to hold a
180 // reference but media::VideoFrame is a thread-safe ref-pointer.
181 scoped_refptr<media::VideoFrame> current_frame_;
182 // |current_frame_used_| is updated on both main and compositing thread.
183 // It's used to track whether |current_frame_| was painted for detecting
184 // when to increase |dropped_frame_count_|.
185 bool current_frame_used_;
186 base::Lock current_frame_lock_;
187 bool pending_repaint_;
189 scoped_ptr<webkit::WebLayerImpl> video_weblayer_;
191 // A pointer back to the compositor to inform it about state changes. This is
192 // not NULL while the compositor is actively using this webmediaplayer.
193 cc::VideoFrameProvider::Client* video_frame_provider_client_;
195 bool received_first_frame_;
196 bool sequence_started_;
197 base::TimeDelta start_time_;
198 unsigned total_frame_count_;
199 unsigned dropped_frame_count_;
200 media::SkCanvasVideoRenderer video_renderer_;
202 scoped_refptr<MediaStreamAudioRenderer> audio_renderer_;
204 scoped_refptr<media::MediaLog> media_log_;
206 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerMS);
209 } // namespace webkit_media
211 #endif // WEBKIT_MEDIA_WEBMEDIAPLAYER_MS_H_