Files
livekit-3d/pkg/sfu/utils/downtrackspreader_test.go
T
Raja SubramanianandClaude Fable 5.1 48700da3f3 sfu: broadcast RTP to down tracks without per-packet allocations (#4875)
* sfu: broadcast RTP to down tracks without a per-packet closure

Every forwarded packet allocated a closure capturing the packet and layer,
and an atomic write counter that escaped with it, even for tracks with a
single subscriber. Add BroadcastRTP, which carries the packet and layer in
the worker state and sums the writes per worker. The serial path no longer
allocates; the parallel path allocates the shared state and the worker
funcval only.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* sfu: reuse the forwarded packet copy in the RED receivers

Both RED receivers copied the ExtPacket (and the opus one the rtp.Packet)
per forwarded packet, and the copies moved to the heap because the
broadcast takes their address. Keep them on the receiver like the existing
redPayloadBuf: ForwardRTP runs on one goroutine and down tracks do not keep
the packet past WriteRTP.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* sfu: skip BroadcastRTP allocation assertion under the race detector

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* sfu: group BroadcastRTP and its interfaces after the spreader methods

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-16 11:53:54 +05:30

85 lines
2.3 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 (
"fmt"
"sync/atomic"
"testing"
"github.com/stretchr/testify/require"
"github.com/livekit/protocol/livekit"
)
type testPacket struct{ seq int }
type testSender struct {
id livekit.ParticipantID
writes atomic.Int32
accept int32
}
func (s *testSender) SubscriberID() livekit.ParticipantID { return s.id }
func (s *testSender) WriteRTP(pkt *testPacket, layer int32) int32 {
s.writes.Add(1)
return s.accept
}
func newTestSpreader(numDownTracks, threshold int) (*DownTrackSpreader[*testSender], []*testSender) {
d := NewDownTrackSpreader[*testSender](DownTrackSpreaderParams{Threshold: threshold})
senders := make([]*testSender, numDownTracks)
for i := range senders {
senders[i] = &testSender{id: livekit.ParticipantID(fmt.Sprintf("p%d", i)), accept: 1}
d.Store(senders[i])
}
return d, senders
}
func TestBroadcastRTP(t *testing.T) {
for _, tc := range []struct {
name string
numDownTracks int
threshold int
maxAllocs float64
}{
{"serial", 5, 20, 0},
{"parallel", 50, 20, 2}, // shared state and worker funcval
} {
t.Run(tc.name, func(t *testing.T) {
d, senders := newTestSpreader(tc.numDownTracks, tc.threshold)
senders[0].accept = 0 // one down track that drops
pkt := &testPacket{}
written := BroadcastRTP(d, pkt, 2)
require.EqualValues(t, tc.numDownTracks-1, written)
for _, s := range senders {
require.EqualValues(t, 1, s.writes.Load())
}
if !RaceEnabled {
allocs := testing.AllocsPerRun(1000, func() { BroadcastRTP(d, pkt, 2) })
require.LessOrEqual(t, allocs, tc.maxAllocs)
}
})
}
}
func TestBroadcastRTPEmpty(t *testing.T) {
d := NewDownTrackSpreader[*testSender](DownTrackSpreaderParams{Threshold: 20})
require.EqualValues(t, 0, BroadcastRTP(d, &testPacket{}, 0))
}