From c6bf7a27868665910100527b0ef9b3579d780e12 Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Wed, 24 Dec 2025 22:48:25 +0530 Subject: [PATCH] Fix logging key and other clean up around stream restart. (#4192) --- pkg/sfu/buffer/buffer_base.go | 50 ++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/pkg/sfu/buffer/buffer_base.go b/pkg/sfu/buffer/buffer_base.go index 6832a6e5..f4d52511 100644 --- a/pkg/sfu/buffer/buffer_base.go +++ b/pkg/sfu/buffer/buffer_base.go @@ -401,27 +401,10 @@ func (b *BufferBase) CloseWithReason(reason string) (stats *livekit.RTPStats, er b.StopKeyFrameSeeder() - b.RLock() - rtpStats := b.rtpStats - rtpStatsLite := b.rtpStatsLite + b.Lock() + b.stopRTPStats(reason) b.readCond.Broadcast() - b.RUnlock() - - if rtpStats != nil { - rtpStats.Stop() - stats = rtpStats.ToProto() - } - if rtpStatsLite != nil { - rtpStatsLite.Stop() - } - - b.logger.Debugw( - "rtp stats", - "direction", "upstream", - "stats", rtpStats, - "statsLite", rtpStatsLite, - "reason", reason, - ) + b.Unlock() go b.flushExtPackets() }) @@ -473,6 +456,26 @@ func (b *BufferBase) setupRTPStats(clockRate uint32) { } } +func (b *BufferBase) stopRTPStats(reason string) (stats *livekit.RTPStats, statsLite *livekit.RTPStats) { + if b.rtpStats != nil { + b.rtpStats.Stop() + stats = b.rtpStats.ToProto() + } + if b.rtpStatsLite != nil { + b.rtpStatsLite.Stop() + statsLite = b.rtpStatsLite.ToProto() + } + + b.logger.Debugw( + "rtp stats", + "direction", "upstream", + "stats", b.rtpStats, + "statsLite", b.rtpStatsLite, + "reason", reason, + ) + return +} + func (b *BufferBase) createDDParserAndFrameRateCalculator() { if mime.IsMimeTypeSVCCapable(b.mime) || b.mime == mime.MimeTypeVP8 { frc := NewFrameRateCalculatorDD(b.clockRate, b.logger) @@ -683,9 +686,8 @@ func (b *BufferBase) HandleIncomingPacketLocked( } b.StopKeyFrameSeeder() - - b.rtpStats.Stop() - b.logger.Infow("stream restart - rtp stats", b.rtpStats) + b.stopRTPStats("stream-restart") + b.flushExtPacketsLocked() b.snRangeMap = utils.NewRangeMap[uint64, uint64](100) b.setupRTPStats(b.clockRate) @@ -693,7 +695,7 @@ func (b *BufferBase) HandleIncomingPacketLocked( if b.nacker != nil { b.nacker = nack.NewNACKQueue(nack.NackQueueParamsDefault) } - b.flushExtPacketsLocked() + b.StartKeyFrameSeeder() flowState = b.rtpStats.Update( arrivalTime,