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/audio_renderer_sink.h"
18 #include "media/base/cdm_factory.h"
19 #include "media/base/media_export.h"
20 #include "media/base/pipeline.h"
21 #include "media/base/renderer.h"
22 #include "media/base/text_track.h"
23 #include "media/blink/buffered_data_source.h"
24 #include "media/blink/buffered_data_source_host_impl.h"
25 #include "media/blink/encrypted_media_player_support.h"
26 #include "media/blink/video_frame_compositor.h"
27 #include "media/filters/skcanvas_video_renderer.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/WebGraphicsContext3D.h"
31 #include "third_party/WebKit/public/platform/WebMediaPlayer.h"
32 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h"
40 class SingleThreadTaskRunner
;
49 class AudioHardwareConfig
;
51 class GpuVideoAcceleratorFactories
;
53 class VideoFrameCompositor
;
54 class WebAudioSourceProviderImpl
;
55 class WebMediaPlayerDelegate
;
56 class WebMediaPlayerParams
;
57 class WebTextTrackImpl
;
59 // The canonical implementation of blink::WebMediaPlayer that's backed by
60 // Pipeline. Handles normal resource loading, Media Source, and
62 class MEDIA_EXPORT WebMediaPlayerImpl
63 : public NON_EXPORTED_BASE(blink::WebMediaPlayer
),
64 public base::SupportsWeakPtr
<WebMediaPlayerImpl
> {
66 // Constructs a WebMediaPlayer implementation using Chromium's media stack.
67 // |delegate| may be null. |renderer| may also be null, in which case an
68 // internal renderer will be created.
69 // TODO(xhwang): Drop the internal renderer path and always pass in a renderer
71 WebMediaPlayerImpl(blink::WebLocalFrame
* frame
,
72 blink::WebMediaPlayerClient
* client
,
73 base::WeakPtr
<WebMediaPlayerDelegate
> delegate
,
74 scoped_ptr
<Renderer
> renderer
,
75 scoped_ptr
<CdmFactory
> cdm_factory
,
76 const WebMediaPlayerParams
& params
);
77 virtual ~WebMediaPlayerImpl();
79 virtual void load(LoadType load_type
,
80 const blink::WebURL
& url
,
86 virtual bool supportsSave() const;
87 virtual void seek(double seconds
);
88 virtual void setRate(double rate
);
89 virtual void setVolume(double volume
);
90 virtual void setPreload(blink::WebMediaPlayer::Preload preload
);
91 virtual blink::WebTimeRanges
buffered() const;
92 virtual blink::WebTimeRanges
seekable() const;
94 // Methods for painting.
95 virtual void paint(blink::WebCanvas
* canvas
,
96 const blink::WebRect
& rect
,
98 SkXfermode::Mode mode
);
100 // True if the loaded media has a playable video/audio track.
101 virtual bool hasVideo() const;
102 virtual bool hasAudio() const;
104 // Dimensions of the video.
105 virtual blink::WebSize
naturalSize() const;
107 // Getters of playback state.
108 virtual bool paused() const;
109 virtual bool seeking() const;
110 virtual double duration() const;
111 virtual double timelineOffset() const;
112 virtual double currentTime() const;
114 // Internal states of loading and network.
115 // TODO(hclam): Ask the pipeline about the state rather than having reading
116 // them from members which would cause race conditions.
117 virtual blink::WebMediaPlayer::NetworkState
networkState() const;
118 virtual blink::WebMediaPlayer::ReadyState
readyState() const;
120 virtual bool didLoadingProgress();
122 virtual bool hasSingleSecurityOrigin() const;
123 virtual bool didPassCORSAccessCheck() const;
125 virtual double mediaTimeForTimeValue(double timeValue
) const;
127 virtual unsigned decodedFrameCount() const;
128 virtual unsigned droppedFrameCount() const;
129 virtual unsigned audioDecodedByteCount() const;
130 virtual unsigned videoDecodedByteCount() const;
132 virtual bool copyVideoTextureToPlatformTexture(
133 blink::WebGraphicsContext3D
* web_graphics_context
,
134 unsigned int texture
,
136 unsigned int internal_format
,
138 bool premultiply_alpha
,
141 virtual blink::WebAudioSourceProvider
* audioSourceProvider();
143 virtual MediaKeyException
generateKeyRequest(
144 const blink::WebString
& key_system
,
145 const unsigned char* init_data
,
146 unsigned init_data_length
);
148 virtual MediaKeyException
addKey(const blink::WebString
& key_system
,
149 const unsigned char* key
,
151 const unsigned char* init_data
,
152 unsigned init_data_length
,
153 const blink::WebString
& session_id
);
155 virtual MediaKeyException
cancelKeyRequest(
156 const blink::WebString
& key_system
,
157 const blink::WebString
& session_id
);
159 // TODO(jrummell): Remove this method once Blink updated to use the other
161 virtual void setContentDecryptionModule(
162 blink::WebContentDecryptionModule
* cdm
);
163 virtual void setContentDecryptionModule(
164 blink::WebContentDecryptionModule
* cdm
,
165 blink::WebContentDecryptionModuleResult result
);
167 void OnPipelineSeeked(bool time_changed
, PipelineStatus status
);
168 void OnPipelineEnded();
169 void OnPipelineError(PipelineStatus error
);
170 void OnPipelineMetadata(PipelineMetadata metadata
);
171 void OnPipelineBufferingStateChanged(BufferingState buffering_state
);
172 void OnDemuxerOpened();
173 void OnAddTextTrack(const TextTrackConfig
& config
,
174 const AddTextTrackDoneCB
& done_cb
);
177 // Called after |defer_load_cb_| has decided to allow the load. If
178 // |defer_load_cb_| is null this is called immediately.
179 void DoLoad(LoadType load_type
,
180 const blink::WebURL
& url
,
183 // Called after asynchronous initialization of a data source completed.
184 void DataSourceInitialized(bool success
);
186 // Called when the data source is downloading or paused.
187 void NotifyDownloading(bool is_downloading
);
189 // Creates a Renderer that will be used by the |pipeline_|.
190 scoped_ptr
<Renderer
> CreateRenderer();
192 // Finishes starting the pipeline due to a call to load().
193 void StartPipeline();
195 // Helpers that set the network/ready state and notifies the client if
197 void SetNetworkState(blink::WebMediaPlayer::NetworkState state
);
198 void SetReadyState(blink::WebMediaPlayer::ReadyState state
);
200 // Gets the duration value reported by the pipeline.
201 double GetPipelineDuration() const;
203 // Callbacks from |pipeline_| that are forwarded to |client_|.
204 void OnDurationChanged();
205 void OnNaturalSizeChanged(gfx::Size size
);
206 void OnOpacityChanged(bool opaque
);
208 // Called by VideoRendererImpl on its internal thread with the new frame to be
210 void FrameReady(const scoped_refptr
<VideoFrame
>& frame
);
212 // Returns the current video frame from |compositor_|. Blocks until the
213 // compositor can return the frame.
214 scoped_refptr
<VideoFrame
> GetCurrentFrameFromCompositor();
216 blink::WebLocalFrame
* frame_
;
218 // TODO(hclam): get rid of these members and read from the pipeline directly.
219 blink::WebMediaPlayer::NetworkState network_state_
;
220 blink::WebMediaPlayer::ReadyState ready_state_
;
222 // Preload state for when |data_source_| is created after setPreload().
223 BufferedDataSource::Preload preload_
;
225 // Task runner for posting tasks on Chrome's main thread. Also used
226 // for DCHECKs so methods calls won't execute in the wrong thread.
227 const scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner_
;
229 scoped_refptr
<base::SingleThreadTaskRunner
> media_task_runner_
;
230 scoped_refptr
<MediaLog
> media_log_
;
233 // The LoadType passed in the |load_type| parameter of the load() call.
236 // Cache of metadata for answering hasAudio(), hasVideo(), and naturalSize().
237 PipelineMetadata pipeline_metadata_
;
239 // Whether the video is known to be opaque or not.
244 // TODO(scherkus): we have these because Pipeline favours the simplicity of a
245 // single "playback rate" over worrying about paused/stopped etc... It forces
246 // all clients to manage the pause+playback rate externally, but is that
247 // really a bad thing?
249 // TODO(scherkus): since SetPlaybackRate(0) is asynchronous and we don't want
250 // to hang the render thread during pause(), we record the time at the same
251 // time we pause and then return that value in currentTime(). Otherwise our
252 // clock can creep forward a little bit while the asynchronous
253 // SetPlaybackRate(0) is being executed.
256 double playback_rate_
;
257 base::TimeDelta paused_time_
;
259 // TODO(scherkus): Replace with an explicit ended signal to HTMLMediaElement,
260 // see http://crbug.com/409280
263 // Seek gets pending if another seek is in progress. Only last pending seek
266 double pending_seek_seconds_
;
268 // Tracks whether to issue time changed notifications during buffering state
270 bool should_notify_time_changed_
;
272 blink::WebMediaPlayerClient
* client_
;
274 base::WeakPtr
<WebMediaPlayerDelegate
> delegate_
;
276 base::Callback
<void(const base::Closure
&)> defer_load_cb_
;
278 // Factories for supporting video accelerators. May be null.
279 scoped_refptr
<GpuVideoAcceleratorFactories
> gpu_factories_
;
281 // Routes audio playback to either AudioRendererSink or WebAudio.
282 scoped_refptr
<WebAudioSourceProviderImpl
> audio_source_provider_
;
286 // These two are mutually exclusive:
287 // |data_source_| is used for regular resource loads.
288 // |chunk_demuxer_| is used for Media Source resource loads.
290 // |demuxer_| will contain the appropriate demuxer based on which resource
291 // load strategy we're using.
292 scoped_ptr
<BufferedDataSource
> data_source_
;
293 scoped_ptr
<Demuxer
> demuxer_
;
294 ChunkDemuxer
* chunk_demuxer_
;
296 BufferedDataSourceHostImpl buffered_data_source_host_
;
298 // Video rendering members.
299 scoped_refptr
<base::SingleThreadTaskRunner
> compositor_task_runner_
;
300 VideoFrameCompositor
* compositor_
; // Deleted on |compositor_task_runner_|.
301 SkCanvasVideoRenderer skcanvas_video_renderer_
;
303 // The compositor layer for displaying the video content when using composited
305 scoped_ptr
<cc_blink::WebLayerImpl
> video_weblayer_
;
307 // Text track objects get a unique index value when they're created.
308 int text_track_index_
;
310 EncryptedMediaPlayerSupport encrypted_media_support_
;
312 const AudioHardwareConfig
& audio_hardware_config_
;
314 scoped_ptr
<Renderer
> renderer_
;
316 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerImpl
);
321 #endif // MEDIA_BLINK_WEBMEDIAPLAYER_IMPL_H_