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

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: with flag Created 4 years, 10 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.
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 };
50
45 51
46 // Returns the MediaSession associated to this WebContents. Creates one if 52 // Returns the MediaSession associated to this WebContents. Creates one if
47 // none is currently available. 53 // none is currently available.
48 static MediaSession* Get(WebContents* web_contents); 54 static MediaSession* Get(WebContents* web_contents);
49 55
50 ~MediaSession() override; 56 ~MediaSession() override;
51 57
52 // Adds the given player to the current media session. Returns whether the 58 // Adds the given player to the current media session. Returns whether the
53 // player was successfully added. If it returns false, AddPlayer() should be 59 // player was successfully added. If it returns false, AddPlayer() should be
54 // called again later. 60 // called again later.
55 bool AddPlayer(MediaSessionObserver* observer, int player_id, Type type); 61 bool AddPlayer(MediaSessionObserver* observer, int player_id, Type type);
56 62
57 // Removes the given player from the current media session. Abandons audio 63 // Removes the given player from the current media session. Abandons audio
58 // focus if that was the last player in the session. 64 // focus if that was the last player in the session.
59 void RemovePlayer(MediaSessionObserver* observer, int player_id); 65 void RemovePlayer(MediaSessionObserver* observer, int player_id);
60 66
61 // Removes all the players associated with |observer|. Abandons audio focus if 67 // Removes all the players associated with |observer|. Abandons audio focus if
62 // these were the last players in the session. 68 // these were the last players in the session.
63 void RemovePlayers(MediaSessionObserver* observer); 69 void RemovePlayers(MediaSessionObserver* observer);
64 70
65 // Called when the Android system requests the MediaSession to be suspended. 71 // Record that the session was ducked.
66 // Called by Java through JNI. 72 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 73
85 // Called when a player is paused in the content. 74 // Called when a player is paused in the content.
86 // If the paused player is the last player, we suspend the MediaSession. 75 // If the paused player is the last player, we suspend the MediaSession.
87 // Otherwise, the paused player will be removed from the MediaSession. 76 // Otherwise, the paused player will be removed from the MediaSession.
88 void OnPlayerPaused(MediaSessionObserver* observer, int player_id); 77 void OnPlayerPaused(MediaSessionObserver* observer, int player_id);
89 78
90 // Called when the user requests resuming the session. No-op if the session is 79 // Resume the media session.
91 // not controllable. 80 // |type| represents the origin of the request.
92 void Resume(); 81 void Resume(SuspendType type);
93 82
94 // Called when the user requests suspending the session. No-op if the session 83 // Suspend the media session.
95 // is not controllable. 84 // |type| represents the origin of the request.
96 void Suspend(); 85 void Suspend(SuspendType type);
97 86
98 // Called when the user requests stopping the session. 87 // Stop the media session.
99 void Stop(); 88 // |type| represents the origin of the request.
89 void Stop(SuspendType type);
90
91 // Change the volume multiplier of the session to |volume_multiplier|.
92 void SetVolumeMultiplier(double volume_multiplier);
100 93
101 // Returns if the session can be controlled by Resume() and Suspend calls 94 // Returns if the session can be controlled by Resume() and Suspend calls
102 // above. 95 // above.
103 bool IsControllable() const; 96 bool IsControllable() const;
104 97
98 // Returns if the session is currently active.
99 bool IsActive() const;
100
105 // Returns if the session is currently suspended. 101 // Returns if the session is currently suspended.
102 // TODO(mlamouri): IsSuspended() is basically !IsActive() but in order to not
103 // have a ridiculously huge refactoring, the change to fix that will happen in
104 // a follow-up.
105 bool IsReallySuspended() const;
106
107 // Returns if the session is currently suspended or inactive.
106 bool IsSuspended() const; 108 bool IsSuspended() const;
107 109
108 private: 110 private:
109 friend class content::WebContentsUserData<MediaSession>; 111 friend class content::WebContentsUserData<MediaSession>;
110 friend class ::MediaSessionBrowserTest; 112 friend class ::MediaSessionBrowserTest;
111 113
112 // Resets the |j_media_session_| ref to prevent calling the Java backend 114 void SetDelegateForTest(scoped_ptr<MediaSessionDelegate> delegate);
113 // during content_browsertests.
114 void ResetJavaRefForTest();
115 bool IsActiveForTest() const; 115 bool IsActiveForTest() const;
116 Type audio_focus_type_for_test() const; 116 Type audio_focus_type_for_test() const;
117 void RemoveAllPlayersForTest(); 117 void RemoveAllPlayersForTest();
118 MediaSessionUmaHelper* uma_helper_for_test(); 118 MediaSessionUmaHelper* uma_helper_for_test();
119 119
120 enum class State { 120 enum class State {
121 ACTIVE, 121 ACTIVE,
122 SUSPENDED, 122 SUSPENDED,
123 INACTIVE 123 INACTIVE
124 }; 124 };
125 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. 126 // Representation of a player for the MediaSession.
136 struct PlayerIdentifier { 127 struct PlayerIdentifier {
137 PlayerIdentifier(MediaSessionObserver* observer, int player_id); 128 PlayerIdentifier(MediaSessionObserver* observer, int player_id);
138 PlayerIdentifier(const PlayerIdentifier&) = default; 129 PlayerIdentifier(const PlayerIdentifier&) = default;
139 130
140 void operator=(const PlayerIdentifier&) = delete; 131 void operator=(const PlayerIdentifier&) = delete;
141 bool operator==(const PlayerIdentifier& player_identifier) const; 132 bool operator==(const PlayerIdentifier& player_identifier) const;
142 133
143 // Hash operator for base::hash_map<>. 134 // Hash operator for base::hash_map<>.
144 struct Hash { 135 struct Hash {
145 size_t operator()(const PlayerIdentifier& player_identifier) const; 136 size_t operator()(const PlayerIdentifier& player_identifier) const;
146 }; 137 };
147 138
148 MediaSessionObserver* observer; 139 MediaSessionObserver* observer;
149 int player_id; 140 int player_id;
150 }; 141 };
151 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>; 142 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>;
152 143
153 explicit MediaSession(WebContents* web_contents); 144 explicit MediaSession(WebContents* web_contents);
154 145
155 // Setup the JNI.
156 void Initialize(); 146 void Initialize();
157 147
158 void OnSuspendInternal(SuspendType type, State new_state); 148 void OnSuspendInternal(SuspendType type, State new_state);
159 void OnResumeInternal(SuspendType type); 149 void OnResumeInternal(SuspendType type);
160 void OnSetVolumeMultiplierInternal(double volume_multiplier);
161 150
162 // Requests audio focus to Android using |j_media_session_|. 151 // Requests audio focus to the MediaSessionDelegate.
163 // Returns whether the request was granted. If |j_media_session_| is null, it 152 // Returns whether the request was granted.
164 // will always return true.
165 bool RequestSystemAudioFocus(Type type); 153 bool RequestSystemAudioFocus(Type type);
166 154
167 // To be called after a call to AbandonAudioFocus() in order to call the Java 155 // To be called after a call to AbandonAudioFocus() in order request the
168 // MediaSession if the audio focus really need to be abandoned. 156 // delegate to abandon the audio focus.
169 void AbandonSystemAudioFocusIfNeeded(); 157 void AbandonSystemAudioFocusIfNeeded();
170 158
171 // Notifies WebContents about the state change of the media session. 159 // Notifies WebContents about the state change of the media session.
172 void UpdateWebContents(); 160 void UpdateWebContents();
173 161
174 // Internal method that should be used instead of setting audio_focus_state_. 162 // 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. 163 // It sets audio_focus_state_ and notifies observers about the state change.
176 void SetAudioFocusState(State audio_focus_state); 164 void SetAudioFocusState(State audio_focus_state);
177 165
178 base::android::ScopedJavaGlobalRef<jobject> j_media_session_; 166 scoped_ptr<MediaSessionDelegate> delegate_;
179 PlayersMap players_; 167 PlayersMap players_;
180 168
181 State audio_focus_state_; 169 State audio_focus_state_;
182 SuspendType suspend_type_; 170 SuspendType suspend_type_;
183 Type audio_focus_type_; 171 Type audio_focus_type_;
184 172
185 MediaSessionUmaHelper uma_helper_; 173 MediaSessionUmaHelper uma_helper_;
186 174
187 // The volume multiplier of this session. All players in this session should 175 // The volume multiplier of this session. All players in this session should
188 // multiply their volume with this multiplier to get the effective volume. 176 // multiply their volume with this multiplier to get the effective volume.
189 double volume_multiplier_; 177 double volume_multiplier_;
190 178
191 DISALLOW_COPY_AND_ASSIGN(MediaSession); 179 DISALLOW_COPY_AND_ASSIGN(MediaSession);
192 }; 180 };
193 181
194 } // namespace content 182 } // namespace content
195 183
196 #endif // CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_ 184 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698