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" |
11 #include "base/id_map.h" | 11 #include "base/id_map.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "content/browser/media/session/audio_focus_manager.h" | |
13 #include "content/browser/media/session/media_session_uma_helper.h" | 14 #include "content/browser/media/session/media_session_uma_helper.h" |
14 #include "content/common/content_export.h" | 15 #include "content/common/content_export.h" |
15 #include "content/public/browser/web_contents_observer.h" | 16 #include "content/public/browser/web_contents_observer.h" |
16 #include "content/public/browser/web_contents_user_data.h" | 17 #include "content/public/browser/web_contents_user_data.h" |
17 #include "content/public/common/media_metadata.h" | 18 #include "content/public/common/media_metadata.h" |
18 | 19 |
19 class MediaSessionBrowserTest; | 20 class MediaSessionBrowserTest; |
20 | 21 |
22 namespace media { | |
23 | |
whywhat
2016/08/18 20:33:42
nit: remove the blank line
Zhiqiang Zhang (Slow)
2016/08/19 15:05:55
Done.
| |
24 enum class MediaContentType; | |
25 } | |
whywhat
2016/08/18 20:33:42
nit: add the // namespace comment
Zhiqiang Zhang (Slow)
2016/08/19 15:05:55
Done.
| |
26 | |
21 namespace content { | 27 namespace content { |
22 | 28 |
23 class AudioFocusManagerTest; | 29 class AudioFocusManagerTest; |
24 class MediaSessionDelegate; | 30 class MediaSessionDelegate; |
25 class MediaSessionObserver; | 31 class MediaSessionObserver; |
26 class MediaSessionStateObserver; | 32 class MediaSessionStateObserver; |
27 class MediaSessionVisibilityBrowserTest; | 33 class MediaSessionVisibilityBrowserTest; |
28 | 34 |
29 // MediaSession manages the media session and audio focus for a given | 35 // MediaSession manages the media session and audio focus for a given |
30 // WebContents. It is requesting the audio focus, pausing when requested by the | 36 // WebContents. It is requesting the audio focus, pausing when requested by the |
31 // system and dropping it on demand. | 37 // system and dropping it on demand. |
32 // The audio focus can be of two types: Transient or Content. A Transient audio | 38 // The audio focus can be of two types: Transient or Content. A Transient audio |
33 // focus will allow other players to duck instead of pausing and will be | 39 // focus will allow other players to duck instead of pausing and will be |
34 // declared as temporary to the system. A Content audio focus will not be | 40 // declared as temporary to the system. A Content audio focus will not be |
35 // declared as temporary and will not allow other players to duck. If a given | 41 // declared as temporary and will not allow other players to duck. If a given |
36 // WebContents can only have one audio focus at a time, it will be Content in | 42 // WebContents can only have one audio focus at a time, it will be Content in |
37 // case of Transient and Content audio focus are both requested. | 43 // case of Transient and Content audio focus are both requested. |
38 // TODO(thakis,mlamouri): MediaSession isn't CONTENT_EXPORT'd because it creates | 44 // TODO(thakis,mlamouri): MediaSession isn't CONTENT_EXPORT'd because it creates |
39 // complicated build issues with WebContentsUserData being a non-exported | 45 // complicated build issues with WebContentsUserData being a non-exported |
40 // template, see htttps://crbug.com/589840. As a result, the class uses | 46 // template, see htttps://crbug.com/589840. As a result, the class uses |
41 // CONTENT_EXPORT for methods that are being used from tests. CONTENT_EXPORT | 47 // CONTENT_EXPORT for methods that are being used from tests. CONTENT_EXPORT |
42 // should be moved back to the class when the Windows build will work with it. | 48 // should be moved back to the class when the Windows build will work with it. |
43 class MediaSession : public WebContentsObserver, | 49 class MediaSession : public WebContentsObserver, |
44 protected WebContentsUserData<MediaSession> { | 50 protected WebContentsUserData<MediaSession> { |
45 public: | 51 public: |
46 enum class Type { | |
47 Content, | |
48 Transient | |
49 }; | |
50 | |
51 enum class SuspendType { | 52 enum class SuspendType { |
52 // 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. |
53 SYSTEM, | 54 SYSTEM, |
54 // Suspended by the UI. | 55 // Suspended by the UI. |
55 UI, | 56 UI, |
56 // Suspended by the page via script or user interaction. | 57 // Suspended by the page via script or user interaction. |
57 CONTENT, | 58 CONTENT, |
58 }; | 59 }; |
59 | 60 |
60 // Returns the MediaSession associated to this WebContents. Creates one if | 61 // Returns the MediaSession associated to this WebContents. Creates one if |
61 // none is currently available. | 62 // none is currently available. |
62 CONTENT_EXPORT static MediaSession* Get(WebContents* web_contents); | 63 CONTENT_EXPORT static MediaSession* Get(WebContents* web_contents); |
63 | 64 |
64 ~MediaSession() override; | 65 ~MediaSession() override; |
65 | 66 |
66 void SetMetadata(const MediaMetadata& metadata); | 67 void SetMetadata(const MediaMetadata& metadata); |
67 const MediaMetadata& metadata() const { return metadata_; } | 68 const MediaMetadata& metadata() const { return metadata_; } |
68 | 69 |
69 // 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 |
70 // player was successfully added. If it returns false, AddPlayer() should be | 71 // player was successfully added. If it returns false, AddPlayer() should be |
71 // called again later. | 72 // called again later. |
72 CONTENT_EXPORT bool AddPlayer(MediaSessionObserver* observer, | 73 CONTENT_EXPORT bool AddPlayer(MediaSessionObserver* observer, |
73 int player_id, Type type); | 74 int player_id, |
75 media::MediaContentType type); | |
74 | 76 |
75 // Removes the given player from the current media session. Abandons audio | 77 // Removes the given player from the current media session. Abandons audio |
76 // focus if that was the last player in the session. | 78 // focus if that was the last player in the session. |
77 CONTENT_EXPORT void RemovePlayer(MediaSessionObserver* observer, | 79 CONTENT_EXPORT void RemovePlayer(MediaSessionObserver* observer, |
78 int player_id); | 80 int player_id); |
79 | 81 |
80 // Removes all the players associated with |observer|. Abandons audio focus if | 82 // Removes all the players associated with |observer|. Abandons audio focus if |
81 // these were the last players in the session. | 83 // these were the last players in the session. |
82 CONTENT_EXPORT void RemovePlayers(MediaSessionObserver* observer); | 84 CONTENT_EXPORT void RemovePlayers(MediaSessionObserver* observer); |
83 | 85 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
125 private: | 127 private: |
126 friend class content::WebContentsUserData<MediaSession>; | 128 friend class content::WebContentsUserData<MediaSession>; |
127 friend class ::MediaSessionBrowserTest; | 129 friend class ::MediaSessionBrowserTest; |
128 friend class content::MediaSessionVisibilityBrowserTest; | 130 friend class content::MediaSessionVisibilityBrowserTest; |
129 friend class content::AudioFocusManagerTest; | 131 friend class content::AudioFocusManagerTest; |
130 friend class content::MediaSessionStateObserver; | 132 friend class content::MediaSessionStateObserver; |
131 | 133 |
132 CONTENT_EXPORT void SetDelegateForTests( | 134 CONTENT_EXPORT void SetDelegateForTests( |
133 std::unique_ptr<MediaSessionDelegate> delegate); | 135 std::unique_ptr<MediaSessionDelegate> delegate); |
134 CONTENT_EXPORT bool IsActiveForTest() const; | 136 CONTENT_EXPORT bool IsActiveForTest() const; |
135 CONTENT_EXPORT Type audio_focus_type_for_test() const; | 137 CONTENT_EXPORT AudioFocusManager::AudioFocusType audio_focus_type_for_test() |
138 const; | |
136 CONTENT_EXPORT void RemoveAllPlayersForTest(); | 139 CONTENT_EXPORT void RemoveAllPlayersForTest(); |
137 CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test(); | 140 CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test(); |
138 | 141 |
139 enum class State { | 142 enum class State { |
140 ACTIVE, | 143 ACTIVE, |
141 SUSPENDED, | 144 SUSPENDED, |
142 INACTIVE | 145 INACTIVE |
143 }; | 146 }; |
144 | 147 |
145 // Representation of a player for the MediaSession. | 148 // Representation of a player for the MediaSession. |
(...skipping 17 matching lines...) Expand all Loading... | |
163 | 166 |
164 CONTENT_EXPORT explicit MediaSession(WebContents* web_contents); | 167 CONTENT_EXPORT explicit MediaSession(WebContents* web_contents); |
165 | 168 |
166 void Initialize(); | 169 void Initialize(); |
167 | 170 |
168 CONTENT_EXPORT void OnSuspendInternal(SuspendType type, State new_state); | 171 CONTENT_EXPORT void OnSuspendInternal(SuspendType type, State new_state); |
169 CONTENT_EXPORT void OnResumeInternal(SuspendType type); | 172 CONTENT_EXPORT void OnResumeInternal(SuspendType type); |
170 | 173 |
171 // Requests audio focus to the MediaSessionDelegate. | 174 // Requests audio focus to the MediaSessionDelegate. |
172 // Returns whether the request was granted. | 175 // Returns whether the request was granted. |
173 bool RequestSystemAudioFocus(Type type); | 176 bool RequestSystemAudioFocus(AudioFocusManager::AudioFocusType type); |
174 | 177 |
175 // To be called after a call to AbandonAudioFocus() in order request the | 178 // To be called after a call to AbandonAudioFocus() in order request the |
176 // delegate to abandon the audio focus. | 179 // delegate to abandon the audio focus. |
177 void AbandonSystemAudioFocusIfNeeded(); | 180 void AbandonSystemAudioFocusIfNeeded(); |
178 | 181 |
179 // Notifies WebContents about the state change of the media session. | 182 // Notifies WebContents about the state change of the media session. |
180 void UpdateWebContents(); | 183 void UpdateWebContents(); |
181 | 184 |
182 // Internal method that should be used instead of setting audio_focus_state_. | 185 // Internal method that should be used instead of setting audio_focus_state_. |
183 // It sets audio_focus_state_ and notifies observers about the state change. | 186 // It sets audio_focus_state_ and notifies observers about the state change. |
184 void SetAudioFocusState(State audio_focus_state); | 187 void SetAudioFocusState(State audio_focus_state); |
185 | 188 |
186 // Registers a MediaSession state change callback. | 189 // Registers a MediaSession state change callback. |
187 CONTENT_EXPORT std::unique_ptr<base::CallbackList<void(State)>::Subscription> | 190 CONTENT_EXPORT std::unique_ptr<base::CallbackList<void(State)>::Subscription> |
188 RegisterMediaSessionStateChangedCallbackForTest( | 191 RegisterMediaSessionStateChangedCallbackForTest( |
189 const StateChangedCallback& cb); | 192 const StateChangedCallback& cb); |
190 | 193 |
191 std::unique_ptr<MediaSessionDelegate> delegate_; | 194 std::unique_ptr<MediaSessionDelegate> delegate_; |
192 PlayersMap players_; | 195 PlayersMap players_; |
193 | 196 |
194 State audio_focus_state_; | 197 State audio_focus_state_; |
195 SuspendType suspend_type_; | 198 SuspendType suspend_type_; |
196 Type audio_focus_type_; | 199 AudioFocusManager::AudioFocusType audio_focus_type_; |
197 | 200 |
198 MediaSessionUmaHelper uma_helper_; | 201 MediaSessionUmaHelper uma_helper_; |
199 | 202 |
200 // The volume multiplier of this session. All players in this session should | 203 // The volume multiplier of this session. All players in this session should |
201 // multiply their volume with this multiplier to get the effective volume. | 204 // multiply their volume with this multiplier to get the effective volume. |
202 double volume_multiplier_; | 205 double volume_multiplier_; |
203 | 206 |
204 MediaMetadata metadata_; | 207 MediaMetadata metadata_; |
205 base::CallbackList<void(State)> media_session_state_listeners_; | 208 base::CallbackList<void(State)> media_session_state_listeners_; |
206 | 209 |
207 DISALLOW_COPY_AND_ASSIGN(MediaSession); | 210 DISALLOW_COPY_AND_ASSIGN(MediaSession); |
208 }; | 211 }; |
209 | 212 |
210 } // namespace content | 213 } // namespace content |
211 | 214 |
212 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_ | 215 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_ |
OLD | NEW |