| 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.h" |
| 6 | 6 |
| 7 #include "content/browser/media/session/media_session_delegate.h" | 7 #include "content/browser/media/session/media_session_delegate.h" |
| 8 #include "content/browser/media/session/media_session_observer.h" | 8 #include "content/browser/media/session/media_session_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 | 13 |
| 13 namespace content { | 14 namespace content { |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 const double kDefaultVolumeMultiplier = 1.0; | 18 const double kDefaultVolumeMultiplier = 1.0; |
| 18 | 19 |
| 19 } // anonymous namespace | 20 } // anonymous namespace |
| 20 | 21 |
| 21 using MediaSessionSuspendedSource = | 22 using MediaSessionSuspendedSource = |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 void MediaSession::SetMetadata(const MediaMetadata& metadata) { | 62 void MediaSession::SetMetadata(const MediaMetadata& metadata) { |
| 62 metadata_ = metadata; | 63 metadata_ = metadata; |
| 63 // TODO(zqzhang): On Android, the metadata is sent though JNI everytime the | 64 // TODO(zqzhang): On Android, the metadata is sent though JNI everytime the |
| 64 // media session play/pause state changes. Need to find a way to seprate the | 65 // media session play/pause state changes. Need to find a way to seprate the |
| 65 // state change and Metadata update. See https://crbug.com/621855. | 66 // state change and Metadata update. See https://crbug.com/621855. |
| 66 static_cast<WebContentsImpl*>(web_contents())->OnMediaSessionStateChanged(); | 67 static_cast<WebContentsImpl*>(web_contents())->OnMediaSessionStateChanged(); |
| 67 } | 68 } |
| 68 | 69 |
| 69 bool MediaSession::AddPlayer(MediaSessionObserver* observer, | 70 bool MediaSession::AddPlayer(MediaSessionObserver* observer, |
| 70 int player_id, | 71 int player_id, |
| 71 Type type) { | 72 media::MediaContentType type) { |
| 73 if (type == media::MediaContentType::Uncontrollable) { |
| 74 return true; |
| 75 } |
| 72 observer->OnSetVolumeMultiplier(player_id, volume_multiplier_); | 76 observer->OnSetVolumeMultiplier(player_id, volume_multiplier_); |
| 73 | 77 |
| 74 // If the audio focus is already granted and is of type Content, there is | 78 // If the audio focus is already granted and is of type Content, there is |
| 75 // nothing to do. If it is granted of type Transient the requested type is | 79 // nothing to do. If it is granted of type Transient the requested type is |
| 76 // also transient, there is also nothing to do. Otherwise, the session needs | 80 // also transient, there is also nothing to do. Otherwise, the session needs |
| 77 // to request audio focus again. | 81 // to request audio focus again. |
| 82 AudioFocusType required_audio_focus_type = |
| 83 (type == media::MediaContentType::Transient) |
| 84 ? AudioFocusType::Transient : AudioFocusType::Content; |
| 85 |
| 78 if (audio_focus_state_ == State::ACTIVE && | 86 if (audio_focus_state_ == State::ACTIVE && |
| 79 (audio_focus_type_ == Type::Content || audio_focus_type_ == type)) { | 87 (audio_focus_type_ == AudioFocusType::Content || |
| 88 audio_focus_type_ == required_audio_focus_type)) { |
| 80 players_.insert(PlayerIdentifier(observer, player_id)); | 89 players_.insert(PlayerIdentifier(observer, player_id)); |
| 81 return true; | 90 return true; |
| 82 } | 91 } |
| 83 | 92 |
| 84 State old_audio_focus_state = audio_focus_state_; | 93 State old_audio_focus_state = audio_focus_state_; |
| 85 State audio_focus_state = RequestSystemAudioFocus(type) ? State::ACTIVE | 94 State audio_focus_state = RequestSystemAudioFocus(required_audio_focus_type) |
| 86 : State::INACTIVE; | 95 ? State::ACTIVE : State::INACTIVE; |
| 87 SetAudioFocusState(audio_focus_state); | 96 SetAudioFocusState(audio_focus_state); |
| 88 audio_focus_type_ = type; | 97 audio_focus_type_ = required_audio_focus_type; |
| 89 | 98 |
| 90 if (audio_focus_state_ != State::ACTIVE) | 99 if (audio_focus_state_ != State::ACTIVE) |
| 91 return false; | 100 return false; |
| 92 | 101 |
| 93 // The session should be reset if a player is starting while all players are | 102 // The session should be reset if a player is starting while all players are |
| 94 // suspended. | 103 // suspended. |
| 95 if (old_audio_focus_state != State::ACTIVE) | 104 if (old_audio_focus_state != State::ACTIVE) |
| 96 players_.clear(); | 105 players_.clear(); |
| 97 | 106 |
| 98 players_.insert(PlayerIdentifier(observer, player_id)); | 107 players_.insert(PlayerIdentifier(observer, player_id)); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 } | 216 } |
| 208 | 217 |
| 209 bool MediaSession::IsSuspended() const { | 218 bool MediaSession::IsSuspended() const { |
| 210 // TODO(mlamouri): should be == State::SUSPENDED. | 219 // TODO(mlamouri): should be == State::SUSPENDED. |
| 211 return audio_focus_state_ != State::ACTIVE; | 220 return audio_focus_state_ != State::ACTIVE; |
| 212 } | 221 } |
| 213 | 222 |
| 214 bool MediaSession::IsControllable() const { | 223 bool MediaSession::IsControllable() const { |
| 215 // Only content type media session can be controllable unless it is inactive. | 224 // Only content type media session can be controllable unless it is inactive. |
| 216 return audio_focus_state_ != State::INACTIVE && | 225 return audio_focus_state_ != State::INACTIVE && |
| 217 audio_focus_type_ == Type::Content; | 226 audio_focus_type_ == AudioFocusType::Content; |
| 218 } | 227 } |
| 219 | 228 |
| 220 std::unique_ptr<base::CallbackList<void(MediaSession::State)>::Subscription> | 229 std::unique_ptr<base::CallbackList<void(MediaSession::State)>::Subscription> |
| 221 MediaSession::RegisterMediaSessionStateChangedCallbackForTest( | 230 MediaSession::RegisterMediaSessionStateChangedCallbackForTest( |
| 222 const StateChangedCallback& cb) { | 231 const StateChangedCallback& cb) { |
| 223 return media_session_state_listeners_.Add(cb); | 232 return media_session_state_listeners_.Add(cb); |
| 224 } | 233 } |
| 225 | 234 |
| 226 void MediaSession::SetDelegateForTests( | 235 void MediaSession::SetDelegateForTests( |
| 227 std::unique_ptr<MediaSessionDelegate> delegate) { | 236 std::unique_ptr<MediaSessionDelegate> delegate) { |
| 228 delegate_ = std::move(delegate); | 237 delegate_ = std::move(delegate); |
| 229 } | 238 } |
| 230 | 239 |
| 231 bool MediaSession::IsActiveForTest() const { | 240 bool MediaSession::IsActiveForTest() const { |
| 232 return audio_focus_state_ == State::ACTIVE; | 241 return audio_focus_state_ == State::ACTIVE; |
| 233 } | 242 } |
| 234 | 243 |
| 235 MediaSession::Type MediaSession::audio_focus_type_for_test() const { | 244 MediaSession::AudioFocusType MediaSession::audio_focus_type_for_test() const { |
| 236 return audio_focus_type_; | 245 return audio_focus_type_; |
| 237 } | 246 } |
| 238 | 247 |
| 239 MediaSessionUmaHelper* MediaSession::uma_helper_for_test() { | 248 MediaSessionUmaHelper* MediaSession::uma_helper_for_test() { |
| 240 return &uma_helper_; | 249 return &uma_helper_; |
| 241 } | 250 } |
| 242 | 251 |
| 243 void MediaSession::RemoveAllPlayersForTest() { | 252 void MediaSession::RemoveAllPlayersForTest() { |
| 244 players_.clear(); | 253 players_.clear(); |
| 245 AbandonSystemAudioFocusIfNeeded(); | 254 AbandonSystemAudioFocusIfNeeded(); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 | 308 |
| 300 for (const auto& it : players_) | 309 for (const auto& it : players_) |
| 301 it.observer->OnResume(it.player_id); | 310 it.observer->OnResume(it.player_id); |
| 302 | 311 |
| 303 UpdateWebContents(); | 312 UpdateWebContents(); |
| 304 } | 313 } |
| 305 | 314 |
| 306 MediaSession::MediaSession(WebContents* web_contents) | 315 MediaSession::MediaSession(WebContents* web_contents) |
| 307 : WebContentsObserver(web_contents), | 316 : WebContentsObserver(web_contents), |
| 308 audio_focus_state_(State::INACTIVE), | 317 audio_focus_state_(State::INACTIVE), |
| 309 audio_focus_type_(Type::Transient), | 318 audio_focus_type_(AudioFocusType::Transient), |
| 310 volume_multiplier_(kDefaultVolumeMultiplier) { | 319 volume_multiplier_(kDefaultVolumeMultiplier) { |
| 311 } | 320 } |
| 312 | 321 |
| 313 void MediaSession::Initialize() { | 322 void MediaSession::Initialize() { |
| 314 delegate_ = MediaSessionDelegate::Create(this); | 323 delegate_ = MediaSessionDelegate::Create(this); |
| 315 } | 324 } |
| 316 | 325 |
| 317 bool MediaSession::RequestSystemAudioFocus(Type type) { | 326 bool MediaSession::RequestSystemAudioFocus(AudioFocusType type) { |
| 318 bool result = delegate_->RequestAudioFocus(type); | 327 bool result = delegate_->RequestAudioFocus(type); |
| 319 uma_helper_.RecordRequestAudioFocusResult(result); | 328 uma_helper_.RecordRequestAudioFocusResult(result); |
| 320 return result; | 329 return result; |
| 321 } | 330 } |
| 322 | 331 |
| 323 void MediaSession::AbandonSystemAudioFocusIfNeeded() { | 332 void MediaSession::AbandonSystemAudioFocusIfNeeded() { |
| 324 if (audio_focus_state_ == State::INACTIVE || !players_.empty()) | 333 if (audio_focus_state_ == State::INACTIVE || !players_.empty()) |
| 325 return; | 334 return; |
| 326 | 335 |
| 327 delegate_->AbandonAudioFocus(); | 336 delegate_->AbandonAudioFocus(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 347 case State::SUSPENDED: | 356 case State::SUSPENDED: |
| 348 uma_helper_.OnSessionSuspended(); | 357 uma_helper_.OnSessionSuspended(); |
| 349 break; | 358 break; |
| 350 case State::INACTIVE: | 359 case State::INACTIVE: |
| 351 uma_helper_.OnSessionInactive(); | 360 uma_helper_.OnSessionInactive(); |
| 352 break; | 361 break; |
| 353 } | 362 } |
| 354 } | 363 } |
| 355 | 364 |
| 356 } // namespace content | 365 } // namespace content |
| OLD | NEW |