mirror of
https://github.com/bluenviron/mediamtx.git
synced 2025-12-20 02:00:05 -08:00
prevent estimated absolute timestamp from drifting too much (#5080)
This commit is contained in:
parent
0cdae40fe3
commit
a9d84103d0
2 changed files with 9 additions and 1 deletions
|
|
@ -5,6 +5,10 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
maxTimeDifference = 5 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
var timeNow = time.Now
|
var timeNow = time.Now
|
||||||
|
|
||||||
func multiplyAndDivide(v, m, d time.Duration) time.Duration {
|
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))))
|
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.refNTP = now
|
||||||
e.refPTS = pts
|
e.refPTS = pts
|
||||||
return now
|
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) }
|
timeNow = func() time.Time { return time.Date(2003, 11, 4, 23, 15, 15, 0, time.UTC) }
|
||||||
ntp = e.Estimate(5 * 90000)
|
ntp = e.Estimate(5 * 90000)
|
||||||
require.Equal(t, time.Date(2003, 11, 4, 23, 15, 10, 0, time.UTC), ntp)
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue