From 86db42524e5ec42ef2639ee22e4f080011e98afd Mon Sep 17 00:00:00 2001 From: Felipe Astroza Date: Tue, 9 Sep 2025 12:29:01 -0500 Subject: [PATCH] BuildReport() was exceeding the maxSize limit --- pkg/rfc8888/recorder.go | 12 ++-------- pkg/rfc8888/recorder_test.go | 45 +++++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/pkg/rfc8888/recorder.go b/pkg/rfc8888/recorder.go index a1a2cf26..c5e81d46 100644 --- a/pkg/rfc8888/recorder.go +++ b/pkg/rfc8888/recorder.go @@ -49,17 +49,9 @@ func (r *Recorder) BuildReport(now time.Time, maxSize int) *rtcp.CCFeedbackRepor } maxReportBlocks := (maxSize - 12 - (8 * len(r.streams))) / 2 - var maxReportBlocksPerStream int - if len(r.streams) > 1 { - maxReportBlocksPerStream = maxReportBlocks / (len(r.streams) - 1) - } else { - maxReportBlocksPerStream = maxReportBlocks - } + maxReportBlocksPerStream := maxReportBlocks / len(r.streams) - for i, log := range r.streams { - if len(r.streams) > 1 && int(i) == len(r.streams)-1 { - maxReportBlocksPerStream = maxReportBlocks % len(r.streams) - } + for _, log := range r.streams { block := log.metricsAfter(now, int64(maxReportBlocksPerStream)) report.ReportBlocks = append(report.ReportBlocks, block) } diff --git a/pkg/rfc8888/recorder_test.go b/pkg/rfc8888/recorder_test.go index e032a78f..3f8714b2 100644 --- a/pkg/rfc8888/recorder_test.go +++ b/pkg/rfc8888/recorder_test.go @@ -4,6 +4,7 @@ package rfc8888 import ( + "math/rand" "testing" "time" @@ -142,21 +143,49 @@ func TestRecorder(t *testing.T) { }, report.ReportBlocks[0]) }) - t.Run("MaxreportsPerStream", func(t *testing.T) { + t.Run("MaxreportsPerStream 3 streams", func(t *testing.T) { recorder := NewRecorder() now := time.Time{} + maxSize := 1200 + streams := 3 + packets := 1000 + // Add 1000 packets on 3 different streams + for i := 0; i < streams; i++ { + ssrc := rand.Uint32() //nolint:gosec + for j := 0; j < packets; j++ { + recorder.AddPacket(now, ssrc, uint16(j), 0) //nolint:gosec // G115 + } + } + reports := recorder.BuildReport(time.Time{}, maxSize) + + blocks := 0 + for i := 0; i < streams; i++ { + blocks += len(reports.ReportBlocks[i].MetricBlocks) + } + assert.Less(t, blocks*2, maxSize) + }) + + t.Run("MaxreportsPerStream 10 streams", func(t *testing.T) { + recorder := NewRecorder() + now := time.Time{} + maxSize := 1300 + + streams := 10 + packets := 1000 // Add 1000 packets on 10 different streams - for i := 0; i < 10; i++ { - for j := 0; j < 100; j++ { - recorder.AddPacket(now, uint32(i), uint16(j), 0) //nolint:gosec // G115 + for i := 0; i < streams; i++ { + ssrc := rand.Uint32() //nolint:gosec + for j := 0; j < packets; j++ { + recorder.AddPacket(now, ssrc, uint16(j), 0) //nolint:gosec // G115 } } - reports := recorder.BuildReport(time.Time{}, 1380) + reports := recorder.BuildReport(time.Time{}, maxSize) - for i := 0; i < 10; i++ { - assert.Greater(t, 72, len(reports.ReportBlocks[i].MetricBlocks)) - assert.Less(t, 3, len(reports.ReportBlocks[i].MetricBlocks)) + blocks := 0 + for i := 0; i < streams; i++ { + blocks += len(reports.ReportBlocks[i].MetricBlocks) } + assert.Less(t, blocks*2, maxSize) }) }