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 "media/blink/webmediaplayer_util.h"
16 #include "skia/ext/platform_canvas.h"
17 #include "third_party/WebKit/public/platform/WebMediaPlayer.h"
22 class WebGraphicsContext3D
;
23 class WebMediaPlayerClient
;
28 class WebMediaPlayerDelegate
;
36 class MediaStreamAudioRenderer
;
37 class MediaStreamRendererFactory
;
38 class VideoFrameProvider
;
40 // WebMediaPlayerMS delegates calls from WebCore::MediaPlayerPrivate to
41 // Chrome's media player when "src" is from media stream.
43 // WebMediaPlayerMS works with multiple objects, the most important ones are:
46 // provides video frames for rendering.
48 // TODO(wjia): add AudioPlayer.
50 // plays audio streams.
52 // blink::WebMediaPlayerClient
53 // WebKit client of this media player object.
54 class WebMediaPlayerMS
55 : public blink::WebMediaPlayer
,
56 public base::SupportsWeakPtr
<WebMediaPlayerMS
> {
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 const scoped_refptr
<base::SingleThreadTaskRunner
>&
66 compositor_task_runner
);
68 virtual ~WebMediaPlayerMS();
70 virtual void load(LoadType load_type
,
71 const blink::WebURL
& url
,
77 virtual bool supportsSave() const;
78 virtual void seek(double seconds
);
79 virtual void setRate(double rate
);
80 virtual void setVolume(double volume
);
81 virtual void setSinkId(const blink::WebString
& device_id
,
82 media::WebSetSinkIdCB
* web_callback
);
83 virtual void setPreload(blink::WebMediaPlayer::Preload preload
);
84 virtual blink::WebTimeRanges
buffered() const;
85 virtual blink::WebTimeRanges
seekable() const;
87 // Methods for painting.
88 virtual void paint(blink::WebCanvas
* canvas
,
89 const blink::WebRect
& rect
,
91 SkXfermode::Mode mode
);
93 // True if the loaded media has a playable video/audio track.
94 virtual bool hasVideo() const;
95 virtual bool hasAudio() const;
97 // Dimensions of the video.
98 virtual blink::WebSize
naturalSize() const;
100 // Getters of playback state.
101 virtual bool paused() const;
102 virtual bool seeking() const;
103 virtual double duration() const;
104 virtual double currentTime() const;
106 // Internal states of loading and network.
107 virtual blink::WebMediaPlayer::NetworkState
networkState() const;
108 virtual blink::WebMediaPlayer::ReadyState
readyState() const;
110 virtual bool didLoadingProgress();
112 virtual bool hasSingleSecurityOrigin() const;
113 virtual bool didPassCORSAccessCheck() const;
115 virtual double mediaTimeForTimeValue(double timeValue
) const;
117 virtual unsigned decodedFrameCount() const;
118 virtual unsigned droppedFrameCount() const;
119 virtual unsigned audioDecodedByteCount() const;
120 virtual unsigned videoDecodedByteCount() const;
122 bool copyVideoTextureToPlatformTexture(
123 blink::WebGraphicsContext3D
* web_graphics_context
,
124 unsigned int texture
,
125 unsigned int internal_format
,
127 bool premultiply_alpha
,
128 bool flip_y
) override
;
131 class Compositor
: public cc::VideoFrameProvider
{
133 explicit Compositor(const scoped_refptr
<base::SingleThreadTaskRunner
>&
134 compositor_task_runner
);
135 ~Compositor() override
;
137 void EnqueueFrame(scoped_refptr
<media::VideoFrame
> const& frame
);
140 gfx::Size
GetCurrentSize();
141 base::TimeDelta
GetCurrentTime();
142 unsigned GetTotalFrameCount();
143 unsigned GetDroppedFrameCount();
145 // VideoFrameProvider implementation.
146 void SetVideoFrameProviderClient(
147 cc::VideoFrameProvider::Client
* client
) override
;
148 bool UpdateCurrentFrame(base::TimeTicks deadline_min
,
149 base::TimeTicks deadline_max
) override
;
150 bool HasCurrentFrame() override
;
151 scoped_refptr
<media::VideoFrame
> GetCurrentFrame() override
;
152 void PutCurrentFrame() override
;
154 void StartRendering();
155 void StopRendering();
156 void ReplaceCurrentFrameWithACopy(media::SkCanvasVideoRenderer
* renderer
);
159 scoped_refptr
<base::SingleThreadTaskRunner
> compositor_task_runner_
;
161 // A pointer back to the compositor to inform it about state changes. This
162 // is not NULL while the compositor is actively using this webmediaplayer.
163 cc::VideoFrameProvider::Client
* video_frame_provider_client_
;
165 // |current_frame_| is updated only on compositor thread. The object it
166 // holds can be freed on the compositor thread if it is the last to hold a
167 // reference but media::VideoFrame is a thread-safe ref-pointer. It is
168 // however read on the compositor and main thread so locking is required
169 // around all modifications and all reads on the any thread.
170 scoped_refptr
<media::VideoFrame
> current_frame_
;
172 // |current_frame_used_| is updated on compositor thread only.
173 // It's used to track whether |current_frame_| was painted for detecting
174 // when to increase |dropped_frame_count_|.
175 bool current_frame_used_
;
177 base::TimeTicks last_deadline_max_
;
178 unsigned total_frame_count_
;
179 unsigned dropped_frame_count_
;
183 // |current_frame_lock_| protects |current_frame_used_| and
185 base::Lock current_frame_lock_
;
188 // The callback for VideoFrameProvider to signal a new frame is available.
189 void OnFrameAvailable(const scoped_refptr
<media::VideoFrame
>& frame
);
190 // Need repaint due to state change.
191 void RepaintInternal();
193 // The callback for source to report error.
194 void OnSourceError();
196 // Helpers that set the network/ready state and notifies the client if
198 void SetNetworkState(blink::WebMediaPlayer::NetworkState state
);
199 void SetReadyState(blink::WebMediaPlayer::ReadyState state
);
201 // Getter method to |client_|.
202 blink::WebMediaPlayerClient
* GetClient();
204 blink::WebFrame
* const frame_
;
206 blink::WebMediaPlayer::NetworkState network_state_
;
207 blink::WebMediaPlayer::ReadyState ready_state_
;
209 const blink::WebTimeRanges buffered_
;
213 blink::WebMediaPlayerClient
* const client_
;
215 const base::WeakPtr
<media::WebMediaPlayerDelegate
> delegate_
;
217 // Specify content:: to disambiguate from cc::.
218 scoped_refptr
<content::VideoFrameProvider
> video_frame_provider_
;
222 scoped_ptr
<cc_blink::WebLayerImpl
> video_weblayer_
;
224 bool received_first_frame_
;
226 scoped_refptr
<MediaStreamAudioRenderer
> audio_renderer_
;
228 media::SkCanvasVideoRenderer video_renderer_
;
230 scoped_refptr
<media::MediaLog
> media_log_
;
232 scoped_ptr
<MediaStreamRendererFactory
> renderer_factory_
;
235 // Used for DCHECKs to ensure methods calls executed in the correct thread.
236 base::ThreadChecker thread_checker_
;
238 // WebMediaPlayerMS owns the Compositor instance, but the destructions of
239 // compositor should take place on Compositor Thread.
240 scoped_ptr
<Compositor
> compositor_
;
242 scoped_refptr
<base::SingleThreadTaskRunner
> compositor_task_runner_
;
245 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerMS
);
248 } // namespace content
250 #endif // CONTENT_RENDERER_MEDIA_WEBMEDIAPLAYER_MS_H_