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