mirror of
https://github.com/aler9/gortsplib
synced 2025-10-06 23:52:46 +08:00
rtptimedec: fix decoding error of 1ns during overflow
This commit is contained in:
@@ -28,10 +28,10 @@ func (d *Decoder) Decode(ts uint32) time.Duration {
|
|||||||
diff := ts64 - *d.tsPrev
|
diff := ts64 - *d.tsPrev
|
||||||
switch {
|
switch {
|
||||||
case diff < -0xFFFFFF: // overflow
|
case diff < -0xFFFFFF: // overflow
|
||||||
d.tsAdd += 0xFFFFFFFF
|
d.tsAdd += 0x100000000
|
||||||
|
|
||||||
case diff > 0xFFFFFF: // timestamp overflowed then went back
|
case diff > 0xFFFFFF: // timestamp overflowed then went back
|
||||||
d.tsAdd -= 0xFFFFFFFF
|
d.tsAdd -= 0x100000000
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -10,11 +10,25 @@ import (
|
|||||||
func TestOverflow(t *testing.T) {
|
func TestOverflow(t *testing.T) {
|
||||||
d := New(90000)
|
d := New(90000)
|
||||||
|
|
||||||
pts := d.Decode(4294877295)
|
i := uint32(4294877296)
|
||||||
|
pts := d.Decode(i)
|
||||||
require.Equal(t, time.Duration(0), pts)
|
require.Equal(t, time.Duration(0), pts)
|
||||||
|
|
||||||
pts = d.Decode(90000)
|
// 1st overflow
|
||||||
|
i += 90000 * 2
|
||||||
|
pts = d.Decode(i)
|
||||||
require.Equal(t, 2*time.Second, pts)
|
require.Equal(t, 2*time.Second, pts)
|
||||||
|
|
||||||
|
// reach 4294890000 slowly
|
||||||
|
for ; i < 4294890000; i += 90000 * 10 {
|
||||||
|
pts = d.Decode(i)
|
||||||
|
require.Equal(t, 2*time.Second+time.Second*time.Duration(i-90000)/90000, pts)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2nd overflow
|
||||||
|
i += 90000 * 10
|
||||||
|
pts = d.Decode(i)
|
||||||
|
require.Equal(t, 47732*time.Second, pts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOverflowAndBack(t *testing.T) {
|
func TestOverflowAndBack(t *testing.T) {
|
||||||
@@ -23,7 +37,7 @@ func TestOverflowAndBack(t *testing.T) {
|
|||||||
pts := d.Decode(4294877296)
|
pts := d.Decode(4294877296)
|
||||||
require.Equal(t, time.Duration(0), pts)
|
require.Equal(t, time.Duration(0), pts)
|
||||||
|
|
||||||
pts = d.Decode(90001)
|
pts = d.Decode(90000)
|
||||||
require.Equal(t, 2*time.Second, pts)
|
require.Equal(t, 2*time.Second, pts)
|
||||||
|
|
||||||
pts = d.Decode(4294877296)
|
pts = d.Decode(4294877296)
|
||||||
@@ -35,6 +49,6 @@ func TestOverflowAndBack(t *testing.T) {
|
|||||||
pts = d.Decode(4294877296)
|
pts = d.Decode(4294877296)
|
||||||
require.Equal(t, time.Duration(0), pts)
|
require.Equal(t, time.Duration(0), pts)
|
||||||
|
|
||||||
pts = d.Decode(90001)
|
pts = d.Decode(90000)
|
||||||
require.Equal(t, 2*time.Second, pts)
|
require.Equal(t, 2*time.Second, pts)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user