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 cc::VideoFrameProvider
,
57 public base::SupportsWeakPtr
<WebMediaPlayerMS
> {
59 // Construct a WebMediaPlayerMS with reference to the client, and
60 // a MediaStreamClient which provides VideoFrameProvider.
61 WebMediaPlayerMS(blink::WebFrame
* frame
,
62 blink::WebMediaPlayerClient
* client
,
63 base::WeakPtr
<media::WebMediaPlayerDelegate
> delegate
,
64 media::MediaLog
* media_log
,
65 scoped_ptr
<MediaStreamRendererFactory
> factory
);
66 virtual ~WebMediaPlayerMS();
68 virtual void load(LoadType load_type
,
69 const blink::WebURL
& url
,
75 virtual bool supportsSave() const;
76 virtual void seek(double seconds
);
77 virtual void setRate(double rate
);
78 virtual void setVolume(double volume
);
79 virtual void setSinkId(const blink::WebString
& device_id
,
80 media::WebSetSinkIdCB
* web_callback
);
81 virtual void setPreload(blink::WebMediaPlayer::Preload preload
);
82 virtual blink::WebTimeRanges
buffered() const;
83 virtual blink::WebTimeRanges
seekable() const;
85 // Methods for painting.
86 virtual void paint(blink::WebCanvas
* canvas
,
87 const blink::WebRect
& rect
,
89 SkXfermode::Mode mode
);
91 // True if the loaded media has a playable video/audio track.
92 virtual bool hasVideo() const;
93 virtual bool hasAudio() const;
95 // Dimensions of the video.
96 virtual blink::WebSize
naturalSize() const;
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;
104 // Internal states of loading and network.
105 virtual blink::WebMediaPlayer::NetworkState
networkState() const;
106 virtual blink::WebMediaPlayer::ReadyState
readyState() const;
108 virtual bool didLoadingProgress();
110 virtual bool hasSingleSecurityOrigin() const;
111 virtual bool didPassCORSAccessCheck() const;
113 virtual double mediaTimeForTimeValue(double timeValue
) const;
115 virtual unsigned decodedFrameCount() const;
116 virtual unsigned droppedFrameCount() const;
117 virtual unsigned audioDecodedByteCount() const;
118 virtual unsigned videoDecodedByteCount() const;
120 bool copyVideoTextureToPlatformTexture(
121 blink::WebGraphicsContext3D
* web_graphics_context
,
122 unsigned int texture
,
123 unsigned int internal_format
,
125 bool premultiply_alpha
,
126 bool flip_y
) override
;
128 // VideoFrameProvider implementation.
129 void SetVideoFrameProviderClient(
130 cc::VideoFrameProvider::Client
* client
) override
;
131 bool UpdateCurrentFrame(base::TimeTicks deadline_min
,
132 base::TimeTicks deadline_max
) override
;
133 bool HasCurrentFrame() override
;
134 scoped_refptr
<media::VideoFrame
> GetCurrentFrame() override
;
135 void PutCurrentFrame() override
;
138 // The callback for VideoFrameProvider to signal a new frame is available.
139 void OnFrameAvailable(const scoped_refptr
<media::VideoFrame
>& frame
);
140 // Need repaint due to state change.
141 void RepaintInternal();
143 // The callback for source to report error.
144 void OnSourceError();
146 // Helpers that set the network/ready state and notifies the client if
148 void SetNetworkState(blink::WebMediaPlayer::NetworkState state
);
149 void SetReadyState(blink::WebMediaPlayer::ReadyState state
);
151 // Getter method to |client_|.
152 blink::WebMediaPlayerClient
* GetClient();
154 blink::WebFrame
* frame_
;
156 blink::WebMediaPlayer::NetworkState network_state_
;
157 blink::WebMediaPlayer::ReadyState ready_state_
;
159 blink::WebTimeRanges buffered_
;
163 // Used for DCHECKs to ensure methods calls executed in the correct thread.
164 base::ThreadChecker thread_checker_
;
166 blink::WebMediaPlayerClient
* client_
;
168 base::WeakPtr
<media::WebMediaPlayerDelegate
> delegate_
;
170 // Specify content:: to disambiguate from cc::.
171 scoped_refptr
<content::VideoFrameProvider
> video_frame_provider_
;
174 // |current_frame_| is updated only on main thread. The object it holds
175 // can be freed on the compositor thread if it is the last to hold a
176 // reference but media::VideoFrame is a thread-safe ref-pointer. It is
177 // however read on the compositing thread so locking is required around all
178 // modifications on the main thread, and all reads on the compositing thread.
179 scoped_refptr
<media::VideoFrame
> current_frame_
;
180 // |current_frame_used_| is updated on both main and compositing thread.
181 // It's used to track whether |current_frame_| was painted for detecting
182 // when to increase |dropped_frame_count_|.
183 bool current_frame_used_
;
184 // |current_frame_lock_| protects |current_frame_used_| and |current_frame_|.
185 base::Lock current_frame_lock_
;
187 scoped_ptr
<cc_blink::WebLayerImpl
> video_weblayer_
;
189 // A pointer back to the compositor to inform it about state changes. This is
190 // not NULL while the compositor is actively using this webmediaplayer.
191 cc::VideoFrameProvider::Client
* video_frame_provider_client_
;
193 bool received_first_frame_
;
194 base::TimeDelta current_time_
;
195 unsigned total_frame_count_
;
196 unsigned dropped_frame_count_
;
197 media::SkCanvasVideoRenderer video_renderer_
;
199 scoped_refptr
<MediaStreamAudioRenderer
> audio_renderer_
;
201 scoped_refptr
<media::MediaLog
> media_log_
;
203 scoped_ptr
<MediaStreamRendererFactory
> renderer_factory_
;
205 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerMS
);
208 } // namespace content
210 #endif // CONTENT_RENDERER_MEDIA_WEBMEDIAPLAYER_MS_H_