OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/capture/audio_mirroring_manager.h" | 5 #include "content/browser/media/capture/audio_mirroring_manager.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
55 InitiateQueriesToFindNewDestination(NULL, candidates); | 55 InitiateQueriesToFindNewDestination(NULL, candidates); |
56 } | 56 } |
57 | 57 |
58 void AudioMirroringManager::RemoveDiverter(Diverter* diverter) { | 58 void AudioMirroringManager::RemoveDiverter(Diverter* diverter) { |
59 DCHECK(thread_checker_.CalledOnValidThread()); | 59 DCHECK(thread_checker_.CalledOnValidThread()); |
60 | 60 |
61 // Find and remove the entry from the routing table. If the stream is being | 61 // Find and remove the entry from the routing table. If the stream is being |
62 // diverted, it is stopped. | 62 // diverted, it is stopped. |
63 for (StreamRoutes::iterator it = routes_.begin(); it != routes_.end(); ++it) { | 63 for (StreamRoutes::iterator it = routes_.begin(); it != routes_.end(); ++it) { |
64 if (it->diverter == diverter) { | 64 if (it->diverter == diverter) { |
65 ChangeRoute(&(*it), NULL); | 65 ChangeRoute(&(*it), NULL); |
miu
2016/05/06 22:29:49
Also need to call it->diverter->StopDuplicating()
qiangchen
2016/05/10 22:36:52
Done.
| |
66 routes_.erase(it); | 66 routes_.erase(it); |
67 return; | 67 return; |
68 } | 68 } |
69 } | 69 } |
70 NOTREACHED(); | 70 NOTREACHED(); |
71 } | 71 } |
72 | 72 |
73 void AudioMirroringManager::StartMirroring(MirroringDestination* destination) { | 73 void AudioMirroringManager::StartMirroring(MirroringDestination* destination) { |
74 DCHECK(thread_checker_.CalledOnValidThread()); | 74 DCHECK(thread_checker_.CalledOnValidThread()); |
75 DCHECK(destination); | 75 DCHECK(destination); |
76 | 76 |
77 // Insert an entry into the set of active mirroring sessions, if this is a | 77 // Insert an entry into the set of active mirroring sessions, if this is a |
78 // previously-unknown destination. | 78 // previously-unknown destination. |
79 if (std::find(sessions_.begin(), sessions_.end(), destination) == | 79 if (std::find(sessions_.begin(), sessions_.end(), destination) == |
80 sessions_.end()) { | 80 sessions_.end()) { |
81 sessions_.push_back(destination); | 81 sessions_.push_back(destination); |
82 } | 82 } |
83 | 83 |
84 std::set<SourceFrameRef> candidates; | |
85 | |
84 // Query the MirroringDestination to see which of the audio streams should be | 86 // Query the MirroringDestination to see which of the audio streams should be |
85 // diverted. | 87 // diverted. |
86 std::set<SourceFrameRef> candidates; | |
87 for (StreamRoutes::const_iterator it = routes_.begin(); it != routes_.end(); | 88 for (StreamRoutes::const_iterator it = routes_.begin(); it != routes_.end(); |
88 ++it) { | 89 ++it) { |
89 if (!it->destination || it->destination == destination) | |
90 candidates.insert(it->source_render_frame); | 90 candidates.insert(it->source_render_frame); |
91 } | 91 } |
92 if (!candidates.empty()) { | 92 if (!candidates.empty()) { |
93 destination->QueryForMatches( | 93 destination->QueryForMatches( |
94 candidates, | 94 candidates, |
95 base::Bind(&AudioMirroringManager::UpdateRoutesToDestination, | 95 base::Bind(&AudioMirroringManager::UpdateRoutesToDestination, |
96 base::Unretained(this), | 96 base::Unretained(this), |
97 destination, | 97 destination, |
98 false)); | 98 false)); |
99 } | 99 } |
100 } | 100 } |
101 | 101 |
102 void AudioMirroringManager::StopMirroring(MirroringDestination* destination) { | 102 void AudioMirroringManager::StopMirroring(MirroringDestination* destination) { |
103 DCHECK(thread_checker_.CalledOnValidThread()); | 103 DCHECK(thread_checker_.CalledOnValidThread()); |
104 | 104 |
105 // Stop diverting each audio stream in the mirroring session being stopped. | 105 // Stop diverting each audio stream in the mirroring session being stopped. |
106 // Each stopped stream becomes a candidate to be diverted to another | 106 // Each stopped stream becomes a candidate to be diverted to another |
107 // destination. | 107 // destination. |
108 std::set<SourceFrameRef> redivert_candidates; | 108 std::set<SourceFrameRef> redivert_candidates; |
109 for (StreamRoutes::iterator it = routes_.begin(); it != routes_.end(); ++it) { | 109 for (StreamRoutes::iterator it = routes_.begin(); it != routes_.end(); ++it) { |
110 if (it->destination == destination) { | 110 if (it->destination == destination) { |
111 ChangeRoute(&(*it), NULL); | 111 ChangeRoute(&(*it), NULL); |
112 redivert_candidates.insert(it->source_render_frame); | 112 redivert_candidates.insert(it->source_render_frame); |
113 } | 113 } |
114 auto dup_it = it->duplications.find(destination); | |
115 if (dup_it != it->duplications.end()) { | |
116 it->diverter->StopDuplicating(dup_it->second); | |
117 it->duplications.erase(dup_it); | |
118 } | |
114 } | 119 } |
115 if (!redivert_candidates.empty()) | 120 if (!redivert_candidates.empty()) |
116 InitiateQueriesToFindNewDestination(destination, redivert_candidates); | 121 InitiateQueriesToFindNewDestination(destination, redivert_candidates); |
117 | 122 |
118 // Remove the entry from the set of active mirroring sessions. | 123 // Remove the entry from the set of active mirroring sessions. |
119 const Destinations::iterator dest_it = | 124 const Destinations::iterator dest_it = |
120 std::find(sessions_.begin(), sessions_.end(), destination); | 125 std::find(sessions_.begin(), sessions_.end(), destination); |
121 if (dest_it == sessions_.end()) { | 126 if (dest_it == sessions_.end()) { |
122 NOTREACHED(); | 127 NOTREACHED(); |
123 return; | 128 return; |
124 } | 129 } |
125 sessions_.erase(dest_it); | 130 sessions_.erase(dest_it); |
126 } | 131 } |
127 | 132 |
128 void AudioMirroringManager::InitiateQueriesToFindNewDestination( | 133 void AudioMirroringManager::InitiateQueriesToFindNewDestination( |
129 MirroringDestination* old_destination, | 134 MirroringDestination* old_destination, |
130 const std::set<SourceFrameRef>& candidates) { | 135 const std::set<SourceFrameRef>& candidates) { |
131 DCHECK(thread_checker_.CalledOnValidThread()); | 136 DCHECK(thread_checker_.CalledOnValidThread()); |
132 | 137 |
133 for (Destinations::const_iterator it = sessions_.begin(); | 138 for (Destinations::const_iterator it = sessions_.begin(); |
134 it != sessions_.end(); ++it) { | 139 it != sessions_.end(); ++it) { |
135 if (*it != old_destination) { | 140 if (*it == old_destination) |
136 (*it)->QueryForMatches( | 141 continue; |
137 candidates, | 142 |
138 base::Bind(&AudioMirroringManager::UpdateRoutesToDestination, | 143 (*it)->QueryForMatches( |
139 base::Unretained(this), | 144 candidates, |
140 *it, | 145 base::Bind(&AudioMirroringManager::UpdateRoutesToDestination, |
141 true)); | 146 base::Unretained(this), *it, true)); |
142 } | |
143 } | 147 } |
144 } | 148 } |
145 | 149 |
146 void AudioMirroringManager::UpdateRoutesToDestination( | 150 void AudioMirroringManager::UpdateRoutesToDestination( |
147 MirroringDestination* destination, | 151 MirroringDestination* destination, |
148 bool add_only, | 152 bool add_only, |
153 const std::set<SourceFrameRef>& matches, | |
154 bool is_duplicate) { | |
155 if (is_duplicate) | |
156 UpdateRoutesToDuplicateDestination(destination, add_only, matches); | |
157 else | |
158 UpdateRoutesToDivertDestination(destination, add_only, matches); | |
159 } | |
160 | |
161 void AudioMirroringManager::UpdateRoutesToDivertDestination( | |
162 MirroringDestination* destination, | |
163 bool add_only, | |
149 const std::set<SourceFrameRef>& matches) { | 164 const std::set<SourceFrameRef>& matches) { |
150 DCHECK(thread_checker_.CalledOnValidThread()); | 165 DCHECK(thread_checker_.CalledOnValidThread()); |
151 | 166 |
152 if (std::find(sessions_.begin(), sessions_.end(), destination) == | 167 if (std::find(sessions_.begin(), sessions_.end(), destination) == |
153 sessions_.end()) { | 168 sessions_.end()) { |
154 return; // Query result callback invoked after StopMirroring(). | 169 return; // Query result callback invoked after StopMirroring(). |
155 } | 170 } |
156 | 171 |
157 DVLOG(1) << (add_only ? "Add " : "Replace with ") << matches.size() | 172 DVLOG(1) << (add_only ? "Add " : "Replace with ") << matches.size() |
158 << " routes to MirroringDestination@" << destination; | 173 << " routes to MirroringDestination@" << destination; |
(...skipping 11 matching lines...) Expand all Loading... | |
170 if (it->destination == destination) { | 185 if (it->destination == destination) { |
171 ChangeRoute(&(*it), NULL); | 186 ChangeRoute(&(*it), NULL); |
172 redivert_candidates.insert(it->source_render_frame); | 187 redivert_candidates.insert(it->source_render_frame); |
173 } | 188 } |
174 } | 189 } |
175 } | 190 } |
176 if (!redivert_candidates.empty()) | 191 if (!redivert_candidates.empty()) |
177 InitiateQueriesToFindNewDestination(destination, redivert_candidates); | 192 InitiateQueriesToFindNewDestination(destination, redivert_candidates); |
178 } | 193 } |
179 | 194 |
195 void AudioMirroringManager::UpdateRoutesToDuplicateDestination( | |
196 MirroringDestination* destination, | |
197 bool add_only, | |
198 const std::set<SourceFrameRef>& matches) { | |
199 DCHECK(thread_checker_.CalledOnValidThread()); | |
200 if (std::find(sessions_.begin(), sessions_.end(), destination) == | |
201 sessions_.end()) { | |
202 return; // Query result callback invoked after StopMirroring(). | |
203 } | |
204 | |
205 for (StreamRoutes::iterator it = routes_.begin(); it != routes_.end(); ++it) { | |
206 if (matches.find(it->source_render_frame) != matches.end()) { | |
207 media::AudioPushSink*& pusher = it->duplications[destination]; | |
miu
2016/05/06 22:29:49
Just before this line, please add a consistency ch
qiangchen
2016/05/10 22:36:52
Done.
| |
208 if (!pusher) { | |
209 pusher = destination->AddPushInput(it->diverter->GetAudioParameters()); | |
210 DCHECK(pusher); | |
211 it->diverter->StartDuplicating(pusher); | |
212 } | |
213 } else if (!add_only) { | |
214 auto dup_it = it->duplications.find(destination); | |
215 if (dup_it != it->duplications.end()) { | |
216 it->diverter->StopDuplicating(dup_it->second); | |
217 it->duplications.erase(dup_it); | |
218 } | |
219 } | |
220 } | |
221 } | |
222 | |
180 // static | 223 // static |
181 void AudioMirroringManager::ChangeRoute( | 224 void AudioMirroringManager::ChangeRoute( |
miu
2016/05/06 22:29:49
To aid readability, could you change the name of t
qiangchen
2016/05/10 22:36:52
Done.
| |
182 StreamRoutingState* route, MirroringDestination* new_destination) { | 225 StreamRoutingState* route, MirroringDestination* new_destination) { |
183 if (route->destination == new_destination) | 226 if (route->destination == new_destination) |
miu
2016/05/06 22:29:49
At the top of this method, let's make sure there i
qiangchen
2016/05/10 22:36:52
Done.
| |
184 return; // No change. | 227 return; // No change. |
185 | 228 |
186 if (route->destination) { | 229 if (route->destination) { |
187 DVLOG(1) << "Stop diverting render_process_id:render_frame_id=" | 230 DVLOG(1) << "Stop diverting render_process_id:render_frame_id=" |
188 << route->source_render_frame.first << ':' | 231 << route->source_render_frame.first << ':' |
189 << route->source_render_frame.second | 232 << route->source_render_frame.second |
190 << " --> MirroringDestination@" << route->destination; | 233 << " --> MirroringDestination@" << route->destination; |
191 route->diverter->StopDiverting(); | 234 route->diverter->StopDiverting(); |
192 route->destination = NULL; | 235 route->destination = NULL; |
193 } | 236 } |
(...skipping 14 matching lines...) Expand all Loading... | |
208 : source_render_frame(source_frame), | 251 : source_render_frame(source_frame), |
209 diverter(stream_diverter), | 252 diverter(stream_diverter), |
210 destination(NULL) {} | 253 destination(NULL) {} |
211 | 254 |
212 AudioMirroringManager::StreamRoutingState::StreamRoutingState( | 255 AudioMirroringManager::StreamRoutingState::StreamRoutingState( |
213 const StreamRoutingState& other) = default; | 256 const StreamRoutingState& other) = default; |
214 | 257 |
215 AudioMirroringManager::StreamRoutingState::~StreamRoutingState() {} | 258 AudioMirroringManager::StreamRoutingState::~StreamRoutingState() {} |
216 | 259 |
217 } // namespace content | 260 } // namespace content |
OLD | NEW |