OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_MEDIA_SESSION_H_ | 5 #ifndef CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_ |
6 #define CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_ | 6 #define CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include "base/callback_list.h" | 10 #include "base/callback_list.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 public: | 51 public: |
52 enum class SuspendType { | 52 enum class SuspendType { |
53 // Suspended by the system because a transient sound needs to be played. | 53 // Suspended by the system because a transient sound needs to be played. |
54 SYSTEM, | 54 SYSTEM, |
55 // Suspended by the UI. | 55 // Suspended by the UI. |
56 UI, | 56 UI, |
57 // Suspended by the page via script or user interaction. | 57 // Suspended by the page via script or user interaction. |
58 CONTENT, | 58 CONTENT, |
59 }; | 59 }; |
60 | 60 |
61 // Only visible to tests. | |
62 enum class State { | |
63 ACTIVE, | |
64 SUSPENDED, | |
65 INACTIVE | |
66 }; | |
67 | |
68 // Returns the MediaSession associated to this WebContents. Creates one if | 61 // Returns the MediaSession associated to this WebContents. Creates one if |
69 // none is currently available. | 62 // none is currently available. |
70 CONTENT_EXPORT static MediaSession* Get(WebContents* web_contents); | 63 CONTENT_EXPORT static MediaSession* Get(WebContents* web_contents); |
71 | 64 |
72 ~MediaSession() override; | 65 ~MediaSession() override; |
73 | 66 |
74 void SetMetadata(const base::Optional<MediaMetadata>& metadata); | 67 void SetMetadata(const base::Optional<MediaMetadata>& metadata); |
75 const base::Optional<MediaMetadata>& metadata() const { return metadata_; } | 68 const base::Optional<MediaMetadata>& metadata() const { return metadata_; } |
76 | 69 |
77 // Adds the given player to the current media session. Returns whether the | 70 // Adds the given player to the current media session. Returns whether the |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 CONTENT_EXPORT bool IsControllable() const; | 117 CONTENT_EXPORT bool IsControllable() const; |
125 | 118 |
126 // Returns if the session is currently active. | 119 // Returns if the session is currently active. |
127 CONTENT_EXPORT bool IsActive() const; | 120 CONTENT_EXPORT bool IsActive() const; |
128 | 121 |
129 // Returns if the session is currently suspended. | 122 // Returns if the session is currently suspended. |
130 // TODO(mlamouri): IsSuspended() below checks if the state is not ACTIVE | 123 // TODO(mlamouri): IsSuspended() below checks if the state is not ACTIVE |
131 // instead of checking if the state is SUSPENDED. In order to not have to | 124 // instead of checking if the state is SUSPENDED. In order to not have to |
132 // change all the callers and make the current refactoring ridiculously huge, | 125 // change all the callers and make the current refactoring ridiculously huge, |
133 // this method is introduced temporarily and will be removed later. | 126 // this method is introduced temporarily and will be removed later. |
134 CONTENT_EXPORT bool IsReallySuspended() const; | 127 bool IsReallySuspended() const; |
135 | 128 |
136 // Returns if the session is currently suspended or inactive. | 129 // Returns if the session is currently suspended or inactive. |
137 CONTENT_EXPORT bool IsSuspended() const; | 130 CONTENT_EXPORT bool IsSuspended() const; |
138 | 131 |
139 // Returns the audio focus type. The type is updated everytime after the | |
140 // session requests audio focus. | |
141 CONTENT_EXPORT AudioFocusManager::AudioFocusType audio_focus_type() const { | |
142 return audio_focus_type_; | |
143 } | |
144 | |
145 // Returns whether the session has Pepper instances. | |
146 bool HasPepper() const; | |
147 | |
148 // WebContentsObserver implementation | |
149 void WebContentsDestroyed() override; | |
150 | |
151 private: | 132 private: |
152 friend class content::WebContentsUserData<MediaSession>; | 133 friend class content::WebContentsUserData<MediaSession>; |
153 friend class ::MediaSessionBrowserTest; | 134 friend class ::MediaSessionBrowserTest; |
154 friend class content::MediaSessionVisibilityBrowserTest; | 135 friend class content::MediaSessionVisibilityBrowserTest; |
155 friend class content::AudioFocusManagerTest; | 136 friend class content::AudioFocusManagerTest; |
156 friend class content::MediaSessionStateObserver; | 137 friend class content::MediaSessionStateObserver; |
157 | 138 |
158 CONTENT_EXPORT void SetDelegateForTests( | 139 CONTENT_EXPORT void SetDelegateForTests( |
159 std::unique_ptr<MediaSessionDelegate> delegate); | 140 std::unique_ptr<MediaSessionDelegate> delegate); |
160 CONTENT_EXPORT bool IsActiveForTest() const; | 141 CONTENT_EXPORT bool IsActiveForTest() const; |
| 142 CONTENT_EXPORT AudioFocusManager::AudioFocusType audio_focus_type_for_test() |
| 143 const; |
161 CONTENT_EXPORT void RemoveAllPlayersForTest(); | 144 CONTENT_EXPORT void RemoveAllPlayersForTest(); |
162 CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test(); | 145 CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test(); |
163 | 146 |
| 147 enum class State { |
| 148 ACTIVE, |
| 149 SUSPENDED, |
| 150 INACTIVE |
| 151 }; |
| 152 |
164 // Representation of a player for the MediaSession. | 153 // Representation of a player for the MediaSession. |
165 struct PlayerIdentifier { | 154 struct PlayerIdentifier { |
166 PlayerIdentifier(MediaSessionObserver* observer, int player_id); | 155 PlayerIdentifier(MediaSessionObserver* observer, int player_id); |
167 PlayerIdentifier(const PlayerIdentifier&) = default; | 156 PlayerIdentifier(const PlayerIdentifier&) = default; |
168 | 157 |
169 void operator=(const PlayerIdentifier&) = delete; | 158 void operator=(const PlayerIdentifier&) = delete; |
170 bool operator==(const PlayerIdentifier& player_identifier) const; | 159 bool operator==(const PlayerIdentifier& player_identifier) const; |
171 | 160 |
172 // Hash operator for base::hash_map<>. | 161 // Hash operator for base::hash_map<>. |
173 struct Hash { | 162 struct Hash { |
174 size_t operator()(const PlayerIdentifier& player_identifier) const; | 163 size_t operator()(const PlayerIdentifier& player_identifier) const; |
175 }; | 164 }; |
176 | 165 |
177 MediaSessionObserver* observer; | 166 MediaSessionObserver* observer; |
178 int player_id; | 167 int player_id; |
179 }; | 168 }; |
180 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>; | 169 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>; |
181 using StateChangedCallback = base::Callback<void(State)>; | 170 using StateChangedCallback = base::Callback<void(State)>; |
182 | 171 |
183 CONTENT_EXPORT explicit MediaSession(WebContents* web_contents); | 172 CONTENT_EXPORT explicit MediaSession(WebContents* web_contents); |
184 | 173 |
185 void Initialize(); | 174 void Initialize(); |
186 | 175 |
187 CONTENT_EXPORT void OnSuspendInternal(SuspendType suspend_type, | 176 CONTENT_EXPORT void OnSuspendInternal(SuspendType suspend_type, |
188 State new_state); | 177 State new_state); |
189 CONTENT_EXPORT void OnResumeInternal(SuspendType suspend_type); | 178 CONTENT_EXPORT void OnResumeInternal(SuspendType suspend_type); |
190 | 179 |
191 // Requests audio focus to the MediaSessionDelegate. | 180 // Requests audio focus to the MediaSessionDelegate. |
192 // Returns whether the request was granted. | 181 // Returns whether the request was granted. |
193 CONTENT_EXPORT bool RequestSystemAudioFocus( | 182 bool RequestSystemAudioFocus( |
194 AudioFocusManager::AudioFocusType audio_focus_type); | 183 AudioFocusManager::AudioFocusType audio_focus_type); |
195 | 184 |
196 // To be called after a call to AbandonAudioFocus() in order request the | 185 // To be called after a call to AbandonAudioFocus() in order request the |
197 // delegate to abandon the audio focus. | 186 // delegate to abandon the audio focus. |
198 CONTENT_EXPORT void AbandonSystemAudioFocusIfNeeded(); | 187 void AbandonSystemAudioFocusIfNeeded(); |
199 | 188 |
200 // Notifies WebContents about the state change of the media session. | 189 // Notifies WebContents about the state change of the media session. |
201 void UpdateWebContents(); | 190 void UpdateWebContents(); |
202 | 191 |
203 // Internal method that should be used instead of setting audio_focus_state_. | 192 // Internal method that should be used instead of setting audio_focus_state_. |
204 // It sets audio_focus_state_ and notifies observers about the state change. | 193 // It sets audio_focus_state_ and notifies observers about the state change. |
205 void SetAudioFocusState(State audio_focus_state); | 194 void SetAudioFocusState(State audio_focus_state); |
206 | 195 |
207 // Update the volume multiplier when ducking state changes. | 196 // Update the volume multiplier when ducking state changes. |
208 void UpdateVolumeMultiplier(); | 197 void UpdateVolumeMultiplier(); |
209 | 198 |
210 // Get the volume multiplier, which depends on whether the media session is | 199 // Get the volume multiplier, which depends on whether the media session is |
211 // ducking. | 200 // ducking. |
212 double GetVolumeMultiplier() const; | 201 double GetVolumeMultiplier() const; |
213 | 202 |
214 // Registers a MediaSession state change callback. | 203 // Registers a MediaSession state change callback. |
215 CONTENT_EXPORT std::unique_ptr<base::CallbackList<void(State)>::Subscription> | 204 CONTENT_EXPORT std::unique_ptr<base::CallbackList<void(State)>::Subscription> |
216 RegisterMediaSessionStateChangedCallbackForTest( | 205 RegisterMediaSessionStateChangedCallbackForTest( |
217 const StateChangedCallback& cb); | 206 const StateChangedCallback& cb); |
218 | 207 |
219 CONTENT_EXPORT bool AddPepperPlayer(MediaSessionObserver* observer, | |
220 int player_id); | |
221 | |
222 std::unique_ptr<MediaSessionDelegate> delegate_; | 208 std::unique_ptr<MediaSessionDelegate> delegate_; |
223 PlayersMap players_; | 209 PlayersMap players_; |
224 PlayersMap pepper_players_; | |
225 | 210 |
226 State audio_focus_state_; | 211 State audio_focus_state_; |
227 SuspendType suspend_type_; | 212 SuspendType suspend_type_; |
228 AudioFocusManager::AudioFocusType audio_focus_type_; | 213 AudioFocusManager::AudioFocusType audio_focus_type_; |
229 | 214 |
230 MediaSessionUmaHelper uma_helper_; | 215 MediaSessionUmaHelper uma_helper_; |
231 | 216 |
232 // The ducking state of this media session. The initial value is |false|, and | 217 // The ducking state of this media session. The initial value is |false|, and |
233 // is set to |true| after StartDucking(), and will be set to |false| after | 218 // is set to |true| after StartDucking(), and will be set to |false| after |
234 // StopDucking(). | 219 // StopDucking(). |
235 bool is_ducking_; | 220 bool is_ducking_; |
236 | 221 |
237 base::Optional<MediaMetadata> metadata_; | 222 base::Optional<MediaMetadata> metadata_; |
238 base::CallbackList<void(State)> media_session_state_listeners_; | 223 base::CallbackList<void(State)> media_session_state_listeners_; |
239 | 224 |
240 DISALLOW_COPY_AND_ASSIGN(MediaSession); | 225 DISALLOW_COPY_AND_ASSIGN(MediaSession); |
241 }; | 226 }; |
242 | 227 |
243 } // namespace content | 228 } // namespace content |
244 | 229 |
245 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_ | 230 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_ |
OLD | NEW |