Skip to main content

pcmflux/
red.rs

1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5 */
6
7//! RFC 2198 redundant audio as a client's microphone sends it: where each block of a payload
8//! sits, before anything is decoded. Nothing here reaches outside the module, so a fuzz build
9//! (`fuzz/`) compiles it on its own.
10
11/// Max redundant copies per frame and RED history depth.
12pub(crate) const RED_MAX_DISTANCE: i32 = 4;
13
14/// The blocks of a payload, oldest first, as `(ts, start, len)`; `red_blocks` says how many.
15pub(crate) type Blocks = [(i64, usize, usize); RED_MAX_DISTANCE as usize + 1];
16
17/// The blocks of an RFC 2198 payload, oldest first, as `(ts, start, len)` within it: each
18/// redundant block at `primary_ts` less its 14-bit offset (an empty one left out), then the
19/// primary, which is whatever bytes remain. `None` for a payload whose headers or blocks run
20/// past it, or that carries more than `RED_MAX_DISTANCE` redundant blocks.
21pub(crate) fn red_blocks(payload: &[u8], primary_ts: i64) -> Option<(Blocks, usize)> {
22    let n = payload.len();
23    let mut i = 0usize;
24    let mut offs = [0i64; RED_MAX_DISTANCE as usize];
25    let mut lens = [0usize; RED_MAX_DISTANCE as usize];
26    let mut nh = 0usize;
27    while i < n && (payload[i] & 0x80) != 0 {
28        if i + 4 > n || nh >= RED_MAX_DISTANCE as usize {
29            return None;
30        }
31        let field = (u32::from(payload[i + 1]) << 16)
32            | (u32::from(payload[i + 2]) << 8)
33            | u32::from(payload[i + 3]);
34        offs[nh] = i64::from((field >> 10) & 0x3FFF);
35        lens[nh] = (field & 0x3FF) as usize;
36        nh += 1;
37        i += 4;
38    }
39    if i >= n {
40        return None;
41    }
42    i += 1;
43
44    let mut frames: Blocks = [(0, 0, 0); RED_MAX_DISTANCE as usize + 1];
45    let mut nf = 0usize;
46    for k in 0..nh {
47        if i + lens[k] > n {
48            return None;
49        }
50        if lens[k] > 0 {
51            frames[nf] = (primary_ts - offs[k], i, lens[k]);
52            nf += 1;
53        }
54        i += lens[k];
55    }
56    frames[nf] = (primary_ts, i, n - i);
57    nf += 1;
58    Some((frames, nf))
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    /// One redundant block header: the follow bit, payload type 111, a 14-bit offset and a
66    /// 10-bit length.
67    fn header(offset: u32, len: u32) -> [u8; 4] {
68        let field = (offset << 10) | len;
69        [
70            0x80 | 111,
71            (field >> 16) as u8,
72            (field >> 8) as u8,
73            field as u8,
74        ]
75    }
76
77    #[test]
78    fn blocks_come_oldest_first_and_the_primary_takes_the_rest() {
79        let mut p = header(960, 3).to_vec();
80        p.push(111);
81        p.extend_from_slice(&[1, 2, 3, 9, 9]);
82        let (blocks, n) = red_blocks(&p, 10_000).expect("well formed");
83        assert_eq!(n, 2);
84        assert_eq!(blocks[0], (10_000 - 960, 5, 3));
85        assert_eq!(blocks[1], (10_000, 8, 2));
86    }
87
88    #[test]
89    fn an_empty_redundant_block_is_left_out() {
90        let mut p = header(960, 0).to_vec();
91        p.push(111);
92        p.extend_from_slice(&[7]);
93        let (blocks, n) = red_blocks(&p, 5_000).expect("well formed");
94        assert_eq!(n, 1);
95        assert_eq!(blocks[0], (5_000, 5, 1));
96    }
97
98    #[test]
99    fn a_payload_that_runs_out_is_refused() {
100        assert_eq!(red_blocks(&[], 0), None, "empty");
101        assert_eq!(
102            red_blocks(&header(960, 3)[..3], 0),
103            None,
104            "a header cut short"
105        );
106        assert_eq!(red_blocks(&header(960, 3), 0), None, "no primary header");
107        let mut p = header(960, 8).to_vec();
108        p.push(111);
109        p.extend_from_slice(&[1, 2]);
110        assert_eq!(red_blocks(&p, 0), None, "a block past the end");
111    }
112
113    #[test]
114    fn more_redundant_blocks_than_kept_are_refused() {
115        let mut p = Vec::new();
116        for _ in 0..=RED_MAX_DISTANCE {
117            p.extend_from_slice(&header(960, 0));
118        }
119        p.push(111);
120        p.push(1);
121        assert_eq!(red_blocks(&p, 0), None);
122        let mut ok = Vec::new();
123        for _ in 0..RED_MAX_DISTANCE {
124            ok.extend_from_slice(&header(960, 0));
125        }
126        ok.push(111);
127        ok.push(1);
128        assert!(red_blocks(&ok, 0).is_some(), "as many as kept are taken");
129    }
130}