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 <tommi@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41596}
This commit is contained in:
Tommi 2024-01-19 17:33:00 +01:00 committed by WebRTC LUCI CQ
parent fd54a619a5
commit be2786cd23
3 changed files with 22 additions and 51 deletions

View File

@ -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);

View File

@ -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

View File

@ -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<Candidate>& 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<Candidate>& 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