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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
// Copyright (c) 2012 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 "webkit/media/android/webmediaplayer_android.h"
#include "base/file_path.h"
#include "base/logging.h"
#include "media/base/android/media_player_bridge.h"
#include "media/base/video_frame.h"
#include "net/base/mime_util.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaPlayerClient.h"
#include "webkit/media/android/stream_texture_factory_android.h"
#include "webkit/media/android/webmediaplayer_manager_android.h"
#include "webkit/media/webmediaplayer_util.h"
#include "webkit/media/webvideoframe_impl.h"
static const uint32 kGLTextureExternalOES = 0x8D65;
using WebKit::WebMediaPlayer;
using WebKit::WebSize;
using WebKit::WebTimeRanges;
using WebKit::WebURL;
using WebKit::WebVideoFrame;
using media::MediaPlayerBridge;
using media::VideoFrame;
namespace webkit_media {
WebMediaPlayerAndroid::WebMediaPlayerAndroid(
WebKit::WebMediaPlayerClient* client,
WebMediaPlayerManagerAndroid* manager,
StreamTextureFactory* factory)
: client_(client),
buffered_(1u),
main_loop_(MessageLoop::current()),
pending_seek_(0),
seeking_(false),
did_loading_progress_(false),
manager_(manager),
network_state_(WebMediaPlayer::NetworkStateEmpty),
ready_state_(WebMediaPlayer::ReadyStateHaveNothing),
is_playing_(false),
needs_establish_peer_(true),
has_size_info_(false),
stream_texture_factory_(factory) {
main_loop_->AddDestructionObserver(this);
if (manager_)
player_id_ = manager_->RegisterMediaPlayer(this);
if (stream_texture_factory_.get()) {
stream_texture_proxy_.reset(stream_texture_factory_->CreateProxy());
stream_id_ = stream_texture_factory_->CreateStreamTexture(&texture_id_);
ReallocateVideoFrame();
}
}
WebMediaPlayerAndroid::~WebMediaPlayerAndroid() {
if (stream_id_)
stream_texture_factory_->DestroyStreamTexture(texture_id_);
if (manager_)
manager_->UnregisterMediaPlayer(player_id_);
if (main_loop_)
main_loop_->RemoveDestructionObserver(this);
}
void WebMediaPlayerAndroid::load(const WebURL& url, CORSMode cors_mode) {
if (cors_mode != CORSModeUnspecified)
NOTIMPLEMENTED() << "No CORS support";
url_ = url;
InitializeMediaPlayer(url_);
}
void WebMediaPlayerAndroid::cancelLoad() {
NOTIMPLEMENTED();
}
void WebMediaPlayerAndroid::play() {
if (hasVideo() && needs_establish_peer_)
EstablishSurfaceTexturePeer();
PlayInternal();
is_playing_ = true;
}
void WebMediaPlayerAndroid::pause() {
PauseInternal();
is_playing_ = false;
}
void WebMediaPlayerAndroid::seek(float seconds) {
pending_seek_ = seconds;
seeking_ = true;
SeekInternal(ConvertSecondsToTimestamp(seconds));
}
bool WebMediaPlayerAndroid::supportsFullscreen() const {
return true;
}
bool WebMediaPlayerAndroid::supportsSave() const {
return false;
}
void WebMediaPlayerAndroid::setEndTime(float seconds) {
// Deprecated.
// TODO(qinmin): Remove this from WebKit::WebMediaPlayer as it is never used.
}
void WebMediaPlayerAndroid::setRate(float rate) {
NOTIMPLEMENTED();
}
void WebMediaPlayerAndroid::setVolume(float volume) {
NOTIMPLEMENTED();
}
void WebMediaPlayerAndroid::setVisible(bool visible) {
// Deprecated.
// TODO(qinmin): Remove this from WebKit::WebMediaPlayer as it is never used.
}
bool WebMediaPlayerAndroid::totalBytesKnown() {
NOTIMPLEMENTED();
return false;
}
bool WebMediaPlayerAndroid::hasVideo() const {
// If we have obtained video size information before, use it.
if (has_size_info_)
return !natural_size_.isEmpty();
// TODO(qinmin): need a better method to determine whether the current media
// content contains video. Android does not provide any function to do
// this.
// We don't know whether the current media content has video unless
// the player is prepared. If the player is not prepared, we fall back
// to the mime-type. There may be no mime-type on a redirect URL.
// In that case, we conservatively assume it contains video so that
// enterfullscreen call will not fail.
if (!url_.has_path())
return false;
std::string mime;
if(!net::GetMimeTypeFromFile(base::FilePath(url_.path()), &mime))
return true;
return mime.find("audio/") == std::string::npos;
}
bool WebMediaPlayerAndroid::hasAudio() const {
// TODO(hclam): Query status of audio and return the actual value.
return true;
}
bool WebMediaPlayerAndroid::paused() const {
return !is_playing_;
}
bool WebMediaPlayerAndroid::seeking() const {
return seeking_;
}
float WebMediaPlayerAndroid::duration() const {
return static_cast<float>(duration_.InSecondsF());
}
float WebMediaPlayerAndroid::currentTime() const {
// If the player is pending for a seek, return the seek time.
if (seeking())
return pending_seek_;
return GetCurrentTimeInternal();
}
int WebMediaPlayerAndroid::dataRate() const {
// Deprecated.
// TODO(qinmin): Remove this from WebKit::WebMediaPlayer as it is never used.
return 0;
}
WebSize WebMediaPlayerAndroid::naturalSize() const {
return natural_size_;
}
WebMediaPlayer::NetworkState WebMediaPlayerAndroid::networkState() const {
return network_state_;
}
WebMediaPlayer::ReadyState WebMediaPlayerAndroid::readyState() const {
return ready_state_;
}
const WebTimeRanges& WebMediaPlayerAndroid::buffered() {
return buffered_;
}
float WebMediaPlayerAndroid::maxTimeSeekable() const {
// TODO(hclam): If this stream is not seekable this should return 0.
return duration();
}
bool WebMediaPlayerAndroid::didLoadingProgress() const {
bool ret = did_loading_progress_;
did_loading_progress_ = false;
return ret;
}
unsigned long long WebMediaPlayerAndroid::totalBytes() const {
// Deprecated.
// TODO(qinmin): Remove this from WebKit::WebMediaPlayer as it is never used.
return 0;
}
void WebMediaPlayerAndroid::setSize(const WebKit::WebSize& size) {
}
void WebMediaPlayerAndroid::paint(WebKit::WebCanvas* canvas,
const WebKit::WebRect& rect,
uint8_t alpha) {
NOTIMPLEMENTED();
}
bool WebMediaPlayerAndroid::hasSingleSecurityOrigin() const {
return false;
}
bool WebMediaPlayerAndroid::didPassCORSAccessCheck() const {
return false;
}
WebMediaPlayer::MovieLoadType WebMediaPlayerAndroid::movieLoadType() const {
// Deprecated.
// TODO(qinmin): Remove this from WebKit::WebMediaPlayer as it is never used.
return WebMediaPlayer::MovieLoadTypeUnknown;
}
float WebMediaPlayerAndroid::mediaTimeForTimeValue(float timeValue) const {
return ConvertSecondsToTimestamp(timeValue).InSecondsF();
}
unsigned WebMediaPlayerAndroid::decodedFrameCount() const {
NOTIMPLEMENTED();
return 0;
}
unsigned WebMediaPlayerAndroid::droppedFrameCount() const {
NOTIMPLEMENTED();
return 0;
}
unsigned WebMediaPlayerAndroid::audioDecodedByteCount() const {
NOTIMPLEMENTED();
return 0;
}
unsigned WebMediaPlayerAndroid::videoDecodedByteCount() const {
NOTIMPLEMENTED();
return 0;
}
void WebMediaPlayerAndroid::OnMediaPrepared(base::TimeDelta duration) {
if (url_.SchemeIs("file"))
UpdateNetworkState(WebMediaPlayer::NetworkStateLoaded);
if (ready_state_ != WebMediaPlayer::ReadyStateHaveEnoughData) {
UpdateReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
UpdateReadyState(WebMediaPlayer::ReadyStateHaveEnoughData);
} else {
// If the status is already set to ReadyStateHaveEnoughData, set it again
// to make sure that Videolayerchromium will get created.
UpdateReadyState(WebMediaPlayer::ReadyStateHaveEnoughData);
}
// In we have skipped loading, we have to update webkit about the new
// duration.
if (duration_ != duration) {
duration_ = duration;
client_->durationChanged();
}
}
void WebMediaPlayerAndroid::OnPlaybackComplete() {
// When playback is about to finish, android media player often stops
// at a time which is smaller than the duration. This makes webkit never
// know that the playback has finished. To solve this, we set the
// current time to media duration when OnPlaybackComplete() get called.
OnTimeUpdate(duration_);
client_->timeChanged();
}
void WebMediaPlayerAndroid::OnBufferingUpdate(int percentage) {
buffered_[0].end = duration() * percentage / 100;
did_loading_progress_ = true;
}
void WebMediaPlayerAndroid::OnSeekComplete(base::TimeDelta current_time) {
seeking_ = false;
OnTimeUpdate(current_time);
UpdateReadyState(WebMediaPlayer::ReadyStateHaveEnoughData);
client_->timeChanged();
}
void WebMediaPlayerAndroid::OnMediaError(int error_type) {
switch (error_type) {
case MediaPlayerBridge::MEDIA_ERROR_FORMAT:
UpdateNetworkState(WebMediaPlayer::NetworkStateFormatError);
break;
case MediaPlayerBridge::MEDIA_ERROR_DECODE:
UpdateNetworkState(WebMediaPlayer::NetworkStateDecodeError);
break;
case MediaPlayerBridge::MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
UpdateNetworkState(WebMediaPlayer::NetworkStateFormatError);
break;
case MediaPlayerBridge::MEDIA_ERROR_INVALID_CODE:
break;
}
client_->repaint();
}
void WebMediaPlayerAndroid::OnVideoSizeChanged(int width, int height) {
has_size_info_ = true;
if (natural_size_.width == width && natural_size_.height == height)
return;
natural_size_.width = width;
natural_size_.height = height;
ReallocateVideoFrame();
}
void WebMediaPlayerAndroid::UpdateNetworkState(
WebMediaPlayer::NetworkState state) {
network_state_ = state;
client_->networkStateChanged();
}
void WebMediaPlayerAndroid::UpdateReadyState(
WebMediaPlayer::ReadyState state) {
ready_state_ = state;
client_->readyStateChanged();
}
void WebMediaPlayerAndroid::OnPlayerReleased() {
needs_establish_peer_ = true;
}
void WebMediaPlayerAndroid::ReleaseMediaResources() {
// Pause the media player first.
pause();
client_->playbackStateChanged();
ReleaseResourcesInternal();
OnPlayerReleased();
}
void WebMediaPlayerAndroid::WillDestroyCurrentMessageLoop() {
if (manager_)
manager_->UnregisterMediaPlayer(player_id_);
Detach();
main_loop_ = NULL;
}
void WebMediaPlayerAndroid::Detach() {
Destroy();
if (stream_id_) {
stream_texture_factory_->DestroyStreamTexture(texture_id_);
stream_id_ = 0;
}
video_frame_.reset();
manager_ = NULL;
}
void WebMediaPlayerAndroid::ReallocateVideoFrame() {
if (texture_id_) {
video_frame_.reset(new WebVideoFrameImpl(VideoFrame::WrapNativeTexture(
texture_id_, kGLTextureExternalOES, natural_size_,
gfx::Rect(natural_size_), natural_size_, base::TimeDelta(),
VideoFrame::ReadPixelsCB(),
base::Closure())));
}
}
WebVideoFrame* WebMediaPlayerAndroid::getCurrentFrame() {
if (stream_texture_proxy_.get() && !stream_texture_proxy_->IsInitialized()
&& stream_id_) {
stream_texture_proxy_->Initialize(
stream_id_, video_frame_->width(), video_frame_->height());
}
return video_frame_.get();
}
void WebMediaPlayerAndroid::putCurrentFrame(
WebVideoFrame* web_video_frame) {
}
void WebMediaPlayerAndroid::setStreamTextureClient(
WebKit::WebStreamTextureClient* client) {
if (stream_texture_proxy_.get())
stream_texture_proxy_->SetClient(client);
}
void WebMediaPlayerAndroid::EstablishSurfaceTexturePeer() {
if (stream_texture_factory_.get() && stream_id_)
stream_texture_factory_->EstablishPeer(stream_id_, player_id_);
needs_establish_peer_ = false;
}
void WebMediaPlayerAndroid::SetNeedsEstablishPeer(bool needs_establish_peer) {
needs_establish_peer_ = needs_establish_peer;
}
void WebMediaPlayerAndroid::UpdatePlayingState(bool is_playing) {
is_playing_ = is_playing;
}
} // namespace webkit_media
|