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