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