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/video_frame_compositor.h"
26 #include "media/blink/webmediaplayer_params.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/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 virtual bool copyVideoTextureToPlatformTexture(
132 blink::WebGraphicsContext3D
* web_graphics_context
,
133 unsigned int texture
,
135 unsigned int internal_format
,
137 bool premultiply_alpha
,
140 virtual blink::WebAudioSourceProvider
* audioSourceProvider();
142 virtual MediaKeyException
generateKeyRequest(
143 const blink::WebString
& key_system
,
144 const unsigned char* init_data
,
145 unsigned init_data_length
);
147 virtual MediaKeyException
addKey(const blink::WebString
& key_system
,
148 const unsigned char* key
,
150 const unsigned char* init_data
,
151 unsigned init_data_length
,
152 const blink::WebString
& session_id
);
154 virtual MediaKeyException
cancelKeyRequest(
155 const blink::WebString
& key_system
,
156 const blink::WebString
& session_id
);
158 virtual void setContentDecryptionModule(
159 blink::WebContentDecryptionModule
* cdm
,
160 blink::WebContentDecryptionModuleResult result
);
162 void OnPipelineSeeked(bool time_changed
, PipelineStatus status
);
163 void OnPipelineEnded();
164 void OnPipelineError(PipelineStatus error
);
165 void OnPipelineMetadata(PipelineMetadata metadata
);
166 void OnPipelineBufferingStateChanged(BufferingState buffering_state
);
167 void OnDemuxerOpened();
168 void OnAddTextTrack(const TextTrackConfig
& config
,
169 const AddTextTrackDoneCB
& done_cb
);
172 // Called after |defer_load_cb_| has decided to allow the load. If
173 // |defer_load_cb_| is null this is called immediately.
174 void DoLoad(LoadType load_type
,
175 const blink::WebURL
& url
,
178 // Called after asynchronous initialization of a data source completed.
179 void DataSourceInitialized(bool success
);
181 // Called when the data source is downloading or paused.
182 void NotifyDownloading(bool is_downloading
);
184 // Creates a Renderer that will be used by the |pipeline_|.
185 scoped_ptr
<Renderer
> CreateRenderer();
187 // Finishes starting the pipeline due to a call to load().
188 void StartPipeline();
190 // Helpers that set the network/ready state and notifies the client if
192 void SetNetworkState(blink::WebMediaPlayer::NetworkState state
);
193 void SetReadyState(blink::WebMediaPlayer::ReadyState state
);
195 // Gets the duration value reported by the pipeline.
196 double GetPipelineDuration() const;
198 // Callbacks from |pipeline_| that are forwarded to |client_|.
199 void OnDurationChanged();
200 void OnNaturalSizeChanged(gfx::Size size
);
201 void OnOpacityChanged(bool opaque
);
203 // Called by VideoRendererImpl on its internal thread with the new frame to be
205 void FrameReady(const scoped_refptr
<VideoFrame
>& frame
);
207 // Returns the current video frame from |compositor_|. Blocks until the
208 // compositor can return the frame.
209 scoped_refptr
<VideoFrame
> GetCurrentFrameFromCompositor();
211 // Called when the demuxer encounters encrypted streams.
212 void OnEncryptedMediaInitData(const std::string
& init_data_type
,
213 const std::vector
<uint8
>& init_data
);
215 void SetCdm(CdmContext
* cdm_context
, const CdmAttachedCB
& cdm_attached_cb
);
217 // Called when a CDM has been attached to the |pipeline_|.
218 void OnCdmAttached(blink::WebContentDecryptionModuleResult result
,
221 // Updates |paused_time_| to the current media time with consideration for the
222 // |ended_| state by clamping current time to duration upon |ended_|.
223 void UpdatePausedTime();
225 blink::WebLocalFrame
* frame_
;
227 // TODO(hclam): get rid of these members and read from the pipeline directly.
228 blink::WebMediaPlayer::NetworkState network_state_
;
229 blink::WebMediaPlayer::ReadyState ready_state_
;
231 // Preload state for when |data_source_| is created after setPreload().
232 BufferedDataSource::Preload preload_
;
234 // Task runner for posting tasks on Chrome's main thread. Also used
235 // for DCHECKs so methods calls won't execute in the wrong thread.
236 const scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner_
;
238 scoped_refptr
<base::SingleThreadTaskRunner
> media_task_runner_
;
239 scoped_refptr
<MediaLog
> media_log_
;
242 // The LoadType passed in the |load_type| parameter of the load() call.
245 // Cache of metadata for answering hasAudio(), hasVideo(), and naturalSize().
246 PipelineMetadata pipeline_metadata_
;
248 // Whether the video is known to be opaque or not.
253 // TODO(scherkus): we have these because Pipeline favours the simplicity of a
254 // single "playback rate" over worrying about paused/stopped etc... It forces
255 // all clients to manage the pause+playback rate externally, but is that
256 // really a bad thing?
258 // TODO(scherkus): since SetPlaybackRate(0) is asynchronous and we don't want
259 // to hang the render thread during pause(), we record the time at the same
260 // time we pause and then return that value in currentTime(). Otherwise our
261 // clock can creep forward a little bit while the asynchronous
262 // SetPlaybackRate(0) is being executed.
265 double playback_rate_
;
266 base::TimeDelta paused_time_
;
268 // TODO(scherkus): Replace with an explicit ended signal to HTMLMediaElement,
269 // see http://crbug.com/409280
272 // Seek gets pending if another seek is in progress. Only last pending seek
275 double pending_seek_seconds_
;
277 // Tracks whether to issue time changed notifications during buffering state
279 bool should_notify_time_changed_
;
281 blink::WebMediaPlayerClient
* client_
;
283 base::WeakPtr
<WebMediaPlayerDelegate
> delegate_
;
285 WebMediaPlayerParams::DeferLoadCB defer_load_cb_
;
286 WebMediaPlayerParams::Context3DCB context_3d_cb_
;
288 // Routes audio playback to either AudioRendererSink or WebAudio.
289 scoped_refptr
<WebAudioSourceProviderImpl
> audio_source_provider_
;
293 // These two are mutually exclusive:
294 // |data_source_| is used for regular resource loads.
295 // |chunk_demuxer_| is used for Media Source resource loads.
297 // |demuxer_| will contain the appropriate demuxer based on which resource
298 // load strategy we're using.
299 scoped_ptr
<BufferedDataSource
> data_source_
;
300 scoped_ptr
<Demuxer
> demuxer_
;
301 ChunkDemuxer
* chunk_demuxer_
;
303 BufferedDataSourceHostImpl buffered_data_source_host_
;
305 // Video rendering members.
306 scoped_refptr
<base::SingleThreadTaskRunner
> compositor_task_runner_
;
307 VideoFrameCompositor
* compositor_
; // Deleted on |compositor_task_runner_|.
308 SkCanvasVideoRenderer skcanvas_video_renderer_
;
310 // The compositor layer for displaying the video content when using composited
312 scoped_ptr
<cc_blink::WebLayerImpl
> video_weblayer_
;
314 EncryptedMediaPlayerSupport encrypted_media_support_
;
316 scoped_ptr
<RendererFactory
> renderer_factory_
;
318 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerImpl
);
323 #endif // MEDIA_BLINK_WEBMEDIAPLAYER_IMPL_H_