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    use alloc::vec;
88
89    #[test]
90    fn contiguous_runs_merge_across_the_wrap() {
91        let top = SEQ_NUMBER_MASK;
92        assert_eq!(
93            contiguous_runs(&[1, 2, 4, top, 0, 1]),
94            vec![(1, 2), (4, 4), (top, 1)]
95        );
96        assert!(contiguous_runs(&[]).is_empty());
97    }
98
99    #[test]
100    fn space_mask_is_the_wire_mask() {
101        assert_eq!(SPACE.mask(), SEQ_NUMBER_MASK);
102    }
103
104    #[test]
105    fn wrap_boundary_increment() {
106        assert_eq!(seq_next(SEQ_NUMBER_MASK), 0);
107        assert_eq!(seq_add(SEQ_NUMBER_MASK, 1), 0);
108        assert_eq!(seq_add(SEQ_NUMBER_MASK, 5), 4);
109        assert_eq!(seq_add(0, SEQ_NUMBER_MASK), SEQ_NUMBER_MASK);
110    }
111
112    #[test]
113    fn wrap_boundary_ordering() {
114        // 0 comes right after SEQ_NUMBER_MASK (the maximum 31-bit value) in
115        // circular sequence order.
116        assert!(seq_lt(SEQ_NUMBER_MASK, 0));
117        assert!(seq_gt(0, SEQ_NUMBER_MASK));
118        assert_eq!(seq_diff(0, SEQ_NUMBER_MASK), 1);
119        assert_eq!(seq_diff(SEQ_NUMBER_MASK, 0), -1);
120    }
121
122    #[test]
123    fn ordinary_ordering_without_wrap() {
124        assert!(seq_lt(10, 20));
125        assert!(seq_leq(10, 10));
126        assert!(seq_leq(10, 20));
127        assert!(seq_gt(20, 10));
128        assert!(seq_geq(20, 20));
129        assert!(seq_geq(20, 10));
130        assert!(!seq_lt(20, 10));
131        assert!(!seq_gt(10, 20));
132    }
133
134    #[test]
135    fn diff_is_antisymmetric() {
136        for (a, b) in [(0u32, 0u32), (5, 100), (SEQ_NUMBER_MASK, 3), (12345, 12340)] {
137            assert_eq!(seq_diff(a, b), -seq_diff(b, a));
138        }
139    }
140
141    #[test]
142    fn closed_range_membership_walks_forward_from_first() {
143        assert!(seq_in_closed_range(5, 5, 10));
144        assert!(seq_in_closed_range(10, 5, 10));
145        assert!(seq_in_closed_range(7, 5, 10));
146        assert!(!seq_in_closed_range(4, 5, 10));
147        assert!(!seq_in_closed_range(11, 5, 10));
148        // A `last` before `first` is a malformed (empty) range.
149        assert!(!seq_in_closed_range(7, 10, 5));
150        // Wrap-safe: the range straddles the 31-bit boundary.
151        assert!(seq_in_closed_range(SEQ_NUMBER_MASK, SEQ_NUMBER_MASK - 2, 1));
152        assert!(seq_in_closed_range(0, SEQ_NUMBER_MASK - 2, 1));
153        assert!(!seq_in_closed_range(2, SEQ_NUMBER_MASK - 2, 1));
154    }
155
156    #[test]
157    fn walking_forward_past_the_wrap_stays_consistent() {
158        // Start just below the wrap and walk forward 10 steps; sequence
159        // order must stay monotonic through the wrap.
160        let start = SEQ_NUMBER_MASK - 4;
161        let mut prev = start;
162        for _ in 0..10 {
163            let next = seq_next(prev);
164            assert!(seq_lt(prev, next), "prev={prev:#x} next={next:#x}");
165            assert_eq!(seq_diff(next, prev), 1);
166            prev = next;
167        }
168    }
169}