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
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user