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/base/p2ptransportchannel.h" | 11 #include "webrtc/p2p/base/p2ptransportchannel.h" |
12 | 12 |
13 #include <set> | 13 #include <set> |
14 #include <algorithm> | |
14 #include "webrtc/p2p/base/common.h" | 15 #include "webrtc/p2p/base/common.h" |
15 #include "webrtc/p2p/base/relayport.h" // For RELAY_PORT_TYPE. | 16 #include "webrtc/p2p/base/relayport.h" // For RELAY_PORT_TYPE. |
16 #include "webrtc/p2p/base/stunport.h" // For STUN_PORT_TYPE. | 17 #include "webrtc/p2p/base/stunport.h" // For STUN_PORT_TYPE. |
17 #include "webrtc/base/common.h" | 18 #include "webrtc/base/common.h" |
18 #include "webrtc/base/crc32.h" | 19 #include "webrtc/base/crc32.h" |
19 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
20 #include "webrtc/base/stringencode.h" | 21 #include "webrtc/base/stringencode.h" |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
173 return b_conn->rtt() <= a_conn->rtt() + kMinImprovement; | 174 return b_conn->rtt() <= a_conn->rtt() + kMinImprovement; |
174 } | 175 } |
175 | 176 |
176 } // unnamed namespace | 177 } // unnamed namespace |
177 | 178 |
178 namespace cricket { | 179 namespace cricket { |
179 | 180 |
180 P2PTransportChannel::P2PTransportChannel(const std::string& content_name, | 181 P2PTransportChannel::P2PTransportChannel(const std::string& content_name, |
181 int component, | 182 int component, |
182 P2PTransport* transport, | 183 P2PTransport* transport, |
183 PortAllocator *allocator) : | 184 PortAllocator* allocator) : |
184 TransportChannelImpl(content_name, component), | 185 TransportChannelImpl(content_name, component), |
185 transport_(transport), | 186 transport_(transport), |
186 allocator_(allocator), | 187 allocator_(allocator), |
187 worker_thread_(rtc::Thread::Current()), | 188 worker_thread_(rtc::Thread::Current()), |
188 incoming_only_(false), | 189 incoming_only_(false), |
189 waiting_for_signaling_(false), | |
190 error_(0), | 190 error_(0), |
191 best_connection_(NULL), | 191 best_connection_(NULL), |
192 pending_best_connection_(NULL), | 192 pending_best_connection_(NULL), |
193 sort_dirty_(false), | 193 sort_dirty_(false), |
194 was_writable_(false), | 194 was_writable_(false), |
195 protocol_type_(ICEPROTO_HYBRID), | 195 protocol_type_(ICEPROTO_HYBRID), |
196 remote_ice_mode_(ICEMODE_FULL), | 196 remote_ice_mode_(ICEMODE_FULL), |
197 ice_role_(ICEROLE_UNKNOWN), | 197 ice_role_(ICEROLE_UNKNOWN), |
198 tiebreaker_(0), | 198 tiebreaker_(0), |
199 remote_candidate_generation_(0), | 199 remote_candidate_generation_(0), |
200 candidate_gathering_state_(Transport::kGatheringNew), | |
200 check_receiving_delay_(MIN_CHECK_RECEIVING_DELAY * 5), | 201 check_receiving_delay_(MIN_CHECK_RECEIVING_DELAY * 5), |
201 receiving_timeout_(MIN_CHECK_RECEIVING_DELAY * 50) { | 202 receiving_timeout_(MIN_CHECK_RECEIVING_DELAY * 50) { |
202 } | 203 } |
203 | 204 |
204 P2PTransportChannel::~P2PTransportChannel() { | 205 P2PTransportChannel::~P2PTransportChannel() { |
205 ASSERT(worker_thread_ == rtc::Thread::Current()); | 206 ASSERT(worker_thread_ == rtc::Thread::Current()); |
206 | 207 |
207 for (uint32 i = 0; i < allocator_sessions_.size(); ++i) | 208 for (uint32 i = 0; i < allocator_sessions_.size(); ++i) |
208 delete allocator_sessions_[i]; | 209 delete allocator_sessions_[i]; |
209 } | 210 } |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
428 for (iter = remote_candidates_.begin(); iter != remote_candidates_.end(); | 429 for (iter = remote_candidates_.begin(); iter != remote_candidates_.end(); |
429 ++iter) { | 430 ++iter) { |
430 CreateConnection(port, *iter, iter->origin_port(), false); | 431 CreateConnection(port, *iter, iter->origin_port(), false); |
431 } | 432 } |
432 | 433 |
433 SortConnections(); | 434 SortConnections(); |
434 } | 435 } |
435 | 436 |
436 // A new candidate is available, let listeners know | 437 // A new candidate is available, let listeners know |
437 void P2PTransportChannel::OnCandidatesReady( | 438 void P2PTransportChannel::OnCandidatesReady( |
438 PortAllocatorSession *session, const std::vector<Candidate>& candidates) { | 439 PortAllocatorSession* session, const std::vector<Candidate>& candidates) { |
439 ASSERT(worker_thread_ == rtc::Thread::Current()); | 440 ASSERT(worker_thread_ == rtc::Thread::Current()); |
440 for (size_t i = 0; i < candidates.size(); ++i) { | 441 for (size_t i = 0; i < candidates.size(); ++i) { |
441 SignalCandidateReady(this, candidates[i]); | 442 SignalCandidateReady(this, candidates[i]); |
442 } | 443 } |
443 } | 444 } |
444 | 445 |
445 void P2PTransportChannel::OnCandidatesAllocationDone( | 446 void P2PTransportChannel::OnCandidatesAllocationDone( |
446 PortAllocatorSession* session) { | 447 PortAllocatorSession* session) { |
447 ASSERT(worker_thread_ == rtc::Thread::Current()); | 448 ASSERT(worker_thread_ == rtc::Thread::Current()); |
449 candidate_gathering_state_ = Transport::kGatheringDone; | |
448 SignalCandidatesAllocationDone(this); | 450 SignalCandidatesAllocationDone(this); |
449 } | 451 } |
450 | 452 |
451 // Handle stun packets | 453 // Handle stun packets |
452 void P2PTransportChannel::OnUnknownAddress( | 454 void P2PTransportChannel::OnUnknownAddress( |
453 PortInterface* port, | 455 PortInterface* port, |
454 const rtc::SocketAddress& address, ProtocolType proto, | 456 const rtc::SocketAddress& address, ProtocolType proto, |
455 IceMessage* stun_msg, const std::string &remote_username, | 457 IceMessage* stun_msg, const std::string &remote_username, |
456 bool port_muxed) { | 458 bool port_muxed) { |
457 ASSERT(worker_thread_ == rtc::Thread::Current()); | 459 ASSERT(worker_thread_ == rtc::Thread::Current()); |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
624 // connection in question. | 626 // connection in question. |
625 SortConnections(); | 627 SortConnections(); |
626 } | 628 } |
627 } | 629 } |
628 | 630 |
629 void P2PTransportChannel::OnRoleConflict(PortInterface* port) { | 631 void P2PTransportChannel::OnRoleConflict(PortInterface* port) { |
630 SignalRoleConflict(this); // STUN ping will be sent when SetRole is called | 632 SignalRoleConflict(this); // STUN ping will be sent when SetRole is called |
631 // from Transport. | 633 // from Transport. |
632 } | 634 } |
633 | 635 |
634 // When the signalling channel is ready, we can really kick off the allocator | |
635 void P2PTransportChannel::OnSignalingReady() { | |
636 ASSERT(worker_thread_ == rtc::Thread::Current()); | |
637 if (waiting_for_signaling_) { | |
638 waiting_for_signaling_ = false; | |
639 AddAllocatorSession(allocator_->CreateSession( | |
640 SessionId(), content_name(), component(), ice_ufrag_, ice_pwd_)); | |
641 } | |
642 } | |
643 | |
644 void P2PTransportChannel::OnUseCandidate(Connection* conn) { | 636 void P2PTransportChannel::OnUseCandidate(Connection* conn) { |
645 ASSERT(worker_thread_ == rtc::Thread::Current()); | 637 ASSERT(worker_thread_ == rtc::Thread::Current()); |
646 ASSERT(ice_role_ == ICEROLE_CONTROLLED); | 638 ASSERT(ice_role_ == ICEROLE_CONTROLLED); |
647 ASSERT(protocol_type_ == ICEPROTO_RFC5245); | 639 ASSERT(protocol_type_ == ICEPROTO_RFC5245); |
648 | 640 |
649 if (conn->write_state() == Connection::STATE_WRITABLE) { | 641 if (conn->write_state() == Connection::STATE_WRITABLE) { |
650 if (best_connection_ != conn) { | 642 if (best_connection_ != conn) { |
651 pending_best_connection_ = NULL; | 643 pending_best_connection_ = NULL; |
652 LOG(LS_INFO) << "Switching best connection on controlled side: " | 644 LOG(LS_INFO) << "Switching best connection on controlled side: " |
653 << conn->ToString(); | 645 << conn->ToString(); |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
910 return sent; | 902 return sent; |
911 } | 903 } |
912 | 904 |
913 bool P2PTransportChannel::GetStats(ConnectionInfos *infos) { | 905 bool P2PTransportChannel::GetStats(ConnectionInfos *infos) { |
914 ASSERT(worker_thread_ == rtc::Thread::Current()); | 906 ASSERT(worker_thread_ == rtc::Thread::Current()); |
915 // Gather connection infos. | 907 // Gather connection infos. |
916 infos->clear(); | 908 infos->clear(); |
917 | 909 |
918 std::vector<Connection *>::const_iterator it; | 910 std::vector<Connection *>::const_iterator it; |
919 for (it = connections_.begin(); it != connections_.end(); ++it) { | 911 for (it = connections_.begin(); it != connections_.end(); ++it) { |
920 Connection *connection = *it; | 912 Connection* connection = *it; |
921 ConnectionInfo info; | 913 ConnectionInfo info; |
922 info.best_connection = (best_connection_ == connection); | 914 info.best_connection = (best_connection_ == connection); |
923 info.readable = | 915 info.readable = |
924 (connection->read_state() == Connection::STATE_READABLE); | 916 (connection->read_state() == Connection::STATE_READABLE); |
925 info.writable = | 917 info.writable = |
926 (connection->write_state() == Connection::STATE_WRITABLE); | 918 (connection->write_state() == Connection::STATE_WRITABLE); |
927 info.timeout = | 919 info.timeout = |
928 (connection->write_state() == Connection::STATE_WRITE_TIMEOUT); | 920 (connection->write_state() == Connection::STATE_WRITE_TIMEOUT); |
929 info.new_connection = !connection->reported(); | 921 info.new_connection = !connection->reported(); |
930 connection->set_reported(true); | 922 connection->set_reported(true); |
(...skipping 14 matching lines...) Expand all Loading... | |
945 } | 937 } |
946 | 938 |
947 rtc::DiffServCodePoint P2PTransportChannel::DefaultDscpValue() const { | 939 rtc::DiffServCodePoint P2PTransportChannel::DefaultDscpValue() const { |
948 OptionMap::const_iterator it = options_.find(rtc::Socket::OPT_DSCP); | 940 OptionMap::const_iterator it = options_.find(rtc::Socket::OPT_DSCP); |
949 if (it == options_.end()) { | 941 if (it == options_.end()) { |
950 return rtc::DSCP_NO_CHANGE; | 942 return rtc::DSCP_NO_CHANGE; |
951 } | 943 } |
952 return static_cast<rtc::DiffServCodePoint> (it->second); | 944 return static_cast<rtc::DiffServCodePoint> (it->second); |
953 } | 945 } |
954 | 946 |
955 // Begin allocate (or immediately re-allocate, if MSG_ALLOCATE pending) | 947 // Begin allocate |
956 void P2PTransportChannel::Allocate() { | 948 void P2PTransportChannel::Allocate() { |
pthatcher1
2015/08/10 20:40:17
We could probably call this Gather() now
Taylor Brandstetter
2015/08/11 01:20:07
Done (I'll call it StartGatheringCandidates for cl
| |
957 // Time for a new allocator, lets make sure we have a signalling channel | 949 // Time for a new allocator |
958 // to communicate candidates through first. | 950 if (candidate_gathering_state_ != Transport::kGatheringGathering) { |
959 waiting_for_signaling_ = true; | 951 candidate_gathering_state_ = Transport::kGatheringGathering; |
960 SignalRequestSignaling(this); | 952 SignalCandidatesAllocationStarted(this); |
953 } | |
954 AddAllocatorSession(allocator_->CreateSession( | |
955 SessionId(), content_name(), component(), ice_ufrag_, ice_pwd_)); | |
961 } | 956 } |
962 | 957 |
963 // Monitor connection states. | 958 // Monitor connection states. |
964 void P2PTransportChannel::UpdateConnectionStates() { | 959 void P2PTransportChannel::UpdateConnectionStates() { |
965 uint32 now = rtc::Time(); | 960 uint32 now = rtc::Time(); |
966 | 961 |
967 // We need to copy the list of connections since some may delete themselves | 962 // We need to copy the list of connections since some may delete themselves |
968 // when we call UpdateState. | 963 // when we call UpdateState. |
969 for (uint32 i = 0; i < connections_.size(); ++i) | 964 for (uint32 i = 0; i < connections_.size(); ++i) |
970 connections_[i]->UpdateState(now); | 965 connections_[i]->UpdateState(now); |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1400 std::find(ports_.begin(), ports_.end(), port); | 1395 std::find(ports_.begin(), ports_.end(), port); |
1401 if (iter != ports_.end()) | 1396 if (iter != ports_.end()) |
1402 ports_.erase(iter); | 1397 ports_.erase(iter); |
1403 | 1398 |
1404 LOG(INFO) << "Removed port from p2p socket: " | 1399 LOG(INFO) << "Removed port from p2p socket: " |
1405 << static_cast<int>(ports_.size()) << " remaining"; | 1400 << static_cast<int>(ports_.size()) << " remaining"; |
1406 } | 1401 } |
1407 | 1402 |
1408 // We data is available, let listeners know | 1403 // We data is available, let listeners know |
1409 void P2PTransportChannel::OnReadPacket( | 1404 void P2PTransportChannel::OnReadPacket( |
1410 Connection *connection, const char *data, size_t len, | 1405 Connection* connection, const char *data, size_t len, |
1411 const rtc::PacketTime& packet_time) { | 1406 const rtc::PacketTime& packet_time) { |
1412 ASSERT(worker_thread_ == rtc::Thread::Current()); | 1407 ASSERT(worker_thread_ == rtc::Thread::Current()); |
1413 | 1408 |
1414 // Do not deliver, if packet doesn't belong to the correct transport channel. | 1409 // Do not deliver, if packet doesn't belong to the correct transport channel. |
1415 if (!FindConnection(connection)) | 1410 if (!FindConnection(connection)) |
1416 return; | 1411 return; |
1417 | 1412 |
1418 // Let the client know of an incoming packet | 1413 // Let the client know of an incoming packet |
1419 SignalReadPacket(this, data, len, packet_time, 0); | 1414 SignalReadPacket(this, data, len, packet_time, 0); |
1420 } | 1415 } |
1421 | 1416 |
1422 void P2PTransportChannel::OnReadyToSend(Connection* connection) { | 1417 void P2PTransportChannel::OnReadyToSend(Connection* connection) { |
1423 if (connection == best_connection_ && writable()) { | 1418 if (connection == best_connection_ && writable()) { |
1424 SignalReadyToSend(this); | 1419 SignalReadyToSend(this); |
1425 } | 1420 } |
1426 } | 1421 } |
1427 | 1422 |
1428 } // namespace cricket | 1423 } // namespace cricket |
OLD | NEW |