From 87e90369762c19c19338ac201769bee5ebc672d3 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Wed, 26 Oct 2022 16:53:27 +0200 Subject: [PATCH] rtptimedec: increase threshold of negative differences --- pkg/rtptimedec/decoder.go | 26 +++++++++++++------------- pkg/rtptimedec/decoder_test.go | 14 +++++++------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/pkg/rtptimedec/decoder.go b/pkg/rtptimedec/decoder.go index 97d2bc16..ed3ceb4b 100644 --- a/pkg/rtptimedec/decoder.go +++ b/pkg/rtptimedec/decoder.go @@ -5,14 +5,14 @@ import ( "time" ) -const negativeThreshold = 0xFFFFFFF +const negativeThreshold = 0xFFFFFFFF / 2 // Decoder is a RTP timestamp decoder. type Decoder struct { clockRate time.Duration initialized bool - tsOverall time.Duration - tsPrev uint32 + overall time.Duration + prev uint32 } // New allocates a Decoder. @@ -26,25 +26,25 @@ func New(clockRate int) *Decoder { func (d *Decoder) Decode(ts uint32) time.Duration { if !d.initialized { d.initialized = true - d.tsPrev = ts + d.prev = ts return 0 } - diff := ts - d.tsPrev + diff := ts - d.prev // negative difference if diff > negativeThreshold { - diff = d.tsPrev - ts - d.tsPrev = ts - d.tsOverall -= time.Duration(diff) + diff = d.prev - ts + d.prev = ts + d.overall -= time.Duration(diff) } else { - d.tsPrev = ts - d.tsOverall += time.Duration(diff) + d.prev = ts + d.overall += time.Duration(diff) } - // avoid an int64 overflow and preserve resolution by splitting division into two parts: + // avoid an int64 overflow and keep resolution by splitting division into two parts: // first add the integer part, then the decimal part. - secs := d.tsOverall / d.clockRate - dec := d.tsOverall % d.clockRate + secs := d.overall / d.clockRate + dec := d.overall % d.clockRate return secs*time.Second + dec*time.Second/d.clockRate } diff --git a/pkg/rtptimedec/decoder_test.go b/pkg/rtptimedec/decoder_test.go index 5286a070..cb33ab56 100644 --- a/pkg/rtptimedec/decoder_test.go +++ b/pkg/rtptimedec/decoder_test.go @@ -30,13 +30,13 @@ func TestNegativeDiff(t *testing.T) { func TestOverflow(t *testing.T) { d := New(90000) - i := uint32(4294877296) + i := uint32(0xFFFFFFFF - 90000 + 1) secs := time.Duration(0) pts := d.Decode(i) require.Equal(t, time.Duration(0), pts) - const stride = 150 - lim := uint32(uint64(4294967296 - (stride * 90000))) + const stride = 1500 + lim := uint32(uint64(0xFFFFFFFF + 1 - (stride * 90000))) for n := 0; n < 100; n++ { // overflow @@ -59,19 +59,19 @@ func TestOverflow(t *testing.T) { func TestOverflowAndBack(t *testing.T) { d := New(90000) - pts := d.Decode(4294877296) + pts := d.Decode(0xFFFFFFFF - 90000 + 1) require.Equal(t, time.Duration(0), pts) pts = d.Decode(90000) require.Equal(t, 2*time.Second, pts) - pts = d.Decode(4294877296) + pts = d.Decode(0xFFFFFFFF - 90000 + 1) require.Equal(t, time.Duration(0), pts) - pts = d.Decode(4294877296 - 90000) + pts = d.Decode(0xFFFFFFFF - 90000 + 1 - 90000) require.Equal(t, -1*time.Second, pts) - pts = d.Decode(4294877296) + pts = d.Decode(0xFFFFFFFF - 90000 + 1) require.Equal(t, time.Duration(0), pts) pts = d.Decode(90000)