Skip to main content

rtci_rtcp/extended_report/
ecn.rs

1use super::*;
2use crate::transport_feedbacks::ecn_feedback::{ECN_COUNTERS_LENGTH, EcnCounters};
3
4/// One ECN Summary Report data block: an SSRC and its counters (RFC 6679 section 5.2,
5/// Figure 4), five 32-bit words.
6const ECN_SUMMARY_ENTRY_LENGTH: usize = SSRC_LENGTH + ECN_COUNTERS_LENGTH;
7
8/// The counters of one media sender in an [`EcnSummaryReportBlock`].
9#[derive(Debug, Default, PartialEq, Eq, Clone, Copy)]
10pub struct EcnSummaryReport {
11    /// The media sender the counters are about.
12    pub ssrc: u32,
13    /// Its cumulative counters, as in the ECN Feedback Report.
14    pub counters: EcnCounters,
15}
16
17/// The RTCP XR ECN Summary Report block (RFC 6679 section 5.2): BT 13, one data block per
18/// media sender. Negotiated with `a=rtcp-xr:ecn-sum` (section 6.3); an ECN-capable receiver
19/// includes one in every regular compound RTCP packet (section 7.3.2), with no data block
20/// when no SSRC counts as a sender (block length zero).
21///
22/// ```text
23///  0                   1                   2                   3
24///  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
25/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26/// |     BT=13     | Reserved      |         Block Length          |
27/// | SSRC of Media Sender                                          |
28/// | ECT (0) Counter                                               |
29/// | ECT (1) Counter                                               |
30/// | ECN-CE Counter                | not-ECT Counter               |
31/// | Lost Packets Counter          | Duplication Counter           |
32/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33/// ```
34#[derive(Debug, Default, PartialEq, Eq, Clone)]
35pub struct EcnSummaryReportBlock {
36    /// One entry per media sender.
37    pub reports: Vec<EcnSummaryReport>,
38}
39
40impl fmt::Display for EcnSummaryReportBlock {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        write!(f, "{self:?}")
43    }
44}
45
46impl EcnSummaryReportBlock {
47    /// The XR block header: "Reserved: All bits SHALL be set to 0 on transmission"; the
48    /// length is 5 × the number of data blocks.
49    pub fn xr_header(&self) -> XRHeader {
50        XRHeader {
51            block_type: BlockType::EcnSummary,
52            type_specific: 0,
53            block_length: (self.raw_size() / 4 - 1) as u16,
54        }
55    }
56}
57
58impl Packet for EcnSummaryReportBlock {
59    fn header(&self) -> Header {
60        Header::default()
61    }
62
63    fn destination_ssrc(&self) -> Vec<u32> {
64        self.reports.iter().map(|r| r.ssrc).collect()
65    }
66
67    fn raw_size(&self) -> usize {
68        XR_HEADER_LENGTH + self.reports.len() * ECN_SUMMARY_ENTRY_LENGTH
69    }
70
71    fn as_any(&self) -> &dyn Any {
72        self
73    }
74
75    fn equal(&self, other: &dyn Packet) -> bool {
76        other.as_any().downcast_ref::<EcnSummaryReportBlock>() == Some(self)
77    }
78
79    fn cloned(&self) -> Box<dyn Packet> {
80        Box::new(self.clone())
81    }
82}
83
84impl MarshalSize for EcnSummaryReportBlock {
85    fn marshal_size(&self) -> usize {
86        self.raw_size()
87    }
88}
89
90impl Marshal for EcnSummaryReportBlock {
91    fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
92        if buf.remaining_mut() < self.marshal_size() {
93            return Err(Error::BufferTooShort);
94        }
95        if self.raw_size() / 4 - 1 > usize::from(u16::MAX) {
96            return Err(Error::InvalidBlockSize);
97        }
98        let h = self.xr_header();
99        let n = h.marshal_to(buf)?;
100        buf = &mut buf[n..];
101        for report in &self.reports {
102            buf.put_u32(report.ssrc);
103            report.counters.put(&mut buf);
104        }
105        Ok(self.marshal_size())
106    }
107}
108
109impl Unmarshal for EcnSummaryReportBlock {
110    /// Decodes the block. "Receivers MUST discard reports where the block length is not a
111    /// multiple of five, since these cannot be valid": such a block is refused (and kept as
112    /// an unknown block by [`ExtendedReport`]); the reserved octet is ignored.
113    fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
114    where
115        Self: Sized,
116        B: Buf,
117    {
118        let xr_header = XRHeader::unmarshal(raw_packet)?;
119        if xr_header.block_type != BlockType::EcnSummary {
120            return Err(Error::WrongType);
121        }
122        if !xr_header.block_length.is_multiple_of(5) {
123            return Err(Error::InvalidBlockSize);
124        }
125        let length = usize::from(xr_header.block_length) * 4;
126        if raw_packet.remaining() < length {
127            return Err(Error::PacketTooShort);
128        }
129        let mut reports = Vec::with_capacity(length / ECN_SUMMARY_ENTRY_LENGTH);
130        for _ in 0..length / ECN_SUMMARY_ENTRY_LENGTH {
131            let ssrc = raw_packet.get_u32();
132            let counters = EcnCounters::get(raw_packet);
133            reports.push(EcnSummaryReport { ssrc, counters });
134        }
135        Ok(EcnSummaryReportBlock { reports })
136    }
137}