Chromium Code Reviews| 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_ANDROID_MEDIA_SESSION_H_ | 5 #ifndef CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_ |
| 6 #define CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_ | 6 #define CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_ |
| 7 | 7 |
| 8 #include <jni.h> | |
| 9 #include <stddef.h> | 8 #include <stddef.h> |
| 10 | 9 |
| 11 #include "base/android/scoped_java_ref.h" | |
| 12 #include "base/id_map.h" | 10 #include "base/id_map.h" |
| 13 #include "base/macros.h" | 11 #include "base/macros.h" |
| 14 #include "content/browser/media/android/media_session_uma_helper.h" | 12 #include "content/browser/media/session/media_session_uma_helper.h" |
| 15 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
| 16 #include "content/public/browser/web_contents_observer.h" | 14 #include "content/public/browser/web_contents_observer.h" |
| 17 #include "content/public/browser/web_contents_user_data.h" | 15 #include "content/public/browser/web_contents_user_data.h" |
| 18 | 16 |
| 19 class MediaSessionBrowserTest; | 17 class MediaSessionBrowserTest; |
| 20 | 18 |
| 21 namespace content { | 19 namespace content { |
| 22 | 20 |
| 21 class MediaSessionDelegate; | |
| 23 class MediaSessionObserver; | 22 class MediaSessionObserver; |
| 24 | 23 |
| 25 // MediaSession manages the Android AudioFocus for a given WebContents. It is | 24 // MediaSession manages the media session and audio focus for a given |
| 26 // requesting the audio focus, pausing when requested by the system and dropping | 25 // WebContents. It is requesting the audio focus, pausing when requested by the |
| 27 // it on demand. | 26 // system and dropping it on demand. |
| 28 // The audio focus can be of two types: Transient or Content. A Transient audio | 27 // The audio focus can be of two types: Transient or Content. A Transient audio |
| 29 // focus will allow other players to duck instead of pausing and will be | 28 // focus will allow other players to duck instead of pausing and will be |
| 30 // declared as temporary to the system. A Content audio focus will not be | 29 // declared as temporary to the system. A Content audio focus will not be |
| 31 // declared as temporary and will not allow other players to duck. If a given | 30 // declared as temporary and will not allow other players to duck. If a given |
| 32 // WebContents can only have one audio focus at a time, it will be Content in | 31 // WebContents can only have one audio focus at a time, it will be Content in |
| 33 // case of Transient and Content audio focus are both requested. | 32 // case of Transient and Content audio focus are both requested. |
| 34 // Android system interaction occurs in the Java counterpart to this class. | |
| 35 class CONTENT_EXPORT MediaSession | 33 class CONTENT_EXPORT MediaSession |
| 36 : public WebContentsObserver, | 34 : public WebContentsObserver, |
| 37 protected WebContentsUserData<MediaSession> { | 35 protected WebContentsUserData<MediaSession> { |
| 38 public: | 36 public: |
| 39 enum class Type { | 37 enum class Type { |
| 40 Content, | 38 Content, |
| 41 Transient | 39 Transient |
| 42 }; | 40 }; |
| 43 | 41 |
| 44 static bool RegisterMediaSession(JNIEnv* env); | 42 enum class SuspendType { |
| 43 // Suspended by the system because a transient sound needs to be played. | |
| 44 SYSTEM, | |
| 45 // Suspended by the UI. | |
| 46 UI, | |
| 47 // Suspended by the page via script or user interaction. | |
| 48 CONTENT, | |
| 49 }; | |
| 45 | 50 |
| 46 // Returns the MediaSession associated to this WebContents. Creates one if | 51 // Returns the MediaSession associated to this WebContents. Creates one if |
| 47 // none is currently available. | 52 // none is currently available. |
| 48 static MediaSession* Get(WebContents* web_contents); | 53 static MediaSession* Get(WebContents* web_contents); |
| 49 | 54 |
| 50 ~MediaSession() override; | 55 ~MediaSession() override; |
| 51 | 56 |
| 52 // Adds the given player to the current media session. Returns whether the | 57 // Adds the given player to the current media session. Returns whether the |
| 53 // player was successfully added. If it returns false, AddPlayer() should be | 58 // player was successfully added. If it returns false, AddPlayer() should be |
| 54 // called again later. | 59 // called again later. |
| 55 bool AddPlayer(MediaSessionObserver* observer, int player_id, Type type); | 60 bool AddPlayer(MediaSessionObserver* observer, int player_id, Type type); |
| 56 | 61 |
| 57 // Removes the given player from the current media session. Abandons audio | 62 // Removes the given player from the current media session. Abandons audio |
| 58 // focus if that was the last player in the session. | 63 // focus if that was the last player in the session. |
| 59 void RemovePlayer(MediaSessionObserver* observer, int player_id); | 64 void RemovePlayer(MediaSessionObserver* observer, int player_id); |
| 60 | 65 |
| 61 // Removes all the players associated with |observer|. Abandons audio focus if | 66 // Removes all the players associated with |observer|. Abandons audio focus if |
| 62 // these were the last players in the session. | 67 // these were the last players in the session. |
| 63 void RemovePlayers(MediaSessionObserver* observer); | 68 void RemovePlayers(MediaSessionObserver* observer); |
| 64 | 69 |
| 65 // Called when the Android system requests the MediaSession to be suspended. | 70 // Record that the session was ducked. |
| 66 // Called by Java through JNI. | 71 void RecordSessionDuck(); |
| 67 void OnSuspend(JNIEnv* env, | |
| 68 const base::android::JavaParamRef<jobject>& obj, | |
| 69 jboolean temporary); | |
| 70 | |
| 71 // Called when the Android system requests the MediaSession to duck. | |
| 72 // Called by Java through JNI. | |
| 73 void OnSetVolumeMultiplier(JNIEnv* env, jobject obj, | |
| 74 jdouble volume_multiplier); | |
| 75 | |
| 76 // Called when the Android system requests the MediaSession to be resumed. | |
| 77 // Called by Java through JNI. | |
| 78 void OnResume(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj); | |
| 79 | |
| 80 // Called when the Android system requests the MediaSession to duck. | |
| 81 // Called by Java through JNI. | |
| 82 void RecordSessionDuck(JNIEnv* env, | |
| 83 const base::android::JavaParamRef<jobject> &obj); | |
| 84 | 72 |
| 85 // Called when a player is paused in the content. | 73 // Called when a player is paused in the content. |
| 86 // If the paused player is the last player, we suspend the MediaSession. | 74 // If the paused player is the last player, we suspend the MediaSession. |
| 87 // Otherwise, the paused player will be removed from the MediaSession. | 75 // Otherwise, the paused player will be removed from the MediaSession. |
| 88 void OnPlayerPaused(MediaSessionObserver* observer, int player_id); | 76 void OnPlayerPaused(MediaSessionObserver* observer, int player_id); |
| 89 | 77 |
| 90 // Called when the user requests resuming the session. No-op if the session is | 78 // Resume the media session. |
| 91 // not controllable. | 79 // |type| represents the origin of the request. |
| 92 void Resume(); | 80 void Resume(SuspendType type); |
| 93 | 81 |
| 94 // Called when the user requests suspending the session. No-op if the session | 82 // Suspend the media session. |
| 95 // is not controllable. | 83 // |type| represents the origin of the request. |
| 96 void Suspend(); | 84 void Suspend(SuspendType type); |
| 97 | 85 |
| 98 // Called when the user requests stopping the session. | 86 // Stop the media session. |
| 99 void Stop(); | 87 // |type| represents the origin of the request. |
| 88 void Stop(SuspendType type); | |
| 89 | |
| 90 // Change the volume multiplier of the session to |volume_multiplier|. | |
| 91 void SetVolumeMultiplier(double volume_multiplier); | |
| 100 | 92 |
| 101 // Returns if the session can be controlled by Resume() and Suspend calls | 93 // Returns if the session can be controlled by Resume() and Suspend calls |
| 102 // above. | 94 // above. |
| 103 bool IsControllable() const; | 95 bool IsControllable() const; |
| 104 | 96 |
| 97 // Returns if the session is currently active. | |
| 98 bool IsActive() const; | |
| 99 | |
| 105 // Returns if the session is currently suspended. | 100 // Returns if the session is currently suspended. |
| 101 // TODO(mlamouri): IsSuspended() is basically !IsActive() but in order to not | |
| 102 // have a ridiculously huge refactoring, the change to fix that will happen in | |
| 103 // a follow-up. | |
| 104 bool IsReallySuspended() const; | |
| 105 | |
| 106 // Returns if the session is currently suspended or inactive. | |
| 106 bool IsSuspended() const; | 107 bool IsSuspended() const; |
| 107 | 108 |
| 108 private: | 109 private: |
| 109 friend class content::WebContentsUserData<MediaSession>; | 110 friend class content::WebContentsUserData<MediaSession>; |
| 110 friend class ::MediaSessionBrowserTest; | 111 friend class ::MediaSessionBrowserTest; |
| 111 | 112 |
| 112 // Resets the |j_media_session_| ref to prevent calling the Java backend | 113 void SetDelegateForTest(scoped_ptr<MediaSessionDelegate> delegate); |
|
DaleCurtis
2016/02/26 03:22:50
You need to use ForTesting or ForTests otherwise t
mlamouri (slow - plz ping)
2016/02/26 12:26:17
Done.
| |
| 113 // during content_browsertests. | |
| 114 void ResetJavaRefForTest(); | |
| 115 bool IsActiveForTest() const; | 114 bool IsActiveForTest() const; |
| 116 Type audio_focus_type_for_test() const; | 115 Type audio_focus_type_for_test() const; |
| 117 void RemoveAllPlayersForTest(); | 116 void RemoveAllPlayersForTest(); |
| 118 MediaSessionUmaHelper* uma_helper_for_test(); | 117 MediaSessionUmaHelper* uma_helper_for_test(); |
| 119 | 118 |
| 120 enum class State { | 119 enum class State { |
| 121 ACTIVE, | 120 ACTIVE, |
| 122 SUSPENDED, | 121 SUSPENDED, |
| 123 INACTIVE | 122 INACTIVE |
| 124 }; | 123 }; |
| 125 | 124 |
| 126 enum class SuspendType { | |
| 127 // Suspended by the system because a transient sound needs to be played. | |
| 128 SYSTEM, | |
| 129 // Suspended by the UI. | |
| 130 UI, | |
| 131 // Suspended by the page via script or user interaction. | |
| 132 CONTENT, | |
| 133 }; | |
| 134 | |
| 135 // Representation of a player for the MediaSession. | 125 // Representation of a player for the MediaSession. |
| 136 struct PlayerIdentifier { | 126 struct PlayerIdentifier { |
| 137 PlayerIdentifier(MediaSessionObserver* observer, int player_id); | 127 PlayerIdentifier(MediaSessionObserver* observer, int player_id); |
| 138 PlayerIdentifier(const PlayerIdentifier&) = default; | 128 PlayerIdentifier(const PlayerIdentifier&) = default; |
| 139 | 129 |
| 140 void operator=(const PlayerIdentifier&) = delete; | 130 void operator=(const PlayerIdentifier&) = delete; |
| 141 bool operator==(const PlayerIdentifier& player_identifier) const; | 131 bool operator==(const PlayerIdentifier& player_identifier) const; |
| 142 | 132 |
| 143 // Hash operator for base::hash_map<>. | 133 // Hash operator for base::hash_map<>. |
| 144 struct Hash { | 134 struct Hash { |
| 145 size_t operator()(const PlayerIdentifier& player_identifier) const; | 135 size_t operator()(const PlayerIdentifier& player_identifier) const; |
| 146 }; | 136 }; |
| 147 | 137 |
| 148 MediaSessionObserver* observer; | 138 MediaSessionObserver* observer; |
| 149 int player_id; | 139 int player_id; |
| 150 }; | 140 }; |
| 151 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>; | 141 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>; |
| 152 | 142 |
| 153 explicit MediaSession(WebContents* web_contents); | 143 explicit MediaSession(WebContents* web_contents); |
| 154 | 144 |
| 155 // Setup the JNI. | |
| 156 void Initialize(); | 145 void Initialize(); |
| 157 | 146 |
| 158 void OnSuspendInternal(SuspendType type, State new_state); | 147 void OnSuspendInternal(SuspendType type, State new_state); |
| 159 void OnResumeInternal(SuspendType type); | 148 void OnResumeInternal(SuspendType type); |
| 160 void OnSetVolumeMultiplierInternal(double volume_multiplier); | |
| 161 | 149 |
| 162 // Requests audio focus to Android using |j_media_session_|. | 150 // Requests audio focus to the MediaSessionDelegate. |
| 163 // Returns whether the request was granted. If |j_media_session_| is null, it | 151 // Returns whether the request was granted. |
| 164 // will always return true. | |
| 165 bool RequestSystemAudioFocus(Type type); | 152 bool RequestSystemAudioFocus(Type type); |
| 166 | 153 |
| 167 // To be called after a call to AbandonAudioFocus() in order to call the Java | 154 // To be called after a call to AbandonAudioFocus() in order request the |
| 168 // MediaSession if the audio focus really need to be abandoned. | 155 // delegate to abandon the audio focus. |
| 169 void AbandonSystemAudioFocusIfNeeded(); | 156 void AbandonSystemAudioFocusIfNeeded(); |
| 170 | 157 |
| 171 // Notifies WebContents about the state change of the media session. | 158 // Notifies WebContents about the state change of the media session. |
| 172 void UpdateWebContents(); | 159 void UpdateWebContents(); |
| 173 | 160 |
| 174 // Internal method that should be used instead of setting audio_focus_state_. | 161 // Internal method that should be used instead of setting audio_focus_state_. |
| 175 // It sets audio_focus_state_ and notifies observers about the state change. | 162 // It sets audio_focus_state_ and notifies observers about the state change. |
| 176 void SetAudioFocusState(State audio_focus_state); | 163 void SetAudioFocusState(State audio_focus_state); |
| 177 | 164 |
| 178 base::android::ScopedJavaGlobalRef<jobject> j_media_session_; | 165 scoped_ptr<MediaSessionDelegate> delegate_; |
| 179 PlayersMap players_; | 166 PlayersMap players_; |
| 180 | 167 |
| 181 State audio_focus_state_; | 168 State audio_focus_state_; |
| 182 SuspendType suspend_type_; | 169 SuspendType suspend_type_; |
| 183 Type audio_focus_type_; | 170 Type audio_focus_type_; |
| 184 | 171 |
| 185 MediaSessionUmaHelper uma_helper_; | 172 MediaSessionUmaHelper uma_helper_; |
| 186 | 173 |
| 187 // The volume multiplier of this session. All players in this session should | 174 // The volume multiplier of this session. All players in this session should |
| 188 // multiply their volume with this multiplier to get the effective volume. | 175 // multiply their volume with this multiplier to get the effective volume. |
| 189 double volume_multiplier_; | 176 double volume_multiplier_; |
| 190 | 177 |
| 191 DISALLOW_COPY_AND_ASSIGN(MediaSession); | 178 DISALLOW_COPY_AND_ASSIGN(MediaSession); |
| 192 }; | 179 }; |
| 193 | 180 |
| 194 } // namespace content | 181 } // namespace content |
| 195 | 182 |
| 196 #endif // CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_ | 183 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_ |
| OLD | NEW |