Pass the affected tree in AXTreeDelegate calls.
[chromium-blink-merge.git] / media / blink / webmediaplayer_impl.h
blob427357b1f615fb8388c87522829dfb97085f8b09
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 // Sets |cdm_context| on the pipeline and fires |cdm_attached_cb| when done.
228 // Parameter order is reversed for easy binding.
229 void SetCdm(const CdmAttachedCB& cdm_attached_cb, CdmContext* cdm_context);
231 // Called when a CDM has been attached to the |pipeline_|.
232 void OnCdmAttached(blink::WebContentDecryptionModuleResult result,
233 bool success);
235 // Updates |paused_time_| to the current media time with consideration for the
236 // |ended_| state by clamping current time to duration upon |ended_|.
237 void UpdatePausedTime();
239 blink::WebLocalFrame* frame_;
241 // TODO(hclam): get rid of these members and read from the pipeline directly.
242 blink::WebMediaPlayer::NetworkState network_state_;
243 blink::WebMediaPlayer::ReadyState ready_state_;
245 // Preload state for when |data_source_| is created after setPreload().
246 BufferedDataSource::Preload preload_;
248 // Task runner for posting tasks on Chrome's main thread. Also used
249 // for DCHECKs so methods calls won't execute in the wrong thread.
250 const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
252 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_;
253 scoped_refptr<MediaLog> media_log_;
254 Pipeline pipeline_;
256 // The LoadType passed in the |load_type| parameter of the load() call.
257 LoadType load_type_;
259 // Cache of metadata for answering hasAudio(), hasVideo(), and naturalSize().
260 PipelineMetadata pipeline_metadata_;
262 // Whether the video is known to be opaque or not.
263 bool opaque_;
265 // Playback state.
267 // TODO(scherkus): we have these because Pipeline favours the simplicity of a
268 // single "playback rate" over worrying about paused/stopped etc... It forces
269 // all clients to manage the pause+playback rate externally, but is that
270 // really a bad thing?
272 // TODO(scherkus): since SetPlaybackRate(0) is asynchronous and we don't want
273 // to hang the render thread during pause(), we record the time at the same
274 // time we pause and then return that value in currentTime(). Otherwise our
275 // clock can creep forward a little bit while the asynchronous
276 // SetPlaybackRate(0) is being executed.
277 bool paused_;
278 bool seeking_;
279 double playback_rate_;
280 base::TimeDelta paused_time_;
282 // TODO(scherkus): Replace with an explicit ended signal to HTMLMediaElement,
283 // see http://crbug.com/409280
284 bool ended_;
286 // Seek gets pending if another seek is in progress. Only last pending seek
287 // will have effect.
288 bool pending_seek_;
289 double pending_seek_seconds_;
291 // Tracks whether to issue time changed notifications during buffering state
292 // changes.
293 bool should_notify_time_changed_;
295 blink::WebMediaPlayerClient* client_;
297 base::WeakPtr<WebMediaPlayerDelegate> delegate_;
299 WebMediaPlayerParams::DeferLoadCB defer_load_cb_;
300 WebMediaPlayerParams::Context3DCB context_3d_cb_;
302 // Routes audio playback to either AudioRendererSink or WebAudio.
303 scoped_refptr<WebAudioSourceProviderImpl> audio_source_provider_;
305 bool supports_save_;
307 // These two are mutually exclusive:
308 // |data_source_| is used for regular resource loads.
309 // |chunk_demuxer_| is used for Media Source resource loads.
311 // |demuxer_| will contain the appropriate demuxer based on which resource
312 // load strategy we're using.
313 scoped_ptr<BufferedDataSource> data_source_;
314 scoped_ptr<Demuxer> demuxer_;
315 ChunkDemuxer* chunk_demuxer_;
317 BufferedDataSourceHostImpl buffered_data_source_host_;
319 // Video rendering members.
320 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_;
321 VideoFrameCompositor* compositor_; // Deleted on |compositor_task_runner_|.
322 SkCanvasVideoRenderer skcanvas_video_renderer_;
324 // The compositor layer for displaying the video content when using composited
325 // playback.
326 scoped_ptr<cc_blink::WebLayerImpl> video_weblayer_;
328 EncryptedMediaPlayerSupport encrypted_media_support_;
330 scoped_ptr<RendererFactory> renderer_factory_;
332 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerImpl);
335 } // namespace media
337 #endif // MEDIA_BLINK_WEBMEDIAPLAYER_IMPL_H_