Index: content/browser/media/session/media_session.cc |
diff --git a/content/browser/media/session/media_session.cc b/content/browser/media/session/media_session.cc |
index 33fa652b363248c485df89dd634ee4aed9951a49..79df56701892976eb46c99079b083a886f0331b1 100644 |
--- a/content/browser/media/session/media_session.cc |
+++ b/content/browser/media/session/media_session.cc |
@@ -9,6 +9,7 @@ |
#include "content/browser/web_contents/web_contents_impl.h" |
#include "content/public/browser/web_contents.h" |
#include "content/public/browser/web_contents_delegate.h" |
+#include "media/base/media_content_type.h" |
namespace content { |
@@ -68,24 +69,37 @@ void MediaSession::SetMetadata(const MediaMetadata& metadata) { |
bool MediaSession::AddPlayer(MediaSessionObserver* observer, |
int player_id, |
- Type type) { |
+ media::MediaContentType media_content_type) { |
observer->OnSetVolumeMultiplier(player_id, volume_multiplier_); |
+ // Determine the audio focus type required for playing the new player. |
+ // TODO(zqzhang): handle duckable and uncontrollable. |
+ // See https://crbug.com/639277. |
+ AudioFocusManager::AudioFocusType required_audio_focus_type; |
+ if (media_content_type == media::MediaContentType::Persistent) { |
+ required_audio_focus_type = AudioFocusManager::AudioFocusType::Gain; |
+ } else { |
+ required_audio_focus_type = |
+ AudioFocusManager::AudioFocusType::GainTransientMayDuck; |
+ } |
+ |
// If the audio focus is already granted and is of type Content, there is |
// nothing to do. If it is granted of type Transient the requested type is |
// also transient, there is also nothing to do. Otherwise, the session needs |
// to request audio focus again. |
if (audio_focus_state_ == State::ACTIVE && |
- (audio_focus_type_ == Type::Content || audio_focus_type_ == type)) { |
+ (audio_focus_type_ == AudioFocusManager::AudioFocusType::Gain || |
+ audio_focus_type_ == required_audio_focus_type)) { |
players_.insert(PlayerIdentifier(observer, player_id)); |
return true; |
} |
State old_audio_focus_state = audio_focus_state_; |
- State audio_focus_state = RequestSystemAudioFocus(type) ? State::ACTIVE |
- : State::INACTIVE; |
+ State audio_focus_state = RequestSystemAudioFocus(required_audio_focus_type) |
+ ? State::ACTIVE |
+ : State::INACTIVE; |
SetAudioFocusState(audio_focus_state); |
- audio_focus_type_ = type; |
+ audio_focus_type_ = required_audio_focus_type; |
if (audio_focus_state_ != State::ACTIVE) |
return false; |
@@ -147,12 +161,12 @@ void MediaSession::OnPlayerPaused(MediaSessionObserver* observer, |
OnSuspendInternal(SuspendType::CONTENT, State::SUSPENDED); |
} |
-void MediaSession::Resume(SuspendType type) { |
+void MediaSession::Resume(SuspendType suspend_type) { |
DCHECK(IsReallySuspended()); |
// When the resume requests comes from another source than system, audio focus |
// must be requested. |
- if (type != SuspendType::SYSTEM) { |
+ if (suspend_type != SuspendType::SYSTEM) { |
// Request audio focus again in case we lost it because another app started |
// playing while the playback was paused. |
State audio_focus_state = RequestSystemAudioFocus(audio_focus_type_) |
@@ -164,28 +178,28 @@ void MediaSession::Resume(SuspendType type) { |
return; |
} |
- OnResumeInternal(type); |
+ OnResumeInternal(suspend_type); |
} |
-void MediaSession::Suspend(SuspendType type) { |
+void MediaSession::Suspend(SuspendType suspend_type) { |
DCHECK(!IsSuspended()); |
- OnSuspendInternal(type, State::SUSPENDED); |
+ OnSuspendInternal(suspend_type, State::SUSPENDED); |
} |
-void MediaSession::Stop(SuspendType type) { |
+void MediaSession::Stop(SuspendType suspend_type) { |
DCHECK(audio_focus_state_ != State::INACTIVE); |
- DCHECK(type != SuspendType::CONTENT); |
+ DCHECK(suspend_type != SuspendType::CONTENT); |
// TODO(mlamouri): merge the logic between UI and SYSTEM. |
- if (type == SuspendType::SYSTEM) { |
- OnSuspendInternal(type, State::INACTIVE); |
+ if (suspend_type == SuspendType::SYSTEM) { |
+ OnSuspendInternal(suspend_type, State::INACTIVE); |
return; |
} |
if (audio_focus_state_ != State::SUSPENDED) |
- OnSuspendInternal(type, State::SUSPENDED); |
+ OnSuspendInternal(suspend_type, State::SUSPENDED); |
DCHECK(audio_focus_state_ == State::SUSPENDED); |
players_.clear(); |
@@ -212,9 +226,10 @@ bool MediaSession::IsSuspended() const { |
} |
bool MediaSession::IsControllable() const { |
- // Only content type media session can be controllable unless it is inactive. |
+ // Only media session having focus Gain can be controllable unless it is |
+ // inactive. |
return audio_focus_state_ != State::INACTIVE && |
- audio_focus_type_ == Type::Content; |
+ audio_focus_type_ == AudioFocusManager::AudioFocusType::Gain; |
} |
std::unique_ptr<base::CallbackList<void(MediaSession::State)>::Subscription> |
@@ -232,7 +247,8 @@ bool MediaSession::IsActiveForTest() const { |
return audio_focus_state_ == State::ACTIVE; |
} |
-MediaSession::Type MediaSession::audio_focus_type_for_test() const { |
+AudioFocusManager::AudioFocusType MediaSession::audio_focus_type_for_test() |
+ const { |
return audio_focus_type_; |
} |
@@ -245,15 +261,16 @@ void MediaSession::RemoveAllPlayersForTest() { |
AbandonSystemAudioFocusIfNeeded(); |
} |
-void MediaSession::OnSuspendInternal(SuspendType type, State new_state) { |
+void MediaSession::OnSuspendInternal(SuspendType suspend_type, |
+ State new_state) { |
DCHECK(new_state == State::SUSPENDED || new_state == State::INACTIVE); |
// UI suspend cannot use State::INACTIVE. |
- DCHECK(type == SuspendType::SYSTEM || new_state == State::SUSPENDED); |
+ DCHECK(suspend_type == SuspendType::SYSTEM || new_state == State::SUSPENDED); |
if (audio_focus_state_ != State::ACTIVE) |
return; |
- switch (type) { |
+ switch (suspend_type) { |
case SuspendType::UI: |
uma_helper_.RecordSessionSuspended(MediaSessionSuspendedSource::UI); |
break; |
@@ -278,9 +295,9 @@ void MediaSession::OnSuspendInternal(SuspendType type, State new_state) { |
} |
SetAudioFocusState(new_state); |
- suspend_type_ = type; |
+ suspend_type_ = suspend_type; |
- if (type != SuspendType::CONTENT) { |
+ if (suspend_type != SuspendType::CONTENT) { |
// SuspendType::CONTENT happens when the suspend action came from |
// the page in which case the player is already paused. |
// Otherwise, the players need to be paused. |
@@ -291,8 +308,8 @@ void MediaSession::OnSuspendInternal(SuspendType type, State new_state) { |
UpdateWebContents(); |
} |
-void MediaSession::OnResumeInternal(SuspendType type) { |
- if (type == SuspendType::SYSTEM && suspend_type_ != type) |
+void MediaSession::OnResumeInternal(SuspendType suspend_type) { |
+ if (suspend_type == SuspendType::SYSTEM && suspend_type_ != suspend_type) |
return; |
SetAudioFocusState(State::ACTIVE); |
@@ -306,16 +323,17 @@ void MediaSession::OnResumeInternal(SuspendType type) { |
MediaSession::MediaSession(WebContents* web_contents) |
: WebContentsObserver(web_contents), |
audio_focus_state_(State::INACTIVE), |
- audio_focus_type_(Type::Transient), |
- volume_multiplier_(kDefaultVolumeMultiplier) { |
-} |
+ audio_focus_type_( |
+ AudioFocusManager::AudioFocusType::GainTransientMayDuck), |
+ volume_multiplier_(kDefaultVolumeMultiplier) {} |
void MediaSession::Initialize() { |
delegate_ = MediaSessionDelegate::Create(this); |
} |
-bool MediaSession::RequestSystemAudioFocus(Type type) { |
- bool result = delegate_->RequestAudioFocus(type); |
+bool MediaSession::RequestSystemAudioFocus( |
+ AudioFocusManager::AudioFocusType audio_focus_type) { |
+ bool result = delegate_->RequestAudioFocus(audio_focus_type); |
uma_helper_.RecordRequestAudioFocusResult(result); |
return result; |
} |