diff --git a/go.mod b/go.mod index fe03158c..c5269e02 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/pion/interceptor -go 1.20 +go 1.21 require ( github.com/pion/logging v0.2.2 diff --git a/pkg/bwe/acknowledgment.go b/pkg/bwe/acknowledgment.go new file mode 100644 index 00000000..3fc75d76 --- /dev/null +++ b/pkg/bwe/acknowledgment.go @@ -0,0 +1,21 @@ +package bwe + +import ( + "fmt" + "time" + + "github.com/pion/rtcp" +) + +type acknowledgment struct { + seqNr int64 + size uint16 + departure time.Time + arrived bool + arrival time.Time + ecn rtcp.ECN +} + +func (a acknowledgment) String() string { + return fmt.Sprintf("seq=%v, departure=%v, arrival=%v", a.seqNr, a.departure, a.arrival) +} diff --git a/pkg/bwe/arrival_group_accumulator.go b/pkg/bwe/arrival_group_accumulator.go new file mode 100644 index 00000000..69bd3498 --- /dev/null +++ b/pkg/bwe/arrival_group_accumulator.go @@ -0,0 +1,43 @@ +package bwe + +import "time" + +type arrivalGroup []acknowledgment + +type arrivalGroupAccumulator struct { + next arrivalGroup + burstInterval time.Duration +} + +func newArrivalGroupAccumulator() *arrivalGroupAccumulator { + return &arrivalGroupAccumulator{ + next: make([]acknowledgment, 0), + burstInterval: 5 * time.Millisecond, + } +} + +func (a *arrivalGroupAccumulator) onPacketAcked(ack acknowledgment) arrivalGroup { + if len(a.next) == 0 { + a.next = append(a.next, ack) + return nil + } + + if ack.departure.Sub(a.next[0].departure) < a.burstInterval { + a.next = append(a.next, ack) + return nil + } + + interDepartureTime := ack.departure.Sub(a.next[0].departure) + interArrivalTime := ack.arrival.Sub(a.next[len(a.next)-1].arrival) + interGroupDelay := interArrivalTime - interDepartureTime + + if interArrivalTime < a.burstInterval && interGroupDelay < 0 { + a.next = append(a.next, ack) + return nil + } + + group := make(arrivalGroup, len(a.next)) + copy(group, a.next) + a.next = arrivalGroup{ack} + return group +} diff --git a/pkg/bwe/arrival_group_accumulator_test.go b/pkg/bwe/arrival_group_accumulator_test.go new file mode 100644 index 00000000..31cc2bc4 --- /dev/null +++ b/pkg/bwe/arrival_group_accumulator_test.go @@ -0,0 +1,204 @@ +package bwe + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestArrivalGroupAccumulator(t *testing.T) { + triggerNewGroupElement := acknowledgment{ + departure: time.Time{}.Add(time.Second), + arrival: time.Time{}.Add(time.Second), + } + cases := []struct { + name string + log []acknowledgment + exp []arrivalGroup + }{ + { + name: "emptyCreatesNoGroups", + log: []acknowledgment{}, + exp: []arrivalGroup{}, + }, + { + name: "createsSingleElementGroup", + log: []acknowledgment{ + { + departure: time.Time{}, + arrival: time.Time{}.Add(time.Millisecond), + }, + triggerNewGroupElement, + }, + exp: []arrivalGroup{{ + { + departure: time.Time{}, + arrival: time.Time{}.Add(time.Millisecond), + }, + }, + }, + }, + { + name: "createsTwoElementGroup", + log: []acknowledgment{ + { + arrival: time.Time{}.Add(15 * time.Millisecond), + }, + { + departure: time.Time{}.Add(3 * time.Millisecond), + arrival: time.Time{}.Add(20 * time.Millisecond), + }, + triggerNewGroupElement, + }, + exp: []arrivalGroup{{ + { + departure: time.Time{}, + arrival: time.Time{}.Add(15 * time.Millisecond), + }, + { + departure: time.Time{}.Add(3 * time.Millisecond), + arrival: time.Time{}.Add(20 * time.Millisecond), + }, + }}, + }, + { + name: "createsTwoArrivalGroups", + log: []acknowledgment{ + { + departure: time.Time{}, + arrival: time.Time{}.Add(15 * time.Millisecond), + }, + { + departure: time.Time{}.Add(3 * time.Millisecond), + arrival: time.Time{}.Add(20 * time.Millisecond), + }, + { + departure: time.Time{}.Add(9 * time.Millisecond), + arrival: time.Time{}.Add(30 * time.Millisecond), + }, + triggerNewGroupElement, + }, + exp: []arrivalGroup{ + { + { + arrival: time.Time{}.Add(15 * time.Millisecond), + }, + { + departure: time.Time{}.Add(3 * time.Millisecond), + arrival: time.Time{}.Add(20 * time.Millisecond), + }, + }, + { + { + departure: time.Time{}.Add(9 * time.Millisecond), + arrival: time.Time{}.Add(30 * time.Millisecond), + }, + }, + }, + }, + { + name: "ignoresOutOfOrderPackets", + log: []acknowledgment{ + { + departure: time.Time{}, + arrival: time.Time{}.Add(15 * time.Millisecond), + }, + { + departure: time.Time{}.Add(6 * time.Millisecond), + arrival: time.Time{}.Add(34 * time.Millisecond), + }, + { + departure: time.Time{}.Add(8 * time.Millisecond), + arrival: time.Time{}.Add(30 * time.Millisecond), + }, + triggerNewGroupElement, + }, + exp: []arrivalGroup{ + { + { + departure: time.Time{}, + arrival: time.Time{}.Add(15 * time.Millisecond), + }, + }, + { + { + departure: time.Time{}.Add(6 * time.Millisecond), + arrival: time.Time{}.Add(34 * time.Millisecond), + }, + { + departure: time.Time{}.Add(8 * time.Millisecond), + arrival: time.Time{}.Add(30 * time.Millisecond), + }, + }, + }, + }, + { + name: "newGroupBecauseOfInterDepartureTime", + log: []acknowledgment{ + { + seqNr: 0, + departure: time.Time{}, + arrival: time.Time{}.Add(4 * time.Millisecond), + }, + { + seqNr: 1, + departure: time.Time{}.Add(3 * time.Millisecond), + arrival: time.Time{}.Add(4 * time.Millisecond), + }, + { + seqNr: 2, + departure: time.Time{}.Add(6 * time.Millisecond), + arrival: time.Time{}.Add(10 * time.Millisecond), + }, + { + seqNr: 3, + departure: time.Time{}.Add(9 * time.Millisecond), + arrival: time.Time{}.Add(10 * time.Millisecond), + }, + triggerNewGroupElement, + }, + exp: []arrivalGroup{ + { + { + seqNr: 0, + departure: time.Time{}, + arrival: time.Time{}.Add(4 * time.Millisecond), + }, + { + seqNr: 1, + departure: time.Time{}.Add(3 * time.Millisecond), + arrival: time.Time{}.Add(4 * time.Millisecond), + }, + }, + { + { + seqNr: 2, + departure: time.Time{}.Add(6 * time.Millisecond), + arrival: time.Time{}.Add(10 * time.Millisecond), + }, + { + seqNr: 3, + departure: time.Time{}.Add(9 * time.Millisecond), + arrival: time.Time{}.Add(10 * time.Millisecond), + }, + }, + }, + }, + } + + for _, tc := range cases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + aga := newArrivalGroupAccumulator() + received := []arrivalGroup{} + for _, ack := range tc.log { + next := aga.onPacketAcked(ack) + if next != nil { + received = append(received, next) + } + } + assert.Equal(t, tc.exp, received) + }) + } +} diff --git a/pkg/bwe/delay_rate_controller.go b/pkg/bwe/delay_rate_controller.go new file mode 100644 index 00000000..507b55bf --- /dev/null +++ b/pkg/bwe/delay_rate_controller.go @@ -0,0 +1,58 @@ +package bwe + +import ( + "log" + "time" +) + +type DelayRateController struct { + aga *arrivalGroupAccumulator + last arrivalGroup + kf *kalman + od *overuseDetector + rc *rateController + latest usage +} + +func NewDelayRateController(initialRate int) *DelayRateController { + return &DelayRateController{ + aga: newArrivalGroupAccumulator(), + last: []acknowledgment{}, + kf: newKalman(), + od: newOveruseDetector(true), + rc: newRateController(initialRate), + } +} + +func (c *DelayRateController) OnPacketAcked(ack acknowledgment) { + next := c.aga.onPacketAcked(ack) + if next == nil { + return + } + if len(next) == 0 { + // ignore empty groups, should never occur + return + } + if len(c.last) == 0 { + c.last = next + return + } + interArrivalTime := next[len(next)-1].arrival.Sub(c.last[len(c.last)-1].arrival) + interDepartureTime := next[0].departure.Sub(c.last[0].departure) + interGroupDelay := interArrivalTime - interDepartureTime + estimate := c.kf.updateEstimate(interGroupDelay) + c.latest = c.od.update(ack.arrival, estimate) + c.last = next + log.Printf( + "interArrivalTime=%v, interDepartureTime=%v, interGroupDelay=%v, estimate=%v, threshold=%v", + interArrivalTime.Nanoseconds(), + interDepartureTime.Nanoseconds(), + interGroupDelay.Nanoseconds(), + estimate.Nanoseconds(), + c.od.delayThreshold.Nanoseconds(), + ) +} + +func (c *DelayRateController) Update(ts time.Time, lastDeliveryRate int, rtt time.Duration) int { + return c.rc.update(ts, c.latest, lastDeliveryRate, rtt) +} diff --git a/pkg/bwe/delivery_rate_estimator.go b/pkg/bwe/delivery_rate_estimator.go new file mode 100644 index 00000000..9d18e3a7 --- /dev/null +++ b/pkg/bwe/delivery_rate_estimator.go @@ -0,0 +1,87 @@ +package bwe + +import ( + "container/heap" + "time" +) + +type deliveryRateHeapItem struct { + arrival time.Time + size int +} + +type deliveryRateHeap []deliveryRateHeapItem + +// Len implements heap.Interface. +func (d deliveryRateHeap) Len() int { + return len(d) +} + +// Less implements heap.Interface. +func (d deliveryRateHeap) Less(i int, j int) bool { + return d[i].arrival.Before(d[j].arrival) +} + +// Pop implements heap.Interface. +func (d *deliveryRateHeap) Pop() any { + old := *d + n := len(old) + x := old[n-1] + *d = old[0 : n-1] + return x +} + +// Push implements heap.Interface. +func (d *deliveryRateHeap) Push(x any) { + *d = append(*d, x.(deliveryRateHeapItem)) +} + +// Swap implements heap.Interface. +func (d deliveryRateHeap) Swap(i int, j int) { + d[i], d[j] = d[j], d[i] +} + +type deliveryRateEstimator struct { + window time.Duration + latestArrival time.Time + history *deliveryRateHeap +} + +func newDeliveryRateEstimator(window time.Duration) *deliveryRateEstimator { + return &deliveryRateEstimator{ + window: window, + latestArrival: time.Time{}, + history: &deliveryRateHeap{}, + } +} + +func (e *deliveryRateEstimator) OnPacketAcked(arrival time.Time, size int) { + if arrival.After(e.latestArrival) { + e.latestArrival = arrival + } + heap.Push(e.history, deliveryRateHeapItem{ + arrival: arrival, + size: size, + }) +} + +func (e *deliveryRateEstimator) GetRate() int { + deadline := e.latestArrival.Add(-e.window) + for len(*e.history) > 0 && (*e.history)[0].arrival.Before(deadline) { + heap.Pop(e.history) + } + earliest := e.latestArrival + sum := 0 + for _, i := range *e.history { + if i.arrival.Before(earliest) { + earliest = i.arrival + } + sum += i.size + } + d := e.latestArrival.Sub(earliest) + if d == 0 { + return 0 + } + rate := 8 * float64(sum) / d.Seconds() + return int(rate) +} diff --git a/pkg/bwe/delivery_rate_estimator_test.go b/pkg/bwe/delivery_rate_estimator_test.go new file mode 100644 index 00000000..15ea3665 --- /dev/null +++ b/pkg/bwe/delivery_rate_estimator_test.go @@ -0,0 +1,74 @@ +package bwe + +import ( + "fmt" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestDeliveryRateEstimator(t *testing.T) { + type ack struct { + arrival time.Time + size int + } + cases := []struct { + window time.Duration + acks []ack + expectedRate int + }{ + { + window: 0, + acks: []ack{}, + expectedRate: 0, + }, + { + window: time.Second, + acks: []ack{}, + expectedRate: 0, + }, + { + window: time.Second, + acks: []ack{ + {time.Time{}, 1200}, + }, + expectedRate: 0, + }, + { + window: time.Second, + acks: []ack{ + {time.Time{}.Add(time.Millisecond), 1200}, + }, + expectedRate: 0, + }, + { + window: time.Second, + acks: []ack{ + {time.Time{}.Add(time.Second), 1200}, + {time.Time{}.Add(1500 * time.Millisecond), 1200}, + {time.Time{}.Add(2 * time.Second), 1200}, + }, + expectedRate: 28800, + }, + { + window: time.Second, + acks: []ack{ + {time.Time{}.Add(500 * time.Millisecond), 1200}, + {time.Time{}.Add(time.Second), 1200}, + {time.Time{}.Add(1500 * time.Millisecond), 1200}, + {time.Time{}.Add(2 * time.Second), 1200}, + }, + expectedRate: 28800, + }, + } + for i, tc := range cases { + t.Run(fmt.Sprintf("%v", i), func(t *testing.T) { + e := newDeliveryRateEstimator(tc.window) + for _, ack := range tc.acks { + e.OnPacketAcked(ack.arrival, ack.size) + } + assert.Equal(t, tc.expectedRate, e.GetRate()) + }) + } +} diff --git a/pkg/bwe/duplicate_ack_filter.go b/pkg/bwe/duplicate_ack_filter.go new file mode 100644 index 00000000..e6753c28 --- /dev/null +++ b/pkg/bwe/duplicate_ack_filter.go @@ -0,0 +1,27 @@ +package bwe + +import "github.com/pion/interceptor/pkg/ccfb" + +type duplicateAckFilter struct { + highestAckedBySSRC map[uint32]int64 +} + +func newDuplicateAckFilter() *duplicateAckFilter { + return &duplicateAckFilter{ + highestAckedBySSRC: make(map[uint32]int64), + } +} + +func (f *duplicateAckFilter) filter(reports map[uint32]*ccfb.PacketReportList) { + for ssrc, prl := range reports { + n := 0 + for _, report := range prl.Reports { + if highest, ok := f.highestAckedBySSRC[ssrc]; !ok || report.SeqNr > highest { + f.highestAckedBySSRC[ssrc] = report.SeqNr + prl.Reports[n] = report + n++ + } + } + prl.Reports = prl.Reports[:n] + } +} diff --git a/pkg/bwe/duplicate_ack_filter_test.go b/pkg/bwe/duplicate_ack_filter_test.go new file mode 100644 index 00000000..3813e96e --- /dev/null +++ b/pkg/bwe/duplicate_ack_filter_test.go @@ -0,0 +1,113 @@ +package bwe + +import ( + "fmt" + "testing" + "time" + + "github.com/pion/interceptor/pkg/ccfb" + "github.com/stretchr/testify/assert" +) + +func TestDuplicateAckFilter(t *testing.T) { + cases := []struct { + in []map[uint32]*ccfb.PacketReportList + expect []map[uint32]*ccfb.PacketReportList + }{ + { + in: []map[uint32]*ccfb.PacketReportList{}, + expect: []map[uint32]*ccfb.PacketReportList{}, + }, + { + in: []map[uint32]*ccfb.PacketReportList{ + { + 0: &ccfb.PacketReportList{ + Arrival: time.Time{}, + Departure: time.Time{}, + Reports: []ccfb.PacketReport{}, + }, + }, + }, + expect: []map[uint32]*ccfb.PacketReportList{ + { + 0: &ccfb.PacketReportList{ + Arrival: time.Time{}, + Departure: time.Time{}, + Reports: []ccfb.PacketReport{}, + }, + }, + }, + }, + { + in: []map[uint32]*ccfb.PacketReportList{ + { + 0: &ccfb.PacketReportList{ + Arrival: time.Time{}, + Departure: time.Time{}, + Reports: []ccfb.PacketReport{ + { + SeqNr: 1, + }, + { + SeqNr: 2, + }, + }, + }, + }, + { + 0: &ccfb.PacketReportList{ + Arrival: time.Time{}, + Departure: time.Time{}, + Reports: []ccfb.PacketReport{ + { + SeqNr: 1, + }, + { + SeqNr: 2, + }, + { + SeqNr: 3, + }, + }, + }, + }, + }, + expect: []map[uint32]*ccfb.PacketReportList{ + { + 0: &ccfb.PacketReportList{ + Arrival: time.Time{}, + Departure: time.Time{}, + Reports: []ccfb.PacketReport{ + { + SeqNr: 1, + }, + { + SeqNr: 2, + }, + }, + }, + }, + { + 0: &ccfb.PacketReportList{ + Arrival: time.Time{}, + Departure: time.Time{}, + Reports: []ccfb.PacketReport{ + { + SeqNr: 3, + }, + }, + }, + }, + }, + }, + } + for i, tc := range cases { + t.Run(fmt.Sprintf("%v", i), func(t *testing.T) { + daf := newDuplicateAckFilter() + for i, m := range tc.in { + daf.filter(m) + assert.Equal(t, tc.expect[i], m) + } + }) + } +} diff --git a/pkg/bwe/exponential_moving_average.go b/pkg/bwe/exponential_moving_average.go new file mode 100644 index 00000000..9971a1e7 --- /dev/null +++ b/pkg/bwe/exponential_moving_average.go @@ -0,0 +1,19 @@ +package bwe + +type exponentialMovingAverage struct { + initialized bool + alpha float64 + average float64 + variance float64 +} + +func (a *exponentialMovingAverage) update(sample float64) { + if !a.initialized { + a.average = sample + a.initialized = true + } else { + delta := sample - a.average + a.average = a.alpha*sample + (1-a.alpha)*a.average + a.variance = (1-a.alpha)*a.variance + a.alpha*(1-a.alpha)*(delta*delta) + } +} diff --git a/pkg/bwe/exponential_moving_average_test.go b/pkg/bwe/exponential_moving_average_test.go new file mode 100644 index 00000000..89976b51 --- /dev/null +++ b/pkg/bwe/exponential_moving_average_test.go @@ -0,0 +1,122 @@ +package bwe + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +// python to generate test cases: +// import numpy as np +// import pandas as pd +// data = np.random.randint(1, 10, size=10) +// df = pd.DataFrame(data) +// expectedAvg = df.ewm(alpha=0.9, adjust=False).mean() +// expectedVar = df.ewm(alpha=0.9, adjust=False).var(bias=True) + +func TestExponentialMovingAverage(t *testing.T) { + cases := []struct { + alpha float64 + updates []float64 + expectedAvg []float64 + expectedVar []float64 + }{ + { + alpha: 0.9, + updates: []float64{}, + expectedAvg: []float64{}, + expectedVar: []float64{}, + }, + { + alpha: 0.9, + updates: []float64{1, 2, 3, 4}, + expectedAvg: []float64{ + 1.000, + 1.900, + 2.890, + 3.889, + }, + expectedVar: []float64{ + 0.000000, + 0.090000, + 0.117900, + 0.122679, + }, + }, + { + alpha: 0.9, + updates: []float64{8, 8, 5, 1, 3, 1, 8, 2, 8, 9}, + expectedAvg: []float64{ + 8.000000, + 8.000000, + 5.300000, + 1.430000, + 2.843000, + 1.184300, + 7.318430, + 2.531843, + 7.453184, + 8.845318, + }, + expectedVar: []float64{ + 0.000000, + 0.000000, + 0.810000, + 1.745100, + 0.396351, + 0.345334, + 4.215372, + 2.967250, + 2.987792, + 0.514117, + }, + }, + { + alpha: 0.9, + updates: []float64{7, 5, 6, 7, 3, 6, 8, 9, 5, 5}, + expectedAvg: []float64{ + 7.000000, + 5.200000, + 5.920000, + 6.892000, + 3.389200, + 5.738920, + 7.773892, + 8.877389, + 5.387739, + 5.038774, + }, + expectedVar: []float64{ + 0.000000, + 0.360000, + 0.093600, + 0.114336, + 1.374723, + 0.750937, + 0.535217, + 0.188822, + 1.371955, + 0.150726, + }, + }, + } + for i, tc := range cases { + t.Run(fmt.Sprintf("%v", i), func(t *testing.T) { + a := exponentialMovingAverage{ + alpha: tc.alpha, + average: 0, + variance: 0, + } + avgs := []float64{} + vars := []float64{} + for _, u := range tc.updates { + a.update(u) + avgs = append(avgs, a.average) + vars = append(vars, a.variance) + } + assert.InDeltaSlice(t, tc.expectedAvg, avgs, 0.001) + assert.InDeltaSlice(t, tc.expectedVar, vars, 0.001) + }) + } +} diff --git a/pkg/bwe/kalman.go b/pkg/bwe/kalman.go new file mode 100644 index 00000000..d6e7dd43 --- /dev/null +++ b/pkg/bwe/kalman.go @@ -0,0 +1,92 @@ +package bwe + +import ( + "math" + "time" +) + +const ( + chi = 0.001 +) + +type kalmanOption func(*kalman) + +type kalman struct { + gain float64 + estimate time.Duration + processUncertainty float64 // Q_i + estimateError float64 + measurementUncertainty float64 + + disableMeasurementUncertaintyUpdates bool +} + +func initEstimate(e time.Duration) kalmanOption { + return func(k *kalman) { + k.estimate = e + } +} + +func initProcessUncertainty(p float64) kalmanOption { + return func(k *kalman) { + k.processUncertainty = p + } +} + +func initEstimateError(e float64) kalmanOption { + return func(k *kalman) { + k.estimateError = e * e // Only need variance from now on + } +} + +func initMeasurementUncertainty(u float64) kalmanOption { + return func(k *kalman) { + k.measurementUncertainty = u + } +} + +func setDisableMeasurementUncertaintyUpdates(b bool) kalmanOption { + return func(k *kalman) { + k.disableMeasurementUncertaintyUpdates = b + } +} + +func newKalman(opts ...kalmanOption) *kalman { + k := &kalman{ + gain: 0, + estimate: 0, + processUncertainty: 1e-3, + estimateError: 0.1, + measurementUncertainty: 0, + disableMeasurementUncertaintyUpdates: false, + } + for _, opt := range opts { + opt(k) + } + return k +} + +func (k *kalman) updateEstimate(measurement time.Duration) time.Duration { + z := measurement - k.estimate + + zms := float64(z.Microseconds()) / 1000.0 + + if !k.disableMeasurementUncertaintyUpdates { + alpha := math.Pow((1 - chi), 30.0/(1000.0*5*float64(time.Millisecond))) + root := math.Sqrt(k.measurementUncertainty) + root3 := 3 * root + if zms > root3 { + k.measurementUncertainty = math.Max(alpha*k.measurementUncertainty+(1-alpha)*root3*root3, 1) + } else { + k.measurementUncertainty = math.Max(alpha*k.measurementUncertainty+(1-alpha)*zms*zms, 1) + } + } + + estimateUncertainty := k.estimateError + k.processUncertainty + k.gain = estimateUncertainty / (estimateUncertainty + k.measurementUncertainty) + + k.estimate += time.Duration(k.gain * zms * float64(time.Millisecond)) + + k.estimateError = (1 - k.gain) * estimateUncertainty + return k.estimate +} diff --git a/pkg/bwe/kalman_test.go b/pkg/bwe/kalman_test.go new file mode 100644 index 00000000..0d7968d3 --- /dev/null +++ b/pkg/bwe/kalman_test.go @@ -0,0 +1,68 @@ +package bwe + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestKalman(t *testing.T) { + cases := []struct { + name string + opts []kalmanOption + measurements []time.Duration + expected []time.Duration + }{ + { + name: "empty", + opts: []kalmanOption{}, + measurements: []time.Duration{}, + expected: []time.Duration{}, + }, + { + name: "kalmanfilter.netExample", + opts: []kalmanOption{ + initEstimate(10 * time.Millisecond), + initEstimateError(100), + initProcessUncertainty(0.15), + initMeasurementUncertainty(0.01), + }, + measurements: []time.Duration{ + time.Duration(50.45 * float64(time.Millisecond)), + time.Duration(50.967 * float64(time.Millisecond)), + time.Duration(51.6 * float64(time.Millisecond)), + time.Duration(52.106 * float64(time.Millisecond)), + time.Duration(52.492 * float64(time.Millisecond)), + time.Duration(52.819 * float64(time.Millisecond)), + time.Duration(53.433 * float64(time.Millisecond)), + time.Duration(54.007 * float64(time.Millisecond)), + time.Duration(54.523 * float64(time.Millisecond)), + time.Duration(54.99 * float64(time.Millisecond)), + }, + expected: []time.Duration{ + time.Duration(50.449959 * float64(time.Millisecond)), + time.Duration(50.936547 * float64(time.Millisecond)), + time.Duration(51.560411 * float64(time.Millisecond)), + time.Duration(52.07324 * float64(time.Millisecond)), + time.Duration(52.466566 * float64(time.Millisecond)), + time.Duration(52.797787 * float64(time.Millisecond)), + time.Duration(53.395303 * float64(time.Millisecond)), + time.Duration(53.970236 * float64(time.Millisecond)), + time.Duration(54.489652 * float64(time.Millisecond)), + time.Duration(54.960137 * float64(time.Millisecond)), + }, + }, + } + for _, tc := range cases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + k := newKalman(append(tc.opts, setDisableMeasurementUncertaintyUpdates(true))...) + estimates := []time.Duration{} + for _, m := range tc.measurements { + estimates = append(estimates, k.updateEstimate(m)) + } + assert.Equal(t, tc.expected, estimates, "%v != %v", tc.expected, estimates) + }) + } +} diff --git a/pkg/bwe/loss_rate_controller.go b/pkg/bwe/loss_rate_controller.go new file mode 100644 index 00000000..e0b08cb6 --- /dev/null +++ b/pkg/bwe/loss_rate_controller.go @@ -0,0 +1,48 @@ +package bwe + +type LossRateController struct { + bitrate int + min, max int + + packetsSinceLastUpdate int + arrivedSinceLastUpdate int + lostSinceLastUpdate int +} + +func NewLossRateController(initialRate, minRate, maxRate int) *LossRateController { + return &LossRateController{ + bitrate: initialRate, + min: minRate, + max: maxRate, + packetsSinceLastUpdate: 0, + arrivedSinceLastUpdate: 0, + lostSinceLastUpdate: 0, + } +} + +func (l *LossRateController) OnPacketAcked() { + l.packetsSinceLastUpdate++ + l.arrivedSinceLastUpdate++ +} + +func (l *LossRateController) OnPacketLost() { + l.packetsSinceLastUpdate++ + l.lostSinceLastUpdate++ +} + +func (l *LossRateController) Update(lastDeliveryRate int) int { + lossRate := float64(l.lostSinceLastUpdate) / float64(l.packetsSinceLastUpdate) + canIncrease := float64(lastDeliveryRate) >= 0.95*float64(l.bitrate) + if lossRate > 0.1 { + l.bitrate = int(float64(l.bitrate) * (1 - 0.5*lossRate)) + } else if lossRate < 0.02 && canIncrease { + l.bitrate = int(float64(l.bitrate) * 1.05) + } + l.bitrate = max(min(l.bitrate, l.max), l.min) + + l.packetsSinceLastUpdate = 0 + l.arrivedSinceLastUpdate = 0 + l.lostSinceLastUpdate = 0 + + return l.bitrate +} diff --git a/pkg/bwe/loss_rate_controller_test.go b/pkg/bwe/loss_rate_controller_test.go new file mode 100644 index 00000000..8b6ac8e7 --- /dev/null +++ b/pkg/bwe/loss_rate_controller_test.go @@ -0,0 +1,86 @@ +package bwe + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestLossRateController(t *testing.T) { + cases := []struct { + init, min, max int + acked int + lost int + deliveredRate int + expectedRate int + }{ + {}, // all zeros + { + init: 100_000, + min: 100_000, + max: 1_000_000, + acked: 0, + lost: 0, + deliveredRate: 0, + expectedRate: 100_000, + }, + { + init: 100_000, + min: 100_000, + max: 1_000_000, + acked: 99, + lost: 1, + deliveredRate: 100_000, + expectedRate: 105_000, + }, + { + init: 100_000, + min: 100_000, + max: 1_000_000, + acked: 99, + lost: 1, + deliveredRate: 90_000, + expectedRate: 100_000, + }, + { + init: 100_000, + min: 100_000, + max: 1_000_000, + acked: 95, + lost: 5, + deliveredRate: 99_000, + expectedRate: 100_000, + }, + { + init: 100_000, + min: 50_000, + max: 1_000_000, + acked: 89, + lost: 11, + deliveredRate: 90_000, + expectedRate: 94_500, + }, + { + init: 100_000, + min: 100_000, + max: 1_000_000, + acked: 89, + lost: 11, + deliveredRate: 90_000, + expectedRate: 100_000, + }, + } + for i, tc := range cases { + t.Run(fmt.Sprintf("%v", i), func(t *testing.T) { + lrc := NewLossRateController(tc.init, tc.min, tc.max) + for i := 0; i < tc.acked; i++ { + lrc.OnPacketAcked() + } + for i := 0; i < tc.lost; i++ { + lrc.OnPacketLost() + } + assert.Equal(t, tc.expectedRate, lrc.Update(tc.deliveredRate)) + }) + } +} diff --git a/pkg/bwe/overuse_detector.go b/pkg/bwe/overuse_detector.go new file mode 100644 index 00000000..dd096fc8 --- /dev/null +++ b/pkg/bwe/overuse_detector.go @@ -0,0 +1,83 @@ +package bwe + +import ( + "time" +) + +const ( + kU = 0.01 + kD = 0.00018 +) + +type overuseDetector struct { + adaptiveThreshold bool + overUseTimeThreshold time.Duration + delayThreshold time.Duration + lastEstimate time.Duration + lastUpdate time.Time + firstOverUse time.Time + inOveruse bool +} + +func newOveruseDetector(adaptive bool) *overuseDetector { + return &overuseDetector{ + adaptiveThreshold: adaptive, + overUseTimeThreshold: 10 * time.Millisecond, + delayThreshold: 12500 * time.Microsecond, + lastEstimate: 0, + lastUpdate: time.Time{}, + firstOverUse: time.Time{}, + inOveruse: false, + } +} + +func (d *overuseDetector) update(ts time.Time, estimate time.Duration) usage { + if d.adaptiveThreshold { + defer d.adaptThreshold(ts, estimate) + } + if estimate >= d.lastEstimate && estimate > d.delayThreshold { + if d.inOveruse && ts.Sub(d.firstOverUse) > d.overUseTimeThreshold { + return usageOver + } + if !d.inOveruse { + d.firstOverUse = ts + } + d.inOveruse = true + return usageNormal + } + if estimate < -d.delayThreshold { + d.inOveruse = false + return usageUnder + } + d.inOveruse = false + return usageNormal +} + +func (d *overuseDetector) adaptThreshold(ts time.Time, estimate time.Duration) { + delta := ts.Sub(d.lastUpdate).Seconds() + d.lastUpdate = ts + absEstimate := estimate + if absEstimate < 0 { + absEstimate = -absEstimate + } + x := absEstimate - d.delayThreshold + if x > 15*time.Millisecond { + return + } + var k float64 + if absEstimate < d.delayThreshold { + k = kD + } else { + k = kU + } + diff := float64((absEstimate - d.delayThreshold).Nanoseconds()) + inc := k * delta * diff + incDuration := time.Duration(inc) + d.delayThreshold = d.delayThreshold + incDuration + if d.delayThreshold < 6*time.Millisecond { + d.delayThreshold = 6 * time.Millisecond + } + if d.delayThreshold > 600*time.Millisecond { + d.delayThreshold = 600 * time.Millisecond + } +} diff --git a/pkg/bwe/overuse_detector_test.go b/pkg/bwe/overuse_detector_test.go new file mode 100644 index 00000000..86603b18 --- /dev/null +++ b/pkg/bwe/overuse_detector_test.go @@ -0,0 +1,185 @@ +package bwe + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestOveruseDetectorUpdate(t *testing.T) { + type estimate struct { + ts time.Time + estimate time.Duration + } + cases := []struct { + name string + adaptive bool + values []estimate + expected []usage + }{ + { + name: "noEstimateNoUsageStatic", + adaptive: false, + values: []estimate{}, + expected: []usage{}, + }, + { + name: "overuseStatic", + adaptive: false, + values: []estimate{ + {time.Time{}, time.Millisecond}, + {time.Time{}.Add(5 * time.Millisecond), 20 * time.Millisecond}, + {time.Time{}.Add(20 * time.Millisecond), 30 * time.Millisecond}, + }, + expected: []usage{usageNormal, usageNormal, usageOver}, + }, + { + name: "normaluseStatic", + adaptive: false, + values: []estimate{{estimate: 0}}, + expected: []usage{usageNormal}, + }, + { + name: "underuseStatic", + adaptive: false, + values: []estimate{{estimate: -20 * time.Millisecond}}, + expected: []usage{usageUnder}, + }, + { + name: "noOverUseBeforeDelayStatic", + adaptive: false, + values: []estimate{ + {time.Time{}.Add(time.Millisecond), 20 * time.Millisecond}, + {time.Time{}.Add(2 * time.Millisecond), 30 * time.Millisecond}, + {time.Time{}.Add(30 * time.Millisecond), 50 * time.Millisecond}, + }, + expected: []usage{usageNormal, usageNormal, usageOver}, + }, + { + name: "noOverUseIfEstimateDecreasedStatic", + adaptive: false, + values: []estimate{ + {time.Time{}.Add(time.Millisecond), 20 * time.Millisecond}, + {time.Time{}.Add(10 * time.Millisecond), 40 * time.Millisecond}, + {time.Time{}.Add(20 * time.Millisecond), 50 * time.Millisecond}, + {time.Time{}.Add(30 * time.Millisecond), 3 * time.Millisecond}, + }, + expected: []usage{usageNormal, usageNormal, usageOver, usageNormal}, + }, + { + name: "noEstimateNoUsageAdaptive", + adaptive: true, + values: []estimate{}, + expected: []usage{}, + }, + { + name: "overuseAdaptive", + adaptive: true, + values: []estimate{ + {time.Time{}, time.Millisecond}, + {time.Time{}.Add(5 * time.Millisecond), 20 * time.Millisecond}, + {time.Time{}.Add(20 * time.Millisecond), 30 * time.Millisecond}, + }, + expected: []usage{usageNormal, usageNormal, usageOver}, + }, + { + name: "normaluseAdaptive", + adaptive: true, + values: []estimate{{estimate: 0}}, + expected: []usage{usageNormal}, + }, + { + name: "underuseAdaptive", + adaptive: true, + values: []estimate{{estimate: -20 * time.Millisecond}}, + expected: []usage{usageUnder}, + }, + { + name: "noOverUseBeforeDelayAdaptive", + adaptive: true, + values: []estimate{ + {time.Time{}.Add(time.Millisecond), 20 * time.Millisecond}, + {time.Time{}.Add(2 * time.Millisecond), 30 * time.Millisecond}, + {time.Time{}.Add(30 * time.Millisecond), 50 * time.Millisecond}, + }, + expected: []usage{usageNormal, usageNormal, usageOver}, + }, + { + name: "noOverUseIfEstimateDecreasedAdaptive", + adaptive: true, + values: []estimate{ + {time.Time{}.Add(time.Millisecond), 20 * time.Millisecond}, + {time.Time{}.Add(10 * time.Millisecond), 40 * time.Millisecond}, + {time.Time{}.Add(20 * time.Millisecond), 50 * time.Millisecond}, + {time.Time{}.Add(30 * time.Millisecond), 3 * time.Millisecond}, + }, + expected: []usage{usageNormal, usageNormal, usageOver, usageNormal}, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + od := newOveruseDetector(tc.adaptive) + received := []usage{} + for _, e := range tc.values { + usage := od.update(e.ts, e.estimate) + received = append(received, usage) + } + assert.Equal(t, tc.expected, received) + }) + } +} + +func TestOveruseDetectorAdaptThreshold(t *testing.T) { + cases := []struct { + name string + od *overuseDetector + ts time.Time + estimate time.Duration + expectedThreshold time.Duration + }{ + { + name: "minThreshold", + od: &overuseDetector{}, + ts: time.Time{}, + estimate: 0, + expectedThreshold: 6 * time.Millisecond, + }, + { + name: "increase", + od: &overuseDetector{ + delayThreshold: 12500 * time.Microsecond, + lastUpdate: time.Time{}, + }, + ts: time.Time{}.Add(time.Second), + estimate: 25 * time.Millisecond, + expectedThreshold: 12625 * time.Microsecond, + }, + { + name: "maxThreshold", + od: &overuseDetector{ + delayThreshold: 600 * time.Millisecond, + lastUpdate: time.Time{}, + }, + ts: time.Time{}.Add(time.Second), + estimate: 610 * time.Millisecond, + expectedThreshold: 600 * time.Millisecond, + }, + { + name: "decrease", + od: &overuseDetector{ + delayThreshold: 12500 * time.Microsecond, + lastUpdate: time.Time{}, + }, + ts: time.Time{}.Add(time.Second), + estimate: time.Millisecond, + expectedThreshold: time.Duration(12497930), + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + tc.od.adaptThreshold(tc.ts, tc.estimate) + assert.Equal(t, tc.expectedThreshold, tc.od.delayThreshold) + }) + } +} diff --git a/pkg/bwe/rate_controller.go b/pkg/bwe/rate_controller.go new file mode 100644 index 00000000..a9a6f6c1 --- /dev/null +++ b/pkg/bwe/rate_controller.go @@ -0,0 +1,76 @@ +package bwe + +import ( + "math" + "time" +) + +type rateController struct { + s state + rate int + + decreaseFactor float64 // (beta) + lastUpdate time.Time + lastDecrease *exponentialMovingAverage +} + +func newRateController(initialRate int) *rateController { + return &rateController{ + s: stateIncrease, + rate: initialRate, + decreaseFactor: 0.85, + lastUpdate: time.Time{}, + lastDecrease: &exponentialMovingAverage{}, + } +} + +func (c *rateController) update(ts time.Time, u usage, deliveredRate int, rtt time.Duration) int { + nextState := c.s.transition(u) + c.s = nextState + + if c.s == stateIncrease { + var target float64 + if c.canIncreaseMultiplicatively(float64(deliveredRate)) { + window := ts.Sub(c.lastUpdate) + target = c.multiplicativeIncrease(float64(c.rate), window) + } else { + bitsPerFrame := float64(c.rate) / 30.0 + packetsPerFrame := math.Ceil(bitsPerFrame / (1200 * 8)) + expectedPacketSizeBits := bitsPerFrame / packetsPerFrame + target = c.additiveIncrease(float64(c.rate), int(expectedPacketSizeBits), rtt) + } + c.rate = int(min(target, 1.5*float64(deliveredRate))) + } + + if c.s == stateDecrease { + c.rate = int(c.decreaseFactor * float64(deliveredRate)) + c.lastDecrease.update(float64(c.rate)) + } + + c.lastUpdate = ts + + return c.rate +} + +func (c *rateController) canIncreaseMultiplicatively(deliveredRate float64) bool { + if c.lastDecrease.average == 0 { + return true + } + stdDev := math.Sqrt(c.lastDecrease.variance) + lower := c.lastDecrease.average - 3*stdDev + upper := c.lastDecrease.average + 3*stdDev + return deliveredRate < lower || deliveredRate > upper +} + +func (c *rateController) multiplicativeIncrease(rate float64, window time.Duration) float64 { + exponent := min(window.Seconds(), 1.0) + eta := math.Pow(1.08, exponent) + target := eta * rate + return target +} + +func (c *rateController) additiveIncrease(rate float64, expectedPacketSizeBits int, window time.Duration) float64 { + alpha := 0.5 * min(window.Seconds(), 1.0) + target := rate + max(1000, alpha*float64(expectedPacketSizeBits)) + return target +} diff --git a/pkg/bwe/send_side_bwe.go b/pkg/bwe/send_side_bwe.go new file mode 100644 index 00000000..21580439 --- /dev/null +++ b/pkg/bwe/send_side_bwe.go @@ -0,0 +1,76 @@ +package bwe + +import ( + "log" + "time" + + "github.com/pion/interceptor/pkg/ccfb" +) + +type SendSideController struct { + daf *duplicateAckFilter + dre *deliveryRateEstimator + lbc *LossRateController + drc *DelayRateController + rate int +} + +func NewSendSideController(initialRate, minRate, maxRate int) *SendSideController { + return &SendSideController{ + daf: newDuplicateAckFilter(), + dre: newDeliveryRateEstimator(time.Second), + lbc: NewLossRateController(initialRate, minRate, maxRate), + drc: NewDelayRateController(initialRate), + } +} + +func (c *SendSideController) OnFeedbackReport(reports map[uint32]*ccfb.PacketReportList) int { + c.daf.filter(reports) + + var latestReportArrival time.Time + var latestReportDeparture time.Time + var lastAckedArrival time.Time + var lastAckedPacketDeparture time.Time + + acksCount := 0 + for _, prl := range reports { + if prl.Arrival.After(latestReportArrival) { + latestReportArrival = prl.Arrival + latestReportDeparture = prl.Departure + } + for _, r := range prl.Reports { + if r.Arrived && !r.Arrival.IsZero() { // in some cases we might receive acks without timestamps. Ignore them. + c.dre.OnPacketAcked(r.Arrival, int(r.Size)) + c.lbc.OnPacketAcked() + c.drc.OnPacketAcked(acknowledgment{ + seqNr: r.SeqNr, + size: r.Size, + departure: r.Departure, + arrived: r.Arrived, + arrival: r.Arrival, + ecn: r.ECN, + }) + if r.Departure.After(lastAckedPacketDeparture) { + lastAckedPacketDeparture = r.Departure + lastAckedArrival = r.Arrival + } + acksCount++ + } else { + c.lbc.OnPacketLost() + } + } + } + if acksCount == 0 { + return c.rate + } + + pendingTime := latestReportDeparture.Sub(lastAckedArrival) + rtt := latestReportArrival.Sub(lastAckedPacketDeparture) - pendingTime + + delivered := c.dre.GetRate() + lossTarget := c.lbc.Update(delivered) + delayTarget := c.drc.Update(latestReportArrival, delivered, rtt) + c.rate = min(lossTarget, delayTarget) + log.Printf("rtt=%v, delivered=%v, lossTarget=%v, delayTarget=%v, target=%v", rtt.Microseconds(), delivered, lossTarget, delayTarget, c.rate) + return c.rate +} diff --git a/pkg/bwe/state.go b/pkg/bwe/state.go new file mode 100644 index 00000000..70f90069 --- /dev/null +++ b/pkg/bwe/state.go @@ -0,0 +1,59 @@ +package bwe + +import "fmt" + +type state int + +const ( + stateIncrease state = iota + stateDecrease + stateHold +) + +func (s state) transition(u usage) state { + switch s { + case stateHold: + switch u { + case usageOver: + return stateDecrease + case usageNormal: + return stateIncrease + case usageUnder: + return stateHold + } + + case stateIncrease: + switch u { + case usageOver: + return stateDecrease + case usageNormal: + return stateIncrease + case usageUnder: + return stateHold + } + + case stateDecrease: + switch u { + case usageOver: + return stateDecrease + case usageNormal: + return stateHold + case usageUnder: + return stateHold + } + } + return stateIncrease +} + +func (s state) String() string { + switch s { + case stateIncrease: + return "increase" + case stateDecrease: + return "decrease" + case stateHold: + return "hold" + default: + return fmt.Sprintf("invalid state: %d", s) + } +} diff --git a/pkg/bwe/state_test.go b/pkg/bwe/state_test.go new file mode 100644 index 00000000..ecc8d1a0 --- /dev/null +++ b/pkg/bwe/state_test.go @@ -0,0 +1,27 @@ +package bwe + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestState(t *testing.T) { + t.Run("hold", func(t *testing.T) { + assert.Equal(t, stateDecrease, stateHold.transition(usageOver)) + assert.Equal(t, stateIncrease, stateHold.transition(usageNormal)) + assert.Equal(t, stateHold, stateHold.transition(usageUnder)) + }) + + t.Run("increase", func(t *testing.T) { + assert.Equal(t, stateDecrease, stateIncrease.transition(usageOver)) + assert.Equal(t, stateIncrease, stateIncrease.transition(usageNormal)) + assert.Equal(t, stateHold, stateIncrease.transition(usageUnder)) + }) + + t.Run("decrease", func(t *testing.T) { + assert.Equal(t, stateDecrease, stateDecrease.transition(usageOver)) + assert.Equal(t, stateHold, stateDecrease.transition(usageNormal)) + assert.Equal(t, stateHold, stateDecrease.transition(usageUnder)) + }) +} diff --git a/pkg/bwe/usage.go b/pkg/bwe/usage.go new file mode 100644 index 00000000..0c520950 --- /dev/null +++ b/pkg/bwe/usage.go @@ -0,0 +1,24 @@ +package bwe + +import "fmt" + +type usage int + +const ( + usageOver usage = iota + usageUnder + usageNormal +) + +func (u usage) String() string { + switch u { + case usageOver: + return "overuse" + case usageUnder: + return "underuse" + case usageNormal: + return "normal" + default: + return fmt.Sprintf("invalid usage: %d", u) + } +}