From c2c187202f9a87a34afb19f931a6de0c263175c6 Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Wed, 31 Jul 2024 12:45:19 +0530 Subject: [PATCH] Fix forced rollover of RTP time stamp. (#2896) * Fix forced rollover of RTP time stamp. Was erroneously forcing a rollover when the timestamp jump actually has room to accommodate large jumps. For example, before pause ts = 10, then eight hour pause, restart ts = 10 + (8 * 00 * 60 * 90000) = 2592000010 (at 90000 clock rate for video). In normal processing, it will look like out-of-order as the difference 2592000000 is more than half the 32-bit range. But, forcing a roll over is incorrect. Fix by calculating excess over the full range and then account for wrap around. * log potential ts rollover * clamp at min 0 --- pkg/sfu/buffer/rtpstats_receiver.go | 26 +++++++++++++++++++++----- pkg/sfu/utils/wraparound.go | 2 +- pkg/sfu/utils/wraparound_test.go | 7 ++++--- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/pkg/sfu/buffer/rtpstats_receiver.go b/pkg/sfu/buffer/rtpstats_receiver.go index 54445c1c..300c3f2c 100644 --- a/pkg/sfu/buffer/rtpstats_receiver.go +++ b/pkg/sfu/buffer/rtpstats_receiver.go @@ -137,14 +137,21 @@ func (r *RTPStatsReceiver) NewSnapshotId() uint32 { return r.newSnapshotID(r.sequenceNumber.GetExtendedHighest()) } -func (r *RTPStatsReceiver) getTSRolloverCount(diffNano int64) int { +func (r *RTPStatsReceiver) getTSRolloverCount(diffNano int64, ts uint32) int { if diffNano < r.tsRolloverThreshold { // time not more than rollover threshold - return 0 + return -1 } - excess := int((diffNano - r.tsRolloverThreshold) * int64(r.params.ClockRate) / 1e9) - return excess/(1<<32) + 1 + excess := int((diffNano - r.tsRolloverThreshold*2) * int64(r.params.ClockRate) / 1e9) + roc := excess / (1 << 32) + if roc < 0 { + roc = 0 + } + if r.timestamp.GetHighest() > ts { + roc++ + } + return roc } func (r *RTPStatsReceiver) Update( @@ -167,6 +174,7 @@ func (r *RTPStatsReceiver) Update( var resSN utils.WrapAroundUpdateResult[uint64] var gapSN int64 var resTS utils.WrapAroundUpdateResult[uint64] + var timeSinceHighest int64 var tsRolloverCount int getLoggingFields := func() []interface{} { @@ -175,6 +183,7 @@ func (r *RTPStatsReceiver) Update( "gapSN", gapSN, "resTS", resTS, "gapTS", int64(resTS.ExtendedVal - resTS.PreExtendedHighest), + "timeSinceHighest", time.Duration(timeSinceHighest), "tsRolloverCount", tsRolloverCount, "packetTime", time.Unix(0, packetTime).String(), "sequenceNumber", sequenceNumber, @@ -221,7 +230,14 @@ func (r *RTPStatsReceiver) Update( } gapSN = int64(resSN.ExtendedVal - resSN.PreExtendedHighest) - tsRolloverCount = r.getTSRolloverCount(packetTime - r.highestTime) + timeSinceHighest = packetTime - r.highestTime + tsRolloverCount = r.getTSRolloverCount(timeSinceHighest, timestamp) + if tsRolloverCount >= 0 { + r.logger.Warnw( + "potential time stamp roll over", nil, + getLoggingFields()..., + ) + } resTS = r.timestamp.Rollover(timestamp, tsRolloverCount) if resTS.IsUnhandled { flowState.IsNotHandled = true diff --git a/pkg/sfu/utils/wraparound.go b/pkg/sfu/utils/wraparound.go index 18e250f3..2a30a632 100644 --- a/pkg/sfu/utils/wraparound.go +++ b/pkg/sfu/utils/wraparound.go @@ -120,7 +120,7 @@ func (w *WrapAround[T, ET]) UndoUpdate(result WrapAroundUpdateResult[ET]) { } func (w *WrapAround[T, ET]) Rollover(val T, numCycles int) (result WrapAroundUpdateResult[ET]) { - if !w.initialized || numCycles == 0 { + if !w.initialized || numCycles < 0 { return w.Update(val) } diff --git a/pkg/sfu/utils/wraparound_test.go b/pkg/sfu/utils/wraparound_test.go index 76cb8fdb..4f2e505d 100644 --- a/pkg/sfu/utils/wraparound_test.go +++ b/pkg/sfu/utils/wraparound_test.go @@ -476,10 +476,11 @@ func TestWrapAroundUint16Rollover(t *testing.T) { highest: 10, extendedHighest: 10, }, - // zero cycles - should just do an update + // negative cycles - should just do an update { - name: "zero", - input: 8, + name: "zero", + input: 8, + numCycles: -1, updated: WrapAroundUpdateResult[uint32]{ IsUnhandled: true, // the following fields are not valid when `IsUnhandled = true`, but code fills it in