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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
|
// 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.
#include "content/browser/media/android/browser_media_player_manager.h"
#include "base/android/scoped_java_ref.h"
#include "base/command_line.h"
#include "content/browser/android/content_view_core_impl.h"
#include "content/browser/media/android/browser_demuxer_android.h"
#include "content/browser/media/android/media_resource_getter_impl.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/web_contents/web_contents_view_android.h"
#include "content/common/media/media_player_messages_android.h"
#include "content/public/browser/android/content_view_core.h"
#include "content/public/browser/android/external_video_surface_container.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
#include "media/base/android/media_player_bridge.h"
#include "media/base/android/media_source_player.h"
#include "media/base/media_switches.h"
using media::MediaPlayerAndroid;
using media::MediaPlayerBridge;
using media::MediaPlayerManager;
using media::MediaSourcePlayer;
namespace content {
// Threshold on the number of media players per renderer before we start
// attempting to release inactive media players.
const int kMediaPlayerThreshold = 1;
static BrowserMediaPlayerManager::Factory g_factory = NULL;
// static
void BrowserMediaPlayerManager::RegisterFactory(Factory factory) {
g_factory = factory;
}
// static
BrowserMediaPlayerManager* BrowserMediaPlayerManager::Create(
RenderFrameHost* rfh) {
if (g_factory)
return g_factory(rfh);
return new BrowserMediaPlayerManager(rfh);
}
ContentViewCoreImpl* BrowserMediaPlayerManager::GetContentViewCore() const {
return ContentViewCoreImpl::FromWebContents(web_contents());
}
MediaPlayerAndroid* BrowserMediaPlayerManager::CreateMediaPlayer(
MediaPlayerHostMsg_Initialize_Type type,
int player_id,
const GURL& url,
const GURL& first_party_for_cookies,
int demuxer_client_id,
bool hide_url_log,
MediaPlayerManager* manager,
BrowserDemuxerAndroid* demuxer) {
switch (type) {
case MEDIA_PLAYER_TYPE_URL: {
const std::string user_agent = GetContentClient()->GetUserAgent();
MediaPlayerBridge* media_player_bridge = new MediaPlayerBridge(
player_id,
url,
first_party_for_cookies,
user_agent,
hide_url_log,
manager,
base::Bind(&BrowserMediaPlayerManager::OnMediaResourcesRequested,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&BrowserMediaPlayerManager::OnMediaResourcesReleased,
weak_ptr_factory_.GetWeakPtr()));
BrowserMediaPlayerManager* browser_media_player_manager =
static_cast<BrowserMediaPlayerManager*>(manager);
ContentViewCoreImpl* content_view_core_impl =
static_cast<ContentViewCoreImpl*>(ContentViewCore::FromWebContents(
browser_media_player_manager->web_contents_));
if (!content_view_core_impl) {
// May reach here due to prerendering. Don't extract the metadata
// since it is expensive.
// TODO(qinmin): extract the metadata once the user decided to load
// the page.
browser_media_player_manager->OnMediaMetadataChanged(
player_id, base::TimeDelta(), 0, 0, false);
} else if (!content_view_core_impl->ShouldBlockMediaRequest(url)) {
media_player_bridge->Initialize();
}
return media_player_bridge;
}
case MEDIA_PLAYER_TYPE_MEDIA_SOURCE: {
return new MediaSourcePlayer(
player_id,
manager,
base::Bind(&BrowserMediaPlayerManager::OnMediaResourcesRequested,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&BrowserMediaPlayerManager::OnMediaResourcesReleased,
weak_ptr_factory_.GetWeakPtr()),
demuxer->CreateDemuxer(demuxer_client_id));
}
}
NOTREACHED();
return NULL;
}
BrowserMediaPlayerManager::BrowserMediaPlayerManager(
RenderFrameHost* render_frame_host)
: render_frame_host_(render_frame_host),
fullscreen_player_id_(-1),
fullscreen_player_is_released_(false),
web_contents_(WebContents::FromRenderFrameHost(render_frame_host)),
weak_ptr_factory_(this) {
}
BrowserMediaPlayerManager::~BrowserMediaPlayerManager() {}
void BrowserMediaPlayerManager::FullscreenPlayerPlay() {
MediaPlayerAndroid* player = GetFullscreenPlayer();
if (player) {
if (fullscreen_player_is_released_) {
video_view_->OpenVideo();
fullscreen_player_is_released_ = false;
}
player->Start();
Send(new MediaPlayerMsg_DidMediaPlayerPlay(RoutingID(),
fullscreen_player_id_));
}
}
void BrowserMediaPlayerManager::FullscreenPlayerPause() {
MediaPlayerAndroid* player = GetFullscreenPlayer();
if (player) {
player->Pause(true);
Send(new MediaPlayerMsg_DidMediaPlayerPause(RoutingID(),
fullscreen_player_id_));
}
}
void BrowserMediaPlayerManager::FullscreenPlayerSeek(int msec) {
MediaPlayerAndroid* player = GetFullscreenPlayer();
if (player) {
// TODO(kbalazs): if |fullscreen_player_is_released_| is true
// at this point, player->GetCurrentTime() will be wrong until
// FullscreenPlayerPlay (http://crbug.com/322798).
OnSeekRequest(fullscreen_player_id_,
base::TimeDelta::FromMilliseconds(msec));
}
}
void BrowserMediaPlayerManager::ExitFullscreen(bool release_media_player) {
if (!CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableOverlayFullscreenVideoSubtitle)) {
if (WebContentsDelegate* delegate = web_contents_->GetDelegate())
delegate->ToggleFullscreenModeForTab(web_contents_, false);
if (RenderWidgetHostViewAndroid* view_android =
static_cast<RenderWidgetHostViewAndroid*>(
web_contents_->GetRenderWidgetHostView())) {
view_android->SetOverlayVideoMode(false);
}
}
Send(
new MediaPlayerMsg_DidExitFullscreen(RoutingID(), fullscreen_player_id_));
video_view_.reset();
MediaPlayerAndroid* player = GetFullscreenPlayer();
fullscreen_player_id_ = -1;
if (!player)
return;
if (release_media_player)
ReleaseFullscreenPlayer(player);
else
player->SetVideoSurface(gfx::ScopedJavaSurface());
}
void BrowserMediaPlayerManager::OnTimeUpdate(int player_id,
base::TimeDelta current_time) {
Send(
new MediaPlayerMsg_MediaTimeUpdate(RoutingID(), player_id, current_time));
}
void BrowserMediaPlayerManager::SetVideoSurface(
gfx::ScopedJavaSurface surface) {
MediaPlayerAndroid* player = GetFullscreenPlayer();
if (!player)
return;
bool empty_surface = surface.IsEmpty();
player->SetVideoSurface(surface.Pass());
if (empty_surface)
return;
Send(new MediaPlayerMsg_DidEnterFullscreen(RoutingID(), player->player_id()));
if (CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableOverlayFullscreenVideoSubtitle)) {
return;
}
if (RenderWidgetHostViewAndroid* view_android =
static_cast<RenderWidgetHostViewAndroid*>(
web_contents_->GetRenderWidgetHostView())) {
view_android->SetOverlayVideoMode(true);
}
if (WebContentsDelegate* delegate = web_contents_->GetDelegate())
delegate->ToggleFullscreenModeForTab(web_contents_, true);
}
void BrowserMediaPlayerManager::OnMediaMetadataChanged(
int player_id, base::TimeDelta duration, int width, int height,
bool success) {
Send(new MediaPlayerMsg_MediaMetadataChanged(
RoutingID(), player_id, duration, width, height, success));
if (fullscreen_player_id_ == player_id)
video_view_->UpdateMediaMetadata();
}
void BrowserMediaPlayerManager::OnPlaybackComplete(int player_id) {
Send(new MediaPlayerMsg_MediaPlaybackCompleted(RoutingID(), player_id));
if (fullscreen_player_id_ == player_id)
video_view_->OnPlaybackComplete();
}
void BrowserMediaPlayerManager::OnMediaInterrupted(int player_id) {
// Tell WebKit that the audio should be paused, then release all resources
Send(new MediaPlayerMsg_MediaPlayerReleased(RoutingID(), player_id));
OnReleaseResources(player_id);
}
void BrowserMediaPlayerManager::OnBufferingUpdate(
int player_id, int percentage) {
Send(new MediaPlayerMsg_MediaBufferingUpdate(
RoutingID(), player_id, percentage));
if (fullscreen_player_id_ == player_id)
video_view_->OnBufferingUpdate(percentage);
}
void BrowserMediaPlayerManager::OnSeekRequest(
int player_id,
const base::TimeDelta& time_to_seek) {
Send(new MediaPlayerMsg_SeekRequest(RoutingID(), player_id, time_to_seek));
}
void BrowserMediaPlayerManager::PauseVideo() {
Send(new MediaPlayerMsg_PauseVideo(RoutingID()));
}
void BrowserMediaPlayerManager::OnSeekComplete(
int player_id,
const base::TimeDelta& current_time) {
Send(new MediaPlayerMsg_SeekCompleted(RoutingID(), player_id, current_time));
}
void BrowserMediaPlayerManager::OnError(int player_id, int error) {
Send(new MediaPlayerMsg_MediaError(RoutingID(), player_id, error));
if (fullscreen_player_id_ == player_id)
video_view_->OnMediaPlayerError(error);
}
void BrowserMediaPlayerManager::OnVideoSizeChanged(
int player_id, int width, int height) {
Send(new MediaPlayerMsg_MediaVideoSizeChanged(RoutingID(), player_id,
width, height));
if (fullscreen_player_id_ == player_id)
video_view_->OnVideoSizeChanged(width, height);
}
media::MediaResourceGetter*
BrowserMediaPlayerManager::GetMediaResourceGetter() {
if (!media_resource_getter_.get()) {
RenderProcessHost* host = web_contents()->GetRenderProcessHost();
BrowserContext* context = host->GetBrowserContext();
StoragePartition* partition = host->GetStoragePartition();
fileapi::FileSystemContext* file_system_context =
partition ? partition->GetFileSystemContext() : NULL;
// Eventually this needs to be fixed to pass the correct frame rather
// than just using the main frame.
media_resource_getter_.reset(new MediaResourceGetterImpl(
context,
file_system_context,
host->GetID(),
web_contents()->GetMainFrame()->GetRoutingID()));
}
return media_resource_getter_.get();
}
MediaPlayerAndroid* BrowserMediaPlayerManager::GetFullscreenPlayer() {
return GetPlayer(fullscreen_player_id_);
}
MediaPlayerAndroid* BrowserMediaPlayerManager::GetPlayer(int player_id) {
for (ScopedVector<MediaPlayerAndroid>::iterator it = players_.begin();
it != players_.end(); ++it) {
if ((*it)->player_id() == player_id)
return *it;
}
return NULL;
}
void BrowserMediaPlayerManager::DestroyAllMediaPlayers() {
players_.clear();
if (fullscreen_player_id_ != -1) {
video_view_.reset();
fullscreen_player_id_ = -1;
}
}
void BrowserMediaPlayerManager::RequestFullScreen(int player_id) {
if (fullscreen_player_id_ == player_id)
return;
if (fullscreen_player_id_ != -1) {
// TODO(qinmin): Determine the correct error code we should report to WMPA.
OnError(player_id, MediaPlayerAndroid::MEDIA_ERROR_DECODE);
return;
}
}
#if defined(VIDEO_HOLE)
void BrowserMediaPlayerManager::AttachExternalVideoSurface(int player_id,
jobject surface) {
MediaPlayerAndroid* player = GetPlayer(player_id);
if (player) {
player->SetVideoSurface(
gfx::ScopedJavaSurface::AcquireExternalSurface(surface));
}
}
void BrowserMediaPlayerManager::DetachExternalVideoSurface(int player_id) {
MediaPlayerAndroid* player = GetPlayer(player_id);
if (player)
player->SetVideoSurface(gfx::ScopedJavaSurface());
}
void BrowserMediaPlayerManager::OnFrameInfoUpdated() {
if (external_video_surface_container_)
external_video_surface_container_->OnFrameInfoUpdated();
}
void BrowserMediaPlayerManager::OnNotifyExternalSurface(
int player_id, bool is_request, const gfx::RectF& rect) {
if (!web_contents_)
return;
if (is_request) {
OnRequestExternalSurface(player_id, rect);
}
if (external_video_surface_container_) {
external_video_surface_container_->OnExternalVideoSurfacePositionChanged(
player_id, rect);
}
}
void BrowserMediaPlayerManager::OnRequestExternalSurface(
int player_id, const gfx::RectF& rect) {
if (!external_video_surface_container_) {
ContentBrowserClient* client = GetContentClient()->browser();
external_video_surface_container_.reset(
client->OverrideCreateExternalVideoSurfaceContainer(web_contents_));
}
// It's safe to use base::Unretained(this), because the callbacks will not
// be called after running ReleaseExternalVideoSurface().
if (external_video_surface_container_) {
external_video_surface_container_->RequestExternalVideoSurface(
player_id,
base::Bind(&BrowserMediaPlayerManager::AttachExternalVideoSurface,
base::Unretained(this)),
base::Bind(&BrowserMediaPlayerManager::DetachExternalVideoSurface,
base::Unretained(this)));
}
}
#endif // defined(VIDEO_HOLE)
void BrowserMediaPlayerManager::OnEnterFullscreen(int player_id) {
DCHECK_EQ(fullscreen_player_id_, -1);
#if defined(VIDEO_HOLE)
if (external_video_surface_container_)
external_video_surface_container_->ReleaseExternalVideoSurface(player_id);
#endif // defined(VIDEO_HOLE)
if (video_view_.get()) {
fullscreen_player_id_ = player_id;
video_view_->OpenVideo();
return;
} else if (!ContentVideoView::GetInstance()) {
// In Android WebView, two ContentViewCores could both try to enter
// fullscreen video, we just ignore the second one.
video_view_.reset(new ContentVideoView(this));
base::android::ScopedJavaLocalRef<jobject> j_content_video_view =
video_view_->GetJavaObject(base::android::AttachCurrentThread());
if (!j_content_video_view.is_null()) {
fullscreen_player_id_ = player_id;
return;
}
}
// Force the second video to exit fullscreen.
// TODO(qinmin): There is no need to send DidEnterFullscreen message.
// However, if we don't send the message, page layers will not be
// correctly restored. http:crbug.com/367346.
Send(new MediaPlayerMsg_DidEnterFullscreen(RoutingID(), player_id));
Send(new MediaPlayerMsg_DidExitFullscreen(RoutingID(), player_id));
video_view_.reset();
}
void BrowserMediaPlayerManager::OnExitFullscreen(int player_id) {
if (fullscreen_player_id_ == player_id) {
MediaPlayerAndroid* player = GetPlayer(player_id);
if (player)
player->SetVideoSurface(gfx::ScopedJavaSurface());
video_view_->OnExitFullscreen();
}
}
void BrowserMediaPlayerManager::OnInitialize(
MediaPlayerHostMsg_Initialize_Type type,
int player_id,
const GURL& url,
const GURL& first_party_for_cookies,
int demuxer_client_id) {
DCHECK(type != MEDIA_PLAYER_TYPE_MEDIA_SOURCE || demuxer_client_id > 0)
<< "Media source players must have positive demuxer client IDs: "
<< demuxer_client_id;
RemovePlayer(player_id);
RenderProcessHostImpl* host = static_cast<RenderProcessHostImpl*>(
web_contents()->GetRenderProcessHost());
MediaPlayerAndroid* player = CreateMediaPlayer(
type, player_id, url, first_party_for_cookies, demuxer_client_id,
host->GetBrowserContext()->IsOffTheRecord(), this,
host->browser_demuxer_android());
if (!player)
return;
AddPlayer(player);
}
void BrowserMediaPlayerManager::OnStart(int player_id) {
MediaPlayerAndroid* player = GetPlayer(player_id);
if (!player)
return;
player->Start();
if (fullscreen_player_id_ == player_id && fullscreen_player_is_released_) {
video_view_->OpenVideo();
fullscreen_player_is_released_ = false;
}
}
void BrowserMediaPlayerManager::OnSeek(
int player_id,
const base::TimeDelta& time) {
MediaPlayerAndroid* player = GetPlayer(player_id);
if (player)
player->SeekTo(time);
}
void BrowserMediaPlayerManager::OnPause(
int player_id,
bool is_media_related_action) {
MediaPlayerAndroid* player = GetPlayer(player_id);
if (player)
player->Pause(is_media_related_action);
}
void BrowserMediaPlayerManager::OnSetVolume(int player_id, double volume) {
MediaPlayerAndroid* player = GetPlayer(player_id);
if (player)
player->SetVolume(volume);
}
void BrowserMediaPlayerManager::OnSetPoster(int player_id, const GURL& url) {
// To be overridden by subclasses.
}
void BrowserMediaPlayerManager::OnReleaseResources(int player_id) {
MediaPlayerAndroid* player = GetPlayer(player_id);
if (player)
player->Release();
if (player_id == fullscreen_player_id_)
fullscreen_player_is_released_ = true;
}
void BrowserMediaPlayerManager::OnDestroyPlayer(int player_id) {
RemovePlayer(player_id);
if (fullscreen_player_id_ == player_id)
fullscreen_player_id_ = -1;
}
void BrowserMediaPlayerManager::AddPlayer(MediaPlayerAndroid* player) {
DCHECK(!GetPlayer(player->player_id()));
players_.push_back(player);
}
void BrowserMediaPlayerManager::RemovePlayer(int player_id) {
for (ScopedVector<MediaPlayerAndroid>::iterator it = players_.begin();
it != players_.end(); ++it) {
MediaPlayerAndroid* player = *it;
if (player->player_id() == player_id) {
players_.erase(it);
break;
}
}
}
scoped_ptr<media::MediaPlayerAndroid> BrowserMediaPlayerManager::SwapPlayer(
int player_id, media::MediaPlayerAndroid* player) {
media::MediaPlayerAndroid* previous_player = NULL;
for (ScopedVector<MediaPlayerAndroid>::iterator it = players_.begin();
it != players_.end(); ++it) {
if ((*it)->player_id() == player_id) {
previous_player = *it;
players_.weak_erase(it);
players_.push_back(player);
break;
}
}
return scoped_ptr<media::MediaPlayerAndroid>(previous_player);
}
int BrowserMediaPlayerManager::RoutingID() {
return render_frame_host_->GetRoutingID();
}
bool BrowserMediaPlayerManager::Send(IPC::Message* msg) {
return render_frame_host_->Send(msg);
}
void BrowserMediaPlayerManager::ReleaseFullscreenPlayer(
MediaPlayerAndroid* player) {
player->Release();
}
void BrowserMediaPlayerManager::OnMediaResourcesRequested(int player_id) {
int num_active_player = 0;
ScopedVector<MediaPlayerAndroid>::iterator it;
for (it = players_.begin(); it != players_.end(); ++it) {
if (!(*it)->IsPlayerReady())
continue;
// The player is already active, ignore it.
if ((*it)->player_id() == player_id)
return;
else
num_active_player++;
}
// Number of active players are less than the threshold, do nothing.
if (num_active_player < kMediaPlayerThreshold)
return;
for (it = players_.begin(); it != players_.end(); ++it) {
if ((*it)->IsPlayerReady() && !(*it)->IsPlaying() &&
fullscreen_player_id_ != (*it)->player_id()) {
(*it)->Release();
Send(new MediaPlayerMsg_MediaPlayerReleased(RoutingID(),
(*it)->player_id()));
}
}
}
void BrowserMediaPlayerManager::OnMediaResourcesReleased(int player_id) {
#if defined(VIDEO_HOLE)
MediaPlayerAndroid* player = GetPlayer(player_id);
if (player && player->IsSurfaceInUse())
return;
if (external_video_surface_container_)
external_video_surface_container_->ReleaseExternalVideoSurface(player_id);
#endif // defined(VIDEO_HOLE)
}
} // namespace content
|