From e4e28481c57b02984e0c25c0112031b030532644 Mon Sep 17 00:00:00 2001 From: Artur Shellunts Date: Fri, 12 May 2023 13:56:30 +0200 Subject: [PATCH] Make TCP candidates less preferrable to UDP With exception of relay candidates (TCP host is more preferrable than UDP relay). --- candidate_base.go | 2 +- candidate_test.go | 14 +++++++------- candidatetype.go | 16 +++++++++++++++- gather_test.go | 3 ++- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/candidate_base.go b/candidate_base.go index e7dae7d6..86d85724 100644 --- a/candidate_base.go +++ b/candidate_base.go @@ -355,7 +355,7 @@ func (c *candidateBase) Priority() uint32 { // candidates for a particular component for a particular data stream // that have the same type, the local preference MUST be unique for each // one. - return (1<<24)*uint32(c.Type().Preference()) + + return (1<<24)*uint32(c.Type().Preference(c.networkType)) + (1<<8)*uint32(c.LocalPreference()) + uint32(256-c.Component()) } diff --git a/candidate_test.go b/candidate_test.go index d668936c..0fcec860 100644 --- a/candidate_test.go +++ b/candidate_test.go @@ -36,7 +36,7 @@ func TestCandidatePriority(t *testing.T) { tcpType: TCPTypeActive, }, }, - WantPriority: 2128609279, + WantPriority: 1675624447, }, { Candidate: &CandidateHost{ @@ -47,7 +47,7 @@ func TestCandidatePriority(t *testing.T) { tcpType: TCPTypePassive, }, }, - WantPriority: 2124414975, + WantPriority: 1671430143, }, { Candidate: &CandidateHost{ @@ -58,7 +58,7 @@ func TestCandidatePriority(t *testing.T) { tcpType: TCPTypeSimultaneousOpen, }, }, - WantPriority: 2120220671, + WantPriority: 1667235839, }, { Candidate: &CandidatePeerReflexive{ @@ -78,7 +78,7 @@ func TestCandidatePriority(t *testing.T) { tcpType: TCPTypeSimultaneousOpen, }, }, - WantPriority: 1860173823, + WantPriority: 1658847231, }, { Candidate: &CandidatePeerReflexive{ @@ -89,7 +89,7 @@ func TestCandidatePriority(t *testing.T) { tcpType: TCPTypeActive, }, }, - WantPriority: 1855979519, + WantPriority: 1654652927, }, { Candidate: &CandidatePeerReflexive{ @@ -100,7 +100,7 @@ func TestCandidatePriority(t *testing.T) { tcpType: TCPTypePassive, }, }, - WantPriority: 1851785215, + WantPriority: 1650458623, }, { Candidate: &CandidateServerReflexive{ @@ -285,7 +285,7 @@ func TestCandidateMarshal(t *testing.T) { }, "", }, - "1052353102 1 tcp 2128609279 192.168.0.196 0 typ host tcptype active", + "1052353102 1 tcp 1675624447 192.168.0.196 0 typ host tcptype active", false, }, { diff --git a/candidatetype.go b/candidatetype.go index 3972934c..5744e220 100644 --- a/candidatetype.go +++ b/candidatetype.go @@ -38,7 +38,21 @@ func (c CandidateType) String() string { // The RECOMMENDED values are 126 for host candidates, 100 // for server reflexive candidates, 110 for peer reflexive candidates, // and 0 for relayed candidates. -func (c CandidateType) Preference() uint16 { +func (c CandidateType) Preference(networkType NetworkType) uint16 { + if networkType == NetworkTypeTCP4 || networkType == NetworkTypeTCP6 { + switch c { + case CandidateTypeHost: + return 99 + case CandidateTypePeerReflexive: + return 98 + case CandidateTypeServerReflexive: + return 97 + case CandidateTypeRelay, CandidateTypeUnspecified: + return 0 + } + } + + // UDP candidates switch c { case CandidateTypeHost: return 126 diff --git a/gather_test.go b/gather_test.go index 24617e33..ecafd509 100644 --- a/gather_test.go +++ b/gather_test.go @@ -751,7 +751,8 @@ func TestMultiTCPMuxUsage(t *testing.T) { portFound := make(map[int]bool) for c := range candidateCh { - if c.NetworkType().IsTCP() { + activeCandidate := c.Port() == 0 + if c.NetworkType().IsTCP() && !activeCandidate { portFound[c.Port()] = true } }