Skip to content

Commit 459f146

Browse files
committed
Make TCP candidates less preferrable to UDP with exception of relay candidates
1 parent 153572c commit 459f146

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

candidate_base.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func (c *candidateBase) Priority() uint32 {
355355
// candidates for a particular component for a particular data stream
356356
// that have the same type, the local preference MUST be unique for each
357357
// one.
358-
return (1<<24)*uint32(c.Type().Preference()) +
358+
return (1<<24)*uint32(c.Type().Preference(c.networkType)) +
359359
(1<<8)*uint32(c.LocalPreference()) +
360360
uint32(256-c.Component())
361361
}

candidate_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestCandidatePriority(t *testing.T) {
3636
tcpType: TCPTypeActive,
3737
},
3838
},
39-
WantPriority: 2128609279,
39+
WantPriority: 1675624447,
4040
},
4141
{
4242
Candidate: &CandidateHost{
@@ -47,7 +47,7 @@ func TestCandidatePriority(t *testing.T) {
4747
tcpType: TCPTypePassive,
4848
},
4949
},
50-
WantPriority: 2124414975,
50+
WantPriority: 1671430143,
5151
},
5252
{
5353
Candidate: &CandidateHost{
@@ -58,7 +58,7 @@ func TestCandidatePriority(t *testing.T) {
5858
tcpType: TCPTypeSimultaneousOpen,
5959
},
6060
},
61-
WantPriority: 2120220671,
61+
WantPriority: 1667235839,
6262
},
6363
{
6464
Candidate: &CandidatePeerReflexive{
@@ -78,7 +78,7 @@ func TestCandidatePriority(t *testing.T) {
7878
tcpType: TCPTypeSimultaneousOpen,
7979
},
8080
},
81-
WantPriority: 1860173823,
81+
WantPriority: 1658847231,
8282
},
8383
{
8484
Candidate: &CandidatePeerReflexive{
@@ -89,7 +89,7 @@ func TestCandidatePriority(t *testing.T) {
8989
tcpType: TCPTypeActive,
9090
},
9191
},
92-
WantPriority: 1855979519,
92+
WantPriority: 1654652927,
9393
},
9494
{
9595
Candidate: &CandidatePeerReflexive{
@@ -100,7 +100,7 @@ func TestCandidatePriority(t *testing.T) {
100100
tcpType: TCPTypePassive,
101101
},
102102
},
103-
WantPriority: 1851785215,
103+
WantPriority: 1650458623,
104104
},
105105
{
106106
Candidate: &CandidateServerReflexive{
@@ -285,7 +285,7 @@ func TestCandidateMarshal(t *testing.T) {
285285
},
286286
"",
287287
},
288-
"1052353102 1 tcp 2128609279 192.168.0.196 0 typ host tcptype active",
288+
"1052353102 1 tcp 1675624447 192.168.0.196 0 typ host tcptype active",
289289
false,
290290
},
291291
{

candidatetype.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,21 @@ func (c CandidateType) String() string {
3838
// The RECOMMENDED values are 126 for host candidates, 100
3939
// for server reflexive candidates, 110 for peer reflexive candidates,
4040
// and 0 for relayed candidates.
41-
func (c CandidateType) Preference() uint16 {
41+
func (c CandidateType) Preference(networkType NetworkType) uint16 {
42+
if networkType == NetworkTypeTCP4 || networkType == NetworkTypeTCP6 {
43+
switch c {
44+
case CandidateTypeHost:
45+
return 99
46+
case CandidateTypePeerReflexive:
47+
return 98
48+
case CandidateTypeServerReflexive:
49+
return 97
50+
case CandidateTypeRelay, CandidateTypeUnspecified:
51+
return 0
52+
}
53+
}
54+
55+
// UDP candidates
4256
switch c {
4357
case CandidateTypeHost:
4458
return 126

gather_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,8 @@ func TestMultiTCPMuxUsage(t *testing.T) {
751751

752752
portFound := make(map[int]bool)
753753
for c := range candidateCh {
754-
if c.NetworkType().IsTCP() {
754+
activeCandidate := c.Port() == 0
755+
if c.NetworkType().IsTCP() && !activeCandidate {
755756
portFound[c.Port()] = true
756757
}
757758
}

0 commit comments

Comments
 (0)