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 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 // Computes our estimate of the RTT given the current estimate. | 68 // Computes our estimate of the RTT given the current estimate. |
69 inline int ConservativeRTTEstimate(int rtt) { | 69 inline int ConservativeRTTEstimate(int rtt) { |
70 return std::max(MINIMUM_RTT, std::min(MAXIMUM_RTT, 2 * rtt)); | 70 return std::max(MINIMUM_RTT, std::min(MAXIMUM_RTT, 2 * rtt)); |
71 } | 71 } |
72 | 72 |
73 // Weighting of the old rtt value to new data. | 73 // Weighting of the old rtt value to new data. |
74 const int RTT_RATIO = 3; // 3 : 1 | 74 const int RTT_RATIO = 3; // 3 : 1 |
75 | 75 |
76 // The delay before we begin checking if this port is useless. | 76 // The delay before we begin checking if this port is useless. |
77 const int kPortTimeoutDelay = 30 * 1000; // 30 seconds | 77 const int kPortTimeoutDelay = 30 * 1000; // 30 seconds |
78 } | 78 } // namespace |
79 | 79 |
80 namespace cricket { | 80 namespace cricket { |
81 | 81 |
82 // TODO(ronghuawu): Use "host", "srflx", "prflx" and "relay". But this requires | 82 // TODO(ronghuawu): Use "host", "srflx", "prflx" and "relay". But this requires |
83 // the signaling part be updated correspondingly as well. | 83 // the signaling part be updated correspondingly as well. |
84 const char LOCAL_PORT_TYPE[] = "local"; | 84 const char LOCAL_PORT_TYPE[] = "local"; |
85 const char STUN_PORT_TYPE[] = "stun"; | 85 const char STUN_PORT_TYPE[] = "stun"; |
86 const char PRFLX_PORT_TYPE[] = "prflx"; | 86 const char PRFLX_PORT_TYPE[] = "prflx"; |
87 const char RELAY_PORT_TYPE[] = "relay"; | 87 const char RELAY_PORT_TYPE[] = "relay"; |
88 | 88 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 void Port::Construct() { | 189 void Port::Construct() { |
190 // TODO(pthatcher): Remove this old behavior once we're sure no one | 190 // TODO(pthatcher): Remove this old behavior once we're sure no one |
191 // relies on it. If the username_fragment and password are empty, | 191 // relies on it. If the username_fragment and password are empty, |
192 // we should just create one. | 192 // we should just create one. |
193 if (ice_username_fragment_.empty()) { | 193 if (ice_username_fragment_.empty()) { |
194 ASSERT(password_.empty()); | 194 ASSERT(password_.empty()); |
195 ice_username_fragment_ = rtc::CreateRandomString(ICE_UFRAG_LENGTH); | 195 ice_username_fragment_ = rtc::CreateRandomString(ICE_UFRAG_LENGTH); |
196 password_ = rtc::CreateRandomString(ICE_PWD_LENGTH); | 196 password_ = rtc::CreateRandomString(ICE_PWD_LENGTH); |
197 } | 197 } |
198 network_->SignalInactive.connect(this, &Port::OnNetworkInactive); | 198 network_->SignalInactive.connect(this, &Port::OnNetworkInactive); |
199 // TODO(honghaiz): Make it configurable from user setting. | 199 network_->SignalTypeChanged.connect(this, &Port::OnNetworkTypeChanged); |
200 network_cost_ = | 200 network_cost_ = network_->GetCost(); |
201 (network_->type() == rtc::ADAPTER_TYPE_CELLULAR) ? kMaxNetworkCost : 0; | |
202 | 201 |
203 LOG_J(LS_INFO, this) << "Port created"; | 202 LOG_J(LS_INFO, this) << "Port created with network cost " << network_cost_; |
204 } | 203 } |
205 | 204 |
206 Port::~Port() { | 205 Port::~Port() { |
207 // Delete all of the remaining connections. We copy the list up front | 206 // Delete all of the remaining connections. We copy the list up front |
208 // because each deletion will cause it to be modified. | 207 // because each deletion will cause it to be modified. |
209 | 208 |
210 std::vector<Connection*> list; | 209 std::vector<Connection*> list; |
211 | 210 |
212 AddressMap::iterator iter = connections_.begin(); | 211 AddressMap::iterator iter = connections_.begin(); |
213 while (iter != connections_.end()) { | 212 while (iter != connections_.end()) { |
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
638 if (dead()) { | 637 if (dead()) { |
639 Destroy(); | 638 Destroy(); |
640 } | 639 } |
641 } | 640 } |
642 | 641 |
643 void Port::OnNetworkInactive(const rtc::Network* network) { | 642 void Port::OnNetworkInactive(const rtc::Network* network) { |
644 ASSERT(network == network_); | 643 ASSERT(network == network_); |
645 SignalNetworkInactive(this); | 644 SignalNetworkInactive(this); |
646 } | 645 } |
647 | 646 |
| 647 void Port::OnNetworkTypeChanged(const rtc::Network* network) { |
| 648 ASSERT(network == network_); |
| 649 |
| 650 UpdateNetworkCost(); |
| 651 } |
| 652 |
648 std::string Port::ToString() const { | 653 std::string Port::ToString() const { |
649 std::stringstream ss; | 654 std::stringstream ss; |
650 ss << "Port[" << std::hex << this << std::dec << ":" << content_name_ << ":" | 655 ss << "Port[" << std::hex << this << std::dec << ":" << content_name_ << ":" |
651 << component_ << ":" << generation_ << ":" << type_ << ":" | 656 << component_ << ":" << generation_ << ":" << type_ << ":" |
652 << network_->ToString() << "]"; | 657 << network_->ToString() << "]"; |
653 return ss.str(); | 658 return ss.str(); |
654 } | 659 } |
655 | 660 |
| 661 // TODO(honghaiz): Make the network cost configurable from user setting. |
| 662 void Port::UpdateNetworkCost() { |
| 663 uint16_t new_cost = network_->GetCost(); |
| 664 if (network_cost_ == new_cost) { |
| 665 return; |
| 666 } |
| 667 LOG(LS_INFO) << "Network cost changed from " << network_cost_ |
| 668 << " to " << new_cost |
| 669 << ". Number of candidates created: " << candidates_.size() |
| 670 << ". Number of connections created: " << connections_.size(); |
| 671 network_cost_ = new_cost; |
| 672 for (cricket::Candidate& candidate : candidates_) { |
| 673 candidate.set_network_cost(network_cost_); |
| 674 } |
| 675 // Network cost change will affect the connection selection criteria. |
| 676 // Signal the connection state change on each connection to force a |
| 677 // re-sort in P2PTransportChannel. |
| 678 for (auto kv : connections_) { |
| 679 Connection* conn = kv.second; |
| 680 conn->SignalStateChange(conn); |
| 681 } |
| 682 } |
| 683 |
656 void Port::EnablePortPackets() { | 684 void Port::EnablePortPackets() { |
657 enable_port_packets_ = true; | 685 enable_port_packets_ = true; |
658 } | 686 } |
659 | 687 |
660 void Port::OnConnectionDestroyed(Connection* conn) { | 688 void Port::OnConnectionDestroyed(Connection* conn) { |
661 AddressMap::iterator iter = | 689 AddressMap::iterator iter = |
662 connections_.find(conn->remote_candidate().address()); | 690 connections_.find(conn->remote_candidate().address()); |
663 ASSERT(iter != connections_.end()); | 691 ASSERT(iter != connections_.end()); |
664 connections_.erase(iter); | 692 connections_.erase(iter); |
665 | 693 |
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1001 } | 1029 } |
1002 | 1030 |
1003 if (port_->GetIceRole() == ICEROLE_CONTROLLED) { | 1031 if (port_->GetIceRole() == ICEROLE_CONTROLLED) { |
1004 const StunByteStringAttribute* use_candidate_attr = | 1032 const StunByteStringAttribute* use_candidate_attr = |
1005 msg->GetByteString(STUN_ATTR_USE_CANDIDATE); | 1033 msg->GetByteString(STUN_ATTR_USE_CANDIDATE); |
1006 if (use_candidate_attr) { | 1034 if (use_candidate_attr) { |
1007 set_nominated(true); | 1035 set_nominated(true); |
1008 SignalNominated(this); | 1036 SignalNominated(this); |
1009 } | 1037 } |
1010 } | 1038 } |
| 1039 // Set the remote cost if the network_info attribute is available. |
| 1040 // Note: If packets are re-ordered, we may get incorrect network cost |
| 1041 // temporarily, but it should get the correct value shortly after that. |
| 1042 const StunUInt32Attribute* network_attr = |
| 1043 msg->GetUInt32(STUN_ATTR_NETWORK_INFO); |
| 1044 if (network_attr) { |
| 1045 uint32_t network_info = network_attr->value(); |
| 1046 uint16_t network_cost = static_cast<uint16_t>(network_info); |
| 1047 if (network_cost != remote_candidate_.network_cost()) { |
| 1048 remote_candidate_.set_network_cost(network_cost); |
| 1049 // Network cost change will affect the connection ranking, so signal |
| 1050 // state change to force a re-sort in P2PTransportChannel. |
| 1051 SignalStateChange(this); |
| 1052 } |
| 1053 } |
1011 } | 1054 } |
1012 | 1055 |
1013 void Connection::OnReadyToSend() { | 1056 void Connection::OnReadyToSend() { |
1014 if (write_state_ == STATE_WRITABLE) { | 1057 if (write_state_ == STATE_WRITABLE) { |
1015 SignalReadyToSend(this); | 1058 SignalReadyToSend(this); |
1016 } | 1059 } |
1017 } | 1060 } |
1018 | 1061 |
1019 void Connection::Prune() { | 1062 void Connection::Prune() { |
1020 if (!pruned_ || active()) { | 1063 if (!pruned_ || active()) { |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1171 } | 1214 } |
1172 | 1215 |
1173 std::string Connection::ToDebugId() const { | 1216 std::string Connection::ToDebugId() const { |
1174 std::stringstream ss; | 1217 std::stringstream ss; |
1175 ss << std::hex << this; | 1218 ss << std::hex << this; |
1176 return ss.str(); | 1219 return ss.str(); |
1177 } | 1220 } |
1178 | 1221 |
1179 uint32_t Connection::ComputeNetworkCost() const { | 1222 uint32_t Connection::ComputeNetworkCost() const { |
1180 // TODO(honghaiz): Will add rtt as part of the network cost. | 1223 // TODO(honghaiz): Will add rtt as part of the network cost. |
1181 return local_candidate().network_cost() + remote_candidate_.network_cost(); | 1224 return port()->network_cost() + remote_candidate_.network_cost(); |
1182 } | 1225 } |
1183 | 1226 |
1184 std::string Connection::ToString() const { | 1227 std::string Connection::ToString() const { |
1185 const char CONNECT_STATE_ABBREV[2] = { | 1228 const char CONNECT_STATE_ABBREV[2] = { |
1186 '-', // not connected (false) | 1229 '-', // not connected (false) |
1187 'C', // connected (true) | 1230 'C', // connected (true) |
1188 }; | 1231 }; |
1189 const char RECEIVE_STATE_ABBREV[2] = { | 1232 const char RECEIVE_STATE_ABBREV[2] = { |
1190 '-', // not receiving (false) | 1233 '-', // not receiving (false) |
1191 'R', // receiving (true) | 1234 'R', // receiving (true) |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1462 ASSERT(sent < 0); | 1505 ASSERT(sent < 0); |
1463 error_ = port_->GetError(); | 1506 error_ = port_->GetError(); |
1464 sent_packets_discarded_++; | 1507 sent_packets_discarded_++; |
1465 } else { | 1508 } else { |
1466 send_rate_tracker_.AddSamples(sent); | 1509 send_rate_tracker_.AddSamples(sent); |
1467 } | 1510 } |
1468 return sent; | 1511 return sent; |
1469 } | 1512 } |
1470 | 1513 |
1471 } // namespace cricket | 1514 } // namespace cricket |
OLD | NEW |