OLD | NEW |
---|---|
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_impl.h" | 5 #include "content/browser/media/session/media_session_impl.h" |
6 | 6 |
7 #include "content/browser/media/session/audio_focus_delegate.h" | 7 #include "content/browser/media/session/audio_focus_delegate.h" |
8 #include "content/browser/media/session/media_session_player_observer.h" | 8 #include "content/browser/media/session/media_session_player_observer.h" |
9 #include "content/browser/media/session/media_session_service_impl.h" | |
9 #include "content/browser/web_contents/web_contents_impl.h" | 10 #include "content/browser/web_contents/web_contents_impl.h" |
11 #include "content/public/browser/media_session.h" | |
12 #include "content/public/browser/media_session_observer.h" | |
10 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
11 #include "content/public/browser/web_contents_delegate.h" | |
12 #include "media/base/media_content_type.h" | 14 #include "media/base/media_content_type.h" |
13 | 15 |
14 #if defined(OS_ANDROID) | 16 #if defined(OS_ANDROID) |
15 #include "content/browser/media/session/media_session_android.h" | 17 #include "content/browser/media/session/media_session_android.h" |
16 #include "jni/MediaSession_jni.h" | 18 #include "jni/MediaSession_jni.h" |
17 #endif // defined(OS_ANDROID) | 19 #endif // defined(OS_ANDROID) |
18 | 20 |
19 namespace content { | 21 namespace content { |
20 | 22 |
21 namespace { | 23 namespace { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 observer.MediaSessionDestroyed(); | 91 observer.MediaSessionDestroyed(); |
90 for (auto& observer : observers_) | 92 for (auto& observer : observers_) |
91 observer.StopObserving(); | 93 observer.StopObserving(); |
92 } | 94 } |
93 | 95 |
94 void MediaSessionImpl::WebContentsDestroyed() { | 96 void MediaSessionImpl::WebContentsDestroyed() { |
95 // This should only work for tests. In production, all the players should have | 97 // This should only work for tests. In production, all the players should have |
96 // already been removed before WebContents is destroyed. | 98 // already been removed before WebContents is destroyed. |
97 | 99 |
98 // TODO(zqzhang): refactor MediaSessionImpl, maybe move the interface used to | 100 // TODO(zqzhang): refactor MediaSessionImpl, maybe move the interface used to |
99 // talk | 101 // talk with AudioFocusManager out to a seperate class. The AudioFocusManager |
100 // with AudioFocusManager out to a seperate class. The AudioFocusManager unit | 102 // unit tests then could mock the interface and abandon audio focus when |
101 // tests then could mock the interface and abandon audio focus when | |
102 // WebContents is destroyed. See https://crbug.com/651069 | 103 // WebContents is destroyed. See https://crbug.com/651069 |
103 players_.clear(); | 104 players_.clear(); |
104 pepper_players_.clear(); | 105 pepper_players_.clear(); |
105 AbandonSystemAudioFocusIfNeeded(); | 106 AbandonSystemAudioFocusIfNeeded(); |
106 } | 107 } |
107 | 108 |
109 void MediaSessionImpl::SetMediaSessionService( | |
110 MediaSessionServiceImpl* service) { | |
111 service_ = service; | |
whywhat
2016/10/28 16:10:23
nit: I assume this is supposed to be called only o
Zhiqiang Zhang (Slow)
2016/11/01 15:24:15
It could be set and unset.
My point is the service
| |
112 } | |
113 | |
108 void MediaSessionImpl::AddObserver(MediaSessionObserver* observer) { | 114 void MediaSessionImpl::AddObserver(MediaSessionObserver* observer) { |
109 observers_.AddObserver(observer); | 115 observers_.AddObserver(observer); |
110 } | 116 } |
111 | 117 |
112 void MediaSessionImpl::RemoveObserver(MediaSessionObserver* observer) { | 118 void MediaSessionImpl::RemoveObserver(MediaSessionObserver* observer) { |
113 observers_.RemoveObserver(observer); | 119 observers_.RemoveObserver(observer); |
114 } | 120 } |
115 | 121 |
116 void MediaSessionImpl::SetMetadata( | 122 void MediaSessionImpl::SetMetadata( |
117 const base::Optional<MediaMetadata>& metadata) { | 123 const base::Optional<MediaMetadata>& metadata) { |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
269 | 275 |
270 if (audio_focus_state_ != State::SUSPENDED) | 276 if (audio_focus_state_ != State::SUSPENDED) |
271 OnSuspendInternal(suspend_type, State::SUSPENDED); | 277 OnSuspendInternal(suspend_type, State::SUSPENDED); |
272 | 278 |
273 DCHECK(audio_focus_state_ == State::SUSPENDED); | 279 DCHECK(audio_focus_state_ == State::SUSPENDED); |
274 players_.clear(); | 280 players_.clear(); |
275 | 281 |
276 AbandonSystemAudioFocusIfNeeded(); | 282 AbandonSystemAudioFocusIfNeeded(); |
277 } | 283 } |
278 | 284 |
285 void MediaSessionImpl::DidReceiveAction( | |
286 blink::mojom::MediaSessionAction action) { | |
287 if (service_) | |
whywhat
2016/10/28 16:10:23
nit: Is it possible to have service_ nullptr?
Zhiqiang Zhang (Slow)
2016/11/01 15:24:15
See my other reply.
| |
288 service_->GetClient()->DidReceiveAction(action); | |
289 } | |
290 | |
291 void MediaSessionImpl::OnMediaSessionEnabledAction( | |
292 blink::mojom::MediaSessionAction action) { | |
293 for (auto& observer : observers_) | |
294 observer.MediaSessionEnabledAction(action); | |
295 } | |
296 | |
297 void MediaSessionImpl::OnMediaSessionDisabledAction( | |
298 blink::mojom::MediaSessionAction action) { | |
299 for (auto& observer : observers_) | |
300 observer.MediaSessionDisabledAction(action); | |
301 } | |
302 | |
279 void MediaSessionImpl::StartDucking() { | 303 void MediaSessionImpl::StartDucking() { |
280 if (is_ducking_) | 304 if (is_ducking_) |
281 return; | 305 return; |
282 is_ducking_ = true; | 306 is_ducking_ = true; |
283 UpdateVolumeMultiplier(); | 307 UpdateVolumeMultiplier(); |
284 } | 308 } |
285 | 309 |
286 void MediaSessionImpl::StopDucking() { | 310 void MediaSessionImpl::StopDucking() { |
287 if (!is_ducking_) | 311 if (!is_ducking_) |
288 return; | 312 return; |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
414 it.observer->OnSetVolumeMultiplier(it.player_id, GetVolumeMultiplier()); | 438 it.observer->OnSetVolumeMultiplier(it.player_id, GetVolumeMultiplier()); |
415 | 439 |
416 UpdateWebContents(); | 440 UpdateWebContents(); |
417 } | 441 } |
418 | 442 |
419 MediaSessionImpl::MediaSessionImpl(WebContents* web_contents) | 443 MediaSessionImpl::MediaSessionImpl(WebContents* web_contents) |
420 : WebContentsObserver(web_contents), | 444 : WebContentsObserver(web_contents), |
421 audio_focus_state_(State::INACTIVE), | 445 audio_focus_state_(State::INACTIVE), |
422 audio_focus_type_( | 446 audio_focus_type_( |
423 AudioFocusManager::AudioFocusType::GainTransientMayDuck), | 447 AudioFocusManager::AudioFocusType::GainTransientMayDuck), |
424 is_ducking_(false) { | 448 is_ducking_(false), |
449 service_(nullptr) { | |
425 #if defined(OS_ANDROID) | 450 #if defined(OS_ANDROID) |
426 session_android_.reset(new MediaSessionAndroid(this)); | 451 session_android_.reset(new MediaSessionAndroid(this)); |
427 #endif // defined(OS_ANDROID) | 452 #endif // defined(OS_ANDROID) |
428 } | 453 } |
429 | 454 |
430 void MediaSessionImpl::Initialize() { | 455 void MediaSessionImpl::Initialize() { |
431 delegate_ = AudioFocusDelegate::Create(this); | 456 delegate_ = AudioFocusDelegate::Create(this); |
432 } | 457 } |
433 | 458 |
434 bool MediaSessionImpl::RequestSystemAudioFocus( | 459 bool MediaSessionImpl::RequestSystemAudioFocus( |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
485 DCHECK(success); | 510 DCHECK(success); |
486 | 511 |
487 pepper_players_.insert(PlayerIdentifier(observer, player_id)); | 512 pepper_players_.insert(PlayerIdentifier(observer, player_id)); |
488 | 513 |
489 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); | 514 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); |
490 | 515 |
491 return true; | 516 return true; |
492 } | 517 } |
493 | 518 |
494 } // namespace content | 519 } // namespace content |
OLD | NEW |