| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CONTENT_BROWSER_MEDIA_SESSION_AUDIO_FOCUS_MANAGER_H_ |
| 6 #define CONTENT_BROWSER_MEDIA_SESSION_AUDIO_FOCUS_MANAGER_H_ |
| 7 |
| 8 #include <unordered_map> |
| 9 |
| 10 #include "base/memory/singleton.h" |
| 11 #include "content/common/content_export.h" |
| 12 #include "content/public/browser/web_contents_observer.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 class MediaSession; |
| 17 class WebContents; |
| 18 |
| 19 class CONTENT_EXPORT AudioFocusManager { |
| 20 public: |
| 21 enum class AudioFocusType { |
| 22 Gain, |
| 23 GainTransientMayDuck, |
| 24 }; |
| 25 |
| 26 // Returns Chromium's internal AudioFocusManager. |
| 27 static AudioFocusManager* GetInstance(); |
| 28 |
| 29 void RequestAudioFocus(MediaSession* media_session, AudioFocusType type); |
| 30 |
| 31 void AbandonAudioFocus(MediaSession* media_session); |
| 32 |
| 33 private: |
| 34 friend struct base::DefaultSingletonTraits<AudioFocusManager>; |
| 35 friend class AudioFocusManagerTest; |
| 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 |
| 58 AudioFocusManager(); |
| 59 ~AudioFocusManager(); |
| 60 |
| 61 void OnWebContentsDestroyed(WebContents* web_contents); |
| 62 |
| 63 // Internal usage of AbandonAudioFocus using WebContents. |
| 64 void AbandonAudioFocusInternal(WebContents* web_contents); |
| 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_; |
| 92 }; |
| 93 |
| 94 } // namespace content |
| 95 |
| 96 #endif // CONTENT_BROWSER_MEDIA_SESSION_AUDIO_FOCUS_MANAGER_H_ |
| OLD | NEW |