OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_ | |
6 #define CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_ | |
7 | |
8 #include <jni.h> | |
9 #include <stddef.h> | |
10 | |
11 #include "base/android/scoped_java_ref.h" | |
12 #include "base/id_map.h" | |
13 #include "base/macros.h" | |
14 #include "content/browser/media/android/media_session_uma_helper.h" | |
15 #include "content/common/content_export.h" | |
16 #include "content/public/browser/web_contents_observer.h" | |
17 #include "content/public/browser/web_contents_user_data.h" | |
18 | |
19 class MediaSessionBrowserTest; | |
20 | |
21 namespace content { | |
22 | |
23 class MediaSessionObserver; | |
24 | |
25 // MediaSession manages the Android AudioFocus for a given WebContents. It is | |
26 // requesting the audio focus, pausing when requested by the system and dropping | |
27 // it on demand. | |
28 // 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 | |
30 // 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 | |
32 // 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. | |
34 // Android system interaction occurs in the Java counterpart to this class. | |
35 class CONTENT_EXPORT MediaSession | |
36 : public WebContentsObserver, | |
37 protected WebContentsUserData<MediaSession> { | |
38 public: | |
39 enum class Type { | |
40 Content, | |
41 Transient | |
42 }; | |
43 | |
44 static bool RegisterMediaSession(JNIEnv* env); | |
45 | |
46 // Returns the MediaSession associated to this WebContents. Creates one if | |
47 // none is currently available. | |
48 static MediaSession* Get(WebContents* web_contents); | |
49 | |
50 ~MediaSession() override; | |
51 | |
52 // Adds the given player to the current media session. Returns whether the | |
53 // player was successfully added. If it returns false, AddPlayer() should be | |
54 // called again later. | |
55 bool AddPlayer(MediaSessionObserver* observer, int player_id, Type type); | |
56 | |
57 // Removes the given player from the current media session. Abandons audio | |
58 // focus if that was the last player in the session. | |
59 void RemovePlayer(MediaSessionObserver* observer, int player_id); | |
60 | |
61 // Removes all the players associated with |observer|. Abandons audio focus if | |
62 // these were the last players in the session. | |
63 void RemovePlayers(MediaSessionObserver* observer); | |
64 | |
65 // Called when the Android system requests the MediaSession to be suspended. | |
66 // Called by Java through JNI. | |
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 | |
85 // Called when a player is paused in the content. | |
86 // If the paused player is the last player, we suspend the MediaSession. | |
87 // Otherwise, the paused player will be removed from the MediaSession. | |
88 void OnPlayerPaused(MediaSessionObserver* observer, int player_id); | |
89 | |
90 // Called when the user requests resuming the session. No-op if the session is | |
91 // not controllable. | |
92 void Resume(); | |
93 | |
94 // Called when the user requests suspending the session. No-op if the session | |
95 // is not controllable. | |
96 void Suspend(); | |
97 | |
98 // Called when the user requests stopping the session. | |
99 void Stop(); | |
100 | |
101 // Returns if the session can be controlled by Resume() and Suspend calls | |
102 // above. | |
103 bool IsControllable() const; | |
104 | |
105 // Returns if the session is currently suspended. | |
106 bool IsSuspended() const; | |
107 | |
108 private: | |
109 friend class content::WebContentsUserData<MediaSession>; | |
110 friend class ::MediaSessionBrowserTest; | |
111 | |
112 // Resets the |j_media_session_| ref to prevent calling the Java backend | |
113 // during content_browsertests. | |
114 void ResetJavaRefForTest(); | |
115 bool IsActiveForTest() const; | |
116 Type audio_focus_type_for_test() const; | |
117 void RemoveAllPlayersForTest(); | |
118 MediaSessionUmaHelper* uma_helper_for_test(); | |
119 | |
120 enum class State { | |
121 ACTIVE, | |
122 SUSPENDED, | |
123 INACTIVE | |
124 }; | |
125 | |
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. | |
136 struct PlayerIdentifier { | |
137 PlayerIdentifier(MediaSessionObserver* observer, int player_id); | |
138 PlayerIdentifier(const PlayerIdentifier&) = default; | |
139 | |
140 void operator=(const PlayerIdentifier&) = delete; | |
141 bool operator==(const PlayerIdentifier& player_identifier) const; | |
142 | |
143 // Hash operator for base::hash_map<>. | |
144 struct Hash { | |
145 size_t operator()(const PlayerIdentifier& player_identifier) const; | |
146 }; | |
147 | |
148 MediaSessionObserver* observer; | |
149 int player_id; | |
150 }; | |
151 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>; | |
152 | |
153 explicit MediaSession(WebContents* web_contents); | |
154 | |
155 // Setup the JNI. | |
156 void Initialize(); | |
157 | |
158 void OnSuspendInternal(SuspendType type, State new_state); | |
159 void OnResumeInternal(SuspendType type); | |
160 void OnSetVolumeMultiplierInternal(double volume_multiplier); | |
161 | |
162 // Requests audio focus to Android using |j_media_session_|. | |
163 // Returns whether the request was granted. If |j_media_session_| is null, it | |
164 // will always return true. | |
165 bool RequestSystemAudioFocus(Type type); | |
166 | |
167 // To be called after a call to AbandonAudioFocus() in order to call the Java | |
168 // MediaSession if the audio focus really need to be abandoned. | |
169 void AbandonSystemAudioFocusIfNeeded(); | |
170 | |
171 // Notifies WebContents about the state change of the media session. | |
172 void UpdateWebContents(); | |
173 | |
174 // 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. | |
176 void SetAudioFocusState(State audio_focus_state); | |
177 | |
178 base::android::ScopedJavaGlobalRef<jobject> j_media_session_; | |
179 PlayersMap players_; | |
180 | |
181 State audio_focus_state_; | |
182 SuspendType suspend_type_; | |
183 Type audio_focus_type_; | |
184 | |
185 MediaSessionUmaHelper uma_helper_; | |
186 | |
187 // The volume multiplier of this session. All players in this session should | |
188 // multiply their volume with this multiplier to get the effective volume. | |
189 double volume_multiplier_; | |
190 | |
191 DISALLOW_COPY_AND_ASSIGN(MediaSession); | |
192 }; | |
193 | |
194 } // namespace content | |
195 | |
196 #endif // CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_ | |
OLD | NEW |