1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-14 04:35:57 +01:00
galene/jitter/jitter.go

50 lines
868 B
Go
Raw Permalink Normal View History

package jitter
import (
"sync/atomic"
2020-12-19 17:37:48 +01:00
"github.com/jech/galene/rtptime"
)
type Estimator struct {
hz uint32
timestamp uint32
2020-06-03 20:12:25 +02:00
time uint32
jitter uint32 // atomic
}
func New(hz uint32) *Estimator {
return &Estimator{hz: hz}
}
func (e *Estimator) accumulate(timestamp, now uint32) {
if e.time == 0 {
e.timestamp = timestamp
e.time = now
}
d := uint32((e.time - now) - (e.timestamp - timestamp))
2020-06-03 20:12:25 +02:00
if d&0x80000000 != 0 {
d = uint32(-int32(d))
}
oldjitter := atomic.LoadUint32(&e.jitter)
2020-06-03 20:12:25 +02:00
jitter := (oldjitter*15 + d) / 16
atomic.StoreUint32(&e.jitter, jitter)
e.timestamp = timestamp
e.time = now
}
func (e *Estimator) Accumulate(timestamp uint32) {
2020-06-03 20:12:25 +02:00
e.accumulate(timestamp, uint32(rtptime.Now(e.hz)))
}
func (e *Estimator) Jitter() uint32 {
return atomic.LoadUint32(&e.jitter)
}
func (e *Estimator) HZ() uint32 {
return e.hz
}