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 "base/memory/ptr_util.h" | |
7 #include "content/browser/media/session/media_session.h" | 8 #include "content/browser/media/session/media_session.h" |
8 #include "content/public/browser/web_contents.h" | 9 #include "content/public/browser/web_contents.h" |
9 | 10 |
10 namespace content { | 11 namespace content { |
11 | 12 |
12 AudioFocusManager::AudioFocusEntry::AudioFocusEntry( | 13 AudioFocusManager::AudioFocusEntry::AudioFocusEntry( |
13 WebContents* web_contents, | 14 WebContents* web_contents, |
14 AudioFocusManager* audio_focus_manager, | 15 AudioFocusManager* audio_focus_manager, |
15 AudioFocusType type) | 16 AudioFocusType type) |
16 : WebContentsObserver(web_contents), | 17 : WebContentsObserver(web_contents), |
17 audio_focus_manager_(audio_focus_manager) {} | 18 audio_focus_manager_(audio_focus_manager), |
19 type_(type) {} | |
18 | 20 |
19 AudioFocusManager::AudioFocusType | 21 AudioFocusManager::AudioFocusType |
20 AudioFocusManager::AudioFocusEntry::type() const { | 22 AudioFocusManager::AudioFocusEntry::type() const { |
21 return type_; | 23 return type_; |
22 } | 24 } |
23 | 25 |
24 void AudioFocusManager::AudioFocusEntry::WebContentsDestroyed() { | 26 void AudioFocusManager::AudioFocusEntry::WebContentsDestroyed() { |
25 audio_focus_manager_->OnWebContentsDestroyed(web_contents()); | 27 audio_focus_manager_->OnWebContentsDestroyed(web_contents()); |
26 // |this| will be destroyed now. | 28 // |this| will be destroyed now. |
27 } | 29 } |
28 | 30 |
29 // static | 31 // static |
30 AudioFocusManager* AudioFocusManager::GetInstance() { | 32 AudioFocusManager* AudioFocusManager::GetInstance() { |
31 return base::Singleton<AudioFocusManager>::get(); | 33 return base::Singleton<AudioFocusManager>::get(); |
32 } | 34 } |
33 | 35 |
34 void AudioFocusManager::RequestAudioFocus(MediaSession* media_session, | 36 void AudioFocusManager::RequestAudioFocus(MediaSession* media_session, |
35 AudioFocusType type) { | 37 AudioFocusType type) { |
36 WebContents* web_contents = media_session->web_contents(); | 38 WebContents* web_contents = media_session->web_contents(); |
37 | 39 |
40 if (!audio_focus_stack_.empty() && | |
41 audio_focus_stack_.back()->web_contents() == web_contents) | |
whywhat
2016/09/07 20:09:54
nit: use {} as the condition statement doesn't fit
Zhiqiang Zhang (Slow)
2016/09/08 14:18:26
Done.
| |
42 return; | |
whywhat
2016/09/07 20:09:54
don't you need to check that the AudioFocusType is
Zhiqiang Zhang (Slow)
2016/09/08 14:18:26
Done.
| |
43 | |
44 MaybeRemoveFocusEntry(web_contents); | |
45 | |
38 if (type == AudioFocusType::GainTransientMayDuck) { | 46 if (type == AudioFocusType::GainTransientMayDuck) { |
47 for (const auto& focus_entry : audio_focus_stack_) { | |
48 MediaSession::Get(focus_entry->web_contents())->StartDucking(); | |
49 } | |
50 } else { | |
51 for (const auto& focus_entry : audio_focus_stack_) { | |
52 MediaSession* session = MediaSession::Get(focus_entry->web_contents()); | |
53 if (session->IsActive()) | |
54 session->Suspend(MediaSession::SuspendType::SYSTEM); | |
55 } | |
56 } | |
57 if (!audio_focus_stack_.empty()) { | |
58 MediaSession::Get(audio_focus_stack_.back()->web_contents()) | |
whywhat
2016/09/07 20:09:54
shouldn't Suspend or StartDucking do that? I feel
Zhiqiang Zhang (Slow)
2016/09/08 14:18:26
I'm renaming the flag `is_on_top_` to `allow_peppe
| |
59 ->SetOnTop(false); | |
60 } | |
61 | |
62 audio_focus_stack_.push_back(base::MakeUnique<AudioFocusEntry>( | |
63 web_contents, this, type)); | |
64 MediaSession::Get(audio_focus_stack_.back()->web_contents())->StopDucking(); | |
whywhat
2016/09/07 20:09:54
How do we know it's not suspended but ducking?
Zhiqiang Zhang (Slow)
2016/09/08 14:18:26
If it's suspended, then this will be no-op, since
| |
65 MediaSession::Get(audio_focus_stack_.back()->web_contents())->SetOnTop(true); | |
66 } | |
67 | |
68 void AudioFocusManager::AbandonAudioFocus(MediaSession* media_session) { | |
69 WebContents* web_contents = media_session->web_contents(); | |
70 | |
71 if (audio_focus_stack_.back()->web_contents() != web_contents) { | |
39 MaybeRemoveFocusEntry(web_contents); | 72 MaybeRemoveFocusEntry(web_contents); |
40 transient_entries_[web_contents].reset( | |
41 new AudioFocusEntry(web_contents, this, type)); | |
42 MaybeStartDucking(); | |
43 return; | 73 return; |
44 } | 74 } |
45 | 75 |
46 DCHECK(type == AudioFocusType::Gain); | 76 MediaSession::Get(audio_focus_stack_.back()->web_contents())->SetOnTop(false); |
47 RequestAudioFocusGain(web_contents); | 77 audio_focus_stack_.pop_back(); |
48 } | 78 if (audio_focus_stack_.empty()) |
79 return; | |
49 | 80 |
50 void AudioFocusManager::AbandonAudioFocus(MediaSession* media_session) { | 81 MediaSession::Get(audio_focus_stack_.back()->web_contents())->SetOnTop(true); |
51 AbandonAudioFocusInternal(media_session->web_contents()); | 82 // Only try to unduck the new MediaSession on top. The session might be still |
83 // inactive but it will not be resumed (so it does surprise the user). | |
84 MediaSession::Get(audio_focus_stack_.back()->web_contents())->StopDucking(); | |
52 } | 85 } |
53 | 86 |
54 AudioFocusManager::AudioFocusManager() = default; | 87 AudioFocusManager::AudioFocusManager() = default; |
55 | 88 |
56 AudioFocusManager::~AudioFocusManager() = default; | 89 AudioFocusManager::~AudioFocusManager() = default; |
57 | 90 |
58 void AudioFocusManager::RequestAudioFocusGain(WebContents* web_contents) { | |
59 MaybeRemoveTransientEntry(web_contents); | |
60 | |
61 if (focus_entry_) { | |
62 if (focus_entry_->web_contents() == web_contents) | |
63 return; | |
64 | |
65 MediaSession* other_session = | |
66 MediaSession::Get(focus_entry_->web_contents()); | |
67 if (other_session->IsActive()) | |
68 other_session->Suspend(MediaSession::SuspendType::SYSTEM); | |
69 } | |
70 | |
71 focus_entry_.reset( | |
72 new AudioFocusEntry(web_contents, this, AudioFocusType::Gain)); | |
73 MaybeStartDucking(); | |
74 } | |
75 | |
76 void AudioFocusManager::OnWebContentsDestroyed(WebContents* web_contents) { | 91 void AudioFocusManager::OnWebContentsDestroyed(WebContents* web_contents) { |
77 AbandonAudioFocusInternal(web_contents); | 92 AbandonAudioFocus(MediaSession::Get(web_contents)); |
78 } | |
79 | |
80 void AudioFocusManager::AbandonAudioFocusInternal(WebContents* web_contents) { | |
81 MaybeRemoveTransientEntry(web_contents); | |
82 MaybeRemoveFocusEntry(web_contents); | |
83 } | |
84 | |
85 void AudioFocusManager::MaybeStartDucking() const { | |
86 if (TransientMayDuckEntriesCount() != 1 || !focus_entry_) | |
87 return; | |
88 | |
89 MediaSession::Get(focus_entry_->web_contents())->StartDucking(); | |
90 } | |
91 | |
92 void AudioFocusManager::MaybeStopDucking() const { | |
93 if (TransientMayDuckEntriesCount() != 0 || !focus_entry_) | |
94 return; | |
95 | |
96 MediaSession::Get(focus_entry_->web_contents())->StopDucking(); | |
97 } | |
98 | |
99 int AudioFocusManager::TransientMayDuckEntriesCount() const { | |
100 return transient_entries_.size(); | |
101 } | |
102 | |
103 void AudioFocusManager::MaybeRemoveTransientEntry(WebContents* web_contents) { | |
104 transient_entries_.erase(web_contents); | |
105 MaybeStopDucking(); | |
106 } | 93 } |
107 | 94 |
108 void AudioFocusManager::MaybeRemoveFocusEntry(WebContents* web_contents) { | 95 void AudioFocusManager::MaybeRemoveFocusEntry(WebContents* web_contents) { |
109 if (focus_entry_ && focus_entry_->web_contents() == web_contents) { | 96 for (auto iter = audio_focus_stack_.begin(); iter != audio_focus_stack_.end(); |
110 MediaSession::Get(focus_entry_->web_contents())->StopDucking(); | 97 ++iter) { |
111 focus_entry_.reset(); | 98 if (web_contents == (*iter)->web_contents()) { |
99 audio_focus_stack_.erase(iter); | |
100 return; | |
101 } | |
112 } | 102 } |
113 } | 103 } |
114 | 104 |
115 } // namespace content | 105 } // namespace content |
OLD | NEW |