Include jitter and delay in receiver reports.
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package jitter
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
|
||||
"sfu/mono"
|
||||
)
|
||||
|
||||
type Estimator struct {
|
||||
hz uint32
|
||||
timestamp uint32
|
||||
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))
|
||||
if d & 0x80000000 != 0 {
|
||||
d = uint32(-int32(d))
|
||||
}
|
||||
oldjitter := atomic.LoadUint32(&e.jitter)
|
||||
jitter := (oldjitter * 15 + d) / 16
|
||||
atomic.StoreUint32(&e.jitter, jitter)
|
||||
|
||||
e.timestamp = timestamp
|
||||
e.time = now
|
||||
}
|
||||
|
||||
func (e *Estimator) Accumulate(timestamp uint32) {
|
||||
e.accumulate(timestamp, uint32(mono.Now(e.hz)))
|
||||
}
|
||||
|
||||
func (e *Estimator) Jitter() uint32 {
|
||||
return atomic.LoadUint32(&e.jitter)
|
||||
}
|
||||
|
||||
func (e *Estimator) HZ() uint32 {
|
||||
return e.hz
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package jitter
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestJitter(t *testing.T) {
|
||||
e := New(48000)
|
||||
e.accumulate(0, 0)
|
||||
e.accumulate(1000, 1000)
|
||||
e.accumulate(2000, 2000)
|
||||
e.accumulate(3000, 3000)
|
||||
|
||||
if e.Jitter() != 0 {
|
||||
t.Errorf("Expected 0, got %v", e.Jitter())
|
||||
}
|
||||
|
||||
e = New(48000)
|
||||
e.accumulate(0, 0)
|
||||
e.accumulate(1000, 1000)
|
||||
e.accumulate(2000, 2200)
|
||||
e.accumulate(3000, 3000)
|
||||
|
||||
if e.Jitter() != 23 {
|
||||
t.Errorf("Expected 23, got %v", e.Jitter())
|
||||
}
|
||||
|
||||
e = New(48000)
|
||||
e.accumulate(0, 0)
|
||||
e.accumulate(1000, 1000)
|
||||
e.accumulate(2000, 1800)
|
||||
e.accumulate(3000, 3000)
|
||||
|
||||
if e.Jitter() != 23 {
|
||||
t.Errorf("Expected 23, got %v", e.Jitter())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user