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