Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(300)

Side by Side Diff: content/browser/media/android/browser_media_player_manager.cc

Issue 1110833004: Move audio focus control from media/ to content/ and make it per WebContents. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review comments Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/media/android/browser_media_player_manager.h" 5 #include "content/browser/media/android/browser_media_player_manager.h"
6 6
7 #include "base/android/scoped_java_ref.h" 7 #include "base/android/scoped_java_ref.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "content/browser/android/content_view_core_impl.h" 9 #include "content/browser/android/content_view_core_impl.h"
10 #include "content/browser/android/media_players_observer.h" 10 #include "content/browser/android/media_players_observer.h"
11 #include "content/browser/media/android/browser_demuxer_android.h" 11 #include "content/browser/media/android/browser_demuxer_android.h"
12 #include "content/browser/media/android/media_resource_getter_impl.h" 12 #include "content/browser/media/android/media_resource_getter_impl.h"
13 #include "content/browser/media/android/media_session.h"
13 #include "content/browser/renderer_host/render_view_host_impl.h" 14 #include "content/browser/renderer_host/render_view_host_impl.h"
14 #include "content/browser/web_contents/web_contents_view_android.h" 15 #include "content/browser/web_contents/web_contents_view_android.h"
15 #include "content/common/media/media_player_messages_android.h" 16 #include "content/common/media/media_player_messages_android.h"
16 #include "content/public/browser/android/content_view_core.h" 17 #include "content/public/browser/android/content_view_core.h"
17 #include "content/public/browser/android/external_video_surface_container.h" 18 #include "content/public/browser/android/external_video_surface_container.h"
18 #include "content/public/browser/browser_context.h" 19 #include "content/public/browser/browser_context.h"
19 #include "content/public/browser/content_browser_client.h" 20 #include "content/public/browser/content_browser_client.h"
20 #include "content/public/browser/render_frame_host.h" 21 #include "content/public/browser/render_frame_host.h"
21 #include "content/public/browser/render_process_host.h" 22 #include "content/public/browser/render_process_host.h"
22 #include "content/public/browser/render_view_host.h" 23 #include "content/public/browser/render_view_host.h"
(...skipping 14 matching lines...) Expand all
37 using media::MediaPlayerManager; 38 using media::MediaPlayerManager;
38 using media::MediaSourcePlayer; 39 using media::MediaSourcePlayer;
39 40
40 namespace content { 41 namespace content {
41 42
42 // Threshold on the number of media players per renderer before we start 43 // Threshold on the number of media players per renderer before we start
43 // attempting to release inactive media players. 44 // attempting to release inactive media players.
44 const int kMediaPlayerThreshold = 1; 45 const int kMediaPlayerThreshold = 1;
45 const int kInvalidMediaPlayerId = -1; 46 const int kInvalidMediaPlayerId = -1;
46 47
48 // Minimal duration of a media player in order to be considered as Content type.
49 const int kMinimumDurationForContentInSeconds = 5;
50
47 static BrowserMediaPlayerManager::Factory g_factory = NULL; 51 static BrowserMediaPlayerManager::Factory g_factory = NULL;
48 static media::MediaUrlInterceptor* media_url_interceptor_ = NULL; 52 static media::MediaUrlInterceptor* media_url_interceptor_ = NULL;
49 53
50 // static 54 // static
51 void BrowserMediaPlayerManager::RegisterFactory(Factory factory) { 55 void BrowserMediaPlayerManager::RegisterFactory(Factory factory) {
52 // TODO(aberent) nullptr test is a temporary fix to simplify upstreaming Cast. 56 // TODO(aberent) nullptr test is a temporary fix to simplify upstreaming Cast.
53 // Until Cast is fully upstreamed we want the downstream factory to take 57 // Until Cast is fully upstreamed we want the downstream factory to take
54 // priority over the upstream factory. The downstream call happens first, 58 // priority over the upstream factory. The downstream call happens first,
55 // so this will ensure that it does. 59 // so this will ensure that it does.
56 if (g_factory == nullptr) 60 if (g_factory == nullptr)
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 157
154 BrowserMediaPlayerManager::~BrowserMediaPlayerManager() { 158 BrowserMediaPlayerManager::~BrowserMediaPlayerManager() {
155 // During the tear down process, OnDestroyPlayer() may or may not be called 159 // During the tear down process, OnDestroyPlayer() may or may not be called
156 // (e.g. the WebContents may be destroyed before the render process). So 160 // (e.g. the WebContents may be destroyed before the render process). So
157 // we cannot DCHECK(players_.empty()) here. Instead, all media players in 161 // we cannot DCHECK(players_.empty()) here. Instead, all media players in
158 // |players_| will be destroyed here because |player_| is a ScopedVector. 162 // |players_| will be destroyed here because |player_| is a ScopedVector.
159 163
160 for (MediaPlayerAndroid* player : players_) 164 for (MediaPlayerAndroid* player : players_)
161 player->DeleteOnCorrectThread(); 165 player->DeleteOnCorrectThread();
162 166
167 MediaSession::Get(web_contents())->RemovePlayers(this);
163 players_.weak_clear(); 168 players_.weak_clear();
164 } 169 }
165 170
166 void BrowserMediaPlayerManager::ExitFullscreen(bool release_media_player) { 171 void BrowserMediaPlayerManager::ExitFullscreen(bool release_media_player) {
167 if (WebContentsDelegate* delegate = web_contents_->GetDelegate()) 172 if (WebContentsDelegate* delegate = web_contents_->GetDelegate())
168 delegate->ExitFullscreenModeForTab(web_contents_); 173 delegate->ExitFullscreenModeForTab(web_contents_);
169 if (RenderWidgetHostViewAndroid* view_android = 174 if (RenderWidgetHostViewAndroid* view_android =
170 static_cast<RenderWidgetHostViewAndroid*>( 175 static_cast<RenderWidgetHostViewAndroid*>(
171 web_contents_->GetRenderWidgetHostView())) { 176 web_contents_->GetRenderWidgetHostView())) {
172 view_android->SetOverlayVideoMode(false); 177 view_android->SetOverlayVideoMode(false);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 int player_id, base::TimeDelta duration, int width, int height, 226 int player_id, base::TimeDelta duration, int width, int height,
222 bool success) { 227 bool success) {
223 Send(new MediaPlayerMsg_MediaMetadataChanged( 228 Send(new MediaPlayerMsg_MediaMetadataChanged(
224 RoutingID(), player_id, duration, width, height, success)); 229 RoutingID(), player_id, duration, width, height, success));
225 if (fullscreen_player_id_ == player_id) 230 if (fullscreen_player_id_ == player_id)
226 video_view_->UpdateMediaMetadata(); 231 video_view_->UpdateMediaMetadata();
227 } 232 }
228 233
229 void BrowserMediaPlayerManager::OnPlaybackComplete(int player_id) { 234 void BrowserMediaPlayerManager::OnPlaybackComplete(int player_id) {
230 Send(new MediaPlayerMsg_MediaPlaybackCompleted(RoutingID(), player_id)); 235 Send(new MediaPlayerMsg_MediaPlaybackCompleted(RoutingID(), player_id));
236 MediaSession::Get(web_contents())->RemovePlayer(this, player_id);
237
231 if (fullscreen_player_id_ == player_id) 238 if (fullscreen_player_id_ == player_id)
232 video_view_->OnPlaybackComplete(); 239 video_view_->OnPlaybackComplete();
233 } 240 }
234 241
235 void BrowserMediaPlayerManager::OnMediaInterrupted(int player_id) { 242 void BrowserMediaPlayerManager::OnMediaInterrupted(int player_id) {
236 // Tell WebKit that the audio should be paused, then release all resources 243 // Tell WebKit that the audio should be paused, then release all resources
237 Send(new MediaPlayerMsg_MediaPlayerReleased(RoutingID(), player_id)); 244 Send(new MediaPlayerMsg_MediaPlayerReleased(RoutingID(), player_id));
238 OnReleaseResources(player_id); 245 OnReleaseResources(player_id);
239 } 246 }
240 247
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 341
335 if (fullscreen_player_id_ != kInvalidMediaPlayerId) { 342 if (fullscreen_player_id_ != kInvalidMediaPlayerId) {
336 // TODO(qinmin): Determine the correct error code we should report to WMPA. 343 // TODO(qinmin): Determine the correct error code we should report to WMPA.
337 OnError(player_id, MediaPlayerAndroid::MEDIA_ERROR_DECODE); 344 OnError(player_id, MediaPlayerAndroid::MEDIA_ERROR_DECODE);
338 return; 345 return;
339 } 346 }
340 347
341 Send(new MediaPlayerMsg_RequestFullscreen(RoutingID(), player_id)); 348 Send(new MediaPlayerMsg_RequestFullscreen(RoutingID(), player_id));
342 } 349 }
343 350
351 bool BrowserMediaPlayerManager::RequestPlay(int player_id) {
352 MediaPlayerAndroid* player = GetPlayer(player_id);
353 DCHECK(player);
354
355 MediaSession::Type media_session_type =
356 player->GetDuration().InSeconds() > kMinimumDurationForContentInSeconds
357 ? MediaSession::Type::Content : MediaSession::Type::Transient;
358
359 bool succeeded = MediaSession::Get(web_contents())->AddPlayer(
360 this, player_id, media_session_type);
361 if (!succeeded)
362 OnSuspend(player_id);
qinmin 2015/05/20 21:35:24 MediaPlayerAndroid::RequestPlay() could possibly c
mlamouri (slow - plz ping) 2015/05/22 15:58:52 It's something I actually fixed while writing unit
363 return succeeded;
364 }
365
344 #if defined(VIDEO_HOLE) 366 #if defined(VIDEO_HOLE)
345 void BrowserMediaPlayerManager::AttachExternalVideoSurface(int player_id, 367 void BrowserMediaPlayerManager::AttachExternalVideoSurface(int player_id,
346 jobject surface) { 368 jobject surface) {
347 MediaPlayerAndroid* player = GetPlayer(player_id); 369 MediaPlayerAndroid* player = GetPlayer(player_id);
348 if (player) { 370 if (player) {
349 player->SetVideoSurface( 371 player->SetVideoSurface(
350 gfx::ScopedJavaSurface::AcquireExternalSurface(surface)); 372 gfx::ScopedJavaSurface::AcquireExternalSurface(surface));
351 } 373 }
352 } 374 }
353 375
354 void BrowserMediaPlayerManager::DetachExternalVideoSurface(int player_id) { 376 void BrowserMediaPlayerManager::DetachExternalVideoSurface(int player_id) {
355 MediaPlayerAndroid* player = GetPlayer(player_id); 377 MediaPlayerAndroid* player = GetPlayer(player_id);
356 if (player) 378 if (player)
357 player->SetVideoSurface(gfx::ScopedJavaSurface()); 379 player->SetVideoSurface(gfx::ScopedJavaSurface());
358 } 380 }
359 381
360 void BrowserMediaPlayerManager::OnFrameInfoUpdated() { 382 void BrowserMediaPlayerManager::OnFrameInfoUpdated() {
361 if (fullscreen_player_id_ != kInvalidMediaPlayerId) 383 if (fullscreen_player_id_ != kInvalidMediaPlayerId)
362 return; 384 return;
363 385
364 if (external_video_surface_container_) 386 if (external_video_surface_container_)
365 external_video_surface_container_->OnFrameInfoUpdated(); 387 external_video_surface_container_->OnFrameInfoUpdated();
366 } 388 }
367 389
390 void BrowserMediaPlayerManager::OnSuspend(int player_id) {
391 MediaPlayerAndroid* player = GetPlayer(player_id);
392 DCHECK(player);
393
394 player->Pause(true);
395 Send(new MediaPlayerMsg_DidMediaPlayerPause(RoutingID(), player_id));
396 }
397
398 void BrowserMediaPlayerManager::OnResume(int player_id) {
399 MediaPlayerAndroid* player = GetPlayer(player_id);
400 DCHECK(player);
401
402 player->Start();
403 Send(new MediaPlayerMsg_DidMediaPlayerPlay(RoutingID(), player_id));
404 }
405
368 void BrowserMediaPlayerManager::OnNotifyExternalSurface( 406 void BrowserMediaPlayerManager::OnNotifyExternalSurface(
369 int player_id, bool is_request, const gfx::RectF& rect) { 407 int player_id, bool is_request, const gfx::RectF& rect) {
370 if (!web_contents_) 408 if (!web_contents_)
371 return; 409 return;
372 410
373 if (is_request) { 411 if (is_request) {
374 OnRequestExternalSurface(player_id, rect); 412 OnRequestExternalSurface(player_id, rect);
375 } 413 }
376 if (external_video_surface_container_) { 414 if (external_video_surface_container_) {
377 external_video_surface_container_->OnExternalVideoSurfacePositionChanged( 415 external_video_surface_container_->OnExternalVideoSurfacePositionChanged(
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 if (player) 534 if (player)
497 player->SeekTo(time); 535 player->SeekTo(time);
498 } 536 }
499 537
500 void BrowserMediaPlayerManager::OnPause( 538 void BrowserMediaPlayerManager::OnPause(
501 int player_id, 539 int player_id,
502 bool is_media_related_action) { 540 bool is_media_related_action) {
503 MediaPlayerAndroid* player = GetPlayer(player_id); 541 MediaPlayerAndroid* player = GetPlayer(player_id);
504 if (player) 542 if (player)
505 player->Pause(is_media_related_action); 543 player->Pause(is_media_related_action);
544
545 MediaSession::Get(web_contents())->RemovePlayer(this, player_id);
506 } 546 }
507 547
508 void BrowserMediaPlayerManager::OnSetVolume(int player_id, double volume) { 548 void BrowserMediaPlayerManager::OnSetVolume(int player_id, double volume) {
509 MediaPlayerAndroid* player = GetPlayer(player_id); 549 MediaPlayerAndroid* player = GetPlayer(player_id);
510 if (player) 550 if (player)
511 player->SetVolume(volume); 551 player->SetVolume(volume);
512 } 552 }
513 553
514 void BrowserMediaPlayerManager::OnSetPoster(int player_id, const GURL& url) { 554 void BrowserMediaPlayerManager::OnSetPoster(int player_id, const GURL& url) {
515 // To be overridden by subclasses. 555 // To be overridden by subclasses.
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
619 external_video_surface_container_->ReleaseExternalVideoSurface(player_id); 659 external_video_surface_container_->ReleaseExternalVideoSurface(player_id);
620 #endif // defined(VIDEO_HOLE) 660 #endif // defined(VIDEO_HOLE)
621 } 661 }
622 662
623 void BrowserMediaPlayerManager::ReleasePlayer(MediaPlayerAndroid* player) { 663 void BrowserMediaPlayerManager::ReleasePlayer(MediaPlayerAndroid* player) {
624 player->Release(); 664 player->Release();
625 ReleaseMediaResources(player->player_id()); 665 ReleaseMediaResources(player->player_id());
626 } 666 }
627 667
628 } // namespace content 668 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698