Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1306)

Side by Side Diff: webrtc/p2p/base/p2ptransportchannel.cc

Issue 1668073002: Add network cost as part of the connection comparison. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fix sdp test error (do not signal network cost if it is 0) Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/p2p/base/candidate.h ('k') | webrtc/p2p/base/p2ptransportchannel_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 21 matching lines...) Expand all
32 cricket::PortInterface::CandidateOrigin GetOrigin(cricket::PortInterface* port, 32 cricket::PortInterface::CandidateOrigin GetOrigin(cricket::PortInterface* port,
33 cricket::PortInterface* origin_port) { 33 cricket::PortInterface* origin_port) {
34 if (!origin_port) 34 if (!origin_port)
35 return cricket::PortInterface::ORIGIN_MESSAGE; 35 return cricket::PortInterface::ORIGIN_MESSAGE;
36 else if (port == origin_port) 36 else if (port == origin_port)
37 return cricket::PortInterface::ORIGIN_THIS_PORT; 37 return cricket::PortInterface::ORIGIN_THIS_PORT;
38 else 38 else
39 return cricket::PortInterface::ORIGIN_OTHER_PORT; 39 return cricket::PortInterface::ORIGIN_OTHER_PORT;
40 } 40 }
41 41
42 // Compares two connections based only on static information about them. 42 // Compares two connections based only on the candidate and network information.
43 // Returns positive if |a| is better than |b|.
43 int CompareConnectionCandidates(cricket::Connection* a, 44 int CompareConnectionCandidates(cricket::Connection* a,
44 cricket::Connection* b) { 45 cricket::Connection* b) {
46 uint32_t a_cost = a->ComputeNetworkCost();
47 uint32_t b_cost = b->ComputeNetworkCost();
48 // Smaller cost is better.
49 if (a_cost < b_cost) {
50 return 1;
51 }
52 if (a_cost > b_cost) {
53 return -1;
54 }
55
45 // Compare connection priority. Lower values get sorted last. 56 // Compare connection priority. Lower values get sorted last.
46 if (a->priority() > b->priority()) 57 if (a->priority() > b->priority())
47 return 1; 58 return 1;
48 if (a->priority() < b->priority()) 59 if (a->priority() < b->priority())
49 return -1; 60 return -1;
50 61
51 // If we're still tied at this point, prefer a younger generation. 62 // If we're still tied at this point, prefer a younger generation.
52 return (a->remote_candidate().generation() + a->port()->generation()) - 63 return (a->remote_candidate().generation() + a->port()->generation()) -
53 (b->remote_candidate().generation() + b->port()->generation()); 64 (b->remote_candidate().generation() + b->port()->generation());
54 } 65 }
(...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after
545 556
546 Candidate remote_candidate; 557 Candidate remote_candidate;
547 bool remote_candidate_is_new = (candidate == nullptr); 558 bool remote_candidate_is_new = (candidate == nullptr);
548 if (!remote_candidate_is_new) { 559 if (!remote_candidate_is_new) {
549 remote_candidate = *candidate; 560 remote_candidate = *candidate;
550 if (ufrag_per_port) { 561 if (ufrag_per_port) {
551 remote_candidate.set_address(address); 562 remote_candidate.set_address(address);
552 } 563 }
553 } else { 564 } else {
554 // Create a new candidate with this address. 565 // Create a new candidate with this address.
555 int remote_candidate_priority;
556
557 // The priority of the candidate is set to the PRIORITY attribute 566 // The priority of the candidate is set to the PRIORITY attribute
558 // from the request. 567 // from the request.
559 const StunUInt32Attribute* priority_attr = 568 const StunUInt32Attribute* priority_attr =
560 stun_msg->GetUInt32(STUN_ATTR_PRIORITY); 569 stun_msg->GetUInt32(STUN_ATTR_PRIORITY);
561 if (!priority_attr) { 570 if (!priority_attr) {
562 LOG(LS_WARNING) << "P2PTransportChannel::OnUnknownAddress - " 571 LOG(LS_WARNING) << "P2PTransportChannel::OnUnknownAddress - "
563 << "No STUN_ATTR_PRIORITY found in the " 572 << "No STUN_ATTR_PRIORITY found in the "
564 << "stun request message"; 573 << "stun request message";
565 port->SendBindingErrorResponse(stun_msg, address, STUN_ERROR_BAD_REQUEST, 574 port->SendBindingErrorResponse(stun_msg, address, STUN_ERROR_BAD_REQUEST,
566 STUN_ERROR_REASON_BAD_REQUEST); 575 STUN_ERROR_REASON_BAD_REQUEST);
567 return; 576 return;
568 } 577 }
569 remote_candidate_priority = priority_attr->value(); 578 int remote_candidate_priority = priority_attr->value();
579
580 const StunUInt32Attribute* cost_attr =
581 stun_msg->GetUInt32(STUN_ATTR_NETWORK_COST);
582 uint32_t network_cost = (cost_attr) ? cost_attr->value() : 0;
570 583
571 // RFC 5245 584 // RFC 5245
572 // If the source transport address of the request does not match any 585 // If the source transport address of the request does not match any
573 // existing remote candidates, it represents a new peer reflexive remote 586 // existing remote candidates, it represents a new peer reflexive remote
574 // candidate. 587 // candidate.
575 remote_candidate = Candidate(component(), ProtoToString(proto), address, 0, 588 remote_candidate = Candidate(component(), ProtoToString(proto), address, 0,
576 remote_username, remote_password, 589 remote_username, remote_password,
577 PRFLX_PORT_TYPE, remote_generation, ""); 590 PRFLX_PORT_TYPE, remote_generation, "");
578 591
579 // From RFC 5245, section-7.2.1.3: 592 // From RFC 5245, section-7.2.1.3:
580 // The foundation of the candidate is set to an arbitrary value, different 593 // The foundation of the candidate is set to an arbitrary value, different
581 // from the foundation for all other remote candidates. 594 // from the foundation for all other remote candidates.
582 remote_candidate.set_foundation( 595 remote_candidate.set_foundation(
583 rtc::ToString<uint32_t>(rtc::ComputeCrc32(remote_candidate.id()))); 596 rtc::ToString<uint32_t>(rtc::ComputeCrc32(remote_candidate.id())));
584
585 remote_candidate.set_priority(remote_candidate_priority); 597 remote_candidate.set_priority(remote_candidate_priority);
598 remote_candidate.set_network_cost(network_cost);
586 } 599 }
587 600
588 // RFC5245, the agent constructs a pair whose local candidate is equal to 601 // RFC5245, the agent constructs a pair whose local candidate is equal to
589 // the transport address on which the STUN request was received, and a 602 // the transport address on which the STUN request was received, and a
590 // remote candidate equal to the source transport address where the 603 // remote candidate equal to the source transport address where the
591 // request came from. 604 // request came from.
592 605
593 // There shouldn't be an existing connection with this remote address. 606 // There shouldn't be an existing connection with this remote address.
594 // When ports are muxed, this channel might get multiple unknown address 607 // When ports are muxed, this channel might get multiple unknown address
595 // signals. In that case if the connection is already exists, we should 608 // signals. In that case if the connection is already exists, we should
(...skipping 708 matching lines...) Expand 10 before | Expand all | Expand 10 after
1304 // a.3) the best connection is unwritable OR 1317 // a.3) the best connection is unwritable OR
1305 // a.4) |conn| has higher priority than best_connection. 1318 // a.4) |conn| has higher priority than best_connection.
1306 // b) we're doing LITE ICE AND 1319 // b) we're doing LITE ICE AND
1307 // b.1) |conn| is the best_connection AND 1320 // b.1) |conn| is the best_connection AND
1308 // b.2) |conn| is writable. 1321 // b.2) |conn| is writable.
1309 void P2PTransportChannel::PingConnection(Connection* conn) { 1322 void P2PTransportChannel::PingConnection(Connection* conn) {
1310 bool use_candidate = false; 1323 bool use_candidate = false;
1311 if (remote_ice_mode_ == ICEMODE_FULL && ice_role_ == ICEROLE_CONTROLLING) { 1324 if (remote_ice_mode_ == ICEMODE_FULL && ice_role_ == ICEROLE_CONTROLLING) {
1312 use_candidate = (conn == best_connection_) || (best_connection_ == NULL) || 1325 use_candidate = (conn == best_connection_) || (best_connection_ == NULL) ||
1313 (!best_connection_->writable()) || 1326 (!best_connection_->writable()) ||
1314 (conn->priority() > best_connection_->priority()); 1327 (CompareConnectionCandidates(best_connection_, conn) < 0);
1315 } else if (remote_ice_mode_ == ICEMODE_LITE && conn == best_connection_) { 1328 } else if (remote_ice_mode_ == ICEMODE_LITE && conn == best_connection_) {
1316 use_candidate = best_connection_->writable(); 1329 use_candidate = best_connection_->writable();
1317 } 1330 }
1318 conn->set_use_candidate_attr(use_candidate); 1331 conn->set_use_candidate_attr(use_candidate);
1319 last_ping_sent_ms_ = rtc::Time(); 1332 last_ping_sent_ms_ = rtc::Time();
1320 conn->Ping(last_ping_sent_ms_); 1333 conn->Ping(last_ping_sent_ms_);
1321 } 1334 }
1322 1335
1323 // When a connection's state changes, we need to figure out who to use as 1336 // When a connection's state changes, we need to figure out who to use as
1324 // the best connection again. It could have become usable, or become unusable. 1337 // the best connection again. It could have become usable, or become unusable.
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
1430 SignalSentPacket(this, sent_packet); 1443 SignalSentPacket(this, sent_packet);
1431 } 1444 }
1432 1445
1433 void P2PTransportChannel::OnReadyToSend(Connection* connection) { 1446 void P2PTransportChannel::OnReadyToSend(Connection* connection) {
1434 if (connection == best_connection_ && writable()) { 1447 if (connection == best_connection_ && writable()) {
1435 SignalReadyToSend(this); 1448 SignalReadyToSend(this);
1436 } 1449 }
1437 } 1450 }
1438 1451
1439 } // namespace cricket 1452 } // namespace cricket
OLDNEW
« no previous file with comments | « webrtc/p2p/base/candidate.h ('k') | webrtc/p2p/base/p2ptransportchannel_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698