Fix simulcast RTX pairing broken by pion/webrtc#3470 (#4800)
pion no longer starts the repair stream reader when a custom BufferFactory is set, so the mid/rid/rsid extensions were never observed and simulcast RTX streams were never paired with their primary streams. Extract the extensions on the buffer write path instead. Migrated publishers send no extensions at all, so pair those from SimTracks. Adds an integration test covering both paths, and moves the vnet setup it shares with the downtrack test into pkg/testutils/vnettest. Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
763ea15293
commit
bbdfd50d62
@@ -530,6 +530,8 @@ func newPeerConnection(
|
||||
params.Logger.Debugw("rtx pair found from extension", "repair", repair, "base", base, "rsid", rsid)
|
||||
params.Config.BufferFactory.SetRTXPair(repair, base, rsid)
|
||||
},
|
||||
params.Config.BufferFactory,
|
||||
params.SimTracks,
|
||||
params.Logger,
|
||||
)
|
||||
// put rtx interceptor behind unhandle simulcast interceptor so it can get the correct mid & rid
|
||||
|
||||
@@ -72,6 +72,24 @@ type Buffer struct {
|
||||
|
||||
primaryBufferForRTX *Buffer
|
||||
rtxPktBuf []byte
|
||||
|
||||
streamInfoProbe *StreamInfoProbe
|
||||
warnedPendingOverflow bool
|
||||
}
|
||||
|
||||
// StreamInfoProbe identifies a stream from the mid/rid/rsid header extensions of its
|
||||
// packets. It runs on the write path, i. e. as SRTP pushes into this buffer, because
|
||||
// nothing reads remote streams through pion's interceptor chain.
|
||||
type StreamInfoProbe struct {
|
||||
MidExtID uint8
|
||||
RidExtID uint8
|
||||
RsidExtID uint8
|
||||
|
||||
// Tries bounds how many packets are inspected before giving up.
|
||||
Tries int
|
||||
|
||||
// OnFound is called at most once, in a goroutine, as it can re-enter this buffer.
|
||||
OnFound func(ssrc uint32, mid, rid, rsid string)
|
||||
}
|
||||
|
||||
func NewBuffer(ssrc uint32, maxVideoPkts, maxAudioPkts int) *Buffer {
|
||||
@@ -166,6 +184,10 @@ func (b *Buffer) Write(pkt []byte) (n int, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
if b.streamInfoProbe != nil {
|
||||
b.probeStreamInfoLocked(&rtpPacket)
|
||||
}
|
||||
|
||||
// handle RTX packet
|
||||
if pb := b.primaryBufferForRTX; pb != nil {
|
||||
b.Unlock()
|
||||
@@ -191,6 +213,17 @@ func (b *Buffer) Write(pkt []byte) (n int, err error) {
|
||||
overflow := len(b.pPackets) - max(b.BufferBase.MaxVideoPkts(), b.BufferBase.MaxAudioPkts())
|
||||
if overflow > 0 {
|
||||
startIdx = overflow
|
||||
|
||||
// a stream that keeps arriving but never binds drops every packet from here
|
||||
// on; for an RTX stream it means the pairing was never established
|
||||
if !b.warnedPendingOverflow {
|
||||
b.warnedPendingOverflow = true
|
||||
b.logger.Warnw(
|
||||
"unbound buffer overflowing, dropping packets", nil,
|
||||
"ssrc", b.BufferBase.SSRC(),
|
||||
"pending", len(b.pPackets),
|
||||
)
|
||||
}
|
||||
}
|
||||
b.pPackets = append(b.pPackets[startIdx:], pendingPacket{
|
||||
packet: packet,
|
||||
@@ -213,6 +246,59 @@ func (b *Buffer) Write(pkt []byte) (n int, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// SetStreamInfoProbe installs probe and runs it over packets already queued.
|
||||
func (b *Buffer) SetStreamInfoProbe(probe *StreamInfoProbe) {
|
||||
b.Lock()
|
||||
defer b.Unlock()
|
||||
|
||||
b.streamInfoProbe = probe
|
||||
|
||||
for _, pp := range b.pPackets {
|
||||
if b.streamInfoProbe == nil {
|
||||
return
|
||||
}
|
||||
|
||||
var rtpPacket rtp.Packet
|
||||
if err := rtpPacket.Unmarshal(pp.packet); err != nil {
|
||||
continue
|
||||
}
|
||||
b.probeStreamInfoLocked(&rtpPacket)
|
||||
}
|
||||
}
|
||||
|
||||
// probeStreamInfoLocked inspects one packet, clearing the probe once the stream is
|
||||
// identified or the try budget runs out.
|
||||
func (b *Buffer) probeStreamInfoLocked(rtpPacket *rtp.Packet) {
|
||||
probe := b.streamInfoProbe
|
||||
|
||||
var mid, rid, rsid string
|
||||
if ext := rtpPacket.GetExtension(probe.MidExtID); ext != nil {
|
||||
mid = string(ext)
|
||||
}
|
||||
if ext := rtpPacket.GetExtension(probe.RidExtID); ext != nil {
|
||||
rid = string(ext)
|
||||
}
|
||||
if ext := rtpPacket.GetExtension(probe.RsidExtID); ext != nil {
|
||||
rsid = string(ext)
|
||||
}
|
||||
|
||||
if mid != "" && (rid != "" || rsid != "") {
|
||||
b.streamInfoProbe = nil
|
||||
b.logger.Debugw("stream found", "ssrc", rtpPacket.SSRC, "mid", mid, "rid", rid, "rsid", rsid)
|
||||
go probe.OnFound(rtpPacket.SSRC, mid, rid, rsid)
|
||||
return
|
||||
}
|
||||
|
||||
// ignore padding only packets for probe count
|
||||
if rtpPacket.Padding && len(rtpPacket.Payload) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if probe.Tries--; probe.Tries <= 0 {
|
||||
b.streamInfoProbe = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Buffer) SetPrimaryBufferForRTX(primaryBuffer *Buffer) {
|
||||
b.Lock()
|
||||
b.primaryBufferForRTX = primaryBuffer
|
||||
|
||||
@@ -118,6 +118,21 @@ func (f *Factory) GetRTCPReader(ssrc uint32) *RTCPReader {
|
||||
return f.rtcpReaders[ssrc]
|
||||
}
|
||||
|
||||
// SetStreamInfoProbe installs probe on the buffer of ssrc, reporting whether that
|
||||
// buffer exists. False means the stream can never be identified.
|
||||
func (f *Factory) SetStreamInfoProbe(ssrc uint32, probe *StreamInfoProbe) bool {
|
||||
f.RLock()
|
||||
buffer := f.rtpBuffers[ssrc]
|
||||
f.RUnlock()
|
||||
|
||||
if buffer == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
buffer.SetStreamInfoProbe(probe)
|
||||
return true
|
||||
}
|
||||
|
||||
func (f *Factory) SetRTXPair(repair, base uint32, rsid string) {
|
||||
f.Lock()
|
||||
repairBuffer, baseBuffer := f.rtpBuffers[repair], f.rtpBuffers[base]
|
||||
|
||||
@@ -45,12 +45,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/pion/interceptor"
|
||||
"github.com/pion/logging"
|
||||
"github.com/pion/rtcp"
|
||||
"github.com/pion/rtp"
|
||||
"github.com/pion/sdp/v3"
|
||||
"github.com/pion/transport/v4/vnet"
|
||||
"github.com/pion/webrtc/v4"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
@@ -70,165 +66,9 @@ import (
|
||||
"github.com/livekit/livekit-server/pkg/sfu/sfufakes"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/streamallocator"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/testutils"
|
||||
"github.com/livekit/livekit-server/pkg/testutils/vnettest"
|
||||
)
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// vnet harness
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
type vnetHarness struct {
|
||||
wan *vnet.Router
|
||||
offerNet *vnet.Net
|
||||
answerNet *vnet.Net
|
||||
}
|
||||
|
||||
func buildVNet(t *testing.T) *vnetHarness {
|
||||
t.Helper()
|
||||
|
||||
wan, err := vnet.NewRouter(&vnet.RouterConfig{
|
||||
CIDR: "1.2.3.0/24",
|
||||
LoggerFactory: logging.NewDefaultLoggerFactory(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
offerNet, err := vnet.NewNet(&vnet.NetConfig{StaticIPs: []string{"1.2.3.4"}})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, wan.AddNet(offerNet))
|
||||
|
||||
answerNet, err := vnet.NewNet(&vnet.NetConfig{StaticIPs: []string{"1.2.3.5"}})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, wan.AddNet(answerNet))
|
||||
|
||||
require.NoError(t, wan.Start())
|
||||
t.Cleanup(func() { _ = wan.Stop() })
|
||||
|
||||
return &vnetHarness{wan: wan, offerNet: offerNet, answerNet: answerNet}
|
||||
}
|
||||
|
||||
// mediaEngineConfig describes what codecs / header extensions to register on a PC.
|
||||
type mediaEngineConfig struct {
|
||||
video bool // register VP8 (+ RTX); otherwise register opus
|
||||
headerExtensions bool // register abs-send-time + transport-cc header extensions
|
||||
}
|
||||
|
||||
func newMediaPC(t *testing.T, net *vnet.Net, factory *buffer.Factory, cfg mediaEngineConfig) *webrtc.PeerConnection {
|
||||
t.Helper()
|
||||
|
||||
me := &webrtc.MediaEngine{}
|
||||
if cfg.video {
|
||||
require.NoError(t, me.RegisterCodec(webrtc.RTPCodecParameters{
|
||||
RTPCodecCapability: webrtc.RTPCodecCapability{
|
||||
MimeType: webrtc.MimeTypeVP8, ClockRate: 90000, RTCPFeedback: videoRTCPFeedback(),
|
||||
},
|
||||
PayloadType: 96,
|
||||
}, webrtc.RTPCodecTypeVideo))
|
||||
require.NoError(t, me.RegisterCodec(webrtc.RTPCodecParameters{
|
||||
RTPCodecCapability: webrtc.RTPCodecCapability{
|
||||
MimeType: webrtc.MimeTypeRTX, ClockRate: 90000, SDPFmtpLine: "apt=96",
|
||||
},
|
||||
PayloadType: 97,
|
||||
}, webrtc.RTPCodecTypeVideo))
|
||||
} else {
|
||||
require.NoError(t, me.RegisterCodec(webrtc.RTPCodecParameters{
|
||||
RTPCodecCapability: webrtc.RTPCodecCapability{
|
||||
MimeType: webrtc.MimeTypeOpus, ClockRate: 48000, Channels: 2,
|
||||
},
|
||||
PayloadType: 111,
|
||||
}, webrtc.RTPCodecTypeAudio))
|
||||
}
|
||||
|
||||
if cfg.headerExtensions {
|
||||
kind := webrtc.RTPCodecTypeAudio
|
||||
if cfg.video {
|
||||
kind = webrtc.RTPCodecTypeVideo
|
||||
}
|
||||
require.NoError(t, me.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: sdp.ABSSendTimeURI}, kind))
|
||||
require.NoError(t, me.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: sdp.TransportCCURI}, kind))
|
||||
}
|
||||
|
||||
// no pion default interceptors: the DownTrack/pacer fill abs-send-time and
|
||||
// transport-cc themselves, and the tests drive RTCP feedback synthetically, so
|
||||
// pion's own feedback generators would only add nondeterminism.
|
||||
ir := &interceptor.Registry{}
|
||||
|
||||
se := webrtc.SettingEngine{}
|
||||
se.SetNet(net)
|
||||
se.SetICETimeouts(time.Second, time.Second, 200*time.Millisecond)
|
||||
se.SetNetworkTypes([]webrtc.NetworkType{webrtc.NetworkTypeUDP4})
|
||||
if factory != nil {
|
||||
se.BufferFactory = factory.GetOrNew
|
||||
}
|
||||
|
||||
api := webrtc.NewAPI(
|
||||
webrtc.WithMediaEngine(me),
|
||||
webrtc.WithInterceptorRegistry(ir),
|
||||
webrtc.WithSettingEngine(se),
|
||||
)
|
||||
pc, err := api.NewPeerConnection(webrtc.Configuration{})
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = pc.Close() })
|
||||
|
||||
return pc
|
||||
}
|
||||
|
||||
func videoRTCPFeedback() []webrtc.RTCPFeedback {
|
||||
return []webrtc.RTCPFeedback{
|
||||
{Type: "nack"},
|
||||
{Type: "nack", Parameter: "pli"},
|
||||
{Type: webrtc.TypeRTCPFBTransportCC},
|
||||
{Type: webrtc.TypeRTCPFBGoogREMB},
|
||||
}
|
||||
}
|
||||
|
||||
// signalPair performs a full offer/answer exchange between two PCs (adapted from
|
||||
// pion's own test helper) and waits for both to reach the connected state.
|
||||
func signalPair(t *testing.T, offerer, answerer *webrtc.PeerConnection) {
|
||||
t.Helper()
|
||||
|
||||
connected := untilConnected(offerer, answerer)
|
||||
|
||||
offer, err := offerer.CreateOffer(nil)
|
||||
require.NoError(t, err)
|
||||
gatherOffer := webrtc.GatheringCompletePromise(offerer)
|
||||
require.NoError(t, offerer.SetLocalDescription(offer))
|
||||
<-gatherOffer
|
||||
|
||||
require.NoError(t, answerer.SetRemoteDescription(*offerer.LocalDescription()))
|
||||
|
||||
answer, err := answerer.CreateAnswer(nil)
|
||||
require.NoError(t, err)
|
||||
gatherAnswer := webrtc.GatheringCompletePromise(answerer)
|
||||
require.NoError(t, answerer.SetLocalDescription(answer))
|
||||
<-gatherAnswer
|
||||
|
||||
require.NoError(t, offerer.SetRemoteDescription(*answerer.LocalDescription()))
|
||||
|
||||
select {
|
||||
case <-connected:
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatal("timed out waiting for peer connections to connect")
|
||||
}
|
||||
}
|
||||
|
||||
func untilConnected(pcs ...*webrtc.PeerConnection) <-chan struct{} {
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(len(pcs))
|
||||
for _, pc := range pcs {
|
||||
var once sync.Once
|
||||
pc.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
|
||||
if s == webrtc.PeerConnectionStateConnected {
|
||||
once.Do(wg.Done)
|
||||
}
|
||||
})
|
||||
}
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(done)
|
||||
}()
|
||||
return done
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// packet capture on the far side
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -320,7 +160,7 @@ var (
|
||||
PayloadType: 111,
|
||||
}
|
||||
vp8CodecParams = webrtc.RTPCodecParameters{
|
||||
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8, ClockRate: 90000, RTCPFeedback: videoRTCPFeedback()},
|
||||
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8, ClockRate: 90000, RTCPFeedback: vnettest.VideoRTCPFeedback()},
|
||||
PayloadType: 96,
|
||||
}
|
||||
)
|
||||
@@ -336,11 +176,11 @@ type downTrackHarness struct {
|
||||
|
||||
// newBoundDownTrack builds a real DownTrack, attaches it to a real sender PC,
|
||||
// negotiates with a real subscriber PC over vnet, and makes it writable.
|
||||
func newBoundDownTrack(t *testing.T, h *vnetHarness, factory *buffer.Factory, codecParams webrtc.RTPCodecParameters, p pacer.Pacer, meCfg mediaEngineConfig) *downTrackHarness {
|
||||
func newBoundDownTrack(t *testing.T, h *vnettest.Hosts, factory *buffer.Factory, codecParams webrtc.RTPCodecParameters, p pacer.Pacer, meCfg vnettest.MediaEngineConfig) *downTrackHarness {
|
||||
t.Helper()
|
||||
|
||||
sender := newMediaPC(t, h.offerNet, factory, meCfg)
|
||||
sub := newMediaPC(t, h.answerNet, factory, meCfg)
|
||||
sender := vnettest.NewPeerConnection(t, vnettest.PCConfig{Net: h.OfferNet, MediaEngine: meCfg, BufferFactory: factory.GetOrNew})
|
||||
sub := vnettest.NewPeerConnection(t, vnettest.PCConfig{Net: h.AnswerNet, MediaEngine: meCfg, BufferFactory: factory.GetOrNew})
|
||||
capture := captureTrack(sub)
|
||||
|
||||
rcv := newFakeTrackReceiver(codecParams)
|
||||
@@ -361,7 +201,7 @@ func newBoundDownTrack(t *testing.T, h *vnetHarness, factory *buffer.Factory, co
|
||||
require.NoError(t, err)
|
||||
dt.SetTransceiver(tr)
|
||||
|
||||
signalPair(t, sender, sub)
|
||||
vnettest.SignalPair(t, sender, sub)
|
||||
dt.SetConnected()
|
||||
|
||||
require.Eventually(t, func() bool {
|
||||
@@ -393,10 +233,10 @@ func distinctivePayload(seed byte, n int) []byte {
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
func TestPionVNetForwardingSpike(t *testing.T) {
|
||||
h := buildVNet(t)
|
||||
h := vnettest.NewHosts(t)
|
||||
|
||||
sender := newMediaPC(t, h.offerNet, nil, mediaEngineConfig{video: true})
|
||||
receiver := newMediaPC(t, h.answerNet, nil, mediaEngineConfig{video: true})
|
||||
sender := vnettest.NewPeerConnection(t, vnettest.PCConfig{Net: h.OfferNet, MediaEngine: vnettest.MediaEngineConfig{Video: true}})
|
||||
receiver := vnettest.NewPeerConnection(t, vnettest.PCConfig{Net: h.AnswerNet, MediaEngine: vnettest.MediaEngineConfig{Video: true}})
|
||||
|
||||
track, err := webrtc.NewTrackLocalStaticRTP(
|
||||
webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8, ClockRate: 90000},
|
||||
@@ -408,7 +248,7 @@ func TestPionVNetForwardingSpike(t *testing.T) {
|
||||
|
||||
cap := captureTrack(receiver)
|
||||
|
||||
signalPair(t, sender, receiver)
|
||||
vnettest.SignalPair(t, sender, receiver)
|
||||
|
||||
// pump a few RTP packets from the sender track
|
||||
go func() {
|
||||
@@ -445,12 +285,12 @@ func TestPionVNetForwardingSpike(t *testing.T) {
|
||||
// contiguous sequence numbers, timestamps, payload bytes, and that the padding bit
|
||||
// is cleared regardless of the source packet's padding bit.
|
||||
func TestDownTrackForwardsMedia(t *testing.T) {
|
||||
h := buildVNet(t)
|
||||
h := vnettest.NewHosts(t)
|
||||
factory := buffer.NewFactoryOfBufferFactory(500, 500).CreateBufferFactory()
|
||||
p := pacer.NewPassThrough(logger.GetLogger(), newNullBWE())
|
||||
t.Cleanup(p.Stop)
|
||||
|
||||
dh := newBoundDownTrack(t, h, factory, opusCodecParams, p, mediaEngineConfig{video: false})
|
||||
dh := newBoundDownTrack(t, h, factory, opusCodecParams, p, vnettest.MediaEngineConfig{})
|
||||
|
||||
const numPackets = 20
|
||||
sn := uint16(23333)
|
||||
@@ -517,12 +357,12 @@ func TestDownTrackForwardsMedia(t *testing.T) {
|
||||
// type, sequence number, timestamp, payload bytes, and that the padding bit is
|
||||
// cleared regardless of the source packet's padding bit.
|
||||
func TestDownTrackRetransmitsPacketsAsIs(t *testing.T) {
|
||||
h := buildVNet(t)
|
||||
h := vnettest.NewHosts(t)
|
||||
factory := buffer.NewFactoryOfBufferFactory(500, 500).CreateBufferFactory()
|
||||
p := pacer.NewPassThrough(logger.GetLogger(), newNullBWE())
|
||||
t.Cleanup(p.Stop)
|
||||
|
||||
dh := newBoundDownTrack(t, h, factory, opusCodecParams, p, mediaEngineConfig{video: false})
|
||||
dh := newBoundDownTrack(t, h, factory, opusCodecParams, p, vnettest.MediaEngineConfig{})
|
||||
|
||||
const numPackets = 10
|
||||
targetSN := uint16(40000)
|
||||
@@ -597,13 +437,13 @@ func TestDownTrackRetransmitsPacketsAsIs(t *testing.T) {
|
||||
// fields are not observable on the far side. The packets are still handed to the real
|
||||
// pacer (and written over pion).
|
||||
func TestDownTrackRetransmitsPacketsViaRTX(t *testing.T) {
|
||||
h := buildVNet(t)
|
||||
h := vnettest.NewHosts(t)
|
||||
factory := buffer.NewFactoryOfBufferFactory(500, 500).CreateBufferFactory()
|
||||
cp := &capturingPacer{inner: pacer.NewPassThrough(logger.GetLogger(), newNullBWE())}
|
||||
t.Cleanup(cp.Stop)
|
||||
|
||||
// VP8 registers an RTX codec, so the bound DownTrack has an RTX SSRC / payload type.
|
||||
dh := newBoundDownTrack(t, h, factory, vp8CodecParams, cp, mediaEngineConfig{video: true})
|
||||
dh := newBoundDownTrack(t, h, factory, vp8CodecParams, cp, vnettest.MediaEngineConfig{Video: true})
|
||||
require.NotZero(t, dh.dt.SSRCRTX(), "RTX ssrc should be negotiated")
|
||||
require.NotZero(t, dh.dt.PayloadTypeRTXForTest(), "RTX payload type should be negotiated")
|
||||
|
||||
@@ -680,12 +520,12 @@ func TestDownTrackRetransmitsPacketsViaRTX(t *testing.T) {
|
||||
// bytes removed, i. e. the retransmitted media payload is byte identical to the
|
||||
// original transmission.
|
||||
func TestDownTrackReplaysPacketTrailerStripOnRetransmit(t *testing.T) {
|
||||
h := buildVNet(t)
|
||||
h := vnettest.NewHosts(t)
|
||||
factory := buffer.NewFactoryOfBufferFactory(500, 500).CreateBufferFactory()
|
||||
cp := &capturingPacer{inner: pacer.NewPassThrough(logger.GetLogger(), newNullBWE())}
|
||||
t.Cleanup(cp.Stop)
|
||||
|
||||
dh := newBoundDownTrack(t, h, factory, vp8CodecParams, cp, mediaEngineConfig{video: true})
|
||||
dh := newBoundDownTrack(t, h, factory, vp8CodecParams, cp, vnettest.MediaEngineConfig{Video: true})
|
||||
require.NotZero(t, dh.dt.SSRCRTX(), "RTX ssrc should be negotiated")
|
||||
|
||||
video := distinctivePayload(11, 40)
|
||||
@@ -749,12 +589,12 @@ func lktsTrailer() []byte {
|
||||
// and reports both the padding bit and the padding size, so the received payload
|
||||
// length equals the declared padding size (RTPPaddingMaxPayloadSize).
|
||||
func TestDownTrackSendsPaddingOnlyPackets(t *testing.T) {
|
||||
h := buildVNet(t)
|
||||
h := vnettest.NewHosts(t)
|
||||
factory := buffer.NewFactoryOfBufferFactory(500, 500).CreateBufferFactory()
|
||||
p := pacer.NewPassThrough(logger.GetLogger(), newNullBWE())
|
||||
t.Cleanup(p.Stop)
|
||||
|
||||
dh := newBoundDownTrack(t, h, factory, vp8CodecParams, p, mediaEngineConfig{video: true})
|
||||
dh := newBoundDownTrack(t, h, factory, vp8CodecParams, p, vnettest.MediaEngineConfig{Video: true})
|
||||
|
||||
// force a valid target/current layer so the video forwarder will forward (test seam
|
||||
// also used by forwarder_test.go's disable()).
|
||||
@@ -971,7 +811,7 @@ func TestDownTrackSendsProbePackets(t *testing.T) {
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
h := buildVNet(t)
|
||||
h := vnettest.NewHosts(t)
|
||||
factory := buffer.NewFactoryOfBufferFactory(500, 500).CreateBufferFactory()
|
||||
|
||||
b := tc.makeBWE()
|
||||
@@ -981,9 +821,9 @@ func TestDownTrackSendsProbePackets(t *testing.T) {
|
||||
// header extensions (abs-send-time for remote BWE, transport-cc for send-side)
|
||||
// are needed for the DownTrack to pick up an ext id, which WriteProbePackets
|
||||
// requires.
|
||||
dh := newBoundDownTrack(t, h, factory, vp8CodecParams, cp, mediaEngineConfig{
|
||||
video: true,
|
||||
headerExtensions: true,
|
||||
dh := newBoundDownTrack(t, h, factory, vp8CodecParams, cp, vnettest.MediaEngineConfig{
|
||||
Video: true,
|
||||
HeaderExtensions: true,
|
||||
})
|
||||
|
||||
require.NotZero(t, dh.dt.SSRCRTX(), "RTX ssrc should be negotiated")
|
||||
|
||||
+67
-63
@@ -21,6 +21,7 @@ import (
|
||||
"github.com/pion/sdp/v3"
|
||||
"github.com/pion/webrtc/v4"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/sfu/buffer"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/utils"
|
||||
"github.com/livekit/protocol/logger"
|
||||
)
|
||||
@@ -31,6 +32,12 @@ const (
|
||||
rtxProbeCount = 10
|
||||
)
|
||||
|
||||
// StreamInfoProber installs a bounded probe that identifies a remote stream from the
|
||||
// mid/rid/rsid header extensions of its packets. Implemented by buffer.Factory.
|
||||
type StreamInfoProber interface {
|
||||
SetStreamInfoProbe(ssrc uint32, probe *buffer.StreamInfoProbe) bool
|
||||
}
|
||||
|
||||
type streamInfo struct {
|
||||
mid string
|
||||
rid string
|
||||
@@ -40,22 +47,51 @@ type streamInfo struct {
|
||||
type RTXInfoExtractorFactory struct {
|
||||
onStreamFound func(*interceptor.StreamInfo)
|
||||
onRTXPairFound func(repair, base uint32, rsid string)
|
||||
prober StreamInfoProber
|
||||
lock sync.Mutex
|
||||
streams map[uint32]streamInfo
|
||||
paired map[uint32]struct{}
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
func NewRTXInfoExtractorFactory(
|
||||
onStreamFound func(*interceptor.StreamInfo),
|
||||
onRTXPairFound func(repair, base uint32, rsid string),
|
||||
prober StreamInfoProber,
|
||||
simTracks map[uint32]SimulcastTrackInfo,
|
||||
logger logger.Logger,
|
||||
) *RTXInfoExtractorFactory {
|
||||
return &RTXInfoExtractorFactory{
|
||||
f := &RTXInfoExtractorFactory{
|
||||
onStreamFound: onStreamFound,
|
||||
onRTXPairFound: onRTXPairFound,
|
||||
prober: prober,
|
||||
streams: make(map[uint32]streamInfo),
|
||||
paired: make(map[uint32]struct{}),
|
||||
logger: logger,
|
||||
}
|
||||
f.seedSimulcastTracks(simTracks)
|
||||
return f
|
||||
}
|
||||
|
||||
// seedSimulcastTracks pairs migrated streams from the migration info. A migrated client
|
||||
// is mid-stream and stops sending rid/rsid, so the extensions never appear on the wire
|
||||
// and the pairing has to come from what is already known about the tracks.
|
||||
func (f *RTXInfoExtractorFactory) seedSimulcastTracks(simTracks map[uint32]SimulcastTrackInfo) {
|
||||
for ssrc, info := range simTracks {
|
||||
if info.Mid == "" || info.StreamID == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if info.IsRepairStream {
|
||||
f.SetStreamInfo(ssrc, info.Mid, "", info.StreamID)
|
||||
continue
|
||||
}
|
||||
|
||||
f.SetStreamInfo(ssrc, info.Mid, info.StreamID, "")
|
||||
if info.RepairSSRC != 0 {
|
||||
f.SetStreamInfo(info.RepairSSRC, info.Mid, "", info.StreamID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (f *RTXInfoExtractorFactory) NewInterceptor(id string) (interceptor.Interceptor, error) {
|
||||
@@ -75,6 +111,12 @@ func (f *RTXInfoExtractorFactory) SetStreamInfo(ssrc uint32, mid, rid, rsid stri
|
||||
return
|
||||
}
|
||||
|
||||
// the same stream can be reported by both the packet probe and the migration info
|
||||
if _, ok := f.paired[ssrc]; ok {
|
||||
f.lock.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
if rsid != "" {
|
||||
// repair stream found, find base stream
|
||||
for base, info := range f.streams {
|
||||
@@ -106,13 +148,15 @@ func (f *RTXInfoExtractorFactory) SetStreamInfo(ssrc uint32, mid, rid, rsid stri
|
||||
rid: rid,
|
||||
rsid: rsid,
|
||||
}
|
||||
f.lock.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
f.paired[repairSsrc] = struct{}{}
|
||||
f.paired[baseSsrc] = struct{}{}
|
||||
f.lock.Unlock()
|
||||
|
||||
if repairSsrc != 0 && baseSsrc != 0 {
|
||||
f.onRTXPairFound(repairSsrc, baseSsrc, repairSid)
|
||||
}
|
||||
f.onRTXPairFound(repairSsrc, baseSsrc, repairSid)
|
||||
}
|
||||
|
||||
// ------------------------------------------
|
||||
@@ -134,65 +178,25 @@ func (u *RTXInfoExtractor) BindRemoteStream(info *interceptor.StreamInfo, reader
|
||||
return reader
|
||||
}
|
||||
|
||||
return &rtxInfoReader{
|
||||
tryTimes: rtxProbeCount,
|
||||
reader: reader,
|
||||
midExtID: uint8(midExtensionID),
|
||||
ridExtID: uint8(streamIDExtensionID),
|
||||
rsidExtID: uint8(repairStreamIDExtensionID),
|
||||
factory: u.factory,
|
||||
logger: u.logger,
|
||||
// Probe on the buffer write path rather than by wrapping this reader. Remote streams
|
||||
// are consumed through SettingEngine.BufferFactory, so nothing here reads the
|
||||
// interceptor chain. pion used to drive the repair stream reader, but since
|
||||
// pion/webrtc#3470 it only does so when the application reads the TrackRemote or
|
||||
// when no BufferFactory is set, neither of which holds.
|
||||
ok := u.factory.prober.SetStreamInfoProbe(info.SSRC, &buffer.StreamInfoProbe{
|
||||
MidExtID: uint8(midExtensionID),
|
||||
RidExtID: uint8(streamIDExtensionID),
|
||||
RsidExtID: uint8(repairStreamIDExtensionID),
|
||||
Tries: rtxProbeCount,
|
||||
OnFound: u.factory.SetStreamInfo,
|
||||
})
|
||||
if !ok {
|
||||
u.logger.Warnw(
|
||||
"could not install stream info probe, rtx pairing will not work", nil,
|
||||
"ssrc", info.SSRC,
|
||||
"mime", info.MimeType,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------
|
||||
|
||||
type rtxInfoReader struct {
|
||||
tryTimes int
|
||||
reader interceptor.RTPReader
|
||||
midExtID uint8
|
||||
ridExtID uint8
|
||||
rsidExtID uint8
|
||||
factory *RTXInfoExtractorFactory
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
func (r *rtxInfoReader) Read(b []byte, a interceptor.Attributes) (int, interceptor.Attributes, error) {
|
||||
n, a, err := r.reader.Read(b, a)
|
||||
if r.tryTimes < 0 || err != nil {
|
||||
return n, a, err
|
||||
}
|
||||
|
||||
if a == nil {
|
||||
a = make(interceptor.Attributes)
|
||||
}
|
||||
header, err := a.GetRTPHeader(b[:n])
|
||||
if err != nil {
|
||||
return n, a, nil
|
||||
}
|
||||
|
||||
var mid, rid, rsid string
|
||||
if payload := header.GetExtension(r.midExtID); payload != nil {
|
||||
mid = string(payload)
|
||||
}
|
||||
|
||||
if payload := header.GetExtension(r.ridExtID); payload != nil {
|
||||
rid = string(payload)
|
||||
}
|
||||
|
||||
if payload := header.GetExtension(r.rsidExtID); payload != nil {
|
||||
rsid = string(payload)
|
||||
}
|
||||
|
||||
if mid != "" && (rid != "" || rsid != "") {
|
||||
r.logger.Debugw("stream found", "mid", mid, "rid", rid, "rsid", rsid, "ssrc", header.SSRC)
|
||||
r.tryTimes = -1
|
||||
go r.factory.SetStreamInfo(header.SSRC, mid, rid, rsid)
|
||||
} else {
|
||||
// ignore padding only packet for probe count
|
||||
if !header.Padding || n-header.MarshalSize()-int(b[n-1]) != 0 {
|
||||
r.tryTimes--
|
||||
}
|
||||
}
|
||||
return n, a, nil
|
||||
return reader
|
||||
}
|
||||
|
||||
@@ -0,0 +1,231 @@
|
||||
// Copyright 2026 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 vnettest sets up real pion peer connections on an in-memory virtual
|
||||
// network, for integration tests that exercise media paths without a server.
|
||||
//
|
||||
// It depends only on pion, so it can be imported both by tests inside pkg/... and by
|
||||
// the top level test package.
|
||||
package vnettest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/pion/interceptor"
|
||||
"github.com/pion/logging"
|
||||
"github.com/pion/sdp/v3"
|
||||
"github.com/pion/transport/v4/packetio"
|
||||
"github.com/pion/transport/v4/vnet"
|
||||
"github.com/pion/webrtc/v4"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const (
|
||||
VP8PayloadType = 96
|
||||
RTXPayloadType = VP8PayloadType + 1
|
||||
OpusPayloadType = 111
|
||||
)
|
||||
|
||||
// Hosts are the two ends of a started virtual network.
|
||||
type Hosts struct {
|
||||
OfferNet *vnet.Net
|
||||
AnswerNet *vnet.Net
|
||||
}
|
||||
|
||||
// NewHosts returns two hosts on a started virtual network, torn down with the test.
|
||||
func NewHosts(t *testing.T) *Hosts {
|
||||
t.Helper()
|
||||
|
||||
wan, err := vnet.NewRouter(&vnet.RouterConfig{
|
||||
CIDR: "1.2.3.0/24",
|
||||
LoggerFactory: logging.NewDefaultLoggerFactory(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
offerNet, err := vnet.NewNet(&vnet.NetConfig{StaticIPs: []string{"1.2.3.4"}})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, wan.AddNet(offerNet))
|
||||
|
||||
answerNet, err := vnet.NewNet(&vnet.NetConfig{StaticIPs: []string{"1.2.3.5"}})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, wan.AddNet(answerNet))
|
||||
|
||||
require.NoError(t, wan.Start())
|
||||
t.Cleanup(func() { _ = wan.Stop() })
|
||||
|
||||
return &Hosts{OfferNet: offerNet, AnswerNet: answerNet}
|
||||
}
|
||||
|
||||
// NewSettingEngine returns a setting engine bound to net, with ICE timeouts short
|
||||
// enough to keep tests quick.
|
||||
func NewSettingEngine(net *vnet.Net) webrtc.SettingEngine {
|
||||
se := webrtc.SettingEngine{}
|
||||
se.SetNet(net)
|
||||
se.SetNetworkTypes([]webrtc.NetworkType{webrtc.NetworkTypeUDP4})
|
||||
se.SetICETimeouts(5*time.Second, 5*time.Second, 500*time.Millisecond)
|
||||
return se
|
||||
}
|
||||
|
||||
// MediaEngineConfig describes what to register on a media engine.
|
||||
type MediaEngineConfig struct {
|
||||
Video bool // VP8 and its RTX codec; otherwise opus
|
||||
HeaderExtensions bool // abs-send-time + transport-cc
|
||||
SimulcastExtensions bool // mid + rid + rsid
|
||||
}
|
||||
|
||||
func VideoRTCPFeedback() []webrtc.RTCPFeedback {
|
||||
return []webrtc.RTCPFeedback{
|
||||
{Type: webrtc.TypeRTCPFBNACK},
|
||||
{Type: webrtc.TypeRTCPFBNACK, Parameter: "pli"},
|
||||
{Type: webrtc.TypeRTCPFBTransportCC},
|
||||
{Type: webrtc.TypeRTCPFBGoogREMB},
|
||||
}
|
||||
}
|
||||
|
||||
func NewMediaEngine(t *testing.T, cfg MediaEngineConfig) *webrtc.MediaEngine {
|
||||
t.Helper()
|
||||
|
||||
me := &webrtc.MediaEngine{}
|
||||
kind := webrtc.RTPCodecTypeAudio
|
||||
if cfg.Video {
|
||||
kind = webrtc.RTPCodecTypeVideo
|
||||
|
||||
require.NoError(t, me.RegisterCodec(webrtc.RTPCodecParameters{
|
||||
RTPCodecCapability: webrtc.RTPCodecCapability{
|
||||
MimeType: webrtc.MimeTypeVP8, ClockRate: 90000, RTCPFeedback: VideoRTCPFeedback(),
|
||||
},
|
||||
PayloadType: VP8PayloadType,
|
||||
}, kind))
|
||||
require.NoError(t, me.RegisterCodec(webrtc.RTPCodecParameters{
|
||||
RTPCodecCapability: webrtc.RTPCodecCapability{
|
||||
MimeType: webrtc.MimeTypeRTX,
|
||||
ClockRate: 90000,
|
||||
SDPFmtpLine: fmt.Sprintf("apt=%d", VP8PayloadType),
|
||||
},
|
||||
PayloadType: RTXPayloadType,
|
||||
}, kind))
|
||||
} else {
|
||||
require.NoError(t, me.RegisterCodec(webrtc.RTPCodecParameters{
|
||||
RTPCodecCapability: webrtc.RTPCodecCapability{
|
||||
MimeType: webrtc.MimeTypeOpus, ClockRate: 48000, Channels: 2,
|
||||
},
|
||||
PayloadType: OpusPayloadType,
|
||||
}, kind))
|
||||
}
|
||||
|
||||
if cfg.HeaderExtensions {
|
||||
require.NoError(t, me.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: sdp.ABSSendTimeURI}, kind))
|
||||
require.NoError(t, me.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: sdp.TransportCCURI}, kind))
|
||||
}
|
||||
if cfg.SimulcastExtensions {
|
||||
require.NoError(t, webrtc.ConfigureSimulcastExtensionHeaders(me))
|
||||
}
|
||||
|
||||
return me
|
||||
}
|
||||
|
||||
// PCConfig describes a peer connection on the virtual network.
|
||||
type PCConfig struct {
|
||||
Net *vnet.Net
|
||||
MediaEngine MediaEngineConfig
|
||||
|
||||
// BufferFactory is SettingEngine.BufferFactory, e. g. buffer.Factory.GetOrNew.
|
||||
// Optional.
|
||||
BufferFactory func(packetType packetio.BufferPacketType, ssrc uint32) io.ReadWriteCloser
|
||||
}
|
||||
|
||||
// NewPeerConnection builds a peer connection on the virtual network with no
|
||||
// interceptors, so nothing rewrites what a test puts on the wire.
|
||||
func NewPeerConnection(t *testing.T, cfg PCConfig) *webrtc.PeerConnection {
|
||||
t.Helper()
|
||||
|
||||
se := NewSettingEngine(cfg.Net)
|
||||
se.BufferFactory = cfg.BufferFactory
|
||||
|
||||
api := webrtc.NewAPI(
|
||||
webrtc.WithMediaEngine(NewMediaEngine(t, cfg.MediaEngine)),
|
||||
webrtc.WithSettingEngine(se),
|
||||
webrtc.WithInterceptorRegistry(&interceptor.Registry{}),
|
||||
)
|
||||
|
||||
pc, err := api.NewPeerConnection(webrtc.Configuration{})
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = pc.Close() })
|
||||
|
||||
return pc
|
||||
}
|
||||
|
||||
// GatheredOffer creates an offer and waits for gathering, so the SDP carries every
|
||||
// candidate and the caller needs no trickle.
|
||||
func GatheredOffer(t *testing.T, pc *webrtc.PeerConnection) webrtc.SessionDescription {
|
||||
t.Helper()
|
||||
|
||||
offer, err := pc.CreateOffer(nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
gathered := webrtc.GatheringCompletePromise(pc)
|
||||
require.NoError(t, pc.SetLocalDescription(offer))
|
||||
<-gathered
|
||||
|
||||
return *pc.LocalDescription()
|
||||
}
|
||||
|
||||
// SignalPair performs a full offer/answer exchange between two peer connections and
|
||||
// waits for both to connect.
|
||||
func SignalPair(t *testing.T, offerer, answerer *webrtc.PeerConnection) {
|
||||
t.Helper()
|
||||
|
||||
connected := UntilConnected(offerer, answerer)
|
||||
|
||||
require.NoError(t, answerer.SetRemoteDescription(GatheredOffer(t, offerer)))
|
||||
|
||||
answer, err := answerer.CreateAnswer(nil)
|
||||
require.NoError(t, err)
|
||||
gathered := webrtc.GatheringCompletePromise(answerer)
|
||||
require.NoError(t, answerer.SetLocalDescription(answer))
|
||||
<-gathered
|
||||
|
||||
require.NoError(t, offerer.SetRemoteDescription(*answerer.LocalDescription()))
|
||||
|
||||
select {
|
||||
case <-connected:
|
||||
case <-time.After(30 * time.Second):
|
||||
t.Fatal("timed out waiting for peer connections to connect")
|
||||
}
|
||||
}
|
||||
|
||||
// UntilConnected closes the returned channel once every peer connection is connected.
|
||||
func UntilConnected(pcs ...*webrtc.PeerConnection) <-chan struct{} {
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(len(pcs))
|
||||
for _, pc := range pcs {
|
||||
var once sync.Once
|
||||
pc.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
|
||||
if s == webrtc.PeerConnectionStateConnected {
|
||||
once.Do(wg.Done)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(done)
|
||||
}()
|
||||
return done
|
||||
}
|
||||
Reference in New Issue
Block a user