mediamtx/internal/ntpestimator/estimator.go
Alessandro Ros 0cdae40fe3
estimate absolute timestamp more precisely (#5078)
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.
2025-10-12 11:02:14 +02:00

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
}