| 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_player_observer.h" | 9 #include "content/browser/media/session/media_session_player_observer.h" |
| 10 #include "content/browser/media/session/media_session_service_impl.h" | 10 #include "content/browser/media/session/media_session_service_impl.h" |
| 11 #include "content/browser/media/session/media_session_service_router.h" |
| 11 #include "content/browser/web_contents/web_contents_impl.h" | 12 #include "content/browser/web_contents/web_contents_impl.h" |
| 12 #include "content/public/browser/media_session.h" | 13 #include "content/public/browser/media_session.h" |
| 13 #include "content/public/browser/media_session_observer.h" | 14 #include "content/public/browser/media_session_observer.h" |
| 14 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 15 #include "media/base/media_content_type.h" | 16 #include "media/base/media_content_type.h" |
| 16 | 17 |
| 17 #if defined(OS_ANDROID) | 18 #if defined(OS_ANDROID) |
| 18 #include "content/browser/media/session/media_session_android.h" | 19 #include "content/browser/media/session/media_session_android.h" |
| 19 #endif // defined(OS_ANDROID) | 20 #endif // defined(OS_ANDROID) |
| 20 | 21 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 // TODO(zqzhang): refactor MediaSessionImpl, maybe move the interface used to | 83 // TODO(zqzhang): refactor MediaSessionImpl, maybe move the interface used to |
| 83 // talk with AudioFocusManager out to a seperate class. The AudioFocusManager | 84 // talk with AudioFocusManager out to a seperate class. The AudioFocusManager |
| 84 // unit tests then could mock the interface and abandon audio focus when | 85 // unit tests then could mock the interface and abandon audio focus when |
| 85 // WebContents is destroyed. See https://crbug.com/651069 | 86 // WebContents is destroyed. See https://crbug.com/651069 |
| 86 normal_players_.clear(); | 87 normal_players_.clear(); |
| 87 pepper_players_.clear(); | 88 pepper_players_.clear(); |
| 88 one_shot_players_.clear(); | 89 one_shot_players_.clear(); |
| 89 AbandonSystemAudioFocusIfNeeded(); | 90 AbandonSystemAudioFocusIfNeeded(); |
| 90 } | 91 } |
| 91 | 92 |
| 92 void MediaSessionImpl::SetMediaSessionService( | 93 MediaSessionPlayerObserver* |
| 93 MediaSessionServiceImpl* service) { | 94 MediaSessionImpl::GetMostMeaningfulPlayerObserver() { |
| 94 service_ = service; | 95 MediaSessionPlayerObserver* player_observer = nullptr; |
| 96 // TODO(zqzhang): improve the selection of most-meaningful player. See |
| 97 // https://crbug.com/xxxxxx |
| 98 if (!normal_players_.empty()) |
| 99 return player_observer = normal_players_.begin()->observer; |
| 100 if (!one_shot_players_.empty()) |
| 101 return player_observer = one_shot_players_.begin()->observer; |
| 102 if (!pepper_players_.empty()) |
| 103 return player_observer = pepper_players_.begin()->observer; |
| 104 |
| 105 return nullptr; |
| 95 } | 106 } |
| 96 | 107 |
| 97 void MediaSessionImpl::AddObserver(MediaSessionObserver* observer) { | 108 void MediaSessionImpl::AddObserver(MediaSessionObserver* observer) { |
| 98 observers_.AddObserver(observer); | 109 observers_.AddObserver(observer); |
| 99 } | 110 } |
| 100 | 111 |
| 101 void MediaSessionImpl::RemoveObserver(MediaSessionObserver* observer) { | 112 void MediaSessionImpl::RemoveObserver(MediaSessionObserver* observer) { |
| 102 observers_.RemoveObserver(observer); | 113 observers_.RemoveObserver(observer); |
| 103 } | 114 } |
| 104 | 115 |
| 105 void MediaSessionImpl::SetMetadata( | 116 void MediaSessionImpl::NotifyMediaSessionMetadataChange( |
| 106 const base::Optional<MediaMetadata>& metadata) { | 117 const base::Optional<MediaMetadata>& metadata) { |
| 107 metadata_ = metadata; | |
| 108 for (auto& observer : observers_) | 118 for (auto& observer : observers_) |
| 109 observer.MediaSessionMetadataChanged(metadata); | 119 observer.MediaSessionMetadataChanged(metadata); |
| 110 } | 120 } |
| 111 | 121 |
| 122 void MediaSessionImpl::NotifyMediaSessionActionsChange( |
| 123 const std::set<blink::mojom::MediaSessionAction>& actions) { |
| 124 for (auto& observer : observers_) |
| 125 observer.MediaSessionActionsChanged(actions); |
| 126 } |
| 127 |
| 112 bool MediaSessionImpl::AddPlayer(MediaSessionPlayerObserver* observer, | 128 bool MediaSessionImpl::AddPlayer(MediaSessionPlayerObserver* observer, |
| 113 int player_id, | 129 int player_id, |
| 114 media::MediaContentType media_content_type) { | 130 media::MediaContentType media_content_type) { |
| 115 if (media_content_type == media::MediaContentType::OneShot) | 131 if (media_content_type == media::MediaContentType::OneShot) |
| 116 return AddOneShotPlayer(observer, player_id); | 132 return AddOneShotPlayer(observer, player_id); |
| 117 if (media_content_type == media::MediaContentType::Pepper) | 133 if (media_content_type == media::MediaContentType::Pepper) |
| 118 return AddPepperPlayer(observer, player_id); | 134 return AddPepperPlayer(observer, player_id); |
| 119 | 135 |
| 120 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); | 136 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); |
| 121 | 137 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 143 | 159 |
| 144 if (audio_focus_state_ != State::ACTIVE) | 160 if (audio_focus_state_ != State::ACTIVE) |
| 145 return false; | 161 return false; |
| 146 | 162 |
| 147 // The session should be reset if a player is starting while all players are | 163 // The session should be reset if a player is starting while all players are |
| 148 // suspended. | 164 // suspended. |
| 149 if (old_audio_focus_state != State::ACTIVE) | 165 if (old_audio_focus_state != State::ACTIVE) |
| 150 normal_players_.clear(); | 166 normal_players_.clear(); |
| 151 | 167 |
| 152 normal_players_.insert(PlayerIdentifier(observer, player_id)); | 168 normal_players_.insert(PlayerIdentifier(observer, player_id)); |
| 169 |
| 170 service_router_->MostMeaningfulPlayerObserverMayHaveChanged(); |
| 153 NotifyAboutStateChange(); | 171 NotifyAboutStateChange(); |
| 154 | |
| 155 return true; | 172 return true; |
| 156 } | 173 } |
| 157 | 174 |
| 158 void MediaSessionImpl::RemovePlayer(MediaSessionPlayerObserver* observer, | 175 void MediaSessionImpl::RemovePlayer(MediaSessionPlayerObserver* observer, |
| 159 int player_id) { | 176 int player_id) { |
| 160 bool was_controllable = IsControllable(); | 177 bool was_controllable = IsControllable(); |
| 161 | 178 |
| 162 PlayerIdentifier identifier(observer, player_id); | 179 PlayerIdentifier identifier(observer, player_id); |
| 163 | 180 |
| 164 auto it = normal_players_.find(identifier); | 181 auto it = normal_players_.find(identifier); |
| 165 if (it != normal_players_.end()) | 182 if (it != normal_players_.end()) |
| 166 normal_players_.erase(it); | 183 normal_players_.erase(it); |
| 167 | 184 |
| 168 it = pepper_players_.find(identifier); | 185 it = pepper_players_.find(identifier); |
| 169 if (it != pepper_players_.end()) | 186 if (it != pepper_players_.end()) |
| 170 pepper_players_.erase(it); | 187 pepper_players_.erase(it); |
| 171 | 188 |
| 172 it = one_shot_players_.find(identifier); | 189 it = one_shot_players_.find(identifier); |
| 173 if (it != one_shot_players_.end()) | 190 if (it != one_shot_players_.end()) |
| 174 one_shot_players_.erase(it); | 191 one_shot_players_.erase(it); |
| 175 | 192 |
| 176 AbandonSystemAudioFocusIfNeeded(); | 193 AbandonSystemAudioFocusIfNeeded(); |
| 194 service_router_->MostMeaningfulPlayerObserverMayHaveChanged(); |
| 177 | 195 |
| 178 // The session may become controllable after removing a one-shot player. | 196 // The session may become controllable after removing a one-shot player. |
| 179 // However AbandonSystemAudioFocusIfNeeded will short-return and won't notify | 197 // However AbandonSystemAudioFocusIfNeeded will short-return and won't notify |
| 180 // about the state change. | 198 // about the state change. |
| 181 if (!was_controllable && IsControllable()) | 199 if (!was_controllable && IsControllable()) |
| 182 NotifyAboutStateChange(); | 200 NotifyAboutStateChange(); |
| 183 } | 201 } |
| 184 | 202 |
| 185 void MediaSessionImpl::RemovePlayers(MediaSessionPlayerObserver* observer) { | 203 void MediaSessionImpl::RemovePlayers(MediaSessionPlayerObserver* observer) { |
| 186 bool was_controllable = IsControllable(); | 204 bool was_controllable = IsControllable(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 200 } | 218 } |
| 201 | 219 |
| 202 for (auto it = one_shot_players_.begin(); it != one_shot_players_.end();) { | 220 for (auto it = one_shot_players_.begin(); it != one_shot_players_.end();) { |
| 203 if (it->observer == observer) | 221 if (it->observer == observer) |
| 204 one_shot_players_.erase(it++); | 222 one_shot_players_.erase(it++); |
| 205 else | 223 else |
| 206 ++it; | 224 ++it; |
| 207 } | 225 } |
| 208 | 226 |
| 209 AbandonSystemAudioFocusIfNeeded(); | 227 AbandonSystemAudioFocusIfNeeded(); |
| 228 service_router_->MostMeaningfulPlayerObserverMayHaveChanged(); |
| 210 | 229 |
| 211 // The session may become controllable after removing a one-shot player. | 230 // The session may become controllable after removing a one-shot player. |
| 212 // However AbandonSystemAudioFocusIfNeeded will short-return and won't notify | 231 // However AbandonSystemAudioFocusIfNeeded will short-return and won't notify |
| 213 // about the state change. | 232 // about the state change. |
| 214 if (!was_controllable && IsControllable()) | 233 if (!was_controllable && IsControllable()) |
| 215 NotifyAboutStateChange(); | 234 NotifyAboutStateChange(); |
| 216 } | 235 } |
| 217 | 236 |
| 218 void MediaSessionImpl::RecordSessionDuck() { | 237 void MediaSessionImpl::RecordSessionDuck() { |
| 219 uma_helper_.RecordSessionSuspended( | 238 uma_helper_.RecordSessionSuspended( |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 OnSuspendInternal(suspend_type, State::SUSPENDED); | 313 OnSuspendInternal(suspend_type, State::SUSPENDED); |
| 295 | 314 |
| 296 DCHECK(audio_focus_state_ == State::SUSPENDED); | 315 DCHECK(audio_focus_state_ == State::SUSPENDED); |
| 297 normal_players_.clear(); | 316 normal_players_.clear(); |
| 298 | 317 |
| 299 AbandonSystemAudioFocusIfNeeded(); | 318 AbandonSystemAudioFocusIfNeeded(); |
| 300 } | 319 } |
| 301 | 320 |
| 302 void MediaSessionImpl::DidReceiveAction( | 321 void MediaSessionImpl::DidReceiveAction( |
| 303 blink::mojom::MediaSessionAction action) { | 322 blink::mojom::MediaSessionAction action) { |
| 304 if (service_) | 323 GetMediaSessionServiceRouter()->DidReceiveAction(action); |
| 305 service_->GetClient()->DidReceiveAction(action); | |
| 306 } | 324 } |
| 307 | 325 |
| 308 void MediaSessionImpl::OnMediaSessionEnabledAction( | 326 void MediaSessionImpl::OnMediaSessionActionsChanged( |
| 309 blink::mojom::MediaSessionAction action) { | 327 const std::set<blink::mojom::MediaSessionAction>& action) { |
| 310 for (auto& observer : observers_) | 328 for (auto& observer : observers_) |
| 311 observer.MediaSessionEnabledAction(action); | 329 observer.MediaSessionActionsChanged(action); |
| 312 } | |
| 313 | |
| 314 void MediaSessionImpl::OnMediaSessionDisabledAction( | |
| 315 blink::mojom::MediaSessionAction action) { | |
| 316 for (auto& observer : observers_) | |
| 317 observer.MediaSessionDisabledAction(action); | |
| 318 } | 330 } |
| 319 | 331 |
| 320 void MediaSessionImpl::StartDucking() { | 332 void MediaSessionImpl::StartDucking() { |
| 321 if (is_ducking_) | 333 if (is_ducking_) |
| 322 return; | 334 return; |
| 323 is_ducking_ = true; | 335 is_ducking_ = true; |
| 324 UpdateVolumeMultiplier(); | 336 UpdateVolumeMultiplier(); |
| 325 } | 337 } |
| 326 | 338 |
| 327 void MediaSessionImpl::StopDucking() { | 339 void MediaSessionImpl::StopDucking() { |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 | 475 |
| 464 NotifyAboutStateChange(); | 476 NotifyAboutStateChange(); |
| 465 } | 477 } |
| 466 | 478 |
| 467 MediaSessionImpl::MediaSessionImpl(WebContents* web_contents) | 479 MediaSessionImpl::MediaSessionImpl(WebContents* web_contents) |
| 468 : WebContentsObserver(web_contents), | 480 : WebContentsObserver(web_contents), |
| 469 audio_focus_state_(State::INACTIVE), | 481 audio_focus_state_(State::INACTIVE), |
| 470 audio_focus_type_( | 482 audio_focus_type_( |
| 471 AudioFocusManager::AudioFocusType::GainTransientMayDuck), | 483 AudioFocusManager::AudioFocusType::GainTransientMayDuck), |
| 472 is_ducking_(false), | 484 is_ducking_(false), |
| 473 service_(nullptr) { | 485 service_router_(new MediaSessionServiceRouter(this)) { |
| 474 #if defined(OS_ANDROID) | 486 #if defined(OS_ANDROID) |
| 475 session_android_.reset(new MediaSessionAndroid(this)); | 487 session_android_.reset(new MediaSessionAndroid(this)); |
| 476 #endif // defined(OS_ANDROID) | 488 #endif // defined(OS_ANDROID) |
| 477 } | 489 } |
| 478 | 490 |
| 479 void MediaSessionImpl::Initialize() { | 491 void MediaSessionImpl::Initialize() { |
| 480 delegate_ = AudioFocusDelegate::Create(this); | 492 delegate_ = AudioFocusDelegate::Create(this); |
| 481 } | 493 } |
| 482 | 494 |
| 483 bool MediaSessionImpl::RequestSystemAudioFocus( | 495 bool MediaSessionImpl::RequestSystemAudioFocus( |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 if (!RequestSystemAudioFocus(AudioFocusManager::AudioFocusType::Gain)) | 558 if (!RequestSystemAudioFocus(AudioFocusManager::AudioFocusType::Gain)) |
| 547 return false; | 559 return false; |
| 548 | 560 |
| 549 one_shot_players_.insert(PlayerIdentifier(observer, player_id)); | 561 one_shot_players_.insert(PlayerIdentifier(observer, player_id)); |
| 550 NotifyAboutStateChange(); | 562 NotifyAboutStateChange(); |
| 551 | 563 |
| 552 return true; | 564 return true; |
| 553 } | 565 } |
| 554 | 566 |
| 555 } // namespace content | 567 } // namespace content |
| OLD | NEW |