1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/media/capture/audio_mirroring_manager.h"
7 #include "base/lazy_instance.h"
13 base::LazyInstance
<AudioMirroringManager
>::Leaky g_audio_mirroring_manager
=
14 LAZY_INSTANCE_INITIALIZER
;
19 AudioMirroringManager
* AudioMirroringManager::GetInstance() {
20 return g_audio_mirroring_manager
.Pointer();
23 AudioMirroringManager::AudioMirroringManager() {
24 // Only *after* construction, check that AudioMirroringManager is being
25 // invoked on the same single thread.
26 thread_checker_
.DetachFromThread();
29 AudioMirroringManager::~AudioMirroringManager() {}
31 void AudioMirroringManager::AddDiverter(
32 int render_process_id
, int render_view_id
, Diverter
* diverter
) {
33 DCHECK(thread_checker_
.CalledOnValidThread());
36 // DCHECK(diverter not already in diverters_ under any key)
38 for (DiverterMap::const_iterator it
= diverters_
.begin();
39 it
!= diverters_
.end(); ++it
) {
40 DCHECK_NE(diverter
, it
->second
);
44 // Add the diverter to the set of active diverters.
45 const Target
target(render_process_id
, render_view_id
);
46 diverters_
.insert(std::make_pair(target
, diverter
));
48 // If a mirroring session is active, start diverting the audio stream
50 SessionMap::iterator session_it
= sessions_
.find(target
);
51 if (session_it
!= sessions_
.end()) {
52 diverter
->StartDiverting(
53 session_it
->second
->AddInput(diverter
->GetAudioParameters()));
57 void AudioMirroringManager::RemoveDiverter(
58 int render_process_id
, int render_view_id
, Diverter
* diverter
) {
59 DCHECK(thread_checker_
.CalledOnValidThread());
61 // Stop diverting the audio stream if a mirroring session is active.
62 const Target
target(render_process_id
, render_view_id
);
63 SessionMap::iterator session_it
= sessions_
.find(target
);
64 if (session_it
!= sessions_
.end())
65 diverter
->StopDiverting();
67 // Remove the diverter from the set of active diverters.
68 for (DiverterMap::iterator it
= diverters_
.lower_bound(target
);
69 it
!= diverters_
.end() && it
->first
== target
; ++it
) {
70 if (it
->second
== diverter
) {
77 void AudioMirroringManager::StartMirroring(
78 int render_process_id
, int render_view_id
,
79 MirroringDestination
* destination
) {
80 DCHECK(thread_checker_
.CalledOnValidThread());
83 // Insert an entry into the set of active mirroring sessions. If a mirroring
84 // session is already active for |render_process_id| + |render_view_id|,
86 const Target
target(render_process_id
, render_view_id
);
87 SessionMap::iterator session_it
= sessions_
.find(target
);
88 MirroringDestination
* old_destination
;
89 if (session_it
== sessions_
.end()) {
90 old_destination
= NULL
;
91 sessions_
.insert(std::make_pair(target
, destination
));
93 DVLOG(1) << "Start mirroring render_process_id:render_view_id="
94 << render_process_id
<< ':' << render_view_id
95 << " --> MirroringDestination@" << destination
;
97 old_destination
= session_it
->second
;
98 session_it
->second
= destination
;
100 DVLOG(1) << "Switch mirroring of render_process_id:render_view_id="
101 << render_process_id
<< ':' << render_view_id
102 << " MirroringDestination@" << old_destination
103 << " --> MirroringDestination@" << destination
;
106 // Divert audio streams coming from |target| to |destination|. If streams
107 // were already diverted to the |old_destination|, remove them.
108 for (DiverterMap::iterator it
= diverters_
.lower_bound(target
);
109 it
!= diverters_
.end() && it
->first
== target
; ++it
) {
110 Diverter
* const diverter
= it
->second
;
112 diverter
->StopDiverting();
113 diverter
->StartDiverting(
114 destination
->AddInput(diverter
->GetAudioParameters()));
118 void AudioMirroringManager::StopMirroring(
119 int render_process_id
, int render_view_id
,
120 MirroringDestination
* destination
) {
121 DCHECK(thread_checker_
.CalledOnValidThread());
123 // Stop mirroring if there is an active session *and* the destination
125 const Target
target(render_process_id
, render_view_id
);
126 SessionMap::iterator session_it
= sessions_
.find(target
);
127 if (session_it
== sessions_
.end() || destination
!= session_it
->second
)
130 DVLOG(1) << "Stop mirroring render_process_id:render_view_id="
131 << render_process_id
<< ':' << render_view_id
132 << " --> MirroringDestination@" << destination
;
134 // Stop diverting each audio stream in the mirroring session being stopped.
135 for (DiverterMap::iterator it
= diverters_
.lower_bound(target
);
136 it
!= diverters_
.end() && it
->first
== target
; ++it
) {
137 it
->second
->StopDiverting();
140 // Remove the entry from the set of active mirroring sessions.
141 sessions_
.erase(session_it
);
144 } // namespace content