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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
// Copyright (c) 2008 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.
#include "chrome/renderer/media/audio_renderer_impl.h"
#include "chrome/renderer/media/data_source_impl.h"
#include "chrome/renderer/media/video_renderer_impl.h"
#include "chrome/renderer/render_view.h"
#include "chrome/renderer/webmediaplayer_delegate_impl.h"
#include "googleurl/src/gurl.h"
#if defined(OS_WIN)
// FFmpeg is not ready for Linux and Mac yet.
#include "media/filters/ffmpeg_demuxer.h"
#endif
/////////////////////////////////////////////////////////////////////////////
// Task to be posted on main thread that fire WebMediaPlayer methods.
class NotifyWebMediaPlayerTask : public CancelableTask {
public:
NotifyWebMediaPlayerTask(WebMediaPlayerDelegateImpl* delegate,
WebMediaPlayerMethod method)
: delegate_(delegate),
method_(method) {}
virtual void Run() {
if (delegate_) {
(delegate_->web_media_player()->*(method_))();
delegate_->DidTask(this);
}
}
virtual void Cancel() {
delegate_ = NULL;
}
private:
WebMediaPlayerDelegateImpl* delegate_;
WebMediaPlayerMethod method_;
DISALLOW_COPY_AND_ASSIGN(NotifyWebMediaPlayerTask);
};
/////////////////////////////////////////////////////////////////////////////
// WebMediaPlayerDelegateImpl implementation
WebMediaPlayerDelegateImpl::WebMediaPlayerDelegateImpl(RenderView* view)
: network_state_(webkit_glue::WebMediaPlayer::EMPTY),
ready_state_(webkit_glue::WebMediaPlayer::DATA_UNAVAILABLE),
main_loop_(NULL),
filter_factory_(new media::FilterFactoryCollection()),
audio_renderer_(NULL),
video_renderer_(NULL),
data_source_(NULL),
web_media_player_(NULL),
view_(view),
tasks_(kLastTaskIndex) {
// TODO(hclam): Add filter factory for demuxer and decoders.
#if defined(OS_WIN)
// FFmpeg is not ready for Linux and Mac yet.
filter_factory_->AddFactory(media::FFmpegDemuxer::CreateFilterFactory());
#endif
filter_factory_->AddFactory(AudioRendererImpl::CreateFactory(this));
filter_factory_->AddFactory(VideoRendererImpl::CreateFactory(this));
filter_factory_->AddFactory(DataSourceImpl::CreateFactory(this));
}
WebMediaPlayerDelegateImpl::~WebMediaPlayerDelegateImpl() {
// Stop the pipeline in the first place so we won't receive any more method
// calls from it.
pipeline_.Stop();
// Cancel all tasks posted on the main_loop_.
CancelAllTasks();
// After cancelling all tasks, we are sure there will be no calls to
// web_media_player_, so we are safe to delete it.
if (web_media_player_) {
delete web_media_player_;
}
// Finally tell the main_loop_ we don't want to be notified of destruction
// event.
if (main_loop_) {
main_loop_->RemoveDestructionObserver(this);
}
}
void WebMediaPlayerDelegateImpl::Initialize(
webkit_glue::WebMediaPlayer* media_player) {
DCHECK(!web_media_player_);
web_media_player_ = media_player;
// Saves the current message loop.
DCHECK(!main_loop_);
main_loop_ = MessageLoop::current();
// Also we want to be notified of main_loop_ destruction.
main_loop_->AddDestructionObserver(this);
}
void WebMediaPlayerDelegateImpl::Load(const GURL& url) {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
// Initialize the pipeline
pipeline_.Start(filter_factory_.get(), url.spec(),
NewCallback(this, &WebMediaPlayerDelegateImpl::DidInitializePipeline));
}
void WebMediaPlayerDelegateImpl::CancelLoad() {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
// TODO(hclam): Calls to render_view_ to stop resource load
}
void WebMediaPlayerDelegateImpl::Play() {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
// TODO(hclam): We should restore the previous playback rate rather than
// having it at 1.0.
pipeline_.SetPlaybackRate(1.0f);
}
void WebMediaPlayerDelegateImpl::Pause() {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
pipeline_.SetPlaybackRate(0.0f);
}
void WebMediaPlayerDelegateImpl::Stop() {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
// We can fire Stop() multiple times.
pipeline_.Stop();
}
void WebMediaPlayerDelegateImpl::Seek(float time) {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
pipeline_.Seek(base::TimeDelta::FromMilliseconds(static_cast<int64>(time)));
}
void WebMediaPlayerDelegateImpl::SetEndTime(float time) {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
// TODO(hclam): add method call when it has been implemented.
return;
}
void WebMediaPlayerDelegateImpl::SetPlaybackRate(float rate) {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
pipeline_.SetPlaybackRate(rate);
}
void WebMediaPlayerDelegateImpl::SetVolume(float volume) {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
pipeline_.SetVolume(volume);
}
void WebMediaPlayerDelegateImpl::SetVisible(bool visible) {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
// TODO(hclam): add appropriate method call when pipeline has it implemented.
return;
}
bool WebMediaPlayerDelegateImpl::IsTotalBytesKnown() {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
return pipeline_.GetTotalBytes() != 0;
}
bool WebMediaPlayerDelegateImpl::IsVideo() const {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
size_t width, height;
pipeline_.GetVideoSize(&width, &height);
return width != 0 && height != 0;
}
size_t WebMediaPlayerDelegateImpl::GetWidth() const {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
size_t width, height;
pipeline_.GetVideoSize(&width, &height);
return width;
}
size_t WebMediaPlayerDelegateImpl::GetHeight() const {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
size_t width, height;
pipeline_.GetVideoSize(&width, &height);
return height;
}
bool WebMediaPlayerDelegateImpl::IsPaused() const {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
return pipeline_.GetPlaybackRate() == 0.0f;
}
bool WebMediaPlayerDelegateImpl::IsSeeking() const {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
// TODO(hclam): Add this method call if pipeline has it in the interface.
return false;
}
float WebMediaPlayerDelegateImpl::GetDuration() const {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
return static_cast<float>(pipeline_.GetDuration().InSecondsF());
}
float WebMediaPlayerDelegateImpl::GetCurrentTime() const {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
return static_cast<float>(pipeline_.GetTime().InSecondsF());
}
float WebMediaPlayerDelegateImpl::GetPlayBackRate() const {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
return pipeline_.GetPlaybackRate();
}
float WebMediaPlayerDelegateImpl::GetVolume() const {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
return pipeline_.GetVolume();
}
int WebMediaPlayerDelegateImpl::GetDataRate() const {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
// TODO(hclam): Add this method call if pipeline has it in the interface.
return 0;
}
float WebMediaPlayerDelegateImpl::GetMaxTimeBuffered() const {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
return static_cast<float>(pipeline_.GetBufferedTime().InSecondsF());
}
float WebMediaPlayerDelegateImpl::GetMaxTimeSeekable() const {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
// TODO(hclam): add this method when pipeline has this method implemented.
return 0.0f;
}
int64 WebMediaPlayerDelegateImpl::GetBytesLoaded() const {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
return pipeline_.GetBufferedBytes();
}
int64 WebMediaPlayerDelegateImpl::GetTotalBytes() const {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
return pipeline_.GetTotalBytes();
}
void WebMediaPlayerDelegateImpl::SetSize(const gfx::Size& size) {
DCHECK(main_loop_ && MessageLoop::current() == main_loop_);
if (video_renderer_) {
// TODO(scherkus): Change API to use SetSize().
video_renderer_->SetRect(gfx::Rect(0, 0, size.width(), size.height()));
}
}
void WebMediaPlayerDelegateImpl::Paint(skia::PlatformCanvas *canvas,
const gfx::Rect& rect) {
if (video_renderer_) {
video_renderer_->Paint(canvas, rect);
}
}
void WebMediaPlayerDelegateImpl::WillDestroyCurrentMessageLoop() {
// Instruct the renderers and data source to release all Renderer related
// resources during destruction of render thread, because they won't have any
// chance to release these resources on render thread by posting tasks on it.
if (audio_renderer_) {
audio_renderer_->ReleaseRendererResources();
audio_renderer_ = NULL;
}
if (data_source_) {
data_source_->ReleaseRendererResources();
data_source_ = NULL;
}
// Stop the pipeline when the render thread is being destroyed so we won't be
// posting any more messages onto it. And we just let this object and
// associated WebMediaPlayer to leak.
pipeline_.Stop();
}
void WebMediaPlayerDelegateImpl::DidInitializePipeline(bool successful) {
if (successful) {
// Since we have initialized the pipeline, we should be able to play it.
// And we skip LOADED_METADATA state and starting with LOADED_FIRST_FRAME.
ready_state_ = webkit_glue::WebMediaPlayer::CAN_PLAY;
network_state_ = webkit_glue::WebMediaPlayer::LOADED_FIRST_FRAME;
} else {
// TODO(hclam): should use pipeline_.GetError() to determine the state
// properly and reports error using MediaError.
ready_state_ = webkit_glue::WebMediaPlayer::DATA_UNAVAILABLE;
network_state_ = webkit_glue::WebMediaPlayer::LOAD_FAILED;
}
PostTask(kNetworkStateTaskIndex,
&webkit_glue::WebMediaPlayer::NotifyNetworkStateChange);
PostTask(kReadyStateTaskIndex,
&webkit_glue::WebMediaPlayer::NotifyReadyStateChange);
}
void WebMediaPlayerDelegateImpl::SetAudioRenderer(
AudioRendererImpl* audio_renderer) {
DCHECK(!audio_renderer_);
audio_renderer_ = audio_renderer;
}
void WebMediaPlayerDelegateImpl::SetVideoRenderer(
VideoRendererImpl* video_renderer) {
DCHECK(!video_renderer_);
video_renderer_ = video_renderer;
}
void WebMediaPlayerDelegateImpl::SetDataSource(
DataSourceImpl* data_source) {
data_source_ = data_source;
}
void WebMediaPlayerDelegateImpl::DidTask(CancelableTask* task) {
AutoLock auto_lock(task_lock_);
for (size_t i = 0; i < tasks_.size(); ++i) {
if (tasks_[i] == task) {
tasks_[i] = NULL;
return;
}
}
NOTREACHED();
}
void WebMediaPlayerDelegateImpl::CancelAllTasks() {
AutoLock auto_lock(task_lock_);
// Loop through the list of tasks and cancel tasks that are still alive.
for (size_t i = 0; i < tasks_.size(); ++i) {
if (tasks_[i])
tasks_[i]->Cancel();
}
}
void WebMediaPlayerDelegateImpl::PostTask(int index,
WebMediaPlayerMethod method) {
DCHECK(main_loop_);
AutoLock auto_lock(task_lock_);
if(!tasks_[index]) {
CancelableTask* task = new NotifyWebMediaPlayerTask(this, method);
tasks_[index] = task;
main_loop_->PostTask(FROM_HERE, task);
}
}
void WebMediaPlayerDelegateImpl::PostRepaintTask() {
PostTask(kRepaintTaskIndex, &webkit_glue::WebMediaPlayer::Repaint);
}
|