Move candidate types from port to candidate.h

Add is_* getters to check candidate type without using the string constants directly.

Bug: none
Change-Id: I82c83c032a30a1c67de2d5d6168ecc04e0254318
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/334800
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41568}
This commit is contained in:
Tommi 2024-01-17 14:19:41 +01:00 committed by WebRTC LUCI CQ
parent 4d706a9fd1
commit 3b2b2afdaa
6 changed files with 40 additions and 31 deletions

View File

@ -17,6 +17,11 @@
namespace cricket { namespace cricket {
const char LOCAL_PORT_TYPE[] = "local";
const char STUN_PORT_TYPE[] = "stun";
const char PRFLX_PORT_TYPE[] = "prflx";
const char RELAY_PORT_TYPE[] = "relay";
Candidate::Candidate() Candidate::Candidate()
: id_(rtc::CreateRandomString(8)), : id_(rtc::CreateRandomString(8)),
component_(0), component_(0),
@ -57,6 +62,19 @@ Candidate::Candidate(const Candidate&) = default;
Candidate::~Candidate() = default; Candidate::~Candidate() = default;
bool Candidate::is_local() const {
return type_ == LOCAL_PORT_TYPE;
}
bool Candidate::is_stun() const {
return type_ == STUN_PORT_TYPE;
}
bool Candidate::is_prflx() const {
return type_ == PRFLX_PORT_TYPE;
}
bool Candidate::is_relay() const {
return type_ == RELAY_PORT_TYPE;
}
bool Candidate::IsEquivalent(const Candidate& c) const { bool Candidate::IsEquivalent(const Candidate& c) const {
// We ignore the network name, since that is just debug information, and // We ignore the network name, since that is just debug information, and
// the priority and the network cost, since they should be the same if the // the priority and the network cost, since they should be the same if the

View File

@ -26,6 +26,13 @@
namespace cricket { namespace cricket {
// TODO(tommi): These are temporarily here, moved from `port.h` and will
// eventually be removed once we use enums instead of strings for these values.
RTC_EXPORT extern const char LOCAL_PORT_TYPE[];
RTC_EXPORT extern const char STUN_PORT_TYPE[];
RTC_EXPORT extern const char PRFLX_PORT_TYPE[];
RTC_EXPORT extern const char RELAY_PORT_TYPE[];
// TURN servers are limited to 32 in accordance with // TURN servers are limited to 32 in accordance with
// https://w3c.github.io/webrtc-pc/#dom-rtcconfiguration-iceservers // https://w3c.github.io/webrtc-pc/#dom-rtcconfiguration-iceservers
static constexpr size_t kMaxTurnServers = 32; static constexpr size_t kMaxTurnServers = 32;
@ -111,6 +118,14 @@ class RTC_EXPORT Candidate {
Assign(type_, type); Assign(type_, type);
} }
// Provide these simple checkers to abstract away dependency on the port types
// that are currently defined outside of Candidate. This will ease the change
// from the string type to an enum.
bool is_local() const;
bool is_stun() const;
bool is_prflx() const;
bool is_relay() const;
const std::string& network_name() const { return network_name_; } const std::string& network_name() const { return network_name_; }
void set_network_name(absl::string_view network_name) { void set_network_name(absl::string_view network_name) {
Assign(network_name_, network_name); Assign(network_name_, network_name);

View File

@ -69,13 +69,6 @@ const int kPortTimeoutDelay = cricket::STUN_TOTAL_TIMEOUT + 5000;
} // namespace } // namespace
// TODO(ronghuawu): Use "local", "srflx", "prflx" and "relay". But this requires
// the signaling part be updated correspondingly as well.
const char LOCAL_PORT_TYPE[] = "local";
const char STUN_PORT_TYPE[] = "stun";
const char PRFLX_PORT_TYPE[] = "prflx";
const char RELAY_PORT_TYPE[] = "relay";
static const char* const PROTO_NAMES[] = {UDP_PROTOCOL_NAME, TCP_PROTOCOL_NAME, static const char* const PROTO_NAMES[] = {UDP_PROTOCOL_NAME, TCP_PROTOCOL_NAME,
SSLTCP_PROTOCOL_NAME, SSLTCP_PROTOCOL_NAME,
TLS_PROTOCOL_NAME}; TLS_PROTOCOL_NAME};

View File

@ -53,11 +53,6 @@
namespace cricket { namespace cricket {
RTC_EXPORT extern const char LOCAL_PORT_TYPE[];
RTC_EXPORT extern const char STUN_PORT_TYPE[];
RTC_EXPORT extern const char PRFLX_PORT_TYPE[];
RTC_EXPORT extern const char RELAY_PORT_TYPE[];
// RFC 6544, TCP candidate encoding rules. // RFC 6544, TCP candidate encoding rules.
extern const int DISCARD_PORT; extern const int DISCARD_PORT;
extern const char TCPTYPE_ACTIVE_STR[]; extern const char TCPTYPE_ACTIVE_STR[];

View File

@ -78,11 +78,6 @@ using cricket::SimulcastLayerList;
using cricket::StreamParams; using cricket::StreamParams;
using cricket::TransportInfo; using cricket::TransportInfo;
using cricket::LOCAL_PORT_TYPE;
using cricket::PRFLX_PORT_TYPE;
using cricket::RELAY_PORT_TYPE;
using cricket::STUN_PORT_TYPE;
namespace webrtc { namespace webrtc {
namespace { namespace {
@ -111,10 +106,10 @@ IceCandidatePairType GetIceCandidatePairCounter(
const cricket::Candidate& remote) { const cricket::Candidate& remote) {
const auto& l = local.type(); const auto& l = local.type();
const auto& r = remote.type(); const auto& r = remote.type();
const auto& host = LOCAL_PORT_TYPE; const auto& host = cricket::LOCAL_PORT_TYPE;
const auto& srflx = STUN_PORT_TYPE; const auto& srflx = cricket::STUN_PORT_TYPE;
const auto& relay = RELAY_PORT_TYPE; const auto& relay = cricket::RELAY_PORT_TYPE;
const auto& prflx = PRFLX_PORT_TYPE; const auto& prflx = cricket::PRFLX_PORT_TYPE;
if (l == host && r == host) { if (l == host && r == host) {
bool local_hostname = bool local_hostname =
!local.address().hostname().empty() && local.address().IsUnresolvedIP(); !local.address().hostname().empty() && local.address().IsUnresolvedIP();
@ -2076,10 +2071,8 @@ void PeerConnection::OnSelectedCandidatePairChanged(
return; return;
} }
if (event.selected_candidate_pair.local_candidate().type() == if (event.selected_candidate_pair.local_candidate().is_local() &&
LOCAL_PORT_TYPE && event.selected_candidate_pair.remote_candidate().is_local()) {
event.selected_candidate_pair.remote_candidate().type() ==
LOCAL_PORT_TYPE) {
NoteUsageEvent(UsageEvent::DIRECT_CONNECTION_SELECTED); NoteUsageEvent(UsageEvent::DIRECT_CONNECTION_SELECTED);
} }
@ -2787,7 +2780,7 @@ void PeerConnection::ReportBestConnectionState(
// Increment the counter for IceCandidatePairType. // Increment the counter for IceCandidatePairType.
if (local.protocol() == cricket::TCP_PROTOCOL_NAME || if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
(local.type() == RELAY_PORT_TYPE && (local.is_relay() &&
local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_TCP", RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_TCP",
GetIceCandidatePairCounter(local, remote), GetIceCandidatePairCounter(local, remote),

View File

@ -77,11 +77,6 @@ using cricket::SimulcastLayerList;
using cricket::StreamParams; using cricket::StreamParams;
using cricket::TransportInfo; using cricket::TransportInfo;
using cricket::LOCAL_PORT_TYPE;
using cricket::PRFLX_PORT_TYPE;
using cricket::RELAY_PORT_TYPE;
using cricket::STUN_PORT_TYPE;
namespace webrtc { namespace webrtc {
namespace { namespace {