1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef MEDIA_BLINK_WEBMEDIAPLAYER_IMPL_H_
#define MEDIA_BLINK_WEBMEDIAPLAYER_IMPL_H_
#include <string>
#include <vector>
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread.h"
#include "media/base/cdm_factory.h"
#include "media/base/media_export.h"
#include "media/base/pipeline.h"
#include "media/base/renderer_factory.h"
#include "media/base/text_track.h"
#include "media/blink/buffered_data_source.h"
#include "media/blink/buffered_data_source_host_impl.h"
#include "media/blink/encrypted_media_player_support.h"
#include "media/blink/skcanvas_video_renderer.h"
#include "media/blink/video_frame_compositor.h"
#include "media/blink/webmediaplayer_params.h"
#include "third_party/WebKit/public/platform/WebAudioSourceProvider.h"
#include "third_party/WebKit/public/platform/WebContentDecryptionModuleResult.h"
#include "third_party/WebKit/public/platform/WebMediaPlayer.h"
#include "third_party/WebKit/public/platform/WebMediaPlayerClient.h"
#include "url/gurl.h"
namespace blink {
class WebGraphicsContext3D;
class WebLocalFrame;
}
namespace base {
class SingleThreadTaskRunner;
}
namespace cc_blink {
class WebLayerImpl;
}
namespace media {
class AudioHardwareConfig;
class ChunkDemuxer;
class GpuVideoAcceleratorFactories;
class MediaLog;
class VideoFrameCompositor;
class WebAudioSourceProviderImpl;
class WebMediaPlayerDelegate;
class WebTextTrackImpl;
// The canonical implementation of blink::WebMediaPlayer that's backed by
// Pipeline. Handles normal resource loading, Media Source, and
// Encrypted Media.
class MEDIA_EXPORT WebMediaPlayerImpl
: public NON_EXPORTED_BASE(blink::WebMediaPlayer),
public base::SupportsWeakPtr<WebMediaPlayerImpl> {
public:
// Constructs a WebMediaPlayer implementation using Chromium's media stack.
// |delegate| may be null. |renderer| may also be null, in which case an
// internal renderer will be created.
// TODO(xhwang): Drop the internal renderer path and always pass in a renderer
// here.
WebMediaPlayerImpl(blink::WebLocalFrame* frame,
blink::WebMediaPlayerClient* client,
base::WeakPtr<WebMediaPlayerDelegate> delegate,
scoped_ptr<RendererFactory> renderer_factory,
scoped_ptr<CdmFactory> cdm_factory,
const WebMediaPlayerParams& params);
virtual ~WebMediaPlayerImpl();
virtual void load(LoadType load_type,
const blink::WebURL& url,
CORSMode cors_mode);
// Playback controls.
virtual void play();
virtual void pause();
virtual bool supportsSave() const;
virtual void seek(double seconds);
virtual void setRate(double rate);
virtual void setVolume(double volume);
virtual void setPreload(blink::WebMediaPlayer::Preload preload);
virtual blink::WebTimeRanges buffered() const;
virtual blink::WebTimeRanges seekable() const;
// Methods for painting.
virtual void paint(blink::WebCanvas* canvas,
const blink::WebRect& rect,
unsigned char alpha,
SkXfermode::Mode mode);
// True if the loaded media has a playable video/audio track.
virtual bool hasVideo() const;
virtual bool hasAudio() const;
// Dimensions of the video.
virtual blink::WebSize naturalSize() const;
// Getters of playback state.
virtual bool paused() const;
virtual bool seeking() const;
virtual double duration() const;
virtual double timelineOffset() const;
virtual double currentTime() const;
// Internal states of loading and network.
// TODO(hclam): Ask the pipeline about the state rather than having reading
// them from members which would cause race conditions.
virtual blink::WebMediaPlayer::NetworkState networkState() const;
virtual blink::WebMediaPlayer::ReadyState readyState() const;
virtual bool didLoadingProgress();
virtual bool hasSingleSecurityOrigin() const;
virtual bool didPassCORSAccessCheck() const;
virtual double mediaTimeForTimeValue(double timeValue) const;
virtual unsigned decodedFrameCount() const;
virtual unsigned droppedFrameCount() const;
virtual unsigned audioDecodedByteCount() const;
virtual unsigned videoDecodedByteCount() const;
// TODO(dshwang): remove |level|. crbug.com/443151
virtual bool copyVideoTextureToPlatformTexture(
blink::WebGraphicsContext3D* web_graphics_context,
unsigned int texture,
unsigned int level,
unsigned int internal_format,
unsigned int type,
bool premultiply_alpha,
bool flip_y);
virtual bool copyVideoTextureToPlatformTexture(
blink::WebGraphicsContext3D* web_graphics_context,
unsigned int texture,
unsigned int internal_format,
unsigned int type,
bool premultiply_alpha,
bool flip_y);
virtual blink::WebAudioSourceProvider* audioSourceProvider();
virtual MediaKeyException generateKeyRequest(
const blink::WebString& key_system,
const unsigned char* init_data,
unsigned init_data_length);
virtual MediaKeyException addKey(const blink::WebString& key_system,
const unsigned char* key,
unsigned key_length,
const unsigned char* init_data,
unsigned init_data_length,
const blink::WebString& session_id);
virtual MediaKeyException cancelKeyRequest(
const blink::WebString& key_system,
const blink::WebString& session_id);
virtual void setContentDecryptionModule(
blink::WebContentDecryptionModule* cdm,
blink::WebContentDecryptionModuleResult result);
void OnPipelineSeeked(bool time_changed, PipelineStatus status);
void OnPipelineEnded();
void OnPipelineError(PipelineStatus error);
void OnPipelineMetadata(PipelineMetadata metadata);
void OnPipelineBufferingStateChanged(BufferingState buffering_state);
void OnDemuxerOpened();
void OnAddTextTrack(const TextTrackConfig& config,
const AddTextTrackDoneCB& done_cb);
private:
// Called after |defer_load_cb_| has decided to allow the load. If
// |defer_load_cb_| is null this is called immediately.
void DoLoad(LoadType load_type,
const blink::WebURL& url,
CORSMode cors_mode);
// Called after asynchronous initialization of a data source completed.
void DataSourceInitialized(bool success);
// Called when the data source is downloading or paused.
void NotifyDownloading(bool is_downloading);
// Creates a Renderer that will be used by the |pipeline_|.
scoped_ptr<Renderer> CreateRenderer();
// Finishes starting the pipeline due to a call to load().
void StartPipeline();
// Helpers that set the network/ready state and notifies the client if
// they've changed.
void SetNetworkState(blink::WebMediaPlayer::NetworkState state);
void SetReadyState(blink::WebMediaPlayer::ReadyState state);
// Gets the duration value reported by the pipeline.
double GetPipelineDuration() const;
// Callbacks from |pipeline_| that are forwarded to |client_|.
void OnDurationChanged();
void OnNaturalSizeChanged(gfx::Size size);
void OnOpacityChanged(bool opaque);
// Called by VideoRendererImpl on its internal thread with the new frame to be
// painted.
void FrameReady(const scoped_refptr<VideoFrame>& frame);
// Returns the current video frame from |compositor_|. Blocks until the
// compositor can return the frame.
scoped_refptr<VideoFrame> GetCurrentFrameFromCompositor();
// Called when the demuxer encounters encrypted streams.
void OnEncryptedMediaInitData(const std::string& init_data_type,
const std::vector<uint8>& init_data);
// Called when a decoder detects that the key needed to decrypt the stream
// is not available.
void OnWaitingForDecryptionKey();
void SetCdm(CdmContext* cdm_context, const CdmAttachedCB& cdm_attached_cb);
// Called when a CDM has been attached to the |pipeline_|.
void OnCdmAttached(blink::WebContentDecryptionModuleResult result,
bool success);
// Updates |paused_time_| to the current media time with consideration for the
// |ended_| state by clamping current time to duration upon |ended_|.
void UpdatePausedTime();
blink::WebLocalFrame* frame_;
// TODO(hclam): get rid of these members and read from the pipeline directly.
blink::WebMediaPlayer::NetworkState network_state_;
blink::WebMediaPlayer::ReadyState ready_state_;
// Preload state for when |data_source_| is created after setPreload().
BufferedDataSource::Preload preload_;
// Task runner for posting tasks on Chrome's main thread. Also used
// for DCHECKs so methods calls won't execute in the wrong thread.
const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_;
scoped_refptr<MediaLog> media_log_;
Pipeline pipeline_;
// The LoadType passed in the |load_type| parameter of the load() call.
LoadType load_type_;
// Cache of metadata for answering hasAudio(), hasVideo(), and naturalSize().
PipelineMetadata pipeline_metadata_;
// Whether the video is known to be opaque or not.
bool opaque_;
// Playback state.
//
// TODO(scherkus): we have these because Pipeline favours the simplicity of a
// single "playback rate" over worrying about paused/stopped etc... It forces
// all clients to manage the pause+playback rate externally, but is that
// really a bad thing?
//
// TODO(scherkus): since SetPlaybackRate(0) is asynchronous and we don't want
// to hang the render thread during pause(), we record the time at the same
// time we pause and then return that value in currentTime(). Otherwise our
// clock can creep forward a little bit while the asynchronous
// SetPlaybackRate(0) is being executed.
bool paused_;
bool seeking_;
double playback_rate_;
base::TimeDelta paused_time_;
// TODO(scherkus): Replace with an explicit ended signal to HTMLMediaElement,
// see http://crbug.com/409280
bool ended_;
// Seek gets pending if another seek is in progress. Only last pending seek
// will have effect.
bool pending_seek_;
double pending_seek_seconds_;
// Tracks whether to issue time changed notifications during buffering state
// changes.
bool should_notify_time_changed_;
blink::WebMediaPlayerClient* client_;
base::WeakPtr<WebMediaPlayerDelegate> delegate_;
WebMediaPlayerParams::DeferLoadCB defer_load_cb_;
WebMediaPlayerParams::Context3DCB context_3d_cb_;
// Routes audio playback to either AudioRendererSink or WebAudio.
scoped_refptr<WebAudioSourceProviderImpl> audio_source_provider_;
bool supports_save_;
// These two are mutually exclusive:
// |data_source_| is used for regular resource loads.
// |chunk_demuxer_| is used for Media Source resource loads.
//
// |demuxer_| will contain the appropriate demuxer based on which resource
// load strategy we're using.
scoped_ptr<BufferedDataSource> data_source_;
scoped_ptr<Demuxer> demuxer_;
ChunkDemuxer* chunk_demuxer_;
BufferedDataSourceHostImpl buffered_data_source_host_;
// Video rendering members.
scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_;
VideoFrameCompositor* compositor_; // Deleted on |compositor_task_runner_|.
SkCanvasVideoRenderer skcanvas_video_renderer_;
// The compositor layer for displaying the video content when using composited
// playback.
scoped_ptr<cc_blink::WebLayerImpl> video_weblayer_;
EncryptedMediaPlayerSupport encrypted_media_support_;
scoped_ptr<RendererFactory> renderer_factory_;
DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerImpl);
};
} // namespace media
#endif // MEDIA_BLINK_WEBMEDIAPLAYER_IMPL_H_
|