| 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 "base/optional.h" | 13 #include "base/optional.h" |
| 14 #include "content/browser/media/session/audio_focus_manager.h" | 14 #include "content/browser/media/session/audio_focus_manager.h" |
| 15 #include "content/browser/media/session/media_session_uma_helper.h" | 15 #include "content/browser/media/session/media_session_uma_helper.h" |
| 16 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
| 17 #include "content/public/browser/web_contents_observer.h" | 17 #include "content/public/browser/web_contents_observer.h" |
| 18 #include "content/public/browser/web_contents_user_data.h" | 18 #include "content/public/browser/web_contents_user_data.h" |
| 19 #include "content/public/common/media_metadata.h" | 19 #include "content/public/common/media_metadata.h" |
| 20 | 20 |
| 21 class MediaSessionBrowserTest; | 21 class MediaSessionBrowserTest; |
| 22 | 22 |
| 23 namespace media { | 23 namespace media { |
| 24 enum class MediaContentType; | 24 enum class MediaContentType; |
| 25 } // namespace media | 25 } // namespace media |
| 26 | 26 |
| 27 namespace content { | 27 namespace content { |
| 28 | 28 |
| 29 class AudioFocusDelegate; |
| 29 class AudioFocusManagerTest; | 30 class AudioFocusManagerTest; |
| 30 class MediaSessionDelegate; | 31 class MediaSessionPlayerObserver; |
| 31 class MediaSessionObserver; | |
| 32 class MediaSessionStateObserver; | 32 class MediaSessionStateObserver; |
| 33 class MediaSessionVisibilityBrowserTest; | 33 class MediaSessionVisibilityBrowserTest; |
| 34 | 34 |
| 35 // MediaSession manages the media session and audio focus for a given | 35 // MediaSession manages the media session and audio focus for a given |
| 36 // 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 |
| 37 // system and dropping it on demand. | 37 // system and dropping it on demand. |
| 38 // 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 |
| 39 // 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 |
| 40 // 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 |
| 41 // 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 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 70 CONTENT_EXPORT static MediaSession* Get(WebContents* web_contents); | 70 CONTENT_EXPORT static MediaSession* Get(WebContents* web_contents); |
| 71 | 71 |
| 72 ~MediaSession() override; | 72 ~MediaSession() override; |
| 73 | 73 |
| 74 void SetMetadata(const base::Optional<MediaMetadata>& metadata); | 74 void SetMetadata(const base::Optional<MediaMetadata>& metadata); |
| 75 const base::Optional<MediaMetadata>& metadata() const { return metadata_; } | 75 const base::Optional<MediaMetadata>& metadata() const { return metadata_; } |
| 76 | 76 |
| 77 // 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 |
| 78 // player was successfully added. If it returns false, AddPlayer() should be | 78 // player was successfully added. If it returns false, AddPlayer() should be |
| 79 // called again later. | 79 // called again later. |
| 80 CONTENT_EXPORT bool AddPlayer(MediaSessionObserver* observer, | 80 CONTENT_EXPORT bool AddPlayer(MediaSessionPlayerObserver* observer, |
| 81 int player_id, | 81 int player_id, |
| 82 media::MediaContentType media_content_type); | 82 media::MediaContentType media_content_type); |
| 83 | 83 |
| 84 // Removes the given player from the current media session. Abandons audio | 84 // Removes the given player from the current media session. Abandons audio |
| 85 // focus if that was the last player in the session. | 85 // focus if that was the last player in the session. |
| 86 CONTENT_EXPORT void RemovePlayer(MediaSessionObserver* observer, | 86 CONTENT_EXPORT void RemovePlayer(MediaSessionPlayerObserver* observer, |
| 87 int player_id); | 87 int player_id); |
| 88 | 88 |
| 89 // Removes all the players associated with |observer|. Abandons audio focus if | 89 // Removes all the players associated with |observer|. Abandons audio focus if |
| 90 // these were the last players in the session. | 90 // these were the last players in the session. |
| 91 CONTENT_EXPORT void RemovePlayers(MediaSessionObserver* observer); | 91 CONTENT_EXPORT void RemovePlayers(MediaSessionPlayerObserver* observer); |
| 92 | 92 |
| 93 // Record that the session was ducked. | 93 // Record that the session was ducked. |
| 94 void RecordSessionDuck(); | 94 void RecordSessionDuck(); |
| 95 | 95 |
| 96 // Called when a player is paused in the content. | 96 // Called when a player is paused in the content. |
| 97 // If the paused player is the last player, we suspend the MediaSession. | 97 // If the paused player is the last player, we suspend the MediaSession. |
| 98 // Otherwise, the paused player will be removed from the MediaSession. | 98 // Otherwise, the paused player will be removed from the MediaSession. |
| 99 CONTENT_EXPORT void OnPlayerPaused(MediaSessionObserver* observer, | 99 CONTENT_EXPORT void OnPlayerPaused(MediaSessionPlayerObserver* observer, |
| 100 int player_id); | 100 int player_id); |
| 101 | 101 |
| 102 // Resume the media session. | 102 // Resume the media session. |
| 103 // |type| represents the origin of the request. | 103 // |type| represents the origin of the request. |
| 104 CONTENT_EXPORT void Resume(SuspendType suspend_type); | 104 CONTENT_EXPORT void Resume(SuspendType suspend_type); |
| 105 | 105 |
| 106 // Suspend the media session. | 106 // Suspend the media session. |
| 107 // |type| represents the origin of the request. | 107 // |type| represents the origin of the request. |
| 108 CONTENT_EXPORT void Suspend(SuspendType suspend_type); | 108 CONTENT_EXPORT void Suspend(SuspendType suspend_type); |
| 109 | 109 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 void WebContentsDestroyed() override; | 149 void WebContentsDestroyed() override; |
| 150 | 150 |
| 151 private: | 151 private: |
| 152 friend class content::WebContentsUserData<MediaSession>; | 152 friend class content::WebContentsUserData<MediaSession>; |
| 153 friend class ::MediaSessionBrowserTest; | 153 friend class ::MediaSessionBrowserTest; |
| 154 friend class content::MediaSessionVisibilityBrowserTest; | 154 friend class content::MediaSessionVisibilityBrowserTest; |
| 155 friend class content::AudioFocusManagerTest; | 155 friend class content::AudioFocusManagerTest; |
| 156 friend class content::MediaSessionStateObserver; | 156 friend class content::MediaSessionStateObserver; |
| 157 | 157 |
| 158 CONTENT_EXPORT void SetDelegateForTests( | 158 CONTENT_EXPORT void SetDelegateForTests( |
| 159 std::unique_ptr<MediaSessionDelegate> delegate); | 159 std::unique_ptr<AudioFocusDelegate> delegate); |
| 160 CONTENT_EXPORT bool IsActiveForTest() const; | 160 CONTENT_EXPORT bool IsActiveForTest() const; |
| 161 CONTENT_EXPORT void RemoveAllPlayersForTest(); | 161 CONTENT_EXPORT void RemoveAllPlayersForTest(); |
| 162 CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test(); | 162 CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test(); |
| 163 | 163 |
| 164 // Representation of a player for the MediaSession. | 164 // Representation of a player for the MediaSession. |
| 165 struct PlayerIdentifier { | 165 struct PlayerIdentifier { |
| 166 PlayerIdentifier(MediaSessionObserver* observer, int player_id); | 166 PlayerIdentifier(MediaSessionPlayerObserver* observer, int player_id); |
| 167 PlayerIdentifier(const PlayerIdentifier&) = default; | 167 PlayerIdentifier(const PlayerIdentifier&) = default; |
| 168 | 168 |
| 169 void operator=(const PlayerIdentifier&) = delete; | 169 void operator=(const PlayerIdentifier&) = delete; |
| 170 bool operator==(const PlayerIdentifier& player_identifier) const; | 170 bool operator==(const PlayerIdentifier& player_identifier) const; |
| 171 | 171 |
| 172 // Hash operator for base::hash_map<>. | 172 // Hash operator for base::hash_map<>. |
| 173 struct Hash { | 173 struct Hash { |
| 174 size_t operator()(const PlayerIdentifier& player_identifier) const; | 174 size_t operator()(const PlayerIdentifier& player_identifier) const; |
| 175 }; | 175 }; |
| 176 | 176 |
| 177 MediaSessionObserver* observer; | 177 MediaSessionPlayerObserver* observer; |
| 178 int player_id; | 178 int player_id; |
| 179 }; | 179 }; |
| 180 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>; | 180 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>; |
| 181 using StateChangedCallback = base::Callback<void(State)>; | 181 using StateChangedCallback = base::Callback<void(State)>; |
| 182 | 182 |
| 183 CONTENT_EXPORT explicit MediaSession(WebContents* web_contents); | 183 CONTENT_EXPORT explicit MediaSession(WebContents* web_contents); |
| 184 | 184 |
| 185 void Initialize(); | 185 void Initialize(); |
| 186 | 186 |
| 187 CONTENT_EXPORT void OnSuspendInternal(SuspendType suspend_type, | 187 CONTENT_EXPORT void OnSuspendInternal(SuspendType suspend_type, |
| 188 State new_state); | 188 State new_state); |
| 189 CONTENT_EXPORT void OnResumeInternal(SuspendType suspend_type); | 189 CONTENT_EXPORT void OnResumeInternal(SuspendType suspend_type); |
| 190 | 190 |
| 191 // Requests audio focus to the MediaSessionDelegate. | 191 // Requests audio focus to the AudioFocusDelegate. |
| 192 // Returns whether the request was granted. | 192 // Returns whether the request was granted. |
| 193 CONTENT_EXPORT bool RequestSystemAudioFocus( | 193 CONTENT_EXPORT bool RequestSystemAudioFocus( |
| 194 AudioFocusManager::AudioFocusType audio_focus_type); | 194 AudioFocusManager::AudioFocusType audio_focus_type); |
| 195 | 195 |
| 196 // To be called after a call to AbandonAudioFocus() in order request the | 196 // To be called after a call to AbandonAudioFocus() in order request the |
| 197 // delegate to abandon the audio focus. | 197 // delegate to abandon the audio focus. |
| 198 CONTENT_EXPORT void AbandonSystemAudioFocusIfNeeded(); | 198 CONTENT_EXPORT void AbandonSystemAudioFocusIfNeeded(); |
| 199 | 199 |
| 200 // Notifies WebContents about the state change of the media session. | 200 // Notifies WebContents about the state change of the media session. |
| 201 void UpdateWebContents(); | 201 void UpdateWebContents(); |
| 202 | 202 |
| 203 // Internal method that should be used instead of setting audio_focus_state_. | 203 // 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. | 204 // It sets audio_focus_state_ and notifies observers about the state change. |
| 205 void SetAudioFocusState(State audio_focus_state); | 205 void SetAudioFocusState(State audio_focus_state); |
| 206 | 206 |
| 207 // Update the volume multiplier when ducking state changes. | 207 // Update the volume multiplier when ducking state changes. |
| 208 void UpdateVolumeMultiplier(); | 208 void UpdateVolumeMultiplier(); |
| 209 | 209 |
| 210 // Get the volume multiplier, which depends on whether the media session is | 210 // Get the volume multiplier, which depends on whether the media session is |
| 211 // ducking. | 211 // ducking. |
| 212 double GetVolumeMultiplier() const; | 212 double GetVolumeMultiplier() const; |
| 213 | 213 |
| 214 // Registers a MediaSession state change callback. | 214 // Registers a MediaSession state change callback. |
| 215 CONTENT_EXPORT std::unique_ptr<base::CallbackList<void(State)>::Subscription> | 215 CONTENT_EXPORT std::unique_ptr<base::CallbackList<void(State)>::Subscription> |
| 216 RegisterMediaSessionStateChangedCallbackForTest( | 216 RegisterMediaSessionStateChangedCallbackForTest( |
| 217 const StateChangedCallback& cb); | 217 const StateChangedCallback& cb); |
| 218 | 218 |
| 219 CONTENT_EXPORT bool AddPepperPlayer(MediaSessionObserver* observer, | 219 CONTENT_EXPORT bool AddPepperPlayer(MediaSessionPlayerObserver* observer, |
| 220 int player_id); | 220 int player_id); |
| 221 | 221 |
| 222 std::unique_ptr<MediaSessionDelegate> delegate_; | 222 std::unique_ptr<AudioFocusDelegate> delegate_; |
| 223 PlayersMap players_; | 223 PlayersMap players_; |
| 224 PlayersMap pepper_players_; | 224 PlayersMap pepper_players_; |
| 225 | 225 |
| 226 State audio_focus_state_; | 226 State audio_focus_state_; |
| 227 SuspendType suspend_type_; | 227 SuspendType suspend_type_; |
| 228 AudioFocusManager::AudioFocusType audio_focus_type_; | 228 AudioFocusManager::AudioFocusType audio_focus_type_; |
| 229 | 229 |
| 230 MediaSessionUmaHelper uma_helper_; | 230 MediaSessionUmaHelper uma_helper_; |
| 231 | 231 |
| 232 // The ducking state of this media session. The initial value is |false|, and | 232 // 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 | 233 // is set to |true| after StartDucking(), and will be set to |false| after |
| 234 // StopDucking(). | 234 // StopDucking(). |
| 235 bool is_ducking_; | 235 bool is_ducking_; |
| 236 | 236 |
| 237 base::Optional<MediaMetadata> metadata_; | 237 base::Optional<MediaMetadata> metadata_; |
| 238 base::CallbackList<void(State)> media_session_state_listeners_; | 238 base::CallbackList<void(State)> media_session_state_listeners_; |
| 239 | 239 |
| 240 DISALLOW_COPY_AND_ASSIGN(MediaSession); | 240 DISALLOW_COPY_AND_ASSIGN(MediaSession); |
| 241 }; | 241 }; |
| 242 | 242 |
| 243 } // namespace content | 243 } // namespace content |
| 244 | 244 |
| 245 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_ | 245 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_ |
| OLD | NEW |