Files
livekit/pkg/rtc/transport_fec_test.go
6342d8fd1b Enable upstream FlexFEC03 (#4845)
* Add upstream FlexFEC recovery support

* Guard short TWCC extensions in FEC packets

* Release completed FlexFEC states

* Validate FlexFEC payload type range

* Invoke FEC callbacks after unlocking

* Reduce FlexFEC protection state allocations

* Avoid allocations for complete FlexFEC packets

* Optimize ordered FlexFEC packet insertion

* Reuse RTP packet storage for FlexFEC recovery

* Limit pending FlexFEC repair packets

* Lower FlexFEC repair window to two seconds

* Harden upstream FlexFEC handling

* Test FlexFEC with E2EE media

* Fix FlexFEC negotiation with libwebrtc publishers

* Enhance payload length validation in decoder

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* remove the ability to configure payload type for FlexFEC

* moved some helpers into protocol

* Handle big gaps in media sequence numbers

Reset buffers when a big gap in media sequence numbers is detected.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* bump protocol & psrpc deps

* Handle FEC packet recovery failure

Increment FEC packets discarded counter and remove packet.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* do not NACK on fec recovered packet (#4896)

* do not NACK on fec recovered packet

* fix test race in CI

* Fix FEC write return after merging RTX feedback changes

* PR fixes for FlexFEC implementation (#4898)

* do not NACK on fec recovered packet

* refactor common code into unlockAndInvokeFECRecovery

* add FEC stats to RTPStats

* add log for when fecdecoder unavailable after creation

* rename markAsFECRepair to setFECRepairBufferLimit for clarity

* refactor packet generation for tests into util

* fix test race in CI

* move the unlocks back inline for readability

* update RTPstats

* Fix FEC write return after merging RTX feedback changes

* Use shared RTP packet helper in merged FEC NACK test

* Remove redundant return from FEC write handler

* packet post process (#5020)

* packet post process

* remove redundant writeRTX

* send out RTCP (NACK and RR) from packetPostProcess only

* handle FEC decoder on receiver restart (#5032)

* handle FEC decoder on receiver restart

* move onStreamRestartLocked into BufferBaseParams

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Raja Subramanian <raja.gobi@tutanota.com>
2026-10-22 13:58:04 -07:00

207 lines
6.0 KiB
Go

// 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 rtc
import (
"testing"
"github.com/pion/sdp/v3"
"github.com/pion/webrtc/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/livekit/livekit-server/pkg/config"
protoCodecs "github.com/livekit/protocol/codecs"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
)
func TestFECPairsFromSDP(t *testing.T) {
offer := `v=0
o=- 8541913762120318441 2 IN IP4 127.0.0.1
s=-
t=0 0
m=video 9 UDP/TLS/RTP/SAVPF 96 97 115
c=IN IP4 0.0.0.0
a=mid:0
a=sendonly
a=rtpmap:96 VP8/90000
a=rtpmap:97 rtx/90000
a=fmtp:97 apt=96
a=rtpmap:115 flexfec-03/90000
a=fmtp:115 repair-window=10000000
a=ssrc-group:FID 1111 2222
a=ssrc-group:FEC-FR 1111 3333
a=ssrc:1111 cname:test
a=ssrc:2222 cname:test
a=ssrc:3333 cname:test
`
parsed := &sdp.SessionDescription{}
require.NoError(t, parsed.Unmarshal([]byte(offer)))
fecPairs := fecPairsFromSDP(parsed, logger.GetLogger())
require.Len(t, fecPairs, 1)
assert.Equal(t, uint32(1111), fecPairs[uint32(3333)])
// FID pairs are not picked up as FEC
rtxPairs := nonSimulcastRTXRepairsFromSDP(parsed, logger.GetLogger())
require.Len(t, rtxPairs, 1)
assert.Equal(t, uint32(1111), rtxPairs[uint32(2222)])
}
func TestFECPairsFromSDPNoGroups(t *testing.T) {
offer := `v=0
o=- 8541913762120318441 2 IN IP4 127.0.0.1
s=-
t=0 0
m=video 9 UDP/TLS/RTP/SAVPF 96
c=IN IP4 0.0.0.0
a=mid:0
a=sendonly
a=rtpmap:96 VP8/90000
a=ssrc:1111 cname:test
`
parsed := &sdp.SessionDescription{}
require.NoError(t, parsed.Unmarshal([]byte(offer)))
assert.Empty(t, fecPairsFromSDP(parsed, logger.GetLogger()))
}
func TestFECPairsFromSDPIgnoresMalformedGroups(t *testing.T) {
description := &sdp.SessionDescription{
MediaDescriptions: []*sdp.MediaDescription{{
Attributes: []sdp.Attribute{
{Key: sdp.AttrKeySSRCGroup},
{Key: sdp.AttrKeySSRCGroup, Value: "FEC-FR 1111"},
{Key: sdp.AttrKeySSRCGroup, Value: "FEC-FR invalid 3333"},
{Key: sdp.AttrKeySSRCGroup, Value: "FEC-FR 1111 invalid"},
{Key: sdp.AttrKeySSRCGroup, Value: "FEC-FR 1111 3333 4444"},
},
}},
}
require.NotPanics(t, func() {
assert.Empty(t, fecPairsFromSDP(description, logger.GetLogger()))
})
}
func TestFECPairsFromSDPHandlesWhitespace(t *testing.T) {
description := &sdp.SessionDescription{
MediaDescriptions: []*sdp.MediaDescription{{
Attributes: []sdp.Attribute{{
Key: sdp.AttrKeySSRCGroup,
Value: " FEC-FR 1111\t3333 ",
}},
}},
}
assert.Equal(t, map[uint32]uint32{3333: 1111}, fecPairsFromSDP(description, logger.GetLogger()))
}
func TestFlexFECPayloadTypeDoesNotCollide(t *testing.T) {
flexFECPayloadType := protoCodecs.FlexFEC03CodecParameters.PayloadType
assert.LessOrEqual(t, flexFECPayloadType, webrtc.PayloadType(127))
for _, codec := range protoCodecs.VideoCodecsParameters {
assert.NotEqual(t, codec.PayloadType, flexFECPayloadType, codec.MimeType)
assert.NotEqual(t, codec.PayloadType+1, flexFECPayloadType, codec.MimeType+" RTX")
}
for _, codec := range []webrtc.RTPCodecParameters{
protoCodecs.OpusCodecParameters,
protoCodecs.RedCodecParameters,
protoCodecs.PCMUCodecParameters,
protoCodecs.PCMACodecParameters,
} {
assert.NotEqual(t, codec.PayloadType, flexFECPayloadType, codec.MimeType)
}
}
func TestMediaEngineRegistersFlexFEC(t *testing.T) {
enabledCodecs := []*livekit.Codec{
{Mime: "video/VP8"},
{Mime: "video/rtx"},
}
for _, test := range []struct {
name string
enabled bool
}{
{name: "disabled", enabled: false},
{name: "enabled", enabled: true},
} {
t.Run(test.name, func(t *testing.T) {
me, err := createMediaEngine(enabledCodecs, DirectionConfig{
FlexFEC: FlexFECDirectionConfig{
Enabled: test.enabled,
},
RTCPFeedback: RTCPFeedbackConfig{
Video: []webrtc.RTCPFeedback{{Type: webrtc.TypeRTCPFBTransportCC}},
},
}, false)
require.NoError(t, err)
pc, err := webrtc.NewAPI(webrtc.WithMediaEngine(me)).NewPeerConnection(webrtc.Configuration{})
require.NoError(t, err)
defer pc.Close()
_, err = pc.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo)
require.NoError(t, err)
offer, err := pc.CreateOffer(nil)
require.NoError(t, err)
flexFECParams := protoCodecs.FlexFEC03CodecParameters
assert.Equal(t, "repair-window=10000000", flexFECParams.SDPFmtpLine)
filtered := filterCodecs(
[]webrtc.RTPCodecParameters{flexFECParams},
enabledCodecs,
RTCPFeedbackConfig{},
false,
test.enabled,
)
if test.enabled {
require.Len(t, filtered, 1)
assert.Equal(t, webrtc.MimeTypeFlexFEC03, filtered[0].MimeType)
assert.Contains(t, offer.SDP, "a=rtpmap:115 flexfec-03/90000")
assert.Contains(t, offer.SDP, "a=fmtp:115 repair-window=10000000")
} else {
assert.Empty(t, filtered)
assert.NotContains(t, offer.SDP, "flexfec-03")
}
})
}
}
func TestWebRTCConfigFlexFEC(t *testing.T) {
newConfig := func(t *testing.T) *config.Config {
t.Helper()
conf, err := config.NewConfig("", true, nil, nil)
require.NoError(t, err)
conf.RTC.TCPPort = 0
return conf
}
t.Run("defaults and publisher updates", func(t *testing.T) {
conf := newConfig(t)
conf.RTC.FlexFEC = config.FlexFECConfig{UpstreamEnabled: true}
webRTCConfig, err := NewWebRTCConfig(conf)
require.NoError(t, err)
assert.Equal(t, FlexFECDirectionConfig{
Enabled: true,
}, webRTCConfig.Publisher.FlexFEC)
assert.False(t, webRTCConfig.Subscriber.FlexFEC.Enabled)
webRTCConfig.UpdatePublisherConfig(true)
assert.True(t, webRTCConfig.Publisher.FlexFEC.Enabled)
})
}