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.h" | 5 #include "content/browser/media/session/media_session.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_observer.h" |
8 #include "content/browser/media/session/media_session_player_observer.h" | 9 #include "content/browser/media/session/media_session_player_observer.h" |
9 #include "content/browser/web_contents/web_contents_impl.h" | 10 #include "content/browser/web_contents/web_contents_impl.h" |
10 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
11 #include "content/public/browser/web_contents_delegate.h" | 12 #include "content/public/browser/web_contents_delegate.h" |
12 #include "media/base/media_content_type.h" | 13 #include "media/base/media_content_type.h" |
13 | 14 |
| 15 #if defined(OS_ANDROID) |
| 16 #include "content/browser/media/session/media_session_android.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 26 matching lines...) Expand all Loading... |
50 CreateForWebContents(web_contents); | 55 CreateForWebContents(web_contents); |
51 session = FromWebContents(web_contents); | 56 session = FromWebContents(web_contents); |
52 session->Initialize(); | 57 session->Initialize(); |
53 } | 58 } |
54 return session; | 59 return session; |
55 } | 60 } |
56 | 61 |
57 MediaSession::~MediaSession() { | 62 MediaSession::~MediaSession() { |
58 DCHECK(players_.empty()); | 63 DCHECK(players_.empty()); |
59 DCHECK(audio_focus_state_ == State::INACTIVE); | 64 DCHECK(audio_focus_state_ == State::INACTIVE); |
| 65 for (auto& observer : observers_) |
| 66 observer.MediaSessionDestroyed(); |
| 67 #if defined(OS_ANDROID) |
| 68 // Reset session_android to prevent further messages from Java. |
| 69 session_android_.reset(); |
| 70 #endif // defined(OS_ANDROID) |
| 71 } |
| 72 |
| 73 void MediaSession::AddObserver(MediaSessionObserver* observer) { |
| 74 observers_.AddObserver(observer); |
| 75 } |
| 76 |
| 77 void MediaSession::RemoveObserver(MediaSessionObserver* observer) { |
| 78 observers_.RemoveObserver(observer); |
60 } | 79 } |
61 | 80 |
62 void MediaSession::WebContentsDestroyed() { | 81 void MediaSession::WebContentsDestroyed() { |
63 // This should only work for tests. In production, all the players should have | 82 // This should only work for tests. In production, all the players should have |
64 // already been removed before WebContents is destroyed. | 83 // already been removed before WebContents is destroyed. |
65 | 84 |
66 // TODO(zqzhang): refactor MediaSession, maybe move the interface used to talk | 85 // TODO(zqzhang): refactor MediaSession, maybe move the interface used to talk |
67 // with AudioFocusManager out to a seperate class. The AudioFocusManager unit | 86 // with AudioFocusManager out to a seperate class. The AudioFocusManager unit |
68 // tests then could mock the interface and abandon audio focus when | 87 // tests then could mock the interface and abandon audio focus when |
69 // WebContents is destroyed. See https://crbug.com/651069 | 88 // WebContents is destroyed. See https://crbug.com/651069 |
70 players_.clear(); | 89 players_.clear(); |
71 pepper_players_.clear(); | 90 pepper_players_.clear(); |
72 AbandonSystemAudioFocusIfNeeded(); | 91 AbandonSystemAudioFocusIfNeeded(); |
73 } | 92 } |
74 | 93 |
75 void MediaSession::SetMetadata(const base::Optional<MediaMetadata>& metadata) { | 94 void MediaSession::SetMetadata(const base::Optional<MediaMetadata>& metadata) { |
76 metadata_ = metadata; | 95 metadata_ = metadata; |
77 static_cast<WebContentsImpl*>(web_contents()) | 96 |
78 ->OnMediaSessionMetadataChanged(); | 97 for (auto& observer : observers_) |
| 98 observer.MediaSessionMetadataChanged(metadata_); |
79 } | 99 } |
80 | 100 |
81 bool MediaSession::AddPlayer(MediaSessionPlayerObserver* observer, | 101 bool MediaSession::AddPlayer(MediaSessionPlayerObserver* observer, |
82 int player_id, | 102 int player_id, |
83 media::MediaContentType media_content_type) { | 103 media::MediaContentType media_content_type) { |
84 if (media_content_type == media::MediaContentType::Uncontrollable) | 104 if (media_content_type == media::MediaContentType::Uncontrollable) |
85 return true; | 105 return true; |
86 if (media_content_type == media::MediaContentType::Pepper) | 106 if (media_content_type == media::MediaContentType::Pepper) |
87 return AddPepperPlayer(observer, player_id); | 107 return AddPepperPlayer(observer, player_id); |
88 | 108 |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 it.observer->OnSetVolumeMultiplier(it.player_id, GetVolumeMultiplier()); | 392 it.observer->OnSetVolumeMultiplier(it.player_id, GetVolumeMultiplier()); |
373 | 393 |
374 UpdateWebContents(); | 394 UpdateWebContents(); |
375 } | 395 } |
376 | 396 |
377 MediaSession::MediaSession(WebContents* web_contents) | 397 MediaSession::MediaSession(WebContents* web_contents) |
378 : WebContentsObserver(web_contents), | 398 : WebContentsObserver(web_contents), |
379 audio_focus_state_(State::INACTIVE), | 399 audio_focus_state_(State::INACTIVE), |
380 audio_focus_type_( | 400 audio_focus_type_( |
381 AudioFocusManager::AudioFocusType::GainTransientMayDuck), | 401 AudioFocusManager::AudioFocusType::GainTransientMayDuck), |
382 is_ducking_(false) {} | 402 is_ducking_(false) { |
| 403 #if defined(OS_ANDROID) |
| 404 session_android_.reset(new MediaSessionAndroid(this)); |
| 405 #endif // defined(OS_ANDROID) |
| 406 } |
383 | 407 |
384 void MediaSession::Initialize() { | 408 void MediaSession::Initialize() { |
385 delegate_ = AudioFocusDelegate::Create(this); | 409 delegate_ = AudioFocusDelegate::Create(this); |
386 } | 410 } |
387 | 411 |
388 bool MediaSession::RequestSystemAudioFocus( | 412 bool MediaSession::RequestSystemAudioFocus( |
389 AudioFocusManager::AudioFocusType audio_focus_type) { | 413 AudioFocusManager::AudioFocusType audio_focus_type) { |
390 bool result = delegate_->RequestAudioFocus(audio_focus_type); | 414 bool result = delegate_->RequestAudioFocus(audio_focus_type); |
391 uma_helper_.RecordRequestAudioFocusResult(result); | 415 uma_helper_.RecordRequestAudioFocusResult(result); |
392 | 416 |
(...skipping 10 matching lines...) Expand all Loading... |
403 return; | 427 return; |
404 } | 428 } |
405 delegate_->AbandonAudioFocus(); | 429 delegate_->AbandonAudioFocus(); |
406 | 430 |
407 SetAudioFocusState(State::INACTIVE); | 431 SetAudioFocusState(State::INACTIVE); |
408 UpdateWebContents(); | 432 UpdateWebContents(); |
409 } | 433 } |
410 | 434 |
411 void MediaSession::UpdateWebContents() { | 435 void MediaSession::UpdateWebContents() { |
412 media_session_state_listeners_.Notify(audio_focus_state_); | 436 media_session_state_listeners_.Notify(audio_focus_state_); |
413 static_cast<WebContentsImpl*>(web_contents())->OnMediaSessionStateChanged(); | 437 for (auto& observer : observers_) |
| 438 observer.MediaSessionStateChanged(IsControllable(), IsSuspended()); |
414 } | 439 } |
415 | 440 |
416 void MediaSession::SetAudioFocusState(State audio_focus_state) { | 441 void MediaSession::SetAudioFocusState(State audio_focus_state) { |
417 if (audio_focus_state == audio_focus_state_) | 442 if (audio_focus_state == audio_focus_state_) |
418 return; | 443 return; |
419 | 444 |
420 audio_focus_state_ = audio_focus_state; | 445 audio_focus_state_ = audio_focus_state; |
421 switch (audio_focus_state_) { | 446 switch (audio_focus_state_) { |
422 case State::ACTIVE: | 447 case State::ACTIVE: |
423 uma_helper_.OnSessionActive(); | 448 uma_helper_.OnSessionActive(); |
(...skipping 14 matching lines...) Expand all Loading... |
438 DCHECK(success); | 463 DCHECK(success); |
439 | 464 |
440 pepper_players_.insert(PlayerIdentifier(observer, player_id)); | 465 pepper_players_.insert(PlayerIdentifier(observer, player_id)); |
441 | 466 |
442 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); | 467 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); |
443 | 468 |
444 return true; | 469 return true; |
445 } | 470 } |
446 | 471 |
447 } // namespace content | 472 } // namespace content |
OLD | NEW |