Skip to content

Commit 62795f0

Browse files
jmelancongenSean-Der
authored andcommitted
Match libwebrtc's TURN protocol priority
Today, all relay candidates from Pion have the same priority. This PR attempts to reproduce libwebrtc's behavior, where the TURN servers candidates priority is based on the underlying relay protocol. UDP are preferred over TCP, which are preferred over the TLS options.
1 parent 1f41061 commit 62795f0

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

candidate_relay.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ func NewCandidateRelay(config *CandidateRelayConfig) (*CandidateRelay, error) {
7070
}, nil
7171
}
7272

73+
// LocalPreference returns the local preference for this candidate
74+
func (c *CandidateRelay) LocalPreference() uint16 {
75+
// These preference values come from libwebrtc
76+
// https://github.com/mozilla/libwebrtc/blob/1389c76d9c79839a2ca069df1db48aa3f2e6a1ac/p2p/base/turn_port.cc#L61
77+
var relayPreference uint16
78+
switch c.relayProtocol {
79+
case relayProtocolTLS, relayProtocolDTLS:
80+
relayPreference = 2
81+
case tcp:
82+
relayPreference = 1
83+
default:
84+
relayPreference = 0
85+
}
86+
87+
return c.candidateBase.LocalPreference() + relayPreference
88+
}
89+
7390
// RelayProtocol returns the protocol used between the endpoint and the relay server.
7491
func (c *CandidateRelay) RelayProtocol() string {
7592
return c.relayProtocol

gather.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ func (a *Agent) gatherCandidatesRelay(ctx context.Context, urls []*stun.URI) { /
616616

617617
relAddr = conn.LocalAddr().(*net.UDPAddr).IP.String() //nolint:forcetypeassert
618618
relPort = conn.LocalAddr().(*net.UDPAddr).Port //nolint:forcetypeassert
619-
relayProtocol = "dtls"
619+
relayProtocol = relayProtocolDTLS
620620
locConn = &fakenet.PacketConn{Conn: conn}
621621
case url.Proto == stun.ProtoTypeTCP && url.Scheme == stun.SchemeTypeTURNS:
622622
tcpAddr, resolvErr := a.net.ResolveTCPAddr(NetworkTypeTCP4.String(), turnServerAddr)
@@ -646,7 +646,7 @@ func (a *Agent) gatherCandidatesRelay(ctx context.Context, urls []*stun.URI) { /
646646

647647
relAddr = conn.LocalAddr().(*net.TCPAddr).IP.String() //nolint:forcetypeassert
648648
relPort = conn.LocalAddr().(*net.TCPAddr).Port //nolint:forcetypeassert
649-
relayProtocol = "tls"
649+
relayProtocol = relayProtocolTLS
650650
locConn = turn.NewSTUNConn(conn)
651651
default:
652652
a.log.Warnf("Unable to handle URL in gatherCandidatesRelay %v", url)

ice.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,8 @@ func (t GatheringState) String() string {
8383
return ErrUnknownType.Error()
8484
}
8585
}
86+
87+
const (
88+
relayProtocolDTLS = "dtls"
89+
relayProtocolTLS = "tls"
90+
)

0 commit comments

Comments
 (0)