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_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_IMPL_H_ |
6 #define CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_IMPL_H_ | 6 #define CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_IMPL_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
| 10 #include <map> |
| 11 #include <set> |
| 12 |
10 #include "base/callback_list.h" | 13 #include "base/callback_list.h" |
11 #include "base/id_map.h" | 14 #include "base/id_map.h" |
12 #include "base/macros.h" | 15 #include "base/macros.h" |
13 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
14 #include "base/optional.h" | 17 #include "base/optional.h" |
15 #include "content/browser/media/session/audio_focus_manager.h" | 18 #include "content/browser/media/session/audio_focus_manager.h" |
16 #include "content/browser/media/session/media_session_uma_helper.h" | 19 #include "content/browser/media/session/media_session_uma_helper.h" |
17 #include "content/common/content_export.h" | 20 #include "content/common/content_export.h" |
18 #include "content/public/browser/media_session.h" | 21 #include "content/public/browser/media_session.h" |
19 #include "content/public/browser/media_session_observer.h" | 22 #include "content/public/browser/media_session_observer.h" |
20 #include "content/public/browser/web_contents_observer.h" | 23 #include "content/public/browser/web_contents_observer.h" |
21 #include "content/public/browser/web_contents_user_data.h" | 24 #include "content/public/browser/web_contents_user_data.h" |
22 #include "content/public/common/media_metadata.h" | 25 #include "content/public/common/media_metadata.h" |
23 | 26 |
24 #if defined(OS_ANDROID) | 27 #if defined(OS_ANDROID) |
25 #include "base/android/scoped_java_ref.h" | 28 #include "base/android/scoped_java_ref.h" |
26 #endif // defined(OS_ANDROID) | 29 #endif // defined(OS_ANDROID) |
27 | 30 |
28 class MediaSessionImplBrowserTest; | 31 class MediaSessionImplBrowserTest; |
29 | 32 |
30 namespace media { | 33 namespace media { |
31 enum class MediaContentType; | 34 enum class MediaContentType; |
32 } // namespace media | 35 } // namespace media |
33 | 36 |
34 namespace content { | 37 namespace content { |
35 | 38 |
36 class AudioFocusDelegate; | 39 class AudioFocusDelegate; |
37 class AudioFocusManagerTest; | 40 class AudioFocusManagerTest; |
| 41 class MediaSessionImplServiceRoutingTest; |
38 class MediaSessionImplStateObserver; | 42 class MediaSessionImplStateObserver; |
39 class MediaSessionImplVisibilityBrowserTest; | 43 class MediaSessionImplVisibilityBrowserTest; |
40 class MediaSessionObserver; | 44 class MediaSessionObserver; |
41 class MediaSessionPlayerObserver; | 45 class MediaSessionPlayerObserver; |
42 class MediaSessionServiceImpl; | 46 class MediaSessionServiceImpl; |
43 | 47 |
44 #if defined(OS_ANDROID) | 48 #if defined(OS_ANDROID) |
45 class MediaSessionAndroid; | 49 class MediaSessionAndroid; |
46 #endif // defined(OS_ANDROID) | 50 #endif // defined(OS_ANDROID) |
47 | 51 |
(...skipping 29 matching lines...) Expand all Loading... |
77 static MediaSession* FromJavaMediaSession( | 81 static MediaSession* FromJavaMediaSession( |
78 const base::android::JavaRef<jobject>& j_media_session); | 82 const base::android::JavaRef<jobject>& j_media_session); |
79 MediaSessionAndroid* session_android() const { | 83 MediaSessionAndroid* session_android() const { |
80 return session_android_.get(); | 84 return session_android_.get(); |
81 } | 85 } |
82 #endif // defined(OS_ANDROID) | 86 #endif // defined(OS_ANDROID) |
83 | 87 |
84 void AddObserver(MediaSessionObserver* observer); | 88 void AddObserver(MediaSessionObserver* observer); |
85 void RemoveObserver(MediaSessionObserver* observer); | 89 void RemoveObserver(MediaSessionObserver* observer); |
86 | 90 |
87 void SetMetadata(const base::Optional<MediaMetadata>& metadata); | 91 void NotifyMediaSessionMetadataChange( |
88 const base::Optional<MediaMetadata>& metadata() const { return metadata_; } | 92 const base::Optional<MediaMetadata>& metadata); |
| 93 void NotifyMediaSessionActionsChange( |
| 94 const std::set<blink::mojom::MediaSessionAction>& actions); |
89 | 95 |
90 // Adds the given player to the current media session. Returns whether the | 96 // Adds the given player to the current media session. Returns whether the |
91 // player was successfully added. If it returns false, AddPlayer() should be | 97 // player was successfully added. If it returns false, AddPlayer() should be |
92 // called again later. | 98 // called again later. |
93 CONTENT_EXPORT bool AddPlayer(MediaSessionPlayerObserver* observer, | 99 CONTENT_EXPORT bool AddPlayer(MediaSessionPlayerObserver* observer, |
94 int player_id, | 100 int player_id, |
95 media::MediaContentType media_content_type); | 101 media::MediaContentType media_content_type); |
96 | 102 |
97 // Removes the given player from the current media session. Abandons audio | 103 // Removes the given player from the current media session. Abandons audio |
98 // focus if that was the last player in the session. | 104 // focus if that was the last player in the session. |
(...skipping 18 matching lines...) Expand all Loading... |
117 CONTENT_EXPORT void Resume(MediaSession::SuspendType suspend_type) override; | 123 CONTENT_EXPORT void Resume(MediaSession::SuspendType suspend_type) override; |
118 | 124 |
119 // Suspend the media session. | 125 // Suspend the media session. |
120 // |type| represents the origin of the request. | 126 // |type| represents the origin of the request. |
121 CONTENT_EXPORT void Suspend(MediaSession::SuspendType suspend_type) override; | 127 CONTENT_EXPORT void Suspend(MediaSession::SuspendType suspend_type) override; |
122 | 128 |
123 // Stop the media session. | 129 // Stop the media session. |
124 // |type| represents the origin of the request. | 130 // |type| represents the origin of the request. |
125 CONTENT_EXPORT void Stop(MediaSession::SuspendType suspend_type) override; | 131 CONTENT_EXPORT void Stop(MediaSession::SuspendType suspend_type) override; |
126 | 132 |
127 // Received a media session action and forward to blink::MediaSession. | |
128 void DidReceiveAction(blink::mojom::MediaSessionAction action) override; | |
129 | |
130 // Called when an action is enabled in blink::MediaSession. This method will | |
131 // notify the observers that the action is enabled. | |
132 void OnMediaSessionEnabledAction(blink::mojom::MediaSessionAction action); | |
133 | |
134 // Called when an action is disabled in blink::MediaSession. This method will | |
135 // notify the observers that the action is disabled. | |
136 void OnMediaSessionDisabledAction(blink::mojom::MediaSessionAction action); | |
137 | |
138 // Let the media session start ducking such that the volume multiplier is | 133 // Let the media session start ducking such that the volume multiplier is |
139 // reduced. | 134 // reduced. |
140 CONTENT_EXPORT void StartDucking(); | 135 CONTENT_EXPORT void StartDucking(); |
141 | 136 |
142 // Let the media session stop ducking such that the volume multiplier is | 137 // Let the media session stop ducking such that the volume multiplier is |
143 // recovered. | 138 // recovered. |
144 CONTENT_EXPORT void StopDucking(); | 139 CONTENT_EXPORT void StopDucking(); |
145 | 140 |
146 // Returns if the session can be controlled by Resume() and Suspend calls | 141 // Returns if the session can be controlled by Resume() and Suspend calls |
147 // above. | 142 // above. |
(...skipping 17 matching lines...) Expand all Loading... |
165 CONTENT_EXPORT AudioFocusManager::AudioFocusType audio_focus_type() const { | 160 CONTENT_EXPORT AudioFocusManager::AudioFocusType audio_focus_type() const { |
166 return audio_focus_type_; | 161 return audio_focus_type_; |
167 } | 162 } |
168 | 163 |
169 // Returns whether the session has Pepper instances. | 164 // Returns whether the session has Pepper instances. |
170 bool HasPepper() const; | 165 bool HasPepper() const; |
171 | 166 |
172 // WebContentsObserver implementation | 167 // WebContentsObserver implementation |
173 void WebContentsDestroyed() override; | 168 void WebContentsDestroyed() override; |
174 | 169 |
175 // Sets the associated MediaSessionService for communicating with | 170 // MediaSessionService-related methods |
176 // blink::MediaSession. | 171 |
177 MediaSessionServiceImpl* GetMediaSessionService() { return service_; } | 172 // Called when a MediaSessionService is created, which registers itself to |
178 void SetMediaSessionService(MediaSessionServiceImpl* service); | 173 // this session. |
| 174 void OnServiceCreated(MediaSessionServiceImpl* service); |
| 175 // Called when a MediaSessionService is destroyed, which unregisters itself |
| 176 // from this session. |
| 177 void OnServiceDestroyed(MediaSessionServiceImpl* service); |
| 178 |
| 179 // Called when the metadata of a MediaSessionService has changed. Will notify |
| 180 // observers if the service is currently routed. |
| 181 void OnMediaSessionMetadataChanged(MediaSessionServiceImpl* service); |
| 182 // Called when the actions of a MediaSessionService has changed. Will notify |
| 183 // observers if the service is currently routed. |
| 184 void OnMediaSessionActionsChanged(MediaSessionServiceImpl* service); |
| 185 |
| 186 // Called when a MediaSessionAction is received. The action will be forwarded |
| 187 // to blink::MediaSession corresponding to the current routed service. |
| 188 void DidReceiveAction(blink::mojom::MediaSessionAction action) override; |
179 | 189 |
180 private: | 190 private: |
181 friend class content::WebContentsUserData<MediaSessionImpl>; | 191 friend class content::WebContentsUserData<MediaSessionImpl>; |
182 friend class ::MediaSessionImplBrowserTest; | 192 friend class ::MediaSessionImplBrowserTest; |
183 friend class content::MediaSessionImplVisibilityBrowserTest; | 193 friend class content::MediaSessionImplVisibilityBrowserTest; |
184 friend class content::AudioFocusManagerTest; | 194 friend class content::AudioFocusManagerTest; |
| 195 friend class content::MediaSessionImplServiceRoutingTest; |
185 friend class content::MediaSessionImplStateObserver; | 196 friend class content::MediaSessionImplStateObserver; |
186 | 197 |
187 CONTENT_EXPORT void SetDelegateForTests( | 198 CONTENT_EXPORT void SetDelegateForTests( |
188 std::unique_ptr<AudioFocusDelegate> delegate); | 199 std::unique_ptr<AudioFocusDelegate> delegate); |
189 CONTENT_EXPORT bool IsActiveForTest() const; | 200 CONTENT_EXPORT bool IsActiveForTest() const; |
190 CONTENT_EXPORT void RemoveAllPlayersForTest(); | 201 CONTENT_EXPORT void RemoveAllPlayersForTest(); |
191 CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test(); | 202 CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test(); |
192 | 203 |
193 // Representation of a player for the MediaSessionImpl. | 204 // Representation of a player for the MediaSessionImpl. |
194 struct PlayerIdentifier { | 205 struct PlayerIdentifier { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 CONTENT_EXPORT std::unique_ptr<base::CallbackList<void(State)>::Subscription> | 255 CONTENT_EXPORT std::unique_ptr<base::CallbackList<void(State)>::Subscription> |
245 RegisterMediaSessionStateChangedCallbackForTest( | 256 RegisterMediaSessionStateChangedCallbackForTest( |
246 const StateChangedCallback& cb); | 257 const StateChangedCallback& cb); |
247 | 258 |
248 CONTENT_EXPORT bool AddPepperPlayer(MediaSessionPlayerObserver* observer, | 259 CONTENT_EXPORT bool AddPepperPlayer(MediaSessionPlayerObserver* observer, |
249 int player_id); | 260 int player_id); |
250 | 261 |
251 CONTENT_EXPORT bool AddOneShotPlayer(MediaSessionPlayerObserver* observer, | 262 CONTENT_EXPORT bool AddOneShotPlayer(MediaSessionPlayerObserver* observer, |
252 int player_id); | 263 int player_id); |
253 | 264 |
| 265 // MediaSessionService-related methods |
| 266 |
| 267 // Called when the routed service may have changed. |
| 268 void UpdateRoutedService(); |
| 269 |
| 270 // Returns whether the frame |rfh| uses MediaSession API. |
| 271 bool IsServiceActiveForRenderFrameHost(RenderFrameHost* rfh); |
| 272 |
| 273 // Compute the MediaSessionService that should be routed, which will be used |
| 274 // to update |routed_service_|. |
| 275 CONTENT_EXPORT MediaSessionServiceImpl* ComputeServiceForRouting(); |
| 276 |
254 std::unique_ptr<AudioFocusDelegate> delegate_; | 277 std::unique_ptr<AudioFocusDelegate> delegate_; |
255 PlayersMap normal_players_; | 278 PlayersMap normal_players_; |
256 PlayersMap pepper_players_; | 279 PlayersMap pepper_players_; |
257 PlayersMap one_shot_players_; | 280 PlayersMap one_shot_players_; |
258 | 281 |
259 State audio_focus_state_; | 282 State audio_focus_state_; |
260 MediaSession::SuspendType suspend_type_; | 283 MediaSession::SuspendType suspend_type_; |
261 AudioFocusManager::AudioFocusType audio_focus_type_; | 284 AudioFocusManager::AudioFocusType audio_focus_type_; |
262 | 285 |
263 MediaSessionUmaHelper uma_helper_; | 286 MediaSessionUmaHelper uma_helper_; |
264 | 287 |
265 // The ducking state of this media session. The initial value is |false|, and | 288 // The ducking state of this media session. The initial value is |false|, and |
266 // is set to |true| after StartDucking(), and will be set to |false| after | 289 // is set to |true| after StartDucking(), and will be set to |false| after |
267 // StopDucking(). | 290 // StopDucking(). |
268 bool is_ducking_; | 291 bool is_ducking_; |
269 | 292 |
270 base::Optional<MediaMetadata> metadata_; | |
271 base::CallbackList<void(State)> media_session_state_listeners_; | 293 base::CallbackList<void(State)> media_session_state_listeners_; |
272 | 294 |
273 base::ObserverList<MediaSessionObserver> observers_; | 295 base::ObserverList<MediaSessionObserver> observers_; |
274 | 296 |
275 #if defined(OS_ANDROID) | 297 #if defined(OS_ANDROID) |
276 std::unique_ptr<MediaSessionAndroid> session_android_; | 298 std::unique_ptr<MediaSessionAndroid> session_android_; |
277 #endif // defined(OS_ANDROID) | 299 #endif // defined(OS_ANDROID) |
278 | 300 |
279 // The MediaSessionService this session is associated with (the service of the | 301 // MediaSessionService-related fields |
280 // top-level frame). | 302 using ServicesMap = std::map<RenderFrameHost*, MediaSessionServiceImpl*>; |
281 MediaSessionServiceImpl* service_; | 303 |
| 304 // The collection of all managed services (non-owned pointers). The services |
| 305 // are owned by RenderFrameHost and should be registered on creation and |
| 306 // unregistered on destroy. |
| 307 ServicesMap services_; |
| 308 // The currently routed service (non-owned pointer). |
| 309 MediaSessionServiceImpl* routed_service_; |
282 | 310 |
283 DISALLOW_COPY_AND_ASSIGN(MediaSessionImpl); | 311 DISALLOW_COPY_AND_ASSIGN(MediaSessionImpl); |
284 }; | 312 }; |
285 | 313 |
286 } // namespace content | 314 } // namespace content |
287 | 315 |
288 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_IMPL_H_ | 316 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_IMPL_H_ |
OLD | NEW |