Revert of Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320) (patchset...
[chromium-blink-merge.git] / media / blink / webmediaplayer_impl.h
blob80bea8f3c05a7cc64611779dbd5526a24f6389d8
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 MEDIA_BLINK_WEBMEDIAPLAYER_IMPL_H_
6 #define MEDIA_BLINK_WEBMEDIAPLAYER_IMPL_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/threading/thread.h"
17 #include "media/base/cdm_factory.h"
18 #include "media/base/media_export.h"
19 #include "media/base/pipeline.h"
20 #include "media/base/renderer_factory.h"
21 #include "media/base/text_track.h"
22 #include "media/blink/buffered_data_source.h"
23 #include "media/blink/buffered_data_source_host_impl.h"
24 #include "media/blink/encrypted_media_player_support.h"
25 #include "media/blink/skcanvas_video_renderer.h"
26 #include "media/blink/video_frame_compositor.h"
27 #include "media/blink/webmediaplayer_params.h"
28 #include "third_party/WebKit/public/platform/WebAudioSourceProvider.h"
29 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleResult.h"
30 #include "third_party/WebKit/public/platform/WebMediaPlayer.h"
31 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h"
32 #include "url/gurl.h"
34 namespace blink {
35 class WebGraphicsContext3D;
36 class WebLocalFrame;
39 namespace base {
40 class SingleThreadTaskRunner;
43 namespace cc_blink {
44 class WebLayerImpl;
47 namespace media {
49 class AudioHardwareConfig;
50 class ChunkDemuxer;
51 class GpuVideoAcceleratorFactories;
52 class MediaLog;
53 class VideoFrameCompositor;
54 class WebAudioSourceProviderImpl;
55 class WebMediaPlayerDelegate;
56 class WebTextTrackImpl;
58 // The canonical implementation of blink::WebMediaPlayer that's backed by
59 // Pipeline. Handles normal resource loading, Media Source, and
60 // Encrypted Media.
61 class MEDIA_EXPORT WebMediaPlayerImpl
62 : public NON_EXPORTED_BASE(blink::WebMediaPlayer),
63 public base::SupportsWeakPtr<WebMediaPlayerImpl> {
64 public:
65 // Constructs a WebMediaPlayer implementation using Chromium's media stack.
66 // |delegate| may be null. |renderer| may also be null, in which case an
67 // internal renderer will be created.
68 // TODO(xhwang): Drop the internal renderer path and always pass in a renderer
69 // here.
70 WebMediaPlayerImpl(blink::WebLocalFrame* frame,
71 blink::WebMediaPlayerClient* client,
72 base::WeakPtr<WebMediaPlayerDelegate> delegate,
73 scoped_ptr<RendererFactory> renderer_factory,
74 CdmFactory* cdm_factory,
75 const WebMediaPlayerParams& params);
76 virtual ~WebMediaPlayerImpl();
78 virtual void load(LoadType load_type,
79 const blink::WebURL& url,
80 CORSMode cors_mode);
82 // Playback controls.
83 virtual void play();
84 virtual void pause();
85 virtual bool supportsSave() const;
86 virtual void seek(double seconds);
87 virtual void setRate(double rate);
88 virtual void setVolume(double volume);
89 virtual void setPreload(blink::WebMediaPlayer::Preload preload);
90 virtual blink::WebTimeRanges buffered() const;
91 virtual blink::WebTimeRanges seekable() const;
93 // Methods for painting.
94 virtual void paint(blink::WebCanvas* canvas,
95 const blink::WebRect& rect,
96 unsigned char alpha,
97 SkXfermode::Mode mode);
99 // True if the loaded media has a playable video/audio track.
100 virtual bool hasVideo() const;
101 virtual bool hasAudio() const;
103 // Dimensions of the video.
104 virtual blink::WebSize naturalSize() const;
106 // Getters of playback state.
107 virtual bool paused() const;
108 virtual bool seeking() const;
109 virtual double duration() const;
110 virtual double timelineOffset() const;
111 virtual double currentTime() const;
113 // Internal states of loading and network.
114 // TODO(hclam): Ask the pipeline about the state rather than having reading
115 // them from members which would cause race conditions.
116 virtual blink::WebMediaPlayer::NetworkState networkState() const;
117 virtual blink::WebMediaPlayer::ReadyState readyState() const;
119 virtual bool didLoadingProgress();
121 virtual bool hasSingleSecurityOrigin() const;
122 virtual bool didPassCORSAccessCheck() const;
124 virtual double mediaTimeForTimeValue(double timeValue) const;
126 virtual unsigned decodedFrameCount() const;
127 virtual unsigned droppedFrameCount() const;
128 virtual unsigned audioDecodedByteCount() const;
129 virtual unsigned videoDecodedByteCount() const;
131 // TODO(dshwang): remove |level|. crbug.com/443151
132 virtual bool copyVideoTextureToPlatformTexture(
133 blink::WebGraphicsContext3D* web_graphics_context,
134 unsigned int texture,
135 unsigned int level,
136 unsigned int internal_format,
137 unsigned int type,
138 bool premultiply_alpha,
139 bool flip_y);
140 virtual bool copyVideoTextureToPlatformTexture(
141 blink::WebGraphicsContext3D* web_graphics_context,
142 unsigned int texture,
143 unsigned int internal_format,
144 unsigned int type,
145 bool premultiply_alpha,
146 bool flip_y);
148 virtual blink::WebAudioSourceProvider* audioSourceProvider();
150 virtual MediaKeyException generateKeyRequest(
151 const blink::WebString& key_system,
152 const unsigned char* init_data,
153 unsigned init_data_length);
155 virtual MediaKeyException addKey(const blink::WebString& key_system,
156 const unsigned char* key,
157 unsigned key_length,
158 const unsigned char* init_data,
159 unsigned init_data_length,
160 const blink::WebString& session_id);
162 virtual MediaKeyException cancelKeyRequest(
163 const blink::WebString& key_system,
164 const blink::WebString& session_id);
166 virtual void setContentDecryptionModule(
167 blink::WebContentDecryptionModule* cdm,
168 blink::WebContentDecryptionModuleResult result);
170 void OnPipelineSeeked(bool time_changed, PipelineStatus status);
171 void OnPipelineEnded();
172 void OnPipelineError(PipelineStatus error);
173 void OnPipelineMetadata(PipelineMetadata metadata);
174 void OnPipelineBufferingStateChanged(BufferingState buffering_state);
175 void OnDemuxerOpened();
176 void OnAddTextTrack(const TextTrackConfig& config,
177 const AddTextTrackDoneCB& done_cb);
179 private:
180 // Called after |defer_load_cb_| has decided to allow the load. If
181 // |defer_load_cb_| is null this is called immediately.
182 void DoLoad(LoadType load_type,
183 const blink::WebURL& url,
184 CORSMode cors_mode);
186 // Called after asynchronous initialization of a data source completed.
187 void DataSourceInitialized(bool success);
189 // Called when the data source is downloading or paused.
190 void NotifyDownloading(bool is_downloading);
192 // Creates a Renderer that will be used by the |pipeline_|.
193 scoped_ptr<Renderer> CreateRenderer();
195 // Finishes starting the pipeline due to a call to load().
196 void StartPipeline();
198 // Helpers that set the network/ready state and notifies the client if
199 // they've changed.
200 void SetNetworkState(blink::WebMediaPlayer::NetworkState state);
201 void SetReadyState(blink::WebMediaPlayer::ReadyState state);
203 // Gets the duration value reported by the pipeline.
204 double GetPipelineDuration() const;
206 // Callbacks from |pipeline_| that are forwarded to |client_|.
207 void OnDurationChanged();
208 void OnNaturalSizeChanged(gfx::Size size);
209 void OnOpacityChanged(bool opaque);
211 // Called by VideoRendererImpl on its internal thread with the new frame to be
212 // painted.
213 void FrameReady(const scoped_refptr<VideoFrame>& frame);
215 // Returns the current video frame from |compositor_|. Blocks until the
216 // compositor can return the frame.
217 scoped_refptr<VideoFrame> GetCurrentFrameFromCompositor();
219 // Called when the demuxer encounters encrypted streams.
220 void OnEncryptedMediaInitData(EmeInitDataType init_data_type,
221 const std::vector<uint8>& init_data);
223 // Called when a decoder detects that the key needed to decrypt the stream
224 // is not available.
225 void OnWaitingForDecryptionKey();
227 void SetCdm(CdmContext* cdm_context, const CdmAttachedCB& cdm_attached_cb);
229 // Called when a CDM has been attached to the |pipeline_|.
230 void OnCdmAttached(blink::WebContentDecryptionModuleResult result,
231 bool success);
233 // Updates |paused_time_| to the current media time with consideration for the
234 // |ended_| state by clamping current time to duration upon |ended_|.
235 void UpdatePausedTime();
237 blink::WebLocalFrame* frame_;
239 // TODO(hclam): get rid of these members and read from the pipeline directly.
240 blink::WebMediaPlayer::NetworkState network_state_;
241 blink::WebMediaPlayer::ReadyState ready_state_;
243 // Preload state for when |data_source_| is created after setPreload().
244 BufferedDataSource::Preload preload_;
246 // Task runner for posting tasks on Chrome's main thread. Also used
247 // for DCHECKs so methods calls won't execute in the wrong thread.
248 const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
250 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_;
251 scoped_refptr<MediaLog> media_log_;
252 Pipeline pipeline_;
254 // The LoadType passed in the |load_type| parameter of the load() call.
255 LoadType load_type_;
257 // Cache of metadata for answering hasAudio(), hasVideo(), and naturalSize().
258 PipelineMetadata pipeline_metadata_;
260 // Whether the video is known to be opaque or not.
261 bool opaque_;
263 // Playback state.
265 // TODO(scherkus): we have these because Pipeline favours the simplicity of a
266 // single "playback rate" over worrying about paused/stopped etc... It forces
267 // all clients to manage the pause+playback rate externally, but is that
268 // really a bad thing?
270 // TODO(scherkus): since SetPlaybackRate(0) is asynchronous and we don't want
271 // to hang the render thread during pause(), we record the time at the same
272 // time we pause and then return that value in currentTime(). Otherwise our
273 // clock can creep forward a little bit while the asynchronous
274 // SetPlaybackRate(0) is being executed.
275 bool paused_;
276 bool seeking_;
277 double playback_rate_;
278 base::TimeDelta paused_time_;
280 // TODO(scherkus): Replace with an explicit ended signal to HTMLMediaElement,
281 // see http://crbug.com/409280
282 bool ended_;
284 // Seek gets pending if another seek is in progress. Only last pending seek
285 // will have effect.
286 bool pending_seek_;
287 double pending_seek_seconds_;
289 // Tracks whether to issue time changed notifications during buffering state
290 // changes.
291 bool should_notify_time_changed_;
293 blink::WebMediaPlayerClient* client_;
295 base::WeakPtr<WebMediaPlayerDelegate> delegate_;
297 WebMediaPlayerParams::DeferLoadCB defer_load_cb_;
298 WebMediaPlayerParams::Context3DCB context_3d_cb_;
300 // Routes audio playback to either AudioRendererSink or WebAudio.
301 scoped_refptr<WebAudioSourceProviderImpl> audio_source_provider_;
303 bool supports_save_;
305 // These two are mutually exclusive:
306 // |data_source_| is used for regular resource loads.
307 // |chunk_demuxer_| is used for Media Source resource loads.
309 // |demuxer_| will contain the appropriate demuxer based on which resource
310 // load strategy we're using.
311 scoped_ptr<BufferedDataSource> data_source_;
312 scoped_ptr<Demuxer> demuxer_;
313 ChunkDemuxer* chunk_demuxer_;
315 BufferedDataSourceHostImpl buffered_data_source_host_;
317 // Video rendering members.
318 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_;
319 VideoFrameCompositor* compositor_; // Deleted on |compositor_task_runner_|.
320 SkCanvasVideoRenderer skcanvas_video_renderer_;
322 // The compositor layer for displaying the video content when using composited
323 // playback.
324 scoped_ptr<cc_blink::WebLayerImpl> video_weblayer_;
326 EncryptedMediaPlayerSupport encrypted_media_support_;
328 scoped_ptr<RendererFactory> renderer_factory_;
330 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerImpl);
333 } // namespace media
335 #endif // MEDIA_BLINK_WEBMEDIAPLAYER_IMPL_H_