| OLD | NEW |
| 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 Loading... |
| 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 Loading... |
| 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 Loading... |
| 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 OnReleaseResources(player_id); | 310 OnReleaseResources(player_id); |
| 317 } | 311 } |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 for (ScopedVector<MediaPlayerAndroid>::iterator it = players_.begin(); | 389 for (ScopedVector<MediaPlayerAndroid>::iterator it = players_.begin(); |
| 396 it != players_.end(); ++it) { | 390 it != players_.end(); ++it) { |
| 397 if ((*it)->player_id() == player_id) | 391 if ((*it)->player_id() == player_id) |
| 398 return *it; | 392 return *it; |
| 399 } | 393 } |
| 400 return NULL; | 394 return NULL; |
| 401 } | 395 } |
| 402 | 396 |
| 403 bool BrowserMediaPlayerManager::RequestPlay(int player_id, | 397 bool BrowserMediaPlayerManager::RequestPlay(int player_id, |
| 404 base::TimeDelta duration) { | 398 base::TimeDelta duration) { |
| 405 MediaSession::Type media_session_type = | 399 // TODO(dalecurtis, timav): Remove the concept of RequestPlay(), it's added |
| 406 duration == base::TimeDelta() || | 400 // latency for the vast majority of playbacks. Instead playback is paused |
| 407 duration.InSeconds() > kMinimumDurationForContentInSeconds | 401 // after startup by the MediaWebContentsObserverAndroid if permission is |
| 408 ? MediaSession::Type::Content | 402 // denied. http://crbug.com/XXXXXX |
| 409 : MediaSession::Type::Transient; | 403 return true; |
| 410 | |
| 411 bool succeeded = MediaSession::Get(web_contents())->AddPlayer( | |
| 412 this, player_id, media_session_type); | |
| 413 if (!succeeded) | |
| 414 Send(new MediaPlayerMsg_DidMediaPlayerPause(RoutingID(), player_id)); | |
| 415 return succeeded; | |
| 416 } | |
| 417 | |
| 418 void BrowserMediaPlayerManager::OnSuspend(int player_id) { | |
| 419 MediaPlayerAndroid* player = GetPlayer(player_id); | |
| 420 DCHECK(player); | |
| 421 | |
| 422 player->Pause(true); | |
| 423 Send(new MediaPlayerMsg_DidMediaPlayerPause(RoutingID(), player_id)); | |
| 424 } | |
| 425 | |
| 426 void BrowserMediaPlayerManager::OnResume(int player_id) { | |
| 427 MediaPlayerAndroid* player = GetPlayer(player_id); | |
| 428 DCHECK(player); | |
| 429 | |
| 430 player->Start(); | |
| 431 Send(new MediaPlayerMsg_DidMediaPlayerPlay(RoutingID(), player_id)); | |
| 432 } | 404 } |
| 433 | 405 |
| 434 #if defined(VIDEO_HOLE) | 406 #if defined(VIDEO_HOLE) |
| 435 void BrowserMediaPlayerManager::AttachExternalVideoSurface(int player_id, | 407 void BrowserMediaPlayerManager::AttachExternalVideoSurface(int player_id, |
| 436 jobject surface) { | 408 jobject surface) { |
| 437 MediaPlayerAndroid* player = GetPlayer(player_id); | 409 MediaPlayerAndroid* player = GetPlayer(player_id); |
| 438 if (player) { | 410 if (player) { |
| 439 player->SetVideoSurface( | 411 player->SetVideoSurface( |
| 440 gfx::ScopedJavaSurface::AcquireExternalSurface(surface)); | 412 gfx::ScopedJavaSurface::AcquireExternalSurface(surface)); |
| 441 } | 413 } |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 589 if (player) | 561 if (player) |
| 590 player->SeekTo(time); | 562 player->SeekTo(time); |
| 591 } | 563 } |
| 592 | 564 |
| 593 void BrowserMediaPlayerManager::OnPause( | 565 void BrowserMediaPlayerManager::OnPause( |
| 594 int player_id, | 566 int player_id, |
| 595 bool is_media_related_action) { | 567 bool is_media_related_action) { |
| 596 MediaPlayerAndroid* player = GetPlayer(player_id); | 568 MediaPlayerAndroid* player = GetPlayer(player_id); |
| 597 if (player) | 569 if (player) |
| 598 player->Pause(is_media_related_action); | 570 player->Pause(is_media_related_action); |
| 599 | |
| 600 if (is_media_related_action) | |
| 601 MediaSession::Get(web_contents())->OnPlayerPaused(this, player_id); | |
| 602 } | 571 } |
| 603 | 572 |
| 604 void BrowserMediaPlayerManager::OnSetVolume(int player_id, double volume) { | 573 void BrowserMediaPlayerManager::OnSetVolume(int player_id, double volume) { |
| 605 MediaPlayerAndroid* player = GetPlayer(player_id); | 574 MediaPlayerAndroid* player = GetPlayer(player_id); |
| 606 if (player) | 575 if (player) |
| 607 player->SetVolume(volume); | 576 player->SetVolume(volume); |
| 608 } | 577 } |
| 609 | 578 |
| 610 void BrowserMediaPlayerManager::OnSetPoster(int player_id, const GURL& url) { | 579 void BrowserMediaPlayerManager::OnSetPoster(int player_id, const GURL& url) { |
| 611 // To be overridden by subclasses. | 580 // To be overridden by subclasses. |
| 612 } | 581 } |
| 613 | 582 |
| 614 void BrowserMediaPlayerManager::OnReleaseResources(int player_id) { | 583 void BrowserMediaPlayerManager::OnReleaseResources(int player_id) { |
| 615 MediaPlayerAndroid* player = GetPlayer(player_id); | 584 MediaPlayerAndroid* player = GetPlayer(player_id); |
| 616 if (player) { | 585 if (player) |
| 617 // Videos can't play in the background, so are removed from the media | |
| 618 // session. | |
| 619 if (player->GetVideoWidth() > 0) | |
| 620 MediaSession::Get(web_contents())->RemovePlayer(this, player_id); | |
| 621 | |
| 622 ReleasePlayer(player); | 586 ReleasePlayer(player); |
| 623 } | |
| 624 if (player_id == fullscreen_player_id_) | 587 if (player_id == fullscreen_player_id_) |
| 625 fullscreen_player_is_released_ = true; | 588 fullscreen_player_is_released_ = true; |
| 626 } | 589 } |
| 627 | 590 |
| 628 void BrowserMediaPlayerManager::OnDestroyPlayer(int player_id) { | 591 void BrowserMediaPlayerManager::OnDestroyPlayer(int player_id) { |
| 629 DestroyPlayer(player_id); | 592 DestroyPlayer(player_id); |
| 630 if (fullscreen_player_id_ == player_id) | 593 if (fullscreen_player_id_ == player_id) |
| 631 fullscreen_player_id_ = kInvalidMediaPlayerId; | 594 fullscreen_player_id_ = kInvalidMediaPlayerId; |
| 632 } | 595 } |
| 633 | 596 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 647 | 610 |
| 648 void BrowserMediaPlayerManager::DestroyPlayer(int player_id) { | 611 void BrowserMediaPlayerManager::DestroyPlayer(int player_id) { |
| 649 for (ScopedVector<MediaPlayerAndroid>::iterator it = players_.begin(); | 612 for (ScopedVector<MediaPlayerAndroid>::iterator it = players_.begin(); |
| 650 it != players_.end(); ++it) { | 613 it != players_.end(); ++it) { |
| 651 if ((*it)->player_id() == player_id) { | 614 if ((*it)->player_id() == player_id) { |
| 652 #if defined(VIDEO_HOLE) | 615 #if defined(VIDEO_HOLE) |
| 653 ReleaseExternalSurface(player_id); | 616 ReleaseExternalSurface(player_id); |
| 654 #endif | 617 #endif |
| 655 (*it)->DeleteOnCorrectThread(); | 618 (*it)->DeleteOnCorrectThread(); |
| 656 players_.weak_erase(it); | 619 players_.weak_erase(it); |
| 657 MediaSession::Get(web_contents())->RemovePlayer(this, player_id); | |
| 658 break; | 620 break; |
| 659 } | 621 } |
| 660 } | 622 } |
| 661 active_players_.erase(player_id); | 623 active_players_.erase(player_id); |
| 662 } | 624 } |
| 663 | 625 |
| 664 scoped_ptr<media::MediaPlayerAndroid> BrowserMediaPlayerManager::SwapPlayer( | 626 scoped_ptr<media::MediaPlayerAndroid> BrowserMediaPlayerManager::SwapPlayer( |
| 665 int player_id, media::MediaPlayerAndroid* player) { | 627 int player_id, media::MediaPlayerAndroid* player) { |
| 666 media::MediaPlayerAndroid* previous_player = NULL; | 628 media::MediaPlayerAndroid* previous_player = NULL; |
| 667 for (ScopedVector<MediaPlayerAndroid>::iterator it = players_.begin(); | 629 for (ScopedVector<MediaPlayerAndroid>::iterator it = players_.begin(); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 759 if (!player) | 721 if (!player) |
| 760 return; | 722 return; |
| 761 player->Start(); | 723 player->Start(); |
| 762 if (fullscreen_player_id_ == player_id && fullscreen_player_is_released_) { | 724 if (fullscreen_player_id_ == player_id && fullscreen_player_is_released_) { |
| 763 video_view_->OpenVideo(); | 725 video_view_->OpenVideo(); |
| 764 fullscreen_player_is_released_ = false; | 726 fullscreen_player_is_released_ = false; |
| 765 } | 727 } |
| 766 } | 728 } |
| 767 | 729 |
| 768 } // namespace content | 730 } // namespace content |
| OLD | NEW |