Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: content/browser/media/session/media_session.h

Issue 1698933004: Make MediaSession a runtime-enabled feature on Desktop. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review comments and windows build fix Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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. 33 // TODO(thakis,mlamouri): MediaSession isn't CONTENT_EXPORT'd because it creates
35 class CONTENT_EXPORT MediaSession 34 // complicated build issues with WebContentsUserData being a non-exported
36 : public WebContentsObserver, 35 // template, see htttps://crbug.com/589840. As a result, the class uses
37 protected WebContentsUserData<MediaSession> { 36 // CONTENT_EXPORT for methods that are being used from tests. CONTENT_EXPORT
37 // should be moved back to the class when the Windows build will work with it.
38 class MediaSession : public WebContentsObserver,
39 protected WebContentsUserData<MediaSession> {
38 public: 40 public:
39 enum class Type { 41 enum class Type {
40 Content, 42 Content,
41 Transient 43 Transient
42 }; 44 };
43 45
44 static bool RegisterMediaSession(JNIEnv* env); 46 enum class SuspendType {
47 // Suspended by the system because a transient sound needs to be played.
48 SYSTEM,
49 // Suspended by the UI.
50 UI,
51 // Suspended by the page via script or user interaction.
52 CONTENT,
53 };
45 54
46 // Returns the MediaSession associated to this WebContents. Creates one if 55 // Returns the MediaSession associated to this WebContents. Creates one if
47 // none is currently available. 56 // none is currently available.
48 static MediaSession* Get(WebContents* web_contents); 57 CONTENT_EXPORT static MediaSession* Get(WebContents* web_contents);
49 58
50 ~MediaSession() override; 59 ~MediaSession() override;
51 60
52 // Adds the given player to the current media session. Returns whether the 61 // Adds the given player to the current media session. Returns whether the
53 // player was successfully added. If it returns false, AddPlayer() should be 62 // player was successfully added. If it returns false, AddPlayer() should be
54 // called again later. 63 // called again later.
55 bool AddPlayer(MediaSessionObserver* observer, int player_id, Type type); 64 CONTENT_EXPORT bool AddPlayer(MediaSessionObserver* observer,
65 int player_id, Type type);
56 66
57 // Removes the given player from the current media session. Abandons audio 67 // Removes the given player from the current media session. Abandons audio
58 // focus if that was the last player in the session. 68 // focus if that was the last player in the session.
59 void RemovePlayer(MediaSessionObserver* observer, int player_id); 69 CONTENT_EXPORT void RemovePlayer(MediaSessionObserver* observer,
70 int player_id);
60 71
61 // Removes all the players associated with |observer|. Abandons audio focus if 72 // Removes all the players associated with |observer|. Abandons audio focus if
62 // these were the last players in the session. 73 // these were the last players in the session.
63 void RemovePlayers(MediaSessionObserver* observer); 74 CONTENT_EXPORT void RemovePlayers(MediaSessionObserver* observer);
64 75
65 // Called when the Android system requests the MediaSession to be suspended. 76 // Record that the session was ducked.
66 // Called by Java through JNI. 77 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 78
85 // Called when a player is paused in the content. 79 // Called when a player is paused in the content.
86 // If the paused player is the last player, we suspend the MediaSession. 80 // If the paused player is the last player, we suspend the MediaSession.
87 // Otherwise, the paused player will be removed from the MediaSession. 81 // Otherwise, the paused player will be removed from the MediaSession.
88 void OnPlayerPaused(MediaSessionObserver* observer, int player_id); 82 CONTENT_EXPORT void OnPlayerPaused(MediaSessionObserver* observer,
83 int player_id);
89 84
90 // Called when the user requests resuming the session. No-op if the session is 85 // Resume the media session.
91 // not controllable. 86 // |type| represents the origin of the request.
92 void Resume(); 87 CONTENT_EXPORT void Resume(SuspendType type);
93 88
94 // Called when the user requests suspending the session. No-op if the session 89 // Suspend the media session.
95 // is not controllable. 90 // |type| represents the origin of the request.
96 void Suspend(); 91 CONTENT_EXPORT void Suspend(SuspendType type);
97 92
98 // Called when the user requests stopping the session. 93 // Stop the media session.
99 void Stop(); 94 // |type| represents the origin of the request.
95 CONTENT_EXPORT void Stop(SuspendType type);
96
97 // Change the volume multiplier of the session to |volume_multiplier|.
98 CONTENT_EXPORT void SetVolumeMultiplier(double volume_multiplier);
100 99
101 // Returns if the session can be controlled by Resume() and Suspend calls 100 // Returns if the session can be controlled by Resume() and Suspend calls
102 // above. 101 // above.
103 bool IsControllable() const; 102 CONTENT_EXPORT bool IsControllable() const;
103
104 // Returns if the session is currently active.
105 CONTENT_EXPORT bool IsActive() const;
104 106
105 // Returns if the session is currently suspended. 107 // Returns if the session is currently suspended.
106 bool IsSuspended() const; 108 // TODO(mlamouri): IsSuspended() is basically !IsActive() but in order to not
109 // have a ridiculously huge refactoring, the change to fix that will happen in
110 // a follow-up.
111 bool IsReallySuspended() const;
112
113 // Returns if the session is currently suspended or inactive.
114 CONTENT_EXPORT bool IsSuspended() const;
107 115
108 private: 116 private:
109 friend class content::WebContentsUserData<MediaSession>; 117 friend class content::WebContentsUserData<MediaSession>;
110 friend class ::MediaSessionBrowserTest; 118 friend class ::MediaSessionBrowserTest;
111 119
112 // Resets the |j_media_session_| ref to prevent calling the Java backend 120 CONTENT_EXPORT void SetDelegateForTests(
113 // during content_browsertests. 121 scoped_ptr<MediaSessionDelegate> delegate);
114 void ResetJavaRefForTest(); 122 CONTENT_EXPORT bool IsActiveForTest() const;
115 bool IsActiveForTest() const; 123 CONTENT_EXPORT Type audio_focus_type_for_test() const;
116 Type audio_focus_type_for_test() const; 124 CONTENT_EXPORT void RemoveAllPlayersForTest();
117 void RemoveAllPlayersForTest(); 125 CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test();
118 MediaSessionUmaHelper* uma_helper_for_test();
119 126
120 enum class State { 127 enum class State {
121 ACTIVE, 128 ACTIVE,
122 SUSPENDED, 129 SUSPENDED,
123 INACTIVE 130 INACTIVE
124 }; 131 };
125 132
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. 133 // Representation of a player for the MediaSession.
136 struct PlayerIdentifier { 134 struct PlayerIdentifier {
137 PlayerIdentifier(MediaSessionObserver* observer, int player_id); 135 PlayerIdentifier(MediaSessionObserver* observer, int player_id);
138 PlayerIdentifier(const PlayerIdentifier&) = default; 136 PlayerIdentifier(const PlayerIdentifier&) = default;
139 137
140 void operator=(const PlayerIdentifier&) = delete; 138 void operator=(const PlayerIdentifier&) = delete;
141 bool operator==(const PlayerIdentifier& player_identifier) const; 139 bool operator==(const PlayerIdentifier& player_identifier) const;
142 140
143 // Hash operator for base::hash_map<>. 141 // Hash operator for base::hash_map<>.
144 struct Hash { 142 struct Hash {
145 size_t operator()(const PlayerIdentifier& player_identifier) const; 143 size_t operator()(const PlayerIdentifier& player_identifier) const;
146 }; 144 };
147 145
148 MediaSessionObserver* observer; 146 MediaSessionObserver* observer;
149 int player_id; 147 int player_id;
150 }; 148 };
151 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>; 149 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>;
152 150
153 explicit MediaSession(WebContents* web_contents); 151 CONTENT_EXPORT explicit MediaSession(WebContents* web_contents);
154 152
155 // Setup the JNI.
156 void Initialize(); 153 void Initialize();
157 154
158 void OnSuspendInternal(SuspendType type, State new_state); 155 CONTENT_EXPORT void OnSuspendInternal(SuspendType type, State new_state);
159 void OnResumeInternal(SuspendType type); 156 CONTENT_EXPORT void OnResumeInternal(SuspendType type);
160 void OnSetVolumeMultiplierInternal(double volume_multiplier);
161 157
162 // Requests audio focus to Android using |j_media_session_|. 158 // Requests audio focus to the MediaSessionDelegate.
163 // Returns whether the request was granted. If |j_media_session_| is null, it 159 // Returns whether the request was granted.
164 // will always return true.
165 bool RequestSystemAudioFocus(Type type); 160 bool RequestSystemAudioFocus(Type type);
166 161
167 // To be called after a call to AbandonAudioFocus() in order to call the Java 162 // To be called after a call to AbandonAudioFocus() in order request the
168 // MediaSession if the audio focus really need to be abandoned. 163 // delegate to abandon the audio focus.
169 void AbandonSystemAudioFocusIfNeeded(); 164 void AbandonSystemAudioFocusIfNeeded();
170 165
171 // Notifies WebContents about the state change of the media session. 166 // Notifies WebContents about the state change of the media session.
172 void UpdateWebContents(); 167 void UpdateWebContents();
173 168
174 // Internal method that should be used instead of setting audio_focus_state_. 169 // 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. 170 // It sets audio_focus_state_ and notifies observers about the state change.
176 void SetAudioFocusState(State audio_focus_state); 171 void SetAudioFocusState(State audio_focus_state);
177 172
178 base::android::ScopedJavaGlobalRef<jobject> j_media_session_; 173 scoped_ptr<MediaSessionDelegate> delegate_;
179 PlayersMap players_; 174 PlayersMap players_;
180 175
181 State audio_focus_state_; 176 State audio_focus_state_;
182 SuspendType suspend_type_; 177 SuspendType suspend_type_;
183 Type audio_focus_type_; 178 Type audio_focus_type_;
184 179
185 MediaSessionUmaHelper uma_helper_; 180 MediaSessionUmaHelper uma_helper_;
186 181
187 // The volume multiplier of this session. All players in this session should 182 // The volume multiplier of this session. All players in this session should
188 // multiply their volume with this multiplier to get the effective volume. 183 // multiply their volume with this multiplier to get the effective volume.
189 double volume_multiplier_; 184 double volume_multiplier_;
190 185
191 DISALLOW_COPY_AND_ASSIGN(MediaSession); 186 DISALLOW_COPY_AND_ASSIGN(MediaSession);
192 }; 187 };
193 188
194 } // namespace content 189 } // namespace content
195 190
196 #endif // CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_ 191 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698