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 |
26 MediaSession* AudioFocusManager::AudioFocusEntry::ToMediaSession() const { | |
27 return MediaSession::Get(web_contents()); | |
28 } | |
29 | |
24 void AudioFocusManager::AudioFocusEntry::WebContentsDestroyed() { | 30 void AudioFocusManager::AudioFocusEntry::WebContentsDestroyed() { |
25 audio_focus_manager_->OnWebContentsDestroyed(web_contents()); | 31 audio_focus_manager_->OnWebContentsDestroyed(web_contents()); |
26 // |this| will be destroyed now. | 32 // |this| will be destroyed now. |
27 } | 33 } |
28 | 34 |
29 // static | 35 // static |
30 AudioFocusManager* AudioFocusManager::GetInstance() { | 36 AudioFocusManager* AudioFocusManager::GetInstance() { |
31 return base::Singleton<AudioFocusManager>::get(); | 37 return base::Singleton<AudioFocusManager>::get(); |
32 } | 38 } |
33 | 39 |
34 void AudioFocusManager::RequestAudioFocus(MediaSession* media_session, | 40 void AudioFocusManager::RequestAudioFocus(MediaSession* media_session, |
35 AudioFocusType type) { | 41 AudioFocusType type) { |
36 WebContents* web_contents = media_session->web_contents(); | 42 WebContents* web_contents = media_session->web_contents(); |
37 | 43 |
38 if (type == AudioFocusType::GainTransientMayDuck) { | 44 if (!audio_focus_stack_.empty() && |
39 MaybeRemoveFocusEntry(web_contents); | 45 audio_focus_stack_.back()->web_contents() == web_contents && |
40 transient_entries_[web_contents].reset( | 46 audio_focus_stack_.back()->type() == type && |
Zhiqiang Zhang (Slow)
2016/09/08 14:18:26
Maybe we could access |audio_focus_type_| from Med
| |
41 new AudioFocusEntry(web_contents, this, type)); | 47 audio_focus_stack_.back()->ToMediaSession()->IsActive()) { |
42 MaybeStartDucking(); | |
43 return; | 48 return; |
44 } | 49 } |
45 | 50 |
46 DCHECK(type == AudioFocusType::Gain); | 51 MaybeRemoveFocusEntry(web_contents); |
47 RequestAudioFocusGain(web_contents); | 52 |
53 if (type == AudioFocusType::GainTransientMayDuck) { | |
54 for (const auto& focus_entry : audio_focus_stack_) { | |
55 focus_entry->ToMediaSession()->StartDucking(); | |
56 } | |
57 } else { | |
58 for (const auto& focus_entry : audio_focus_stack_) { | |
Zhiqiang Zhang (Slow)
2016/09/08 14:18:26
These for-loops visiting the full stack might be s
| |
59 MediaSession* session = focus_entry->ToMediaSession(); | |
60 if (session->IsActive()) | |
61 session->Suspend(MediaSession::SuspendType::SYSTEM); | |
62 session->DisallowPepperOverrideDucking(); | |
63 } | |
64 } | |
65 | |
66 audio_focus_stack_.push_back(base::MakeUnique<AudioFocusEntry>( | |
67 web_contents, this, type)); | |
68 audio_focus_stack_.back()->ToMediaSession()->StopDucking(); | |
69 audio_focus_stack_.back()->ToMediaSession()->AllowPepperOverrideDucking(); | |
48 } | 70 } |
49 | 71 |
50 void AudioFocusManager::AbandonAudioFocus(MediaSession* media_session) { | 72 void AudioFocusManager::AbandonAudioFocus(MediaSession* media_session) { |
51 AbandonAudioFocusInternal(media_session->web_contents()); | 73 WebContents* web_contents = media_session->web_contents(); |
74 | |
75 if (audio_focus_stack_.back()->web_contents() != web_contents) { | |
76 MaybeRemoveFocusEntry(web_contents); | |
77 return; | |
78 } | |
79 | |
80 audio_focus_stack_.back()->ToMediaSession()->DisallowPepperOverrideDucking(); | |
81 audio_focus_stack_.pop_back(); | |
82 if (audio_focus_stack_.empty()) | |
83 return; | |
84 | |
85 // Allow the top-most MediaSession having Pepper to unduck pepper event it's | |
86 // not active. | |
87 for (auto iter = audio_focus_stack_.rbegin(); | |
88 iter != audio_focus_stack_.rend(); ++iter) { | |
89 if ((*iter)->ToMediaSession()->HasPepper()) { | |
90 (*iter)->ToMediaSession()->AllowPepperOverrideDucking(); | |
91 break; | |
92 } | |
93 } | |
94 // Only try to unduck the new MediaSession on top. The session might be still | |
95 // inactive but it will not be resumed (so it does surprise the user). | |
96 audio_focus_stack_.back()->ToMediaSession()->StopDucking(); | |
52 } | 97 } |
53 | 98 |
54 AudioFocusManager::AudioFocusManager() = default; | 99 AudioFocusManager::AudioFocusManager() = default; |
55 | 100 |
56 AudioFocusManager::~AudioFocusManager() = default; | 101 AudioFocusManager::~AudioFocusManager() = default; |
57 | 102 |
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) { | 103 void AudioFocusManager::OnWebContentsDestroyed(WebContents* web_contents) { |
77 AbandonAudioFocusInternal(web_contents); | 104 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 } | 105 } |
107 | 106 |
108 void AudioFocusManager::MaybeRemoveFocusEntry(WebContents* web_contents) { | 107 void AudioFocusManager::MaybeRemoveFocusEntry(WebContents* web_contents) { |
109 if (focus_entry_ && focus_entry_->web_contents() == web_contents) { | 108 for (auto iter = audio_focus_stack_.begin(); iter != audio_focus_stack_.end(); |
110 MediaSession::Get(focus_entry_->web_contents())->StopDucking(); | 109 ++iter) { |
111 focus_entry_.reset(); | 110 if (web_contents == (*iter)->web_contents()) { |
111 audio_focus_stack_.erase(iter); | |
112 return; | |
113 } | |
112 } | 114 } |
113 } | 115 } |
114 | 116 |
115 } // namespace content | 117 } // namespace content |
OLD | NEW |