Skip to content

Commit 535d066

Browse files
Rayleigh LiSean-Der
authored andcommitted
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
1 parent 049f4cd commit 535d066

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

pkg/stats/stats_recorder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func (r *recorder) recordIncomingXR(latestStats internalStats, pkt *rtcp.Extende
223223
for i := min(r.maxLastReceiverReferenceTimes, len(latestStats.lastReceiverReferenceTimes)) - 1; i >= 0; i-- {
224224
lastRR := latestStats.lastReceiverReferenceTimes[i]
225225
if (lastRR&0x0000FFFFFFFF0000)>>16 == uint64(xrReport.LastRR) {
226-
dlrr := time.Duration(xrReport.DLRR/65536.0) * time.Second
226+
dlrr := time.Duration(float64(xrReport.DLRR) / 65536.0 * float64(time.Second))
227227
latestStats.RemoteOutboundRTPStreamStats.RoundTripTime = (ts.Add(-dlrr)).Sub(ntp.ToTime(lastRR))
228228
latestStats.RemoteOutboundRTPStreamStats.TotalRoundTripTime += latestStats.RemoteOutboundRTPStreamStats.RoundTripTime
229229
latestStats.RemoteOutboundRTPStreamStats.RoundTripTimeMeasurements++

pkg/stats/stats_recorder_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,27 @@ func TestStatsRecorder(t *testing.T) {
289289
})
290290
}
291291
}
292+
293+
func TestStatsRecorder_DLRR_Precision(t *testing.T) {
294+
r := newRecorder(0, 90_000)
295+
296+
report := &rtcp.ExtendedReport{
297+
Reports: []rtcp.ReportBlock{
298+
&rtcp.DLRRReportBlock{
299+
Reports: []rtcp.DLRRReport{
300+
{
301+
SSRC: 5000,
302+
LastRR: 762,
303+
DLRR: 30000,
304+
},
305+
},
306+
},
307+
},
308+
}
309+
310+
s := r.recordIncomingXR(internalStats{
311+
lastReceiverReferenceTimes: []uint64{50000000},
312+
}, report, time.Time{})
313+
314+
assert.Equal(t, int64(s.RemoteOutboundRTPStreamStats.RoundTripTime), int64(-9223372036854775808))
315+
}

0 commit comments

Comments
 (0)