Skip to content

Commit

Permalink
Fix precision error in stats interceptor
Browse files Browse the repository at this point in the history
When calculating dlrr, precision is lost in integer operations,
errors occur in rtt calculations.
Use float64 just like calculate dlsr just works
  • Loading branch information
Rayleigh Li authored and Sean-Der committed Apr 25, 2023
1 parent 049f4cd commit 535d066
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/stats/stats_recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down
24 changes: 24 additions & 0 deletions pkg/stats/stats_recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 535d066

Please sign in to comment.