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 CONTENT_RENDERER_MEDIA_WEBMEDIAPLAYER_IMPL_H_
6 #define CONTENT_RENDERER_MEDIA_WEBMEDIAPLAYER_IMPL_H_
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/threading/thread.h"
16 #include "content/renderer/media/buffered_data_source_host_impl.h"
17 #include "content/renderer/media/video_frame_compositor.h"
18 #include "media/base/audio_renderer_sink.h"
19 // TODO(xhwang): Remove when we remove prefixed EME implementation.
20 #include "media/base/media_keys.h"
21 #include "media/base/pipeline.h"
22 #include "media/base/text_track.h"
23 #include "media/filters/skcanvas_video_renderer.h"
24 #include "skia/ext/platform_canvas.h"
25 #include "third_party/WebKit/public/platform/WebAudioSourceProvider.h"
26 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleResult.h"
27 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
28 #include "third_party/WebKit/public/platform/WebMediaPlayer.h"
29 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h"
37 class SingleThreadTaskRunner
;
46 class GpuVideoAcceleratorFactories
;
52 class BufferedDataSource
;
53 class EncryptedMediaPlayerSupport
;
54 class VideoFrameCompositor
;
55 class WebAudioSourceProviderImpl
;
56 class WebMediaPlayerDelegate
;
57 class WebMediaPlayerParams
;
58 class WebTextTrackImpl
;
60 // The canonical implementation of blink::WebMediaPlayer that's backed by
61 // media::Pipeline. Handles normal resource loading, Media Source, and
63 class WebMediaPlayerImpl
64 : public blink::WebMediaPlayer
,
65 public base::SupportsWeakPtr
<WebMediaPlayerImpl
> {
67 // Constructs a WebMediaPlayer implementation using Chromium's media stack.
68 // |delegate| may be null.
69 WebMediaPlayerImpl(blink::WebLocalFrame
* frame
,
70 blink::WebMediaPlayerClient
* client
,
71 base::WeakPtr
<WebMediaPlayerDelegate
> delegate
,
72 const WebMediaPlayerParams
& params
);
73 virtual ~WebMediaPlayerImpl();
75 virtual void load(LoadType load_type
,
76 const blink::WebURL
& url
,
82 virtual bool supportsSave() const;
83 virtual void seek(double seconds
);
84 virtual void setRate(double rate
);
85 virtual void setVolume(double volume
);
86 virtual void setPreload(blink::WebMediaPlayer::Preload preload
);
87 virtual blink::WebTimeRanges
buffered() const;
88 virtual double maxTimeSeekable() const;
90 // Methods for painting.
91 virtual void paint(blink::WebCanvas
* canvas
,
92 const blink::WebRect
& rect
,
94 SkXfermode::Mode mode
);
95 // TODO(dshwang): remove it because above method replaces. crbug.com/401027
96 virtual void paint(blink::WebCanvas
* canvas
,
97 const blink::WebRect
& rect
,
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
);
166 virtual void setContentDecryptionModuleSync(
167 blink::WebContentDecryptionModule
* cdm
);
169 void OnPipelineSeeked(bool time_changed
, media::PipelineStatus status
);
170 void OnPipelineEnded();
171 void OnPipelineError(media::PipelineStatus error
);
172 void OnPipelineMetadata(media::PipelineMetadata metadata
);
173 void OnPipelineBufferingStateChanged(media::BufferingState buffering_state
);
174 void OnDemuxerOpened();
175 void OnAddTextTrack(const media::TextTrackConfig
& config
,
176 const media::AddTextTrackDoneCB
& done_cb
);
179 // Called after |defer_load_cb_| has decided to allow the load. If
180 // |defer_load_cb_| is null this is called immediately.
181 void DoLoad(LoadType load_type
,
182 const blink::WebURL
& url
,
185 // Called after asynchronous initialization of a data source completed.
186 void DataSourceInitialized(bool success
);
188 // Called when the data source is downloading or paused.
189 void NotifyDownloading(bool is_downloading
);
191 // Creates a media::Renderer that will be used by the |pipeline_|.
192 scoped_ptr
<media::Renderer
> CreateRenderer();
194 // Finishes starting the pipeline due to a call to load().
195 void StartPipeline();
197 // Helpers that set the network/ready state and notifies the client if
199 void SetNetworkState(blink::WebMediaPlayer::NetworkState state
);
200 void SetReadyState(blink::WebMediaPlayer::ReadyState state
);
202 // Gets the duration value reported by the pipeline.
203 double GetPipelineDuration() const;
205 // Callbacks from |pipeline_| that are forwarded to |client_|.
206 void OnDurationChanged();
207 void OnNaturalSizeChanged(gfx::Size size
);
208 void OnOpacityChanged(bool opaque
);
210 // Called by VideoRendererImpl on its internal thread with the new frame to be
212 void FrameReady(const scoped_refptr
<media::VideoFrame
>& frame
);
214 // Called when the ContentDecryptionModule has been attached to the
215 // pipeline/decoders.
216 void ContentDecryptionModuleAttached(
217 blink::WebContentDecryptionModuleResult result
,
220 // Returns the current video frame from |compositor_|. Blocks until the
221 // compositor can return the frame.
222 scoped_refptr
<media::VideoFrame
> GetCurrentFrameFromCompositor();
224 blink::WebLocalFrame
* frame_
;
226 // TODO(hclam): get rid of these members and read from the pipeline directly.
227 blink::WebMediaPlayer::NetworkState network_state_
;
228 blink::WebMediaPlayer::ReadyState ready_state_
;
230 // Preload state for when |data_source_| is created after setPreload().
231 content::Preload preload_
;
233 // Task runner for posting tasks on Chrome's main thread. Also used
234 // for DCHECKs so methods calls won't execute in the wrong thread.
235 const scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner_
;
237 scoped_refptr
<base::SingleThreadTaskRunner
> media_task_runner_
;
238 scoped_refptr
<media::MediaLog
> media_log_
;
239 media::Pipeline pipeline_
;
241 // The LoadType passed in the |load_type| parameter of the load() call.
244 // Cache of metadata for answering hasAudio(), hasVideo(), and naturalSize().
245 media::PipelineMetadata pipeline_metadata_
;
247 // Whether the video is known to be opaque or not.
252 // TODO(scherkus): we have these because Pipeline favours the simplicity of a
253 // single "playback rate" over worrying about paused/stopped etc... It forces
254 // all clients to manage the pause+playback rate externally, but is that
255 // really a bad thing?
257 // TODO(scherkus): since SetPlaybackRate(0) is asynchronous and we don't want
258 // to hang the render thread during pause(), we record the time at the same
259 // time we pause and then return that value in currentTime(). Otherwise our
260 // clock can creep forward a little bit while the asynchronous
261 // SetPlaybackRate(0) is being executed.
264 double playback_rate_
;
265 base::TimeDelta paused_time_
;
267 // Seek gets pending if another seek is in progress. Only last pending seek
270 double pending_seek_seconds_
;
272 // Tracks whether to issue time changed notifications during buffering state
274 bool should_notify_time_changed_
;
276 blink::WebMediaPlayerClient
* client_
;
278 base::WeakPtr
<WebMediaPlayerDelegate
> delegate_
;
280 base::Callback
<void(const base::Closure
&)> defer_load_cb_
;
282 // Factories for supporting video accelerators. May be null.
283 scoped_refptr
<media::GpuVideoAcceleratorFactories
> gpu_factories_
;
285 // Routes audio playback to either AudioRendererSink or WebAudio.
286 scoped_refptr
<WebAudioSourceProviderImpl
> audio_source_provider_
;
290 // These two are mutually exclusive:
291 // |data_source_| is used for regular resource loads.
292 // |chunk_demuxer_| is used for Media Source resource loads.
294 // |demuxer_| will contain the appropriate demuxer based on which resource
295 // load strategy we're using.
296 scoped_ptr
<BufferedDataSource
> data_source_
;
297 scoped_ptr
<media::Demuxer
> demuxer_
;
298 media::ChunkDemuxer
* chunk_demuxer_
;
300 BufferedDataSourceHostImpl buffered_data_source_host_
;
302 // Video rendering members.
303 scoped_refptr
<base::SingleThreadTaskRunner
> compositor_task_runner_
;
304 VideoFrameCompositor
* compositor_
; // Deleted on |compositor_task_runner_|.
305 media::SkCanvasVideoRenderer skcanvas_video_renderer_
;
307 // The compositor layer for displaying the video content when using composited
309 scoped_ptr
<cc_blink::WebLayerImpl
> video_weblayer_
;
311 // Text track objects get a unique index value when they're created.
312 int text_track_index_
;
314 scoped_ptr
<EncryptedMediaPlayerSupport
> encrypted_media_support_
;
316 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerImpl
);
319 } // namespace content
321 #endif // CONTENT_RENDERER_MEDIA_WEBMEDIAPLAYER_IMPL_H_