mirror of
https://github.com/livekit/livekit.git
synced 2026-11-23 02:29:22 +00:00
* sfu: don't count opus useinbandfec=0 as FEC in the down track DownTrack decides whether opus FEC is on by looking for "fec" anywhere in the fmtp line, both on Bind and when the upstream codec changes. "useinbandfec=0" contains "fec", so a publisher that turns FEC off is scored as if FEC were on. The connection quality scorer then lowers the packet loss weight for opus from 8.0 to 5.33, and a subscriber with 8% audio loss is reported as GOOD instead of POOR. The receiver side already checks for useinbandfec=1, so the publisher's and subscribers' scores disagree about the same stream. Add utils.IsOpusInbandFECEnabled, which reads the useinbandfec parameter by key and treats only a value of 1 as on, and use it for all three FEC checks in the down track. The RED path already looked for useinbandfec=1 and only gains tolerance of spaces around the parameter. * sfu: read FEC from the upstream codec when the down track's codec changes handleUpstreamCodecChange took the FEC flag from the codec the subscriber negotiated, while Bind takes it from the upstream codec. FEC describes what the publisher encodes, so after RED falls back to opus, a publisher with useinbandfec=1 and a subscriber with useinbandfec=0 was scored as having no FEC, and the reverse as having FEC. Decide FEC in one place from the upstream codecs, using the upstream opus codec for RED, and use it in both Bind and the codec change path. Log the flag on codec change as Bind already does. * sfu: use the same useinbandfec=1 check as the RED path for FEC Drop utils.IsOpusInbandFECEnabled and check FEC with strings.Contains(strings.ToLower(fmtp), "useinbandfec=1"), as the RED path already did. * sfu: move the FEC check to utils and match useinbandfec with a regex Move the check to utils.IsFECEnabled, as asked in review, and match useinbandfec=1 as one fmtp parameter with a regex, ignoring case and spaces around the parameter and the "=". A publisher that sends "minptime=10; useinbandfec = 1" was scored as having no FEC. Move the tests to utils/helpers_test.go.
115 lines
4.1 KiB
Go
115 lines
4.1 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 utils
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/pion/webrtc/v4"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/livekit/protocol/codecs/mime"
|
|
)
|
|
|
|
func TestFindRTXPayloadType(t *testing.T) {
|
|
rtx := func(pt webrtc.PayloadType, fmtp string) webrtc.RTPCodecParameters {
|
|
return webrtc.RTPCodecParameters{
|
|
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: mime.MimeTypeRTX.String(), ClockRate: 90000, SDPFmtpLine: fmtp},
|
|
PayloadType: pt,
|
|
}
|
|
}
|
|
vp8 := webrtc.RTPCodecParameters{
|
|
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: mime.MimeTypeVP8.String(), ClockRate: 90000},
|
|
PayloadType: 96,
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
haystack []webrtc.RTPCodecParameters
|
|
expected webrtc.PayloadType
|
|
}{
|
|
{name: "apt only", haystack: []webrtc.RTPCodecParameters{vp8, rtx(97, "apt=96")}, expected: 97},
|
|
{name: "apt with rtx-time", haystack: []webrtc.RTPCodecParameters{vp8, rtx(97, "apt=96;rtx-time=3000")}, expected: 97},
|
|
{name: "rtx-time before apt", haystack: []webrtc.RTPCodecParameters{vp8, rtx(97, "rtx-time=3000; apt=96")}, expected: 97},
|
|
{name: "other codec", haystack: []webrtc.RTPCodecParameters{vp8, rtx(99, "apt=98"), rtx(97, "apt=96")}, expected: 97},
|
|
{name: "apt prefix", haystack: []webrtc.RTPCodecParameters{vp8, rtx(97, "apt=960")}, expected: 0},
|
|
{name: "no rtx", haystack: []webrtc.RTPCodecParameters{vp8}, expected: 0},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
require.Equal(t, tc.expected, FindRTXPayloadType(96, tc.haystack))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIsFECEnabled(t *testing.T) {
|
|
codec := func(mimeType mime.MimeType, fmtp string) webrtc.RTPCodecParameters {
|
|
return webrtc.RTPCodecParameters{
|
|
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: mimeType.String(), SDPFmtpLine: fmtp},
|
|
}
|
|
}
|
|
opusOn := codec(mime.MimeTypeOpus, "minptime=10;useinbandfec=1")
|
|
opusOff := codec(mime.MimeTypeOpus, "minptime=10;useinbandfec=0")
|
|
red := codec(mime.MimeTypeRED, "111/111")
|
|
vp8 := codec(mime.MimeTypeVP8, "")
|
|
|
|
t.Run("fmtp", func(t *testing.T) {
|
|
tests := []struct {
|
|
fmtp string
|
|
expected bool
|
|
}{
|
|
{"minptime=10;useinbandfec=1", true},
|
|
{"useinbandfec=1", true},
|
|
{"useinbandfec=1;stereo=1", true},
|
|
{"minptime=10; useinbandfec = 1", true},
|
|
{"minptime=10; useinbandfec=1 ;stereo=1", true},
|
|
{"minptime=10;UseInbandFEC=1", true},
|
|
{"minptime=10;useinbandfec=0", false},
|
|
{"minptime=10;useinbandfec=10", false},
|
|
{"minptime=10;xuseinbandfec=1", false},
|
|
{"minptime=10", false},
|
|
{"", false},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.fmtp, func(t *testing.T) {
|
|
opus := codec(mime.MimeTypeOpus, tc.fmtp)
|
|
require.Equal(t, tc.expected, IsFECEnabled(opus, []webrtc.RTPCodecParameters{opus}))
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("codecs", func(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
codec webrtc.RTPCodecParameters
|
|
codecs []webrtc.RTPCodecParameters
|
|
expected bool
|
|
}{
|
|
{"opus with FEC", opusOn, []webrtc.RTPCodecParameters{opusOn}, true},
|
|
{"opus without FEC", opusOff, []webrtc.RTPCodecParameters{opusOff}, false},
|
|
{"RED over opus with FEC", red, []webrtc.RTPCodecParameters{red, opusOn}, true},
|
|
{"RED over opus without FEC", red, []webrtc.RTPCodecParameters{red, opusOff}, false},
|
|
{"RED without an opus codec", red, []webrtc.RTPCodecParameters{red}, false},
|
|
{"video", vp8, []webrtc.RTPCodecParameters{vp8}, false},
|
|
{"no codec", webrtc.RTPCodecParameters{}, []webrtc.RTPCodecParameters{opusOn}, false},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
require.Equal(t, tc.expected, IsFECEnabled(tc.codec, tc.codecs))
|
|
})
|
|
}
|
|
})
|
|
}
|