| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/audio_focus_manager.h" | 5 #include "content/browser/media/session/audio_focus_manager.h" |
| 6 | 6 |
| 7 #include "content/browser/media/session/media_session.h" | 7 #include "content/browser/media/session/media_session.h" |
| 8 #include "content/public/browser/web_contents.h" | 8 #include "content/public/browser/web_contents.h" |
| 9 | 9 |
| 10 namespace content { | 10 namespace content { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 MaybeStartDucking(); | 80 MaybeStartDucking(); |
| 81 } | 81 } |
| 82 | 82 |
| 83 void AudioFocusManager::OnWebContentsDestroyed(WebContents* web_contents) { | 83 void AudioFocusManager::OnWebContentsDestroyed(WebContents* web_contents) { |
| 84 AbandonAudioFocusInternal(web_contents); | 84 AbandonAudioFocusInternal(web_contents); |
| 85 } | 85 } |
| 86 | 86 |
| 87 void AudioFocusManager::AbandonAudioFocusInternal(WebContents* web_contents) { | 87 void AudioFocusManager::AbandonAudioFocusInternal(WebContents* web_contents) { |
| 88 MaybeRemoveTransientEntry(web_contents); | 88 MaybeRemoveTransientEntry(web_contents); |
| 89 MaybeRemoveFocusEntry(web_contents); | 89 MaybeRemoveFocusEntry(web_contents); |
| 90 AbandonPepperAudioFocus(MediaSession::Get(web_contents)); |
| 90 } | 91 } |
| 91 | 92 |
| 92 void AudioFocusManager::MaybeStartDucking() const { | 93 void AudioFocusManager::MaybeStartDucking() const { |
| 94 UpdatePepperFocus(); |
| 93 if (TransientMayDuckEntriesCount() != 1 || !focus_entry_) | 95 if (TransientMayDuckEntriesCount() != 1 || !focus_entry_) |
| 94 return; | 96 return; |
| 95 | 97 |
| 96 // TODO(mlamouri): add StartDuck to MediaSession. | 98 // TODO(mlamouri): add StartDuck to MediaSession. |
| 97 MediaSession::Get(focus_entry_->web_contents()) | 99 MediaSession::Get(focus_entry_->web_contents()) |
| 98 ->SetVolumeMultiplier(kDuckingVolumeMultiplier); | 100 ->SetVolumeMultiplier(kDuckingVolumeMultiplier); |
| 99 } | 101 } |
| 100 | 102 |
| 101 void AudioFocusManager::MaybeStopDucking() const { | 103 void AudioFocusManager::MaybeStopDucking() const { |
| 104 UpdatePepperFocus(); |
| 102 if (TransientMayDuckEntriesCount() != 0 || !focus_entry_) | 105 if (TransientMayDuckEntriesCount() != 0 || !focus_entry_) |
| 103 return; | 106 return; |
| 104 | 107 |
| 105 // TODO(mlamouri): add StopDuck to MediaSession. | 108 // TODO(mlamouri): add StopDuck to MediaSession. |
| 106 MediaSession::Get(focus_entry_->web_contents()) | 109 MediaSession::Get(focus_entry_->web_contents()) |
| 107 ->SetVolumeMultiplier(kDefaultVolumeMultiplier); | 110 ->SetVolumeMultiplier(kDefaultVolumeMultiplier); |
| 108 } | 111 } |
| 109 | 112 |
| 110 int AudioFocusManager::TransientMayDuckEntriesCount() const { | 113 int AudioFocusManager::TransientMayDuckEntriesCount() const { |
| 111 return transient_entries_.size(); | 114 return transient_entries_.size(); |
| 112 } | 115 } |
| 113 | 116 |
| 114 void AudioFocusManager::MaybeRemoveTransientEntry(WebContents* web_contents) { | 117 void AudioFocusManager::MaybeRemoveTransientEntry(WebContents* web_contents) { |
| 115 transient_entries_.erase(web_contents); | 118 transient_entries_.erase(web_contents); |
| 116 MaybeStopDucking(); | 119 MaybeStopDucking(); |
| 117 } | 120 } |
| 118 | 121 |
| 119 void AudioFocusManager::MaybeRemoveFocusEntry(WebContents* web_contents) { | 122 void AudioFocusManager::MaybeRemoveFocusEntry(WebContents* web_contents) { |
| 120 if (focus_entry_ && focus_entry_->web_contents() == web_contents) { | 123 if (focus_entry_ && focus_entry_->web_contents() == web_contents) { |
| 121 MediaSession::Get(focus_entry_->web_contents()) | 124 MediaSession::Get(focus_entry_->web_contents()) |
| 122 ->SetVolumeMultiplier(kDefaultVolumeMultiplier); | 125 ->SetVolumeMultiplier(kDefaultVolumeMultiplier); |
| 123 focus_entry_.reset(); | 126 focus_entry_.reset(); |
| 124 } | 127 } |
| 125 } | 128 } |
| 126 | 129 |
| 130 void AudioFocusManager::MaybeRemovePepperEntry(WebContents* web_contents) { |
| 131 for (auto iter = pepper_entries_.begin(); |
| 132 iter != pepper_entries_.end(); ++iter) { |
| 133 if ((*iter)->web_contents() == web_contents) { |
| 134 pepper_entries_.erase(iter); |
| 135 break; |
| 136 } |
| 137 } |
| 138 } |
| 139 |
| 140 void AudioFocusManager::RequestPepperAudioFocus(MediaSession* session) { |
| 141 MaybeRemovePepperEntry(session->web_contents()); |
| 142 pepper_entries_.push_back(std::unique_ptr<AudioFocusEntry>( |
| 143 new AudioFocusEntry(session->web_contents(), |
| 144 this, AudioFocusType::Gain))); |
| 145 |
| 146 UpdatePepperFocus(); |
| 147 } |
| 148 |
| 149 void AudioFocusManager::AbandonPepperAudioFocus(MediaSession* session) { |
| 150 MaybeRemovePepperEntry(session->web_contents()); |
| 151 |
| 152 UpdatePepperFocus(); |
| 153 } |
| 154 |
| 155 void AudioFocusManager::UpdatePepperFocus() const { |
| 156 if (!transient_entries_.empty()) { |
| 157 for (const auto& pepper_entry : pepper_entries_) { |
| 158 if (transient_entries_.count(pepper_entry->web_contents())) { |
| 159 MediaSession::Get(pepper_entry->web_contents()) |
| 160 ->SetPepperVolumeMultiplier(kDefaultVolumeMultiplier); |
| 161 } else { |
| 162 MediaSession::Get(pepper_entry->web_contents()) |
| 163 ->SetPepperVolumeMultiplier(kDuckingVolumeMultiplier); |
| 164 } |
| 165 } |
| 166 } else if (focus_entry_) { |
| 167 for (const auto& pepper_entry : pepper_entries_) { |
| 168 if (focus_entry_->web_contents() == pepper_entry->web_contents()) { |
| 169 MediaSession::Get(pepper_entry->web_contents()) |
| 170 ->SetPepperVolumeMultiplier(kDefaultVolumeMultiplier); |
| 171 } else { |
| 172 MediaSession::Get(pepper_entry->web_contents()) |
| 173 ->SetPepperVolumeMultiplier(kDuckingVolumeMultiplier); |
| 174 } |
| 175 } |
| 176 } else { |
| 177 bool is_first = true; |
| 178 for (auto iter = pepper_entries_.rbegin(); |
| 179 iter != pepper_entries_.rend(); ++iter) { |
| 180 if (is_first) { |
| 181 MediaSession::Get(pepper_entries_.back()->web_contents()) |
| 182 ->SetPepperVolumeMultiplier(kDefaultVolumeMultiplier); |
| 183 is_first = false; |
| 184 } else { |
| 185 MediaSession::Get((*iter)->web_contents()) |
| 186 ->SetPepperVolumeMultiplier(kDuckingVolumeMultiplier); |
| 187 } |
| 188 } |
| 189 } |
| 190 } |
| 191 |
| 127 } // namespace content | 192 } // namespace content |
| OLD | NEW |