OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/p2p/base/transportcontroller.h" | 11 #include "webrtc/p2p/base/transportcontroller.h" |
12 | 12 |
13 #include <algorithm> | |
14 | |
13 #include "webrtc/base/bind.h" | 15 #include "webrtc/base/bind.h" |
14 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
15 #include "webrtc/base/thread.h" | 17 #include "webrtc/base/thread.h" |
16 #include "webrtc/p2p/base/dtlstransport.h" | 18 #include "webrtc/p2p/base/dtlstransport.h" |
17 #include "webrtc/p2p/base/p2ptransport.h" | 19 #include "webrtc/p2p/base/p2ptransport.h" |
20 #include "webrtc/p2p/base/port.h" | |
18 | 21 |
19 namespace cricket { | 22 namespace cricket { |
20 | 23 |
21 enum { | 24 enum { |
22 MSG_ICECONNECTIONSTATE, | 25 MSG_ICECONNECTIONSTATE, |
23 MSG_RECEIVING, | 26 MSG_RECEIVING, |
24 MSG_ICEGATHERINGSTATE, | 27 MSG_ICEGATHERINGSTATE, |
25 MSG_CANDIDATESGATHERED, | 28 MSG_CANDIDATESGATHERED, |
26 }; | 29 }; |
27 | 30 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 return worker_thread_->Invoke<bool>( | 137 return worker_thread_->Invoke<bool>( |
135 rtc::Bind(&TransportController::GetStats_w, this, transport_name, stats)); | 138 rtc::Bind(&TransportController::GetStats_w, this, transport_name, stats)); |
136 } | 139 } |
137 | 140 |
138 TransportChannel* TransportController::CreateTransportChannel_w( | 141 TransportChannel* TransportController::CreateTransportChannel_w( |
139 const std::string& transport_name, | 142 const std::string& transport_name, |
140 int component) { | 143 int component) { |
141 RTC_DCHECK(worker_thread_->IsCurrent()); | 144 RTC_DCHECK(worker_thread_->IsCurrent()); |
142 | 145 |
143 Transport* transport = GetOrCreateTransport_w(transport_name); | 146 Transport* transport = GetOrCreateTransport_w(transport_name); |
144 return transport->CreateChannel(component); | 147 bool new_channel = !transport->HasChannel(component); |
148 TransportChannelImpl* channel = transport->CreateChannel(component); | |
pthatcher1
2015/09/29 18:12:43
Would it make sense to move the ref-counting logic
Taylor Brandstetter
2015/09/29 23:21:02
Done.
| |
149 if (new_channel) { | |
150 AddChannel_w(channel); | |
151 // Adding a channel could cause aggregate state to change. | |
152 UpdateAggregateStates_w(); | |
153 } | |
154 return channel; | |
145 } | 155 } |
146 | 156 |
147 void TransportController::DestroyTransportChannel_w( | 157 void TransportController::DestroyTransportChannel_w( |
148 const std::string& transport_name, | 158 const std::string& transport_name, |
149 int component) { | 159 int component) { |
150 RTC_DCHECK(worker_thread_->IsCurrent()); | 160 RTC_DCHECK(worker_thread_->IsCurrent()); |
151 | 161 |
152 Transport* transport = GetTransport_w(transport_name); | 162 Transport* transport = GetTransport_w(transport_name); |
153 if (!transport) { | 163 if (!transport) { |
154 ASSERT(false); | 164 ASSERT(false); |
155 return; | 165 return; |
156 } | 166 } |
157 transport->DestroyChannel(component); | 167 |
168 TransportChannelImpl* channel = transport->GetChannel(component); | |
169 bool destroyed = transport->DestroyChannel(component); | |
170 if (destroyed) { | |
171 RemoveChannel_w(channel); | |
172 // Removing a channel could cause aggregate state to change. | |
173 UpdateAggregateStates_w(); | |
174 } | |
158 | 175 |
159 // Just as we create a Transport when its first channel is created, | 176 // Just as we create a Transport when its first channel is created, |
160 // we delete it when its last channel is deleted. | 177 // we delete it when its last channel is deleted. |
161 if (!transport->HasChannels()) { | 178 if (!transport->HasChannels()) { |
162 DestroyTransport_w(transport_name); | 179 DestroyTransport_w(transport_name); |
163 } | 180 } |
164 } | 181 } |
165 | 182 |
166 const rtc::scoped_refptr<rtc::RTCCertificate>& | 183 const rtc::scoped_refptr<rtc::RTCCertificate>& |
167 TransportController::certificate_for_testing() { | 184 TransportController::certificate_for_testing() { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 transport = CreateTransport_w(transport_name); | 250 transport = CreateTransport_w(transport_name); |
234 // The stuff below happens outside of CreateTransport_w so that unit tests | 251 // The stuff below happens outside of CreateTransport_w so that unit tests |
235 // can override CreateTransport_w to return a different type of transport. | 252 // can override CreateTransport_w to return a different type of transport. |
236 transport->SetSslMaxProtocolVersion(ssl_max_version_); | 253 transport->SetSslMaxProtocolVersion(ssl_max_version_); |
237 transport->SetIceConfig(ice_config_); | 254 transport->SetIceConfig(ice_config_); |
238 transport->SetIceRole(ice_role_); | 255 transport->SetIceRole(ice_role_); |
239 transport->SetIceTiebreaker(ice_tiebreaker_); | 256 transport->SetIceTiebreaker(ice_tiebreaker_); |
240 if (certificate_) { | 257 if (certificate_) { |
241 transport->SetLocalCertificate(certificate_); | 258 transport->SetLocalCertificate(certificate_); |
242 } | 259 } |
243 transport->SignalConnecting.connect( | |
244 this, &TransportController::OnTransportConnecting_w); | |
245 transport->SignalWritableState.connect( | |
246 this, &TransportController::OnTransportWritableState_w); | |
247 transport->SignalReceivingState.connect( | |
248 this, &TransportController::OnTransportReceivingState_w); | |
249 transport->SignalCompleted.connect( | |
250 this, &TransportController::OnTransportCompleted_w); | |
251 transport->SignalFailed.connect(this, | |
252 &TransportController::OnTransportFailed_w); | |
253 transport->SignalGatheringState.connect( | |
254 this, &TransportController::OnTransportGatheringState_w); | |
255 transport->SignalCandidatesGathered.connect( | |
256 this, &TransportController::OnTransportCandidatesGathered_w); | |
257 transport->SignalRoleConflict.connect( | |
258 this, &TransportController::OnTransportRoleConflict_w); | |
259 transports_[transport_name] = transport; | 260 transports_[transport_name] = transport; |
260 | 261 |
261 return transport; | 262 return transport; |
262 } | 263 } |
263 | 264 |
264 void TransportController::DestroyTransport_w( | 265 void TransportController::DestroyTransport_w( |
265 const std::string& transport_name) { | 266 const std::string& transport_name) { |
266 RTC_DCHECK(worker_thread_->IsCurrent()); | 267 RTC_DCHECK(worker_thread_->IsCurrent()); |
267 | 268 |
268 auto iter = transports_.find(transport_name); | 269 auto iter = transports_.find(transport_name); |
269 if (iter != transports_.end()) { | 270 if (iter != transports_.end()) { |
270 delete iter->second; | 271 delete iter->second; |
271 transports_.erase(transport_name); | 272 transports_.erase(transport_name); |
272 } | 273 } |
273 // Destroying a transport may cause aggregate state to change. | |
274 UpdateAggregateStates_w(); | |
275 } | 274 } |
276 | 275 |
277 void TransportController::DestroyAllTransports_w() { | 276 void TransportController::DestroyAllTransports_w() { |
278 RTC_DCHECK(worker_thread_->IsCurrent()); | 277 RTC_DCHECK(worker_thread_->IsCurrent()); |
279 | 278 |
280 for (const auto& kv : transports_) { | 279 for (const auto& kv : transports_) { |
281 delete kv.second; | 280 delete kv.second; |
282 } | 281 } |
283 transports_.clear(); | 282 transports_.clear(); |
284 } | 283 } |
285 | 284 |
285 void TransportController::AddChannel_w(TransportChannelImpl* channel) { | |
286 channels_.push_back(channel); | |
287 channel->SignalWritableState.connect( | |
288 this, &TransportController::OnChannelWritableState_w); | |
289 channel->SignalReceivingState.connect( | |
290 this, &TransportController::OnChannelReceivingState_w); | |
291 channel->SignalGatheringState.connect( | |
292 this, &TransportController::OnChannelGatheringState_w); | |
293 channel->SignalCandidateGathered.connect( | |
294 this, &TransportController::OnChannelCandidateGathered_w); | |
295 channel->SignalRoleConflict.connect( | |
296 this, &TransportController::OnChannelRoleConflict_w); | |
297 channel->SignalConnectionRemoved.connect( | |
298 this, &TransportController::OnChannelConnectionRemoved_w); | |
299 } | |
300 | |
301 void TransportController::RemoveChannel_w(TransportChannelImpl* channel) { | |
302 channels_.erase(std::remove(channels_.begin(), channels_.end(), channel), | |
303 channels_.end()); | |
304 } | |
305 | |
286 bool TransportController::SetSslMaxProtocolVersion_w( | 306 bool TransportController::SetSslMaxProtocolVersion_w( |
287 rtc::SSLProtocolVersion version) { | 307 rtc::SSLProtocolVersion version) { |
288 RTC_DCHECK(worker_thread_->IsCurrent()); | 308 RTC_DCHECK(worker_thread_->IsCurrent()); |
289 | 309 |
290 // Max SSL version can only be set before transports are created. | 310 // Max SSL version can only be set before transports are created. |
291 if (!transports_.empty()) { | 311 if (!transports_.empty()) { |
292 return false; | 312 return false; |
293 } | 313 } |
294 | 314 |
295 ssl_max_version_ = version; | 315 ssl_max_version_ = version; |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
440 TransportStats* stats) { | 460 TransportStats* stats) { |
441 RTC_DCHECK(worker_thread()->IsCurrent()); | 461 RTC_DCHECK(worker_thread()->IsCurrent()); |
442 | 462 |
443 Transport* transport = GetTransport_w(transport_name); | 463 Transport* transport = GetTransport_w(transport_name); |
444 if (!transport) { | 464 if (!transport) { |
445 return false; | 465 return false; |
446 } | 466 } |
447 return transport->GetStats(stats); | 467 return transport->GetStats(stats); |
448 } | 468 } |
449 | 469 |
450 void TransportController::OnTransportConnecting_w(Transport* transport) { | 470 void TransportController::OnChannelWritableState_w(TransportChannel* channel) { |
471 RTC_DCHECK(worker_thread_->IsCurrent()); | |
472 LOG(LS_INFO) << channel->transport_name() << " TransportChannel " | |
473 << channel->component() << " writability changed to " | |
474 << channel->writable() << "."; | |
475 UpdateAggregateStates_w(); | |
476 } | |
477 | |
478 void TransportController::OnChannelReceivingState_w(TransportChannel* channel) { | |
451 RTC_DCHECK(worker_thread_->IsCurrent()); | 479 RTC_DCHECK(worker_thread_->IsCurrent()); |
452 UpdateAggregateStates_w(); | 480 UpdateAggregateStates_w(); |
453 } | 481 } |
454 | 482 |
455 void TransportController::OnTransportWritableState_w(Transport* transport) { | 483 void TransportController::OnChannelGatheringState_w( |
484 TransportChannelImpl* channel) { | |
456 RTC_DCHECK(worker_thread_->IsCurrent()); | 485 RTC_DCHECK(worker_thread_->IsCurrent()); |
457 UpdateAggregateStates_w(); | 486 UpdateAggregateStates_w(); |
458 } | 487 } |
459 | 488 |
460 void TransportController::OnTransportReceivingState_w(Transport* transport) { | 489 void TransportController::OnChannelCandidateGathered_w( |
490 TransportChannelImpl* channel, | |
491 const Candidate& candidate) { | |
461 RTC_DCHECK(worker_thread_->IsCurrent()); | 492 RTC_DCHECK(worker_thread_->IsCurrent()); |
462 UpdateAggregateStates_w(); | |
463 } | |
464 | 493 |
465 void TransportController::OnTransportCompleted_w(Transport* transport) { | 494 // We should never signal peer-reflexive candidates. |
466 RTC_DCHECK(worker_thread_->IsCurrent()); | 495 if (candidate.type() == PRFLX_PORT_TYPE) { |
467 UpdateAggregateStates_w(); | 496 RTC_DCHECK(false); |
468 } | 497 return; |
469 | 498 } |
470 void TransportController::OnTransportFailed_w(Transport* transport) { | 499 std::vector<Candidate> candidates; |
471 RTC_DCHECK(worker_thread_->IsCurrent()); | 500 candidates.push_back(candidate); |
472 UpdateAggregateStates_w(); | 501 CandidatesData* data = |
473 } | 502 new CandidatesData(channel->transport_name(), candidates); |
474 | |
475 void TransportController::OnTransportGatheringState_w(Transport* transport) { | |
476 RTC_DCHECK(worker_thread_->IsCurrent()); | |
477 UpdateAggregateStates_w(); | |
478 } | |
479 | |
480 void TransportController::OnTransportCandidatesGathered_w( | |
481 Transport* transport, | |
482 const std::vector<Candidate>& candidates) { | |
483 RTC_DCHECK(worker_thread_->IsCurrent()); | |
484 CandidatesData* data = new CandidatesData(transport->name(), candidates); | |
485 signaling_thread_->Post(this, MSG_CANDIDATESGATHERED, data); | 503 signaling_thread_->Post(this, MSG_CANDIDATESGATHERED, data); |
486 } | 504 } |
487 | 505 |
488 void TransportController::OnTransportRoleConflict_w() { | 506 void TransportController::OnChannelRoleConflict_w( |
507 TransportChannelImpl* channel) { | |
489 RTC_DCHECK(worker_thread_->IsCurrent()); | 508 RTC_DCHECK(worker_thread_->IsCurrent()); |
490 | 509 |
491 if (ice_role_switch_) { | 510 if (ice_role_switch_) { |
492 LOG(LS_WARNING) << "Repeat of role conflict signal from Transport."; | 511 LOG(LS_WARNING) |
512 << "Repeat of role conflict signal from TransportChannelImpl."; | |
493 return; | 513 return; |
494 } | 514 } |
495 | 515 |
496 ice_role_switch_ = true; | 516 ice_role_switch_ = true; |
497 IceRole reversed_role = (ice_role_ == ICEROLE_CONTROLLING) | 517 IceRole reversed_role = (ice_role_ == ICEROLE_CONTROLLING) |
498 ? ICEROLE_CONTROLLED | 518 ? ICEROLE_CONTROLLED |
499 : ICEROLE_CONTROLLING; | 519 : ICEROLE_CONTROLLING; |
500 for (const auto& kv : transports_) { | 520 for (const auto& kv : transports_) { |
501 kv.second->SetIceRole(reversed_role); | 521 kv.second->SetIceRole(reversed_role); |
502 } | 522 } |
503 } | 523 } |
504 | 524 |
525 void TransportController::OnChannelConnectionRemoved_w( | |
526 TransportChannelImpl* channel) { | |
527 RTC_DCHECK(worker_thread_->IsCurrent()); | |
528 LOG(LS_INFO) << channel->transport_name() << " TransportChannel " | |
529 << channel->component() | |
530 << " connection removed. Check if state is complete."; | |
531 UpdateAggregateStates_w(); | |
532 } | |
533 | |
505 void TransportController::UpdateAggregateStates_w() { | 534 void TransportController::UpdateAggregateStates_w() { |
506 RTC_DCHECK(worker_thread_->IsCurrent()); | 535 RTC_DCHECK(worker_thread_->IsCurrent()); |
507 | 536 |
508 IceConnectionState new_connection_state = kIceConnectionConnecting; | 537 IceConnectionState new_connection_state = kIceConnectionConnecting; |
509 IceGatheringState new_gathering_state = kIceGatheringNew; | 538 IceGatheringState new_gathering_state = kIceGatheringNew; |
510 bool any_receiving = false; | 539 bool any_receiving = false; |
511 bool any_failed = false; | 540 bool any_failed = false; |
512 bool all_connected = HasChannels_w(); | 541 bool all_connected = !channels_.empty(); |
513 bool all_completed = HasChannels_w(); | 542 bool all_completed = !channels_.empty(); |
514 bool any_gathering = false; | 543 bool any_gathering = false; |
515 bool all_done_gathering = HasChannels_w(); | 544 bool all_done_gathering = !channels_.empty(); |
516 for (const auto& kv : transports_) { | 545 for (const TransportChannelImpl* channel : channels_) { |
517 // Ignore transports without channels since they're about to be deleted, | 546 any_receiving = any_receiving || channel->receiving(); |
518 // and their state is meaningless. | 547 any_failed = any_failed || |
519 if (!kv.second->HasChannels()) { | 548 channel->GetState() == TransportChannelState::STATE_FAILED; |
520 continue; | 549 all_connected = all_connected && channel->writable(); |
521 } | 550 all_completed = |
522 any_receiving = any_receiving || kv.second->any_channel_receiving(); | 551 all_completed && channel->writable() && |
523 any_failed = any_failed || kv.second->AnyChannelFailed(); | 552 channel->GetState() == TransportChannelState::STATE_COMPLETED && |
524 all_connected = all_connected && kv.second->all_channels_writable(); | 553 channel->GetIceRole() == ICEROLE_CONTROLLING && |
525 all_completed = all_completed && kv.second->AllChannelsCompleted(); | 554 channel->gathering_state() == kIceGatheringComplete; |
526 any_gathering = | 555 any_gathering = |
527 any_gathering || kv.second->gathering_state() != kIceGatheringNew; | 556 any_gathering || channel->gathering_state() != kIceGatheringNew; |
528 all_done_gathering = all_done_gathering && | 557 all_done_gathering = all_done_gathering && |
529 kv.second->gathering_state() == kIceGatheringComplete; | 558 channel->gathering_state() == kIceGatheringComplete; |
530 } | 559 } |
531 | 560 |
532 if (any_failed) { | 561 if (any_failed) { |
533 new_connection_state = kIceConnectionFailed; | 562 new_connection_state = kIceConnectionFailed; |
534 } else if (all_completed) { | 563 } else if (all_completed) { |
535 new_connection_state = kIceConnectionCompleted; | 564 new_connection_state = kIceConnectionCompleted; |
536 } else if (all_connected) { | 565 } else if (all_connected) { |
537 new_connection_state = kIceConnectionConnected; | 566 new_connection_state = kIceConnectionConnected; |
538 } | 567 } |
539 if (connection_state_ != new_connection_state) { | 568 if (connection_state_ != new_connection_state) { |
(...skipping 15 matching lines...) Expand all Loading... | |
555 new_gathering_state = kIceGatheringGathering; | 584 new_gathering_state = kIceGatheringGathering; |
556 } | 585 } |
557 if (gathering_state_ != new_gathering_state) { | 586 if (gathering_state_ != new_gathering_state) { |
558 gathering_state_ = new_gathering_state; | 587 gathering_state_ = new_gathering_state; |
559 signaling_thread_->Post( | 588 signaling_thread_->Post( |
560 this, MSG_ICEGATHERINGSTATE, | 589 this, MSG_ICEGATHERINGSTATE, |
561 new rtc::TypedMessageData<IceGatheringState>(new_gathering_state)); | 590 new rtc::TypedMessageData<IceGatheringState>(new_gathering_state)); |
562 } | 591 } |
563 } | 592 } |
564 | 593 |
565 bool TransportController::HasChannels_w() { | |
566 for (const auto& kv : transports_) { | |
567 if (kv.second->HasChannels()) { | |
568 return true; | |
569 } | |
570 } | |
571 return false; | |
572 } | |
573 | |
574 } // namespace cricket | 594 } // namespace cricket |
OLD | NEW |