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

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

Issue 2453623003: Decouple MediaSession messages from WebContents (full patch) (Closed)
Patch Set: nit 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_
6 #define CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_
7
8 #include <stddef.h>
9
10 #include "base/callback_list.h"
11 #include "base/id_map.h"
12 #include "base/macros.h"
13 #include "base/optional.h"
14 #include "content/browser/media/session/audio_focus_manager.h"
15 #include "content/browser/media/session/media_session_uma_helper.h"
16 #include "content/common/content_export.h"
17 #include "content/public/browser/web_contents_observer.h"
18 #include "content/public/browser/web_contents_user_data.h"
19 #include "content/public/common/media_metadata.h"
20
21 class MediaSessionBrowserTest;
22
23 namespace media {
24 enum class MediaContentType;
25 } // namespace media
26
27 namespace content {
28
29 class AudioFocusDelegate;
30 class AudioFocusManagerTest;
31 class MediaSessionPlayerObserver;
32 class MediaSessionStateObserver;
33 class MediaSessionVisibilityBrowserTest;
34
35 // MediaSession manages the media session and audio focus for a given
36 // WebContents. It is requesting the audio focus, pausing when requested by the
37 // system and dropping it on demand.
38 // The 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
40 // 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
42 // 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.
44 // TODO(thakis,mlamouri): MediaSession isn't CONTENT_EXPORT'd because it creates
45 // complicated build issues with WebContentsUserData being a non-exported
46 // template, see htttps://crbug.com/589840. As a result, the class uses
47 // CONTENT_EXPORT for methods that are being used from tests. CONTENT_EXPORT
48 // should be moved back to the class when the Windows build will work with it.
49 class MediaSession : public WebContentsObserver,
50 protected WebContentsUserData<MediaSession> {
51 public:
52 enum class SuspendType {
53 // Suspended by the system because a transient sound needs to be played.
54 SYSTEM,
55 // Suspended by the UI.
56 UI,
57 // Suspended by the page via script or user interaction.
58 CONTENT,
59 };
60
61 // Only visible to tests.
62 enum class State {
63 ACTIVE,
64 SUSPENDED,
65 INACTIVE
66 };
67
68 // Returns the MediaSession associated to this WebContents. Creates one if
69 // none is currently available.
70 CONTENT_EXPORT static MediaSession* Get(WebContents* web_contents);
71
72 ~MediaSession() override;
73
74 void SetMetadata(const base::Optional<MediaMetadata>& metadata);
75 const base::Optional<MediaMetadata>& metadata() const { return metadata_; }
76
77 // Adds the given player to the current media session. Returns whether the
78 // player was successfully added. If it returns false, AddPlayer() should be
79 // called again later.
80 CONTENT_EXPORT bool AddPlayer(MediaSessionPlayerObserver* observer,
81 int player_id,
82 media::MediaContentType media_content_type);
83
84 // Removes the given player from the current media session. Abandons audio
85 // focus if that was the last player in the session.
86 CONTENT_EXPORT void RemovePlayer(MediaSessionPlayerObserver* observer,
87 int player_id);
88
89 // Removes all the players associated with |observer|. Abandons audio focus if
90 // these were the last players in the session.
91 CONTENT_EXPORT void RemovePlayers(MediaSessionPlayerObserver* observer);
92
93 // Record that the session was ducked.
94 void RecordSessionDuck();
95
96 // Called when a player is paused in the content.
97 // If the paused player is the last player, we suspend the MediaSession.
98 // Otherwise, the paused player will be removed from the MediaSession.
99 CONTENT_EXPORT void OnPlayerPaused(MediaSessionPlayerObserver* observer,
100 int player_id);
101
102 // Resume the media session.
103 // |type| represents the origin of the request.
104 CONTENT_EXPORT void Resume(SuspendType suspend_type);
105
106 // Suspend the media session.
107 // |type| represents the origin of the request.
108 CONTENT_EXPORT void Suspend(SuspendType suspend_type);
109
110 // Stop the media session.
111 // |type| represents the origin of the request.
112 CONTENT_EXPORT void Stop(SuspendType suspend_type);
113
114 // Let the media session start ducking such that the volume multiplier is
115 // reduced.
116 CONTENT_EXPORT void StartDucking();
117
118 // Let the media session stop ducking such that the volume multiplier is
119 // recovered.
120 CONTENT_EXPORT void StopDucking();
121
122 // Returns if the session can be controlled by Resume() and Suspend calls
123 // above.
124 CONTENT_EXPORT bool IsControllable() const;
125
126 // Returns if the session is currently active.
127 CONTENT_EXPORT bool IsActive() const;
128
129 // Returns if the session is currently suspended.
130 // TODO(mlamouri): IsSuspended() below checks if the state is not ACTIVE
131 // instead of checking if the state is SUSPENDED. In order to not have to
132 // change all the callers and make the current refactoring ridiculously huge,
133 // this method is introduced temporarily and will be removed later.
134 CONTENT_EXPORT bool IsReallySuspended() const;
135
136 // Returns if the session is currently suspended or inactive.
137 CONTENT_EXPORT bool IsSuspended() const;
138
139 // Returns the audio focus type. The type is updated everytime after the
140 // session requests audio focus.
141 CONTENT_EXPORT AudioFocusManager::AudioFocusType audio_focus_type() const {
142 return audio_focus_type_;
143 }
144
145 // Returns whether the session has Pepper instances.
146 bool HasPepper() const;
147
148 // WebContentsObserver implementation
149 void WebContentsDestroyed() override;
150
151 private:
152 friend class content::WebContentsUserData<MediaSession>;
153 friend class ::MediaSessionBrowserTest;
154 friend class content::MediaSessionVisibilityBrowserTest;
155 friend class content::AudioFocusManagerTest;
156 friend class content::MediaSessionStateObserver;
157
158 CONTENT_EXPORT void SetDelegateForTests(
159 std::unique_ptr<AudioFocusDelegate> delegate);
160 CONTENT_EXPORT bool IsActiveForTest() const;
161 CONTENT_EXPORT void RemoveAllPlayersForTest();
162 CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test();
163
164 // Representation of a player for the MediaSession.
165 struct PlayerIdentifier {
166 PlayerIdentifier(MediaSessionPlayerObserver* observer, int player_id);
167 PlayerIdentifier(const PlayerIdentifier&) = default;
168
169 void operator=(const PlayerIdentifier&) = delete;
170 bool operator==(const PlayerIdentifier& player_identifier) const;
171
172 // Hash operator for base::hash_map<>.
173 struct Hash {
174 size_t operator()(const PlayerIdentifier& player_identifier) const;
175 };
176
177 MediaSessionPlayerObserver* observer;
178 int player_id;
179 };
180 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>;
181 using StateChangedCallback = base::Callback<void(State)>;
182
183 CONTENT_EXPORT explicit MediaSession(WebContents* web_contents);
184
185 void Initialize();
186
187 CONTENT_EXPORT void OnSuspendInternal(SuspendType suspend_type,
188 State new_state);
189 CONTENT_EXPORT void OnResumeInternal(SuspendType suspend_type);
190
191 // Requests audio focus to the AudioFocusDelegate.
192 // Returns whether the request was granted.
193 CONTENT_EXPORT bool RequestSystemAudioFocus(
194 AudioFocusManager::AudioFocusType audio_focus_type);
195
196 // To be called after a call to AbandonAudioFocus() in order request the
197 // delegate to abandon the audio focus.
198 CONTENT_EXPORT void AbandonSystemAudioFocusIfNeeded();
199
200 // Notifies WebContents about the state change of the media session.
201 void UpdateWebContents();
202
203 // 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.
205 void SetAudioFocusState(State audio_focus_state);
206
207 // Update the volume multiplier when ducking state changes.
208 void UpdateVolumeMultiplier();
209
210 // Get the volume multiplier, which depends on whether the media session is
211 // ducking.
212 double GetVolumeMultiplier() const;
213
214 // Registers a MediaSession state change callback.
215 CONTENT_EXPORT std::unique_ptr<base::CallbackList<void(State)>::Subscription>
216 RegisterMediaSessionStateChangedCallbackForTest(
217 const StateChangedCallback& cb);
218
219 CONTENT_EXPORT bool AddPepperPlayer(MediaSessionPlayerObserver* observer,
220 int player_id);
221
222 std::unique_ptr<AudioFocusDelegate> delegate_;
223 PlayersMap players_;
224 PlayersMap pepper_players_;
225
226 State audio_focus_state_;
227 SuspendType suspend_type_;
228 AudioFocusManager::AudioFocusType audio_focus_type_;
229
230 MediaSessionUmaHelper uma_helper_;
231
232 // 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
234 // StopDucking().
235 bool is_ducking_;
236
237 base::Optional<MediaMetadata> metadata_;
238 base::CallbackList<void(State)> media_session_state_listeners_;
239
240 DISALLOW_COPY_AND_ASSIGN(MediaSession);
241 };
242
243 } // namespace content
244
245 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_
OLDNEW
« no previous file with comments | « content/browser/media/session/audio_focus_manager_unittest.cc ('k') | content/browser/media/session/media_session.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698