Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: content/browser/media/session/media_session.cc

Issue 2439483003: Link MediaSessionTabHelper with native MediaSession [CL is going to be split] (Closed)
Patch Set: Super rough, please give some initial feedbacks Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
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);
60 } 61 }
61 62
63 void MediaSession::AddObserver(MediaSessionObserver* observer) {
64 LOG(INFO) << "MediaSession::AddObserver()";
65 observers_.AddObserver(observer);
66 }
67
68 void MediaSession::RemoveObserver(MediaSessionObserver* observer) {
69 LOG(INFO) << "MediaSession::RemoveObserver()";
70 observers_.RemoveObserver(observer);
71 owned_observers_.erase(observer);
72 }
73
74 void MediaSession::PassObserverOwnership(std::unique_ptr<MediaSessionObserver> o bserver) {
75 LOG(INFO) << "MediaSession::PassObserverOwnership()";
76 DCHECK(!owned_observers_.count(observer.get()));
77 owned_observers_[observer.get()] = std::move(observer);
78 }
79
62 void MediaSession::WebContentsDestroyed() { 80 void MediaSession::WebContentsDestroyed() {
63 // This should only work for tests. In production, all the players should have 81 // This should only work for tests. In production, all the players should have
64 // already been removed before WebContents is destroyed. 82 // already been removed before WebContents is destroyed.
65 83
66 // TODO(zqzhang): refactor MediaSession, maybe move the interface used to talk 84 // TODO(zqzhang): refactor MediaSession, maybe move the interface used to talk
67 // with AudioFocusManager out to a seperate class. The AudioFocusManager unit 85 // with AudioFocusManager out to a seperate class. The AudioFocusManager unit
68 // tests then could mock the interface and abandon audio focus when 86 // tests then could mock the interface and abandon audio focus when
69 // WebContents is destroyed. See https://crbug.com/651069 87 // WebContents is destroyed. See https://crbug.com/651069
70 players_.clear(); 88 players_.clear();
71 pepper_players_.clear(); 89 pepper_players_.clear();
72 AbandonSystemAudioFocusIfNeeded(); 90 AbandonSystemAudioFocusIfNeeded();
73 } 91 }
74 92
75 void MediaSession::SetMetadata(const base::Optional<MediaMetadata>& metadata) { 93 void MediaSession::SetMetadata(const base::Optional<MediaMetadata>& metadata) {
76 metadata_ = metadata; 94 metadata_ = metadata;
77 static_cast<WebContentsImpl*>(web_contents()) 95
78 ->OnMediaSessionMetadataChanged(); 96 for (auto& observer : observers_)
97 observer.MediaSessionMetadataChanged(metadata_);
79 } 98 }
80 99
81 bool MediaSession::AddPlayer(MediaSessionPlayerObserver* observer, 100 bool MediaSession::AddPlayer(MediaSessionPlayerObserver* observer,
82 int player_id, 101 int player_id,
83 media::MediaContentType media_content_type) { 102 media::MediaContentType media_content_type) {
84 if (media_content_type == media::MediaContentType::Pepper) 103 if (media_content_type == media::MediaContentType::Pepper)
85 return AddPepperPlayer(observer, player_id); 104 return AddPepperPlayer(observer, player_id);
86 105
87 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); 106 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier());
88 107
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 !pepper_players_.empty()) { 415 !pepper_players_.empty()) {
397 return; 416 return;
398 } 417 }
399 delegate_->AbandonAudioFocus(); 418 delegate_->AbandonAudioFocus();
400 419
401 SetAudioFocusState(State::INACTIVE); 420 SetAudioFocusState(State::INACTIVE);
402 UpdateWebContents(); 421 UpdateWebContents();
403 } 422 }
404 423
405 void MediaSession::UpdateWebContents() { 424 void MediaSession::UpdateWebContents() {
425 // TODO in this CL: make media_session_state_listeners inherit from MediaSessi onObserver.
406 media_session_state_listeners_.Notify(audio_focus_state_); 426 media_session_state_listeners_.Notify(audio_focus_state_);
407 static_cast<WebContentsImpl*>(web_contents())->OnMediaSessionStateChanged(); 427 for (auto& observer : observers_)
428 observer.MediaSessionStateChanged(IsControllable(), IsSuspended());
408 } 429 }
409 430
410 void MediaSession::SetAudioFocusState(State audio_focus_state) { 431 void MediaSession::SetAudioFocusState(State audio_focus_state) {
411 if (audio_focus_state == audio_focus_state_) 432 if (audio_focus_state == audio_focus_state_)
412 return; 433 return;
413 434
414 audio_focus_state_ = audio_focus_state; 435 audio_focus_state_ = audio_focus_state;
415 switch (audio_focus_state_) { 436 switch (audio_focus_state_) {
416 case State::ACTIVE: 437 case State::ACTIVE:
417 uma_helper_.OnSessionActive(); 438 uma_helper_.OnSessionActive();
(...skipping 14 matching lines...) Expand all
432 DCHECK(success); 453 DCHECK(success);
433 454
434 pepper_players_.insert(PlayerIdentifier(observer, player_id)); 455 pepper_players_.insert(PlayerIdentifier(observer, player_id));
435 456
436 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier()); 457 observer->OnSetVolumeMultiplier(player_id, GetVolumeMultiplier());
437 458
438 return true; 459 return true;
439 } 460 }
440 461
441 } // namespace content 462 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698