Files
livekit/pkg/rtc/transportmanager_test.go
cnderrauber db4cf486fc Shutdown ICE of publisher pc to help migration (#5301)
* Shutdown ICE of publisher pc to help migration

* remove comment

* log level

* Solve comments
2026-12-02 11:45:21 +08:00

207 lines
8.2 KiB
Go

// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rtc
import (
"net"
"testing"
"time"
"github.com/pion/webrtc/v4"
"github.com/stretchr/testify/require"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/livekit-server/pkg/rtc/transport/transportfakes"
"github.com/livekit/livekit-server/pkg/rtc/types"
)
func TestShortFailureFallback(t *testing.T) {
const (
none = livekit.ICECandidateType_ICT_NONE
tcp = livekit.ICECandidateType_ICT_TCP
tls = livekit.ICECandidateType_ICT_TLS
unknown = types.ICEConnectionTypeUnknown
udpPair = types.ICEConnectionTypeUDP
tcpPair = types.ICEConnectionTypeTCP
)
cases := []struct {
name string
tcpListener bool
turns bool
publisher types.ICEConnectionType
subscriber types.ICEConnectionType
// connection type of the transport that failed
failed types.ICEConnectionType
// the failure came soon after the transport connected, or from the TCP connect timer
short bool
current livekit.ICECandidateType
expected livekit.ICECandidateType
}{
{"UDP failed, node offers ICE/TCP", true, true, udpPair, udpPair, udpPair, true, none, tcp},
{"UDP failed, no ICE/TCP, TURN/TLS on", false, true, udpPair, udpPair, udpPair, true, none, tls},
{"UDP failed, no ICE/TCP, no TURN/TLS", false, false, udpPair, udpPair, udpPair, true, none, none},
{"TCP failed, TURN/TLS on", true, true, tcpPair, tcpPair, tcpPair, true, tcp, tls},
{"TCP failed, no TURN/TLS", true, false, tcpPair, tcpPair, tcpPair, true, tcp, tcp},
{"TCP never connected, TCP connect timer", true, true, unknown, unknown, unknown, true, tcp, tls},
{"TCP never connected, ICE failure", true, true, unknown, unknown, unknown, false, tcp, tls},
{"TCP never connected, no TURN/TLS", true, false, unknown, unknown, unknown, false, tcp, none},
{"nothing connected, no preference", true, true, unknown, unknown, unknown, false, none, none},
{"TURN/TLS never connected", true, true, unknown, unknown, unknown, false, tls, none},
// the subscriber restarted in TCP mode, the publisher still has the UDP pair from before the preference
{"TCP restart never connected, other transport on UDP", true, true, udpPair, unknown, unknown, false, tcp, tls},
{"TCP restart never connected, other transport on UDP, no TURN/TLS", true, false, udpPair, unknown, unknown, false, tcp, none},
{"old UDP pair failed after a TCP preference", true, true, udpPair, unknown, udpPair, true, tcp, tcp},
{"old UDP pair failed after a TLS preference", false, true, udpPair, unknown, udpPair, true, tls, tls},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
p := newParticipantForTestWithOpts("test", &participantOpts{
protocolVersion: 17,
clientInfo: &livekit.ClientInfo{Sdk: livekit.ClientInfo_JS, Version: "2.15.2"},
})
defer p.Close(false, types.ParticipantCloseReasonNone, false)
tm := p.TransportManager
tm.params.AllowTCPFallback = true
tm.params.TURNSEnabled = c.turns
if c.tcpListener {
tm.params.Config.TCPMuxListener = &net.TCPListener{}
}
tm.publisher.connectionDetails.Type = c.publisher
tm.subscriber.connectionDetails.Type = c.subscriber
p.UpdateLastSeenSignal()
p.SetSignalSourceValid(true)
p.SetICEConfig(&livekit.ICEConfig{PreferenceSubscriber: c.current, PreferencePublisher: c.current})
tm.handleConnectionFailed(c.short, c.failed)
require.Equal(t, c.expected, p.GetICEConfig().GetPreferenceSubscriber())
})
}
}
func TestClientReconnectFallback(t *testing.T) {
const (
tcp = livekit.ICECandidateType_ICT_TCP
tls = livekit.ICECandidateType_ICT_TLS
)
// the subscriber restarted in TCP mode and did not connect, the publisher has the UDP pair from before
setup := func(t *testing.T) (*ParticipantImpl, *TransportManager) {
p := newParticipantForTestWithOpts("test", &participantOpts{
protocolVersion: 17,
clientInfo: &livekit.ClientInfo{Sdk: livekit.ClientInfo_JS, Version: "2.15.2"},
})
t.Cleanup(func() { p.Close(false, types.ParticipantCloseReasonNone, false) })
tm := p.TransportManager
tm.params.AllowTCPFallback = true
tm.params.TURNSEnabled = true
tm.params.Config.TCPMuxListener = &net.TCPListener{}
tm.publisher.connectionDetails.Type = types.ICEConnectionTypeUDP
tm.subscriber.connectionDetails.Type = types.ICEConnectionTypeUnknown
for _, pc := range []*PCTransport{tm.publisher, tm.subscriber} {
pc.iceFirstConnectedAt = time.Now()
}
p.UpdateLastSeenSignal()
p.SetSignalSourceValid(true)
p.SetICEConfig(&livekit.ICEConfig{PreferenceSubscriber: tcp, PreferencePublisher: tcp})
return p, tm
}
t.Run("report for the old UDP pair keeps the preference", func(t *testing.T) {
p, tm := setup(t)
tm.HandleClientReconnect(livekit.ReconnectReason_RR_PUBLISHER_FAILED)
require.Equal(t, tcp, p.GetICEConfig().GetPreferenceSubscriber())
})
t.Run("report for the transport that did not connect in TCP mode", func(t *testing.T) {
p, tm := setup(t)
tm.HandleClientReconnect(livekit.ReconnectReason_RR_SUBSCRIBER_FAILED)
require.Equal(t, tls, p.GetICEConfig().GetPreferenceSubscriber())
})
t.Run("report after the server chose a fallback for the same failure", func(t *testing.T) {
p, tm := setup(t)
tm.handleConnectionFailed(false, types.ICEConnectionTypeUnknown)
require.Equal(t, tls, p.GetICEConfig().GetPreferenceSubscriber())
tm.HandleClientReconnect(livekit.ReconnectReason_RR_SUBSCRIBER_FAILED)
require.Equal(t, tls, p.GetICEConfig().GetPreferenceSubscriber())
})
}
func TestMigratedReliableDataChannelIsOrdered(t *testing.T) {
clientHandler := &transportfakes.FakeHandler{}
client, err := NewPCTransport(TransportParams{Config: &WebRTCConfig{}, IsOfferer: true, Handler: clientHandler})
require.NoError(t, err)
defer client.Close()
serverHandler := &transportfakes.FakeHandler{}
server, err := NewPCTransport(TransportParams{Config: &WebRTCConfig{}, Handler: serverHandler})
require.NoError(t, err)
defer server.Close()
negotiated := true
lossyID, reliableID := uint16(1), uint16(0)
ordered, unordered := true, false
retransmits := uint16(0)
_, err = client.pc.CreateDataChannel(LossyDataChannel, &webrtc.DataChannelInit{
Negotiated: &negotiated, ID: &lossyID, Ordered: &unordered, MaxRetransmits: &retransmits,
})
require.NoError(t, err)
_, err = client.pc.CreateDataChannel(ReliableDataChannel, &webrtc.DataChannelInit{
Negotiated: &negotiated, ID: &reliableID, Ordered: &ordered,
})
require.NoError(t, err)
handleICEExchange(t, client, server, clientHandler, serverHandler)
connectTransports(t, client, server, clientHandler, serverHandler, false, 1, 1)
// same order as the JS SDK reports channels on resume: lossy first
tm := &TransportManager{
params: TransportManagerParams{Logger: logger.GetLogger()},
publisher: server,
pendingDataChannelsPublisher: []*livekit.DataChannelInfo{
{Label: LossyDataChannel, Id: uint32(lossyID), Target: livekit.SignalTarget_PUBLISHER},
{Label: ReliableDataChannel, Id: uint32(reliableID), Target: livekit.SignalTarget_PUBLISHER},
},
}
tm.ProcessPendingPublisherDataChannels()
require.Eventually(t, func() bool {
server.lock.RLock()
defer server.lock.RUnlock()
return server.reliableDC != nil
}, 10*time.Second, 10*time.Millisecond, "reliable data channel did not open")
server.lock.RLock()
dc := server.reliableDC.BufferedAmountGetter()
server.lock.RUnlock()
require.True(t, dc.Ordered())
}
func TestICERestartAfterShutdownICE(t *testing.T) {
p := newParticipantForTestWithOpts("test", nil)
defer p.Close(false, types.ParticipantCloseReasonNone, false)
tm := p.TransportManager
require.NoError(t, tm.ICERestart(nil))
tm.PublisherShutdownICE()
require.ErrorIs(t, tm.ICERestart(nil), ErrIceRestartAfterShutdown)
}