Skip to main content

rtci_interceptor/flexfec/rfc8627/
params.rs

1//! The payload format parameters of `flexfec` (RFC 8627 section 5.1) as an `a=fmtp` line.
2
3use std::time::Duration;
4
5/// The parameters of an RFC 8627 `flexfec` payload type beyond its clock rate.
6///
7/// RFC 8627 registers one: the required `repair-window`. (`L`, `D` and `ToP` were optional
8/// parameters of draft-03 and did not survive into the RFC; a line that carries them is
9/// accepted and they are ignored, as any unknown parameter is: section 5.2.1.)
10#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
11pub struct FlexFecParameters {
12    /// `repair-window`: "the time that spans the source packets and the corresponding repair
13    /// packets", in microseconds on the wire. `None` when the line does not carry a usable one.
14    pub repair_window: Option<Duration>,
15}
16
17impl FlexFecParameters {
18    /// Parameters with the given repair window.
19    pub fn with_repair_window(repair_window: Duration) -> Self {
20        Self {
21            repair_window: Some(repair_window),
22        }
23    }
24
25    /// Read the parameters from the value of an `a=fmtp` line (what follows the payload type),
26    /// a semicolon-separated list of `name=value` pairs (section 5.2).
27    ///
28    /// Names are compared without case; unknown parameters are ignored. `repair-window:200000`,
29    /// the spelling of the RFC's second SDP example (section 7.1.2), is read as well.
30    pub fn parse(fmtp: &str) -> Self {
31        let mut parameters = Self::default();
32        for pair in fmtp.split(';') {
33            let pair = pair.trim();
34            let Some((name, value)) = pair.split_once('=').or_else(|| pair.split_once(':')) else {
35                continue;
36            };
37            if name.trim().eq_ignore_ascii_case("repair-window")
38                && let Ok(microseconds) = value.trim().parse::<u64>()
39            {
40                parameters.repair_window = Some(Duration::from_micros(microseconds));
41            }
42        }
43        parameters
44    }
45
46    /// The `a=fmtp` value for these parameters: `repair-window=<microseconds>`, or the empty
47    /// string when there is no repair window to state.
48    pub fn to_fmtp(&self) -> String {
49        match self.repair_window {
50            Some(repair_window) => format!("repair-window={}", repair_window.as_micros()),
51            None => String::new(),
52        }
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    /// Section 7.1.1: `a=fmtp:98; repair-window=200000` is a 200 ms repair window.
61    #[test]
62    fn the_repair_window_is_read_in_microseconds() {
63        assert_eq!(
64            Some(Duration::from_millis(200)),
65            FlexFecParameters::parse("repair-window=200000").repair_window
66        );
67        assert_eq!(
68            Some(Duration::from_millis(200)),
69            FlexFecParameters::parse("; repair-window=200000").repair_window,
70            "as section 7.1.1 writes it, after the payload type"
71        );
72        assert_eq!(
73            Some(Duration::from_millis(200)),
74            FlexFecParameters::parse(" repair-window:200000").repair_window,
75            "as section 7.1.2 writes it"
76        );
77        assert_eq!(
78            Some(Duration::from_secs(10)),
79            FlexFecParameters::parse("Repair-Window = 10000000 ").repair_window
80        );
81    }
82
83    /// Section 5.2.1: any unknown option is ignored.
84    #[test]
85    fn unknown_parameters_are_ignored() {
86        let parameters = FlexFecParameters::parse("L=4; D=3;ToP=2;repair-window=5000;x-future=1");
87        assert_eq!(Some(Duration::from_millis(5)), parameters.repair_window);
88        assert_eq!(
89            "repair-window=5000",
90            parameters.to_fmtp(),
91            "and not written back"
92        );
93    }
94
95    #[test]
96    fn a_line_without_a_usable_repair_window_has_none() {
97        for line in [
98            "",
99            "repair-window",
100            "repair-window=",
101            "repair-window=soon",
102            "L=4",
103        ] {
104            assert_eq!(
105                None,
106                FlexFecParameters::parse(line).repair_window,
107                "{line:?}"
108            );
109        }
110        assert_eq!("", FlexFecParameters::default().to_fmtp());
111    }
112
113    #[test]
114    fn the_repair_window_is_written_in_microseconds() {
115        assert_eq!(
116            "repair-window=200000",
117            FlexFecParameters::with_repair_window(Duration::from_millis(200)).to_fmtp()
118        );
119    }
120}