reduce some heap use in packet path (#4478)

This commit is contained in:
Raja Subramanian
2026-04-25 14:24:23 +05:30
committed by GitHub
parent f3b80b2886
commit dc6b75058e
4 changed files with 31 additions and 22 deletions

View File

@@ -162,16 +162,16 @@ func (v *VP8) Unmarshal(payload []byte) error {
return nil
}
func (v *VP8) Marshal() ([]byte, error) {
buf := make([]byte, v.HeaderSize)
n, err := v.MarshalTo(buf)
func (v VP8) Marshal() ([]byte, error) {
var buf [8]byte
n, err := v.MarshalTo(buf[:])
if err != nil {
return nil, err
}
return buf[:n], err
}
func (v *VP8) MarshalTo(buf []byte) (int, error) {
func (v VP8) MarshalTo(buf []byte) (int, error) {
if len(buf) < v.HeaderSize {
return 0, errShortPacket
}

View File

@@ -174,7 +174,7 @@ func (v *VP8) UpdateAndGet(extPkt *buffer.ExtPacket, snOutOfOrder bool, snHasGap
// when it reaches a certain size.
mungedPictureId := uint16((extPictureId - pictureIdOffset) & 0x7fff)
vp8Packet := &buffer.VP8{
vp8Packet := buffer.VP8{
FirstByte: vp8.FirstByte,
I: vp8.I,
M: mungedPictureId > 127,
@@ -281,7 +281,7 @@ func (v *VP8) UpdateAndGet(extPkt *buffer.ExtPacket, snOutOfOrder bool, snHasGap
v.lastTl0PicIdx = mungedTl0PicIdx
v.lastKeyIdx = mungedKeyIdx
vp8Packet := &buffer.VP8{
vp8Packet := buffer.VP8{
FirstByte: vp8.FirstByte,
I: vp8.I,
M: mungedPictureId > 127,

View File

@@ -969,18 +969,24 @@ func (f *Forwarder) ProvisionalAllocatePrepare(availableLayers []int32, bitrates
f.lock.Lock()
defer f.lock.Unlock()
f.provisional = &VideoAllocationProvisional{
allocatedLayer: buffer.InvalidLayer,
muted: f.muted,
pubMuted: f.pubMuted,
maxSeenLayer: f.vls.GetMaxSeen(),
bitrates: bitrates,
maxLayer: f.vls.GetMax(),
currentLayer: f.vls.GetCurrent(),
if f.provisional == nil {
f.provisional = &VideoAllocationProvisional{}
}
f.provisional.availableLayers = make([]int32, len(availableLayers))
copy(f.provisional.availableLayers, availableLayers)
p := f.provisional
p.allocatedLayer = buffer.InvalidLayer
p.muted = f.muted
p.pubMuted = f.pubMuted
p.maxSeenLayer = f.vls.GetMaxSeen()
p.bitrates = bitrates
p.maxLayer = f.vls.GetMax()
p.currentLayer = f.vls.GetCurrent()
if cap(p.availableLayers) >= len(availableLayers) {
p.availableLayers = p.availableLayers[:len(availableLayers)]
} else {
p.availableLayers = make([]int32, len(availableLayers))
}
copy(p.availableLayers, availableLayers)
}
func (f *Forwarder) ProvisionalAllocateReset() {
@@ -1811,8 +1817,7 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) (
// 3. extExpectedTS -> expected timestamp of this packet calculated based on elapsed time since first packet
// Ideally, extRefTS and extExpectedTS should be very close and extLastTS should be before both of those.
// But, cases like muting/unmuting, clock vagaries, pacing, etc. make them not satisfy those conditions always.
rtpMungerState := f.rtpMunger.GetState()
extLastTS := rtpMungerState.ExtLastTimestamp
extLastTS := f.rtpMunger.GetExtLastTimestamp()
extExpectedTS := extLastTS
extRefTS := extLastTS
refTS := uint32(extRefTS)
@@ -1956,14 +1961,14 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) (
"switchingAt", switchingAt,
"layer", layer,
"extLastTS", extLastTS,
"lastMarker", rtpMungerState.LastMarker,
"lastMarker", f.rtpMunger.GetState().LastMarker,
"extRefTS", extRefTS,
"dummyStartTSOffset", f.dummyStartTSOffset,
"referenceLayerSpatial", f.referenceLayerSpatial,
"extExpectedTS", extExpectedTS,
"extNextTS", extNextTS,
"tsJump", extNextTS-extLastTS,
"nextSN", rtpMungerState.ExtLastSequenceNumber+1,
"nextSN", f.rtpMunger.GetState().ExtLastSequenceNumber+1,
"extIncomingSN", extPkt.ExtSequenceNumber,
"extIncomingTS", extPkt.ExtTimestamp,
"rtpStats", f.rtpStats,
@@ -2197,7 +2202,7 @@ func (f *Forwarder) GetSnTsForPadding(num int, frameRate uint32, forceMarker boo
f.clockRate,
frameRate,
forceMarker,
f.rtpMunger.GetState().ExtLastTimestamp,
f.rtpMunger.GetExtLastTimestamp(),
)
}
@@ -2212,7 +2217,7 @@ func (f *Forwarder) GetSnTsForBlankFrames(frameRate uint32, numPackets int) ([]S
numPackets++
}
extLastTS := f.rtpMunger.GetState().ExtLastTimestamp
extLastTS := f.rtpMunger.GetExtLastTimestamp()
extExpectedTS := extLastTS
if f.rtpStats != nil {
tsExt, err := f.rtpStats.GetExpectedRTPTimestamp(mono.Now())

View File

@@ -102,6 +102,10 @@ func (r *RTPMunger) GetState() *livekit.RTPMungerState {
}
}
func (r *RTPMunger) GetExtLastTimestamp() uint64 {
return r.extLastTS
}
func (r *RTPMunger) GetTSOffset() uint64 {
return r.tsOffset
}