| 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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 if (destination->IsDuplication()) { |
| 86 for (StreamRoutes::const_iterator it = routes_.begin(); it != routes_.end(); |
| 87 ++it) { |
| 88 if (it->dup_destinations.count(destination) == 0) |
| 89 candidates.insert(it->source_render_frame); |
| 90 } |
| 91 if (!candidates.empty()) { |
| 92 destination->QueryForMatches( |
| 93 candidates, |
| 94 base::Bind(&AudioMirroringManager::UpdateRoutesToDupDestination, |
| 95 base::Unretained(this), destination, false)); |
| 96 } |
| 97 return; |
| 98 } |
| 99 |
| 84 // Query the MirroringDestination to see which of the audio streams should be | 100 // Query the MirroringDestination to see which of the audio streams should be |
| 85 // diverted. | 101 // diverted. |
| 86 std::set<SourceFrameRef> candidates; | |
| 87 for (StreamRoutes::const_iterator it = routes_.begin(); it != routes_.end(); | 102 for (StreamRoutes::const_iterator it = routes_.begin(); it != routes_.end(); |
| 88 ++it) { | 103 ++it) { |
| 89 if (!it->destination || it->destination == destination) | 104 if (!it->destination || it->destination == destination) |
| 90 candidates.insert(it->source_render_frame); | 105 candidates.insert(it->source_render_frame); |
| 91 } | 106 } |
| 92 if (!candidates.empty()) { | 107 if (!candidates.empty()) { |
| 93 destination->QueryForMatches( | 108 destination->QueryForMatches( |
| 94 candidates, | 109 candidates, |
| 95 base::Bind(&AudioMirroringManager::UpdateRoutesToDestination, | 110 base::Bind(&AudioMirroringManager::UpdateRoutesToDestination, |
| 96 base::Unretained(this), | 111 base::Unretained(this), |
| 97 destination, | 112 destination, |
| 98 false)); | 113 false)); |
| 99 } | 114 } |
| 100 } | 115 } |
| 101 | 116 |
| 102 void AudioMirroringManager::StopMirroring(MirroringDestination* destination) { | 117 void AudioMirroringManager::StopMirroring(MirroringDestination* destination) { |
| 103 DCHECK(thread_checker_.CalledOnValidThread()); | 118 DCHECK(thread_checker_.CalledOnValidThread()); |
| 104 | 119 |
| 105 // Stop diverting each audio stream in the mirroring session being stopped. | 120 if (destination->IsDuplication()) { |
| 106 // Each stopped stream becomes a candidate to be diverted to another | 121 std::set<SourceFrameRef> matches; |
| 107 // destination. | 122 UpdateRoutesToDupDestination(destination, false, matches); |
| 108 std::set<SourceFrameRef> redivert_candidates; | 123 } else { |
| 109 for (StreamRoutes::iterator it = routes_.begin(); it != routes_.end(); ++it) { | 124 // Stop diverting each audio stream in the mirroring session being stopped. |
| 110 if (it->destination == destination) { | 125 // Each stopped stream becomes a candidate to be diverted to another |
| 111 ChangeRoute(&(*it), NULL); | 126 // destination. |
| 112 redivert_candidates.insert(it->source_render_frame); | 127 std::set<SourceFrameRef> redivert_candidates; |
| 128 for (StreamRoutes::iterator it = routes_.begin(); it != routes_.end(); |
| 129 ++it) { |
| 130 if (it->destination == destination) { |
| 131 ChangeRoute(&(*it), NULL); |
| 132 redivert_candidates.insert(it->source_render_frame); |
| 133 } |
| 113 } | 134 } |
| 135 if (!redivert_candidates.empty()) |
| 136 InitiateQueriesToFindNewDestination(destination, redivert_candidates); |
| 114 } | 137 } |
| 115 if (!redivert_candidates.empty()) | |
| 116 InitiateQueriesToFindNewDestination(destination, redivert_candidates); | |
| 117 | 138 |
| 118 // Remove the entry from the set of active mirroring sessions. | 139 // Remove the entry from the set of active mirroring sessions. |
| 119 const Destinations::iterator dest_it = | 140 const Destinations::iterator dest_it = |
| 120 std::find(sessions_.begin(), sessions_.end(), destination); | 141 std::find(sessions_.begin(), sessions_.end(), destination); |
| 121 if (dest_it == sessions_.end()) { | 142 if (dest_it == sessions_.end()) { |
| 122 NOTREACHED(); | 143 NOTREACHED(); |
| 123 return; | 144 return; |
| 124 } | 145 } |
| 125 sessions_.erase(dest_it); | 146 sessions_.erase(dest_it); |
| 126 } | 147 } |
| 127 | 148 |
| 128 void AudioMirroringManager::InitiateQueriesToFindNewDestination( | 149 void AudioMirroringManager::InitiateQueriesToFindNewDestination( |
| 129 MirroringDestination* old_destination, | 150 MirroringDestination* old_destination, |
| 130 const std::set<SourceFrameRef>& candidates) { | 151 const std::set<SourceFrameRef>& candidates) { |
| 131 DCHECK(thread_checker_.CalledOnValidThread()); | 152 DCHECK(thread_checker_.CalledOnValidThread()); |
| 132 | 153 |
| 133 for (Destinations::const_iterator it = sessions_.begin(); | 154 for (Destinations::const_iterator it = sessions_.begin(); |
| 134 it != sessions_.end(); ++it) { | 155 it != sessions_.end(); ++it) { |
| 135 if (*it != old_destination) { | 156 if (*it == old_destination) |
| 157 continue; |
| 158 if ((*it)->IsDuplication()) { |
| 159 (*it)->QueryForMatches( |
| 160 candidates, |
| 161 base::Bind(&AudioMirroringManager::UpdateRoutesToDupDestination, |
| 162 base::Unretained(this), *it, true)); |
| 163 } else { |
| 136 (*it)->QueryForMatches( | 164 (*it)->QueryForMatches( |
| 137 candidates, | 165 candidates, |
| 138 base::Bind(&AudioMirroringManager::UpdateRoutesToDestination, | 166 base::Bind(&AudioMirroringManager::UpdateRoutesToDestination, |
| 139 base::Unretained(this), | 167 base::Unretained(this), |
| 140 *it, | 168 *it, |
| 141 true)); | 169 true)); |
| 142 } | 170 } |
| 143 } | 171 } |
| 144 } | 172 } |
| 145 | 173 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 170 if (it->destination == destination) { | 198 if (it->destination == destination) { |
| 171 ChangeRoute(&(*it), NULL); | 199 ChangeRoute(&(*it), NULL); |
| 172 redivert_candidates.insert(it->source_render_frame); | 200 redivert_candidates.insert(it->source_render_frame); |
| 173 } | 201 } |
| 174 } | 202 } |
| 175 } | 203 } |
| 176 if (!redivert_candidates.empty()) | 204 if (!redivert_candidates.empty()) |
| 177 InitiateQueriesToFindNewDestination(destination, redivert_candidates); | 205 InitiateQueriesToFindNewDestination(destination, redivert_candidates); |
| 178 } | 206 } |
| 179 | 207 |
| 208 void AudioMirroringManager::UpdateRoutesToDupDestination( |
| 209 MirroringDestination* destination, |
| 210 bool add_only, |
| 211 const std::set<SourceFrameRef>& matches) { |
| 212 DCHECK(thread_checker_.CalledOnValidThread()); |
| 213 |
| 214 if (std::find(sessions_.begin(), sessions_.end(), destination) == |
| 215 sessions_.end()) { |
| 216 return; // Query result callback invoked after StopMirroring(). |
| 217 } |
| 218 |
| 219 for (StreamRoutes::iterator it = routes_.begin(); it != routes_.end(); ++it) { |
| 220 if (matches.find(it->source_render_frame) != matches.end()) { |
| 221 if (it->dup_destinations.count(destination) == 0) { |
| 222 it->dup_destinations[destination] = |
| 223 destination->AddPushInput(it->diverter->GetAudioParameters()); |
| 224 it->diverter->StartDuplicating(it->dup_destinations[destination]); |
| 225 } |
| 226 |
| 227 } else if (!add_only) { |
| 228 if (it->dup_destinations.count(destination) > 0) { |
| 229 it->diverter->StopDuplicating(it->dup_destinations[destination]); |
| 230 it->dup_destinations.erase(destination); |
| 231 } |
| 232 } |
| 233 } |
| 234 } |
| 235 |
| 180 // static | 236 // static |
| 181 void AudioMirroringManager::ChangeRoute( | 237 void AudioMirroringManager::ChangeRoute( |
| 182 StreamRoutingState* route, MirroringDestination* new_destination) { | 238 StreamRoutingState* route, MirroringDestination* new_destination) { |
| 183 if (route->destination == new_destination) | 239 if (route->destination == new_destination) |
| 184 return; // No change. | 240 return; // No change. |
| 185 | 241 |
| 186 if (route->destination) { | 242 if (route->destination) { |
| 187 DVLOG(1) << "Stop diverting render_process_id:render_frame_id=" | 243 DVLOG(1) << "Stop diverting render_process_id:render_frame_id=" |
| 188 << route->source_render_frame.first << ':' | 244 << route->source_render_frame.first << ':' |
| 189 << route->source_render_frame.second | 245 << route->source_render_frame.second |
| (...skipping 18 matching lines...) Expand all Loading... |
| 208 : source_render_frame(source_frame), | 264 : source_render_frame(source_frame), |
| 209 diverter(stream_diverter), | 265 diverter(stream_diverter), |
| 210 destination(NULL) {} | 266 destination(NULL) {} |
| 211 | 267 |
| 212 AudioMirroringManager::StreamRoutingState::StreamRoutingState( | 268 AudioMirroringManager::StreamRoutingState::StreamRoutingState( |
| 213 const StreamRoutingState& other) = default; | 269 const StreamRoutingState& other) = default; |
| 214 | 270 |
| 215 AudioMirroringManager::StreamRoutingState::~StreamRoutingState() {} | 271 AudioMirroringManager::StreamRoutingState::~StreamRoutingState() {} |
| 216 | 272 |
| 217 } // namespace content | 273 } // namespace content |
| OLD | NEW |