| 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_impl.h" | 5 #include "content/browser/media/session/media_session_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include "content/browser/media/session/audio_focus_delegate.h" | 8 #include "content/browser/media/session/audio_focus_delegate.h" |
| 9 #include "content/browser/media/session/media_session_controller.h" | 9 #include "content/browser/media/session/media_session_controller.h" |
| 10 #include "content/browser/media/session/media_session_player_observer.h" | 10 #include "content/browser/media/session/media_session_player_observer.h" |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 } | 284 } |
| 285 | 285 |
| 286 // If the player is a one-shot player, just remove it since it is not expected | 286 // If the player is a one-shot player, just remove it since it is not expected |
| 287 // to resume a one-shot player via resuming MediaSession. | 287 // to resume a one-shot player via resuming MediaSession. |
| 288 if (one_shot_players_.count(identifier)) { | 288 if (one_shot_players_.count(identifier)) { |
| 289 RemovePlayer(observer, player_id); | 289 RemovePlayer(observer, player_id); |
| 290 return; | 290 return; |
| 291 } | 291 } |
| 292 | 292 |
| 293 // Otherwise, suspend the session. | 293 // Otherwise, suspend the session. |
| 294 DCHECK(!IsSuspended()); | 294 DCHECK(IsActive()); |
| 295 OnSuspendInternal(SuspendType::CONTENT, State::SUSPENDED); | 295 OnSuspendInternal(SuspendType::CONTENT, State::SUSPENDED); |
| 296 } | 296 } |
| 297 | 297 |
| 298 void MediaSessionImpl::Resume(SuspendType suspend_type) { | 298 void MediaSessionImpl::Resume(SuspendType suspend_type) { |
| 299 DCHECK(IsReallySuspended()); | 299 DCHECK(IsSuspended()); |
| 300 | 300 |
| 301 // When the resume requests comes from another source than system, audio focus | 301 // When the resume requests comes from another source than system, audio focus |
| 302 // must be requested. | 302 // must be requested. |
| 303 if (suspend_type != SuspendType::SYSTEM) { | 303 if (suspend_type != SuspendType::SYSTEM) { |
| 304 // Request audio focus again in case we lost it because another app started | 304 // Request audio focus again in case we lost it because another app started |
| 305 // playing while the playback was paused. | 305 // playing while the playback was paused. |
| 306 State audio_focus_state = RequestSystemAudioFocus(audio_focus_type_) | 306 State audio_focus_state = RequestSystemAudioFocus(audio_focus_type_) |
| 307 ? State::ACTIVE | 307 ? State::ACTIVE |
| 308 : State::INACTIVE; | 308 : State::INACTIVE; |
| 309 SetAudioFocusState(audio_focus_state); | 309 SetAudioFocusState(audio_focus_state); |
| 310 | 310 |
| 311 if (audio_focus_state_ != State::ACTIVE) | 311 if (audio_focus_state_ != State::ACTIVE) |
| 312 return; | 312 return; |
| 313 } | 313 } |
| 314 | 314 |
| 315 OnResumeInternal(suspend_type); | 315 OnResumeInternal(suspend_type); |
| 316 } | 316 } |
| 317 | 317 |
| 318 void MediaSessionImpl::Suspend(SuspendType suspend_type) { | 318 void MediaSessionImpl::Suspend(SuspendType suspend_type) { |
| 319 if (IsSuspended()) | 319 if (!IsActive()) |
| 320 return; | 320 return; |
| 321 | 321 |
| 322 OnSuspendInternal(suspend_type, State::SUSPENDED); | 322 OnSuspendInternal(suspend_type, State::SUSPENDED); |
| 323 } | 323 } |
| 324 | 324 |
| 325 void MediaSessionImpl::Stop(SuspendType suspend_type) { | 325 void MediaSessionImpl::Stop(SuspendType suspend_type) { |
| 326 DCHECK(audio_focus_state_ != State::INACTIVE); | 326 DCHECK(audio_focus_state_ != State::INACTIVE); |
| 327 DCHECK(suspend_type != SuspendType::CONTENT); | 327 DCHECK(suspend_type != SuspendType::CONTENT); |
| 328 DCHECK(!HasPepper()); | 328 DCHECK(!HasPepper()); |
| 329 | 329 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 } | 364 } |
| 365 | 365 |
| 366 double MediaSessionImpl::GetVolumeMultiplier() const { | 366 double MediaSessionImpl::GetVolumeMultiplier() const { |
| 367 return is_ducking_ ? kDuckingVolumeMultiplier : kDefaultVolumeMultiplier; | 367 return is_ducking_ ? kDuckingVolumeMultiplier : kDefaultVolumeMultiplier; |
| 368 } | 368 } |
| 369 | 369 |
| 370 bool MediaSessionImpl::IsActive() const { | 370 bool MediaSessionImpl::IsActive() const { |
| 371 return audio_focus_state_ == State::ACTIVE; | 371 return audio_focus_state_ == State::ACTIVE; |
| 372 } | 372 } |
| 373 | 373 |
| 374 bool MediaSessionImpl::IsReallySuspended() const { | 374 bool MediaSessionImpl::IsSuspended() const { |
| 375 return audio_focus_state_ == State::SUSPENDED; | 375 return audio_focus_state_ == State::SUSPENDED; |
| 376 } | 376 } |
| 377 | 377 |
| 378 bool MediaSessionImpl::IsSuspended() const { | 378 bool MediaSessionImpl::IsActuallyPaused() const { |
| 379 // TODO(mlamouri): should be == State::SUSPENDED. | 379 if (routed_service_ && routed_service_->playback_state() == |
| 380 return audio_focus_state_ != State::ACTIVE; | 380 blink::mojom::MediaSessionPlaybackState::PLAYING) { |
| 381 return false; |
| 382 } |
| 383 return !IsActive(); |
| 381 } | 384 } |
| 382 | 385 |
| 383 bool MediaSessionImpl::IsControllable() const { | 386 bool MediaSessionImpl::IsControllable() const { |
| 384 // Only media session having focus Gain can be controllable unless it is | 387 // Only media session having focus Gain can be controllable unless it is |
| 385 // inactive. Also, the session will be uncontrollable if it contains one-shot | 388 // inactive. Also, the session will be uncontrollable if it contains one-shot |
| 386 // players. | 389 // players. |
| 387 return audio_focus_state_ != State::INACTIVE && | 390 return audio_focus_state_ != State::INACTIVE && |
| 388 audio_focus_type_ == AudioFocusManager::AudioFocusType::Gain && | 391 audio_focus_type_ == AudioFocusManager::AudioFocusType::Gain && |
| 389 one_shot_players_.empty(); | 392 one_shot_players_.empty(); |
| 390 } | 393 } |
| 391 | 394 |
| 392 bool MediaSessionImpl::HasPepper() const { | 395 bool MediaSessionImpl::HasPepper() const { |
| 393 return !pepper_players_.empty(); | 396 return !pepper_players_.empty(); |
| 394 } | 397 } |
| 395 | 398 |
| 396 std::unique_ptr<base::CallbackList<void(MediaSessionImpl::State)>::Subscription> | 399 std::unique_ptr<base::CallbackList<void(MediaSessionImpl::State)>::Subscription> |
| 397 MediaSessionImpl::RegisterMediaSessionStateChangedCallbackForTest( | 400 MediaSessionImpl::RegisterMediaSessionStateChangedCallbackForTest( |
| 398 const StateChangedCallback& cb) { | 401 const StateChangedCallback& cb) { |
| 399 return media_session_state_listeners_.Add(cb); | 402 return media_session_state_listeners_.Add(cb); |
| 400 } | 403 } |
| 401 | 404 |
| 402 void MediaSessionImpl::SetDelegateForTests( | 405 void MediaSessionImpl::SetDelegateForTests( |
| 403 std::unique_ptr<AudioFocusDelegate> delegate) { | 406 std::unique_ptr<AudioFocusDelegate> delegate) { |
| 404 delegate_ = std::move(delegate); | 407 delegate_ = std::move(delegate); |
| 405 } | 408 } |
| 406 | 409 |
| 407 bool MediaSessionImpl::IsActiveForTest() const { | |
| 408 return audio_focus_state_ == State::ACTIVE; | |
| 409 } | |
| 410 | |
| 411 MediaSessionUmaHelper* MediaSessionImpl::uma_helper_for_test() { | 410 MediaSessionUmaHelper* MediaSessionImpl::uma_helper_for_test() { |
| 412 return &uma_helper_; | 411 return &uma_helper_; |
| 413 } | 412 } |
| 414 | 413 |
| 415 void MediaSessionImpl::RemoveAllPlayersForTest() { | 414 void MediaSessionImpl::RemoveAllPlayersForTest() { |
| 416 normal_players_.clear(); | 415 normal_players_.clear(); |
| 417 pepper_players_.clear(); | 416 pepper_players_.clear(); |
| 418 one_shot_players_.clear(); | 417 one_shot_players_.clear(); |
| 419 AbandonSystemAudioFocusIfNeeded(); | 418 AbandonSystemAudioFocusIfNeeded(); |
| 420 } | 419 } |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 522 !pepper_players_.empty() || !one_shot_players_.empty()) { | 521 !pepper_players_.empty() || !one_shot_players_.empty()) { |
| 523 return; | 522 return; |
| 524 } | 523 } |
| 525 delegate_->AbandonAudioFocus(); | 524 delegate_->AbandonAudioFocus(); |
| 526 | 525 |
| 527 SetAudioFocusState(State::INACTIVE); | 526 SetAudioFocusState(State::INACTIVE); |
| 528 NotifyAboutStateChange(); | 527 NotifyAboutStateChange(); |
| 529 } | 528 } |
| 530 | 529 |
| 531 void MediaSessionImpl::NotifyAboutStateChange() { | 530 void MediaSessionImpl::NotifyAboutStateChange() { |
| 532 bool is_actually_suspended = IsSuspended(); | 531 bool is_actually_paused = IsActuallyPaused(); |
| 533 // Compute the actual playback state using both the MediaSessionService state | |
| 534 // and real state. | |
| 535 // | |
| 536 // TODO(zqzhang): Maybe also compute for IsControllable()? See | |
| 537 // https://crbug.com/674983 | |
| 538 if (routed_service_ && | |
| 539 routed_service_->playback_state() == | |
| 540 blink::mojom::MediaSessionPlaybackState::PLAYING) { | |
| 541 is_actually_suspended = false; | |
| 542 } | |
| 543 | 532 |
| 544 media_session_state_listeners_.Notify(audio_focus_state_); | 533 media_session_state_listeners_.Notify(audio_focus_state_); |
| 545 for (auto& observer : observers_) | 534 for (auto& observer : observers_) |
| 546 observer.MediaSessionStateChanged(IsControllable(), is_actually_suspended); | 535 observer.MediaSessionStateChanged(IsControllable(), is_actually_paused); |
| 547 } | 536 } |
| 548 | 537 |
| 549 void MediaSessionImpl::SetAudioFocusState(State audio_focus_state) { | 538 void MediaSessionImpl::SetAudioFocusState(State audio_focus_state) { |
| 550 if (audio_focus_state == audio_focus_state_) | 539 if (audio_focus_state == audio_focus_state_) |
| 551 return; | 540 return; |
| 552 | 541 |
| 553 audio_focus_state_ = audio_focus_state; | 542 audio_focus_state_ = audio_focus_state; |
| 554 switch (audio_focus_state_) { | 543 switch (audio_focus_state_) { |
| 555 case State::ACTIVE: | 544 case State::ACTIVE: |
| 556 uma_helper_.OnSessionActive(); | 545 uma_helper_.OnSessionActive(); |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 722 if (!IsServiceActiveForRenderFrameHost(frame)) | 711 if (!IsServiceActiveForRenderFrameHost(frame)) |
| 723 continue; | 712 continue; |
| 724 best_frame = frame; | 713 best_frame = frame; |
| 725 min_depth = depth; | 714 min_depth = depth; |
| 726 } | 715 } |
| 727 | 716 |
| 728 return best_frame ? services_[best_frame] : nullptr; | 717 return best_frame ? services_[best_frame] : nullptr; |
| 729 } | 718 } |
| 730 | 719 |
| 731 } // namespace content | 720 } // namespace content |
| OLD | NEW |