Chromium Code Reviews| 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/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 #if defined(OS_ANDROID) | |
| 15 #include "content/browser/media/session/media_session_android.h" | |
| 16 #include "jni/MediaSession_jni.h" | |
| 17 #endif // defined(OS_ANDROID) | |
| 18 | |
| 14 namespace content { | 19 namespace content { |
| 15 | 20 |
| 16 namespace { | 21 namespace { |
| 17 | 22 |
| 18 const double kDefaultVolumeMultiplier = 1.0; | 23 const double kDefaultVolumeMultiplier = 1.0; |
| 19 const double kDuckingVolumeMultiplier = 0.2; | 24 const double kDuckingVolumeMultiplier = 0.2; |
| 20 | 25 |
| 21 } // anonymous namespace | 26 } // anonymous namespace |
| 22 | 27 |
| 23 using MediaSessionSuspendedSource = | 28 using MediaSessionSuspendedSource = |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 52 MediaSessionImpl* MediaSessionImpl::Get(WebContents* web_contents) { | 57 MediaSessionImpl* MediaSessionImpl::Get(WebContents* web_contents) { |
| 53 MediaSessionImpl* session = FromWebContents(web_contents); | 58 MediaSessionImpl* session = FromWebContents(web_contents); |
| 54 if (!session) { | 59 if (!session) { |
| 55 CreateForWebContents(web_contents); | 60 CreateForWebContents(web_contents); |
| 56 session = FromWebContents(web_contents); | 61 session = FromWebContents(web_contents); |
| 57 session->Initialize(); | 62 session->Initialize(); |
| 58 } | 63 } |
| 59 return session; | 64 return session; |
| 60 } | 65 } |
| 61 | 66 |
| 67 #if defined(OS_ANDROID) | |
| 68 bool RegisterMediaSessionNatives(JNIEnv* env) { | |
| 69 return RegisterNativesImpl(env); | |
| 70 } | |
| 71 | |
| 72 // static | |
| 73 base::android::ScopedJavaLocalRef<jobject> GetMediaSessionFromWebContents( | |
| 74 JNIEnv* env, | |
| 75 const base::android::JavaParamRef<jclass>& clazz, | |
| 76 const base::android::JavaParamRef<jobject>& j_contents_android) { | |
| 77 WebContents* contents = WebContents::FromJavaWebContents(j_contents_android); | |
| 78 DCHECK(contents); | |
|
mlamouri (slow - plz ping)
2016/10/28 10:24:50
Early return?
Zhiqiang Zhang (Slow)
2016/10/28 11:18:48
Done.
| |
| 79 MediaSessionImpl* session = MediaSessionImpl::Get(contents); | |
| 80 return session->session_android()->GetJavaSession(); | |
| 81 } | |
| 82 | |
| 83 #endif // defined(OS_ANDROID) | |
| 84 | |
| 62 MediaSessionImpl::~MediaSessionImpl() { | 85 MediaSessionImpl::~MediaSessionImpl() { |
| 63 DCHECK(players_.empty()); | 86 DCHECK(players_.empty()); |
| 64 DCHECK(audio_focus_state_ == State::INACTIVE); | 87 DCHECK(audio_focus_state_ == State::INACTIVE); |
| 65 for (auto& observer : observers_) | 88 for (auto& observer : observers_) |
| 66 observer.MediaSessionDestroyed(); | 89 observer.MediaSessionDestroyed(); |
| 67 for (auto& observer : observers_) | 90 for (auto& observer : observers_) |
| 68 observer.StopObserving(); | 91 observer.StopObserving(); |
| 69 } | 92 } |
| 70 | 93 |
| 71 void MediaSessionImpl::WebContentsDestroyed() { | 94 void MediaSessionImpl::WebContentsDestroyed() { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 86 observers_.AddObserver(observer); | 109 observers_.AddObserver(observer); |
| 87 } | 110 } |
| 88 | 111 |
| 89 void MediaSessionImpl::RemoveObserver(MediaSessionObserver* observer) { | 112 void MediaSessionImpl::RemoveObserver(MediaSessionObserver* observer) { |
| 90 observers_.RemoveObserver(observer); | 113 observers_.RemoveObserver(observer); |
| 91 } | 114 } |
| 92 | 115 |
| 93 void MediaSessionImpl::SetMetadata( | 116 void MediaSessionImpl::SetMetadata( |
| 94 const base::Optional<MediaMetadata>& metadata) { | 117 const base::Optional<MediaMetadata>& metadata) { |
| 95 metadata_ = metadata; | 118 metadata_ = metadata; |
| 96 static_cast<WebContentsImpl*>(web_contents()) | |
| 97 ->OnMediaSessionMetadataChanged(); | |
| 98 for (auto& observer : observers_) | 119 for (auto& observer : observers_) |
| 99 observer.MediaSessionMetadataChanged(metadata); | 120 observer.MediaSessionMetadataChanged(metadata); |
| 100 } | 121 } |
| 101 | 122 |
| 102 bool MediaSessionImpl::AddPlayer(MediaSessionPlayerObserver* observer, | 123 bool MediaSessionImpl::AddPlayer(MediaSessionPlayerObserver* observer, |
| 103 int player_id, | 124 int player_id, |
| 104 media::MediaContentType media_content_type) { | 125 media::MediaContentType media_content_type) { |
| 105 if (media_content_type == media::MediaContentType::Uncontrollable) | 126 if (media_content_type == media::MediaContentType::Uncontrollable) |
| 106 return true; | 127 return true; |
| 107 if (media_content_type == media::MediaContentType::Pepper) | 128 if (media_content_type == media::MediaContentType::Pepper) |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 393 it.observer->OnSetVolumeMultiplier(it.player_id, GetVolumeMultiplier()); | 414 it.observer->OnSetVolumeMultiplier(it.player_id, GetVolumeMultiplier()); |
| 394 | 415 |
| 395 UpdateWebContents(); | 416 UpdateWebContents(); |
| 396 } | 417 } |
| 397 | 418 |
| 398 MediaSessionImpl::MediaSessionImpl(WebContents* web_contents) | 419 MediaSessionImpl::MediaSessionImpl(WebContents* web_contents) |
| 399 : WebContentsObserver(web_contents), | 420 : WebContentsObserver(web_contents), |
| 400 audio_focus_state_(State::INACTIVE), | 421 audio_focus_state_(State::INACTIVE), |
| 401 audio_focus_type_( | 422 audio_focus_type_( |
| 402 AudioFocusManager::AudioFocusType::GainTransientMayDuck), | 423 AudioFocusManager::AudioFocusType::GainTransientMayDuck), |
| 403 is_ducking_(false) {} | 424 is_ducking_(false) { |
| 425 #if defined(OS_ANDROID) | |
| 426 session_android_.reset(new MediaSessionAndroid(this)); | |
| 427 #endif // defined(OS_ANDROID) | |
| 428 } | |
| 404 | 429 |
| 405 void MediaSessionImpl::Initialize() { | 430 void MediaSessionImpl::Initialize() { |
| 406 delegate_ = AudioFocusDelegate::Create(this); | 431 delegate_ = AudioFocusDelegate::Create(this); |
| 407 } | 432 } |
| 408 | 433 |
| 409 bool MediaSessionImpl::RequestSystemAudioFocus( | 434 bool MediaSessionImpl::RequestSystemAudioFocus( |
| 410 AudioFocusManager::AudioFocusType audio_focus_type) { | 435 AudioFocusManager::AudioFocusType audio_focus_type) { |
| 411 bool result = delegate_->RequestAudioFocus(audio_focus_type); | 436 bool result = delegate_->RequestAudioFocus(audio_focus_type); |
| 412 uma_helper_.RecordRequestAudioFocusResult(result); | 437 uma_helper_.RecordRequestAudioFocusResult(result); |
| 413 | 438 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 424 return; | 449 return; |
| 425 } | 450 } |
| 426 delegate_->AbandonAudioFocus(); | 451 delegate_->AbandonAudioFocus(); |
| 427 | 452 |
| 428 SetAudioFocusState(State::INACTIVE); | 453 SetAudioFocusState(State::INACTIVE); |
| 429 UpdateWebContents(); | 454 UpdateWebContents(); |
| 430 } | 455 } |
| 431 | 456 |
| 432 void MediaSessionImpl::UpdateWebContents() { | 457 void MediaSessionImpl::UpdateWebContents() { |
| 433 media_session_state_listeners_.Notify(audio_focus_state_); | 458 media_session_state_listeners_.Notify(audio_focus_state_); |
| 434 static_cast<WebContentsImpl*>(web_contents())->OnMediaSessionStateChanged(); | |
| 435 for (auto& observer : observers_) | 459 for (auto& observer : observers_) |
| 436 observer.MediaSessionStateChanged(IsControllable(), IsSuspended()); | 460 observer.MediaSessionStateChanged(IsControllable(), IsSuspended()); |
| 437 } | 461 } |
| 438 | 462 |
| 439 void MediaSessionImpl::SetAudioFocusState(State audio_focus_state) { | 463 void MediaSessionImpl::SetAudioFocusState(State audio_focus_state) { |
| 440 if (audio_focus_state == audio_focus_state_) | 464 if (audio_focus_state == audio_focus_state_) |
| 441 return; | 465 return; |
| 442 | 466 |
| 443 audio_focus_state_ = audio_focus_state; | 467 audio_focus_state_ = audio_focus_state; |
| 444 switch (audio_focus_state_) { | 468 switch (audio_focus_state_) { |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 461 DCHECK(success); | 485 DCHECK(success); |
| 462 | 486 |
| 463 pepper_players_.insert(PlayerIdentifier(observer, player_id)); | 487 pepper_players_.insert(PlayerIdentifier(observer, player_id)); |
| 464 | 488 |
| 465 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); | 489 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); |
| 466 | 490 |
| 467 return true; | 491 return true; |
| 468 } | 492 } |
| 469 | 493 |
| 470 } // namespace content | 494 } // namespace content |
| OLD | NEW |