sfu: return VP8 munged header by value to avoid per-packet allocation (#4871)

CodecMunger.UpdateAndGet returned a freshly allocated slice for every
forwarded packet on every down track. Return the header in a fixed array
with a length instead and marshal into it with MarshalTo. TranslationParams
carries the array; WriteRTP slices it locally.

UpdateAndGet now allocates zero times per call (was one). Escape analysis
confirms TranslationParams stays on the stack.

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Raja Subramanian
2026-09-15 21:34:10 +05:30
committed by GitHub
co-authored by Claude Fable 5.1
parent 08c5433449
commit 0392ca90b4
7 changed files with 66 additions and 40 deletions
+17 -5
View File
@@ -27,6 +27,7 @@ import (
"github.com/livekit/protocol/logger"
"github.com/livekit/livekit-server/pkg/sfu/buffer"
"github.com/livekit/livekit-server/pkg/sfu/codecmunger"
dd "github.com/livekit/livekit-server/pkg/sfu/rtpextension/dependencydescriptor"
"github.com/livekit/livekit-server/pkg/sfu/testutils"
)
@@ -1598,7 +1599,8 @@ func TestForwarderGetTranslationParamsVideo(t *testing.T) {
extTimestamp: 0xabcdef,
},
incomingHeaderSize: 6,
codecBytes: marshalledVP8,
codecBytes: codecHeaderOf(marshalledVP8),
codecBytesLen: len(marshalledVP8),
marker: true,
}
actualTP, err = f.GetTranslationParams(extPkt, 0)
@@ -1677,7 +1679,8 @@ func TestForwarderGetTranslationParamsVideo(t *testing.T) {
extTimestamp: 0xabcdef,
},
incomingHeaderSize: 6,
codecBytes: marshalledVP8,
codecBytes: codecHeaderOf(marshalledVP8),
codecBytesLen: len(marshalledVP8),
}
actualTP, err = f.GetTranslationParams(extPkt, 0)
require.NoError(t, err)
@@ -1731,7 +1734,8 @@ func TestForwarderGetTranslationParamsVideo(t *testing.T) {
extTimestamp: 0xabcdef,
},
incomingHeaderSize: 6,
codecBytes: marshalledVP8,
codecBytes: codecHeaderOf(marshalledVP8),
codecBytesLen: len(marshalledVP8),
}
actualTP, err = f.GetTranslationParams(extPkt, 0)
require.NoError(t, err)
@@ -1819,7 +1823,8 @@ func TestForwarderGetTranslationParamsVideo(t *testing.T) {
extTimestamp: 0xabcdef,
},
incomingHeaderSize: 6,
codecBytes: marshalledVP8,
codecBytes: codecHeaderOf(marshalledVP8),
codecBytesLen: len(marshalledVP8),
}
actualTP, err = f.GetTranslationParams(extPkt, 0)
require.NoError(t, err)
@@ -1918,7 +1923,8 @@ func TestForwarderGetTranslationParamsVideo(t *testing.T) {
extTimestamp: 0xabcdf0,
},
incomingHeaderSize: 5,
codecBytes: marshalledVP8,
codecBytes: codecHeaderOf(marshalledVP8),
codecBytesLen: len(marshalledVP8),
}
actualTP, err = f.GetTranslationParams(extPkt, 1)
require.NoError(t, err)
@@ -2249,3 +2255,9 @@ func TestForwarderIsEndOfLayerFrame(t *testing.T) {
DependencyDescriptor: &buffer.ExtDependencyDescriptor{},
}))
}
func codecHeaderOf(b []byte) [codecmunger.MaxHeaderSize]byte {
var hdr [codecmunger.MaxHeaderSize]byte
copy(hdr[:], b)
return hdr
}