Skip to content

Commit

Permalink
Make Listener::OnLatencyChange called only when latency value has cha…
Browse files Browse the repository at this point in the history
…nged
  • Loading branch information
kjs001127 authored Apr 24, 2023
2 parents 41cfeb9 + 5b1e466 commit 9444cbc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
20 changes: 13 additions & 7 deletions pkg/jitter/jitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,26 @@ func (b *Jitter) dequeuePackets() []*Packet {
}

func (b *Jitter) adaptive() {
// late 가 너무 많다면 b.latency 를 늦춤
newLatency := b.calculateLatency()
if newLatency != b.latency {
b.listener.OnLatencyChanged(b.latency, newLatency)
b.late.Init()
b.latency = newLatency
}
}

func (b *Jitter) calculateLatency() int64 {
if b.sumTsOfLatePackets() > b.window*2/100 { // late 패킷들의 ptime 합이 윈도우의 2% 를 초과시
candidate := b.latency + maxInList(b.late)
b.latency = lo.Min([]int64{candidate, b.maxLatency})
b.listener.OnLatencyChanged(b.latency)
b.late.Init()
return lo.Min([]int64{candidate, b.maxLatency})
}

if b.loss.Len() == 0 && b.late.Len() == 0 { // loss 와 late 가 모두 없으면
candidate := b.latency - minInList(b.normal)
b.latency = lo.Max([]int64{candidate, b.minLatency})
b.listener.OnLatencyChanged(b.latency)
b.late.Init()
return lo.Max([]int64{candidate, b.minLatency})
}

return b.latency
}

func (b *Jitter) sumTsOfLatePackets() int64 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/jitter/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type Listener interface {
OnPacketLoss(currentTs, remainingTs int64)
OnPacketEnqueue(currentTs, remainingTs int64, pkt *Packet)
OnPacketDequeue(currentTs, remainingTs int64, pkt []*Packet)
OnLatencyChanged(new int64)
OnLatencyChanged(old, new int64)
}

type NullListener struct {
Expand All @@ -13,7 +13,7 @@ type NullListener struct {
func (n NullListener) OnPacketLoss(currentTs, remainingTs int64) {
}

func (n NullListener) OnLatencyChanged(new int64) {
func (n NullListener) OnLatencyChanged(old, new int64) {
}

func (n NullListener) OnPacketEnqueue(currentTs, remainingTs int64, pkt *Packet) {
Expand Down

0 comments on commit 9444cbc

Please sign in to comment.