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(); | |
whywhat
2016/10/21 18:28:14
I think MediaSession should notify MediaSessionObs
derekjchow1
2016/10/21 20:47:30
+1. This is the pattern used in WebContentsObserve
| |
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(); | |
derekjchow1
2016/10/21 20:47:30
You could use std::find here.
| |
71 ++iter) { | |
72 if (iter->get() == observer) | |
73 owned_observers_.erase(iter); | |
74 return; | |
whywhat
2016/10/21 18:28:14
this return belongs to the if above
| |
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::Uncontrollable) | 106 if (media_content_type == media::MediaContentType::Uncontrollable) |
85 return true; | 107 return true; |
86 if (media_content_type == media::MediaContentType::Pepper) | 108 if (media_content_type == media::MediaContentType::Pepper) |
87 return AddPepperPlayer(observer, player_id); | 109 return AddPepperPlayer(observer, player_id); |
88 | 110 |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
403 return; | 425 return; |
404 } | 426 } |
405 delegate_->AbandonAudioFocus(); | 427 delegate_->AbandonAudioFocus(); |
406 | 428 |
407 SetAudioFocusState(State::INACTIVE); | 429 SetAudioFocusState(State::INACTIVE); |
408 UpdateWebContents(); | 430 UpdateWebContents(); |
409 } | 431 } |
410 | 432 |
411 void MediaSession::UpdateWebContents() { | 433 void MediaSession::UpdateWebContents() { |
412 media_session_state_listeners_.Notify(audio_focus_state_); | 434 media_session_state_listeners_.Notify(audio_focus_state_); |
413 static_cast<WebContentsImpl*>(web_contents())->OnMediaSessionStateChanged(); | 435 for (auto& observer : observers_) |
436 observer.MediaSessionStateChanged(IsControllable(), IsSuspended()); | |
414 } | 437 } |
415 | 438 |
416 void MediaSession::SetAudioFocusState(State audio_focus_state) { | 439 void MediaSession::SetAudioFocusState(State audio_focus_state) { |
417 if (audio_focus_state == audio_focus_state_) | 440 if (audio_focus_state == audio_focus_state_) |
418 return; | 441 return; |
419 | 442 |
420 audio_focus_state_ = audio_focus_state; | 443 audio_focus_state_ = audio_focus_state; |
421 switch (audio_focus_state_) { | 444 switch (audio_focus_state_) { |
422 case State::ACTIVE: | 445 case State::ACTIVE: |
423 uma_helper_.OnSessionActive(); | 446 uma_helper_.OnSessionActive(); |
(...skipping 14 matching lines...) Expand all Loading... | |
438 DCHECK(success); | 461 DCHECK(success); |
439 | 462 |
440 pepper_players_.insert(PlayerIdentifier(observer, player_id)); | 463 pepper_players_.insert(PlayerIdentifier(observer, player_id)); |
441 | 464 |
442 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); | 465 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); |
443 | 466 |
444 return true; | 467 return true; |
445 } | 468 } |
446 | 469 |
447 } // namespace content | 470 } // namespace content |
OLD | NEW |