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/filters/skcanvas_video_renderer.h"
15 #include "skia/ext/platform_canvas.h"
16 #include "third_party/WebKit/public/platform/WebMediaPlayer.h"
21 class WebMediaPlayerClient;
30 class MediaStreamAudioRenderer;
31 class MediaStreamRendererFactory;
32 class VideoFrameProvider;
34 class WebMediaPlayerDelegate;
36 // WebMediaPlayerMS delegates calls from WebCore::MediaPlayerPrivate to
37 // Chrome's media player when "src" is from media stream.
39 // WebMediaPlayerMS works with multiple objects, the most important ones are:
42 // provides video frames for rendering.
44 // TODO(wjia): add AudioPlayer.
46 // plays audio streams.
48 // blink::WebMediaPlayerClient
49 // WebKit client of this media player object.
50 class WebMediaPlayerMS
51 : public blink::WebMediaPlayer,
52 public cc::VideoFrameProvider,
53 public base::SupportsWeakPtr<WebMediaPlayerMS> {
55 // Construct a WebMediaPlayerMS with reference to the client, and
56 // a MediaStreamClient which provides VideoFrameProvider.
57 WebMediaPlayerMS(blink::WebFrame* frame,
58 blink::WebMediaPlayerClient* client,
59 base::WeakPtr<WebMediaPlayerDelegate> delegate,
60 media::MediaLog* media_log,
61 scoped_ptr<MediaStreamRendererFactory> factory);
62 virtual ~WebMediaPlayerMS();
64 virtual void load(LoadType load_type,
65 const blink::WebURL& url,
71 virtual bool supportsSave() const;
72 virtual void seek(double seconds);
73 virtual void setRate(double rate);
74 virtual void setVolume(double volume);
75 virtual void setPreload(blink::WebMediaPlayer::Preload preload);
76 virtual blink::WebTimeRanges buffered() const;
77 virtual double maxTimeSeekable() const;
79 // Methods for painting.
80 virtual void paint(blink::WebCanvas* canvas,
81 const blink::WebRect& rect,
83 SkXfermode::Mode mode);
84 // TODO(dshwang): remove it because above method replaces. crbug.com/401027
85 virtual void paint(blink::WebCanvas* canvas,
86 const blink::WebRect& rect,
89 // True if the loaded media has a playable video/audio track.
90 virtual bool hasVideo() const;
91 virtual bool hasAudio() const;
93 // Dimensions of the video.
94 virtual blink::WebSize naturalSize() const;
96 // Getters of playback state.
97 virtual bool paused() const;
98 virtual bool seeking() const;
99 virtual double duration() const;
100 virtual double currentTime() const;
102 // Internal states of loading and network.
103 virtual blink::WebMediaPlayer::NetworkState networkState() const;
104 virtual blink::WebMediaPlayer::ReadyState readyState() const;
106 virtual bool didLoadingProgress();
108 virtual bool hasSingleSecurityOrigin() const;
109 virtual bool didPassCORSAccessCheck() const;
111 virtual double mediaTimeForTimeValue(double timeValue) const;
113 virtual unsigned decodedFrameCount() const;
114 virtual unsigned droppedFrameCount() const;
115 virtual unsigned audioDecodedByteCount() const;
116 virtual unsigned videoDecodedByteCount() const;
118 // VideoFrameProvider implementation.
119 virtual void SetVideoFrameProviderClient(
120 cc::VideoFrameProvider::Client* client) OVERRIDE;
121 virtual scoped_refptr<media::VideoFrame> GetCurrentFrame() OVERRIDE;
122 virtual void PutCurrentFrame(const scoped_refptr<media::VideoFrame>& frame)
126 // The callback for VideoFrameProvider to signal a new frame is available.
127 void OnFrameAvailable(const scoped_refptr<media::VideoFrame>& frame);
128 // Need repaint due to state change.
129 void RepaintInternal();
131 // The callback for source to report error.
132 void OnSourceError();
134 // Helpers that set the network/ready state and notifies the client if
136 void SetNetworkState(blink::WebMediaPlayer::NetworkState state);
137 void SetReadyState(blink::WebMediaPlayer::ReadyState state);
139 // Getter method to |client_|.
140 blink::WebMediaPlayerClient* GetClient();
142 blink::WebFrame* frame_;
144 blink::WebMediaPlayer::NetworkState network_state_;
145 blink::WebMediaPlayer::ReadyState ready_state_;
147 blink::WebTimeRanges buffered_;
151 // Used for DCHECKs to ensure methods calls executed in the correct thread.
152 base::ThreadChecker thread_checker_;
154 blink::WebMediaPlayerClient* client_;
156 base::WeakPtr<WebMediaPlayerDelegate> delegate_;
158 // Specify content:: to disambiguate from cc::.
159 scoped_refptr<content::VideoFrameProvider> video_frame_provider_;
162 // |current_frame_| is updated only on main thread. The object it holds
163 // can be freed on the compositor thread if it is the last to hold a
164 // reference but media::VideoFrame is a thread-safe ref-pointer. It is
165 // however read on the compositing thread so locking is required around all
166 // modifications on the main thread, and all reads on the compositing thread.
167 scoped_refptr<media::VideoFrame> current_frame_;
168 // |current_frame_used_| is updated on both main and compositing thread.
169 // It's used to track whether |current_frame_| was painted for detecting
170 // when to increase |dropped_frame_count_|.
171 bool current_frame_used_;
172 // |current_frame_lock_| protects |current_frame_used_| and |current_frame_|.
173 base::Lock current_frame_lock_;
174 bool pending_repaint_;
176 scoped_ptr<WebLayerImpl> video_weblayer_;
178 // A pointer back to the compositor to inform it about state changes. This is
179 // not NULL while the compositor is actively using this webmediaplayer.
180 cc::VideoFrameProvider::Client* video_frame_provider_client_;
182 bool received_first_frame_;
183 base::TimeDelta current_time_;
184 unsigned total_frame_count_;
185 unsigned dropped_frame_count_;
186 media::SkCanvasVideoRenderer video_renderer_;
188 scoped_refptr<MediaStreamAudioRenderer> audio_renderer_;
190 scoped_refptr<media::MediaLog> media_log_;
192 scoped_ptr<MediaStreamRendererFactory> renderer_factory_;
194 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerMS);
197 } // namespace content
199 #endif // CONTENT_RENDERER_MEDIA_WEBMEDIAPLAYER_MS_H_