From b07e7a382885d85097660a9321134b408578e600 Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Fri, 19 Sep 2025 15:15:26 +0530 Subject: [PATCH] Use difference in key frame counter to stop seeder. (#3936) The key frame seeder could be started multiple times. Use difference to detect stop condition. --- pkg/sfu/buffer/buffer.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/sfu/buffer/buffer.go b/pkg/sfu/buffer/buffer.go index 4805fde5..72c32a6b 100644 --- a/pkg/sfu/buffer/buffer.go +++ b/pkg/sfu/buffer/buffer.go @@ -1343,6 +1343,14 @@ func (b *Buffer) seedKeyFrame(keyFrameSeederGeneration int32) { ticker := time.NewTicker(time.Second) defer ticker.Stop() + initialCount := uint32(0) + b.RLock() + rtpStats := b.rtpStats + b.RUnlock() + if rtpStats != nil { + initialCount, _ = rtpStats.KeyFrame() + } + for { if b.closed.Load() || b.keyFrameSeederGeneration.Load() != keyFrameSeederGeneration { return @@ -1354,15 +1362,12 @@ func (b *Buffer) seedKeyFrame(keyFrameSeederGeneration int32) { return case <-ticker.C: - b.RLock() - rtpStats := b.rtpStats - b.RUnlock() - if rtpStats != nil { cnt, last := rtpStats.KeyFrame() - if cnt > 0 { + if cnt > initialCount { b.logger.Debugw( "stopping key frame seeder: received key frame", + "keyFrameCountInitial", initialCount, "keyFrameCount", cnt, "lastKeyFrame", last, )