Commit df644fb9 authored by Wojciech Dzierżanowski's avatar Wojciech Dzierżanowski Committed by Commit Bot

Start routing MediaSessionService when requesting playback with PiP active

When a new playback request arrives on a site that is already playing
videos in PiP, make sure the MediaSession is active.

Bug: 1062291
Change-Id: Ib082b183a0ccd32f757841c1e37564be92a0692e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2120403
Commit-Queue: Wojciech Dzierżanowski <wdzierzanowski@opera.com>
Reviewed-by: default avatarBecca Hughes <beccahughes@chromium.org>
Cr-Commit-Position: refs/heads/master@{#755313}
parent 0b6c5b6d
......@@ -158,7 +158,6 @@ void MediaSessionController::OnPlaybackPaused() {
void MediaSessionController::PictureInPictureStateChanged(
bool is_picture_in_picture) {
is_picture_in_picture_active_ = is_picture_in_picture;
AddOrRemovePlayer();
}
......@@ -184,7 +183,9 @@ bool MediaSessionController::IsMediaSessionNeeded() const {
const bool has_audio =
has_audio_ &&
!media_web_contents_observer_->web_contents()->IsAudioMuted();
return !is_remote_ && (has_audio || is_picture_in_picture_active_);
return !is_remote_ &&
(has_audio || media_web_contents_observer_->web_contents()
->HasPictureInPictureVideo());
}
void MediaSessionController::AddOrRemovePlayer() {
......
......@@ -105,7 +105,6 @@ class CONTENT_EXPORT MediaSessionController
bool has_video_ = false;
bool is_remote_ = false;
bool is_picture_in_picture_available_ = false;
bool is_picture_in_picture_active_ = false;
media::MediaContentType media_content_type_ =
media::MediaContentType::Persistent;
......
......@@ -66,12 +66,6 @@ class MediaSessionControllerTest : public RenderViewHostImplTestHarness {
multiplier);
}
void SetWebContentsAudioMuted(bool muted) {
contents()->SetAudioMuted(muted);
// The following happens automatically in a running browser.
controller_->WebContentsMutedStateChanged(muted);
}
void ResetHasSessionBit() { controller_->has_session_ = false; }
template <typename T>
......@@ -336,7 +330,7 @@ TEST_F(MediaSessionControllerTest, PictureInPictureAvailability) {
}
TEST_F(MediaSessionControllerTest, AddPlayerWhenUnmuted) {
SetWebContentsAudioMuted(true);
contents()->SetAudioMuted(true);
ASSERT_TRUE(controller_->Initialize(
/* has_audio = */ true, /* is_remote = */ false,
......@@ -344,7 +338,8 @@ TEST_F(MediaSessionControllerTest, AddPlayerWhenUnmuted) {
/* is_pip_available = */ true, /* has_video = */ false));
ASSERT_FALSE(media_session()->IsActive());
SetWebContentsAudioMuted(false);
contents()->SetAudioMuted(false);
controller_->WebContentsMutedStateChanged(false);
EXPECT_TRUE(media_session()->IsActive());
}
......@@ -355,12 +350,13 @@ TEST_F(MediaSessionControllerTest, RemovePlayerWhenMuted) {
/* is_pip_available = */ true, /* has_video = */ false));
ASSERT_TRUE(media_session()->IsActive());
SetWebContentsAudioMuted(true);
contents()->SetAudioMuted(true);
controller_->WebContentsMutedStateChanged(true);
EXPECT_FALSE(media_session()->IsActive());
}
TEST_F(MediaSessionControllerTest, EnterLeavePictureInPictureMuted) {
SetWebContentsAudioMuted(true);
contents()->SetAudioMuted(true);
ASSERT_TRUE(controller_->Initialize(
/* has_audio = */ true, /* is_remote = */ false,
......@@ -370,9 +366,11 @@ TEST_F(MediaSessionControllerTest, EnterLeavePictureInPictureMuted) {
// Entering PictureInPicture means the user expects to control the media, so
// this should activate the session.
contents()->SetHasPictureInPictureVideo(true);
controller_->PictureInPictureStateChanged(true);
EXPECT_TRUE(media_session()->IsActive());
contents()->SetHasPictureInPictureVideo(false);
controller_->PictureInPictureStateChanged(false);
EXPECT_FALSE(media_session()->IsActive());
}
......@@ -382,15 +380,17 @@ TEST_F(MediaSessionControllerTest, MuteWithPictureInPicture) {
/* has_audio = */ true, /* is_remote = */ false,
media::MediaContentType::Persistent, nullptr,
/* is_pip_available = */ true, /* has_video = */ false));
contents()->SetHasPictureInPictureVideo(true);
controller_->PictureInPictureStateChanged(true);
ASSERT_TRUE(media_session()->IsActive());
SetWebContentsAudioMuted(true);
contents()->SetAudioMuted(true);
controller_->WebContentsMutedStateChanged(true);
EXPECT_TRUE(media_session()->IsActive());
}
TEST_F(MediaSessionControllerTest, LeavePictureInPictureUnmuted) {
SetWebContentsAudioMuted(true);
contents()->SetAudioMuted(true);
ASSERT_TRUE(controller_->Initialize(
/* has_audio = */ true, /* is_remote = */ false,
......@@ -398,11 +398,14 @@ TEST_F(MediaSessionControllerTest, LeavePictureInPictureUnmuted) {
/* is_pip_available = */ true, /* has_video = */ false));
ASSERT_FALSE(media_session()->IsActive());
contents()->SetAudioMuted(false);
controller_->WebContentsMutedStateChanged(false);
contents()->SetHasPictureInPictureVideo(true);
controller_->PictureInPictureStateChanged(true);
SetWebContentsAudioMuted(false);
// Media was unmuted, so we now have audio focus, which should keep the
// session active.
contents()->SetHasPictureInPictureVideo(false);
controller_->PictureInPictureStateChanged(false);
EXPECT_TRUE(media_session()->IsActive());
}
......@@ -429,10 +432,27 @@ TEST_F(MediaSessionControllerTest,
/* is_pip_available = */ true, /* has_video = */ false));
ASSERT_FALSE(media_session()->IsActive());
contents()->SetHasPictureInPictureVideo(true);
controller_->PictureInPictureStateChanged(true);
EXPECT_TRUE(media_session()->IsActive());
}
TEST_F(MediaSessionControllerTest,
AddPlayerInitiallyPictureInPictureWithNoAudio) {
contents()->SetHasPictureInPictureVideo(true);
ASSERT_TRUE(controller_->Initialize(
/* has_audio = */ false, /* is_remote = */ false,
media::MediaContentType::Persistent, nullptr,
/* is_pip_available = */ true, /* has_video = */ false));
EXPECT_TRUE(media_session()->IsActive());
contents()->SetHasPictureInPictureVideo(false);
controller_->PictureInPictureStateChanged(false);
EXPECT_FALSE(media_session()->IsActive());
}
TEST_F(MediaSessionControllerTest, HasVideo_True) {
ASSERT_TRUE(controller_->Initialize(
/* has_audio = */ true, /* is_remote = */ false,
......
......@@ -65,8 +65,8 @@ class CONTENT_EXPORT MediaSessionControllersManager {
const MediaPlayerId& id,
const media_session::MediaPosition& position);
// Called when entering/leaving Picture-in-Picture for the given media
// player.
// Called when entering/leaving Picture-in-Picture for the associated
// WebContents.
void PictureInPictureStateChanged(bool is_picture_in_picture);
// Called when the WebContents was muted or unmuted.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment