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

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: rebase 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
« no previous file with comments | « content/browser/media/session/OWNERS ('k') | content/browser/media/session/media_session.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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() below checks if the state is not ACTIVE
109 // instead of checking if the state is SUSPENDED. In order to not have to
110 // change all the callers and make the current refactoring ridiculously huge,
111 // this method is introduced temporarily and will be removed later.
112 bool IsReallySuspended() const;
113
114 // Returns if the session is currently suspended or inactive.
115 CONTENT_EXPORT bool IsSuspended() const;
107 116
108 private: 117 private:
109 friend class content::WebContentsUserData<MediaSession>; 118 friend class content::WebContentsUserData<MediaSession>;
110 friend class ::MediaSessionBrowserTest; 119 friend class ::MediaSessionBrowserTest;
111 120
112 // Resets the |j_media_session_| ref to prevent calling the Java backend 121 CONTENT_EXPORT void SetDelegateForTests(
113 // during content_browsertests. 122 scoped_ptr<MediaSessionDelegate> delegate);
114 void ResetJavaRefForTest(); 123 CONTENT_EXPORT bool IsActiveForTest() const;
115 bool IsActiveForTest() const; 124 CONTENT_EXPORT Type audio_focus_type_for_test() const;
116 Type audio_focus_type_for_test() const; 125 CONTENT_EXPORT void RemoveAllPlayersForTest();
117 void RemoveAllPlayersForTest(); 126 CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test();
118 MediaSessionUmaHelper* uma_helper_for_test();
119 127
120 enum class State { 128 enum class State {
121 ACTIVE, 129 ACTIVE,
122 SUSPENDED, 130 SUSPENDED,
123 INACTIVE 131 INACTIVE
124 }; 132 };
125 133
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. 134 // Representation of a player for the MediaSession.
136 struct PlayerIdentifier { 135 struct PlayerIdentifier {
137 PlayerIdentifier(MediaSessionObserver* observer, int player_id); 136 PlayerIdentifier(MediaSessionObserver* observer, int player_id);
138 PlayerIdentifier(const PlayerIdentifier&) = default; 137 PlayerIdentifier(const PlayerIdentifier&) = default;
139 138
140 void operator=(const PlayerIdentifier&) = delete; 139 void operator=(const PlayerIdentifier&) = delete;
141 bool operator==(const PlayerIdentifier& player_identifier) const; 140 bool operator==(const PlayerIdentifier& player_identifier) const;
142 141
143 // Hash operator for base::hash_map<>. 142 // Hash operator for base::hash_map<>.
144 struct Hash { 143 struct Hash {
145 size_t operator()(const PlayerIdentifier& player_identifier) const; 144 size_t operator()(const PlayerIdentifier& player_identifier) const;
146 }; 145 };
147 146
148 MediaSessionObserver* observer; 147 MediaSessionObserver* observer;
149 int player_id; 148 int player_id;
150 }; 149 };
151 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>; 150 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>;
152 151
153 explicit MediaSession(WebContents* web_contents); 152 CONTENT_EXPORT explicit MediaSession(WebContents* web_contents);
154 153
155 // Setup the JNI.
156 void Initialize(); 154 void Initialize();
157 155
158 void OnSuspendInternal(SuspendType type, State new_state); 156 CONTENT_EXPORT void OnSuspendInternal(SuspendType type, State new_state);
159 void OnResumeInternal(SuspendType type); 157 CONTENT_EXPORT void OnResumeInternal(SuspendType type);
160 void OnSetVolumeMultiplierInternal(double volume_multiplier);
161 158
162 // Requests audio focus to Android using |j_media_session_|. 159 // Requests audio focus to the MediaSessionDelegate.
163 // Returns whether the request was granted. If |j_media_session_| is null, it 160 // Returns whether the request was granted.
164 // will always return true.
165 bool RequestSystemAudioFocus(Type type); 161 bool RequestSystemAudioFocus(Type type);
166 162
167 // To be called after a call to AbandonAudioFocus() in order to call the Java 163 // To be called after a call to AbandonAudioFocus() in order request the
168 // MediaSession if the audio focus really need to be abandoned. 164 // delegate to abandon the audio focus.
169 void AbandonSystemAudioFocusIfNeeded(); 165 void AbandonSystemAudioFocusIfNeeded();
170 166
171 // Notifies WebContents about the state change of the media session. 167 // Notifies WebContents about the state change of the media session.
172 void UpdateWebContents(); 168 void UpdateWebContents();
173 169
174 // Internal method that should be used instead of setting audio_focus_state_. 170 // 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. 171 // It sets audio_focus_state_ and notifies observers about the state change.
176 void SetAudioFocusState(State audio_focus_state); 172 void SetAudioFocusState(State audio_focus_state);
177 173
178 base::android::ScopedJavaGlobalRef<jobject> j_media_session_; 174 scoped_ptr<MediaSessionDelegate> delegate_;
179 PlayersMap players_; 175 PlayersMap players_;
180 176
181 State audio_focus_state_; 177 State audio_focus_state_;
182 SuspendType suspend_type_; 178 SuspendType suspend_type_;
183 Type audio_focus_type_; 179 Type audio_focus_type_;
184 180
185 MediaSessionUmaHelper uma_helper_; 181 MediaSessionUmaHelper uma_helper_;
186 182
187 // The volume multiplier of this session. All players in this session should 183 // The volume multiplier of this session. All players in this session should
188 // multiply their volume with this multiplier to get the effective volume. 184 // multiply their volume with this multiplier to get the effective volume.
189 double volume_multiplier_; 185 double volume_multiplier_;
190 186
191 DISALLOW_COPY_AND_ASSIGN(MediaSession); 187 DISALLOW_COPY_AND_ASSIGN(MediaSession);
192 }; 188 };
193 189
194 } // namespace content 190 } // namespace content
195 191
196 #endif // CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_ 192 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_
OLDNEW
« no previous file with comments | « content/browser/media/session/OWNERS ('k') | content/browser/media/session/media_session.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698