From 5b1e466031cbada305f255f1c4656fe137e58b7a Mon Sep 17 00:00:00 2001 From: kjs001127 Date: Mon, 24 Apr 2023 14:52:53 +0900 Subject: [PATCH] feat: make listener OnLatencyChange call only when latency value has changed --- pkg/jitter/jitter.go | 20 +++++++++++++------- pkg/jitter/listener.go | 4 ++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/pkg/jitter/jitter.go b/pkg/jitter/jitter.go index 66d8a68..45a56c3 100644 --- a/pkg/jitter/jitter.go +++ b/pkg/jitter/jitter.go @@ -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 { diff --git a/pkg/jitter/listener.go b/pkg/jitter/listener.go index 07993f5..ec2f424 100644 --- a/pkg/jitter/listener.go +++ b/pkg/jitter/listener.go @@ -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 { @@ -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) {