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

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: 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 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 154
154 BrowserMediaPlayerManager::~BrowserMediaPlayerManager() { 155 BrowserMediaPlayerManager::~BrowserMediaPlayerManager() {
155 // During the tear down process, OnDestroyPlayer() may or may not be called 156 // 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 157 // (e.g. the WebContents may be destroyed before the render process). So
157 // we cannot DCHECK(players_.empty()) here. Instead, all media players in 158 // we cannot DCHECK(players_.empty()) here. Instead, all media players in
158 // |players_| will be destroyed here because |player_| is a ScopedVector. 159 // |players_| will be destroyed here because |player_| is a ScopedVector.
159 160
160 for (MediaPlayerAndroid* player : players_) 161 for (MediaPlayerAndroid* player : players_)
161 player->DeleteOnCorrectThread(); 162 player->DeleteOnCorrectThread();
162 163
164 MediaSession::Get(web_contents())->AbandonAudioFocus(this);
whywhat 2015/05/12 12:50:44 nit: s/Get/From?
mlamouri (slow - plz ping) 2015/05/19 21:56:15 I used ::Get() instead of ::From() because ::FromW
163 players_.weak_clear(); 165 players_.weak_clear();
164 } 166 }
165 167
166 void BrowserMediaPlayerManager::ExitFullscreen(bool release_media_player) { 168 void BrowserMediaPlayerManager::ExitFullscreen(bool release_media_player) {
167 if (WebContentsDelegate* delegate = web_contents_->GetDelegate()) 169 if (WebContentsDelegate* delegate = web_contents_->GetDelegate())
168 delegate->ExitFullscreenModeForTab(web_contents_); 170 delegate->ExitFullscreenModeForTab(web_contents_);
169 if (RenderWidgetHostViewAndroid* view_android = 171 if (RenderWidgetHostViewAndroid* view_android =
170 static_cast<RenderWidgetHostViewAndroid*>( 172 static_cast<RenderWidgetHostViewAndroid*>(
171 web_contents_->GetRenderWidgetHostView())) { 173 web_contents_->GetRenderWidgetHostView())) {
172 view_android->SetOverlayVideoMode(false); 174 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, 223 int player_id, base::TimeDelta duration, int width, int height,
222 bool success) { 224 bool success) {
223 Send(new MediaPlayerMsg_MediaMetadataChanged( 225 Send(new MediaPlayerMsg_MediaMetadataChanged(
224 RoutingID(), player_id, duration, width, height, success)); 226 RoutingID(), player_id, duration, width, height, success));
225 if (fullscreen_player_id_ == player_id) 227 if (fullscreen_player_id_ == player_id)
226 video_view_->UpdateMediaMetadata(); 228 video_view_->UpdateMediaMetadata();
227 } 229 }
228 230
229 void BrowserMediaPlayerManager::OnPlaybackComplete(int player_id) { 231 void BrowserMediaPlayerManager::OnPlaybackComplete(int player_id) {
230 Send(new MediaPlayerMsg_MediaPlaybackCompleted(RoutingID(), player_id)); 232 Send(new MediaPlayerMsg_MediaPlaybackCompleted(RoutingID(), player_id));
233 MediaSession::Get(web_contents())->AbandonAudioFocus(this, player_id);
234
231 if (fullscreen_player_id_ == player_id) 235 if (fullscreen_player_id_ == player_id)
232 video_view_->OnPlaybackComplete(); 236 video_view_->OnPlaybackComplete();
233 } 237 }
234 238
235 void BrowserMediaPlayerManager::OnMediaInterrupted(int player_id) { 239 void BrowserMediaPlayerManager::OnMediaInterrupted(int player_id) {
236 // Tell WebKit that the audio should be paused, then release all resources 240 // Tell WebKit that the audio should be paused, then release all resources
237 Send(new MediaPlayerMsg_MediaPlayerReleased(RoutingID(), player_id)); 241 Send(new MediaPlayerMsg_MediaPlayerReleased(RoutingID(), player_id));
238 OnReleaseResources(player_id); 242 OnReleaseResources(player_id);
239 } 243 }
240 244
(...skipping 19 matching lines...) Expand all
260 (*it)->Release(); 264 (*it)->Release();
261 } 265 }
262 } 266 }
263 267
264 void BrowserMediaPlayerManager::OnSeekComplete( 268 void BrowserMediaPlayerManager::OnSeekComplete(
265 int player_id, 269 int player_id,
266 const base::TimeDelta& current_time) { 270 const base::TimeDelta& current_time) {
267 Send(new MediaPlayerMsg_SeekCompleted(RoutingID(), player_id, current_time)); 271 Send(new MediaPlayerMsg_SeekCompleted(RoutingID(), player_id, current_time));
268 } 272 }
269 273
270 void BrowserMediaPlayerManager::OnError(int player_id, int error) { 274 void BrowserMediaPlayerManager::OnError(int player_id, int error) {
qinmin 2015/05/15 18:06:19 what should happen to the onError case? We still h
271 Send(new MediaPlayerMsg_MediaError(RoutingID(), player_id, error)); 275 Send(new MediaPlayerMsg_MediaError(RoutingID(), player_id, error));
272 if (fullscreen_player_id_ == player_id) 276 if (fullscreen_player_id_ == player_id)
273 video_view_->OnMediaPlayerError(error); 277 video_view_->OnMediaPlayerError(error);
274 } 278 }
275 279
276 void BrowserMediaPlayerManager::OnVideoSizeChanged( 280 void BrowserMediaPlayerManager::OnVideoSizeChanged(
277 int player_id, int width, int height) { 281 int player_id, int width, int height) {
278 Send(new MediaPlayerMsg_MediaVideoSizeChanged(RoutingID(), player_id, 282 Send(new MediaPlayerMsg_MediaVideoSizeChanged(RoutingID(), player_id,
279 width, height)); 283 width, height));
280 if (fullscreen_player_id_ == player_id) 284 if (fullscreen_player_id_ == player_id)
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 338
335 if (fullscreen_player_id_ != kInvalidMediaPlayerId) { 339 if (fullscreen_player_id_ != kInvalidMediaPlayerId) {
336 // TODO(qinmin): Determine the correct error code we should report to WMPA. 340 // TODO(qinmin): Determine the correct error code we should report to WMPA.
337 OnError(player_id, MediaPlayerAndroid::MEDIA_ERROR_DECODE); 341 OnError(player_id, MediaPlayerAndroid::MEDIA_ERROR_DECODE);
338 return; 342 return;
339 } 343 }
340 344
341 Send(new MediaPlayerMsg_RequestFullscreen(RoutingID(), player_id)); 345 Send(new MediaPlayerMsg_RequestFullscreen(RoutingID(), player_id));
342 } 346 }
343 347
348 bool BrowserMediaPlayerManager::RequestPlay(int player_id) {
349 MediaPlayerAndroid* player = GetPlayer(player_id);
whywhat 2015/05/12 12:50:45 I believe this should return true for remote playe
mlamouri (slow - plz ping) 2015/05/19 21:56:15 Remote players do not call this method AFAICT. How
350 DCHECK(player);
351 MediaSession::Type media_session_type =
352 player->GetDuration().InSeconds() > 5 ? MediaSession::Type::Content
whywhat 2015/05/12 12:50:45 5 is a magic constant! make it a named one, please
qinmin 2015/05/15 18:06:19 You need to special treat duration==0 case. Durati
mlamouri (slow - plz ping) 2015/05/19 21:56:15 Oups again.
qinmin 2015/05/20 21:35:24 HLS duration is never trustworthy on android. If w
mlamouri (slow - plz ping) 2015/05/22 15:58:52 Yeah, we made the call in StartInternal() on purpo
353 : MediaSession::Type::Transient;
354
355 bool succeeded = MediaSession::Get(web_contents())->RequestAudioFocus(
356 this, player_id, media_session_type);
357 if (!succeeded)
358 OnSuspend(player_id);
359 return succeeded;
360 }
361
344 #if defined(VIDEO_HOLE) 362 #if defined(VIDEO_HOLE)
345 void BrowserMediaPlayerManager::AttachExternalVideoSurface(int player_id, 363 void BrowserMediaPlayerManager::AttachExternalVideoSurface(int player_id,
346 jobject surface) { 364 jobject surface) {
347 MediaPlayerAndroid* player = GetPlayer(player_id); 365 MediaPlayerAndroid* player = GetPlayer(player_id);
348 if (player) { 366 if (player) {
349 player->SetVideoSurface( 367 player->SetVideoSurface(
350 gfx::ScopedJavaSurface::AcquireExternalSurface(surface)); 368 gfx::ScopedJavaSurface::AcquireExternalSurface(surface));
351 } 369 }
352 } 370 }
353 371
354 void BrowserMediaPlayerManager::DetachExternalVideoSurface(int player_id) { 372 void BrowserMediaPlayerManager::DetachExternalVideoSurface(int player_id) {
355 MediaPlayerAndroid* player = GetPlayer(player_id); 373 MediaPlayerAndroid* player = GetPlayer(player_id);
356 if (player) 374 if (player)
357 player->SetVideoSurface(gfx::ScopedJavaSurface()); 375 player->SetVideoSurface(gfx::ScopedJavaSurface());
358 } 376 }
359 377
360 void BrowserMediaPlayerManager::OnFrameInfoUpdated() { 378 void BrowserMediaPlayerManager::OnFrameInfoUpdated() {
361 if (fullscreen_player_id_ != kInvalidMediaPlayerId) 379 if (fullscreen_player_id_ != kInvalidMediaPlayerId)
362 return; 380 return;
363 381
364 if (external_video_surface_container_) 382 if (external_video_surface_container_)
365 external_video_surface_container_->OnFrameInfoUpdated(); 383 external_video_surface_container_->OnFrameInfoUpdated();
366 } 384 }
367 385
386 void BrowserMediaPlayerManager::OnSuspend(int player_id) {
387 MediaPlayerAndroid* player = GetPlayer(player_id);
388 DCHECK(player);
389
390 player->Pause(true);
391 Send(new MediaPlayerMsg_DidMediaPlayerPause(RoutingID(), player_id));
392 }
393
394 void BrowserMediaPlayerManager::OnResume(int player_id) {
395 MediaPlayerAndroid* player = GetPlayer(player_id);
396 DCHECK(player);
397
398 player->Start();
399 Send(new MediaPlayerMsg_DidMediaPlayerPlay(RoutingID(), player_id));
400 }
401
368 void BrowserMediaPlayerManager::OnNotifyExternalSurface( 402 void BrowserMediaPlayerManager::OnNotifyExternalSurface(
369 int player_id, bool is_request, const gfx::RectF& rect) { 403 int player_id, bool is_request, const gfx::RectF& rect) {
370 if (!web_contents_) 404 if (!web_contents_)
371 return; 405 return;
372 406
373 if (is_request) { 407 if (is_request) {
374 OnRequestExternalSurface(player_id, rect); 408 OnRequestExternalSurface(player_id, rect);
375 } 409 }
376 if (external_video_surface_container_) { 410 if (external_video_surface_container_) {
377 external_video_surface_container_->OnExternalVideoSurfacePositionChanged( 411 external_video_surface_container_->OnExternalVideoSurfacePositionChanged(
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 if (player) 530 if (player)
497 player->SeekTo(time); 531 player->SeekTo(time);
498 } 532 }
499 533
500 void BrowserMediaPlayerManager::OnPause( 534 void BrowserMediaPlayerManager::OnPause(
501 int player_id, 535 int player_id,
502 bool is_media_related_action) { 536 bool is_media_related_action) {
503 MediaPlayerAndroid* player = GetPlayer(player_id); 537 MediaPlayerAndroid* player = GetPlayer(player_id);
504 if (player) 538 if (player)
505 player->Pause(is_media_related_action); 539 player->Pause(is_media_related_action);
540
541 MediaSession::Get(web_contents())->AbandonAudioFocus(this, player_id);
506 } 542 }
507 543
508 void BrowserMediaPlayerManager::OnSetVolume(int player_id, double volume) { 544 void BrowserMediaPlayerManager::OnSetVolume(int player_id, double volume) {
509 MediaPlayerAndroid* player = GetPlayer(player_id); 545 MediaPlayerAndroid* player = GetPlayer(player_id);
510 if (player) 546 if (player)
511 player->SetVolume(volume); 547 player->SetVolume(volume);
512 } 548 }
513 549
514 void BrowserMediaPlayerManager::OnSetPoster(int player_id, const GURL& url) { 550 void BrowserMediaPlayerManager::OnSetPoster(int player_id, const GURL& url) {
515 // To be overridden by subclasses. 551 // 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); 655 external_video_surface_container_->ReleaseExternalVideoSurface(player_id);
620 #endif // defined(VIDEO_HOLE) 656 #endif // defined(VIDEO_HOLE)
621 } 657 }
622 658
623 void BrowserMediaPlayerManager::ReleasePlayer(MediaPlayerAndroid* player) { 659 void BrowserMediaPlayerManager::ReleasePlayer(MediaPlayerAndroid* player) {
624 player->Release(); 660 player->Release();
625 ReleaseMediaResources(player->player_id()); 661 ReleaseMediaResources(player->player_id());
626 } 662 }
627 663
628 } // namespace content 664 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698