Skip to main content

rtci_interceptor/cc/
feedback.rs

1//! Which congestion control feedback one stream runs on.
2
3use crate::rfc8888::sender::stream_supports_ccfb;
4use crate::stream_info::StreamInfo;
5use crate::twcc::stream_supports_twcc;
6
7/// The per-packet feedback format a stream's arrivals are reported in.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub enum CongestionFeedbackFormat {
10    /// RTCP congestion control feedback, RFC 8888 (`a=rtcp-fb:<pt> ack ccfb`).
11    Rfc8888,
12    /// Transport-wide congestion control, draft-holmer-rmcat-transport-wide-cc-extensions-01
13    /// (`a=rtcp-fb:<pt> transport-cc` and its header extension).
14    TransportCc,
15}
16
17/// The one feedback format a stream uses, from what was negotiated for it.
18///
19/// An offer may name several congestion control feedback mechanisms "that offer substantially
20/// the same semantics" (RFC 8888 section 6), and the answer SHOULD keep one. Whatever the answer
21/// kept, a stream is reported in exactly one format, or the sender's estimator counts every
22/// packet twice: RFC 8888 when `ack ccfb` is among the stream's negotiated feedback, otherwise
23/// transport-wide congestion control when its header extension was negotiated, otherwise
24/// none. Every interceptor that numbers, records or reads per-packet feedback decides with this
25/// function, so both directions of both endpoints agree.
26pub fn negotiated_congestion_feedback(info: &StreamInfo) -> Option<CongestionFeedbackFormat> {
27    if stream_supports_ccfb(info) {
28        Some(CongestionFeedbackFormat::Rfc8888)
29    } else if stream_supports_twcc(info).is_some_and(|id| id != 0) {
30        Some(CongestionFeedbackFormat::TransportCc)
31    } else {
32        None
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39    use crate::stream_info::{RTCPFeedback, RTPHeaderExtension};
40
41    fn info(twcc: bool, ccfb: bool) -> StreamInfo {
42        StreamInfo {
43            ssrc: 1,
44            rtp_header_extensions: if twcc {
45                vec![RTPHeaderExtension {
46                    uri: crate::twcc::TRANSPORT_CC_URI.to_string(),
47                    id: 5,
48                }]
49            } else {
50                vec![]
51            },
52            rtcp_feedback: if ccfb {
53                vec![RTCPFeedback {
54                    typ: "ack".to_owned(),
55                    parameter: "ccfb".to_owned(),
56                }]
57            } else {
58                vec![]
59            },
60            ..Default::default()
61        }
62    }
63
64    #[test]
65    fn rfc8888_is_used_when_the_peer_accepted_it_and_transport_cc_otherwise() {
66        assert_eq!(
67            Some(CongestionFeedbackFormat::Rfc8888),
68            negotiated_congestion_feedback(&info(true, true)),
69            "both negotiated: RFC 8888, and only it"
70        );
71        assert_eq!(
72            Some(CongestionFeedbackFormat::Rfc8888),
73            negotiated_congestion_feedback(&info(false, true))
74        );
75        assert_eq!(
76            Some(CongestionFeedbackFormat::TransportCc),
77            negotiated_congestion_feedback(&info(true, false))
78        );
79        assert_eq!(None, negotiated_congestion_feedback(&info(false, false)));
80    }
81}