vidaptive 0.1.0

Responsive rate control for real-time video
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! Copa delay-based congestion control (adapted from TQUIC).
//!
//! See <https://web.mit.edu/copa/>.

use alloc::collections::{BTreeMap, VecDeque};
use core::time::Duration;
use std::time::Instant;

use crate::cc::CongestionController;
use crate::types::{AckInfo, LostPacket, SentPacket};

// δ = 0.9 in the Vidaptive reference implementation.
const DEFAULT_DELTA: f64 = 0.9;

const SPEED_UP_THRESHOLD: u64 = 3;
const STANDING_RTT_WINDOW: Duration = Duration::from_millis(100);
const MIN_RTT_WINDOW: Duration = Duration::from_secs(10);
const PACING_GAIN: u64 = 2;
const LOSS_RATE_THRESHOLD: f64 = 0.1;
const DEFAULT_INITIAL_RTT: Duration = Duration::from_millis(50);
const DEFAULT_PACKET_SIZE: u64 = 1_200;

/// Copa configuration.
#[derive(Debug, Clone)]
pub struct CopaConfig {
    /// Minimum cwnd in bytes.
    pub min_cwnd: u64,
    /// Initial cwnd in bytes.
    pub initial_cwnd: u64,
    /// Initial RTT guess.
    pub initial_rtt: Duration,
    /// Reference packet size for target-rate calculation.
    pub packet_size: u64,
    /// δ in slow start.
    pub slow_start_delta: f64,
    /// δ in steady state.
    pub steady_delta: f64,
    /// Use standing RTT window = srtt (true) or srtt/2 (false).
    pub use_standing_rtt: bool,
}

impl Default for CopaConfig {
    fn default() -> Self {
        Self {
            min_cwnd: 4 * DEFAULT_PACKET_SIZE,
            initial_cwnd: 80 * DEFAULT_PACKET_SIZE,
            initial_rtt: DEFAULT_INITIAL_RTT,
            packet_size: DEFAULT_PACKET_SIZE,
            slow_start_delta: DEFAULT_DELTA,
            steady_delta: DEFAULT_DELTA,
            use_standing_rtt: true,
        }
    }
}

/// Copa congestion controller.
#[derive(Debug)]
pub struct Copa {
    config: CopaConfig,
    init_time: Instant,
    cwnd: u64,
    slow_start: bool,
    mode: CompetingMode,
    delta: f64,
    velocity: Velocity,
    standing_rtt: MinMax,
    min_rtt: MinMax,
    rtt: RttEstimator,
    ack: AckState,
    increase_cwnd: bool,
    target_rate: u64,
    round: Round,
    last_sent_seq: u64,
    bytes_in_flight: u64,
    bytes_acked_total: u64,
    bytes_lost_total: u64,
    sent: BTreeMap<u64, SentRecord>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Direction {
    Up,
    Down,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CompetingMode {
    Default,
    Competitive,
}

#[derive(Debug, Clone, Copy)]
struct SentRecord {
    sent_at: Instant,
}

#[derive(Debug, Clone, Copy)]
struct Velocity {
    direction: Direction,
    velocity: u64,
    last_cwnd: u64,
    same_direction_cnt: u64,
}

#[derive(Debug, Clone, Copy)]
struct AckState {
    now: Instant,
    newly_lost_bytes: u64,
    newly_acked_bytes: u64,
    largest_acked_seq: u64,
    min_rtt: Duration,
    last_srtt: Duration,
}

#[derive(Debug, Default, Clone, Copy)]
struct Round {
    count: u64,
    is_start: bool,
    end_seq: u64,
    last_acked_bytes: u64,
    last_lost_bytes: u64,
    loss_rate: f64,
}

#[derive(Debug, Clone)]
struct MinMax {
    window_us: u64,
    samples: VecDeque<(u64, u64)>,
    min: u64,
}

impl MinMax {
    fn new(window: Duration) -> Self {
        Self {
            window_us: window.as_micros().min(u64::MAX as u128) as u64,
            samples: VecDeque::new(),
            min: 0,
        }
    }

    fn set_window(&mut self, window: Duration) {
        self.window_us = window.as_micros().min(u64::MAX as u128) as u64;
    }

    fn update_min(&mut self, now_us: u64, sample_us: u64) {
        self.samples.push_back((now_us, sample_us));
        let cutoff = now_us.saturating_sub(self.window_us);
        while self.samples.front().is_some_and(|(t, _)| *t < cutoff) {
            self.samples.pop_front();
        }
        self.min = self
            .samples
            .iter()
            .map(|(_, v)| *v)
            .min()
            .unwrap_or(sample_us);
    }

    const fn get(&self) -> u64 {
        self.min
    }
}

#[derive(Debug, Clone, Copy)]
struct RttEstimator {
    latest_rtt: Duration,
    smoothed_rtt: Duration,
    rttvar: Duration,
}

impl RttEstimator {
    fn new(initial: Duration) -> Self {
        Self {
            latest_rtt: initial,
            smoothed_rtt: initial,
            rttvar: initial / 2,
        }
    }

    fn on_sample(&mut self, sample: Duration) {
        self.latest_rtt = sample;
        if self.smoothed_rtt.is_zero() {
            self.smoothed_rtt = sample;
            self.rttvar = sample / 2;
            return;
        }
        let diff = sample.abs_diff(self.smoothed_rtt);
        self.rttvar = duration_mul(self.rttvar, 0.75) + duration_mul(diff, 0.25);
        self.smoothed_rtt = duration_mul(self.smoothed_rtt, 0.875) + duration_mul(sample, 0.125);
    }

    const fn smoothed_rtt(&self) -> Duration {
        self.smoothed_rtt
    }
}

fn duration_mul(d: Duration, factor: f64) -> Duration {
    Duration::from_secs_f64(d.as_secs_f64() * factor)
}

impl Copa {
    /// Creates a Copa instance.
    #[must_use]
    pub fn new(config: CopaConfig) -> Self {
        let initial_rtt = config.initial_rtt;
        let initial_cwnd = config.initial_cwnd;
        Self {
            delta: config.slow_start_delta,
            cwnd: initial_cwnd,
            rtt: RttEstimator::new(initial_rtt),
            standing_rtt: MinMax::new(STANDING_RTT_WINDOW),
            min_rtt: MinMax::new(MIN_RTT_WINDOW),
            init_time: Instant::now(),
            config,
            slow_start: true,
            mode: CompetingMode::Default,
            velocity: Velocity {
                direction: Direction::Up,
                velocity: 1,
                last_cwnd: 0,
                same_direction_cnt: 0,
            },
            ack: AckState {
                now: Instant::now(),
                newly_lost_bytes: 0,
                newly_acked_bytes: 0,
                largest_acked_seq: 0,
                min_rtt: Duration::ZERO,
                last_srtt: Duration::ZERO,
            },
            increase_cwnd: true,
            target_rate: 0,
            round: Round::default(),
            last_sent_seq: 0,
            bytes_in_flight: 0,
            bytes_acked_total: 0,
            bytes_lost_total: 0,
            sent: BTreeMap::new(),
        }
    }

    /// Current target throughput from the Copa model (bytes/sec).
    #[must_use]
    pub const fn target_rate(&self) -> u64 {
        self.target_rate
    }

    /// Whether Copa is still in slow start.
    #[must_use]
    pub const fn in_slow_start(&self) -> bool {
        self.slow_start
    }

    fn standing_rtt(&self) -> Duration {
        let rtt = Duration::from_micros(self.standing_rtt.get());
        if rtt.is_zero() {
            self.config.initial_rtt.max(Duration::from_micros(1))
        } else {
            rtt
        }
    }

    fn begin_ack(&mut self, now: Instant) {
        self.ack.now = now;
        self.ack.newly_lost_bytes = 0;
        self.ack.newly_acked_bytes = 0;
        self.ack.largest_acked_seq = 0;
        self.ack.min_rtt = Duration::ZERO;
        self.ack.last_srtt = Duration::ZERO;
    }

    fn on_ack_sample(&mut self, seq: u64, acked_bytes: u64, rtt_sample: Duration) {
        self.ack.newly_acked_bytes = self.ack.newly_acked_bytes.saturating_add(acked_bytes);
        self.ack.largest_acked_seq = self.ack.largest_acked_seq.max(seq);
        self.ack.last_srtt = self.rtt.smoothed_rtt();
        if self.ack.min_rtt.is_zero() || self.ack.min_rtt >= rtt_sample {
            self.ack.min_rtt = rtt_sample;
        }
    }

    fn end_ack(&mut self) {
        self.update_round();
        self.update_model();
    }

    fn update_round(&mut self) {
        if self.ack.largest_acked_seq >= self.round.end_seq {
            let lost = self
                .bytes_lost_total
                .saturating_sub(self.round.last_lost_bytes);
            let acked = self
                .bytes_acked_total
                .saturating_sub(self.round.last_acked_bytes);
            let denom = lost.saturating_add(acked);
            self.round.loss_rate = if denom == 0 {
                0.0
            } else {
                lost as f64 / denom as f64
            };
            self.round.last_acked_bytes = self.bytes_acked_total;
            self.round.last_lost_bytes = self.bytes_lost_total;
            self.round.count = self.round.count.saturating_add(1);
            self.round.end_seq = self.last_sent_seq;
            self.round.is_start = true;
        } else {
            self.round.is_start = false;
        }
    }

    fn update_mode(&mut self) {
        self.mode = if self.round.loss_rate >= LOSS_RATE_THRESHOLD {
            CompetingMode::Competitive
        } else {
            CompetingMode::Default
        };

        self.delta = match self.mode {
            CompetingMode::Default => {
                if self.slow_start {
                    self.config.slow_start_delta
                } else {
                    self.config.steady_delta
                }
            }
            CompetingMode::Competitive => (self.delta * 2.0).min(0.5),
        };
    }

    fn update_velocity(&mut self) {
        if self.slow_start && self.increase_cwnd {
            return;
        }

        if self.velocity.last_cwnd == 0 {
            self.velocity.last_cwnd = self.cwnd.max(self.config.min_cwnd);
            self.velocity.velocity = 1;
            self.velocity.same_direction_cnt = 0;
            return;
        }

        if !self.round.is_start {
            return;
        }

        let new_dir = if self.cwnd > self.velocity.last_cwnd {
            Direction::Up
        } else {
            Direction::Down
        };

        if new_dir != self.velocity.direction {
            self.velocity.velocity = 1;
            self.velocity.same_direction_cnt = 0;
        } else {
            self.velocity.same_direction_cnt = self.velocity.same_direction_cnt.saturating_add(1);
            if self.velocity.same_direction_cnt >= SPEED_UP_THRESHOLD {
                self.velocity.velocity = self.velocity.velocity.saturating_mul(2);
            }
        }

        if self.increase_cwnd
            && self.velocity.direction != Direction::Up
            && self.velocity.velocity > 1
        {
            self.velocity.direction = Direction::Up;
            self.velocity.velocity = 1;
        } else if !self.increase_cwnd
            && self.velocity.direction != Direction::Down
            && self.velocity.velocity > 1
        {
            self.velocity.direction = Direction::Down;
            self.velocity.velocity = 1;
        }

        self.velocity.direction = new_dir;
        self.velocity.last_cwnd = self.cwnd;
    }

    fn update_cwnd(&mut self) {
        if self.slow_start && !self.increase_cwnd {
            self.slow_start = false;
        }

        if self.slow_start {
            if self.increase_cwnd {
                self.cwnd = self.cwnd.saturating_add(self.ack.newly_acked_bytes);
            }
        } else {
            let cwnd_delta = ((self.velocity.velocity
                * self.ack.newly_acked_bytes
                * self.config.packet_size) as f64
                / (self.delta * self.cwnd as f64)) as u64;

            self.cwnd = if self.increase_cwnd {
                self.cwnd.saturating_add(cwnd_delta)
            } else {
                self.cwnd.saturating_sub(cwnd_delta)
            };

            if self.cwnd == 0 {
                self.cwnd = self.config.min_cwnd;
                self.velocity.velocity = 1;
            }
        }
    }

    fn update_model(&mut self) {
        if self.config.use_standing_rtt {
            self.standing_rtt
                .set_window(self.ack.last_srtt.max(Duration::from_micros(1)));
        } else {
            self.standing_rtt
                .set_window((self.ack.last_srtt / 2).max(Duration::from_micros(1)));
        }

        if self.ack.min_rtt.is_zero() {
            self.ack.min_rtt = if self.ack.last_srtt.is_zero() {
                self.config.initial_rtt
            } else {
                self.ack.last_srtt
            };
        }

        let elapsed = self.ack.now.saturating_duration_since(self.init_time);
        let elapsed_us = elapsed.as_micros().min(u64::MAX as u128) as u64;
        let min_sample = self.ack.min_rtt.as_micros().min(u64::MAX as u128) as u64;

        self.min_rtt.update_min(elapsed_us, min_sample);
        self.standing_rtt.update_min(elapsed_us, min_sample);

        self.update_mode();

        let min_rtt = Duration::from_micros(self.min_rtt.get());
        let standing_rtt = self.standing_rtt();
        let standing_secs = standing_rtt.as_secs_f64().max(1e-9);
        let current_rate = (self.cwnd as f64 / standing_secs) as u64;
        let queueing_delay = standing_rtt.saturating_sub(min_rtt);

        if queueing_delay.is_zero() {
            self.increase_cwnd = true;
            self.target_rate = current_rate;
        } else {
            self.target_rate = (self.config.packet_size as f64
                / self.delta
                / queueing_delay.max(Duration::from_micros(1)).as_secs_f64())
                as u64;
            self.increase_cwnd = self.target_rate >= current_rate;
        }

        self.update_velocity();
        self.update_cwnd();
    }
}

impl CongestionController for Copa {
    fn on_packet_sent(&mut self, packet: &SentPacket) {
        self.bytes_in_flight = self.bytes_in_flight.saturating_add(packet.len as u64);
        self.last_sent_seq = packet.seq;
        self.sent.insert(
            packet.seq,
            SentRecord {
                sent_at: packet.sent_at,
            },
        );
    }

    fn on_packets_acked(&mut self, acks: &[AckInfo]) {
        if acks.is_empty() {
            return;
        }

        self.begin_ack(acks[0].acked_at);

        for ack in acks {
            let Some(record) = self.sent.remove(&ack.seq) else {
                continue;
            };

            self.bytes_in_flight = self.bytes_in_flight.saturating_sub(ack.len as u64);
            self.bytes_acked_total = self.bytes_acked_total.saturating_add(ack.len as u64);

            let rtt_sample = ack.acked_at.saturating_duration_since(record.sent_at);
            self.rtt.on_sample(rtt_sample);

            self.on_ack_sample(ack.seq, ack.len as u64, rtt_sample);
        }

        self.end_ack();
    }

    fn on_packet_lost(&mut self, packet: &LostPacket) {
        if self.sent.remove(&packet.seq).is_some() {
            self.bytes_in_flight = self.bytes_in_flight.saturating_sub(packet.len as u64);
        }
        self.bytes_lost_total = self.bytes_lost_total.saturating_add(packet.len as u64);
        self.ack.newly_lost_bytes = self.ack.newly_lost_bytes.saturating_add(packet.len as u64);
    }

    fn pacing_rate(&self) -> u64 {
        let standing = self.standing_rtt();
        let secs = standing.as_secs_f64().max(1e-9);
        PACING_GAIN.saturating_mul((self.cwnd() as f64 / secs) as u64)
    }

    fn cwnd(&self) -> u64 {
        self.cwnd.max(self.config.min_cwnd)
    }

    fn rtt(&self) -> Duration {
        self.rtt.smoothed_rtt()
    }

    fn can_send(&self, bytes_in_flight: u64) -> bool {
        bytes_in_flight < self.cwnd()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn cwnd_grows_on_ack() {
        let mut copa = Copa::new(CopaConfig::default());
        let t0 = Instant::now();
        let pkt_len = 1_200usize;

        for seq in 1..=10 {
            copa.on_packet_sent(&SentPacket {
                seq,
                len: pkt_len,
                sent_at: t0 + Duration::from_millis(seq),
            });
        }

        let cwnd_before = copa.cwnd();
        copa.on_packet_acked(&AckInfo {
            seq: 1,
            len: pkt_len,
            acked_at: t0 + Duration::from_millis(50),
        });
        assert!(copa.cwnd() >= cwnd_before);
        assert!(copa.pacing_rate() > 0);
    }

    #[test]
    fn can_send_respects_cwnd() {
        let copa = Copa::new(CopaConfig {
            min_cwnd: 3_600,
            initial_cwnd: 3_600,
            ..CopaConfig::default()
        });
        assert!(copa.can_send(0));
        assert!(copa.can_send(3_599));
        assert!(!copa.can_send(3_600));
    }
}