Skip to content

Commit

Permalink
Make TCP candidates less preferrable to UDP
Browse files Browse the repository at this point in the history
With exception of relay candidates (TCP host is more preferrable
than UDP relay).
  • Loading branch information
ashellunts committed May 12, 2023
1 parent 012cd28 commit e4e2848
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion candidate_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
14 changes: 7 additions & 7 deletions candidate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestCandidatePriority(t *testing.T) {
tcpType: TCPTypeActive,
},
},
WantPriority: 2128609279,
WantPriority: 1675624447,
},
{
Candidate: &CandidateHost{
Expand All @@ -47,7 +47,7 @@ func TestCandidatePriority(t *testing.T) {
tcpType: TCPTypePassive,
},
},
WantPriority: 2124414975,
WantPriority: 1671430143,
},
{
Candidate: &CandidateHost{
Expand All @@ -58,7 +58,7 @@ func TestCandidatePriority(t *testing.T) {
tcpType: TCPTypeSimultaneousOpen,
},
},
WantPriority: 2120220671,
WantPriority: 1667235839,
},
{
Candidate: &CandidatePeerReflexive{
Expand All @@ -78,7 +78,7 @@ func TestCandidatePriority(t *testing.T) {
tcpType: TCPTypeSimultaneousOpen,
},
},
WantPriority: 1860173823,
WantPriority: 1658847231,
},
{
Candidate: &CandidatePeerReflexive{
Expand All @@ -89,7 +89,7 @@ func TestCandidatePriority(t *testing.T) {
tcpType: TCPTypeActive,
},
},
WantPriority: 1855979519,
WantPriority: 1654652927,
},
{
Candidate: &CandidatePeerReflexive{
Expand All @@ -100,7 +100,7 @@ func TestCandidatePriority(t *testing.T) {
tcpType: TCPTypePassive,
},
},
WantPriority: 1851785215,
WantPriority: 1650458623,
},
{
Candidate: &CandidateServerReflexive{
Expand Down Expand Up @@ -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,
},
{
Expand Down
16 changes: 15 additions & 1 deletion candidatetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion gather_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit e4e2848

Please sign in to comment.