From 427769960051834d442bafd6b3d56d911c67b734 Mon Sep 17 00:00:00 2001 From: cnderrauber Date: Wed, 1 Mar 2023 16:45:39 +0800 Subject: [PATCH] Add option to enable skip tcp ice if tcp rtt is high (#1484) * Add option to switch tcp ice only if tcp works well * solve comment * rename and remove config change --- pkg/rtc/participant.go | 2 ++ pkg/rtc/transportmanager.go | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pkg/rtc/participant.go b/pkg/rtc/participant.go index f37fb05f..e9bf9b5d 100644 --- a/pkg/rtc/participant.go +++ b/pkg/rtc/participant.go @@ -73,6 +73,7 @@ type ParticipantParams struct { Migration bool AdaptiveStream bool AllowTCPFallback bool + TCPFallbackRTTThreshold int TURNSEnabled bool GetParticipantInfo func(pID livekit.ParticipantID) *livekit.ParticipantInfo ReconnectOnPublicationError bool @@ -981,6 +982,7 @@ func (p *ParticipantImpl) setupTransportManager() error { ClientInfo: p.params.ClientInfo, Migration: p.params.Migration, AllowTCPFallback: p.params.AllowTCPFallback, + TCPFallbackRTTThreshold: p.params.TCPFallbackRTTThreshold, TURNSEnabled: p.params.TURNSEnabled, Logger: p.params.Logger, }) diff --git a/pkg/rtc/transportmanager.go b/pkg/rtc/transportmanager.go index 381b8224..1b5787c6 100644 --- a/pkg/rtc/transportmanager.go +++ b/pkg/rtc/transportmanager.go @@ -30,7 +30,6 @@ const ( udpLossFracUnstable = 25 // if in last 32 times RR, the unstable report count over this threshold, the connection is unstable udpLossUnstableCountThreshold = 20 - tcpGoodRTT = 80 ) type TransportManagerParams struct { @@ -47,6 +46,7 @@ type TransportManagerParams struct { ClientInfo ClientInfo Migration bool AllowTCPFallback bool + TCPFallbackRTTThreshold int TURNSEnabled bool Logger logger.Logger } @@ -554,7 +554,7 @@ func (t *TransportManager) handleConnectionFailed(isShortLived bool) { // As both transports are switched to the same type on any failure, checking just subscriber should be fine. // getNext := func(ic *livekit.ICEConfig) livekit.ICECandidateType { - if ic.PreferenceSubscriber == livekit.ICECandidateType_ICT_NONE && t.params.ClientInfo.SupportsICETCP() { + if ic.PreferenceSubscriber == livekit.ICECandidateType_ICT_NONE && t.params.ClientInfo.SupportsICETCP() && t.canUseICETCP() { return livekit.ICECandidateType_ICT_TCP } else if ic.PreferenceSubscriber != livekit.ICECandidateType_ICT_TLS && t.params.TURNSEnabled { return livekit.ICECandidateType_ICT_TLS @@ -675,12 +675,15 @@ func (t *TransportManager) OnReceiverReport(dt *sfu.DownTrack, report *rtcp.Rece } func (t *TransportManager) onMediaLossUpdate(loss uint8) { + if t.params.TCPFallbackRTTThreshold == 0 { + return + } t.lock.Lock() t.udpLossUnstableCount <<= 1 if loss >= uint8(255*udpLossFracUnstable/100) { t.udpLossUnstableCount |= 1 if bits.OnesCount32(t.udpLossUnstableCount) >= udpLossUnstableCountThreshold { - if t.udpRTT > 0 && t.signalingRTT < uint32(float32(t.udpRTT)*1.3) && t.signalingRTT < tcpGoodRTT && time.Since(t.lastSignalAt) < iceFailedTimeout { + if t.udpRTT > 0 && t.signalingRTT < uint32(float32(t.udpRTT)*1.3) && int(t.signalingRTT) < t.params.TCPFallbackRTTThreshold && time.Since(t.lastSignalAt) < iceFailedTimeout { t.udpLossUnstableCount = 0 t.lock.Unlock() @@ -697,7 +700,9 @@ func (t *TransportManager) onMediaLossUpdate(loss uint8) { } func (t *TransportManager) UpdateSignalingRTT(rtt uint32) { + t.lock.Lock() t.signalingRTT = rtt + t.lock.Unlock() t.publisher.SetSignalingRTT(rtt) t.subscriber.SetSignalingRTT(rtt) @@ -707,11 +712,13 @@ func (t *TransportManager) UpdateSignalingRTT(rtt uint32) { } func (t *TransportManager) UpdateMediaRTT(rtt uint32) { + t.lock.Lock() if t.udpRTT == 0 { t.udpRTT = rtt } else { t.udpRTT = uint32(int(t.udpRTT) + (int(rtt)-int(t.udpRTT))/2) } + t.lock.Unlock() } func (t *TransportManager) UpdateLastSeenSignal() { @@ -725,3 +732,7 @@ func (t *TransportManager) SinceLastSignal() time.Duration { defer t.lock.RUnlock() return time.Since(t.lastSignalAt) } + +func (t *TransportManager) canUseICETCP() bool { + return t.params.TCPFallbackRTTThreshold == 0 || int(t.signalingRTT) < t.params.TCPFallbackRTTThreshold +}