Chromium Code Reviews| Index: content/browser/media/session/audio_focus_manager.cc |
| diff --git a/content/browser/media/session/audio_focus_manager.cc b/content/browser/media/session/audio_focus_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..23b6b666db5e99696879cdeba230dd5e5006e15f |
| --- /dev/null |
| +++ b/content/browser/media/session/audio_focus_manager.cc |
| @@ -0,0 +1,163 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/media/session/audio_focus_manager.h" |
| + |
| +#include "base/optional.h" |
| +#include "content/browser/media/session/media_session.h" |
| +#include "content/public/browser/web_contents.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +const double kDuckingVolumeMultiplier = 0.2; |
| +const double kDefaultVolumeMultiplier = 1.0; |
| + |
| +} // anonymous namespace |
| + |
| +AudioFocusManager::AudioFocusEntry::AudioFocusEntry(WebContents* web_contents, AudioFocusManager* audio_focus_manager, AudioFocusType type) |
| + : WebContentsObserver(web_contents), |
| + audio_focus_manager_(audio_focus_manager) { |
|
Zhiqiang Zhang (Slow)
2016/05/11 15:57:32
You forgot type_(type) here :)
|
| +} |
| + |
| +AudioFocusManager::AudioFocusType AudioFocusManager::AudioFocusEntry::type() const { |
| + return type_; |
| +} |
| + |
| +void AudioFocusManager::AudioFocusEntry::set_type(AudioFocusType type) { |
| + type_ = type; |
| +} |
| + |
| +void AudioFocusManager::AudioFocusEntry::WebContentsDestroyed() { |
| + audio_focus_manager_->OnWebContentsDestroyed(web_contents()); |
| + // |this| will be destroyed now. |
| +} |
| + |
| +// static |
| +AudioFocusManager* AudioFocusManager::GetInstance() { |
| + return base::Singleton<AudioFocusManager>::get(); |
| +} |
| + |
| +void AudioFocusManager::RequestAudioFocus( |
| + MediaSession* media_session, AudioFocusType type) { |
| + WebContents* web_contents = media_session->web_contents(); |
| + base::Optional<AudioFocusType> previous_type; |
| + |
| + auto it = entries_.find(web_contents); |
| + if (it != entries_.end()) { |
| + previous_type = it->second->type(); |
| + it->second->set_type(type); |
| + } else { |
| + entries_[web_contents] = std::unique_ptr<AudioFocusEntry>( |
| + new AudioFocusEntry(web_contents, this, type)); |
| + } |
| + |
| + if (previous_type && previous_type.value() != type) { |
| + switch (previous_type.value()) { |
| + case AudioFocusType::GainTransientMayDuck: |
| + MaybeStopDucking(); |
| + break; |
| + case AudioFocusType::Gain: |
| + RemoveFromFocusStack(web_contents); |
| + break; |
| + } |
| + } |
| + |
| + if (type == AudioFocusType::GainTransientMayDuck) { |
| + MaybeStartDucking(); |
| + return; |
| + } |
| + |
| + DCHECK(type == AudioFocusType::Gain); |
| + |
| + if (!focus_stack_.empty()) { |
| + // Already on top, it's a no-op. |
| + if (*focus_stack_.begin() == web_contents) |
| + return; |
| + |
| + MediaSession* other_session = MediaSession::Get(*focus_stack_.begin()); |
| + if (other_session->IsActive()) |
| + other_session->Suspend(MediaSession::SuspendType::SYSTEM); |
| + |
| + RemoveFromFocusStack(web_contents); |
| + } |
| + |
| + focus_stack_.push_front(web_contents); |
| +} |
| + |
| +void AudioFocusManager::AbandonAudioFocus(MediaSession* media_session) { |
| + WebContents* web_contents = media_session->web_contents(); |
| + |
| + // We do not expect to receive an AbandonAudioFocus call for a session that |
| + // didn't ask for audio focus at some point. |
| + auto it = entries_.find(web_contents); |
| + DCHECK(it != entries_.end()); |
| + |
| + AudioFocusType type = it->second->type(); |
| + entries_.erase(web_contents); |
| + |
| + if (type == AudioFocusType::GainTransientMayDuck) { |
| + MaybeStopDucking(); |
| + return; |
| + } |
| + |
| + DCHECK(type == AudioFocusType::Gain); |
| + |
| + if (focus_stack_.empty()) |
| + return; |
| + |
| + // Something that doesn't have focus is closing. |
| + if (*focus_stack_.begin() != web_contents) { |
| + RemoveFromFocusStack(web_contents); |
| + return; |
| + } |
| + |
| + focus_stack_.erase(focus_stack_.begin()); |
| + if (!focus_stack_.empty()) |
| + MediaSession::Get(*focus_stack_.begin())->Resume(MediaSession::SuspendType::SYSTEM); |
|
Zhiqiang Zhang (Slow)
2016/05/11 15:57:32
On Android, we don't resume other sessions when ab
Zhiqiang Zhang (Slow)
2016/05/20 14:14:43
Sorry, no need to clear the stack here.
Our curren
|
| +} |
| + |
| +AudioFocusManager::AudioFocusManager() = default; |
| + |
| +AudioFocusManager::~AudioFocusManager() = default; |
| + |
| +void AudioFocusManager::OnWebContentsDestroyed(WebContents* web_contents) { |
| + entries_.erase(web_contents); |
| + RemoveFromFocusStack(web_contents); |
| +} |
| + |
| +void AudioFocusManager::MaybeStartDucking() const { |
| + if (TransientMayDuckEntriesCount() != 1) |
| + return; |
| + |
| + // TODO(mlamouri): add StartDuck to MediaSession. |
| + for (const auto& it : focus_stack_) |
| + MediaSession::Get(it)->SetVolumeMultiplier(kDuckingVolumeMultiplier); |
| +} |
| + |
| +void AudioFocusManager::MaybeStopDucking() const { |
| + if (TransientMayDuckEntriesCount() != 0) |
| + return; |
| + |
| + // TODO(mlamouri): add StopDuck to MediaSession. |
| + for (const auto& it : focus_stack_) |
| + MediaSession::Get(it)->SetVolumeMultiplier(kDefaultVolumeMultiplier); |
| +} |
| + |
| +int AudioFocusManager::TransientMayDuckEntriesCount() const { |
| + int count = 0; |
| + for (const auto& it : entries_) { |
| + if (it.second->type() != AudioFocusType::GainTransientMayDuck) |
| + continue; |
| + ++count; |
| + } |
| + return count; |
| +} |
| + |
| +void AudioFocusManager::RemoveFromFocusStack(WebContents* web_contents) { |
| + focus_stack_.remove_if([web_contents](WebContents* wc) -> bool { return wc == web_contents; }); |
| +} |
| + |
| +} // namespace content |