From 535d0669650b5dea1deb4064bc91a116e8edff01 Mon Sep 17 00:00:00 2001 From: Rayleigh Li Date: Wed, 15 Feb 2023 14:26:47 +0800 Subject: [PATCH] Fix precision error in stats interceptor When calculating dlrr, precision is lost in integer operations, errors occur in rtt calculations. Use float64 just like calculate dlsr just works --- pkg/stats/stats_recorder.go | 2 +- pkg/stats/stats_recorder_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/pkg/stats/stats_recorder.go b/pkg/stats/stats_recorder.go index e6206fbb..11bcc9e2 100644 --- a/pkg/stats/stats_recorder.go +++ b/pkg/stats/stats_recorder.go @@ -223,7 +223,7 @@ func (r *recorder) recordIncomingXR(latestStats internalStats, pkt *rtcp.Extende for i := min(r.maxLastReceiverReferenceTimes, len(latestStats.lastReceiverReferenceTimes)) - 1; i >= 0; i-- { lastRR := latestStats.lastReceiverReferenceTimes[i] if (lastRR&0x0000FFFFFFFF0000)>>16 == uint64(xrReport.LastRR) { - dlrr := time.Duration(xrReport.DLRR/65536.0) * time.Second + dlrr := time.Duration(float64(xrReport.DLRR) / 65536.0 * float64(time.Second)) latestStats.RemoteOutboundRTPStreamStats.RoundTripTime = (ts.Add(-dlrr)).Sub(ntp.ToTime(lastRR)) latestStats.RemoteOutboundRTPStreamStats.TotalRoundTripTime += latestStats.RemoteOutboundRTPStreamStats.RoundTripTime latestStats.RemoteOutboundRTPStreamStats.RoundTripTimeMeasurements++ diff --git a/pkg/stats/stats_recorder_test.go b/pkg/stats/stats_recorder_test.go index d0a79387..bc2e9b47 100644 --- a/pkg/stats/stats_recorder_test.go +++ b/pkg/stats/stats_recorder_test.go @@ -289,3 +289,27 @@ func TestStatsRecorder(t *testing.T) { }) } } + +func TestStatsRecorder_DLRR_Precision(t *testing.T) { + r := newRecorder(0, 90_000) + + report := &rtcp.ExtendedReport{ + Reports: []rtcp.ReportBlock{ + &rtcp.DLRRReportBlock{ + Reports: []rtcp.DLRRReport{ + { + SSRC: 5000, + LastRR: 762, + DLRR: 30000, + }, + }, + }, + }, + } + + s := r.recordIncomingXR(internalStats{ + lastReceiverReferenceTimes: []uint64{50000000}, + }, report, time.Time{}) + + assert.Equal(t, int64(s.RemoteOutboundRTPStreamStats.RoundTripTime), int64(-9223372036854775808)) +}