From be2786cd2383b7ec5d158add166275d19e246763 Mon Sep 17 00:00:00 2001 From: Tommi Date: Fri, 19 Jan 2024 17:33:00 +0100 Subject: [PATCH] Move candidate type preference defaults to the Candidate class Bug: none Change-Id: Ibd875230b22e878967bcce7d5e967bc28e0f308e Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/335380 Commit-Queue: Tomas Gunnarsson Reviewed-by: Harald Alvestrand Cr-Commit-Position: refs/heads/main@{#41596} --- api/candidate.h | 18 ++++++++++++++++++ pc/jsep_session_description.cc | 28 ++-------------------------- pc/webrtc_sdp.cc | 27 ++------------------------- 3 files changed, 22 insertions(+), 51 deletions(-) diff --git a/api/candidate.h b/api/candidate.h index 8eae59ba03..d48f4fc559 100644 --- a/api/candidate.h +++ b/api/candidate.h @@ -105,6 +105,24 @@ class RTC_EXPORT Candidate { bool is_prflx() const; bool is_relay() const; + // Returns the type preference, a value between 0-126 inclusive, with 0 being + // the lowest preference value, as described in RFC 5245. + // https://datatracker.ietf.org/doc/html/rfc5245#section-4.1.2.1 + int type_preference() const { + // From https://datatracker.ietf.org/doc/html/rfc5245#section-4.1.4 : + // It is RECOMMENDED that default candidates be chosen based on the + // likelihood of those candidates to work with the peer that is being + // contacted. + // I.e. it is recommended that relayed > reflexive > host. + if (is_local()) + return 1; // Host. + if (is_stun()) + return 2; // Reflexive. + if (is_relay()) + return 3; // Relayed. + return 0; // Unknown, lowest preference. + } + const std::string& network_name() const { return network_name_; } void set_network_name(absl::string_view network_name) { Assign(network_name_, network_name); diff --git a/pc/jsep_session_description.cc b/pc/jsep_session_description.cc index db54dbccda..7fae4459ec 100644 --- a/pc/jsep_session_description.cc +++ b/pc/jsep_session_description.cc @@ -32,32 +32,9 @@ using cricket::SessionDescription; namespace webrtc { namespace { -// RFC 5245 -// It is RECOMMENDED that default candidates be chosen based on the -// likelihood of those candidates to work with the peer that is being -// contacted. It is RECOMMENDED that relayed > reflexive > host. -constexpr int kPreferenceUnknown = 0; -constexpr int kPreferenceHost = 1; -constexpr int kPreferenceReflexive = 2; -constexpr int kPreferenceRelayed = 3; - constexpr char kDummyAddress[] = "0.0.0.0"; constexpr int kDummyPort = 9; -int GetCandidatePreferenceFromType(const Candidate& c) { - int preference = kPreferenceUnknown; - if (c.is_local()) { - preference = kPreferenceHost; - } else if (c.is_stun()) { - preference = kPreferenceReflexive; - } else if (c.is_relay()) { - preference = kPreferenceRelayed; - } else { - preference = kPreferenceUnknown; - } - return preference; -} - // Update the connection address for the MediaContentDescription based on the // candidates. void UpdateConnectionAddress( @@ -66,7 +43,7 @@ void UpdateConnectionAddress( int port = kDummyPort; std::string ip = kDummyAddress; std::string hostname; - int current_preference = kPreferenceUnknown; + int current_preference = 0; // Start with lowest preference. int current_family = AF_UNSPEC; for (size_t i = 0; i < candidate_collection.count(); ++i) { const IceCandidateInterface* jsep_candidate = candidate_collection.at(i); @@ -78,8 +55,7 @@ void UpdateConnectionAddress( if (jsep_candidate->candidate().protocol() != cricket::UDP_PROTOCOL_NAME) { continue; } - const int preference = - GetCandidatePreferenceFromType(jsep_candidate->candidate()); + const int preference = jsep_candidate->candidate().type_preference(); const int family = jsep_candidate->candidate().address().ipaddr().family(); // See if this candidate is more preferable then the current one if it's the // same family. Or if the current family is IPv4 already so we could safely diff --git a/pc/webrtc_sdp.cc b/pc/webrtc_sdp.cc index 060599b3e5..88f1ce0d1b 100644 --- a/pc/webrtc_sdp.cc +++ b/pc/webrtc_sdp.cc @@ -762,29 +762,6 @@ void GetMediaStreamIds(const ContentInfo* content, } } -// RFC 5245 -// It is RECOMMENDED that default candidates be chosen based on the -// likelihood of those candidates to work with the peer that is being -// contacted. It is RECOMMENDED that relayed > reflexive > host. -static const int kPreferenceUnknown = 0; -static const int kPreferenceHost = 1; -static const int kPreferenceReflexive = 2; -static const int kPreferenceRelayed = 3; - -static int GetCandidatePreferenceFromType(const Candidate& candidate) { - int preference = kPreferenceUnknown; - if (candidate.is_local()) { - preference = kPreferenceHost; - } else if (candidate.is_stun()) { - preference = kPreferenceReflexive; - } else if (candidate.is_relay()) { - preference = kPreferenceRelayed; - } else { - RTC_DCHECK_NOTREACHED(); - } - return preference; -} - // Get ip and port of the default destination from the `candidates` with the // given value of `component_id`. The default candidate should be the one most // likely to work, typically IPv4 relay. @@ -800,7 +777,7 @@ static void GetDefaultDestination(const std::vector& candidates, *addr_type = kConnectionIpv4Addrtype; *port = kDummyPort; *ip = kDummyAddress; - int current_preference = kPreferenceUnknown; + int current_preference = 0; // Start with lowest preference int current_family = AF_UNSPEC; for (const Candidate& candidate : candidates) { if (candidate.component() != component_id) { @@ -810,7 +787,7 @@ static void GetDefaultDestination(const std::vector& candidates, if (candidate.protocol() != cricket::UDP_PROTOCOL_NAME) { continue; } - const int preference = GetCandidatePreferenceFromType(candidate); + const int preference = candidate.type_preference(); const int family = candidate.address().ipaddr().family(); // See if this candidate is more preferable then the current one if it's the // same family. Or if the current family is IPv4 already so we could safely