srt-runtime 0.4.0

SRT (Secure Reliable Transport, draft-sharabayko-srt-01) packet codecs + sans-IO HSv5 Caller-Listener and Rendezvous handshake state machines, an ARQ reliability engine, a TSBPD delivery scheduler, LiveCC packet pacing, FileCC window-based congestion control, optional (crypto feature) payload encryption, and an optional (tokio feature) async UDP socket adapter; no_std core.
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
//! SRT Live Congestion Control — packet pacing (`draft-sharabayko-srt-01` §5.1,
//! curated at `specs/rules/srt-livecc.md`).
//!
//! A sans-IO, `no_std` sender-side pacing controller that computes the minimum
//! inter-packet send period (`PKT_SND_PERIOD`) from a configured maximum
//! bandwidth (`MAX_BW`) and a running EWMA of the average payload size
//! (`AvgPayloadSize`).
//!
//! ## Usage
//!
//! ```rust
//! use srt_runtime::livecc::{LiveCC, MaxBwConfig};
//! use core::time::Duration;
//!
//! let mut cc = LiveCC::new(Default::default());
//! // Application sends a data packet with 1316 bytes of payload:
//! cc.on_data_packet(1316);
//! // On ACK reception, recompute the send period:
//! let period = cc.on_ack_received();
//! assert!(period > Duration::ZERO);
//! ```
//!
//! ## Spec grounding
//!
//! - `specs/rules/srt-livecc.md` §5.1.1 — MAX_BW configuration modes (§5.1.1,
//!   lines L3116-L3202).
//! - `specs/rules/srt-livecc.md` §5.1.2 — LiveCC algorithm (lines L3203-L3238):
//!   - EWMA formula: L3219 (`AvgPayloadSize = 7/8 * AvgPayloadSize + 1/8 * PacketPayloadSize`)
//!   - Initial value cap: L3222-3223 (max 1456 bytes).
//!   - PktSize formula: L3227-3229 (`PktSize = AvgPayloadSize + SRT header size`).
//!   - PKT_SND_PERIOD formula: L3234 (`PktSize * 1000000 / MAX_BW`).
//! - `specs/rules/srt-livecc.md` — SYN = 0.01 s (imported from §5.2.1,
//!   L3421-3423, restated at L197-L199 of the curated doc).

use core::time::Duration;

/// The fixed SRT header size (16 bytes), defined in `draft-sharabayko-srt-01` §3,
/// Figure 2. Used by the PktSize formula (`specs/rules/srt-livecc.md` §5.1.2
/// step 2, L3227-3229).
const SRT_HEADER_SIZE: u64 = 16;

/// Initial cap for `AvgPayloadSize`: the maximum allowed packet payload size,
/// which cannot be larger than 1456 bytes (`specs/rules/srt-livecc.md` §5.1.2,
/// L3222-3223).
const INITIAL_AVG_PAYLOAD_SIZE_CAP: u64 = 1456;

/// Default MAX_BW for MAXBW_SET mode — 1 Gbps (`specs/rules/srt-livecc.md`
/// §5.1.1, L3122-3123). Stored in bytes per second (L3156-3157): 1 Gbps =
/// 125_000_000 bytes/s.
const DEFAULT_MAX_BW_BYTES_PER_SEC: u64 = 125_000_000;

/// Maximum bandwidth configuration mode (`specs/rules/srt-livecc.md` §5.1.1,
/// lines L3116-L3202).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum MaxBwConfig {
    /// **MAXBW_SET** (§5.1.1, L3120-3128): MAX_BW set explicitly (bytes/sec).
    /// Default: 1 Gbps = 125_000_000 bytes/sec.
    Set(u64),
    /// **INPUTBW_SET** (§5.1.1, L3130-3146): input rate (bytes/sec) + overhead
    /// percentage. MAX_BW = `input_bw * (1 + overhead / 100)` (L3143).
    InputBased {
        /// The sender's input rate, in bytes per second.
        input_bw: u64,
        /// Overhead percentage (e.g. 25 means 25%).
        overhead: u64,
    },
    /// **INPUTBW_ESTIMATED** (§5.1.1, L3148-3184): measured input rate +
    /// overhead. MAX_BW = `est_input_bw * (1 + overhead / 100)` (L3154).
    ///
    /// Unlike [`MaxBwConfig::InputBased`], `est_input_bw` is updated
    /// externally (e.g. the encoder's measured bitrate). The formula is
    /// identical.
    Estimated {
        /// The estimated sender input rate, in bytes per second.
        est_input_bw: u64,
        /// Overhead percentage (e.g. 25 means 25%).
        overhead: u64,
    },
    /// Unbounded — infinite bandwidth. No packet pacing is applied.
    ///
    /// Not a named mode in §5.1.1's table (L3197-3201), but is the implicit
    /// effect when MAX_BW is not set or is unlimited. Represented by a
    /// PKT_SND_PERIOD of 0 (send as fast as possible).
    Infinite,
}

impl Default for MaxBwConfig {
    /// Default: [`MaxBwConfig::Set`] at 1 Gbps (`specs/rules/srt-livecc.md`
    /// §5.1.1, L3122-3123).
    fn default() -> Self {
        MaxBwConfig::Set(DEFAULT_MAX_BW_BYTES_PER_SEC)
    }
}

impl MaxBwConfig {
    /// The §5.1.1 mode name.
    pub fn name(&self) -> &'static str {
        match self {
            MaxBwConfig::Set(_) => "MAXBW_SET",
            MaxBwConfig::InputBased { .. } => "INPUTBW_SET",
            MaxBwConfig::Estimated { .. } => "INPUTBW_ESTIMATED",
            MaxBwConfig::Infinite => "Infinite",
        }
    }

    /// Resolve the current `MAX_BW` value in bytes per second, or `None` for
    /// unbounded (infinite bandwidth).
    ///
    /// Per `specs/rules/srt-livecc.md` §5.1.1:
    /// - MAXBW_SET: returns the explicit value (L3120-3128).
    /// - INPUTBW_SET: returns `input_bw * (1 + overhead / 100)` (L3143).
    /// - INPUTBW_ESTIMATED: returns `est_input_bw * (1 + overhead / 100)` (L3154).
    /// - Infinite: returns `None`.
    pub fn max_bw_bytes_per_sec(&self) -> Option<u64> {
        match *self {
            MaxBwConfig::Set(bw) => Some(bw),
            MaxBwConfig::InputBased { input_bw, overhead } => {
                // L3143: MAX_BW = INPUT_BW * (1 + OVERHEAD / 100)
                Some(apply_overhead(input_bw, overhead))
            }
            MaxBwConfig::Estimated {
                est_input_bw,
                overhead,
            } => {
                // L3154: MAX_BW = EST_INPUT_BW * (1 + OVERHEAD / 100)
                Some(apply_overhead(est_input_bw, overhead))
            }
            MaxBwConfig::Infinite => None,
        }
    }
}

broadcast_common::impl_spec_display!(MaxBwConfig);

/// Apply `'MAX_BW = bw * (1 + overhead / 100)'` (`specs/rules/srt-livecc.md`
/// L3143/L3154, verbatim).
fn apply_overhead(bw: u64, overhead: u64) -> u64 {
    // `overhead / 100` is truncated integer division, but the spec formula
    // `(1 + OVERHEAD / 100)` is a real multiplier — we lose precision with
    // integer arithmetic. Compute as `bw + bw * overhead / 100` instead.
    bw + bw * overhead / 100
}

/// SRT Live Congestion Control — sender-side pacing state
/// (`draft-sharabayko-srt-01` §5.1.2).
///
/// Tracks the EWMA average payload size and recomputes the inter-packet send
/// period on each ACK.
///
/// Sans-IO: all timing is caller-driven — [`LiveCC::on_data_packet`],
/// [`LiveCC::on_ack_received`], and [`LiveCC::tick`] take explicit inputs and
/// return computed values; the controller never reads a wall clock internally.
#[derive(Debug, Clone)]
pub struct LiveCC {
    /// Average payload size (EWMA), in bytes — `AvgPayloadSize` from §5.1.2
    /// (L3219).
    avg_payload_size: u64,
    /// Maximum bandwidth configuration (§5.1.1).
    max_bw_config: MaxBwConfig,
}

impl LiveCC {
    /// Create a new LiveCC pacing controller.
    ///
    /// `max_bw_config` — the MAX_BW mode (§5.1.1). Use `Default::default()`
    /// for the default 1 Gbps fixed MAXBW_SET mode.
    ///
    /// Initial `AvgPayloadSize` is set to the initial cap (1456 bytes),
    /// per `specs/rules/srt-livecc.md` §5.1.2 (L3222-3223).
    pub fn new(max_bw_config: MaxBwConfig) -> Self {
        LiveCC {
            avg_payload_size: INITIAL_AVG_PAYLOAD_SIZE_CAP,
            max_bw_config,
        }
    }

    /// Update the EWMA average payload size on sending a data packet
    /// (`specs/rules/srt-livecc.md` §5.1.2, L3216-3223).
    ///
    /// `packet_payload_size` — the payload size (bytes) of the just-sent data
    /// packet (original or retransmitted, L3216-3217).
    ///
    /// Formula (L3219, verbatim):
    ///
    /// ```text
    /// AvgPayloadSize = 7/8 * AvgPayloadSize + 1/8 * PacketPayloadSize
    /// ```
    pub fn on_data_packet(&mut self, packet_payload_size: u64) {
        // L3219: AvgPayloadSize = 7/8 * AvgPayloadSize + 1/8 * PacketPayloadSize
        let old = self.avg_payload_size;
        self.avg_payload_size = (7 * old + packet_payload_size) / 8;
    }

    /// The current average payload size estimate, in bytes.
    pub fn avg_payload_size(&self) -> u64 {
        self.avg_payload_size
    }

    /// The current MAX_BW configuration.
    pub fn max_bw_config(&self) -> &MaxBwConfig {
        &self.max_bw_config
    }

    /// Reconfigure the MAX_BW mode at runtime.
    pub fn set_max_bw_config(&mut self, config: MaxBwConfig) {
        self.max_bw_config = config;
    }

    /// Compute the current inter-packet send period in microseconds
    /// (`specs/rules/srt-livecc.md` §5.1.2, L3225-3238).
    ///
    /// Call this on ACK reception (method (2), L3225). The period is also
    /// used after any state change (e.g. reconfiguration).
    ///
    /// Returns `Duration::ZERO` when bandwidth is unbounded (infinite mode),
    /// meaning "send as fast as possible".
    ///
    /// Step 1 — PktSize (L3227-3229, paraphrased):
    ///
    /// ```text
    /// PktSize = AvgPayloadSize + SRT header size
    /// ```
    ///
    /// Step 2 — PKT_SND_PERIOD (L3234, verbatim):
    ///
    /// ```text
    /// PKT_SND_PERIOD = PktSize * 1000000 / MAX_BW
    /// ```
    pub fn on_ack_received(&self) -> Duration {
        let Some(max_bw) = self.max_bw_config.max_bw_bytes_per_sec() else {
            // Infinite: no pacing.
            return Duration::ZERO;
        };
        if max_bw == 0 {
            return Duration::ZERO;
        }

        // L3227-3229: PktSize = AvgPayloadSize + SRT header size (16 bytes).
        let pkt_size = self.avg_payload_size + SRT_HEADER_SIZE;

        // L3234: PKT_SND_PERIOD = PktSize * 1000000 / MAX_BW
        //
        // pkt_size is in bytes, MAX_BW in bytes/sec. The factor 1_000_000
        // converts seconds to microseconds (L3237-3238).
        let period_us = pkt_size * 1_000_000 / max_bw;
        Duration::from_micros(period_us)
    }

    /// Time-driven tick — currently a no-op for LiveCC (the `§5.1.2` algorithm
    /// is driven by packet/ACK events only; an RTO timeout also triggers
    /// `on_data_packet`, per L3240-3241).
    ///
    /// Provided for forward compatibility: the sender loop should call this
    /// every cycle. Returns `None`.
    pub fn tick(&mut self) -> Option<Duration> {
        // Per spec, LiveCC reacts to events (data send, ACK, RTO — L3211-3214);
        // the RTO timeout also calls on_data_packet (L3240-3241). No periodic
        // internal computation needed.
        None
    }
}

impl Default for LiveCC {
    fn default() -> Self {
        LiveCC::new(Default::default())
    }
}

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

    #[test]
    fn initial_avg_payload_size_is_1456_cap() {
        // L3222-3223: initial AvgPayloadSize is the max allowed payload, max 1456.
        let cc = LiveCC::new(Default::default());
        assert_eq!(cc.avg_payload_size(), 1456);
    }

    #[test]
    fn default_bw_is_1_gbps() {
        // L3122-3123: recommended default is 1 Gbps.
        let cfg = MaxBwConfig::default();
        assert_eq!(cfg.max_bw_bytes_per_sec(), Some(125_000_000));
    }

    #[test]
    fn ewma_update_matches_hand_computed_formula() {
        // L3219: AvgPayloadSize = 7/8 * old + 1/8 * payload.
        // Initial: 1456. Feed a 100-byte payload.
        // new = (7 * 1456 + 100) / 8 = (10192 + 100) / 8 = 10292 / 8 = 1286
        let mut cc = LiveCC::new(Default::default());
        cc.on_data_packet(100);
        assert_eq!(cc.avg_payload_size(), 1286);
    }

    #[test]
    fn ewma_converges_toward_constant_payload() {
        // Feed a constant 1000-byte payload many times; the EWMA should
        // converge toward 1000.
        let mut cc = LiveCC::new(Default::default());
        for _ in 0..64 {
            cc.on_data_packet(1000);
        }
        let avg = cc.avg_payload_size();
        // Converged within 1 of 1000.
        assert!(avg.abs_diff(1000) <= 1, "expected ~1000, got {avg}");
    }

    #[test]
    fn ewma_respects_initial_cap_regardless_of_first_feed() {
        // The initial cap (1456) is separate from the first update.
        // Even if we feed a tiny payload, the init was 1456, not the fed value.
        let mut cc = LiveCC::new(Default::default());
        cc.on_data_packet(50);
        // new = (7 * 1456 + 50) / 8 = (10192 + 50) / 8 = 10242 / 8 = 1280
        assert_eq!(cc.avg_payload_size(), 1280);
    }

    #[test]
    fn pkt_snd_period_formula_matches_hand_computed() {
        // L3234: PKT_SND_PERIOD = PktSize * 1000000 / MAX_BW.
        //
        // With default MAX_BW = 125_000_000 bytes/sec and init AvgPayloadSize
        // = 1456:
        //   PktSize = 1456 + 16 = 1472
        //   period = 1472 * 1_000_000 / 125_000_000 = 1_472_000_000 / 125_000_000
        //          = 11 (integer division; 11.776 truncated to 11)
        let cc = LiveCC::new(Default::default());
        let period = cc.on_ack_received();
        assert_eq!(period, Duration::from_micros(11));
    }

    #[test]
    fn pkt_snd_period_with_different_payload_size() {
        // After feeding a 1316-byte payload seven times (to get close to
        // converged), compute the period.
        //
        // EWMA after 7 steps: we don't hand-compute the exact 8-step weight
        // here; instead we use a simpler scenario:
        //   initial: 1456
        //   feed 1316: avg = (7*1456 + 1316)/8 = 1438
        //   feed 1316: avg = (7*1438 + 1316)/8 = 1422
        //   PktSize = 1422 + 16 = 1438
        //   period = 1438 * 1_000_000 / 125_000_000 = 11
        let mut cc = LiveCC::new(Default::default());
        cc.on_data_packet(1316);
        cc.on_data_packet(1316);
        let period = cc.on_ack_received();
        assert_eq!(period, Duration::from_micros(11));
    }

    #[test]
    fn input_bw_mode_formula() {
        // L3143: MAX_BW = INPUT_BW * (1 + OVERHEAD / 100).
        // input_bw = 10_000_000 (10 MB/s), overhead = 25%.
        // MAX_BW = 10_000_000 + 10_000_000 * 25 / 100 = 12_500_000 bytes/sec.
        let cfg = MaxBwConfig::InputBased {
            input_bw: 10_000_000,
            overhead: 25,
        };
        assert_eq!(cfg.max_bw_bytes_per_sec(), Some(12_500_000));
    }

    #[test]
    fn estimated_mode_formula() {
        // L3154: same formula as INPUTBW_SET but with EST_INPUT_BW.
        let cfg = MaxBwConfig::Estimated {
            est_input_bw: 5_000_000,
            overhead: 10,
        };
        // MAX_BW = 5_000_000 + 5_000_000 * 10 / 100 = 5_500_000
        assert_eq!(cfg.max_bw_bytes_per_sec(), Some(5_500_000));
    }

    #[test]
    fn infinite_mode_returns_zero_period() {
        let cc = LiveCC::new(MaxBwConfig::Infinite);
        assert_eq!(cc.on_ack_received(), Duration::ZERO);
    }

    #[test]
    fn set_mode_period_scales_with_bw() {
        // MAXBW_SET at 62_500_000 bytes/sec (500 Mbps); init PktSize = 1472.
        // period = 1472 * 1_000_000 / 62_500_000 = 23 (23.552 truncated).
        let cc = LiveCC::new(MaxBwConfig::Set(62_500_000));
        assert_eq!(cc.on_ack_received(), Duration::from_micros(23));
    }

    #[test]
    fn runtime_reconfiguration_affects_period() {
        let mut cc = LiveCC::new(MaxBwConfig::Infinite);
        assert_eq!(cc.on_ack_received(), Duration::ZERO);

        cc.set_max_bw_config(MaxBwConfig::Set(125_000_000));
        assert_eq!(cc.on_ack_received(), Duration::from_micros(11));
    }

    #[test]
    fn zero_bw_returns_zero_period() {
        // Guard: if MAX_BW is 0, we'd divide by zero. Return ZERO instead.
        let cc = LiveCC::new(MaxBwConfig::Set(0));
        assert_eq!(cc.on_ack_received(), Duration::ZERO);
    }

    #[test]
    fn tick_is_noop() {
        let mut cc = LiveCC::new(Default::default());
        assert_eq!(cc.tick(), None);
        // Calling tick must not mutate state.
        assert_eq!(cc.avg_payload_size(), 1456);
    }

    #[test]
    fn max_bw_config_clone_and_eq() {
        let a = MaxBwConfig::Set(100);
        let b = MaxBwConfig::Set(100);
        assert_eq!(a, b);
        let c = MaxBwConfig::InputBased {
            input_bw: 1000,
            overhead: 20,
        };
        let d = MaxBwConfig::InputBased {
            input_bw: 1001,
            overhead: 20,
        };
        assert_ne!(c, d);
    }

    #[test]
    fn overhead_formula_edge_cases() {
        // overhead = 0: MAX_BW = bw + bw*0/100 = bw
        assert_eq!(
            MaxBwConfig::InputBased {
                input_bw: 10_000,
                overhead: 0,
            }
            .max_bw_bytes_per_sec(),
            Some(10_000)
        );
        // overhead = 100: MAX_BW = bw + bw*100/100 = 2*bw
        assert_eq!(
            MaxBwConfig::InputBased {
                input_bw: 10_000,
                overhead: 100,
            }
            .max_bw_bytes_per_sec(),
            Some(20_000)
        );
    }
}