BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / media / blink / webmediaplayer_impl.h
blob2f7516f7202c8c36384fe0998907c7b2d16e248a
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 "media/blink/webmediaplayer_util.h"
29 #include "third_party/WebKit/public/platform/WebAudioSourceProvider.h"
30 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleResult.h"
31 #include "third_party/WebKit/public/platform/WebMediaPlayer.h"
32 #include "url/gurl.h"
34 namespace blink {
35 class WebGraphicsContext3D;
36 class WebLocalFrame;
37 class WebMediaPlayerClient;
38 class WebMediaPlayerEncryptedMediaClient;
41 namespace base {
42 class SingleThreadTaskRunner;
43 class TaskRunner;
46 namespace cc_blink {
47 class WebLayerImpl;
50 namespace media {
52 class AudioHardwareConfig;
53 class ChunkDemuxer;
54 class GpuVideoAcceleratorFactories;
55 class MediaLog;
56 class VideoFrameCompositor;
57 class WebAudioSourceProviderImpl;
58 class WebMediaPlayerDelegate;
59 class WebTextTrackImpl;
61 // The canonical implementation of blink::WebMediaPlayer that's backed by
62 // Pipeline. Handles normal resource loading, Media Source, and
63 // Encrypted Media.
64 class MEDIA_EXPORT WebMediaPlayerImpl
65 : public NON_EXPORTED_BASE(blink::WebMediaPlayer),
66 public base::SupportsWeakPtr<WebMediaPlayerImpl> {
67 public:
68 // Constructs a WebMediaPlayer implementation using Chromium's media stack.
69 // |delegate| may be null. |renderer| may also be null, in which case an
70 // internal renderer will be created.
71 // TODO(xhwang): Drop the internal renderer path and always pass in a renderer
72 // here.
73 WebMediaPlayerImpl(
74 blink::WebLocalFrame* frame,
75 blink::WebMediaPlayerClient* client,
76 blink::WebMediaPlayerEncryptedMediaClient* encrypted_client,
77 base::WeakPtr<WebMediaPlayerDelegate> delegate,
78 scoped_ptr<RendererFactory> renderer_factory,
79 CdmFactory* cdm_factory,
80 const WebMediaPlayerParams& params);
81 virtual ~WebMediaPlayerImpl();
83 virtual void load(LoadType load_type,
84 const blink::WebURL& url,
85 CORSMode cors_mode);
87 // Playback controls.
88 virtual void play();
89 virtual void pause();
90 virtual bool supportsSave() const;
91 virtual void seek(double seconds);
92 virtual void setRate(double rate);
93 virtual void setVolume(double volume);
94 virtual void setSinkId(const blink::WebString& device_id,
95 WebSetSinkIdCB* web_callbacks);
96 virtual void setPreload(blink::WebMediaPlayer::Preload preload);
97 virtual blink::WebTimeRanges buffered() const;
98 virtual blink::WebTimeRanges seekable() const;
100 // Methods for painting.
101 virtual void paint(blink::WebCanvas* canvas,
102 const blink::WebRect& rect,
103 unsigned char alpha,
104 SkXfermode::Mode mode);
106 // True if the loaded media has a playable video/audio track.
107 virtual bool hasVideo() const;
108 virtual bool hasAudio() const;
110 // Dimensions of the video.
111 virtual blink::WebSize naturalSize() const;
113 // Getters of playback state.
114 virtual bool paused() const;
115 virtual bool seeking() const;
116 virtual double duration() const;
117 virtual double timelineOffset() const;
118 virtual double currentTime() const;
120 // Internal states of loading and network.
121 // TODO(hclam): Ask the pipeline about the state rather than having reading
122 // them from members which would cause race conditions.
123 virtual blink::WebMediaPlayer::NetworkState networkState() const;
124 virtual blink::WebMediaPlayer::ReadyState readyState() const;
126 virtual bool didLoadingProgress();
128 virtual bool hasSingleSecurityOrigin() const;
129 virtual bool didPassCORSAccessCheck() const;
131 virtual double mediaTimeForTimeValue(double timeValue) const;
133 virtual unsigned decodedFrameCount() const;
134 virtual unsigned droppedFrameCount() const;
135 virtual unsigned audioDecodedByteCount() const;
136 virtual unsigned videoDecodedByteCount() const;
138 bool copyVideoTextureToPlatformTexture(
139 blink::WebGraphicsContext3D* web_graphics_context,
140 unsigned int texture,
141 unsigned int internal_format,
142 unsigned int type,
143 bool premultiply_alpha,
144 bool flip_y) override;
146 virtual blink::WebAudioSourceProvider* audioSourceProvider();
148 virtual MediaKeyException generateKeyRequest(
149 const blink::WebString& key_system,
150 const unsigned char* init_data,
151 unsigned init_data_length);
153 virtual MediaKeyException addKey(const blink::WebString& key_system,
154 const unsigned char* key,
155 unsigned key_length,
156 const unsigned char* init_data,
157 unsigned init_data_length,
158 const blink::WebString& session_id);
160 virtual MediaKeyException cancelKeyRequest(
161 const blink::WebString& key_system,
162 const blink::WebString& session_id);
164 virtual void setContentDecryptionModule(
165 blink::WebContentDecryptionModule* cdm,
166 blink::WebContentDecryptionModuleResult result);
168 void OnPipelineSeeked(bool time_changed, PipelineStatus status);
169 void OnPipelineEnded();
170 void OnPipelineError(PipelineStatus error);
171 void OnPipelineMetadata(PipelineMetadata metadata);
172 void OnPipelineBufferingStateChanged(BufferingState buffering_state);
173 void OnDemuxerOpened();
174 void OnAddTextTrack(const TextTrackConfig& config,
175 const AddTextTrackDoneCB& done_cb);
177 private:
178 // Called after |defer_load_cb_| has decided to allow the load. If
179 // |defer_load_cb_| is null this is called immediately.
180 void DoLoad(LoadType load_type,
181 const blink::WebURL& url,
182 CORSMode cors_mode);
184 // Called after asynchronous initialization of a data source completed.
185 void DataSourceInitialized(bool success);
187 // Called when the data source is downloading or paused.
188 void NotifyDownloading(bool is_downloading);
190 // Creates a Renderer that will be used by the |pipeline_|.
191 scoped_ptr<Renderer> CreateRenderer();
193 // Finishes starting the pipeline due to a call to load().
194 void StartPipeline();
196 // Helpers that set the network/ready state and notifies the client if
197 // they've changed.
198 void SetNetworkState(blink::WebMediaPlayer::NetworkState state);
199 void SetReadyState(blink::WebMediaPlayer::ReadyState state);
201 // Gets the duration value reported by the pipeline.
202 double GetPipelineDuration() const;
204 // Callbacks from |pipeline_| that are forwarded to |client_|.
205 void OnDurationChanged();
206 void OnNaturalSizeChanged(gfx::Size size);
207 void OnOpacityChanged(bool opaque);
209 // Called by VideoRendererImpl on its internal thread with the new frame to be
210 // painted.
211 void FrameReady(const scoped_refptr<VideoFrame>& frame);
213 // Returns the current video frame from |compositor_|. Blocks until the
214 // compositor can return the frame.
215 scoped_refptr<VideoFrame> GetCurrentFrameFromCompositor();
217 // Called when the demuxer encounters encrypted streams.
218 void OnEncryptedMediaInitData(EmeInitDataType init_data_type,
219 const std::vector<uint8>& init_data);
221 // Called when a decoder detects that the key needed to decrypt the stream
222 // is not available.
223 void OnWaitingForDecryptionKey();
225 // Sets |cdm_context| on the pipeline and fires |cdm_attached_cb| when done.
226 // Parameter order is reversed for easy binding.
227 void SetCdm(const CdmAttachedCB& cdm_attached_cb, CdmContext* cdm_context);
229 // Called when a CDM has been attached to the |pipeline_|.
230 void OnCdmAttached(bool success);
232 // Updates |paused_time_| to the current media time with consideration for the
233 // |ended_| state by clamping current time to duration upon |ended_|.
234 void UpdatePausedTime();
236 blink::WebLocalFrame* frame_;
238 // TODO(hclam): get rid of these members and read from the pipeline directly.
239 blink::WebMediaPlayer::NetworkState network_state_;
240 blink::WebMediaPlayer::ReadyState ready_state_;
242 // Preload state for when |data_source_| is created after setPreload().
243 BufferedDataSource::Preload preload_;
245 // Task runner for posting tasks on Chrome's main thread. Also used
246 // for DCHECKs so methods calls won't execute in the wrong thread.
247 const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
249 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_;
250 scoped_refptr<base::TaskRunner> worker_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 double playback_rate_;
276 bool paused_;
277 base::TimeDelta paused_time_;
278 bool seeking_;
279 base::TimeDelta seek_time_; // Meaningless when |seeking_| is false.
281 // TODO(scherkus): Replace with an explicit ended signal to HTMLMediaElement,
282 // see http://crbug.com/409280
283 bool ended_;
285 // Seek gets pending if another seek is in progress. Only last pending seek
286 // will have effect.
287 bool pending_seek_;
288 // |pending_seek_time_| is meaningless when |pending_seek_| is false.
289 base::TimeDelta pending_seek_time_;
291 // Tracks whether to issue time changed notifications during buffering state
292 // changes.
293 bool should_notify_time_changed_;
295 blink::WebMediaPlayerClient* client_;
296 blink::WebMediaPlayerEncryptedMediaClient* encrypted_client_;
298 base::WeakPtr<WebMediaPlayerDelegate> delegate_;
300 WebMediaPlayerParams::DeferLoadCB defer_load_cb_;
301 WebMediaPlayerParams::Context3DCB context_3d_cb_;
303 // Routes audio playback to either AudioRendererSink or WebAudio.
304 scoped_refptr<WebAudioSourceProviderImpl> audio_source_provider_;
306 bool supports_save_;
308 // These two are mutually exclusive:
309 // |data_source_| is used for regular resource loads.
310 // |chunk_demuxer_| is used for Media Source resource loads.
312 // |demuxer_| will contain the appropriate demuxer based on which resource
313 // load strategy we're using.
314 scoped_ptr<BufferedDataSource> data_source_;
315 scoped_ptr<Demuxer> demuxer_;
316 ChunkDemuxer* chunk_demuxer_;
318 BufferedDataSourceHostImpl buffered_data_source_host_;
320 // Video rendering members.
321 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_;
322 VideoFrameCompositor* compositor_; // Deleted on |compositor_task_runner_|.
323 SkCanvasVideoRenderer skcanvas_video_renderer_;
325 // The compositor layer for displaying the video content when using composited
326 // playback.
327 scoped_ptr<cc_blink::WebLayerImpl> video_weblayer_;
329 EncryptedMediaPlayerSupport encrypted_media_support_;
331 scoped_ptr<blink::WebContentDecryptionModuleResult> set_cdm_result_;
333 scoped_ptr<RendererFactory> renderer_factory_;
335 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerImpl);
338 } // namespace media
340 #endif // MEDIA_BLINK_WEBMEDIAPLAYER_IMPL_H_