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 alloc::vec::Vec;
20use broadcast_common::seq::SeqSpace;
21
22/// The SRT sequence-number space: 31 bits (§3.1). The arithmetic itself is
23/// `broadcast_common::seq` (shared with `rist-runtime`, #1141), sized from
24/// the packet layer's own bit count.
25// `SEQ_NUMBER_MASK` is `0x7FFF_FFFF`; `space_mask_is_the_wire_mask` pins the
26// two spellings of "31 bits" together for every build that runs tests.
27const SPACE_BITS: u32 = 31;
28const SPACE: SeqSpace = broadcast_common::seq_space!(SPACE_BITS);
29
30/// Add `n` to a sequence number, wrapping at the 31-bit boundary.
31pub fn seq_add(seq: u32, n: u32) -> u32 {
32    SPACE.add(seq, n)
33}
34
35/// The next sequence number after `seq` (wraps `0x7FFF_FFFF` -> `0`).
36pub fn seq_next(seq: u32) -> u32 {
37    seq_add(seq, 1)
38}
39
40/// Signed circular distance `a - b` in the 31-bit sequence space, in
41/// `(-2^30, 2^30]`. Positive means `a` is ahead of `b`.
42pub fn seq_diff(a: u32, b: u32) -> i32 {
43    SPACE.diff(a, b)
44}
45
46/// `a` precedes `b` in circular sequence order.
47pub fn seq_lt(a: u32, b: u32) -> bool {
48    SPACE.lt(a, b)
49}
50
51/// `a` precedes or equals `b`.
52pub fn seq_leq(a: u32, b: u32) -> bool {
53    SPACE.leq(a, b)
54}
55
56/// `a` follows `b` in circular sequence order.
57pub fn seq_gt(a: u32, b: u32) -> bool {
58    SPACE.gt(a, b)
59}
60
61/// `a` follows or equals `b`.
62pub fn seq_geq(a: u32, b: u32) -> bool {
63    SPACE.geq(a, b)
64}
65
66/// `seq` lies within the inclusive circular range `first..=last`, walking
67/// forward from `first`. A `last` that precedes `first` in circular order is
68/// an empty (malformed) range and never matches.
69pub fn seq_in_closed_range(seq: u32, first: u32, last: u32) -> bool {
70    SPACE.in_closed_range(seq, first, last)
71}
72
73/// Split ascending, circularly ordered sequence numbers into inclusive
74/// `(first, last)` runs of consecutive values.
75pub fn contiguous_runs(seqs: &[u32]) -> Vec<(u32, u32)> {
76    let mut runs: Vec<(u32, u32)> = Vec::new();
77    for &s in seqs {
78        match runs.last_mut() {
79            Some((_, last)) if seq_next(*last) == s => *last = s,
80            _ => runs.push((s, s)),
81        }
82    }
83    runs
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89    use crate::packet::SEQ_NUMBER_MASK;
90    use alloc::vec;
91
92    #[test]
93    fn contiguous_runs_merge_across_the_wrap() {
94        let top = SEQ_NUMBER_MASK;
95        assert_eq!(
96            contiguous_runs(&[1, 2, 4, top, 0, 1]),
97            vec![(1, 2), (4, 4), (top, 1)]
98        );
99        assert!(contiguous_runs(&[]).is_empty());
100    }
101
102    #[test]
103    fn space_mask_is_the_wire_mask() {
104        assert_eq!(SPACE.mask(), SEQ_NUMBER_MASK);
105        assert_eq!(SPACE.bits(), SEQ_NUMBER_MASK.count_ones());
106    }
107
108    #[test]
109    fn wrap_boundary_increment() {
110        assert_eq!(seq_next(SEQ_NUMBER_MASK), 0);
111        assert_eq!(seq_add(SEQ_NUMBER_MASK, 1), 0);
112        assert_eq!(seq_add(SEQ_NUMBER_MASK, 5), 4);
113        assert_eq!(seq_add(0, SEQ_NUMBER_MASK), SEQ_NUMBER_MASK);
114    }
115
116    #[test]
117    fn wrap_boundary_ordering() {
118        // 0 comes right after SEQ_NUMBER_MASK (the maximum 31-bit value) in
119        // circular sequence order.
120        assert!(seq_lt(SEQ_NUMBER_MASK, 0));
121        assert!(seq_gt(0, SEQ_NUMBER_MASK));
122        assert_eq!(seq_diff(0, SEQ_NUMBER_MASK), 1);
123        assert_eq!(seq_diff(SEQ_NUMBER_MASK, 0), -1);
124    }
125
126    #[test]
127    fn ordinary_ordering_without_wrap() {
128        assert!(seq_lt(10, 20));
129        assert!(seq_leq(10, 10));
130        assert!(seq_leq(10, 20));
131        assert!(seq_gt(20, 10));
132        assert!(seq_geq(20, 20));
133        assert!(seq_geq(20, 10));
134        assert!(!seq_lt(20, 10));
135        assert!(!seq_gt(10, 20));
136    }
137
138    #[test]
139    fn diff_is_antisymmetric() {
140        for (a, b) in [(0u32, 0u32), (5, 100), (SEQ_NUMBER_MASK, 3), (12345, 12340)] {
141            assert_eq!(seq_diff(a, b), -seq_diff(b, a));
142        }
143    }
144
145    #[test]
146    fn closed_range_membership_walks_forward_from_first() {
147        assert!(seq_in_closed_range(5, 5, 10));
148        assert!(seq_in_closed_range(10, 5, 10));
149        assert!(seq_in_closed_range(7, 5, 10));
150        assert!(!seq_in_closed_range(4, 5, 10));
151        assert!(!seq_in_closed_range(11, 5, 10));
152        // A `last` before `first` is a malformed (empty) range.
153        assert!(!seq_in_closed_range(7, 10, 5));
154        // Wrap-safe: the range straddles the 31-bit boundary.
155        assert!(seq_in_closed_range(SEQ_NUMBER_MASK, SEQ_NUMBER_MASK - 2, 1));
156        assert!(seq_in_closed_range(0, SEQ_NUMBER_MASK - 2, 1));
157        assert!(!seq_in_closed_range(2, SEQ_NUMBER_MASK - 2, 1));
158    }
159
160    #[test]
161    fn walking_forward_past_the_wrap_stays_consistent() {
162        // Start just below the wrap and walk forward 10 steps; sequence
163        // order must stay monotonic through the wrap.
164        let start = SEQ_NUMBER_MASK - 4;
165        let mut prev = start;
166        for _ in 0..10 {
167            let next = seq_next(prev);
168            assert!(seq_lt(prev, next), "prev={prev:#x} next={next:#x}");
169            assert_eq!(seq_diff(next, prev), 1);
170            prev = next;
171        }
172    }
173}