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

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

Issue 2453623003: Decouple MediaSession messages from WebContents (full patch) (Closed)
Patch Set: Created 4 years, 1 month 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.
Zhiqiang Zhang (Slow) 2016/10/26 12:50:37 The interface has been extracted into content/publ
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_SESSION_MEDIA_SESSION_H_ 5 #ifndef CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_IMPL_H_
6 #define CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_ 6 #define CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_IMPL_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include "base/callback_list.h" 10 #include "base/callback_list.h"
11 #include "base/id_map.h" 11 #include "base/id_map.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/observer_list.h"
13 #include "base/optional.h" 14 #include "base/optional.h"
14 #include "content/browser/media/session/audio_focus_manager.h" 15 #include "content/browser/media/session/audio_focus_manager.h"
15 #include "content/browser/media/session/media_session_uma_helper.h" 16 #include "content/browser/media/session/media_session_uma_helper.h"
16 #include "content/common/content_export.h" 17 #include "content/common/content_export.h"
18 #include "content/public/browser/media_session.h"
19 #include "content/public/browser/media_session_observer.h"
17 #include "content/public/browser/web_contents_observer.h" 20 #include "content/public/browser/web_contents_observer.h"
18 #include "content/public/browser/web_contents_user_data.h" 21 #include "content/public/browser/web_contents_user_data.h"
19 #include "content/public/common/media_metadata.h" 22 #include "content/public/common/media_metadata.h"
20 23
21 class MediaSessionBrowserTest; 24 #if defined(OS_ANDROID)
25 #include "base/android/scoped_java_ref.h"
26 #endif // defined(OS_ANDROID)
27
28 class MediaSessionImplBrowserTest;
22 29
23 namespace media { 30 namespace media {
24 enum class MediaContentType; 31 enum class MediaContentType;
25 } // namespace media 32 } // namespace media
26 33
27 namespace content { 34 namespace content {
28 35
29 class AudioFocusDelegate; 36 class AudioFocusDelegate;
30 class AudioFocusManagerTest; 37 class AudioFocusManagerTest;
31 class MediaSessionPlayerObserver; 38 class MediaSessionPlayerObserver;
32 class MediaSessionStateObserver; 39 class MediaSessionImplStateObserver;
33 class MediaSessionVisibilityBrowserTest; 40 class MediaSessionImplVisibilityBrowserTest;
41 class MediaSessionObserver;
34 42
35 // MediaSession manages the media session and audio focus for a given 43 #if defined(OS_ANDROID)
36 // WebContents. It is requesting the audio focus, pausing when requested by the 44 class MediaSessionAndroid;
37 // system and dropping it on demand. 45 #endif // defined(OS_ANDROID)
38 // The audio focus can be of two types: Transient or Content. A Transient audio 46
47 // MediaSessionImpl is the implementation of MediaSession. It manages the media
48 // session and audio focus for a given WebContents. It is requesting the audio
49 // focus, pausing when requested by the system and dropping it on demand. The
50 // audio focus can be of two types: Transient or Content. A Transient audio
39 // focus will allow other players to duck instead of pausing and will be 51 // focus will allow other players to duck instead of pausing and will be
40 // declared as temporary to the system. A Content audio focus will not be 52 // declared as temporary to the system. A Content audio focus will not be
41 // declared as temporary and will not allow other players to duck. If a given 53 // declared as temporary and will not allow other players to duck. If a given
42 // WebContents can only have one audio focus at a time, it will be Content in 54 // WebContents can only have one audio focus at a time, it will be Content in
43 // case of Transient and Content audio focus are both requested. 55 // case of Transient and Content audio focus are both requested.
44 // TODO(thakis,mlamouri): MediaSession isn't CONTENT_EXPORT'd because it creates 56 // TODO(thakis,mlamouri): MediaSessionImpl isn't CONTENT_EXPORT'd because it
45 // complicated build issues with WebContentsUserData being a non-exported 57 // creates complicated build issues with WebContentsUserData being a
46 // template, see htttps://crbug.com/589840. As a result, the class uses 58 // non-exported template, see https://crbug.com/589840. As a result, the class
47 // CONTENT_EXPORT for methods that are being used from tests. CONTENT_EXPORT 59 // uses CONTENT_EXPORT for methods that are being used from tests.
48 // should be moved back to the class when the Windows build will work with it. 60 // CONTENT_EXPORT should be moved back to the class when the Windows build will
49 class MediaSession : public WebContentsObserver, 61 // work with it.
50 protected WebContentsUserData<MediaSession> { 62 class MediaSessionImpl : public MediaSession,
63 WebContentsObserver,
64 protected WebContentsUserData<MediaSessionImpl> {
51 public: 65 public:
52 enum class SuspendType { 66 // Only visible to tests.
53 // Suspended by the system because a transient sound needs to be played. 67 enum class State { ACTIVE, SUSPENDED, INACTIVE };
54 SYSTEM,
55 // Suspended by the UI.
56 UI,
57 // Suspended by the page via script or user interaction.
58 CONTENT,
59 };
60 68
61 // Only visible to tests. 69 // Returns the MediaSessionImpl associated to this WebContents. Creates one if
62 enum class State { 70 // none is currently available.
63 ACTIVE, 71 CONTENT_EXPORT static MediaSessionImpl* Get(WebContents* web_contents);
64 SUSPENDED,
65 INACTIVE
66 };
67 72
68 // Returns the MediaSession associated to this WebContents. Creates one if 73 ~MediaSessionImpl() override;
69 // none is currently available.
70 CONTENT_EXPORT static MediaSession* Get(WebContents* web_contents);
71 74
72 ~MediaSession() override; 75 #if defined(OS_ANDROID)
76 static MediaSession* FromJavaMediaSession(
77 const base::android::JavaRef<jobject>& j_media_session);
78 MediaSessionAndroid* session_android() const {
79 return session_android_.get();
80 }
81 #endif // defined(OS_ANDROID)
82
83 void AddObserver(MediaSessionObserver* observer);
84 void RemoveObserver(MediaSessionObserver* observer);
73 85
74 void SetMetadata(const base::Optional<MediaMetadata>& metadata); 86 void SetMetadata(const base::Optional<MediaMetadata>& metadata);
75 const base::Optional<MediaMetadata>& metadata() const { return metadata_; } 87 const base::Optional<MediaMetadata>& metadata() const { return metadata_; }
76 88
77 // Adds the given player to the current media session. Returns whether the 89 // Adds the given player to the current media session. Returns whether the
78 // player was successfully added. If it returns false, AddPlayer() should be 90 // player was successfully added. If it returns false, AddPlayer() should be
79 // called again later. 91 // called again later.
80 CONTENT_EXPORT bool AddPlayer(MediaSessionPlayerObserver* observer, 92 CONTENT_EXPORT bool AddPlayer(MediaSessionPlayerObserver* observer,
81 int player_id, 93 int player_id,
82 media::MediaContentType media_content_type); 94 media::MediaContentType media_content_type);
(...skipping 11 matching lines...) Expand all
94 void RecordSessionDuck(); 106 void RecordSessionDuck();
95 107
96 // Called when a player is paused in the content. 108 // Called when a player is paused in the content.
97 // If the paused player is the last player, we suspend the MediaSession. 109 // If the paused player is the last player, we suspend the MediaSession.
98 // Otherwise, the paused player will be removed from the MediaSession. 110 // Otherwise, the paused player will be removed from the MediaSession.
99 CONTENT_EXPORT void OnPlayerPaused(MediaSessionPlayerObserver* observer, 111 CONTENT_EXPORT void OnPlayerPaused(MediaSessionPlayerObserver* observer,
100 int player_id); 112 int player_id);
101 113
102 // Resume the media session. 114 // Resume the media session.
103 // |type| represents the origin of the request. 115 // |type| represents the origin of the request.
104 CONTENT_EXPORT void Resume(SuspendType suspend_type); 116 CONTENT_EXPORT void Resume(MediaSession::SuspendType suspend_type) override;
105 117
106 // Suspend the media session. 118 // Suspend the media session.
107 // |type| represents the origin of the request. 119 // |type| represents the origin of the request.
108 CONTENT_EXPORT void Suspend(SuspendType suspend_type); 120 CONTENT_EXPORT void Suspend(MediaSession::SuspendType suspend_type) override;
109 121
110 // Stop the media session. 122 // Stop the media session.
111 // |type| represents the origin of the request. 123 // |type| represents the origin of the request.
112 CONTENT_EXPORT void Stop(SuspendType suspend_type); 124 CONTENT_EXPORT void Stop(MediaSession::SuspendType suspend_type) override;
113 125
114 // Let the media session start ducking such that the volume multiplier is 126 // Let the media session start ducking such that the volume multiplier is
115 // reduced. 127 // reduced.
116 CONTENT_EXPORT void StartDucking(); 128 CONTENT_EXPORT void StartDucking();
117 129
118 // Let the media session stop ducking such that the volume multiplier is 130 // Let the media session stop ducking such that the volume multiplier is
119 // recovered. 131 // recovered.
120 CONTENT_EXPORT void StopDucking(); 132 CONTENT_EXPORT void StopDucking();
121 133
122 // Returns if the session can be controlled by Resume() and Suspend calls 134 // Returns if the session can be controlled by Resume() and Suspend calls
(...skipping 19 matching lines...) Expand all
142 return audio_focus_type_; 154 return audio_focus_type_;
143 } 155 }
144 156
145 // Returns whether the session has Pepper instances. 157 // Returns whether the session has Pepper instances.
146 bool HasPepper() const; 158 bool HasPepper() const;
147 159
148 // WebContentsObserver implementation 160 // WebContentsObserver implementation
149 void WebContentsDestroyed() override; 161 void WebContentsDestroyed() override;
150 162
151 private: 163 private:
152 friend class content::WebContentsUserData<MediaSession>; 164 friend class content::WebContentsUserData<MediaSessionImpl>;
153 friend class ::MediaSessionBrowserTest; 165 friend class ::MediaSessionImplBrowserTest;
154 friend class content::MediaSessionVisibilityBrowserTest; 166 friend class content::MediaSessionImplVisibilityBrowserTest;
155 friend class content::AudioFocusManagerTest; 167 friend class content::AudioFocusManagerTest;
156 friend class content::MediaSessionStateObserver; 168 friend class content::MediaSessionImplStateObserver;
157 169
158 CONTENT_EXPORT void SetDelegateForTests( 170 CONTENT_EXPORT void SetDelegateForTests(
159 std::unique_ptr<AudioFocusDelegate> delegate); 171 std::unique_ptr<AudioFocusDelegate> delegate);
160 CONTENT_EXPORT bool IsActiveForTest() const; 172 CONTENT_EXPORT bool IsActiveForTest() const;
161 CONTENT_EXPORT void RemoveAllPlayersForTest(); 173 CONTENT_EXPORT void RemoveAllPlayersForTest();
162 CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test(); 174 CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test();
163 175
164 // Representation of a player for the MediaSession. 176 // Representation of a player for the MediaSessionImpl.
165 struct PlayerIdentifier { 177 struct PlayerIdentifier {
166 PlayerIdentifier(MediaSessionPlayerObserver* observer, int player_id); 178 PlayerIdentifier(MediaSessionPlayerObserver* observer, int player_id);
167 PlayerIdentifier(const PlayerIdentifier&) = default; 179 PlayerIdentifier(const PlayerIdentifier&) = default;
168 180
169 void operator=(const PlayerIdentifier&) = delete; 181 void operator=(const PlayerIdentifier&) = delete;
170 bool operator==(const PlayerIdentifier& player_identifier) const; 182 bool operator==(const PlayerIdentifier& player_identifier) const;
171 183
172 // Hash operator for base::hash_map<>. 184 // Hash operator for base::hash_map<>.
173 struct Hash { 185 struct Hash {
174 size_t operator()(const PlayerIdentifier& player_identifier) const; 186 size_t operator()(const PlayerIdentifier& player_identifier) const;
175 }; 187 };
176 188
177 MediaSessionPlayerObserver* observer; 189 MediaSessionPlayerObserver* observer;
178 int player_id; 190 int player_id;
179 }; 191 };
180 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>; 192 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>;
181 using StateChangedCallback = base::Callback<void(State)>; 193 using StateChangedCallback = base::Callback<void(State)>;
182 194
183 CONTENT_EXPORT explicit MediaSession(WebContents* web_contents); 195 CONTENT_EXPORT explicit MediaSessionImpl(WebContents* web_contents);
184 196
185 void Initialize(); 197 void Initialize();
186 198
187 CONTENT_EXPORT void OnSuspendInternal(SuspendType suspend_type, 199 CONTENT_EXPORT void OnSuspendInternal(MediaSession::SuspendType suspend_type,
188 State new_state); 200 State new_state);
189 CONTENT_EXPORT void OnResumeInternal(SuspendType suspend_type); 201 CONTENT_EXPORT void OnResumeInternal(MediaSession::SuspendType suspend_type);
190 202
191 // Requests audio focus to the AudioFocusDelegate. 203 // Requests audio focus to the AudioFocusDelegate.
192 // Returns whether the request was granted. 204 // Returns whether the request was granted.
193 CONTENT_EXPORT bool RequestSystemAudioFocus( 205 CONTENT_EXPORT bool RequestSystemAudioFocus(
194 AudioFocusManager::AudioFocusType audio_focus_type); 206 AudioFocusManager::AudioFocusType audio_focus_type);
195 207
196 // To be called after a call to AbandonAudioFocus() in order request the 208 // To be called after a call to AbandonAudioFocus() in order request the
197 // delegate to abandon the audio focus. 209 // delegate to abandon the audio focus.
198 CONTENT_EXPORT void AbandonSystemAudioFocusIfNeeded(); 210 CONTENT_EXPORT void AbandonSystemAudioFocusIfNeeded();
199 211
200 // Notifies WebContents about the state change of the media session. 212 // Notifies WebContents about the state change of the media session.
201 void UpdateWebContents(); 213 void UpdateWebContents();
202 214
203 // Internal method that should be used instead of setting audio_focus_state_. 215 // Internal method that should be used instead of setting audio_focus_state_.
204 // It sets audio_focus_state_ and notifies observers about the state change. 216 // It sets audio_focus_state_ and notifies observers about the state change.
205 void SetAudioFocusState(State audio_focus_state); 217 void SetAudioFocusState(State audio_focus_state);
206 218
207 // Update the volume multiplier when ducking state changes. 219 // Update the volume multiplier when ducking state changes.
208 void UpdateVolumeMultiplier(); 220 void UpdateVolumeMultiplier();
209 221
210 // Get the volume multiplier, which depends on whether the media session is 222 // Get the volume multiplier, which depends on whether the media session is
211 // ducking. 223 // ducking.
212 double GetVolumeMultiplier() const; 224 double GetVolumeMultiplier() const;
213 225
214 // Registers a MediaSession state change callback. 226 // Registers a MediaSessionImpl state change callback.
215 CONTENT_EXPORT std::unique_ptr<base::CallbackList<void(State)>::Subscription> 227 CONTENT_EXPORT std::unique_ptr<base::CallbackList<void(State)>::Subscription>
216 RegisterMediaSessionStateChangedCallbackForTest( 228 RegisterMediaSessionStateChangedCallbackForTest(
217 const StateChangedCallback& cb); 229 const StateChangedCallback& cb);
218 230
219 CONTENT_EXPORT bool AddPepperPlayer(MediaSessionPlayerObserver* observer, 231 CONTENT_EXPORT bool AddPepperPlayer(MediaSessionPlayerObserver* observer,
220 int player_id); 232 int player_id);
221 233
222 std::unique_ptr<AudioFocusDelegate> delegate_; 234 std::unique_ptr<AudioFocusDelegate> delegate_;
223 PlayersMap players_; 235 PlayersMap players_;
224 PlayersMap pepper_players_; 236 PlayersMap pepper_players_;
225 237
226 State audio_focus_state_; 238 State audio_focus_state_;
227 SuspendType suspend_type_; 239 MediaSession::SuspendType suspend_type_;
228 AudioFocusManager::AudioFocusType audio_focus_type_; 240 AudioFocusManager::AudioFocusType audio_focus_type_;
229 241
230 MediaSessionUmaHelper uma_helper_; 242 MediaSessionUmaHelper uma_helper_;
231 243
232 // The ducking state of this media session. The initial value is |false|, and 244 // The ducking state of this media session. The initial value is |false|, and
233 // is set to |true| after StartDucking(), and will be set to |false| after 245 // is set to |true| after StartDucking(), and will be set to |false| after
234 // StopDucking(). 246 // StopDucking().
235 bool is_ducking_; 247 bool is_ducking_;
236 248
237 base::Optional<MediaMetadata> metadata_; 249 base::Optional<MediaMetadata> metadata_;
238 base::CallbackList<void(State)> media_session_state_listeners_; 250 base::CallbackList<void(State)> media_session_state_listeners_;
239 251
240 DISALLOW_COPY_AND_ASSIGN(MediaSession); 252 base::ObserverList<MediaSessionObserver> observers_;
253
254 #if defined(OS_ANDROID)
255 std::unique_ptr<MediaSessionAndroid> session_android_;
256 #endif // defined(OS_ANDROID)
257
258 DISALLOW_COPY_AND_ASSIGN(MediaSessionImpl);
241 }; 259 };
242 260
243 } // namespace content 261 } // namespace content
244 262
245 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_ 263 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698