OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 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/client/basicportallocator.h" | 11 #include "webrtc/p2p/client/basicportallocator.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <string> | 14 #include <string> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "webrtc/api/peerconnectioninterface.h" | |
17 #include "webrtc/p2p/base/basicpacketsocketfactory.h" | 18 #include "webrtc/p2p/base/basicpacketsocketfactory.h" |
18 #include "webrtc/p2p/base/common.h" | 19 #include "webrtc/p2p/base/common.h" |
19 #include "webrtc/p2p/base/port.h" | 20 #include "webrtc/p2p/base/port.h" |
20 #include "webrtc/p2p/base/relayport.h" | 21 #include "webrtc/p2p/base/relayport.h" |
21 #include "webrtc/p2p/base/stunport.h" | 22 #include "webrtc/p2p/base/stunport.h" |
22 #include "webrtc/p2p/base/tcpport.h" | 23 #include "webrtc/p2p/base/tcpport.h" |
23 #include "webrtc/p2p/base/turnport.h" | 24 #include "webrtc/p2p/base/turnport.h" |
24 #include "webrtc/p2p/base/udpport.h" | 25 #include "webrtc/p2p/base/udpport.h" |
25 #include "webrtc/base/checks.h" | 26 #include "webrtc/base/checks.h" |
26 #include "webrtc/base/common.h" | 27 #include "webrtc/base/common.h" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 } | 144 } |
144 | 145 |
145 SetConfiguration(stun_servers, turn_servers, 0, false); | 146 SetConfiguration(stun_servers, turn_servers, 0, false); |
146 Construct(); | 147 Construct(); |
147 } | 148 } |
148 | 149 |
149 void BasicPortAllocator::Construct() { | 150 void BasicPortAllocator::Construct() { |
150 allow_tcp_listen_ = true; | 151 allow_tcp_listen_ = true; |
151 } | 152 } |
152 | 153 |
154 void BasicPortAllocator::OnIceRegathering(PortAllocatorSession* session, | |
155 IceRegatheringReason reason) { | |
156 if (!metrics_observer()) { | |
157 return; | |
158 } | |
159 // If the session has not been taken by an active channel, do not report the | |
160 // metric. | |
161 for (auto& allocator_session : pooled_sessions()) { | |
Taylor Brandstetter
2016/10/05 17:40:02
nit: Could use something from <algorithm> here, li
honghaiz3
2016/10/05 18:04:37
I tried this. We cannot use find here because the
| |
162 if (allocator_session.get() == session) { | |
163 return; | |
164 } | |
165 } | |
166 | |
167 metrics_observer()->IncrementEnumCounter( | |
168 webrtc::kEnumCounterIceRegathering, static_cast<int>(reason), | |
169 static_cast<int>(IceRegatheringReason::MAX_VALUE)); | |
170 } | |
171 | |
153 BasicPortAllocator::~BasicPortAllocator() { | 172 BasicPortAllocator::~BasicPortAllocator() { |
154 } | 173 } |
155 | 174 |
156 PortAllocatorSession* BasicPortAllocator::CreateSessionInternal( | 175 PortAllocatorSession* BasicPortAllocator::CreateSessionInternal( |
157 const std::string& content_name, int component, | 176 const std::string& content_name, int component, |
158 const std::string& ice_ufrag, const std::string& ice_pwd) { | 177 const std::string& ice_ufrag, const std::string& ice_pwd) { |
159 return new BasicPortAllocatorSession( | 178 PortAllocatorSession* session = new BasicPortAllocatorSession( |
160 this, content_name, component, ice_ufrag, ice_pwd); | 179 this, content_name, component, ice_ufrag, ice_pwd); |
180 session->SignalIceRegathering.connect(this, | |
181 &BasicPortAllocator::OnIceRegathering); | |
182 return session; | |
161 } | 183 } |
162 | 184 |
163 void BasicPortAllocator::AddTurnServer(const RelayServerConfig& turn_server) { | 185 void BasicPortAllocator::AddTurnServer(const RelayServerConfig& turn_server) { |
164 std::vector<RelayServerConfig> new_turn_servers = turn_servers(); | 186 std::vector<RelayServerConfig> new_turn_servers = turn_servers(); |
165 new_turn_servers.push_back(turn_server); | 187 new_turn_servers.push_back(turn_server); |
166 SetConfiguration(stun_servers(), new_turn_servers, candidate_pool_size(), | 188 SetConfiguration(stun_servers(), new_turn_servers, candidate_pool_size(), |
167 prune_turn_ports()); | 189 prune_turn_ports()); |
168 } | 190 } |
169 | 191 |
170 // BasicPortAllocatorSession | 192 // BasicPortAllocatorSession |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
240 network_thread_ = rtc::Thread::Current(); | 262 network_thread_ = rtc::Thread::Current(); |
241 state_ = SessionState::GATHERING; | 263 state_ = SessionState::GATHERING; |
242 if (!socket_factory_) { | 264 if (!socket_factory_) { |
243 owned_socket_factory_.reset( | 265 owned_socket_factory_.reset( |
244 new rtc::BasicPacketSocketFactory(network_thread_)); | 266 new rtc::BasicPacketSocketFactory(network_thread_)); |
245 socket_factory_ = owned_socket_factory_.get(); | 267 socket_factory_ = owned_socket_factory_.get(); |
246 } | 268 } |
247 | 269 |
248 network_thread_->Post(RTC_FROM_HERE, this, MSG_CONFIG_START); | 270 network_thread_->Post(RTC_FROM_HERE, this, MSG_CONFIG_START); |
249 | 271 |
250 LOG(LS_INFO) << "Pruning turn ports " | 272 LOG(LS_INFO) << "Start getting ports with prune_turn_ports " |
251 << (prune_turn_ports_ ? "enabled" : "disabled"); | 273 << (prune_turn_ports_ ? "enabled" : "disabled"); |
252 } | 274 } |
253 | 275 |
254 void BasicPortAllocatorSession::StopGettingPorts() { | 276 void BasicPortAllocatorSession::StopGettingPorts() { |
255 ASSERT(rtc::Thread::Current() == network_thread_); | 277 ASSERT(rtc::Thread::Current() == network_thread_); |
256 ClearGettingPorts(); | 278 ClearGettingPorts(); |
257 // Note: this must be called after ClearGettingPorts because both may set the | 279 // Note: this must be called after ClearGettingPorts because both may set the |
258 // session state and we should set the state to STOPPED. | 280 // session state and we should set the state to STOPPED. |
259 state_ = SessionState::STOPPED; | 281 state_ = SessionState::STOPPED; |
260 } | 282 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
295 return networks; | 317 return networks; |
296 } | 318 } |
297 | 319 |
298 void BasicPortAllocatorSession::RegatherOnFailedNetworks() { | 320 void BasicPortAllocatorSession::RegatherOnFailedNetworks() { |
299 // Find the list of networks that have no connection. | 321 // Find the list of networks that have no connection. |
300 std::vector<rtc::Network*> failed_networks = GetFailedNetworks(); | 322 std::vector<rtc::Network*> failed_networks = GetFailedNetworks(); |
301 if (failed_networks.empty()) { | 323 if (failed_networks.empty()) { |
302 return; | 324 return; |
303 } | 325 } |
304 | 326 |
327 LOG(LS_INFO) << "Regather candidates on failed networks"; | |
328 | |
305 // Mark a sequence as "network failed" if its network is in the list of failed | 329 // Mark a sequence as "network failed" if its network is in the list of failed |
306 // networks, so that it won't be considered as equivalent when the session | 330 // networks, so that it won't be considered as equivalent when the session |
307 // regathers ports and candidates. | 331 // regathers ports and candidates. |
308 for (AllocationSequence* sequence : sequences_) { | 332 for (AllocationSequence* sequence : sequences_) { |
309 if (!sequence->network_failed() && | 333 if (!sequence->network_failed() && |
310 std::find(failed_networks.begin(), failed_networks.end(), | 334 std::find(failed_networks.begin(), failed_networks.end(), |
311 sequence->network()) != failed_networks.end()) { | 335 sequence->network()) != failed_networks.end()) { |
312 sequence->set_network_failed(); | 336 sequence->set_network_failed(); |
313 } | 337 } |
314 } | 338 } |
315 // Remove ports from being used locally and send signaling to remove | 339 // Remove ports from being used locally and send signaling to remove |
316 // the candidates on the remote side. | 340 // the candidates on the remote side. |
317 std::vector<PortData*> ports_to_prune = GetUnprunedPorts(failed_networks); | 341 std::vector<PortData*> ports_to_prune = GetUnprunedPorts(failed_networks); |
318 if (!ports_to_prune.empty()) { | 342 if (!ports_to_prune.empty()) { |
319 LOG(LS_INFO) << "Prune " << ports_to_prune.size() | 343 LOG(LS_INFO) << "Prune " << ports_to_prune.size() |
320 << " ports because their networks failed"; | 344 << " ports because their networks failed"; |
321 PrunePortsAndRemoveCandidates(ports_to_prune); | 345 PrunePortsAndRemoveCandidates(ports_to_prune); |
322 } | 346 } |
323 | 347 |
324 if (allocation_started_ && network_manager_started_) { | 348 if (allocation_started_ && network_manager_started_ && !IsStopped()) { |
349 SignalIceRegathering(this, IceRegatheringReason::NETWORK_FAILURE); | |
350 | |
325 DoAllocate(); | 351 DoAllocate(); |
326 } | 352 } |
327 } | 353 } |
328 | 354 |
329 std::vector<PortInterface*> BasicPortAllocatorSession::ReadyPorts() const { | 355 std::vector<PortInterface*> BasicPortAllocatorSession::ReadyPorts() const { |
330 std::vector<PortInterface*> ret; | 356 std::vector<PortInterface*> ret; |
331 for (const PortData& data : ports_) { | 357 for (const PortData& data : ports_) { |
332 if (data.ready()) { | 358 if (data.ready()) { |
333 ret.push_back(data.port()); | 359 ret.push_back(data.port()); |
334 } | 360 } |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
494 MaybeSignalCandidatesAllocationDone(); | 520 MaybeSignalCandidatesAllocationDone(); |
495 } | 521 } |
496 } | 522 } |
497 | 523 |
498 void BasicPortAllocatorSession::AllocatePorts() { | 524 void BasicPortAllocatorSession::AllocatePorts() { |
499 ASSERT(rtc::Thread::Current() == network_thread_); | 525 ASSERT(rtc::Thread::Current() == network_thread_); |
500 network_thread_->Post(RTC_FROM_HERE, this, MSG_ALLOCATE); | 526 network_thread_->Post(RTC_FROM_HERE, this, MSG_ALLOCATE); |
501 } | 527 } |
502 | 528 |
503 void BasicPortAllocatorSession::OnAllocate() { | 529 void BasicPortAllocatorSession::OnAllocate() { |
504 if (network_manager_started_) | 530 if (network_manager_started_ && !IsStopped()) |
505 DoAllocate(); | 531 DoAllocate(); |
506 | 532 |
507 allocation_started_ = true; | 533 allocation_started_ = true; |
508 } | 534 } |
509 | 535 |
510 std::vector<rtc::Network*> BasicPortAllocatorSession::GetNetworks() { | 536 std::vector<rtc::Network*> BasicPortAllocatorSession::GetNetworks() { |
511 std::vector<rtc::Network*> networks; | 537 std::vector<rtc::Network*> networks; |
512 rtc::NetworkManager* network_manager = allocator_->network_manager(); | 538 rtc::NetworkManager* network_manager = allocator_->network_manager(); |
513 ASSERT(network_manager != nullptr); | 539 ASSERT(network_manager != nullptr); |
514 // If the network permission state is BLOCKED, we just act as if the flag has | 540 // If the network permission state is BLOCKED, we just act as if the flag has |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
546 networks.end()); | 572 networks.end()); |
547 } | 573 } |
548 return networks; | 574 return networks; |
549 } | 575 } |
550 | 576 |
551 // For each network, see if we have a sequence that covers it already. If not, | 577 // For each network, see if we have a sequence that covers it already. If not, |
552 // create a new sequence to create the appropriate ports. | 578 // create a new sequence to create the appropriate ports. |
553 void BasicPortAllocatorSession::DoAllocate() { | 579 void BasicPortAllocatorSession::DoAllocate() { |
554 bool done_signal_needed = false; | 580 bool done_signal_needed = false; |
555 std::vector<rtc::Network*> networks = GetNetworks(); | 581 std::vector<rtc::Network*> networks = GetNetworks(); |
556 | |
557 if (IsStopped()) { | |
558 return; | |
559 } | |
560 if (networks.empty()) { | 582 if (networks.empty()) { |
561 LOG(LS_WARNING) << "Machine has no networks; no ports will be allocated"; | 583 LOG(LS_WARNING) << "Machine has no networks; no ports will be allocated"; |
562 done_signal_needed = true; | 584 done_signal_needed = true; |
563 } else { | 585 } else { |
564 LOG(LS_INFO) << "Allocate ports on "<< networks.size() << " networks"; | 586 LOG(LS_INFO) << "Allocate ports on "<< networks.size() << " networks"; |
565 PortConfiguration* config = configs_.empty() ? nullptr : configs_.back(); | 587 PortConfiguration* config = configs_.empty() ? nullptr : configs_.back(); |
566 for (uint32_t i = 0; i < networks.size(); ++i) { | 588 for (uint32_t i = 0; i < networks.size(); ++i) { |
567 uint32_t sequence_flags = flags(); | 589 uint32_t sequence_flags = flags(); |
568 if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) { | 590 if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) { |
569 // If all the ports are disabled we should just fire the allocation | 591 // If all the ports are disabled we should just fire the allocation |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
620 failed_networks.push_back(sequence->network()); | 642 failed_networks.push_back(sequence->network()); |
621 } | 643 } |
622 } | 644 } |
623 std::vector<PortData*> ports_to_prune = GetUnprunedPorts(failed_networks); | 645 std::vector<PortData*> ports_to_prune = GetUnprunedPorts(failed_networks); |
624 if (!ports_to_prune.empty()) { | 646 if (!ports_to_prune.empty()) { |
625 LOG(LS_INFO) << "Prune " << ports_to_prune.size() | 647 LOG(LS_INFO) << "Prune " << ports_to_prune.size() |
626 << " ports because their networks were gone"; | 648 << " ports because their networks were gone"; |
627 PrunePortsAndRemoveCandidates(ports_to_prune); | 649 PrunePortsAndRemoveCandidates(ports_to_prune); |
628 } | 650 } |
629 | 651 |
652 if (allocation_started_ && !IsStopped()) { | |
653 if (network_manager_started_) { | |
654 // If the network manager has started, it must be regathering. | |
655 SignalIceRegathering(this, IceRegatheringReason::NETWORK_CHANGE); | |
656 } | |
657 DoAllocate(); | |
658 } | |
659 | |
630 if (!network_manager_started_) { | 660 if (!network_manager_started_) { |
631 LOG(LS_INFO) << "Network manager is started"; | 661 LOG(LS_INFO) << "Network manager has started"; |
632 network_manager_started_ = true; | 662 network_manager_started_ = true; |
633 } | 663 } |
634 if (allocation_started_) | |
635 DoAllocate(); | |
636 } | 664 } |
637 | 665 |
638 void BasicPortAllocatorSession::DisableEquivalentPhases( | 666 void BasicPortAllocatorSession::DisableEquivalentPhases( |
639 rtc::Network* network, | 667 rtc::Network* network, |
640 PortConfiguration* config, | 668 PortConfiguration* config, |
641 uint32_t* flags) { | 669 uint32_t* flags) { |
642 for (uint32_t i = 0; i < sequences_.size() && | 670 for (uint32_t i = 0; i < sequences_.size() && |
643 (*flags & DISABLE_ALL_PHASES) != DISABLE_ALL_PHASES; | 671 (*flags & DISABLE_ALL_PHASES) != DISABLE_ALL_PHASES; |
644 ++i) { | 672 ++i) { |
645 sequences_[i]->DisableEquivalentPhases(network, config, flags); | 673 sequences_[i]->DisableEquivalentPhases(network, config, flags); |
(...skipping 837 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1483 ServerAddresses servers; | 1511 ServerAddresses servers; |
1484 for (size_t i = 0; i < relays.size(); ++i) { | 1512 for (size_t i = 0; i < relays.size(); ++i) { |
1485 if (relays[i].type == turn_type && SupportsProtocol(relays[i], type)) { | 1513 if (relays[i].type == turn_type && SupportsProtocol(relays[i], type)) { |
1486 servers.insert(relays[i].ports.front().address); | 1514 servers.insert(relays[i].ports.front().address); |
1487 } | 1515 } |
1488 } | 1516 } |
1489 return servers; | 1517 return servers; |
1490 } | 1518 } |
1491 | 1519 |
1492 } // namespace cricket | 1520 } // namespace cricket |
OLD | NEW |