Let clients set a preferred ICE candidate protocol on join (#5267)

* Let clients set a preferred ICE candidate protocol on join

The v1 JoinRequest ConnectionSettings.candidate_protocol and the v0
candidate_protocol query parameter let a client ask for TCP or TLS
from the start. The SFU applies the stronger of the client preference
and the learned room fallback, and keeps the join preference on resume.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Require the candidate protocol on every connect, check ICE/TCP

Resume uses the preference from the resume request, the same as join,
so the participant no longer stores the join value. TCP mode drops UDP
candidates, so a TCP preference applies only when ICE/TCP is enabled,
and TLS without TURN/TLS falls back to TCP only under the same check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Keep the client preference apart from the learned fallback

The room cache now holds only fallbacks the transport learned after a
failure. The participant stores the client preference from join, and
resume applies the stronger of it and the resume request before the
resume response, so the response carries the matching ForceRelay.
TLS mode lists only TLS relays, also from external TURN servers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Let a resume preference replace the join preference

A resume that sends TCP or TLS uses that value, and only a resume
without a value inherits the preference from join.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Ignore an unsupported preference on resume

A resume preference for a transport the server does not offer no
longer replaces the join preference. Each preference is lowered to a
supported transport before the resume picks between them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Raja SubramanianandClaude Opus 5.5 authored and GitHub committed 2026-11-24 15:16:42 +05:30
1 parent c8a3c95fd3
commit c19ae42e2c
11 files changed
+270 -30

No files matched your search

+6 -6
View File
@@ -22,7 +22,7 @@ require (
github.com/jxskiss/base62 v1.1.0
github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731
github.com/livekit/mediatransportutil v0.0.0-20261116220501-6af81419a977
github.com/livekit/protocol v1.54.1
github.com/livekit/protocol v1.54.3-0.20261124075400-36e1b27375d9
github.com/livekit/psrpc v0.8.2
github.com/mackerelio/go-osstat v0.2.8
github.com/magefile/mage v1.17.2
@@ -85,7 +85,7 @@ require (
github.com/felixge/httpsnoop v1.1.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/goccy/go-json v0.10.6 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.30.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.31.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.15 // indirect
github.com/mattn/go-isatty v0.0.22 // indirect
@@ -105,15 +105,15 @@ require (
github.com/rivo/uniseg v0.4.7 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.70.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.71.0 // indirect
go.opentelemetry.io/otel v1.47.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.46.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.46.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.47.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.47.0 // indirect
go.opentelemetry.io/otel/log v1.47.0 // indirect
go.opentelemetry.io/otel/metric v1.47.0 // indirect
go.opentelemetry.io/otel/sdk v1.47.0 // indirect
go.opentelemetry.io/otel/trace v1.47.0 // indirect
go.opentelemetry.io/proto/otlp v1.11.0 // indirect
go.opentelemetry.io/proto/otlp v1.11.1 // indirect
go.yaml.in/yaml/v3 v3.0.6 // indirect
golang.org/x/exp v0.0.0-20261109185158-a52e3aef5920 // indirect
golang.org/x/telemetry v0.0.0-20261009151841-97e8ea77aa2a // indirect
+12 -12
View File
@@ -123,8 +123,8 @@ github.com/google/wire v0.7.0 h1:JxUKI6+CVBgCO2WToKy/nQk0sS+amI9z9EjVmdaocj4=
github.com/google/wire v0.7.0/go.mod h1:n6YbUQD9cPKTnHXEBN2DXlOp/mVADhVErcMFb0v3J18=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.30.0 h1:/Tnpcb2E0Pz/tN9s3bfEY2Q8ePCEX9iuS+cneUwncnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.30.0/go.mod h1:zOBXOsUaBSjKgmH4OGzV1esUpR3oUSCPYVd2cUBjKYY=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.31.0 h1:Bd7KaOxzULLxtZ/K5s1aLbWhR0+5RToO65TXHsf3bqQ=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.31.0/go.mod h1:nN7ts3dFXKtCZWc//yfkpcQNKJABg16/uDVAZpLDalo=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k=
@@ -178,8 +178,8 @@ github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731 h1:9x+U2HGLrSw5AT
github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ=
github.com/livekit/mediatransportutil v0.0.0-20261116220501-6af81419a977 h1:Pe6Hmo7ZWuTiCNHuRqc/7DDRn4MQjNBO6EcQcmaeRhA=
github.com/livekit/mediatransportutil v0.0.0-20261116220501-6af81419a977/go.mod h1:/T+BvdqiQN7mYJJk5EFFqbdbe7yBAvR8Ilx5kMVzT4M=
github.com/livekit/protocol v1.54.1 h1:vksNci0cQmQF8X7AFbsIxTLmuO+6EQMcdu5PIZwqab8=
github.com/livekit/protocol v1.54.1/go.mod h1:S0AnW03n6zgnWZOq+T4L1PtT9POxTBgrgEH1jyR6fl4=
github.com/livekit/protocol v1.54.3-0.20261124075400-36e1b27375d9 h1:qPD/T1vxE9NFJL2xn2gAqhnalgi9VMfHqQSzBIDV0Ts=
github.com/livekit/protocol v1.54.3-0.20261124075400-36e1b27375d9/go.mod h1:nMEqUOj5v+v+GsAgVifdu7yvuhoJu6fAt674ObTIe/8=
github.com/livekit/psrpc v0.8.2 h1:7tbkhBJoYlK5Csfkdv1XYbZjyAQeaEZgupvIXNCv7Dc=
github.com/livekit/psrpc v0.8.2/go.mod h1:6KnU8vULoizX0Er9NNfeKJDV8ug87j3fuo19M0bhj6Q=
github.com/livekit/webrtc-pion/v4 v4.2.25-warp.3 h1:n6Rctm2NI+YuAZlAuI0yDw+ZmazAIcol6sjeeTqU32I=
@@ -359,14 +359,14 @@ github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.70.0 h1:LMuyCAyfalSjDyjdC65nK6N0zoTT63+E/u95X0JovZI=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.70.0/go.mod h1:085m8qbm4hgc8rZWGDEa4vmyyo2c3nPxUslYUKUIU04=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.71.0 h1:3g7B90UzBltIDKq1/5mrTGxTnOFDV0ICOhLoxiZ8jlg=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.71.0/go.mod h1:Ef8SuTh59BT7+ofpDxN9z+yOlc4t2GjLmKDgYNJL/NU=
go.opentelemetry.io/otel v1.47.0 h1:j7ALJ/zgkS7Z6aeJW09p8VC9804bC+PpeTfCD4XPnOM=
go.opentelemetry.io/otel v1.47.0/go.mod h1:8wS9O2qfXrYrzp6hIF/HOYJJf/wIhFPhR2xLuP+iXQU=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.46.0 h1:OFnwLJr+pF3iHrlGSzbxyuo6/6HyBlnlN1CWEJmBVcw=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.46.0/go.mod h1:716wFneO0ov19A2beH5hjfh9AK5z/VWNAtDijp1Y0/g=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.46.0 h1:KrC1YrQeSt46ITMWAbgQx1M1eV1/1TKzttrBzymPmss=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.46.0/go.mod h1:zDSEzoEqsOrgBeGvH66KRgxh90VonFyJqBHA0Pk3+rM=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.47.0 h1:julhjPeUH/q/7hinbSdDdqt5h7Zw9YWmRlWRhI0jd54=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.47.0/go.mod h1:Ao2mz688LH/tFf0yMAenidq6k2YNSx6SIY2q6jDACck=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.47.0 h1:aXZXsZ012wOgVkqaiQv+Z/d8V0xKjmg70F6m4CE94lc=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.47.0/go.mod h1:ziu1gUIJhAaxM3EV1e3Ralk5AAyL1UOYz3lJJ4VSI38=
go.opentelemetry.io/otel/log v1.47.0 h1:cOTS1CcLbSQeZKanGJ+0JpF/+t4PELi3O3bbl2lqCcI=
go.opentelemetry.io/otel/log v1.47.0/go.mod h1:9byitSQ5pLC6PpqwGXjqdMKya6ZTswHRZh2vvXT33nw=
go.opentelemetry.io/otel/metric v1.47.0 h1:4PptaldXx3Eat1XjMZ68pPJEs5wrhlemctZE9a3UdWY=
@@ -377,8 +377,8 @@ go.opentelemetry.io/otel/sdk/metric v1.47.0 h1:lfISg2j93VT6yqdk9OfUaZmw/GfcZqCCV
go.opentelemetry.io/otel/sdk/metric v1.47.0/go.mod h1:ypLp+mW1Nt2x+Szt3b5/i1syodyts49lMOwxpDI3VGw=
go.opentelemetry.io/otel/trace v1.47.0 h1:JOjX/Oci8K94QHddo+bbfya/Ai/nf6/dt9ZfrFNWSrM=
go.opentelemetry.io/otel/trace v1.47.0/go.mod h1:jNaSLa2PZEYFG6fRjJABAu+bw4FS08uDmPg28lTghu0=
go.opentelemetry.io/proto/otlp v1.11.0 h1:5rrYs0Ykyj50sdU/JU0x8etU+LubXWb+gED6TbEdMIk=
go.opentelemetry.io/proto/otlp v1.11.0/go.mod h1:SmVizdCOAm3XBtG1g1NnOdhW6jtddT72hLMhv8VwA8E=
go.opentelemetry.io/proto/otlp v1.11.1 h1:SCrPqH9NE5CmBrOqpjNCiT0X5mGwMyGERXdogfR7Yjw=
go.opentelemetry.io/proto/otlp v1.11.1/go.mod h1:wq8W4aSf6bOy8RZyESOeQFr9Pu7wydWX/TY4sgvrTPg=
go.uber.org/atomic v1.12.0 h1:BvcXdFKuviU4fTL/f+SxdQ5qJX/Jix8pAkgdUcb3XOE=
go.uber.org/atomic v1.12.0/go.mod h1:I6c4cg+6HCxRjfjSsYtApoFILnpc0CGUdGkXVqbYVNk=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
+4
View File
@@ -205,6 +205,7 @@ type ParticipantInit struct {
PublisherOffer *livekit.SessionDescription
SyncState *livekit.SyncState
UseSinglePeerConnection bool
CandidateProtocol livekit.CandidateProtocol
}
func (pi *ParticipantInit) MarshalLogObject(e zapcore.ObjectEncoder) error {
@@ -240,6 +241,7 @@ func (pi *ParticipantInit) MarshalLogObject(e zapcore.ObjectEncoder) error {
e.AddObject("PublisherOffer", logger.Proto(pi.PublisherOffer))
e.AddObject("SyncState", logger.Proto(pi.SyncState))
logBoolPtr("UseSinglePeerConnection", &pi.UseSinglePeerConnection)
e.AddString("CandidateProtocol", pi.CandidateProtocol.String())
return nil
}
@@ -274,6 +276,7 @@ func (pi *ParticipantInit) ToStartSession(roomName livekit.RoomName, connectionI
PublisherOffer: pi.PublisherOffer,
SyncState: pi.SyncState,
UseSinglePeerConnection: pi.UseSinglePeerConnection,
CandidateProtocol: pi.CandidateProtocol,
}
if pi.AutoSubscribeDataTrack != nil {
autoSubscribeDataTrack := *pi.AutoSubscribeDataTrack
@@ -316,6 +319,7 @@ func ParticipantInitFromStartSession(ss *livekit.StartSession, region string) (*
PublisherOffer: ss.PublisherOffer,
SyncState: ss.SyncState,
UseSinglePeerConnection: ss.UseSinglePeerConnection,
CandidateProtocol: ss.CandidateProtocol,
}
if ss.AutoSubscribeDataTrack != nil {
autoSubscribeDataTrack := *ss.AutoSubscribeDataTrack
+8 -2
View File
@@ -240,6 +240,7 @@ type ParticipantParams struct {
Country string
PreferVideoSizeFromMedia bool
UseSinglePeerConnection bool
CandidateProtocol livekit.CandidateProtocol
EnableDataTracks bool
EnableRTPStreamRestartDetection bool
ForceBackupCodecPolicySimulcast bool
@@ -2241,7 +2242,7 @@ func (p *ParticipantImpl) setupTransportManager() error {
return err
}
tm.OnICEConfigChanged(func(iceConfig *livekit.ICEConfig) {
tm.OnICEConfigChanged(func(iceConfig *livekit.ICEConfig, learned bool) {
p.lock.Lock()
onICEConfigChanged := p.onICEConfigChanged
@@ -2256,7 +2257,8 @@ func (p *ParticipantImpl) setupTransportManager() error {
}
p.lock.Unlock()
if onICEConfigChanged != nil {
// only learned fallbacks are reported, an explicit config comes from the client preference
if learned && onICEConfigChanged != nil {
onICEConfigChanged(p, iceConfig)
}
})
@@ -4722,6 +4724,10 @@ func (p *ParticipantImpl) IsUsingSinglePeerConnection() bool {
return p.params.UseSinglePeerConnection
}
func (p *ParticipantImpl) GetCandidateProtocol() livekit.CandidateProtocol {
return p.params.CandidateProtocol
}
func (p *ParticipantImpl) AddTrackLocal(
trackLocal webrtc.TrackLocal,
params types.AddTrackParams,
+21
View File
@@ -2087,3 +2087,24 @@ func TestPerformRpcForwardedResponseTimeout(t *testing.T) {
})
}
}
func TestICEConfigChangedReportsOnlyLearned(t *testing.T) {
p := newParticipantForTest("test")
defer p.Close(false, types.ParticipantCloseReasonNone, false)
var reported []*livekit.ICEConfig
p.OnICEConfigChanged(func(_ types.LocalParticipant, iceConfig *livekit.ICEConfig) {
reported = append(reported, iceConfig)
})
tls := &livekit.ICEConfig{PreferenceSubscriber: livekit.ICECandidateType_ICT_TLS, PreferencePublisher: livekit.ICECandidateType_ICT_TLS}
p.SetICEConfig(tls)
require.Empty(t, reported, "explicit config must not be reported as learned")
require.Equal(t, livekit.ClientConfigSetting_ENABLED, p.GetClientConfiguration().GetForceRelay())
tcp := &livekit.ICEConfig{PreferenceSubscriber: livekit.ICECandidateType_ICT_TCP, PreferencePublisher: livekit.ICECandidateType_ICT_TCP}
p.TransportManager.configureICE(tcp, false)
require.Len(t, reported, 1)
require.True(t, proto.Equal(tcp, reported[0]))
require.Equal(t, livekit.ClientConfigSetting_UNSET, p.GetClientConfiguration().GetForceRelay())
}
+4 -3
View File
@@ -125,7 +125,7 @@ type TransportManager struct {
udpLossUnstableCount uint32
signalingRTT, udpRTT uint32
onICEConfigChanged func(iceConfig *livekit.ICEConfig)
onICEConfigChanged func(iceConfig *livekit.ICEConfig, learned bool)
dataChannelSendErrorDroppedBySlowReaderCount atomic.Uint32
dataChannelSendErrorCount atomic.Uint32
@@ -702,7 +702,8 @@ func (t *TransportManager) ICERestart(iceConfig *livekit.ICEConfig) error {
return nil
}
func (t *TransportManager) OnICEConfigChanged(f func(iceConfig *livekit.ICEConfig)) {
// OnICEConfigChanged reports each change, learned is true for a fallback the transport chose after a failure.
func (t *TransportManager) OnICEConfigChanged(f func(iceConfig *livekit.ICEConfig, learned bool)) {
t.lock.Lock()
t.onICEConfigChanged = f
t.lock.Unlock()
@@ -759,7 +760,7 @@ func (t *TransportManager) configureICE(iceConfig *livekit.ICEConfig, reset bool
}
if onICEConfigChanged != nil {
onICEConfigChanged(iceConfig)
onICEConfigChanged(iceConfig, !reset)
}
}
+3
View File
@@ -623,6 +623,9 @@ type LocalParticipant interface {
SetICEConfig(iceConfig *livekit.ICEConfig)
GetICEConfig() *livekit.ICEConfig
// client preferred candidate protocol from join
GetCandidateProtocol() livekit.CandidateProtocol
// called when the transport learns a fallback ICE config after a failure
OnICEConfigChanged(callback func(participant LocalParticipant, iceConfig *livekit.ICEConfig))
UpdateSubscribedQuality(nodeID livekit.NodeID, trackID livekit.TrackID, maxQualities []SubscribedCodecQuality) error
@@ -300,6 +300,16 @@ type FakeLocalParticipant struct {
result1 *webrtc.RTPTransceiver
result2 sfu.DownTrackState
}
GetCandidateProtocolStub func() livekit.CandidateProtocol
getCandidateProtocolMutex sync.RWMutex
getCandidateProtocolArgsForCall []struct {
}
getCandidateProtocolReturns struct {
result1 livekit.CandidateProtocol
}
getCandidateProtocolReturnsOnCall map[int]struct {
result1 livekit.CandidateProtocol
}
GetClientConfigurationStub func() *livekit.ClientConfiguration
getClientConfigurationMutex sync.RWMutex
getClientConfigurationArgsForCall []struct {
@@ -3095,6 +3105,59 @@ func (fake *FakeLocalParticipant) GetCachedDownTrackReturnsOnCall(i int, result1
}{result1, result2}
}
func (fake *FakeLocalParticipant) GetCandidateProtocol() livekit.CandidateProtocol {
fake.getCandidateProtocolMutex.Lock()
ret, specificReturn := fake.getCandidateProtocolReturnsOnCall[len(fake.getCandidateProtocolArgsForCall)]
fake.getCandidateProtocolArgsForCall = append(fake.getCandidateProtocolArgsForCall, struct {
}{})
stub := fake.GetCandidateProtocolStub
fakeReturns := fake.getCandidateProtocolReturns
fake.recordInvocation("GetCandidateProtocol", []interface{}{})
fake.getCandidateProtocolMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeLocalParticipant) GetCandidateProtocolCallCount() int {
fake.getCandidateProtocolMutex.RLock()
defer fake.getCandidateProtocolMutex.RUnlock()
return len(fake.getCandidateProtocolArgsForCall)
}
func (fake *FakeLocalParticipant) GetCandidateProtocolCalls(stub func() livekit.CandidateProtocol) {
fake.getCandidateProtocolMutex.Lock()
defer fake.getCandidateProtocolMutex.Unlock()
fake.GetCandidateProtocolStub = stub
}
func (fake *FakeLocalParticipant) GetCandidateProtocolReturns(result1 livekit.CandidateProtocol) {
fake.getCandidateProtocolMutex.Lock()
defer fake.getCandidateProtocolMutex.Unlock()
fake.GetCandidateProtocolStub = nil
fake.getCandidateProtocolReturns = struct {
result1 livekit.CandidateProtocol
}{result1}
}
func (fake *FakeLocalParticipant) GetCandidateProtocolReturnsOnCall(i int, result1 livekit.CandidateProtocol) {
fake.getCandidateProtocolMutex.Lock()
defer fake.getCandidateProtocolMutex.Unlock()
fake.GetCandidateProtocolStub = nil
if fake.getCandidateProtocolReturnsOnCall == nil {
fake.getCandidateProtocolReturnsOnCall = make(map[int]struct {
result1 livekit.CandidateProtocol
})
}
fake.getCandidateProtocolReturnsOnCall[i] = struct {
result1 livekit.CandidateProtocol
}{result1}
}
func (fake *FakeLocalParticipant) GetClientConfiguration() *livekit.ClientConfiguration {
fake.getClientConfigurationMutex.Lock()
ret, specificReturn := fake.getClientConfigurationReturnsOnCall[len(fake.getClientConfigurationArgsForCall)]
+43 -7
View File
@@ -370,7 +370,8 @@ func (r *RoomManager) StartSession(
"participantInit", &pi,
"numParticipants", room.GetParticipantCount(),
)
iceConfig := r.getIceConfig(room.Name(), participant)
// applied before the resume response, so it carries the matching client configuration
iceConfig := r.setIceConfig(room.Name(), participant, resumeCandidateProtocol(r.supportedCandidateProtocol(pi.CandidateProtocol), participant.GetCandidateProtocol()))
if err = room.ResumeParticipant(
participant,
requestSource,
@@ -543,6 +544,7 @@ func (r *RoomManager) StartSession(
DatachannelDataTrackTargetLatency: r.config.RTC.DatachannelDataTrackTargetLatency,
FireOnTrackBySdp: true,
UseSinglePeerConnection: pi.UseSinglePeerConnection,
CandidateProtocol: pi.CandidateProtocol,
EnableDataTracks: r.config.EnableDataTracks,
EnableParticipantDataBlob: r.config.EnableParticipantDataBlob,
EnableRTPStreamRestartDetection: r.config.RTC.EnableRTPStreamRestartDetection,
@@ -555,7 +557,7 @@ func (r *RoomManager) StartSession(
prometheus.IncrementParticipantRtcCanceled(1, enableWarp, pi.Client.GetSdk())
return err
}
iceConfig := r.setIceConfig(room.Name(), participant)
iceConfig := r.setIceConfig(room.Name(), participant, pi.CandidateProtocol)
// join room
opts := rtc.ParticipantOptions{
@@ -1078,7 +1080,7 @@ func (r *RoomManager) iceServersForParticipant(apiKey string, participant types.
var iceServers []*livekit.ICEServer
rtcConf := r.config.RTC
if tlsOnly && r.config.TURN.TLSPort == 0 {
if tlsOnly && !r.config.IsTURNSEnabled() {
logger.Warnw("tls only enabled but no turn tls config", nil)
tlsOnly = false
}
@@ -1115,6 +1117,9 @@ func (r *RoomManager) iceServersForParticipant(apiKey string, participant types.
if len(rtcConf.TURNServers) > 0 {
hasSTUN = true
for _, s := range r.config.RTC.TURNServers {
if tlsOnly && s.Protocol != "tls" {
continue
}
scheme := "turn"
transport := "tcp"
switch s.Protocol {
@@ -1208,14 +1213,37 @@ func (r *RoomManager) refreshToken(participant types.LocalParticipant) error {
return nil
}
func (r *RoomManager) setIceConfig(roomName livekit.RoomName, participant types.LocalParticipant) *livekit.ICEConfig {
iceConfig := r.getIceConfig(roomName, participant)
func (r *RoomManager) setIceConfig(roomName livekit.RoomName, participant types.LocalParticipant, clientPreference livekit.CandidateProtocol) *livekit.ICEConfig {
iceConfig := r.getIceConfig(roomName, participant, clientPreference)
participant.SetICEConfig(iceConfig)
return iceConfig
}
func (r *RoomManager) getIceConfig(roomName livekit.RoomName, participant types.LocalParticipant) *livekit.ICEConfig {
return r.iceConfigCache.Get(iceConfigCacheKey{roomName, participant.Identity()})
// getIceConfig returns the stronger of the learned fallback and the client preference (NONE < TCP < TLS).
func (r *RoomManager) getIceConfig(roomName livekit.RoomName, participant types.LocalParticipant, clientPreference livekit.CandidateProtocol) *livekit.ICEConfig {
iceConfig := r.iceConfigCache.Get(iceConfigCacheKey{roomName, participant.Identity()})
// CandidateProtocol and ICECandidateType share values (UDP/NONE, TCP, TLS)
preferred := livekit.ICECandidateType(r.supportedCandidateProtocol(clientPreference))
if preferred <= iceConfig.PreferenceSubscriber {
return iceConfig
}
return &livekit.ICEConfig{
PreferenceSubscriber: preferred,
PreferencePublisher: preferred,
}
}
// supportedCandidateProtocol lowers a preference to a transport the server offers, as TCP mode drops UDP candidates.
func (r *RoomManager) supportedCandidateProtocol(cp livekit.CandidateProtocol) livekit.CandidateProtocol {
tcpEnabled := r.config.RTC.TCPPort != 0
switch {
case cp == livekit.CandidateProtocol_TLS && r.config.IsTURNSEnabled():
return livekit.CandidateProtocol_TLS
case (cp == livekit.CandidateProtocol_TCP || cp == livekit.CandidateProtocol_TLS) && tcpEnabled:
return livekit.CandidateProtocol_TCP
}
return livekit.CandidateProtocol_UDP
}
// getSigningKeyPair prefers the key that signed the join token so refreshed tokens stay valid when other keys are rotated.
@@ -1280,3 +1308,11 @@ func (h *roomManagerParticipantHelper) ShouldRegressCodec() bool {
func (h *roomManagerParticipantHelper) GetCachedReliableDataMessage(seqs map[livekit.ParticipantID]uint32) []*types.DataMessageCache {
return h.room.GetCachedReliableDataMessage(seqs)
}
// resumeCandidateProtocol uses the preference sent on resume, and the one from join when none was sent.
func resumeCandidateProtocol(requested, joined livekit.CandidateProtocol) livekit.CandidateProtocol {
if requested != livekit.CandidateProtocol_UDP {
return requested
}
return joined
}
+99
View File
@@ -20,9 +20,11 @@ import (
"github.com/stretchr/testify/require"
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/livekit"
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/rtc/types/typesfakes"
sutils "github.com/livekit/livekit-server/pkg/utils"
)
func TestRefreshTokenSigningKey(t *testing.T) {
@@ -56,3 +58,100 @@ func TestRefreshTokenSigningKey(t *testing.T) {
require.Contains(t, keys, refresh(t, "unknown").APIKey())
})
}
func TestGetIceConfigClientPreference(t *testing.T) {
const (
none = livekit.ICECandidateType_ICT_NONE
tcp = livekit.ICECandidateType_ICT_TCP
tls = livekit.ICECandidateType_ICT_TLS
)
fullConf := &config.Config{}
fullConf.RTC.TCPPort = 7881
fullConf.TURN.Enabled = true
fullConf.TURN.TLSPort = 5349
tcpOnlyConf := &config.Config{}
tcpOnlyConf.RTC.TCPPort = 7881
udpOnlyConf := &config.Config{}
cases := []struct {
name string
conf *config.Config
cached livekit.ICECandidateType
client livekit.CandidateProtocol
want livekit.ICECandidateType
}{
{"no preference", fullConf, none, livekit.CandidateProtocol_UDP, none},
{"client tcp", fullConf, none, livekit.CandidateProtocol_TCP, tcp},
{"client tls", fullConf, none, livekit.CandidateProtocol_TLS, tls},
{"client tls without turns", tcpOnlyConf, none, livekit.CandidateProtocol_TLS, tcp},
{"client tcp without ice/tcp", udpOnlyConf, none, livekit.CandidateProtocol_TCP, none},
{"client tls without turns or ice/tcp", udpOnlyConf, none, livekit.CandidateProtocol_TLS, none},
{"cached tls beats client tcp", fullConf, tls, livekit.CandidateProtocol_TCP, tls},
{"client tls beats cached tcp", fullConf, tcp, livekit.CandidateProtocol_TLS, tls},
{"unknown value ignored", fullConf, none, livekit.CandidateProtocol(7), none},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
r := &RoomManager{config: c.conf, iceConfigCache: sutils.NewIceConfigCache[iceConfigCacheKey](0)}
t.Cleanup(r.iceConfigCache.Stop)
p := &typesfakes.FakeLocalParticipant{}
p.IdentityReturns("p")
if c.cached != none {
r.iceConfigCache.Put(iceConfigCacheKey{"room", "p"}, &livekit.ICEConfig{PreferenceSubscriber: c.cached, PreferencePublisher: c.cached})
}
ic := r.getIceConfig("room", p, c.client)
require.Equal(t, c.want, ic.PreferenceSubscriber)
require.Equal(t, c.want, ic.PreferencePublisher)
})
}
}
func TestIceServersForParticipantTLSOnly(t *testing.T) {
conf := &config.Config{}
conf.RTC.TURNServers = []config.TURNServer{
{Host: "udp.example.com", Port: 3478, Protocol: "udp"},
{Host: "tcp.example.com", Port: 3478, Protocol: "tcp"},
{Host: "tls.example.com", Port: 443, Protocol: "tls"},
}
r := &RoomManager{config: conf}
p := &typesfakes.FakeLocalParticipant{}
p.IDReturns("PA_test")
urls := func(tlsOnly bool) []string {
var out []string
for _, s := range r.iceServersForParticipant("key", p, tlsOnly) {
out = append(out, s.Urls...)
}
return out
}
require.Equal(t, []string{
"turn:udp.example.com:3478?transport=udp",
"turn:tcp.example.com:3478?transport=tcp",
"turns:tls.example.com:443?transport=tcp",
}, urls(false))
require.Equal(t, []string{"turns:tls.example.com:443?transport=tcp"}, urls(true))
}
func TestResumeCandidateProtocol(t *testing.T) {
udp, tcp, tls := livekit.CandidateProtocol_UDP, livekit.CandidateProtocol_TCP, livekit.CandidateProtocol_TLS
require.Equal(t, tls, resumeCandidateProtocol(udp, tls), "no preference on resume keeps join")
require.Equal(t, tcp, resumeCandidateProtocol(tcp, tls), "tcp on resume replaces tls from join")
require.Equal(t, tls, resumeCandidateProtocol(tls, udp), "tls on resume replaces no preference")
require.Equal(t, udp, resumeCandidateProtocol(udp, udp))
}
func TestResumeUnsupportedPreferenceKeepsJoin(t *testing.T) {
// TURN/TLS configured, ICE/TCP off
conf := &config.Config{}
conf.TURN.Enabled = true
conf.TURN.TLSPort = 5349
r := &RoomManager{config: conf, iceConfigCache: sutils.NewIceConfigCache[iceConfigCacheKey](0)}
t.Cleanup(r.iceConfigCache.Stop)
p := &typesfakes.FakeLocalParticipant{}
p.IdentityReturns("p")
cp := resumeCandidateProtocol(r.supportedCandidateProtocol(livekit.CandidateProtocol_TCP), livekit.CandidateProtocol_TLS)
require.Equal(t, livekit.ICECandidateType_ICT_TLS, r.getIceConfig("room", p, cp).PreferenceSubscriber)
}
+7
View File
@@ -25,6 +25,7 @@ import (
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
@@ -252,6 +253,9 @@ func (s *RTCService) validateInternal(
pi.AdaptiveStream = boolValue(r.FormValue("adaptive_stream"))
pi.DisableICELite = boolValue(r.FormValue("disable_ice_lite"))
if cp, ok := livekit.CandidateProtocol_value[strings.ToUpper(r.FormValue("candidate_protocol"))]; ok {
pi.CandidateProtocol = livekit.CandidateProtocol(cp)
}
reconnectReason, _ := strconv.Atoi(r.FormValue("reconnect_reason")) // 0 means unknown reason
pi.ReconnectReason = livekit.ReconnectReason(reconnectReason)
@@ -280,6 +284,9 @@ func (s *RTCService) validateInternal(
pi.AdaptiveStream = joinRequest.GetConnectionSettings().GetAdaptiveStream()
pi.DisableICELite = joinRequest.GetConnectionSettings().GetDisableIceLite()
if cp := joinRequest.GetConnectionSettings().GetCandidateProtocol(); livekit.CandidateProtocol_name[int32(cp)] != "" {
pi.CandidateProtocol = cp
}
if cs := joinRequest.GetConnectionSettings(); cs != nil && cs.SubscriberAllowPause != nil {
v := cs.GetSubscriberAllowPause()