rtci_interceptor/cc/
feedback.rs1use crate::rfc8888::sender::stream_supports_ccfb;
4use crate::stream_info::StreamInfo;
5use crate::twcc::stream_supports_twcc;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub enum CongestionFeedbackFormat {
10 Rfc8888,
12 TransportCc,
15}
16
17pub 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}