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