Skip to content

Commit

Permalink
Reduce negative threshold for resync #9
Browse files Browse the repository at this point in the history
  • Loading branch information
kjs001127 authored May 3, 2023
2 parents f44eb7f + 3d21c13 commit cd58203
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion pkg/jitter/jitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func (f *Factory) CreateBuffer() Buffer {
return NewJitter(f.minLatency, f.maxLatency, f.window, f.defaultTickInterval, f.listener)
}

const negativeThreshold = 2

type deltaWithSampleCnt struct {
delta int64
sampleCnt int64
Expand Down Expand Up @@ -89,7 +91,7 @@ func (b *Jitter) Put(p *Packet) {

b.listener.OnPacketEnqueue(b.currentTime(), b.targetTime(), b.sumRemainingTs(), p)

if !b.marked || math.Abs(float64(p.Timestamp-b.currentTime())) > float64(b.maxLatency) {
if !b.marked || b.shouldReSyncWith(p) {
oldCurrent := b.currentTime()
b.init(p.Timestamp)
newCurrent := b.currentTime()
Expand All @@ -106,6 +108,17 @@ func (b *Jitter) Put(p *Packet) {
}
}

func (b *Jitter) shouldReSyncWith(p *Packet) bool {
gap := p.Timestamp - b.currentTime()
if gap < 0 && gap <= -negativeThreshold*b.defaultTickInterval {
return true
}
if gap > 0 && gap >= b.maxLatency {
return true
}
return false
}

func (b *Jitter) Get() ([]*Packet, bool) {
b.Lock()
defer b.Unlock()
Expand Down

0 comments on commit cd58203

Please sign in to comment.