use crate::rfc8888::sender::stream_supports_ccfb;
use crate::stream_info::StreamInfo;
use crate::twcc::stream_supports_twcc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CongestionFeedbackFormat {
Rfc8888,
TransportCc,
}
pub fn negotiated_congestion_feedback(info: &StreamInfo) -> Option<CongestionFeedbackFormat> {
if stream_supports_ccfb(info) {
Some(CongestionFeedbackFormat::Rfc8888)
} else if stream_supports_twcc(info).is_some_and(|id| id != 0) {
Some(CongestionFeedbackFormat::TransportCc)
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::stream_info::{RTCPFeedback, RTPHeaderExtension};
fn info(twcc: bool, ccfb: bool) -> StreamInfo {
StreamInfo {
ssrc: 1,
rtp_header_extensions: if twcc {
vec![RTPHeaderExtension {
uri: crate::twcc::TRANSPORT_CC_URI.to_string(),
id: 5,
}]
} else {
vec![]
},
rtcp_feedback: if ccfb {
vec![RTCPFeedback {
typ: "ack".to_owned(),
parameter: "ccfb".to_owned(),
}]
} else {
vec![]
},
..Default::default()
}
}
#[test]
fn rfc8888_is_used_when_the_peer_accepted_it_and_transport_cc_otherwise() {
assert_eq!(
Some(CongestionFeedbackFormat::Rfc8888),
negotiated_congestion_feedback(&info(true, true)),
"both negotiated: RFC 8888, and only it"
);
assert_eq!(
Some(CongestionFeedbackFormat::Rfc8888),
negotiated_congestion_feedback(&info(false, true))
);
assert_eq!(
Some(CongestionFeedbackFormat::TransportCc),
negotiated_congestion_feedback(&info(true, false))
);
assert_eq!(None, negotiated_congestion_feedback(&info(false, false)));
}
}