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.cc

Issue 1996043002: Split MediaContentType and AudioFocusType (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressed Mounir's comments Created 4 years, 4 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/media_session_delegate.h"
8 #include "content/browser/media/session/media_session_observer.h" 8 #include "content/browser/media/session/media_session_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 13
13 namespace content { 14 namespace content {
14 15
15 namespace { 16 namespace {
16 17
17 const double kDefaultVolumeMultiplier = 1.0; 18 const double kDefaultVolumeMultiplier = 1.0;
18 19
19 } // anonymous namespace 20 } // anonymous namespace
20 21
21 using MediaSessionSuspendedSource = 22 using MediaSessionSuspendedSource =
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 void MediaSession::SetMetadata(const MediaMetadata& metadata) { 62 void MediaSession::SetMetadata(const MediaMetadata& metadata) {
62 metadata_ = metadata; 63 metadata_ = metadata;
63 // TODO(zqzhang): On Android, the metadata is sent though JNI everytime the 64 // TODO(zqzhang): On Android, the metadata is sent though JNI everytime the
64 // media session play/pause state changes. Need to find a way to seprate the 65 // media session play/pause state changes. Need to find a way to seprate the
65 // state change and Metadata update. See https://crbug.com/621855. 66 // state change and Metadata update. See https://crbug.com/621855.
66 static_cast<WebContentsImpl*>(web_contents())->OnMediaSessionStateChanged(); 67 static_cast<WebContentsImpl*>(web_contents())->OnMediaSessionStateChanged();
67 } 68 }
68 69
69 bool MediaSession::AddPlayer(MediaSessionObserver* observer, 70 bool MediaSession::AddPlayer(MediaSessionObserver* observer,
70 int player_id, 71 int player_id,
71 Type type) { 72 media::MediaContentType type) {
73 if (type == media::MediaContentType::Uncontrollable) {
74 return true;
75 }
whywhat 2016/08/18 20:33:41 nit: no need for {} here
Zhiqiang Zhang (Slow) 2016/08/19 15:05:55 Done.
72 observer->OnSetVolumeMultiplier(player_id, volume_multiplier_); 76 observer->OnSetVolumeMultiplier(player_id, volume_multiplier_);
73 77
74 // If the audio focus is already granted and is of type Content, there is 78 // If the audio focus is already granted and is of type Content, there is
whywhat 2016/08/18 20:33:41 nit: this comment needs to be updated
Zhiqiang Zhang (Slow) 2016/08/19 15:05:55 Moved these to the next block (early return).
75 // nothing to do. If it is granted of type Transient the requested type is 79 // nothing to do. If it is granted of type Transient the requested type is
76 // also transient, there is also nothing to do. Otherwise, the session needs 80 // also transient, there is also nothing to do. Otherwise, the session needs
77 // to request audio focus again. 81 // to request audio focus again.
82 // TODO(zqzhang): handle duckable and uncontrollable.
whywhat 2016/08/18 20:33:41 nit: do you need to file a crbug to track the TODO
Zhiqiang Zhang (Slow) 2016/08/19 15:05:55 Done.
83 AudioFocusManager::AudioFocusType required_audio_focus_type =
whywhat 2016/08/18 20:33:42 could this be a media::AudioFocutTypeFromMediaCont
Zhiqiang Zhang (Slow) 2016/08/19 15:05:55 Using if for now. After adding Pepper&WebRTC, we'l
84 (type == media::MediaContentType::Gain)
whywhat 2016/08/18 20:33:41 there's no MediaContentType::Gain, right?
Zhiqiang Zhang (Slow) 2016/08/19 15:05:55 Done.
85 ? AudioFocusManager::AudioFocusType::Gain
86 : AudioFocusManager::AudioFocusType::GainTransientMayDuck;
87
78 if (audio_focus_state_ == State::ACTIVE && 88 if (audio_focus_state_ == State::ACTIVE &&
79 (audio_focus_type_ == Type::Content || audio_focus_type_ == type)) { 89 (audio_focus_type_ == AudioFocusManager::AudioFocusType::Gain ||
90 audio_focus_type_ == required_audio_focus_type)) {
80 players_.insert(PlayerIdentifier(observer, player_id)); 91 players_.insert(PlayerIdentifier(observer, player_id));
81 return true; 92 return true;
82 } 93 }
83 94
84 State old_audio_focus_state = audio_focus_state_; 95 State old_audio_focus_state = audio_focus_state_;
85 State audio_focus_state = RequestSystemAudioFocus(type) ? State::ACTIVE 96 State audio_focus_state = RequestSystemAudioFocus(required_audio_focus_type)
86 : State::INACTIVE; 97 ? State::ACTIVE
98 : State::INACTIVE;
87 SetAudioFocusState(audio_focus_state); 99 SetAudioFocusState(audio_focus_state);
88 audio_focus_type_ = type; 100 audio_focus_type_ = required_audio_focus_type;
89 101
90 if (audio_focus_state_ != State::ACTIVE) 102 if (audio_focus_state_ != State::ACTIVE)
91 return false; 103 return false;
92 104
93 // The session should be reset if a player is starting while all players are 105 // The session should be reset if a player is starting while all players are
94 // suspended. 106 // suspended.
95 if (old_audio_focus_state != State::ACTIVE) 107 if (old_audio_focus_state != State::ACTIVE)
96 players_.clear(); 108 players_.clear();
97 109
98 players_.insert(PlayerIdentifier(observer, player_id)); 110 players_.insert(PlayerIdentifier(observer, player_id));
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 bool MediaSession::IsReallySuspended() const { 217 bool MediaSession::IsReallySuspended() const {
206 return audio_focus_state_ == State::SUSPENDED; 218 return audio_focus_state_ == State::SUSPENDED;
207 } 219 }
208 220
209 bool MediaSession::IsSuspended() const { 221 bool MediaSession::IsSuspended() const {
210 // TODO(mlamouri): should be == State::SUSPENDED. 222 // TODO(mlamouri): should be == State::SUSPENDED.
211 return audio_focus_state_ != State::ACTIVE; 223 return audio_focus_state_ != State::ACTIVE;
212 } 224 }
213 225
214 bool MediaSession::IsControllable() const { 226 bool MediaSession::IsControllable() const {
215 // Only content type media session can be controllable unless it is inactive. 227 // Only content type media session can be controllable unless it is inactive.
whywhat 2016/08/18 20:33:42 nit: update comment?
Zhiqiang Zhang (Slow) 2016/08/19 15:05:55 Done.
216 return audio_focus_state_ != State::INACTIVE && 228 return audio_focus_state_ != State::INACTIVE &&
217 audio_focus_type_ == Type::Content; 229 audio_focus_type_ == AudioFocusManager::AudioFocusType::Gain;
218 } 230 }
219 231
220 std::unique_ptr<base::CallbackList<void(MediaSession::State)>::Subscription> 232 std::unique_ptr<base::CallbackList<void(MediaSession::State)>::Subscription>
221 MediaSession::RegisterMediaSessionStateChangedCallbackForTest( 233 MediaSession::RegisterMediaSessionStateChangedCallbackForTest(
222 const StateChangedCallback& cb) { 234 const StateChangedCallback& cb) {
223 return media_session_state_listeners_.Add(cb); 235 return media_session_state_listeners_.Add(cb);
224 } 236 }
225 237
226 void MediaSession::SetDelegateForTests( 238 void MediaSession::SetDelegateForTests(
227 std::unique_ptr<MediaSessionDelegate> delegate) { 239 std::unique_ptr<MediaSessionDelegate> delegate) {
228 delegate_ = std::move(delegate); 240 delegate_ = std::move(delegate);
229 } 241 }
230 242
231 bool MediaSession::IsActiveForTest() const { 243 bool MediaSession::IsActiveForTest() const {
232 return audio_focus_state_ == State::ACTIVE; 244 return audio_focus_state_ == State::ACTIVE;
233 } 245 }
234 246
235 MediaSession::Type MediaSession::audio_focus_type_for_test() const { 247 AudioFocusManager::AudioFocusType MediaSession::audio_focus_type_for_test()
248 const {
236 return audio_focus_type_; 249 return audio_focus_type_;
237 } 250 }
238 251
239 MediaSessionUmaHelper* MediaSession::uma_helper_for_test() { 252 MediaSessionUmaHelper* MediaSession::uma_helper_for_test() {
240 return &uma_helper_; 253 return &uma_helper_;
241 } 254 }
242 255
243 void MediaSession::RemoveAllPlayersForTest() { 256 void MediaSession::RemoveAllPlayersForTest() {
244 players_.clear(); 257 players_.clear();
245 AbandonSystemAudioFocusIfNeeded(); 258 AbandonSystemAudioFocusIfNeeded();
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 312
300 for (const auto& it : players_) 313 for (const auto& it : players_)
301 it.observer->OnResume(it.player_id); 314 it.observer->OnResume(it.player_id);
302 315
303 UpdateWebContents(); 316 UpdateWebContents();
304 } 317 }
305 318
306 MediaSession::MediaSession(WebContents* web_contents) 319 MediaSession::MediaSession(WebContents* web_contents)
307 : WebContentsObserver(web_contents), 320 : WebContentsObserver(web_contents),
308 audio_focus_state_(State::INACTIVE), 321 audio_focus_state_(State::INACTIVE),
309 audio_focus_type_(Type::Transient), 322 audio_focus_type_(
310 volume_multiplier_(kDefaultVolumeMultiplier) { 323 AudioFocusManager::AudioFocusType::GainTransientMayDuck),
311 } 324 volume_multiplier_(kDefaultVolumeMultiplier) {}
312 325
313 void MediaSession::Initialize() { 326 void MediaSession::Initialize() {
314 delegate_ = MediaSessionDelegate::Create(this); 327 delegate_ = MediaSessionDelegate::Create(this);
315 } 328 }
316 329
317 bool MediaSession::RequestSystemAudioFocus(Type type) { 330 bool MediaSession::RequestSystemAudioFocus(
331 AudioFocusManager::AudioFocusType type) {
318 bool result = delegate_->RequestAudioFocus(type); 332 bool result = delegate_->RequestAudioFocus(type);
319 uma_helper_.RecordRequestAudioFocusResult(result); 333 uma_helper_.RecordRequestAudioFocusResult(result);
320 return result; 334 return result;
321 } 335 }
322 336
323 void MediaSession::AbandonSystemAudioFocusIfNeeded() { 337 void MediaSession::AbandonSystemAudioFocusIfNeeded() {
324 if (audio_focus_state_ == State::INACTIVE || !players_.empty()) 338 if (audio_focus_state_ == State::INACTIVE || !players_.empty())
325 return; 339 return;
326 340
327 delegate_->AbandonAudioFocus(); 341 delegate_->AbandonAudioFocus();
(...skipping 19 matching lines...) Expand all
347 case State::SUSPENDED: 361 case State::SUSPENDED:
348 uma_helper_.OnSessionSuspended(); 362 uma_helper_.OnSessionSuspended();
349 break; 363 break;
350 case State::INACTIVE: 364 case State::INACTIVE:
351 uma_helper_.OnSessionInactive(); 365 uma_helper_.OnSessionInactive();
352 break; 366 break;
353 } 367 }
354 } 368 }
355 369
356 } // namespace content 370 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698