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 |
14 namespace content { | 15 namespace content { |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 CreateForWebContents(web_contents); | 51 CreateForWebContents(web_contents); |
51 session = FromWebContents(web_contents); | 52 session = FromWebContents(web_contents); |
52 session->Initialize(); | 53 session->Initialize(); |
53 } | 54 } |
54 return session; | 55 return session; |
55 } | 56 } |
56 | 57 |
57 MediaSession::~MediaSession() { | 58 MediaSession::~MediaSession() { |
58 DCHECK(players_.empty()); | 59 DCHECK(players_.empty()); |
59 DCHECK(audio_focus_state_ == State::INACTIVE); | 60 DCHECK(audio_focus_state_ == State::INACTIVE); |
| 61 owned_observers_.clear(); |
| 62 } |
| 63 |
| 64 void MediaSession::AddObserver(MediaSessionObserver* observer) { |
| 65 observers_.AddObserver(observer); |
| 66 } |
| 67 |
| 68 void MediaSession::RemoveObserver(MediaSessionObserver* observer) { |
| 69 observers_.RemoveObserver(observer); |
| 70 for (auto iter = owned_observers_.begin(); iter != owned_observers_.end(); |
| 71 ++iter) { |
| 72 if (iter->get() == observer) |
| 73 owned_observers_.erase(iter); |
| 74 return; |
| 75 } |
| 76 } |
| 77 |
| 78 void MediaSession::PassObserverOwnership( |
| 79 std::unique_ptr<MediaSessionObserver> observer) { |
| 80 owned_observers_.push_back(std::move(observer)); |
60 } | 81 } |
61 | 82 |
62 void MediaSession::WebContentsDestroyed() { | 83 void MediaSession::WebContentsDestroyed() { |
63 // This should only work for tests. In production, all the players should have | 84 // This should only work for tests. In production, all the players should have |
64 // already been removed before WebContents is destroyed. | 85 // already been removed before WebContents is destroyed. |
65 | 86 |
66 // TODO(zqzhang): refactor MediaSession, maybe move the interface used to talk | 87 // TODO(zqzhang): refactor MediaSession, maybe move the interface used to talk |
67 // with AudioFocusManager out to a seperate class. The AudioFocusManager unit | 88 // with AudioFocusManager out to a seperate class. The AudioFocusManager unit |
68 // tests then could mock the interface and abandon audio focus when | 89 // tests then could mock the interface and abandon audio focus when |
69 // WebContents is destroyed. See https://crbug.com/651069 | 90 // WebContents is destroyed. See https://crbug.com/651069 |
70 players_.clear(); | 91 players_.clear(); |
71 pepper_players_.clear(); | 92 pepper_players_.clear(); |
72 AbandonSystemAudioFocusIfNeeded(); | 93 AbandonSystemAudioFocusIfNeeded(); |
73 } | 94 } |
74 | 95 |
75 void MediaSession::SetMetadata(const base::Optional<MediaMetadata>& metadata) { | 96 void MediaSession::SetMetadata(const base::Optional<MediaMetadata>& metadata) { |
76 metadata_ = metadata; | 97 metadata_ = metadata; |
77 static_cast<WebContentsImpl*>(web_contents()) | 98 |
78 ->OnMediaSessionMetadataChanged(); | 99 for (auto& observer : observers_) |
| 100 observer.MediaSessionMetadataChanged(metadata_); |
79 } | 101 } |
80 | 102 |
81 bool MediaSession::AddPlayer(MediaSessionPlayerObserver* observer, | 103 bool MediaSession::AddPlayer(MediaSessionPlayerObserver* observer, |
82 int player_id, | 104 int player_id, |
83 media::MediaContentType media_content_type) { | 105 media::MediaContentType media_content_type) { |
84 if (media_content_type == media::MediaContentType::Pepper) | 106 if (media_content_type == media::MediaContentType::Pepper) |
85 return AddPepperPlayer(observer, player_id); | 107 return AddPepperPlayer(observer, player_id); |
86 | 108 |
87 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); | 109 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); |
88 | 110 |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 return; | 423 return; |
402 } | 424 } |
403 delegate_->AbandonAudioFocus(); | 425 delegate_->AbandonAudioFocus(); |
404 | 426 |
405 SetAudioFocusState(State::INACTIVE); | 427 SetAudioFocusState(State::INACTIVE); |
406 UpdateWebContents(); | 428 UpdateWebContents(); |
407 } | 429 } |
408 | 430 |
409 void MediaSession::UpdateWebContents() { | 431 void MediaSession::UpdateWebContents() { |
410 media_session_state_listeners_.Notify(audio_focus_state_); | 432 media_session_state_listeners_.Notify(audio_focus_state_); |
411 static_cast<WebContentsImpl*>(web_contents())->OnMediaSessionStateChanged(); | 433 for (auto& observer : observers_) |
| 434 observer.MediaSessionStateChanged(IsControllable(), IsSuspended()); |
412 } | 435 } |
413 | 436 |
414 void MediaSession::SetAudioFocusState(State audio_focus_state) { | 437 void MediaSession::SetAudioFocusState(State audio_focus_state) { |
415 if (audio_focus_state == audio_focus_state_) | 438 if (audio_focus_state == audio_focus_state_) |
416 return; | 439 return; |
417 | 440 |
418 audio_focus_state_ = audio_focus_state; | 441 audio_focus_state_ = audio_focus_state; |
419 switch (audio_focus_state_) { | 442 switch (audio_focus_state_) { |
420 case State::ACTIVE: | 443 case State::ACTIVE: |
421 uma_helper_.OnSessionActive(); | 444 uma_helper_.OnSessionActive(); |
(...skipping 14 matching lines...) Expand all Loading... |
436 DCHECK(success); | 459 DCHECK(success); |
437 | 460 |
438 pepper_players_.insert(PlayerIdentifier(observer, player_id)); | 461 pepper_players_.insert(PlayerIdentifier(observer, player_id)); |
439 | 462 |
440 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); | 463 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); |
441 | 464 |
442 return true; | 465 return true; |
443 } | 466 } |
444 | 467 |
445 } // namespace content | 468 } // namespace content |
OLD | NEW |