| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/session/media_session.h" | 5 #include "content/browser/media/session/media_session_impl.h" |
| 6 | 6 |
| 7 #include "content/browser/media/session/audio_focus_delegate.h" | 7 #include "content/browser/media/session/audio_focus_delegate.h" |
| 8 #include "content/browser/media/session/media_session_player_observer.h" | 8 #include "content/browser/media/session/media_session_player_observer.h" |
| 9 #include "content/browser/web_contents/web_contents_impl.h" | 9 #include "content/browser/web_contents/web_contents_impl.h" |
| 10 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
| 11 #include "content/public/browser/web_contents_delegate.h" | 11 #include "content/public/browser/web_contents_delegate.h" |
| 12 #include "media/base/media_content_type.h" | 12 #include "media/base/media_content_type.h" |
| 13 | 13 |
| 14 #if defined(OS_ANDROID) |
| 15 #include "content/browser/media/session/media_session_android.h" |
| 16 #include "jni/MediaSession_jni.h" |
| 17 #endif // defined(OS_ANDROID) |
| 18 |
| 14 namespace content { | 19 namespace content { |
| 15 | 20 |
| 16 namespace { | 21 namespace { |
| 17 | 22 |
| 18 const double kDefaultVolumeMultiplier = 1.0; | 23 const double kDefaultVolumeMultiplier = 1.0; |
| 19 const double kDuckingVolumeMultiplier = 0.2; | 24 const double kDuckingVolumeMultiplier = 0.2; |
| 20 | 25 |
| 21 } // anonymous namespace | 26 } // anonymous namespace |
| 22 | 27 |
| 23 using MediaSessionSuspendedSource = | 28 using MediaSessionSuspendedSource = |
| 24 MediaSessionUmaHelper::MediaSessionSuspendedSource; | 29 MediaSessionUmaHelper::MediaSessionSuspendedSource; |
| 25 | 30 |
| 26 DEFINE_WEB_CONTENTS_USER_DATA_KEY(MediaSession); | 31 DEFINE_WEB_CONTENTS_USER_DATA_KEY(MediaSessionImpl); |
| 27 | 32 |
| 28 MediaSession::PlayerIdentifier::PlayerIdentifier( | 33 MediaSessionImpl::PlayerIdentifier::PlayerIdentifier( |
| 29 MediaSessionPlayerObserver* observer, | 34 MediaSessionPlayerObserver* observer, |
| 30 int player_id) | 35 int player_id) |
| 31 : observer(observer), player_id(player_id) {} | 36 : observer(observer), player_id(player_id) {} |
| 32 | 37 |
| 33 bool MediaSession::PlayerIdentifier::operator==( | 38 bool MediaSessionImpl::PlayerIdentifier::operator==( |
| 34 const PlayerIdentifier& other) const { | 39 const PlayerIdentifier& other) const { |
| 35 return this->observer == other.observer && this->player_id == other.player_id; | 40 return this->observer == other.observer && this->player_id == other.player_id; |
| 36 } | 41 } |
| 37 | 42 |
| 38 size_t MediaSession::PlayerIdentifier::Hash::operator()( | 43 size_t MediaSessionImpl::PlayerIdentifier::Hash::operator()( |
| 39 const PlayerIdentifier& player_identifier) const { | 44 const PlayerIdentifier& player_identifier) const { |
| 40 size_t hash = BASE_HASH_NAMESPACE::hash<MediaSessionPlayerObserver*>()( | 45 size_t hash = BASE_HASH_NAMESPACE::hash<MediaSessionPlayerObserver*>()( |
| 41 player_identifier.observer); | 46 player_identifier.observer); |
| 42 hash += BASE_HASH_NAMESPACE::hash<int>()(player_identifier.player_id); | 47 hash += BASE_HASH_NAMESPACE::hash<int>()(player_identifier.player_id); |
| 43 return hash; | 48 return hash; |
| 44 } | 49 } |
| 45 | 50 |
| 46 // static | 51 // static |
| 47 MediaSession* MediaSession::Get(WebContents* web_contents) { | 52 MediaSession* MediaSession::Get(WebContents* web_contents) { |
| 48 MediaSession* session = FromWebContents(web_contents); | 53 return MediaSessionImpl::Get(web_contents); |
| 54 } |
| 55 |
| 56 // static |
| 57 MediaSessionImpl* MediaSessionImpl::Get(WebContents* web_contents) { |
| 58 MediaSessionImpl* session = FromWebContents(web_contents); |
| 49 if (!session) { | 59 if (!session) { |
| 50 CreateForWebContents(web_contents); | 60 CreateForWebContents(web_contents); |
| 51 session = FromWebContents(web_contents); | 61 session = FromWebContents(web_contents); |
| 52 session->Initialize(); | 62 session->Initialize(); |
| 53 } | 63 } |
| 54 return session; | 64 return session; |
| 55 } | 65 } |
| 56 | 66 |
| 57 MediaSession::~MediaSession() { | 67 #if defined(OS_ANDROID) |
| 68 bool RegisterMediaSessionNatives(JNIEnv* env) { |
| 69 return RegisterNativesImpl(env); |
| 70 } |
| 71 |
| 72 // static |
| 73 base::android::ScopedJavaLocalRef<jobject> GetMediaSessionFromWebContents( |
| 74 JNIEnv* env, |
| 75 const base::android::JavaParamRef<jclass>& clazz, |
| 76 const base::android::JavaParamRef<jobject>& j_contents_android) { |
| 77 WebContents* contents = WebContents::FromJavaWebContents(j_contents_android); |
| 78 DCHECK(contents); |
| 79 MediaSessionImpl* session = MediaSessionImpl::Get(contents); |
| 80 return session->session_android()->GetJavaObject(); |
| 81 } |
| 82 |
| 83 #endif // defined(OS_ANDROID) |
| 84 |
| 85 MediaSessionImpl::~MediaSessionImpl() { |
| 58 DCHECK(players_.empty()); | 86 DCHECK(players_.empty()); |
| 59 DCHECK(audio_focus_state_ == State::INACTIVE); | 87 DCHECK(audio_focus_state_ == State::INACTIVE); |
| 88 for (auto& observer : observers_) |
| 89 observer.MediaSessionDestroyed(); |
| 60 } | 90 } |
| 61 | 91 |
| 62 void MediaSession::WebContentsDestroyed() { | 92 void MediaSessionImpl::WebContentsDestroyed() { |
| 63 // This should only work for tests. In production, all the players should have | 93 // This should only work for tests. In production, all the players should have |
| 64 // already been removed before WebContents is destroyed. | 94 // already been removed before WebContents is destroyed. |
| 65 | 95 |
| 66 // TODO(zqzhang): refactor MediaSession, maybe move the interface used to talk | 96 // TODO(zqzhang): refactor MediaSessionImpl, maybe move the interface used to |
| 97 // talk |
| 67 // with AudioFocusManager out to a seperate class. The AudioFocusManager unit | 98 // with AudioFocusManager out to a seperate class. The AudioFocusManager unit |
| 68 // tests then could mock the interface and abandon audio focus when | 99 // tests then could mock the interface and abandon audio focus when |
| 69 // WebContents is destroyed. See https://crbug.com/651069 | 100 // WebContents is destroyed. See https://crbug.com/651069 |
| 70 players_.clear(); | 101 players_.clear(); |
| 71 pepper_players_.clear(); | 102 pepper_players_.clear(); |
| 72 AbandonSystemAudioFocusIfNeeded(); | 103 AbandonSystemAudioFocusIfNeeded(); |
| 73 } | 104 } |
| 74 | 105 |
| 75 void MediaSession::SetMetadata(const base::Optional<MediaMetadata>& metadata) { | 106 void MediaSessionImpl::AddObserver(MediaSessionObserver* observer) { |
| 76 metadata_ = metadata; | 107 observers_.AddObserver(observer); |
| 77 static_cast<WebContentsImpl*>(web_contents()) | |
| 78 ->OnMediaSessionMetadataChanged(); | |
| 79 } | 108 } |
| 80 | 109 |
| 81 bool MediaSession::AddPlayer(MediaSessionPlayerObserver* observer, | 110 void MediaSessionImpl::RemoveObserver(MediaSessionObserver* observer) { |
| 82 int player_id, | 111 observers_.RemoveObserver(observer); |
| 83 media::MediaContentType media_content_type) { | 112 } |
| 113 |
| 114 void MediaSessionImpl::SetMetadata( |
| 115 const base::Optional<MediaMetadata>& metadata) { |
| 116 metadata_ = metadata; |
| 117 for (auto& observer : observers_) |
| 118 observer.MediaSessionMetadataChanged(metadata); |
| 119 } |
| 120 |
| 121 bool MediaSessionImpl::AddPlayer(MediaSessionPlayerObserver* observer, |
| 122 int player_id, |
| 123 media::MediaContentType media_content_type) { |
| 84 if (media_content_type == media::MediaContentType::Uncontrollable) | 124 if (media_content_type == media::MediaContentType::Uncontrollable) |
| 85 return true; | 125 return true; |
| 86 if (media_content_type == media::MediaContentType::Pepper) | 126 if (media_content_type == media::MediaContentType::Pepper) |
| 87 return AddPepperPlayer(observer, player_id); | 127 return AddPepperPlayer(observer, player_id); |
| 88 | 128 |
| 89 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); | 129 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); |
| 90 | 130 |
| 91 // Determine the audio focus type required for playing the new player. | 131 // Determine the audio focus type required for playing the new player. |
| 92 // TODO(zqzhang): handle duckable and uncontrollable. | 132 // TODO(zqzhang): handle duckable and uncontrollable. |
| 93 // See https://crbug.com/639277. | 133 // See https://crbug.com/639277. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 120 // suspended. | 160 // suspended. |
| 121 if (old_audio_focus_state != State::ACTIVE) | 161 if (old_audio_focus_state != State::ACTIVE) |
| 122 players_.clear(); | 162 players_.clear(); |
| 123 | 163 |
| 124 players_.insert(PlayerIdentifier(observer, player_id)); | 164 players_.insert(PlayerIdentifier(observer, player_id)); |
| 125 UpdateWebContents(); | 165 UpdateWebContents(); |
| 126 | 166 |
| 127 return true; | 167 return true; |
| 128 } | 168 } |
| 129 | 169 |
| 130 void MediaSession::RemovePlayer(MediaSessionPlayerObserver* observer, | 170 void MediaSessionImpl::RemovePlayer(MediaSessionPlayerObserver* observer, |
| 131 int player_id) { | 171 int player_id) { |
| 132 auto it = players_.find(PlayerIdentifier(observer, player_id)); | 172 auto it = players_.find(PlayerIdentifier(observer, player_id)); |
| 133 if (it != players_.end()) | 173 if (it != players_.end()) |
| 134 players_.erase(it); | 174 players_.erase(it); |
| 135 | 175 |
| 136 it = pepper_players_.find(PlayerIdentifier(observer, player_id)); | 176 it = pepper_players_.find(PlayerIdentifier(observer, player_id)); |
| 137 if (it != pepper_players_.end()) | 177 if (it != pepper_players_.end()) |
| 138 pepper_players_.erase(it); | 178 pepper_players_.erase(it); |
| 139 | 179 |
| 140 AbandonSystemAudioFocusIfNeeded(); | 180 AbandonSystemAudioFocusIfNeeded(); |
| 141 } | 181 } |
| 142 | 182 |
| 143 void MediaSession::RemovePlayers(MediaSessionPlayerObserver* observer) { | 183 void MediaSessionImpl::RemovePlayers(MediaSessionPlayerObserver* observer) { |
| 144 for (auto it = players_.begin(); it != players_.end(); ) { | 184 for (auto it = players_.begin(); it != players_.end();) { |
| 145 if (it->observer == observer) | 185 if (it->observer == observer) |
| 146 players_.erase(it++); | 186 players_.erase(it++); |
| 147 else | 187 else |
| 148 ++it; | 188 ++it; |
| 149 } | 189 } |
| 150 | 190 |
| 151 for (auto it = pepper_players_.begin(); it != pepper_players_.end(); ) { | 191 for (auto it = pepper_players_.begin(); it != pepper_players_.end();) { |
| 152 if (it->observer == observer) | 192 if (it->observer == observer) |
| 153 pepper_players_.erase(it++); | 193 pepper_players_.erase(it++); |
| 154 else | 194 else |
| 155 ++it; | 195 ++it; |
| 156 } | 196 } |
| 157 | 197 |
| 158 AbandonSystemAudioFocusIfNeeded(); | 198 AbandonSystemAudioFocusIfNeeded(); |
| 159 } | 199 } |
| 160 | 200 |
| 161 void MediaSession::RecordSessionDuck() { | 201 void MediaSessionImpl::RecordSessionDuck() { |
| 162 uma_helper_.RecordSessionSuspended( | 202 uma_helper_.RecordSessionSuspended( |
| 163 MediaSessionSuspendedSource::SystemTransientDuck); | 203 MediaSessionSuspendedSource::SystemTransientDuck); |
| 164 } | 204 } |
| 165 | 205 |
| 166 void MediaSession::OnPlayerPaused(MediaSessionPlayerObserver* observer, | 206 void MediaSessionImpl::OnPlayerPaused(MediaSessionPlayerObserver* observer, |
| 167 int player_id) { | 207 int player_id) { |
| 168 // If a playback is completed, BrowserMediaPlayerManager will call | 208 // If a playback is completed, BrowserMediaPlayerManager will call |
| 169 // OnPlayerPaused() after RemovePlayer(). This is a workaround. | 209 // OnPlayerPaused() after RemovePlayer(). This is a workaround. |
| 170 // Also, this method may be called when a player that is not added | 210 // Also, this method may be called when a player that is not added |
| 171 // to this session (e.g. a silent video) is paused. MediaSession | 211 // to this session (e.g. a silent video) is paused. MediaSessionImpl |
| 172 // should ignore the paused player for this case. | 212 // should ignore the paused player for this case. |
| 173 if (!players_.count(PlayerIdentifier(observer, player_id)) && | 213 if (!players_.count(PlayerIdentifier(observer, player_id)) && |
| 174 !pepper_players_.count(PlayerIdentifier(observer, player_id))) { | 214 !pepper_players_.count(PlayerIdentifier(observer, player_id))) { |
| 175 return; | 215 return; |
| 176 } | 216 } |
| 177 | 217 |
| 178 // If the player to be removed is a pepper player, or there is more than one | 218 // If the player to be removed is a pepper player, or there is more than one |
| 179 // observer, remove the paused one from the session. | 219 // observer, remove the paused one from the session. |
| 180 if (pepper_players_.count(PlayerIdentifier(observer, player_id)) || | 220 if (pepper_players_.count(PlayerIdentifier(observer, player_id)) || |
| 181 players_.size() != 1) { | 221 players_.size() != 1) { |
| 182 RemovePlayer(observer, player_id); | 222 RemovePlayer(observer, player_id); |
| 183 return; | 223 return; |
| 184 } | 224 } |
| 185 | 225 |
| 186 // Otherwise, suspend the session. | 226 // Otherwise, suspend the session. |
| 187 DCHECK(!IsSuspended()); | 227 DCHECK(!IsSuspended()); |
| 188 OnSuspendInternal(SuspendType::CONTENT, State::SUSPENDED); | 228 OnSuspendInternal(SuspendType::CONTENT, State::SUSPENDED); |
| 189 } | 229 } |
| 190 | 230 |
| 191 void MediaSession::Resume(SuspendType suspend_type) { | 231 void MediaSessionImpl::Resume(SuspendType suspend_type) { |
| 192 DCHECK(IsReallySuspended()); | 232 DCHECK(IsReallySuspended()); |
| 193 | 233 |
| 194 // When the resume requests comes from another source than system, audio focus | 234 // When the resume requests comes from another source than system, audio focus |
| 195 // must be requested. | 235 // must be requested. |
| 196 if (suspend_type != SuspendType::SYSTEM) { | 236 if (suspend_type != SuspendType::SYSTEM) { |
| 197 // Request audio focus again in case we lost it because another app started | 237 // Request audio focus again in case we lost it because another app started |
| 198 // playing while the playback was paused. | 238 // playing while the playback was paused. |
| 199 State audio_focus_state = RequestSystemAudioFocus(audio_focus_type_) | 239 State audio_focus_state = RequestSystemAudioFocus(audio_focus_type_) |
| 200 ? State::ACTIVE | 240 ? State::ACTIVE |
| 201 : State::INACTIVE; | 241 : State::INACTIVE; |
| 202 SetAudioFocusState(audio_focus_state); | 242 SetAudioFocusState(audio_focus_state); |
| 203 | 243 |
| 204 if (audio_focus_state_ != State::ACTIVE) | 244 if (audio_focus_state_ != State::ACTIVE) |
| 205 return; | 245 return; |
| 206 } | 246 } |
| 207 | 247 |
| 208 OnResumeInternal(suspend_type); | 248 OnResumeInternal(suspend_type); |
| 209 } | 249 } |
| 210 | 250 |
| 211 void MediaSession::Suspend(SuspendType suspend_type) { | 251 void MediaSessionImpl::Suspend(SuspendType suspend_type) { |
| 212 DCHECK(!IsSuspended()); | 252 DCHECK(!IsSuspended()); |
| 213 | 253 |
| 214 OnSuspendInternal(suspend_type, State::SUSPENDED); | 254 OnSuspendInternal(suspend_type, State::SUSPENDED); |
| 215 } | 255 } |
| 216 | 256 |
| 217 void MediaSession::Stop(SuspendType suspend_type) { | 257 void MediaSessionImpl::Stop(SuspendType suspend_type) { |
| 218 DCHECK(audio_focus_state_ != State::INACTIVE); | 258 DCHECK(audio_focus_state_ != State::INACTIVE); |
| 219 DCHECK(suspend_type != SuspendType::CONTENT); | 259 DCHECK(suspend_type != SuspendType::CONTENT); |
| 220 DCHECK(!HasPepper()); | 260 DCHECK(!HasPepper()); |
| 221 | 261 |
| 222 // TODO(mlamouri): merge the logic between UI and SYSTEM. | 262 // TODO(mlamouri): merge the logic between UI and SYSTEM. |
| 223 if (suspend_type == SuspendType::SYSTEM) { | 263 if (suspend_type == SuspendType::SYSTEM) { |
| 224 OnSuspendInternal(suspend_type, State::INACTIVE); | 264 OnSuspendInternal(suspend_type, State::INACTIVE); |
| 225 return; | 265 return; |
| 226 } | 266 } |
| 227 | 267 |
| 228 if (audio_focus_state_ != State::SUSPENDED) | 268 if (audio_focus_state_ != State::SUSPENDED) |
| 229 OnSuspendInternal(suspend_type, State::SUSPENDED); | 269 OnSuspendInternal(suspend_type, State::SUSPENDED); |
| 230 | 270 |
| 231 DCHECK(audio_focus_state_ == State::SUSPENDED); | 271 DCHECK(audio_focus_state_ == State::SUSPENDED); |
| 232 players_.clear(); | 272 players_.clear(); |
| 233 | 273 |
| 234 AbandonSystemAudioFocusIfNeeded(); | 274 AbandonSystemAudioFocusIfNeeded(); |
| 235 } | 275 } |
| 236 | 276 |
| 237 void MediaSession::StartDucking() { | 277 void MediaSessionImpl::StartDucking() { |
| 238 if (is_ducking_) | 278 if (is_ducking_) |
| 239 return; | 279 return; |
| 240 is_ducking_ = true; | 280 is_ducking_ = true; |
| 241 UpdateVolumeMultiplier(); | 281 UpdateVolumeMultiplier(); |
| 242 } | 282 } |
| 243 | 283 |
| 244 void MediaSession::StopDucking() { | 284 void MediaSessionImpl::StopDucking() { |
| 245 if (!is_ducking_) | 285 if (!is_ducking_) |
| 246 return; | 286 return; |
| 247 is_ducking_ = false; | 287 is_ducking_ = false; |
| 248 UpdateVolumeMultiplier(); | 288 UpdateVolumeMultiplier(); |
| 249 } | 289 } |
| 250 | 290 |
| 251 void MediaSession::UpdateVolumeMultiplier() { | 291 void MediaSessionImpl::UpdateVolumeMultiplier() { |
| 252 for (const auto& it : players_) | 292 for (const auto& it : players_) |
| 253 it.observer->OnSetVolumeMultiplier(it.player_id, GetVolumeMultiplier()); | 293 it.observer->OnSetVolumeMultiplier(it.player_id, GetVolumeMultiplier()); |
| 254 for (const auto& it : pepper_players_) | 294 for (const auto& it : pepper_players_) |
| 255 it.observer->OnSetVolumeMultiplier(it.player_id, GetVolumeMultiplier()); | 295 it.observer->OnSetVolumeMultiplier(it.player_id, GetVolumeMultiplier()); |
| 256 } | 296 } |
| 257 | 297 |
| 258 double MediaSession::GetVolumeMultiplier() const { | 298 double MediaSessionImpl::GetVolumeMultiplier() const { |
| 259 return is_ducking_ ? kDuckingVolumeMultiplier : kDefaultVolumeMultiplier; | 299 return is_ducking_ ? kDuckingVolumeMultiplier : kDefaultVolumeMultiplier; |
| 260 } | 300 } |
| 261 | 301 |
| 262 bool MediaSession::IsActive() const { | 302 bool MediaSessionImpl::IsActive() const { |
| 263 return audio_focus_state_ == State::ACTIVE; | 303 return audio_focus_state_ == State::ACTIVE; |
| 264 } | 304 } |
| 265 | 305 |
| 266 bool MediaSession::IsReallySuspended() const { | 306 bool MediaSessionImpl::IsReallySuspended() const { |
| 267 return audio_focus_state_ == State::SUSPENDED; | 307 return audio_focus_state_ == State::SUSPENDED; |
| 268 } | 308 } |
| 269 | 309 |
| 270 bool MediaSession::IsSuspended() const { | 310 bool MediaSessionImpl::IsSuspended() const { |
| 271 // TODO(mlamouri): should be == State::SUSPENDED. | 311 // TODO(mlamouri): should be == State::SUSPENDED. |
| 272 return audio_focus_state_ != State::ACTIVE; | 312 return audio_focus_state_ != State::ACTIVE; |
| 273 } | 313 } |
| 274 | 314 |
| 275 bool MediaSession::IsControllable() const { | 315 bool MediaSessionImpl::IsControllable() const { |
| 276 // Only media session having focus Gain can be controllable unless it is | 316 // Only media session having focus Gain can be controllable unless it is |
| 277 // inactive. | 317 // inactive. |
| 278 return audio_focus_state_ != State::INACTIVE && | 318 return audio_focus_state_ != State::INACTIVE && |
| 279 audio_focus_type_ == AudioFocusManager::AudioFocusType::Gain; | 319 audio_focus_type_ == AudioFocusManager::AudioFocusType::Gain; |
| 280 } | 320 } |
| 281 | 321 |
| 282 bool MediaSession::HasPepper() const { | 322 bool MediaSessionImpl::HasPepper() const { |
| 283 return !pepper_players_.empty(); | 323 return !pepper_players_.empty(); |
| 284 } | 324 } |
| 285 | 325 |
| 286 std::unique_ptr<base::CallbackList<void(MediaSession::State)>::Subscription> | 326 std::unique_ptr<base::CallbackList<void(MediaSessionImpl::State)>::Subscription> |
| 287 MediaSession::RegisterMediaSessionStateChangedCallbackForTest( | 327 MediaSessionImpl::RegisterMediaSessionStateChangedCallbackForTest( |
| 288 const StateChangedCallback& cb) { | 328 const StateChangedCallback& cb) { |
| 289 return media_session_state_listeners_.Add(cb); | 329 return media_session_state_listeners_.Add(cb); |
| 290 } | 330 } |
| 291 | 331 |
| 292 void MediaSession::SetDelegateForTests( | 332 void MediaSessionImpl::SetDelegateForTests( |
| 293 std::unique_ptr<AudioFocusDelegate> delegate) { | 333 std::unique_ptr<AudioFocusDelegate> delegate) { |
| 294 delegate_ = std::move(delegate); | 334 delegate_ = std::move(delegate); |
| 295 } | 335 } |
| 296 | 336 |
| 297 bool MediaSession::IsActiveForTest() const { | 337 bool MediaSessionImpl::IsActiveForTest() const { |
| 298 return audio_focus_state_ == State::ACTIVE; | 338 return audio_focus_state_ == State::ACTIVE; |
| 299 } | 339 } |
| 300 | 340 |
| 301 MediaSessionUmaHelper* MediaSession::uma_helper_for_test() { | 341 MediaSessionUmaHelper* MediaSessionImpl::uma_helper_for_test() { |
| 302 return &uma_helper_; | 342 return &uma_helper_; |
| 303 } | 343 } |
| 304 | 344 |
| 305 void MediaSession::RemoveAllPlayersForTest() { | 345 void MediaSessionImpl::RemoveAllPlayersForTest() { |
| 306 players_.clear(); | 346 players_.clear(); |
| 307 AbandonSystemAudioFocusIfNeeded(); | 347 AbandonSystemAudioFocusIfNeeded(); |
| 308 } | 348 } |
| 309 | 349 |
| 310 void MediaSession::OnSuspendInternal(SuspendType suspend_type, | 350 void MediaSessionImpl::OnSuspendInternal(SuspendType suspend_type, |
| 311 State new_state) { | 351 State new_state) { |
| 312 DCHECK(!HasPepper()); | 352 DCHECK(!HasPepper()); |
| 313 | 353 |
| 314 DCHECK(new_state == State::SUSPENDED || new_state == State::INACTIVE); | 354 DCHECK(new_state == State::SUSPENDED || new_state == State::INACTIVE); |
| 315 // UI suspend cannot use State::INACTIVE. | 355 // UI suspend cannot use State::INACTIVE. |
| 316 DCHECK(suspend_type == SuspendType::SYSTEM || new_state == State::SUSPENDED); | 356 DCHECK(suspend_type == SuspendType::SYSTEM || new_state == State::SUSPENDED); |
| 317 | 357 |
| 318 if (audio_focus_state_ != State::ACTIVE) | 358 if (audio_focus_state_ != State::ACTIVE) |
| 319 return; | 359 return; |
| 320 | 360 |
| 321 switch (suspend_type) { | 361 switch (suspend_type) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 352 for (const auto& it : players_) | 392 for (const auto& it : players_) |
| 353 it.observer->OnSuspend(it.player_id); | 393 it.observer->OnSuspend(it.player_id); |
| 354 } | 394 } |
| 355 | 395 |
| 356 for (const auto& it : pepper_players_) | 396 for (const auto& it : pepper_players_) |
| 357 it.observer->OnSetVolumeMultiplier(it.player_id, kDuckingVolumeMultiplier); | 397 it.observer->OnSetVolumeMultiplier(it.player_id, kDuckingVolumeMultiplier); |
| 358 | 398 |
| 359 UpdateWebContents(); | 399 UpdateWebContents(); |
| 360 } | 400 } |
| 361 | 401 |
| 362 void MediaSession::OnResumeInternal(SuspendType suspend_type) { | 402 void MediaSessionImpl::OnResumeInternal(SuspendType suspend_type) { |
| 363 if (suspend_type == SuspendType::SYSTEM && suspend_type_ != suspend_type) | 403 if (suspend_type == SuspendType::SYSTEM && suspend_type_ != suspend_type) |
| 364 return; | 404 return; |
| 365 | 405 |
| 366 SetAudioFocusState(State::ACTIVE); | 406 SetAudioFocusState(State::ACTIVE); |
| 367 | 407 |
| 368 for (const auto& it : players_) | 408 for (const auto& it : players_) |
| 369 it.observer->OnResume(it.player_id); | 409 it.observer->OnResume(it.player_id); |
| 370 | 410 |
| 371 for (const auto& it : pepper_players_) | 411 for (const auto& it : pepper_players_) |
| 372 it.observer->OnSetVolumeMultiplier(it.player_id, GetVolumeMultiplier()); | 412 it.observer->OnSetVolumeMultiplier(it.player_id, GetVolumeMultiplier()); |
| 373 | 413 |
| 374 UpdateWebContents(); | 414 UpdateWebContents(); |
| 375 } | 415 } |
| 376 | 416 |
| 377 MediaSession::MediaSession(WebContents* web_contents) | 417 MediaSessionImpl::MediaSessionImpl(WebContents* web_contents) |
| 378 : WebContentsObserver(web_contents), | 418 : WebContentsObserver(web_contents), |
| 379 audio_focus_state_(State::INACTIVE), | 419 audio_focus_state_(State::INACTIVE), |
| 380 audio_focus_type_( | 420 audio_focus_type_( |
| 381 AudioFocusManager::AudioFocusType::GainTransientMayDuck), | 421 AudioFocusManager::AudioFocusType::GainTransientMayDuck), |
| 382 is_ducking_(false) {} | 422 is_ducking_(false) { |
| 423 #if defined(OS_ANDROID) |
| 424 session_android_.reset(new MediaSessionAndroid(this)); |
| 425 #endif // defined(OS_ANDROID) |
| 426 } |
| 383 | 427 |
| 384 void MediaSession::Initialize() { | 428 void MediaSessionImpl::Initialize() { |
| 385 delegate_ = AudioFocusDelegate::Create(this); | 429 delegate_ = AudioFocusDelegate::Create(this); |
| 386 } | 430 } |
| 387 | 431 |
| 388 bool MediaSession::RequestSystemAudioFocus( | 432 bool MediaSessionImpl::RequestSystemAudioFocus( |
| 389 AudioFocusManager::AudioFocusType audio_focus_type) { | 433 AudioFocusManager::AudioFocusType audio_focus_type) { |
| 390 bool result = delegate_->RequestAudioFocus(audio_focus_type); | 434 bool result = delegate_->RequestAudioFocus(audio_focus_type); |
| 391 uma_helper_.RecordRequestAudioFocusResult(result); | 435 uma_helper_.RecordRequestAudioFocusResult(result); |
| 392 | 436 |
| 393 // MediaSession must change its state & audio focus type AFTER requesting | 437 // MediaSessionImpl must change its state & audio focus type AFTER requesting |
| 394 // audio focus. | 438 // audio focus. |
| 395 SetAudioFocusState(result ? State::ACTIVE : State::INACTIVE); | 439 SetAudioFocusState(result ? State::ACTIVE : State::INACTIVE); |
| 396 audio_focus_type_ = audio_focus_type; | 440 audio_focus_type_ = audio_focus_type; |
| 397 return result; | 441 return result; |
| 398 } | 442 } |
| 399 | 443 |
| 400 void MediaSession::AbandonSystemAudioFocusIfNeeded() { | 444 void MediaSessionImpl::AbandonSystemAudioFocusIfNeeded() { |
| 401 if (audio_focus_state_ == State::INACTIVE || !players_.empty() || | 445 if (audio_focus_state_ == State::INACTIVE || !players_.empty() || |
| 402 !pepper_players_.empty()) { | 446 !pepper_players_.empty()) { |
| 403 return; | 447 return; |
| 404 } | 448 } |
| 405 delegate_->AbandonAudioFocus(); | 449 delegate_->AbandonAudioFocus(); |
| 406 | 450 |
| 407 SetAudioFocusState(State::INACTIVE); | 451 SetAudioFocusState(State::INACTIVE); |
| 408 UpdateWebContents(); | 452 UpdateWebContents(); |
| 409 } | 453 } |
| 410 | 454 |
| 411 void MediaSession::UpdateWebContents() { | 455 void MediaSessionImpl::UpdateWebContents() { |
| 412 media_session_state_listeners_.Notify(audio_focus_state_); | 456 media_session_state_listeners_.Notify(audio_focus_state_); |
| 413 static_cast<WebContentsImpl*>(web_contents())->OnMediaSessionStateChanged(); | 457 for (auto& observer : observers_) |
| 458 observer.MediaSessionStateChanged(IsControllable(), IsSuspended()); |
| 414 } | 459 } |
| 415 | 460 |
| 416 void MediaSession::SetAudioFocusState(State audio_focus_state) { | 461 void MediaSessionImpl::SetAudioFocusState(State audio_focus_state) { |
| 417 if (audio_focus_state == audio_focus_state_) | 462 if (audio_focus_state == audio_focus_state_) |
| 418 return; | 463 return; |
| 419 | 464 |
| 420 audio_focus_state_ = audio_focus_state; | 465 audio_focus_state_ = audio_focus_state; |
| 421 switch (audio_focus_state_) { | 466 switch (audio_focus_state_) { |
| 422 case State::ACTIVE: | 467 case State::ACTIVE: |
| 423 uma_helper_.OnSessionActive(); | 468 uma_helper_.OnSessionActive(); |
| 424 break; | 469 break; |
| 425 case State::SUSPENDED: | 470 case State::SUSPENDED: |
| 426 uma_helper_.OnSessionSuspended(); | 471 uma_helper_.OnSessionSuspended(); |
| 427 break; | 472 break; |
| 428 case State::INACTIVE: | 473 case State::INACTIVE: |
| 429 uma_helper_.OnSessionInactive(); | 474 uma_helper_.OnSessionInactive(); |
| 430 break; | 475 break; |
| 431 } | 476 } |
| 432 } | 477 } |
| 433 | 478 |
| 434 bool MediaSession::AddPepperPlayer(MediaSessionPlayerObserver* observer, | 479 bool MediaSessionImpl::AddPepperPlayer(MediaSessionPlayerObserver* observer, |
| 435 int player_id) { | 480 int player_id) { |
| 436 bool success = RequestSystemAudioFocus( | 481 bool success = |
| 437 AudioFocusManager::AudioFocusType::Gain); | 482 RequestSystemAudioFocus(AudioFocusManager::AudioFocusType::Gain); |
| 438 DCHECK(success); | 483 DCHECK(success); |
| 439 | 484 |
| 440 pepper_players_.insert(PlayerIdentifier(observer, player_id)); | 485 pepper_players_.insert(PlayerIdentifier(observer, player_id)); |
| 441 | 486 |
| 442 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); | 487 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); |
| 443 | 488 |
| 444 return true; | 489 return true; |
| 445 } | 490 } |
| 446 | 491 |
| 447 } // namespace content | 492 } // namespace content |
| OLD | NEW |