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

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

Issue 2416853005: Fixing naming issues in MediaSession (Closed)
Patch Set: addressed nits 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 #include "content/browser/media/session/media_session.h" 5 #include "content/browser/media/session/media_session.h"
6 6
7 #include "content/browser/media/session/media_session_delegate.h" 7 #include "content/browser/media/session/audio_focus_delegate.h"
8 #include "content/browser/media/session/media_session_observer.h" 8 #include "content/browser/media/session/media_session_player_observer.h"
9 #include "content/browser/web_contents/web_contents_impl.h" 9 #include "content/browser/web_contents/web_contents_impl.h"
10 #include "content/public/browser/web_contents.h" 10 #include "content/public/browser/web_contents.h"
11 #include "content/public/browser/web_contents_delegate.h" 11 #include "content/public/browser/web_contents_delegate.h"
12 #include "media/base/media_content_type.h" 12 #include "media/base/media_content_type.h"
13 13
14 namespace content { 14 namespace content {
15 15
16 namespace { 16 namespace {
17 17
18 const double kDefaultVolumeMultiplier = 1.0; 18 const double kDefaultVolumeMultiplier = 1.0;
19 const double kDuckingVolumeMultiplier = 0.2; 19 const double kDuckingVolumeMultiplier = 0.2;
20 20
21 } // anonymous namespace 21 } // anonymous namespace
22 22
23 using MediaSessionSuspendedSource = 23 using MediaSessionSuspendedSource =
24 MediaSessionUmaHelper::MediaSessionSuspendedSource; 24 MediaSessionUmaHelper::MediaSessionSuspendedSource;
25 25
26 DEFINE_WEB_CONTENTS_USER_DATA_KEY(MediaSession); 26 DEFINE_WEB_CONTENTS_USER_DATA_KEY(MediaSession);
27 27
28 MediaSession::PlayerIdentifier::PlayerIdentifier(MediaSessionObserver* observer, 28 MediaSession::PlayerIdentifier::PlayerIdentifier(
29 int player_id) 29 MediaSessionPlayerObserver* observer,
30 : observer(observer), 30 int player_id)
31 player_id(player_id) { 31 : observer(observer), player_id(player_id) {}
32 }
33 32
34 bool MediaSession::PlayerIdentifier::operator==( 33 bool MediaSession::PlayerIdentifier::operator==(
35 const PlayerIdentifier& other) const { 34 const PlayerIdentifier& other) const {
36 return this->observer == other.observer && this->player_id == other.player_id; 35 return this->observer == other.observer && this->player_id == other.player_id;
37 } 36 }
38 37
39 size_t MediaSession::PlayerIdentifier::Hash::operator()( 38 size_t MediaSession::PlayerIdentifier::Hash::operator()(
40 const PlayerIdentifier& player_identifier) const { 39 const PlayerIdentifier& player_identifier) const {
41 size_t hash = BASE_HASH_NAMESPACE::hash<MediaSessionObserver*>()( 40 size_t hash = BASE_HASH_NAMESPACE::hash<MediaSessionPlayerObserver*>()(
42 player_identifier.observer); 41 player_identifier.observer);
43 hash += BASE_HASH_NAMESPACE::hash<int>()(player_identifier.player_id); 42 hash += BASE_HASH_NAMESPACE::hash<int>()(player_identifier.player_id);
44 return hash; 43 return hash;
45 } 44 }
46 45
47 // static 46 // static
48 MediaSession* MediaSession::Get(WebContents* web_contents) { 47 MediaSession* MediaSession::Get(WebContents* web_contents) {
49 MediaSession* session = FromWebContents(web_contents); 48 MediaSession* session = FromWebContents(web_contents);
50 if (!session) { 49 if (!session) {
51 CreateForWebContents(web_contents); 50 CreateForWebContents(web_contents);
(...skipping 20 matching lines...) Expand all
72 pepper_players_.clear(); 71 pepper_players_.clear();
73 AbandonSystemAudioFocusIfNeeded(); 72 AbandonSystemAudioFocusIfNeeded();
74 } 73 }
75 74
76 void MediaSession::SetMetadata(const base::Optional<MediaMetadata>& metadata) { 75 void MediaSession::SetMetadata(const base::Optional<MediaMetadata>& metadata) {
77 metadata_ = metadata; 76 metadata_ = metadata;
78 static_cast<WebContentsImpl*>(web_contents()) 77 static_cast<WebContentsImpl*>(web_contents())
79 ->OnMediaSessionMetadataChanged(); 78 ->OnMediaSessionMetadataChanged();
80 } 79 }
81 80
82 bool MediaSession::AddPlayer(MediaSessionObserver* observer, 81 bool MediaSession::AddPlayer(MediaSessionPlayerObserver* observer,
83 int player_id, 82 int player_id,
84 media::MediaContentType media_content_type) { 83 media::MediaContentType media_content_type) {
85 if (media_content_type == media::MediaContentType::Pepper) 84 if (media_content_type == media::MediaContentType::Pepper)
86 return AddPepperPlayer(observer, player_id); 85 return AddPepperPlayer(observer, player_id);
87 86
88 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); 87 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier());
89 88
90 // Determine the audio focus type required for playing the new player. 89 // Determine the audio focus type required for playing the new player.
91 // TODO(zqzhang): handle duckable and uncontrollable. 90 // TODO(zqzhang): handle duckable and uncontrollable.
92 // See https://crbug.com/639277. 91 // See https://crbug.com/639277.
(...skipping 26 matching lines...) Expand all
119 // suspended. 118 // suspended.
120 if (old_audio_focus_state != State::ACTIVE) 119 if (old_audio_focus_state != State::ACTIVE)
121 players_.clear(); 120 players_.clear();
122 121
123 players_.insert(PlayerIdentifier(observer, player_id)); 122 players_.insert(PlayerIdentifier(observer, player_id));
124 UpdateWebContents(); 123 UpdateWebContents();
125 124
126 return true; 125 return true;
127 } 126 }
128 127
129 void MediaSession::RemovePlayer(MediaSessionObserver* observer, 128 void MediaSession::RemovePlayer(MediaSessionPlayerObserver* observer,
130 int player_id) { 129 int player_id) {
131 auto it = players_.find(PlayerIdentifier(observer, player_id)); 130 auto it = players_.find(PlayerIdentifier(observer, player_id));
132 if (it != players_.end()) 131 if (it != players_.end())
133 players_.erase(it); 132 players_.erase(it);
134 133
135 it = pepper_players_.find(PlayerIdentifier(observer, player_id)); 134 it = pepper_players_.find(PlayerIdentifier(observer, player_id));
136 if (it != pepper_players_.end()) 135 if (it != pepper_players_.end())
137 pepper_players_.erase(it); 136 pepper_players_.erase(it);
138 137
139 AbandonSystemAudioFocusIfNeeded(); 138 AbandonSystemAudioFocusIfNeeded();
140 } 139 }
141 140
142 void MediaSession::RemovePlayers(MediaSessionObserver* observer) { 141 void MediaSession::RemovePlayers(MediaSessionPlayerObserver* observer) {
143 for (auto it = players_.begin(); it != players_.end(); ) { 142 for (auto it = players_.begin(); it != players_.end(); ) {
144 if (it->observer == observer) 143 if (it->observer == observer)
145 players_.erase(it++); 144 players_.erase(it++);
146 else 145 else
147 ++it; 146 ++it;
148 } 147 }
149 148
150 for (auto it = pepper_players_.begin(); it != pepper_players_.end(); ) { 149 for (auto it = pepper_players_.begin(); it != pepper_players_.end(); ) {
151 if (it->observer == observer) 150 if (it->observer == observer)
152 pepper_players_.erase(it++); 151 pepper_players_.erase(it++);
153 else 152 else
154 ++it; 153 ++it;
155 } 154 }
156 155
157 AbandonSystemAudioFocusIfNeeded(); 156 AbandonSystemAudioFocusIfNeeded();
158 } 157 }
159 158
160 void MediaSession::RecordSessionDuck() { 159 void MediaSession::RecordSessionDuck() {
161 uma_helper_.RecordSessionSuspended( 160 uma_helper_.RecordSessionSuspended(
162 MediaSessionSuspendedSource::SystemTransientDuck); 161 MediaSessionSuspendedSource::SystemTransientDuck);
163 } 162 }
164 163
165 void MediaSession::OnPlayerPaused(MediaSessionObserver* observer, 164 void MediaSession::OnPlayerPaused(MediaSessionPlayerObserver* observer,
166 int player_id) { 165 int player_id) {
167 // If a playback is completed, BrowserMediaPlayerManager will call 166 // If a playback is completed, BrowserMediaPlayerManager will call
168 // OnPlayerPaused() after RemovePlayer(). This is a workaround. 167 // OnPlayerPaused() after RemovePlayer(). This is a workaround.
169 // Also, this method may be called when a player that is not added 168 // Also, this method may be called when a player that is not added
170 // to this session (e.g. a silent video) is paused. MediaSession 169 // to this session (e.g. a silent video) is paused. MediaSession
171 // should ignore the paused player for this case. 170 // should ignore the paused player for this case.
172 if (!players_.count(PlayerIdentifier(observer, player_id))) 171 if (!players_.count(PlayerIdentifier(observer, player_id)))
173 return; 172 return;
174 173
175 // If there is more than one observer, remove the paused one from the session. 174 // If there is more than one observer, remove the paused one from the session.
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 return !pepper_players_.empty(); 277 return !pepper_players_.empty();
279 } 278 }
280 279
281 std::unique_ptr<base::CallbackList<void(MediaSession::State)>::Subscription> 280 std::unique_ptr<base::CallbackList<void(MediaSession::State)>::Subscription>
282 MediaSession::RegisterMediaSessionStateChangedCallbackForTest( 281 MediaSession::RegisterMediaSessionStateChangedCallbackForTest(
283 const StateChangedCallback& cb) { 282 const StateChangedCallback& cb) {
284 return media_session_state_listeners_.Add(cb); 283 return media_session_state_listeners_.Add(cb);
285 } 284 }
286 285
287 void MediaSession::SetDelegateForTests( 286 void MediaSession::SetDelegateForTests(
288 std::unique_ptr<MediaSessionDelegate> delegate) { 287 std::unique_ptr<AudioFocusDelegate> delegate) {
289 delegate_ = std::move(delegate); 288 delegate_ = std::move(delegate);
290 } 289 }
291 290
292 bool MediaSession::IsActiveForTest() const { 291 bool MediaSession::IsActiveForTest() const {
293 return audio_focus_state_ == State::ACTIVE; 292 return audio_focus_state_ == State::ACTIVE;
294 } 293 }
295 294
296 MediaSessionUmaHelper* MediaSession::uma_helper_for_test() { 295 MediaSessionUmaHelper* MediaSession::uma_helper_for_test() {
297 return &uma_helper_; 296 return &uma_helper_;
298 } 297 }
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 } 369 }
371 370
372 MediaSession::MediaSession(WebContents* web_contents) 371 MediaSession::MediaSession(WebContents* web_contents)
373 : WebContentsObserver(web_contents), 372 : WebContentsObserver(web_contents),
374 audio_focus_state_(State::INACTIVE), 373 audio_focus_state_(State::INACTIVE),
375 audio_focus_type_( 374 audio_focus_type_(
376 AudioFocusManager::AudioFocusType::GainTransientMayDuck), 375 AudioFocusManager::AudioFocusType::GainTransientMayDuck),
377 is_ducking_(false) {} 376 is_ducking_(false) {}
378 377
379 void MediaSession::Initialize() { 378 void MediaSession::Initialize() {
380 delegate_ = MediaSessionDelegate::Create(this); 379 delegate_ = AudioFocusDelegate::Create(this);
381 } 380 }
382 381
383 bool MediaSession::RequestSystemAudioFocus( 382 bool MediaSession::RequestSystemAudioFocus(
384 AudioFocusManager::AudioFocusType audio_focus_type) { 383 AudioFocusManager::AudioFocusType audio_focus_type) {
385 bool result = delegate_->RequestAudioFocus(audio_focus_type); 384 bool result = delegate_->RequestAudioFocus(audio_focus_type);
386 uma_helper_.RecordRequestAudioFocusResult(result); 385 uma_helper_.RecordRequestAudioFocusResult(result);
387 386
388 // MediaSession must change its state & audio focus type AFTER requesting 387 // MediaSession must change its state & audio focus type AFTER requesting
389 // audio focus. 388 // audio focus.
390 SetAudioFocusState(result ? State::ACTIVE : State::INACTIVE); 389 SetAudioFocusState(result ? State::ACTIVE : State::INACTIVE);
(...skipping 28 matching lines...) Expand all
419 break; 418 break;
420 case State::SUSPENDED: 419 case State::SUSPENDED:
421 uma_helper_.OnSessionSuspended(); 420 uma_helper_.OnSessionSuspended();
422 break; 421 break;
423 case State::INACTIVE: 422 case State::INACTIVE:
424 uma_helper_.OnSessionInactive(); 423 uma_helper_.OnSessionInactive();
425 break; 424 break;
426 } 425 }
427 } 426 }
428 427
429 bool MediaSession::AddPepperPlayer(MediaSessionObserver* observer, 428 bool MediaSession::AddPepperPlayer(MediaSessionPlayerObserver* observer,
430 int player_id) { 429 int player_id) {
431 bool success = RequestSystemAudioFocus( 430 bool success = RequestSystemAudioFocus(
432 AudioFocusManager::AudioFocusType::Gain); 431 AudioFocusManager::AudioFocusType::Gain);
433 DCHECK(success); 432 DCHECK(success);
434 433
435 pepper_players_.insert(PlayerIdentifier(observer, player_id)); 434 pepper_players_.insert(PlayerIdentifier(observer, player_id));
436 435
437 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); 436 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier());
438 437
439 return true; 438 return true;
440 } 439 }
441 440
442 } // namespace content 441 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/media/session/media_session.h ('k') | content/browser/media/session/media_session_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698