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 #ifndef CONTENT_BROWSER_MEDIA_SESSION_AUDIO_FOCUS_MANAGER_H_ | 5 #ifndef CONTENT_BROWSER_MEDIA_SESSION_AUDIO_FOCUS_MANAGER_H_ |
6 #define CONTENT_BROWSER_MEDIA_SESSION_AUDIO_FOCUS_MANAGER_H_ | 6 #define CONTENT_BROWSER_MEDIA_SESSION_AUDIO_FOCUS_MANAGER_H_ |
7 | 7 |
8 #include <list> | |
9 #include <unordered_map> | 8 #include <unordered_map> |
10 | 9 |
11 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
12 #include "content/common/content_export.h" | 11 #include "content/common/content_export.h" |
13 #include "content/public/browser/web_contents_observer.h" | 12 #include "content/public/browser/web_contents_observer.h" |
14 | 13 |
15 namespace content { | 14 namespace content { |
16 | 15 |
17 class MediaSession; | 16 class MediaSession; |
| 17 class WebContents; |
18 | 18 |
19 class CONTENT_EXPORT AudioFocusManager { | 19 class CONTENT_EXPORT AudioFocusManager { |
20 public: | 20 public: |
21 enum class AudioFocusType { | 21 enum class AudioFocusType { |
22 Gain, | 22 Gain, |
23 GainTransientMayDuck, | 23 GainTransientMayDuck, |
24 }; | 24 }; |
25 | 25 |
26 // Returns Chromium's internal AudioFocusManager. | 26 // Returns Chromium's internal AudioFocusManager. |
27 static AudioFocusManager* GetInstance(); | 27 static AudioFocusManager* GetInstance(); |
28 | 28 |
29 void RequestAudioFocus(MediaSession* media_session, AudioFocusType type); | 29 void RequestAudioFocus(MediaSession* media_session, AudioFocusType type); |
30 | 30 |
31 void AbandonAudioFocus(MediaSession* media_session); | 31 void AbandonAudioFocus(MediaSession* media_session); |
32 | 32 |
33 private: | 33 private: |
34 friend struct base::DefaultSingletonTraits<AudioFocusManager>; | 34 friend struct base::DefaultSingletonTraits<AudioFocusManager>; |
35 friend class AudioFocusManagerTest; | 35 friend class AudioFocusManagerTest; |
36 | 36 |
| 37 // TODO(mlamouri): in order to allow multiple MediaSession per WebContents, we |
| 38 // will have to keep track of MediaSession's. Though, we can easily keep track |
| 39 // of WebContents' life time right now but not MediaSession's. |
| 40 class AudioFocusEntry : public WebContentsObserver { |
| 41 public: |
| 42 AudioFocusEntry(WebContents* web_contents, |
| 43 AudioFocusManager* audio_focus_manager, |
| 44 AudioFocusType type); |
| 45 |
| 46 AudioFocusType type() const; |
| 47 |
| 48 private: |
| 49 // WebContentsObserver implementation. |
| 50 void WebContentsDestroyed() override; |
| 51 |
| 52 AudioFocusManager* audio_focus_manager_; // Owns |this|. |
| 53 AudioFocusType type_; |
| 54 |
| 55 DISALLOW_COPY_AND_ASSIGN(AudioFocusEntry); |
| 56 }; |
| 57 |
37 AudioFocusManager(); | 58 AudioFocusManager(); |
38 ~AudioFocusManager(); | 59 ~AudioFocusManager(); |
39 | 60 |
40 void MaybeRemoveFocusEntry(MediaSession* media_session); | 61 void OnWebContentsDestroyed(WebContents* web_contents); |
41 | 62 |
42 // Weak reference of managed MediaSessions. A MediaSession must abandon audio | 63 // Internal usage of AbandonAudioFocus using WebContents. |
43 // foucs before its destruction. | 64 void AbandonAudioFocusInternal(WebContents* web_contents); |
44 std::list<MediaSession*> audio_focus_stack_; | 65 |
| 66 // This method is meant to be called when a new session is of type |
| 67 // GainTransientMayDuck. If it is the first one, other clients will be asked |
| 68 // to duck. |
| 69 void MaybeStartDucking() const; |
| 70 |
| 71 // This method is meant to be called when a session is no longer of type |
| 72 // GainTransientMayDuck. If it was the last one, other clients will be asked |
| 73 // to no longer duck. |
| 74 void MaybeStopDucking() const; |
| 75 |
| 76 // Returns how many sessions require current audio focused session to duck. |
| 77 int TransientMayDuckEntriesCount() const; |
| 78 |
| 79 // Internal method to request audio focus of type AudioFocusType::Gain. |
| 80 void RequestAudioFocusGain(WebContents* web_contents); |
| 81 |
| 82 // Removes the entry associated with |web_contents| from the |
| 83 // |transient_entries_| if there is one. |
| 84 void MaybeRemoveTransientEntry(WebContents* web_contents); |
| 85 |
| 86 // Removes the focused session if it is associated with |web_contents|. |
| 87 void MaybeRemoveFocusEntry(WebContents* web_contents); |
| 88 |
| 89 std::unordered_map<WebContents*, std::unique_ptr<AudioFocusEntry>> |
| 90 transient_entries_; |
| 91 std::unique_ptr<AudioFocusEntry> focus_entry_; |
45 }; | 92 }; |
46 | 93 |
47 } // namespace content | 94 } // namespace content |
48 | 95 |
49 #endif // CONTENT_BROWSER_MEDIA_SESSION_AUDIO_FOCUS_MANAGER_H_ | 96 #endif // CONTENT_BROWSER_MEDIA_SESSION_AUDIO_FOCUS_MANAGER_H_ |
OLD | NEW |