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" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 static MediaSession* FromJavaMediaSession( | 80 static MediaSession* FromJavaMediaSession( |
78 const base::android::JavaRef<jobject>& j_media_session); | 81 const base::android::JavaRef<jobject>& j_media_session); |
79 MediaSessionAndroid* session_android() const { | 82 MediaSessionAndroid* session_android() const { |
80 return session_android_.get(); | 83 return session_android_.get(); |
81 } | 84 } |
82 #endif // defined(OS_ANDROID) | 85 #endif // defined(OS_ANDROID) |
83 | 86 |
84 void AddObserver(MediaSessionObserver* observer); | 87 void AddObserver(MediaSessionObserver* observer); |
85 void RemoveObserver(MediaSessionObserver* observer); | 88 void RemoveObserver(MediaSessionObserver* observer); |
86 | 89 |
87 void SetMetadata(const base::Optional<MediaMetadata>& metadata); | 90 void NotifyMediaSessionMetadataChange( |
88 const base::Optional<MediaMetadata>& metadata() const { return metadata_; } | 91 const base::Optional<MediaMetadata>& metadata); |
| 92 void NotifyMediaSessionActionsChange( |
| 93 const std::set<blink::mojom::MediaSessionAction>& actions); |
89 | 94 |
90 // Adds the given player to the current media session. Returns whether the | 95 // Adds the given player to the current media session. Returns whether the |
91 // player was successfully added. If it returns false, AddPlayer() should be | 96 // player was successfully added. If it returns false, AddPlayer() should be |
92 // called again later. | 97 // called again later. |
93 CONTENT_EXPORT bool AddPlayer(MediaSessionPlayerObserver* observer, | 98 CONTENT_EXPORT bool AddPlayer(MediaSessionPlayerObserver* observer, |
94 int player_id, | 99 int player_id, |
95 media::MediaContentType media_content_type); | 100 media::MediaContentType media_content_type); |
96 | 101 |
97 // Removes the given player from the current media session. Abandons audio | 102 // Removes the given player from the current media session. Abandons audio |
98 // focus if that was the last player in the session. | 103 // 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; | 122 CONTENT_EXPORT void Resume(MediaSession::SuspendType suspend_type) override; |
118 | 123 |
119 // Suspend the media session. | 124 // Suspend the media session. |
120 // |type| represents the origin of the request. | 125 // |type| represents the origin of the request. |
121 CONTENT_EXPORT void Suspend(MediaSession::SuspendType suspend_type) override; | 126 CONTENT_EXPORT void Suspend(MediaSession::SuspendType suspend_type) override; |
122 | 127 |
123 // Stop the media session. | 128 // Stop the media session. |
124 // |type| represents the origin of the request. | 129 // |type| represents the origin of the request. |
125 CONTENT_EXPORT void Stop(MediaSession::SuspendType suspend_type) override; | 130 CONTENT_EXPORT void Stop(MediaSession::SuspendType suspend_type) override; |
126 | 131 |
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 | 132 // Let the media session start ducking such that the volume multiplier is |
139 // reduced. | 133 // reduced. |
140 CONTENT_EXPORT void StartDucking(); | 134 CONTENT_EXPORT void StartDucking(); |
141 | 135 |
142 // Let the media session stop ducking such that the volume multiplier is | 136 // Let the media session stop ducking such that the volume multiplier is |
143 // recovered. | 137 // recovered. |
144 CONTENT_EXPORT void StopDucking(); | 138 CONTENT_EXPORT void StopDucking(); |
145 | 139 |
146 // Returns if the session can be controlled by Resume() and Suspend calls | 140 // Returns if the session can be controlled by Resume() and Suspend calls |
147 // above. | 141 // above. |
(...skipping 17 matching lines...) Expand all Loading... |
165 CONTENT_EXPORT AudioFocusManager::AudioFocusType audio_focus_type() const { | 159 CONTENT_EXPORT AudioFocusManager::AudioFocusType audio_focus_type() const { |
166 return audio_focus_type_; | 160 return audio_focus_type_; |
167 } | 161 } |
168 | 162 |
169 // Returns whether the session has Pepper instances. | 163 // Returns whether the session has Pepper instances. |
170 bool HasPepper() const; | 164 bool HasPepper() const; |
171 | 165 |
172 // WebContentsObserver implementation | 166 // WebContentsObserver implementation |
173 void WebContentsDestroyed() override; | 167 void WebContentsDestroyed() override; |
174 | 168 |
175 // Sets the associated MediaSessionService for communicating with | 169 // MediaSessionService-related methods |
176 // blink::MediaSession. | 170 |
177 MediaSessionServiceImpl* GetMediaSessionService() { return service_; } | 171 // Called when a MediaSessionService is created, which registers itself to |
178 void SetMediaSessionService(MediaSessionServiceImpl* service); | 172 // this session. |
| 173 void OnServiceCreated(MediaSessionServiceImpl* service); |
| 174 // Called when a MediaSessionService is destroyed, which unregisters itself |
| 175 // from this session. |
| 176 void OnServiceDestroyed(MediaSessionServiceImpl* service); |
| 177 |
| 178 // Called when the metadata of a MediaSessionService has changed. Will notify |
| 179 // observers if the service is currently routed. |
| 180 void OnMediaSessionMetadataChanged(MediaSessionServiceImpl* service); |
| 181 // Called when the actions of a MediaSessionService has changed. Will notify |
| 182 // observers if the service is currently routed. |
| 183 void OnMediaSessionActionsChanged(MediaSessionServiceImpl* service); |
| 184 |
| 185 // Called when a MediaSessionAction is received. The action will be forwarded |
| 186 // to blink::MediaSession corresponding to the current routed service. |
| 187 void DidReceiveAction(blink::mojom::MediaSessionAction action) override; |
179 | 188 |
180 private: | 189 private: |
181 friend class content::WebContentsUserData<MediaSessionImpl>; | 190 friend class content::WebContentsUserData<MediaSessionImpl>; |
182 friend class ::MediaSessionImplBrowserTest; | 191 friend class ::MediaSessionImplBrowserTest; |
183 friend class content::MediaSessionImplVisibilityBrowserTest; | 192 friend class content::MediaSessionImplVisibilityBrowserTest; |
184 friend class content::AudioFocusManagerTest; | 193 friend class content::AudioFocusManagerTest; |
185 friend class content::MediaSessionImplStateObserver; | 194 friend class content::MediaSessionImplStateObserver; |
186 | 195 |
187 CONTENT_EXPORT void SetDelegateForTests( | 196 CONTENT_EXPORT void SetDelegateForTests( |
188 std::unique_ptr<AudioFocusDelegate> delegate); | 197 std::unique_ptr<AudioFocusDelegate> delegate); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 CONTENT_EXPORT std::unique_ptr<base::CallbackList<void(State)>::Subscription> | 253 CONTENT_EXPORT std::unique_ptr<base::CallbackList<void(State)>::Subscription> |
245 RegisterMediaSessionStateChangedCallbackForTest( | 254 RegisterMediaSessionStateChangedCallbackForTest( |
246 const StateChangedCallback& cb); | 255 const StateChangedCallback& cb); |
247 | 256 |
248 CONTENT_EXPORT bool AddPepperPlayer(MediaSessionPlayerObserver* observer, | 257 CONTENT_EXPORT bool AddPepperPlayer(MediaSessionPlayerObserver* observer, |
249 int player_id); | 258 int player_id); |
250 | 259 |
251 CONTENT_EXPORT bool AddOneShotPlayer(MediaSessionPlayerObserver* observer, | 260 CONTENT_EXPORT bool AddOneShotPlayer(MediaSessionPlayerObserver* observer, |
252 int player_id); | 261 int player_id); |
253 | 262 |
| 263 // MediaSessionService-related methods |
| 264 |
| 265 // Called when the routed service may have changed. |
| 266 void UpdateRoutedService(); |
| 267 // Returns whether the frame |rfh| uses MediaSession API. |
| 268 bool IsServiceActiveForRenderFrameHost(RenderFrameHost* rfh); |
| 269 // Compute the MediaSessionService that should be routed, which will be used |
| 270 // to update |routed_service_|. |
| 271 MediaSessionServiceImpl* ComputeServiceForRouting(); |
| 272 void ComputeServiceForRoutingInternal( |
| 273 MediaSessionPlayerObserver* player_observer, |
| 274 MediaSessionServiceImpl** best_service, |
| 275 size_t* min_depth); |
| 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 |