Chromium Code Reviews| Index: webrtc/p2p/base/port.cc |
| diff --git a/webrtc/p2p/base/port.cc b/webrtc/p2p/base/port.cc |
| index a353dc2a2f0a75975af37fd2b1d5a2d3ae6cd93f..a443c8fb4f304b39af3d673374089a9304c87f78 100644 |
| --- a/webrtc/p2p/base/port.cc |
| +++ b/webrtc/p2p/base/port.cc |
| @@ -75,7 +75,7 @@ const int RTT_RATIO = 3; // 3 : 1 |
| // The delay before we begin checking if this port is useless. |
| const int kPortTimeoutDelay = 30 * 1000; // 30 seconds |
| -} |
| +} // namespace |
| namespace cricket { |
| @@ -196,11 +196,10 @@ void Port::Construct() { |
| password_ = rtc::CreateRandomString(ICE_PWD_LENGTH); |
| } |
| network_->SignalInactive.connect(this, &Port::OnNetworkInactive); |
| - // TODO(honghaiz): Make it configurable from user setting. |
| - network_cost_ = |
| - (network_->type() == rtc::ADAPTER_TYPE_CELLULAR) ? kMaxNetworkCost : 0; |
| + network_->SignalNetworkTypeChanged.connect(this, &Port::OnNetworkTypeChanged); |
| + network_cost_ = network_->GetCost(); |
| - LOG_J(LS_INFO, this) << "Port created"; |
| + LOG_J(LS_INFO, this) << "Port created with network cost " << network_cost_; |
| } |
| Port::~Port() { |
| @@ -632,6 +631,12 @@ void Port::OnNetworkInactive(const rtc::Network* network) { |
| SignalNetworkInactive(this); |
| } |
| +void Port::OnNetworkTypeChanged(const rtc::Network* network) { |
| + ASSERT(network == network_); |
| + |
| + UpdateNetworkCost(); |
| +} |
| + |
| std::string Port::ToString() const { |
| std::stringstream ss; |
| ss << "Port[" << std::hex << this << std::dec << ":" << content_name_ << ":" |
| @@ -640,6 +645,27 @@ std::string Port::ToString() const { |
| return ss.str(); |
| } |
| +// TODO(honghaiz): Make the network cost configurable from user setting. |
| +void Port::UpdateNetworkCost() { |
| + uint16_t new_cost = network_->GetCost(); |
| + if (network_cost_ == new_cost) { |
| + return; |
| + } |
| + LOG(LS_INFO) << "Network cost changed from " << network_cost_ |
| + << " to " << new_cost |
| + << ". Number of candidates created: " << candidates_.size() |
| + << ". Number of connections created: " << connections_.size(); |
| + network_cost_ = new_cost; |
| + for (cricket::Candidate& candidate : candidates_) { |
| + candidate.set_network_cost(network_cost_); |
| + } |
| + // Network cost change will affect the connection selection criteria. |
| + // Signal the network cost change if any connection has been created. |
| + if (!connections_.empty()) { |
| + SignalNetworkCostChanged(this); |
| + } |
| +} |
| + |
| void Port::EnablePortPackets() { |
| enable_port_packets_ = true; |
| } |
| @@ -995,6 +1021,19 @@ void Connection::HandleBindingRequest(IceMessage* msg) { |
| SignalNominated(this); |
| } |
| } |
| + // Set the remote cost if the network_info attribute is available. |
| + const StunUInt32Attribute* network_attr = |
| + msg->GetUInt32(STUN_ATTR_NETWORK_INFO); |
| + if (network_attr) { |
| + uint32_t network_info = network_attr->value(); |
| + uint16_t network_cost = static_cast<uint16_t>(network_info); |
| + if (network_cost != remote_candidate_.network_cost()) { |
| + remote_candidate_.set_network_cost(network_cost); |
| + // Network cost change will affect the connection ranking, so signal |
| + // state change so that the transport channel will re-sort connections. |
| + 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.
|
| + } |
| + } |
| } |
| void Connection::OnReadyToSend() { |
| @@ -1165,7 +1204,7 @@ std::string Connection::ToDebugId() const { |
| uint32_t Connection::ComputeNetworkCost() const { |
| // TODO(honghaiz): Will add rtt as part of the network cost. |
| - return local_candidate().network_cost() + remote_candidate_.network_cost(); |
| + return port()->network_cost() + remote_candidate_.network_cost(); |
| } |
| std::string Connection::ToString() const { |