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

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

Issue 1570043002: Implement MediaSession on top of the WebMediaPlayerDelegate. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@media_session
Patch Set: Merge. Cleanup. Fix RequestPlay. Created 4 years, 11 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 <utility> 7 #include <utility>
8 8
9 #include "base/android/scoped_java_ref.h" 9 #include "base/android/scoped_java_ref.h"
10 #include "content/browser/frame_host/render_frame_host_impl.h" 10 #include "content/browser/frame_host/render_frame_host_impl.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"
14 #include "content/browser/media/android/media_throttler.h" 13 #include "content/browser/media/android/media_throttler.h"
15 #include "content/browser/media/android/media_web_contents_observer_android.h" 14 #include "content/browser/media/android/media_web_contents_observer_android.h"
16 #include "content/browser/renderer_host/render_view_host_impl.h" 15 #include "content/browser/renderer_host/render_view_host_impl.h"
17 #include "content/browser/web_contents/web_contents_impl.h" 16 #include "content/browser/web_contents/web_contents_impl.h"
18 #include "content/common/media/media_player_messages_android.h" 17 #include "content/common/media/media_player_messages_android.h"
19 #include "content/public/browser/android/external_video_surface_container.h" 18 #include "content/public/browser/android/external_video_surface_container.h"
20 #include "content/public/browser/browser_context.h" 19 #include "content/public/browser/browser_context.h"
21 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/content_browser_client.h" 21 #include "content/public/browser/content_browser_client.h"
23 #include "content/public/browser/render_frame_host.h" 22 #include "content/public/browser/render_frame_host.h"
(...skipping 21 matching lines...) Expand all
45 using media::MediaPlayerManager; 44 using media::MediaPlayerManager;
46 using media::MediaSourcePlayer; 45 using media::MediaSourcePlayer;
47 46
48 namespace content { 47 namespace content {
49 48
50 // Threshold on the number of media players per renderer before we start 49 // Threshold on the number of media players per renderer before we start
51 // attempting to release inactive media players. 50 // attempting to release inactive media players.
52 const int kMediaPlayerThreshold = 1; 51 const int kMediaPlayerThreshold = 1;
53 const int kInvalidMediaPlayerId = -1; 52 const int kInvalidMediaPlayerId = -1;
54 53
55 // Minimal duration of a media player in order to be considered as Content type.
56 const int kMinimumDurationForContentInSeconds = 5;
57
58 static BrowserMediaPlayerManager::Factory g_factory = NULL; 54 static BrowserMediaPlayerManager::Factory g_factory = NULL;
59 static media::MediaUrlInterceptor* media_url_interceptor_ = NULL; 55 static media::MediaUrlInterceptor* media_url_interceptor_ = NULL;
60 56
61 // static 57 // static
62 void BrowserMediaPlayerManager::RegisterFactory(Factory factory) { 58 void BrowserMediaPlayerManager::RegisterFactory(Factory factory) {
63 // TODO(aberent) nullptr test is a temporary fix to simplify upstreaming Cast. 59 // TODO(aberent) nullptr test is a temporary fix to simplify upstreaming Cast.
64 // Until Cast is fully upstreamed we want the downstream factory to take 60 // Until Cast is fully upstreamed we want the downstream factory to take
65 // priority over the upstream factory. The downstream call happens first, 61 // priority over the upstream factory. The downstream call happens first,
66 // so this will ensure that it does. 62 // so this will ensure that it does.
67 if (g_factory == nullptr) 63 if (g_factory == nullptr)
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 217
222 BrowserMediaPlayerManager::~BrowserMediaPlayerManager() { 218 BrowserMediaPlayerManager::~BrowserMediaPlayerManager() {
223 // During the tear down process, OnDestroyPlayer() may or may not be called 219 // During the tear down process, OnDestroyPlayer() may or may not be called
224 // (e.g. the WebContents may be destroyed before the render process). So 220 // (e.g. the WebContents may be destroyed before the render process). So
225 // we cannot DCHECK(players_.empty()) here. Instead, all media players in 221 // we cannot DCHECK(players_.empty()) here. Instead, all media players in
226 // |players_| will be destroyed here because |player_| is a ScopedVector. 222 // |players_| will be destroyed here because |player_| is a ScopedVector.
227 223
228 for (MediaPlayerAndroid* player : players_) 224 for (MediaPlayerAndroid* player : players_)
229 player->DeleteOnCorrectThread(); 225 player->DeleteOnCorrectThread();
230 226
231 MediaSession::Get(web_contents())->RemovePlayers(this);
232 players_.weak_clear(); 227 players_.weak_clear();
233 } 228 }
234 229
235 void BrowserMediaPlayerManager::ExitFullscreen(bool release_media_player) { 230 void BrowserMediaPlayerManager::ExitFullscreen(bool release_media_player) {
236 #if defined(USE_AURA) 231 #if defined(USE_AURA)
237 // TODO(crbug.com/548024) 232 // TODO(crbug.com/548024)
238 NOTIMPLEMENTED(); 233 NOTIMPLEMENTED();
239 #else 234 #else
240 if (WebContentsDelegate* delegate = web_contents_->GetDelegate()) 235 if (WebContentsDelegate* delegate = web_contents_->GetDelegate())
241 delegate->ExitFullscreenModeForTab(web_contents_); 236 delegate->ExitFullscreenModeForTab(web_contents_);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 int player_id, base::TimeDelta duration, int width, int height, 292 int player_id, base::TimeDelta duration, int width, int height,
298 bool success) { 293 bool success) {
299 Send(new MediaPlayerMsg_MediaMetadataChanged( 294 Send(new MediaPlayerMsg_MediaMetadataChanged(
300 RoutingID(), player_id, duration, width, height, success)); 295 RoutingID(), player_id, duration, width, height, success));
301 if (fullscreen_player_id_ == player_id) 296 if (fullscreen_player_id_ == player_id)
302 video_view_->UpdateMediaMetadata(); 297 video_view_->UpdateMediaMetadata();
303 } 298 }
304 299
305 void BrowserMediaPlayerManager::OnPlaybackComplete(int player_id) { 300 void BrowserMediaPlayerManager::OnPlaybackComplete(int player_id) {
306 Send(new MediaPlayerMsg_MediaPlaybackCompleted(RoutingID(), player_id)); 301 Send(new MediaPlayerMsg_MediaPlaybackCompleted(RoutingID(), player_id));
307 MediaSession::Get(web_contents())->RemovePlayer(this, player_id);
308 302
309 if (fullscreen_player_id_ == player_id) 303 if (fullscreen_player_id_ == player_id)
310 video_view_->OnPlaybackComplete(); 304 video_view_->OnPlaybackComplete();
311 } 305 }
312 306
313 void BrowserMediaPlayerManager::OnMediaInterrupted(int player_id) { 307 void BrowserMediaPlayerManager::OnMediaInterrupted(int player_id) {
314 // Tell WebKit that the audio should be paused, then release all resources 308 // Tell WebKit that the audio should be paused, then release all resources
315 Send(new MediaPlayerMsg_MediaPlayerReleased(RoutingID(), player_id)); 309 Send(new MediaPlayerMsg_MediaPlayerReleased(RoutingID(), player_id));
316 MediaSession::Get(web_contents())->RemovePlayer(this, player_id);
317 ReleaseResources(player_id); 310 ReleaseResources(player_id);
318 } 311 }
319 312
320 void BrowserMediaPlayerManager::OnBufferingUpdate(int player_id, 313 void BrowserMediaPlayerManager::OnBufferingUpdate(int player_id,
321 int percentage) { 314 int percentage) {
322 Send(new MediaPlayerMsg_MediaBufferingUpdate(RoutingID(), player_id, 315 Send(new MediaPlayerMsg_MediaBufferingUpdate(RoutingID(), player_id,
323 percentage)); 316 percentage));
324 } 317 }
325 318
326 void BrowserMediaPlayerManager::OnSeekRequest( 319 void BrowserMediaPlayerManager::OnSeekRequest(
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 it != players_.end(); ++it) { 390 it != players_.end(); ++it) {
398 if ((*it)->player_id() == player_id) 391 if ((*it)->player_id() == player_id)
399 return *it; 392 return *it;
400 } 393 }
401 return NULL; 394 return NULL;
402 } 395 }
403 396
404 bool BrowserMediaPlayerManager::RequestPlay(int player_id, 397 bool BrowserMediaPlayerManager::RequestPlay(int player_id,
405 base::TimeDelta duration, 398 base::TimeDelta duration,
406 bool has_audio) { 399 bool has_audio) {
407 if (!has_audio) 400 DCHECK(player_id_to_delegate_id_map_.find(player_id) !=
408 return true; 401 player_id_to_delegate_id_map_.end());
409 402 return MediaWebContentsObserverAndroid::FromWebContents(web_contents_)
410 MediaSession::Type media_session_type = 403 ->RequestPlay(render_frame_host_,
411 duration == base::TimeDelta() || 404 player_id_to_delegate_id_map_[player_id], has_audio,
412 duration.InSeconds() > kMinimumDurationForContentInSeconds 405 IsPlayingRemotely(player_id), duration);
413 ? MediaSession::Type::Content
414 : MediaSession::Type::Transient;
415
416 bool succeeded = MediaSession::Get(web_contents())->AddPlayer(
417 this, player_id, media_session_type);
418 if (!succeeded)
419 Send(new MediaPlayerMsg_DidMediaPlayerPause(RoutingID(), player_id));
420 return succeeded;
421 }
422
423 void BrowserMediaPlayerManager::OnSuspend(int player_id) {
424 MediaPlayerAndroid* player = GetPlayer(player_id);
425 DCHECK(player);
426
427 player->Pause(true);
428 Send(new MediaPlayerMsg_DidMediaPlayerPause(RoutingID(), player_id));
429 }
430
431 void BrowserMediaPlayerManager::OnResume(int player_id) {
432 MediaPlayerAndroid* player = GetPlayer(player_id);
433 DCHECK(player);
434
435 player->Start();
436 Send(new MediaPlayerMsg_DidMediaPlayerPlay(RoutingID(), player_id));
437 } 406 }
438 407
439 #if defined(VIDEO_HOLE) 408 #if defined(VIDEO_HOLE)
440 void BrowserMediaPlayerManager::AttachExternalVideoSurface(int player_id, 409 void BrowserMediaPlayerManager::AttachExternalVideoSurface(int player_id,
441 jobject surface) { 410 jobject surface) {
442 MediaPlayerAndroid* player = GetPlayer(player_id); 411 MediaPlayerAndroid* player = GetPlayer(player_id);
443 if (player) { 412 if (player) {
444 player->SetVideoSurface( 413 player->SetVideoSurface(
445 gfx::ScopedJavaSurface::AcquireExternalSurface(surface)); 414 gfx::ScopedJavaSurface::AcquireExternalSurface(surface));
446 } 415 }
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 RenderProcessHostImpl* host = static_cast<RenderProcessHostImpl*>( 531 RenderProcessHostImpl* host = static_cast<RenderProcessHostImpl*>(
563 web_contents()->GetRenderProcessHost()); 532 web_contents()->GetRenderProcessHost());
564 MediaPlayerAndroid* player = 533 MediaPlayerAndroid* player =
565 CreateMediaPlayer(media_player_params, 534 CreateMediaPlayer(media_player_params,
566 host->GetBrowserContext()->IsOffTheRecord(), 535 host->GetBrowserContext()->IsOffTheRecord(),
567 host->browser_demuxer_android().get()); 536 host->browser_demuxer_android().get());
568 537
569 if (!player) 538 if (!player)
570 return; 539 return;
571 540
541 player_id_to_delegate_id_map_[media_player_params.player_id] =
542 media_player_params.delegate_id;
572 AddPlayer(player); 543 AddPlayer(player);
573 } 544 }
574 545
575 void BrowserMediaPlayerManager::OnStart(int player_id) { 546 void BrowserMediaPlayerManager::OnStart(int player_id) {
576 MediaPlayerAndroid* player = GetPlayer(player_id); 547 MediaPlayerAndroid* player = GetPlayer(player_id);
577 if (!player) 548 if (!player)
578 return; 549 return;
579 550
580 if (RequestDecoderResources(player_id, false)) { 551 if (RequestDecoderResources(player_id, false)) {
581 StartInternal(player_id); 552 StartInternal(player_id);
(...skipping 12 matching lines...) Expand all
594 if (player) 565 if (player)
595 player->SeekTo(time); 566 player->SeekTo(time);
596 } 567 }
597 568
598 void BrowserMediaPlayerManager::OnPause( 569 void BrowserMediaPlayerManager::OnPause(
599 int player_id, 570 int player_id,
600 bool is_media_related_action) { 571 bool is_media_related_action) {
601 MediaPlayerAndroid* player = GetPlayer(player_id); 572 MediaPlayerAndroid* player = GetPlayer(player_id);
602 if (player) 573 if (player)
603 player->Pause(is_media_related_action); 574 player->Pause(is_media_related_action);
604
605 if (is_media_related_action)
606 MediaSession::Get(web_contents())->OnPlayerPaused(this, player_id);
607 } 575 }
608 576
609 void BrowserMediaPlayerManager::OnSetVolume(int player_id, double volume) { 577 void BrowserMediaPlayerManager::OnSetVolume(int player_id, double volume) {
610 MediaPlayerAndroid* player = GetPlayer(player_id); 578 MediaPlayerAndroid* player = GetPlayer(player_id);
611 if (player) 579 if (player)
612 player->SetVolume(volume); 580 player->SetVolume(volume);
613 } 581 }
614 582
615 void BrowserMediaPlayerManager::OnSetPoster(int player_id, const GURL& url) { 583 void BrowserMediaPlayerManager::OnSetPoster(int player_id, const GURL& url) {
616 // To be overridden by subclasses. 584 // To be overridden by subclasses.
617 } 585 }
618 586
619 void BrowserMediaPlayerManager::OnSuspendAndReleaseResources(int player_id) { 587 void BrowserMediaPlayerManager::OnSuspendAndReleaseResources(int player_id) {
620 MediaSession::Get(web_contents())->RemovePlayer(this, player_id);
621 ReleaseResources(player_id); 588 ReleaseResources(player_id);
622 } 589 }
623 590
624 void BrowserMediaPlayerManager::OnDestroyPlayer(int player_id) { 591 void BrowserMediaPlayerManager::OnDestroyPlayer(int player_id) {
625 DestroyPlayer(player_id); 592 DestroyPlayer(player_id);
626 if (fullscreen_player_id_ == player_id) 593 if (fullscreen_player_id_ == player_id)
627 fullscreen_player_id_ = kInvalidMediaPlayerId; 594 fullscreen_player_id_ = kInvalidMediaPlayerId;
628 } 595 }
629 596
630 void BrowserMediaPlayerManager::OnRequestRemotePlayback(int /* player_id */) { 597 void BrowserMediaPlayerManager::OnRequestRemotePlayback(int /* player_id */) {
(...skipping 12 matching lines...) Expand all
643 610
644 void BrowserMediaPlayerManager::DestroyPlayer(int player_id) { 611 void BrowserMediaPlayerManager::DestroyPlayer(int player_id) {
645 for (ScopedVector<MediaPlayerAndroid>::iterator it = players_.begin(); 612 for (ScopedVector<MediaPlayerAndroid>::iterator it = players_.begin();
646 it != players_.end(); ++it) { 613 it != players_.end(); ++it) {
647 if ((*it)->player_id() == player_id) { 614 if ((*it)->player_id() == player_id) {
648 #if defined(VIDEO_HOLE) 615 #if defined(VIDEO_HOLE)
649 ReleaseExternalSurface(player_id); 616 ReleaseExternalSurface(player_id);
650 #endif 617 #endif
651 (*it)->DeleteOnCorrectThread(); 618 (*it)->DeleteOnCorrectThread();
652 players_.weak_erase(it); 619 players_.weak_erase(it);
653 MediaSession::Get(web_contents())->RemovePlayer(this, player_id);
654 break; 620 break;
655 } 621 }
656 } 622 }
657 active_players_.erase(player_id); 623 active_players_.erase(player_id);
658 } 624 }
659 625
660 void BrowserMediaPlayerManager::ReleaseResources(int player_id) { 626 void BrowserMediaPlayerManager::ReleaseResources(int player_id) {
661 MediaPlayerAndroid* player = GetPlayer(player_id); 627 MediaPlayerAndroid* player = GetPlayer(player_id);
662 if (player) 628 if (player)
663 ReleasePlayer(player); 629 ReleasePlayer(player);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
722 } 688 }
723 689
724 void BrowserMediaPlayerManager::OnDecoderResourcesReleased(int player_id) { 690 void BrowserMediaPlayerManager::OnDecoderResourcesReleased(int player_id) {
725 if (active_players_.find(player_id) == active_players_.end()) 691 if (active_players_.find(player_id) == active_players_.end())
726 return; 692 return;
727 693
728 active_players_.erase(player_id); 694 active_players_.erase(player_id);
729 MediaThrottler::GetInstance()->OnDecodeRequestFinished(); 695 MediaThrottler::GetInstance()->OnDecodeRequestFinished();
730 } 696 }
731 697
698 bool BrowserMediaPlayerManager::IsPlayingRemotely(int player_id) {
699 return false;
700 }
701
732 int BrowserMediaPlayerManager::RoutingID() { 702 int BrowserMediaPlayerManager::RoutingID() {
733 return render_frame_host_->GetRoutingID(); 703 return render_frame_host_->GetRoutingID();
734 } 704 }
735 705
736 bool BrowserMediaPlayerManager::Send(IPC::Message* msg) { 706 bool BrowserMediaPlayerManager::Send(IPC::Message* msg) {
737 return render_frame_host_->Send(msg); 707 return render_frame_host_->Send(msg);
738 } 708 }
739 709
740 void BrowserMediaPlayerManager::ReleaseFullscreenPlayer( 710 void BrowserMediaPlayerManager::ReleaseFullscreenPlayer(
741 MediaPlayerAndroid* player) { 711 MediaPlayerAndroid* player) {
(...skipping 21 matching lines...) Expand all
763 if (!player) 733 if (!player)
764 return; 734 return;
765 player->Start(); 735 player->Start();
766 if (fullscreen_player_id_ == player_id && fullscreen_player_is_released_) { 736 if (fullscreen_player_id_ == player_id && fullscreen_player_is_released_) {
767 video_view_->OpenVideo(); 737 video_view_->OpenVideo();
768 fullscreen_player_is_released_ = false; 738 fullscreen_player_is_released_ = false;
769 } 739 }
770 } 740 }
771 741
772 } // namespace content 742 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698