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_
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"
35 class WebGraphicsContext3D
;
40 class SingleThreadTaskRunner
;
49 class AudioHardwareConfig
;
51 class GpuVideoAcceleratorFactories
;
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
61 class MEDIA_EXPORT WebMediaPlayerImpl
62 : public NON_EXPORTED_BASE(blink::WebMediaPlayer
),
63 public base::SupportsWeakPtr
<WebMediaPlayerImpl
> {
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
70 WebMediaPlayerImpl(blink::WebLocalFrame
* frame
,
71 blink::WebMediaPlayerClient
* client
,
72 base::WeakPtr
<WebMediaPlayerDelegate
> delegate
,
73 scoped_ptr
<RendererFactory
> renderer_factory
,
74 scoped_ptr
<CdmFactory
> cdm_factory
,
75 const WebMediaPlayerParams
& params
);
76 virtual ~WebMediaPlayerImpl();
78 virtual void load(LoadType load_type
,
79 const blink::WebURL
& url
,
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
,
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
,
136 unsigned int internal_format
,
138 bool premultiply_alpha
,
140 virtual bool copyVideoTextureToPlatformTexture(
141 blink::WebGraphicsContext3D
* web_graphics_context
,
142 unsigned int texture
,
143 unsigned int internal_format
,
145 bool premultiply_alpha
,
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
,
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
);
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
,
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
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
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(const std::string
& init_data_type
,
221 const std::vector
<uint8
>& init_data
);
223 // Called when a decoder detects that the key needed to decrypt the stream
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
,
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_
;
254 // The LoadType passed in the |load_type| parameter of the load() call.
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.
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.
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
284 // Seek gets pending if another seek is in progress. Only last pending seek
287 double pending_seek_seconds_
;
289 // Tracks whether to issue time changed notifications during buffering state
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_
;
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
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
);
335 #endif // MEDIA_BLINK_WEBMEDIAPLAYER_IMPL_H_