Skip to main content

srt_runtime/arq/
seq.rs

1//! 32-bit wrap-safe sequence-number arithmetic for SRT ARQ.
2//!
3//! SRT data packet sequence numbers occupy the low 31 bits of the header's
4//! first word (`draft-sharabayko-srt-01` §3.1, Figure 3: `Packet Sequence
5//! Number (31)`; the top bit is the header `F` packet-type flag, reused
6//! bit-for-bit as the NAK loss-list range marker, Appendix A) — see the
7//! crate-internal `crate::packet::SEQ_NUMBER_MASK`.
8//!
9//! `private/specs-verified/rules/srt-arq.md` does not specify a sequence-number comparison or
10//! arithmetic algorithm for ARQ purposes (it is not one of the curated
11//! rules). This module resolves that gap the standard way for a modular
12//! sequence space — comparable to RFC 1982 serial number arithmetic:
13//! circular over the 31-bit space, picking the shorter of the two
14//! directions between two numbers as "before"/"after". This is
15//! implementation-defined, not spec-cited, and is needed for the send/
16//! receive buffers to behave correctly once a stream's sequence numbers
17//! wrap past `0x7FFF_FFFF` back to `0`.
18
19use crate::packet::SEQ_NUMBER_MASK;
20use alloc::vec::Vec;
21use broadcast_common::seq::SeqSpace;
22
23/// The SRT sequence-number space: 31 bits (§3.1). The arithmetic itself is
24/// `broadcast_common::seq` (shared with `rist-runtime`, #1141), sized from
25/// [`SEQ_NUMBER_MASK`].
26const SPACE: SeqSpace = SeqSpace::new(SEQ_NUMBER_MASK.count_ones());
27
28/// Add `n` to a sequence number, wrapping at the 31-bit boundary.
29pub fn seq_add(seq: u32, n: u32) -> u32 {
30    SPACE.add(seq, n)
31}
32
33/// The next sequence number after `seq` (wraps `0x7FFF_FFFF` -> `0`).
34pub fn seq_next(seq: u32) -> u32 {
35    seq_add(seq, 1)
36}
37
38/// Signed circular distance `a - b` in the 31-bit sequence space, in
39/// `(-2^30, 2^30]`. Positive means `a` is ahead of `b`.
40pub fn seq_diff(a: u32, b: u32) -> i32 {
41    SPACE.diff(a, b)
42}
43
44/// `a` precedes `b` in circular sequence order.
45pub fn seq_lt(a: u32, b: u32) -> bool {
46    SPACE.lt(a, b)
47}
48
49/// `a` precedes or equals `b`.
50pub fn seq_leq(a: u32, b: u32) -> bool {
51    SPACE.leq(a, b)
52}
53
54/// `a` follows `b` in circular sequence order.
55pub fn seq_gt(a: u32, b: u32) -> bool {
56    SPACE.gt(a, b)
57}
58
59/// `a` follows or equals `b`.
60pub fn seq_geq(a: u32, b: u32) -> bool {
61    SPACE.geq(a, b)
62}
63
64/// `seq` lies within the inclusive circular range `first..=last`, walking
65/// forward from `first`. A `last` that precedes `first` in circular order is
66/// an empty (malformed) range and never matches.
67pub fn seq_in_closed_range(seq: u32, first: u32, last: u32) -> bool {
68    SPACE.in_closed_range(seq, first, last)
69}
70
71/// Split ascending, circularly ordered sequence numbers into inclusive
72/// `(first, last)` runs of consecutive values.
73pub fn contiguous_runs(seqs: &[u32]) -> Vec<(u32, u32)> {
74    let mut runs: Vec<(u32, u32)> = Vec::new();
75    for &s in seqs {
76        match runs.last_mut() {
77            Some((_, last)) if seq_next(*last) == s => *last = s,
78            _ => runs.push((s, s)),
79        }
80    }
81    runs
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    #[test]
89    fn contiguous_runs_merge_across_the_wrap() {
90        let top = SEQ_NUMBER_MASK;
91        assert_eq!(
92            contiguous_runs(&[1, 2, 4, top, 0, 1]),
93            vec![(1, 2), (4, 4), (top, 1)]
94        );
95        assert!(contiguous_runs(&[]).is_empty());
96    }
97
98    #[test]
99    fn space_mask_is_the_wire_mask() {
100        assert_eq!(SPACE.mask(), SEQ_NUMBER_MASK);
101    }
102
103    #[test]
104    fn wrap_boundary_increment() {
105        assert_eq!(seq_next(SEQ_NUMBER_MASK), 0);
106        assert_eq!(seq_add(SEQ_NUMBER_MASK, 1), 0);
107        assert_eq!(seq_add(SEQ_NUMBER_MASK, 5), 4);
108        assert_eq!(seq_add(0, SEQ_NUMBER_MASK), SEQ_NUMBER_MASK);
109    }
110
111    #[test]
112    fn wrap_boundary_ordering() {
113        // 0 comes right after SEQ_NUMBER_MASK (the maximum 31-bit value) in
114        // circular sequence order.
115        assert!(seq_lt(SEQ_NUMBER_MASK, 0));
116        assert!(seq_gt(0, SEQ_NUMBER_MASK));
117        assert_eq!(seq_diff(0, SEQ_NUMBER_MASK), 1);
118        assert_eq!(seq_diff(SEQ_NUMBER_MASK, 0), -1);
119    }
120
121    #[test]
122    fn ordinary_ordering_without_wrap() {
123        assert!(seq_lt(10, 20));
124        assert!(seq_leq(10, 10));
125        assert!(seq_leq(10, 20));
126        assert!(seq_gt(20, 10));
127        assert!(seq_geq(20, 20));
128        assert!(seq_geq(20, 10));
129        assert!(!seq_lt(20, 10));
130        assert!(!seq_gt(10, 20));
131    }
132
133    #[test]
134    fn diff_is_antisymmetric() {
135        for (a, b) in [(0u32, 0u32), (5, 100), (SEQ_NUMBER_MASK, 3), (12345, 12340)] {
136            assert_eq!(seq_diff(a, b), -seq_diff(b, a));
137        }
138    }
139
140    #[test]
141    fn closed_range_membership_walks_forward_from_first() {
142        assert!(seq_in_closed_range(5, 5, 10));
143        assert!(seq_in_closed_range(10, 5, 10));
144        assert!(seq_in_closed_range(7, 5, 10));
145        assert!(!seq_in_closed_range(4, 5, 10));
146        assert!(!seq_in_closed_range(11, 5, 10));
147        // A `last` before `first` is a malformed (empty) range.
148        assert!(!seq_in_closed_range(7, 10, 5));
149        // Wrap-safe: the range straddles the 31-bit boundary.
150        assert!(seq_in_closed_range(SEQ_NUMBER_MASK, SEQ_NUMBER_MASK - 2, 1));
151        assert!(seq_in_closed_range(0, SEQ_NUMBER_MASK - 2, 1));
152        assert!(!seq_in_closed_range(2, SEQ_NUMBER_MASK - 2, 1));
153    }
154
155    #[test]
156    fn walking_forward_past_the_wrap_stays_consistent() {
157        // Start just below the wrap and walk forward 10 steps; sequence
158        // order must stay monotonic through the wrap.
159        let start = SEQ_NUMBER_MASK - 4;
160        let mut prev = start;
161        for _ in 0..10 {
162            let next = seq_next(prev);
163            assert!(seq_lt(prev, next), "prev={prev:#x} next={next:#x}");
164            assert_eq!(seq_diff(next, prev), 1);
165            prev = next;
166        }
167    }
168}