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

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

Issue 2441423002: Add MediaSessionObserver to allow clients observe MediaSession directly (Closed)
Patch Set: don't store session pointer in observer, remove StopObserving() 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.
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 class MediaSessionImplBrowserTest;
22 25
23 namespace media { 26 namespace media {
24 enum class MediaContentType; 27 enum class MediaContentType;
25 } // namespace media 28 } // namespace media
26 29
27 namespace content { 30 namespace content {
28 31
29 class AudioFocusDelegate; 32 class AudioFocusDelegate;
30 class AudioFocusManagerTest; 33 class AudioFocusManagerTest;
31 class MediaSessionPlayerObserver; 34 class MediaSessionPlayerObserver;
32 class MediaSessionStateObserver; 35 class MediaSessionImplStateObserver;
33 class MediaSessionVisibilityBrowserTest; 36 class MediaSessionImplVisibilityBrowserTest;
37 class MediaSessionObserver;
34 38
35 // MediaSession manages the media session and audio focus for a given 39 // MediaSessionImpl is the implementation of MediaSession. It manages the media
36 // WebContents. It is requesting the audio focus, pausing when requested by the 40 // session and audio focus for a given WebContents. It is requesting the audio
37 // system and dropping it on demand. 41 // focus, pausing when requested by the system and dropping it on demand. The
38 // The audio focus can be of two types: Transient or Content. A Transient audio 42 // 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 43 // 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 44 // 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 45 // 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 46 // 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. 47 // case of Transient and Content audio focus are both requested.
44 // TODO(thakis,mlamouri): MediaSession isn't CONTENT_EXPORT'd because it creates 48 // TODO(thakis,mlamouri): MediaSessionImpl isn't CONTENT_EXPORT'd because it
45 // complicated build issues with WebContentsUserData being a non-exported 49 // creates complicated build issues with WebContentsUserData being a
46 // template, see htttps://crbug.com/589840. As a result, the class uses 50 // 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 51 // 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. 52 // CONTENT_EXPORT should be moved back to the class when the Windows build will
49 class MediaSession : public WebContentsObserver, 53 // work with it.
50 protected WebContentsUserData<MediaSession> { 54 class MediaSessionImpl : public MediaSession,
55 WebContentsObserver,
56 protected WebContentsUserData<MediaSessionImpl> {
51 public: 57 public:
52 enum class SuspendType { 58 // Only visible to tests.
53 // Suspended by the system because a transient sound needs to be played. 59 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 60
61 // Only visible to tests. 61 // Returns the MediaSessionImpl associated to this WebContents. Creates one if
62 enum class State { 62 // none is currently available.
63 ACTIVE, 63 CONTENT_EXPORT static MediaSessionImpl* Get(WebContents* web_contents);
64 SUSPENDED,
65 INACTIVE
66 };
67 64
68 // Returns the MediaSession associated to this WebContents. Creates one if 65 ~MediaSessionImpl() override;
69 // none is currently available.
70 CONTENT_EXPORT static MediaSession* Get(WebContents* web_contents);
71 66
72 ~MediaSession() override; 67 void AddObserver(MediaSessionObserver* observer);
68 void RemoveObserver(MediaSessionObserver* observer);
73 69
74 void SetMetadata(const base::Optional<MediaMetadata>& metadata); 70 void SetMetadata(const base::Optional<MediaMetadata>& metadata);
75 const base::Optional<MediaMetadata>& metadata() const { return metadata_; } 71 const base::Optional<MediaMetadata>& metadata() const { return metadata_; }
76 72
77 // Adds the given player to the current media session. Returns whether the 73 // Adds the given player to the current media session. Returns whether the
78 // player was successfully added. If it returns false, AddPlayer() should be 74 // player was successfully added. If it returns false, AddPlayer() should be
79 // called again later. 75 // called again later.
80 CONTENT_EXPORT bool AddPlayer(MediaSessionPlayerObserver* observer, 76 CONTENT_EXPORT bool AddPlayer(MediaSessionPlayerObserver* observer,
81 int player_id, 77 int player_id,
82 media::MediaContentType media_content_type); 78 media::MediaContentType media_content_type);
(...skipping 11 matching lines...) Expand all
94 void RecordSessionDuck(); 90 void RecordSessionDuck();
95 91
96 // Called when a player is paused in the content. 92 // Called when a player is paused in the content.
97 // If the paused player is the last player, we suspend the MediaSession. 93 // If the paused player is the last player, we suspend the MediaSession.
98 // Otherwise, the paused player will be removed from the MediaSession. 94 // Otherwise, the paused player will be removed from the MediaSession.
99 CONTENT_EXPORT void OnPlayerPaused(MediaSessionPlayerObserver* observer, 95 CONTENT_EXPORT void OnPlayerPaused(MediaSessionPlayerObserver* observer,
100 int player_id); 96 int player_id);
101 97
102 // Resume the media session. 98 // Resume the media session.
103 // |type| represents the origin of the request. 99 // |type| represents the origin of the request.
104 CONTENT_EXPORT void Resume(SuspendType suspend_type); 100 CONTENT_EXPORT void Resume(MediaSession::SuspendType suspend_type) override;
105 101
106 // Suspend the media session. 102 // Suspend the media session.
107 // |type| represents the origin of the request. 103 // |type| represents the origin of the request.
108 CONTENT_EXPORT void Suspend(SuspendType suspend_type); 104 CONTENT_EXPORT void Suspend(MediaSession::SuspendType suspend_type) override;
109 105
110 // Stop the media session. 106 // Stop the media session.
111 // |type| represents the origin of the request. 107 // |type| represents the origin of the request.
112 CONTENT_EXPORT void Stop(SuspendType suspend_type); 108 CONTENT_EXPORT void Stop(MediaSession::SuspendType suspend_type) override;
113 109
114 // Let the media session start ducking such that the volume multiplier is 110 // Let the media session start ducking such that the volume multiplier is
115 // reduced. 111 // reduced.
116 CONTENT_EXPORT void StartDucking(); 112 CONTENT_EXPORT void StartDucking();
117 113
118 // Let the media session stop ducking such that the volume multiplier is 114 // Let the media session stop ducking such that the volume multiplier is
119 // recovered. 115 // recovered.
120 CONTENT_EXPORT void StopDucking(); 116 CONTENT_EXPORT void StopDucking();
121 117
122 // Returns if the session can be controlled by Resume() and Suspend calls 118 // Returns if the session can be controlled by Resume() and Suspend calls
(...skipping 19 matching lines...) Expand all
142 return audio_focus_type_; 138 return audio_focus_type_;
143 } 139 }
144 140
145 // Returns whether the session has Pepper instances. 141 // Returns whether the session has Pepper instances.
146 bool HasPepper() const; 142 bool HasPepper() const;
147 143
148 // WebContentsObserver implementation 144 // WebContentsObserver implementation
149 void WebContentsDestroyed() override; 145 void WebContentsDestroyed() override;
150 146
151 private: 147 private:
152 friend class content::WebContentsUserData<MediaSession>; 148 friend class content::WebContentsUserData<MediaSessionImpl>;
153 friend class ::MediaSessionBrowserTest; 149 friend class ::MediaSessionImplBrowserTest;
154 friend class content::MediaSessionVisibilityBrowserTest; 150 friend class content::MediaSessionImplVisibilityBrowserTest;
155 friend class content::AudioFocusManagerTest; 151 friend class content::AudioFocusManagerTest;
156 friend class content::MediaSessionStateObserver; 152 friend class content::MediaSessionImplStateObserver;
157 153
158 CONTENT_EXPORT void SetDelegateForTests( 154 CONTENT_EXPORT void SetDelegateForTests(
159 std::unique_ptr<AudioFocusDelegate> delegate); 155 std::unique_ptr<AudioFocusDelegate> delegate);
160 CONTENT_EXPORT bool IsActiveForTest() const; 156 CONTENT_EXPORT bool IsActiveForTest() const;
161 CONTENT_EXPORT void RemoveAllPlayersForTest(); 157 CONTENT_EXPORT void RemoveAllPlayersForTest();
162 CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test(); 158 CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test();
163 159
164 // Representation of a player for the MediaSession. 160 // Representation of a player for the MediaSessionImpl.
165 struct PlayerIdentifier { 161 struct PlayerIdentifier {
166 PlayerIdentifier(MediaSessionPlayerObserver* observer, int player_id); 162 PlayerIdentifier(MediaSessionPlayerObserver* observer, int player_id);
167 PlayerIdentifier(const PlayerIdentifier&) = default; 163 PlayerIdentifier(const PlayerIdentifier&) = default;
168 164
169 void operator=(const PlayerIdentifier&) = delete; 165 void operator=(const PlayerIdentifier&) = delete;
170 bool operator==(const PlayerIdentifier& player_identifier) const; 166 bool operator==(const PlayerIdentifier& player_identifier) const;
171 167
172 // Hash operator for base::hash_map<>. 168 // Hash operator for base::hash_map<>.
173 struct Hash { 169 struct Hash {
174 size_t operator()(const PlayerIdentifier& player_identifier) const; 170 size_t operator()(const PlayerIdentifier& player_identifier) const;
175 }; 171 };
176 172
177 MediaSessionPlayerObserver* observer; 173 MediaSessionPlayerObserver* observer;
178 int player_id; 174 int player_id;
179 }; 175 };
180 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>; 176 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>;
181 using StateChangedCallback = base::Callback<void(State)>; 177 using StateChangedCallback = base::Callback<void(State)>;
182 178
183 CONTENT_EXPORT explicit MediaSession(WebContents* web_contents); 179 CONTENT_EXPORT explicit MediaSessionImpl(WebContents* web_contents);
184 180
185 void Initialize(); 181 void Initialize();
186 182
187 CONTENT_EXPORT void OnSuspendInternal(SuspendType suspend_type, 183 CONTENT_EXPORT void OnSuspendInternal(MediaSession::SuspendType suspend_type,
188 State new_state); 184 State new_state);
189 CONTENT_EXPORT void OnResumeInternal(SuspendType suspend_type); 185 CONTENT_EXPORT void OnResumeInternal(MediaSession::SuspendType suspend_type);
190 186
191 // Requests audio focus to the AudioFocusDelegate. 187 // Requests audio focus to the AudioFocusDelegate.
192 // Returns whether the request was granted. 188 // Returns whether the request was granted.
193 CONTENT_EXPORT bool RequestSystemAudioFocus( 189 CONTENT_EXPORT bool RequestSystemAudioFocus(
194 AudioFocusManager::AudioFocusType audio_focus_type); 190 AudioFocusManager::AudioFocusType audio_focus_type);
195 191
196 // To be called after a call to AbandonAudioFocus() in order request the 192 // To be called after a call to AbandonAudioFocus() in order request the
197 // delegate to abandon the audio focus. 193 // delegate to abandon the audio focus.
198 CONTENT_EXPORT void AbandonSystemAudioFocusIfNeeded(); 194 CONTENT_EXPORT void AbandonSystemAudioFocusIfNeeded();
199 195
200 // Notifies WebContents about the state change of the media session. 196 // Notifies WebContents about the state change of the media session.
201 void UpdateWebContents(); 197 void UpdateWebContents();
202 198
203 // Internal method that should be used instead of setting audio_focus_state_. 199 // 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. 200 // It sets audio_focus_state_ and notifies observers about the state change.
205 void SetAudioFocusState(State audio_focus_state); 201 void SetAudioFocusState(State audio_focus_state);
206 202
207 // Update the volume multiplier when ducking state changes. 203 // Update the volume multiplier when ducking state changes.
208 void UpdateVolumeMultiplier(); 204 void UpdateVolumeMultiplier();
209 205
210 // Get the volume multiplier, which depends on whether the media session is 206 // Get the volume multiplier, which depends on whether the media session is
211 // ducking. 207 // ducking.
212 double GetVolumeMultiplier() const; 208 double GetVolumeMultiplier() const;
213 209
214 // Registers a MediaSession state change callback. 210 // Registers a MediaSessionImpl state change callback.
215 CONTENT_EXPORT std::unique_ptr<base::CallbackList<void(State)>::Subscription> 211 CONTENT_EXPORT std::unique_ptr<base::CallbackList<void(State)>::Subscription>
216 RegisterMediaSessionStateChangedCallbackForTest( 212 RegisterMediaSessionStateChangedCallbackForTest(
217 const StateChangedCallback& cb); 213 const StateChangedCallback& cb);
218 214
219 CONTENT_EXPORT bool AddPepperPlayer(MediaSessionPlayerObserver* observer, 215 CONTENT_EXPORT bool AddPepperPlayer(MediaSessionPlayerObserver* observer,
220 int player_id); 216 int player_id);
221 217
222 std::unique_ptr<AudioFocusDelegate> delegate_; 218 std::unique_ptr<AudioFocusDelegate> delegate_;
223 PlayersMap players_; 219 PlayersMap players_;
224 PlayersMap pepper_players_; 220 PlayersMap pepper_players_;
225 221
226 State audio_focus_state_; 222 State audio_focus_state_;
227 SuspendType suspend_type_; 223 MediaSession::SuspendType suspend_type_;
228 AudioFocusManager::AudioFocusType audio_focus_type_; 224 AudioFocusManager::AudioFocusType audio_focus_type_;
229 225
230 MediaSessionUmaHelper uma_helper_; 226 MediaSessionUmaHelper uma_helper_;
231 227
232 // The ducking state of this media session. The initial value is |false|, and 228 // 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 229 // is set to |true| after StartDucking(), and will be set to |false| after
234 // StopDucking(). 230 // StopDucking().
235 bool is_ducking_; 231 bool is_ducking_;
236 232
237 base::Optional<MediaMetadata> metadata_; 233 base::Optional<MediaMetadata> metadata_;
238 base::CallbackList<void(State)> media_session_state_listeners_; 234 base::CallbackList<void(State)> media_session_state_listeners_;
239 235
240 DISALLOW_COPY_AND_ASSIGN(MediaSession); 236 base::ObserverList<MediaSessionObserver> observers_;
237
238 DISALLOW_COPY_AND_ASSIGN(MediaSessionImpl);
241 }; 239 };
242 240
243 } // namespace content 241 } // namespace content
244 242
245 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_ 243 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698