mirror of
https://github.com/aler9/rtsp-simple-server
synced 2025-12-24 11:51:18 +08:00
prevent estimated absolute timestamp from drifting too much (#5080)
This commit is contained in:
@@ -5,6 +5,10 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
maxTimeDifference = 5 * time.Second
|
||||
)
|
||||
|
||||
var timeNow = time.Now
|
||||
|
||||
func multiplyAndDivide(v, m, d time.Duration) time.Duration {
|
||||
@@ -35,7 +39,7 @@ func (e *Estimator) Estimate(pts int64) time.Time {
|
||||
|
||||
computed := e.refNTP.Add((multiplyAndDivide(time.Duration(pts-e.refPTS), time.Second, time.Duration(e.ClockRate))))
|
||||
|
||||
if computed.After(now) {
|
||||
if computed.After(now) || computed.Before(now.Add(-maxTimeDifference)) {
|
||||
e.refNTP = now
|
||||
e.refPTS = pts
|
||||
return now
|
||||
|
||||
@@ -29,4 +29,8 @@ func TestEstimator(t *testing.T) {
|
||||
timeNow = func() time.Time { return time.Date(2003, 11, 4, 23, 15, 15, 0, time.UTC) }
|
||||
ntp = e.Estimate(5 * 90000)
|
||||
require.Equal(t, time.Date(2003, 11, 4, 23, 15, 10, 0, time.UTC), ntp)
|
||||
|
||||
timeNow = func() time.Time { return time.Date(2003, 11, 4, 23, 15, 20, 0, time.UTC) }
|
||||
ntp = e.Estimate(6 * 90000)
|
||||
require.Equal(t, time.Date(2003, 11, 4, 23, 15, 20, 0, time.UTC), ntp)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user