| 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 enum class MediaContentType; |
| 24 } // namespace media |
| 25 |
| 21 namespace content { | 26 namespace content { |
| 22 | 27 |
| 23 class AudioFocusManagerTest; | 28 class AudioFocusManagerTest; |
| 24 class MediaSessionDelegate; | 29 class MediaSessionDelegate; |
| 25 class MediaSessionObserver; | 30 class MediaSessionObserver; |
| 26 class MediaSessionStateObserver; | 31 class MediaSessionStateObserver; |
| 27 class MediaSessionVisibilityBrowserTest; | 32 class MediaSessionVisibilityBrowserTest; |
| 28 | 33 |
| 29 // MediaSession manages the media session and audio focus for a given | 34 // MediaSession manages the media session and audio focus for a given |
| 30 // WebContents. It is requesting the audio focus, pausing when requested by the | 35 // WebContents. It is requesting the audio focus, pausing when requested by the |
| 31 // system and dropping it on demand. | 36 // system and dropping it on demand. |
| 32 // The audio focus can be of two types: Transient or Content. A Transient audio | 37 // 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 | 38 // 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 | 39 // 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 | 40 // 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 | 41 // 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. | 42 // case of Transient and Content audio focus are both requested. |
| 38 // TODO(thakis,mlamouri): MediaSession isn't CONTENT_EXPORT'd because it creates | 43 // TODO(thakis,mlamouri): MediaSession isn't CONTENT_EXPORT'd because it creates |
| 39 // complicated build issues with WebContentsUserData being a non-exported | 44 // complicated build issues with WebContentsUserData being a non-exported |
| 40 // template, see htttps://crbug.com/589840. As a result, the class uses | 45 // 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 | 46 // 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. | 47 // should be moved back to the class when the Windows build will work with it. |
| 43 class MediaSession : public WebContentsObserver, | 48 class MediaSession : public WebContentsObserver, |
| 44 protected WebContentsUserData<MediaSession> { | 49 protected WebContentsUserData<MediaSession> { |
| 45 public: | 50 public: |
| 46 enum class Type { | |
| 47 Content, | |
| 48 Transient | |
| 49 }; | |
| 50 | |
| 51 enum class SuspendType { | 51 enum class SuspendType { |
| 52 // Suspended by the system because a transient sound needs to be played. | 52 // Suspended by the system because a transient sound needs to be played. |
| 53 SYSTEM, | 53 SYSTEM, |
| 54 // Suspended by the UI. | 54 // Suspended by the UI. |
| 55 UI, | 55 UI, |
| 56 // Suspended by the page via script or user interaction. | 56 // Suspended by the page via script or user interaction. |
| 57 CONTENT, | 57 CONTENT, |
| 58 }; | 58 }; |
| 59 | 59 |
| 60 // Returns the MediaSession associated to this WebContents. Creates one if | 60 // Returns the MediaSession associated to this WebContents. Creates one if |
| 61 // none is currently available. | 61 // none is currently available. |
| 62 CONTENT_EXPORT static MediaSession* Get(WebContents* web_contents); | 62 CONTENT_EXPORT static MediaSession* Get(WebContents* web_contents); |
| 63 | 63 |
| 64 ~MediaSession() override; | 64 ~MediaSession() override; |
| 65 | 65 |
| 66 void SetMetadata(const MediaMetadata& metadata); | 66 void SetMetadata(const MediaMetadata& metadata); |
| 67 const MediaMetadata& metadata() const { return metadata_; } | 67 const MediaMetadata& metadata() const { return metadata_; } |
| 68 | 68 |
| 69 // Adds the given player to the current media session. Returns whether the | 69 // Adds the given player to the current media session. Returns whether the |
| 70 // player was successfully added. If it returns false, AddPlayer() should be | 70 // player was successfully added. If it returns false, AddPlayer() should be |
| 71 // called again later. | 71 // called again later. |
| 72 CONTENT_EXPORT bool AddPlayer(MediaSessionObserver* observer, | 72 CONTENT_EXPORT bool AddPlayer(MediaSessionObserver* observer, |
| 73 int player_id, Type type); | 73 int player_id, |
| 74 media::MediaContentType media_content_type); |
| 74 | 75 |
| 75 // Removes the given player from the current media session. Abandons audio | 76 // Removes the given player from the current media session. Abandons audio |
| 76 // focus if that was the last player in the session. | 77 // focus if that was the last player in the session. |
| 77 CONTENT_EXPORT void RemovePlayer(MediaSessionObserver* observer, | 78 CONTENT_EXPORT void RemovePlayer(MediaSessionObserver* observer, |
| 78 int player_id); | 79 int player_id); |
| 79 | 80 |
| 80 // Removes all the players associated with |observer|. Abandons audio focus if | 81 // Removes all the players associated with |observer|. Abandons audio focus if |
| 81 // these were the last players in the session. | 82 // these were the last players in the session. |
| 82 CONTENT_EXPORT void RemovePlayers(MediaSessionObserver* observer); | 83 CONTENT_EXPORT void RemovePlayers(MediaSessionObserver* observer); |
| 83 | 84 |
| 84 // Record that the session was ducked. | 85 // Record that the session was ducked. |
| 85 void RecordSessionDuck(); | 86 void RecordSessionDuck(); |
| 86 | 87 |
| 87 // Called when a player is paused in the content. | 88 // Called when a player is paused in the content. |
| 88 // If the paused player is the last player, we suspend the MediaSession. | 89 // If the paused player is the last player, we suspend the MediaSession. |
| 89 // Otherwise, the paused player will be removed from the MediaSession. | 90 // Otherwise, the paused player will be removed from the MediaSession. |
| 90 CONTENT_EXPORT void OnPlayerPaused(MediaSessionObserver* observer, | 91 CONTENT_EXPORT void OnPlayerPaused(MediaSessionObserver* observer, |
| 91 int player_id); | 92 int player_id); |
| 92 | 93 |
| 93 // Resume the media session. | 94 // Resume the media session. |
| 94 // |type| represents the origin of the request. | 95 // |type| represents the origin of the request. |
| 95 CONTENT_EXPORT void Resume(SuspendType type); | 96 CONTENT_EXPORT void Resume(SuspendType suspend_type); |
| 96 | 97 |
| 97 // Suspend the media session. | 98 // Suspend the media session. |
| 98 // |type| represents the origin of the request. | 99 // |type| represents the origin of the request. |
| 99 CONTENT_EXPORT void Suspend(SuspendType type); | 100 CONTENT_EXPORT void Suspend(SuspendType suspend_type); |
| 100 | 101 |
| 101 // Stop the media session. | 102 // Stop the media session. |
| 102 // |type| represents the origin of the request. | 103 // |type| represents the origin of the request. |
| 103 CONTENT_EXPORT void Stop(SuspendType type); | 104 CONTENT_EXPORT void Stop(SuspendType suspend_type); |
| 104 | 105 |
| 105 // Change the volume multiplier of the session to |volume_multiplier|. | 106 // Change the volume multiplier of the session to |volume_multiplier|. |
| 106 CONTENT_EXPORT void SetVolumeMultiplier(double volume_multiplier); | 107 CONTENT_EXPORT void SetVolumeMultiplier(double volume_multiplier); |
| 107 | 108 |
| 108 // Returns if the session can be controlled by Resume() and Suspend calls | 109 // Returns if the session can be controlled by Resume() and Suspend calls |
| 109 // above. | 110 // above. |
| 110 CONTENT_EXPORT bool IsControllable() const; | 111 CONTENT_EXPORT bool IsControllable() const; |
| 111 | 112 |
| 112 // Returns if the session is currently active. | 113 // Returns if the session is currently active. |
| 113 CONTENT_EXPORT bool IsActive() const; | 114 CONTENT_EXPORT bool IsActive() const; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 125 private: | 126 private: |
| 126 friend class content::WebContentsUserData<MediaSession>; | 127 friend class content::WebContentsUserData<MediaSession>; |
| 127 friend class ::MediaSessionBrowserTest; | 128 friend class ::MediaSessionBrowserTest; |
| 128 friend class content::MediaSessionVisibilityBrowserTest; | 129 friend class content::MediaSessionVisibilityBrowserTest; |
| 129 friend class content::AudioFocusManagerTest; | 130 friend class content::AudioFocusManagerTest; |
| 130 friend class content::MediaSessionStateObserver; | 131 friend class content::MediaSessionStateObserver; |
| 131 | 132 |
| 132 CONTENT_EXPORT void SetDelegateForTests( | 133 CONTENT_EXPORT void SetDelegateForTests( |
| 133 std::unique_ptr<MediaSessionDelegate> delegate); | 134 std::unique_ptr<MediaSessionDelegate> delegate); |
| 134 CONTENT_EXPORT bool IsActiveForTest() const; | 135 CONTENT_EXPORT bool IsActiveForTest() const; |
| 135 CONTENT_EXPORT Type audio_focus_type_for_test() const; | 136 CONTENT_EXPORT AudioFocusManager::AudioFocusType audio_focus_type_for_test() |
| 137 const; |
| 136 CONTENT_EXPORT void RemoveAllPlayersForTest(); | 138 CONTENT_EXPORT void RemoveAllPlayersForTest(); |
| 137 CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test(); | 139 CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test(); |
| 138 | 140 |
| 139 enum class State { | 141 enum class State { |
| 140 ACTIVE, | 142 ACTIVE, |
| 141 SUSPENDED, | 143 SUSPENDED, |
| 142 INACTIVE | 144 INACTIVE |
| 143 }; | 145 }; |
| 144 | 146 |
| 145 // Representation of a player for the MediaSession. | 147 // Representation of a player for the MediaSession. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 158 MediaSessionObserver* observer; | 160 MediaSessionObserver* observer; |
| 159 int player_id; | 161 int player_id; |
| 160 }; | 162 }; |
| 161 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>; | 163 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>; |
| 162 using StateChangedCallback = base::Callback<void(State)>; | 164 using StateChangedCallback = base::Callback<void(State)>; |
| 163 | 165 |
| 164 CONTENT_EXPORT explicit MediaSession(WebContents* web_contents); | 166 CONTENT_EXPORT explicit MediaSession(WebContents* web_contents); |
| 165 | 167 |
| 166 void Initialize(); | 168 void Initialize(); |
| 167 | 169 |
| 168 CONTENT_EXPORT void OnSuspendInternal(SuspendType type, State new_state); | 170 CONTENT_EXPORT void OnSuspendInternal(SuspendType suspend_type, |
| 169 CONTENT_EXPORT void OnResumeInternal(SuspendType type); | 171 State new_state); |
| 172 CONTENT_EXPORT void OnResumeInternal(SuspendType suspend_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( |
| 177 AudioFocusManager::AudioFocusType audio_focus_type); |
| 174 | 178 |
| 175 // To be called after a call to AbandonAudioFocus() in order request the | 179 // To be called after a call to AbandonAudioFocus() in order request the |
| 176 // delegate to abandon the audio focus. | 180 // delegate to abandon the audio focus. |
| 177 void AbandonSystemAudioFocusIfNeeded(); | 181 void AbandonSystemAudioFocusIfNeeded(); |
| 178 | 182 |
| 179 // Notifies WebContents about the state change of the media session. | 183 // Notifies WebContents about the state change of the media session. |
| 180 void UpdateWebContents(); | 184 void UpdateWebContents(); |
| 181 | 185 |
| 182 // Internal method that should be used instead of setting audio_focus_state_. | 186 // 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. | 187 // It sets audio_focus_state_ and notifies observers about the state change. |
| 184 void SetAudioFocusState(State audio_focus_state); | 188 void SetAudioFocusState(State audio_focus_state); |
| 185 | 189 |
| 186 // Registers a MediaSession state change callback. | 190 // Registers a MediaSession state change callback. |
| 187 CONTENT_EXPORT std::unique_ptr<base::CallbackList<void(State)>::Subscription> | 191 CONTENT_EXPORT std::unique_ptr<base::CallbackList<void(State)>::Subscription> |
| 188 RegisterMediaSessionStateChangedCallbackForTest( | 192 RegisterMediaSessionStateChangedCallbackForTest( |
| 189 const StateChangedCallback& cb); | 193 const StateChangedCallback& cb); |
| 190 | 194 |
| 191 std::unique_ptr<MediaSessionDelegate> delegate_; | 195 std::unique_ptr<MediaSessionDelegate> delegate_; |
| 192 PlayersMap players_; | 196 PlayersMap players_; |
| 193 | 197 |
| 194 State audio_focus_state_; | 198 State audio_focus_state_; |
| 195 SuspendType suspend_type_; | 199 SuspendType suspend_type_; |
| 196 Type audio_focus_type_; | 200 AudioFocusManager::AudioFocusType audio_focus_type_; |
| 197 | 201 |
| 198 MediaSessionUmaHelper uma_helper_; | 202 MediaSessionUmaHelper uma_helper_; |
| 199 | 203 |
| 200 // The volume multiplier of this session. All players in this session should | 204 // The volume multiplier of this session. All players in this session should |
| 201 // multiply their volume with this multiplier to get the effective volume. | 205 // multiply their volume with this multiplier to get the effective volume. |
| 202 double volume_multiplier_; | 206 double volume_multiplier_; |
| 203 | 207 |
| 204 MediaMetadata metadata_; | 208 MediaMetadata metadata_; |
| 205 base::CallbackList<void(State)> media_session_state_listeners_; | 209 base::CallbackList<void(State)> media_session_state_listeners_; |
| 206 | 210 |
| 207 DISALLOW_COPY_AND_ASSIGN(MediaSession); | 211 DISALLOW_COPY_AND_ASSIGN(MediaSession); |
| 208 }; | 212 }; |
| 209 | 213 |
| 210 } // namespace content | 214 } // namespace content |
| 211 | 215 |
| 212 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_ | 216 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_ |
| OLD | NEW |