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

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

Issue 2381963004: Reland "Letting Flash join MediaSession (stack implementaion)" (Closed)
Patch Set: Created 4 years, 2 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_SESSION_MEDIA_SESSION_H_ 5 #ifndef CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_
6 #define CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_ 6 #define CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_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"
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 public: 51 public:
52 enum class SuspendType { 52 enum class SuspendType {
53 // Suspended by the system because a transient sound needs to be played. 53 // Suspended by the system because a transient sound needs to be played.
54 SYSTEM, 54 SYSTEM,
55 // Suspended by the UI. 55 // Suspended by the UI.
56 UI, 56 UI,
57 // Suspended by the page via script or user interaction. 57 // Suspended by the page via script or user interaction.
58 CONTENT, 58 CONTENT,
59 }; 59 };
60 60
61 // Only visible to tests.
62 enum class State {
63 ACTIVE,
64 SUSPENDED,
65 INACTIVE
66 };
67
61 // Returns the MediaSession associated to this WebContents. Creates one if 68 // Returns the MediaSession associated to this WebContents. Creates one if
62 // none is currently available. 69 // none is currently available.
63 CONTENT_EXPORT static MediaSession* Get(WebContents* web_contents); 70 CONTENT_EXPORT static MediaSession* Get(WebContents* web_contents);
64 71
65 ~MediaSession() override; 72 ~MediaSession() override;
66 73
67 void SetMetadata(const base::Optional<MediaMetadata>& metadata); 74 void SetMetadata(const base::Optional<MediaMetadata>& metadata);
68 const base::Optional<MediaMetadata>& metadata() const { return metadata_; } 75 const base::Optional<MediaMetadata>& metadata() const { return metadata_; }
69 76
70 // Adds the given player to the current media session. Returns whether the 77 // Adds the given player to the current media session. Returns whether the
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 CONTENT_EXPORT bool IsControllable() const; 124 CONTENT_EXPORT bool IsControllable() const;
118 125
119 // Returns if the session is currently active. 126 // Returns if the session is currently active.
120 CONTENT_EXPORT bool IsActive() const; 127 CONTENT_EXPORT bool IsActive() const;
121 128
122 // Returns if the session is currently suspended. 129 // Returns if the session is currently suspended.
123 // TODO(mlamouri): IsSuspended() below checks if the state is not ACTIVE 130 // TODO(mlamouri): IsSuspended() below checks if the state is not ACTIVE
124 // instead of checking if the state is SUSPENDED. In order to not have to 131 // instead of checking if the state is SUSPENDED. In order to not have to
125 // change all the callers and make the current refactoring ridiculously huge, 132 // change all the callers and make the current refactoring ridiculously huge,
126 // this method is introduced temporarily and will be removed later. 133 // this method is introduced temporarily and will be removed later.
127 bool IsReallySuspended() const; 134 CONTENT_EXPORT bool IsReallySuspended() const;
128 135
129 // Returns if the session is currently suspended or inactive. 136 // Returns if the session is currently suspended or inactive.
130 CONTENT_EXPORT bool IsSuspended() const; 137 CONTENT_EXPORT bool IsSuspended() const;
131 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
132 private: 151 private:
133 friend class content::WebContentsUserData<MediaSession>; 152 friend class content::WebContentsUserData<MediaSession>;
134 friend class ::MediaSessionBrowserTest; 153 friend class ::MediaSessionBrowserTest;
135 friend class content::MediaSessionVisibilityBrowserTest; 154 friend class content::MediaSessionVisibilityBrowserTest;
136 friend class content::AudioFocusManagerTest; 155 friend class content::AudioFocusManagerTest;
137 friend class content::MediaSessionStateObserver; 156 friend class content::MediaSessionStateObserver;
138 157
139 CONTENT_EXPORT void SetDelegateForTests( 158 CONTENT_EXPORT void SetDelegateForTests(
140 std::unique_ptr<MediaSessionDelegate> delegate); 159 std::unique_ptr<MediaSessionDelegate> delegate);
141 CONTENT_EXPORT bool IsActiveForTest() const; 160 CONTENT_EXPORT bool IsActiveForTest() const;
142 CONTENT_EXPORT AudioFocusManager::AudioFocusType audio_focus_type_for_test()
143 const;
144 CONTENT_EXPORT void RemoveAllPlayersForTest(); 161 CONTENT_EXPORT void RemoveAllPlayersForTest();
145 CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test(); 162 CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test();
146 163
147 enum class State {
148 ACTIVE,
149 SUSPENDED,
150 INACTIVE
151 };
152
153 // Representation of a player for the MediaSession. 164 // Representation of a player for the MediaSession.
154 struct PlayerIdentifier { 165 struct PlayerIdentifier {
155 PlayerIdentifier(MediaSessionObserver* observer, int player_id); 166 PlayerIdentifier(MediaSessionObserver* observer, int player_id);
156 PlayerIdentifier(const PlayerIdentifier&) = default; 167 PlayerIdentifier(const PlayerIdentifier&) = default;
157 168
158 void operator=(const PlayerIdentifier&) = delete; 169 void operator=(const PlayerIdentifier&) = delete;
159 bool operator==(const PlayerIdentifier& player_identifier) const; 170 bool operator==(const PlayerIdentifier& player_identifier) const;
160 171
161 // Hash operator for base::hash_map<>. 172 // Hash operator for base::hash_map<>.
162 struct Hash { 173 struct Hash {
163 size_t operator()(const PlayerIdentifier& player_identifier) const; 174 size_t operator()(const PlayerIdentifier& player_identifier) const;
164 }; 175 };
165 176
166 MediaSessionObserver* observer; 177 MediaSessionObserver* observer;
167 int player_id; 178 int player_id;
168 }; 179 };
169 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>; 180 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>;
170 using StateChangedCallback = base::Callback<void(State)>; 181 using StateChangedCallback = base::Callback<void(State)>;
171 182
172 CONTENT_EXPORT explicit MediaSession(WebContents* web_contents); 183 CONTENT_EXPORT explicit MediaSession(WebContents* web_contents);
173 184
174 void Initialize(); 185 void Initialize();
175 186
176 CONTENT_EXPORT void OnSuspendInternal(SuspendType suspend_type, 187 CONTENT_EXPORT void OnSuspendInternal(SuspendType suspend_type,
177 State new_state); 188 State new_state);
178 CONTENT_EXPORT void OnResumeInternal(SuspendType suspend_type); 189 CONTENT_EXPORT void OnResumeInternal(SuspendType suspend_type);
179 190
180 // Requests audio focus to the MediaSessionDelegate. 191 // Requests audio focus to the MediaSessionDelegate.
181 // Returns whether the request was granted. 192 // Returns whether the request was granted.
182 bool RequestSystemAudioFocus( 193 CONTENT_EXPORT bool RequestSystemAudioFocus(
183 AudioFocusManager::AudioFocusType audio_focus_type); 194 AudioFocusManager::AudioFocusType audio_focus_type);
184 195
185 // To be called after a call to AbandonAudioFocus() in order request the 196 // To be called after a call to AbandonAudioFocus() in order request the
186 // delegate to abandon the audio focus. 197 // delegate to abandon the audio focus.
187 void AbandonSystemAudioFocusIfNeeded(); 198 CONTENT_EXPORT void AbandonSystemAudioFocusIfNeeded();
188 199
189 // Notifies WebContents about the state change of the media session. 200 // Notifies WebContents about the state change of the media session.
190 void UpdateWebContents(); 201 void UpdateWebContents();
191 202
192 // Internal method that should be used instead of setting audio_focus_state_. 203 // Internal method that should be used instead of setting audio_focus_state_.
193 // It sets audio_focus_state_ and notifies observers about the state change. 204 // It sets audio_focus_state_ and notifies observers about the state change.
194 void SetAudioFocusState(State audio_focus_state); 205 void SetAudioFocusState(State audio_focus_state);
195 206
196 // Update the volume multiplier when ducking state changes. 207 // Update the volume multiplier when ducking state changes.
197 void UpdateVolumeMultiplier(); 208 void UpdateVolumeMultiplier();
198 209
199 // Get the volume multiplier, which depends on whether the media session is 210 // Get the volume multiplier, which depends on whether the media session is
200 // ducking. 211 // ducking.
201 double GetVolumeMultiplier() const; 212 double GetVolumeMultiplier() const;
202 213
203 // Registers a MediaSession state change callback. 214 // Registers a MediaSession state change callback.
204 CONTENT_EXPORT std::unique_ptr<base::CallbackList<void(State)>::Subscription> 215 CONTENT_EXPORT std::unique_ptr<base::CallbackList<void(State)>::Subscription>
205 RegisterMediaSessionStateChangedCallbackForTest( 216 RegisterMediaSessionStateChangedCallbackForTest(
206 const StateChangedCallback& cb); 217 const StateChangedCallback& cb);
207 218
219 CONTENT_EXPORT bool AddPepperPlayer(MediaSessionObserver* observer,
220 int player_id);
221
208 std::unique_ptr<MediaSessionDelegate> delegate_; 222 std::unique_ptr<MediaSessionDelegate> delegate_;
209 PlayersMap players_; 223 PlayersMap players_;
224 PlayersMap pepper_players_;
210 225
211 State audio_focus_state_; 226 State audio_focus_state_;
212 SuspendType suspend_type_; 227 SuspendType suspend_type_;
213 AudioFocusManager::AudioFocusType audio_focus_type_; 228 AudioFocusManager::AudioFocusType audio_focus_type_;
214 229
215 MediaSessionUmaHelper uma_helper_; 230 MediaSessionUmaHelper uma_helper_;
216 231
217 // The ducking state of this media session. The initial value is |false|, and 232 // The ducking state of this media session. The initial value is |false|, and
218 // is set to |true| after StartDucking(), and will be set to |false| after 233 // is set to |true| after StartDucking(), and will be set to |false| after
219 // StopDucking(). 234 // StopDucking().
220 bool is_ducking_; 235 bool is_ducking_;
221 236
222 base::Optional<MediaMetadata> metadata_; 237 base::Optional<MediaMetadata> metadata_;
223 base::CallbackList<void(State)> media_session_state_listeners_; 238 base::CallbackList<void(State)> media_session_state_listeners_;
224 239
225 DISALLOW_COPY_AND_ASSIGN(MediaSession); 240 DISALLOW_COPY_AND_ASSIGN(MediaSession);
226 }; 241 };
227 242
228 } // namespace content 243 } // namespace content
229 244
230 #endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_ 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