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_->SignalNetworkTypeChanged.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 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
625 if (dead()) { | 624 if (dead()) { |
626 Destroy(); | 625 Destroy(); |
627 } | 626 } |
628 } | 627 } |
629 | 628 |
630 void Port::OnNetworkInactive(const rtc::Network* network) { | 629 void Port::OnNetworkInactive(const rtc::Network* network) { |
631 ASSERT(network == network_); | 630 ASSERT(network == network_); |
632 SignalNetworkInactive(this); | 631 SignalNetworkInactive(this); |
633 } | 632 } |
634 | 633 |
634 void Port::OnNetworkTypeChanged(const rtc::Network* network) { | |
635 ASSERT(network == network_); | |
636 | |
637 UpdateNetworkCost(); | |
638 } | |
639 | |
635 std::string Port::ToString() const { | 640 std::string Port::ToString() const { |
636 std::stringstream ss; | 641 std::stringstream ss; |
637 ss << "Port[" << std::hex << this << std::dec << ":" << content_name_ << ":" | 642 ss << "Port[" << std::hex << this << std::dec << ":" << content_name_ << ":" |
638 << component_ << ":" << generation_ << ":" << type_ << ":" | 643 << component_ << ":" << generation_ << ":" << type_ << ":" |
639 << network_->ToString() << "]"; | 644 << network_->ToString() << "]"; |
640 return ss.str(); | 645 return ss.str(); |
641 } | 646 } |
642 | 647 |
648 // TODO(honghaiz): Make the network cost configurable from user setting. | |
649 void Port::UpdateNetworkCost() { | |
650 uint16_t new_cost = network_->GetCost(); | |
651 if (network_cost_ == new_cost) { | |
652 return; | |
653 } | |
654 LOG(LS_INFO) << "Network cost changed from " << network_cost_ | |
655 << " to " << new_cost | |
656 << ". Number of candidates created: " << candidates_.size() | |
657 << ". Number of connections created: " << connections_.size(); | |
658 network_cost_ = new_cost; | |
659 for (cricket::Candidate& candidate : candidates_) { | |
660 candidate.set_network_cost(network_cost_); | |
661 } | |
662 // Network cost change will affect the connection selection criteria. | |
663 // Signal the network cost change if any connection has been created. | |
664 if (!connections_.empty()) { | |
665 SignalNetworkCostChanged(this); | |
666 } | |
667 } | |
668 | |
643 void Port::EnablePortPackets() { | 669 void Port::EnablePortPackets() { |
644 enable_port_packets_ = true; | 670 enable_port_packets_ = true; |
645 } | 671 } |
646 | 672 |
647 void Port::OnConnectionDestroyed(Connection* conn) { | 673 void Port::OnConnectionDestroyed(Connection* conn) { |
648 AddressMap::iterator iter = | 674 AddressMap::iterator iter = |
649 connections_.find(conn->remote_candidate().address()); | 675 connections_.find(conn->remote_candidate().address()); |
650 ASSERT(iter != connections_.end()); | 676 ASSERT(iter != connections_.end()); |
651 connections_.erase(iter); | 677 connections_.erase(iter); |
652 | 678 |
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
988 } | 1014 } |
989 | 1015 |
990 if (port_->GetIceRole() == ICEROLE_CONTROLLED) { | 1016 if (port_->GetIceRole() == ICEROLE_CONTROLLED) { |
991 const StunByteStringAttribute* use_candidate_attr = | 1017 const StunByteStringAttribute* use_candidate_attr = |
992 msg->GetByteString(STUN_ATTR_USE_CANDIDATE); | 1018 msg->GetByteString(STUN_ATTR_USE_CANDIDATE); |
993 if (use_candidate_attr) { | 1019 if (use_candidate_attr) { |
994 set_nominated(true); | 1020 set_nominated(true); |
995 SignalNominated(this); | 1021 SignalNominated(this); |
996 } | 1022 } |
997 } | 1023 } |
1024 // Set the remote cost if the network_info attribute is available. | |
1025 const StunUInt32Attribute* network_attr = | |
1026 msg->GetUInt32(STUN_ATTR_NETWORK_INFO); | |
1027 if (network_attr) { | |
1028 uint32_t network_info = network_attr->value(); | |
1029 uint16_t network_cost = static_cast<uint16_t>(network_info); | |
1030 if (network_cost != remote_candidate_.network_cost()) { | |
1031 remote_candidate_.set_network_cost(network_cost); | |
1032 // Network cost change will affect the connection ranking, so signal | |
1033 // state change so that the transport channel will re-sort connections. | |
1034 SignalStateChange(this); | |
pthatcher1
2016/05/17 21:23:11
If we always send the cost, then this could cause
honghaiz3
2016/05/18 07:41:47
Added note at the beginning of the added code.
| |
1035 } | |
1036 } | |
998 } | 1037 } |
999 | 1038 |
1000 void Connection::OnReadyToSend() { | 1039 void Connection::OnReadyToSend() { |
1001 if (write_state_ == STATE_WRITABLE) { | 1040 if (write_state_ == STATE_WRITABLE) { |
1002 SignalReadyToSend(this); | 1041 SignalReadyToSend(this); |
1003 } | 1042 } |
1004 } | 1043 } |
1005 | 1044 |
1006 void Connection::Prune() { | 1045 void Connection::Prune() { |
1007 if (!pruned_ || active()) { | 1046 if (!pruned_ || active()) { |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1158 } | 1197 } |
1159 | 1198 |
1160 std::string Connection::ToDebugId() const { | 1199 std::string Connection::ToDebugId() const { |
1161 std::stringstream ss; | 1200 std::stringstream ss; |
1162 ss << std::hex << this; | 1201 ss << std::hex << this; |
1163 return ss.str(); | 1202 return ss.str(); |
1164 } | 1203 } |
1165 | 1204 |
1166 uint32_t Connection::ComputeNetworkCost() const { | 1205 uint32_t Connection::ComputeNetworkCost() const { |
1167 // TODO(honghaiz): Will add rtt as part of the network cost. | 1206 // TODO(honghaiz): Will add rtt as part of the network cost. |
1168 return local_candidate().network_cost() + remote_candidate_.network_cost(); | 1207 return port()->network_cost() + remote_candidate_.network_cost(); |
1169 } | 1208 } |
1170 | 1209 |
1171 std::string Connection::ToString() const { | 1210 std::string Connection::ToString() const { |
1172 const char CONNECT_STATE_ABBREV[2] = { | 1211 const char CONNECT_STATE_ABBREV[2] = { |
1173 '-', // not connected (false) | 1212 '-', // not connected (false) |
1174 'C', // connected (true) | 1213 'C', // connected (true) |
1175 }; | 1214 }; |
1176 const char RECEIVE_STATE_ABBREV[2] = { | 1215 const char RECEIVE_STATE_ABBREV[2] = { |
1177 '-', // not receiving (false) | 1216 '-', // not receiving (false) |
1178 'R', // receiving (true) | 1217 'R', // receiving (true) |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1449 ASSERT(sent < 0); | 1488 ASSERT(sent < 0); |
1450 error_ = port_->GetError(); | 1489 error_ = port_->GetError(); |
1451 sent_packets_discarded_++; | 1490 sent_packets_discarded_++; |
1452 } else { | 1491 } else { |
1453 send_rate_tracker_.AddSamples(sent); | 1492 send_rate_tracker_.AddSamples(sent); |
1454 } | 1493 } |
1455 return sent; | 1494 return sent; |
1456 } | 1495 } |
1457 | 1496 |
1458 } // namespace cricket | 1497 } // namespace cricket |
OLD | NEW |