mirror of
https://github.com/bluenviron/mediamtx.git
synced 2025-12-20 02:00:05 -08:00
When the absolute timestamp of incoming frames was not available, it was filled with the current timestamp, which is influenced by latency over time. This mechanism is replaced by an algorithm that detects when latency is the lowest, stores the current timestamp and uses it as reference throughout the rest of the stream.
45 lines
767 B
Go
45 lines
767 B
Go
// Package ntpestimator contains a NTP estimator.
|
|
package ntpestimator
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
var timeNow = time.Now
|
|
|
|
func multiplyAndDivide(v, m, d time.Duration) time.Duration {
|
|
secs := v / d
|
|
dec := v % d
|
|
return (secs*m + dec*m/d)
|
|
}
|
|
|
|
// Estimator is a NTP estimator.
|
|
type Estimator struct {
|
|
ClockRate int
|
|
|
|
refNTP time.Time
|
|
refPTS int64
|
|
}
|
|
|
|
var zero = time.Time{}
|
|
|
|
// Estimate returns estimated NTP.
|
|
func (e *Estimator) Estimate(pts int64) time.Time {
|
|
now := timeNow()
|
|
|
|
if e.refNTP.Equal(zero) {
|
|
e.refNTP = now
|
|
e.refPTS = pts
|
|
return now
|
|
}
|
|
|
|
computed := e.refNTP.Add((multiplyAndDivide(time.Duration(pts-e.refPTS), time.Second, time.Duration(e.ClockRate))))
|
|
|
|
if computed.After(now) {
|
|
e.refNTP = now
|
|
e.refPTS = pts
|
|
return now
|
|
}
|
|
|
|
return computed
|
|
}
|