Skip to main content

rtci_rtcp/extended_report/
ssr.rs

1use super::*;
2
3const SSR_REPORT_BLOCK_LENGTH: u16 = 4 + 2 * 2 + 4 * 6 + 4;
4
5/// StatisticsSummaryReportBlock encodes a Statistics Summary Report
6/// Block as described in RFC 3611, section 4.6.
7///
8///  0                   1                   2                   3
9///  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
10/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
11/// |     BT=6      |L|D|J|ToH|rsvd.|       block length = 9        |
12/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
13/// |                        ssrc of source                         |
14/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
15/// |          begin_seq            |             end_seq           |
16/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
17/// |                        lost_packets                           |
18/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
19/// |                        dup_packets                            |
20/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
21/// |                         min_jitter                            |
22/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
23/// |                         max_jitter                            |
24/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
25/// |                         mean_jitter                           |
26/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
27/// |                         dev_jitter                            |
28/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
29/// | min_ttl_or_hl | max_ttl_or_hl |mean_ttl_or_hl | dev_ttl_or_hl |
30/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31#[derive(Debug, Default, PartialEq, Eq, Clone)]
32pub struct StatisticsSummaryReportBlock {
33    //not included in marshal/unmarshal
34    /// Whether the loss fields are present.
35    pub loss_reports: bool,
36    /// Whether the duplicate fields are present.
37    pub duplicate_reports: bool,
38    /// Whether the jitter fields are present.
39    pub jitter_reports: bool,
40    /// Whether the TTL fields hold an IPv4 TTL, an IPv6 hop limit, or nothing.
41    pub ttl_or_hop_limit: TTLorHopLimitType,
42
43    //marshal/unmarshal
44    /// The SSRC being summarized.
45    pub ssrc: u32,
46    /// The first sequence number covered.
47    pub begin_seq: u16,
48    /// One past the last sequence number covered.
49    pub end_seq: u16,
50    /// Packets lost in the interval.
51    pub lost_packets: u32,
52    /// Duplicate packets in the interval.
53    pub dup_packets: u32,
54    /// Minimum observed jitter, in RTP timestamp units.
55    pub min_jitter: u32,
56    /// Maximum observed jitter.
57    pub max_jitter: u32,
58    /// Mean observed jitter.
59    pub mean_jitter: u32,
60    /// Standard deviation of observed jitter.
61    pub dev_jitter: u32,
62    /// Minimum TTL or hop limit seen.
63    pub min_ttl_or_hl: u8,
64    /// Maximum TTL or hop limit seen.
65    pub max_ttl_or_hl: u8,
66    /// Mean TTL or hop limit.
67    pub mean_ttl_or_hl: u8,
68    /// Standard deviation of TTL or hop limit.
69    pub dev_ttl_or_hl: u8,
70}
71
72impl fmt::Display for StatisticsSummaryReportBlock {
73    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74        write!(f, "{self:?}")
75    }
76}
77
78/// TTLorHopLimitType encodes values for the ToH field in
79/// a StatisticsSummaryReportBlock
80#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
81pub enum TTLorHopLimitType {
82    #[default]
83    /// No TTL or hop-limit data is present.
84    Missing = 0,
85    /// The fields hold an IPv4 TTL.
86    IPv4 = 1,
87    /// The fields hold an IPv6 hop limit.
88    IPv6 = 2,
89}
90
91impl From<u8> for TTLorHopLimitType {
92    fn from(v: u8) -> Self {
93        match v {
94            1 => TTLorHopLimitType::IPv4,
95            2 => TTLorHopLimitType::IPv6,
96            _ => TTLorHopLimitType::Missing,
97        }
98    }
99}
100
101impl fmt::Display for TTLorHopLimitType {
102    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103        let s = match *self {
104            TTLorHopLimitType::Missing => "[ToH Missing]",
105            TTLorHopLimitType::IPv4 => "[ToH = IPv4]",
106            TTLorHopLimitType::IPv6 => "[ToH = IPv6]",
107        };
108        write!(f, "{s}")
109    }
110}
111
112impl StatisticsSummaryReportBlock {
113    /// This block with every field its flags do not report set to zero: the loss count
114    /// without `loss_reports`, the duplicate count without `duplicate_reports`, the jitter
115    /// values without `jitter_reports`, the TTL / hop limit values when `ttl_or_hop_limit` is
116    /// `Missing`.
117    pub fn reported(&self) -> Self {
118        let mut block = self.clone();
119        if !block.loss_reports {
120            block.lost_packets = 0;
121        }
122        if !block.duplicate_reports {
123            block.dup_packets = 0;
124        }
125        if !block.jitter_reports {
126            block.min_jitter = 0;
127            block.max_jitter = 0;
128            block.mean_jitter = 0;
129            block.dev_jitter = 0;
130        }
131        if block.ttl_or_hop_limit == TTLorHopLimitType::Missing {
132            block.min_ttl_or_hl = 0;
133            block.max_ttl_or_hl = 0;
134            block.mean_ttl_or_hl = 0;
135            block.dev_ttl_or_hl = 0;
136        }
137        block
138    }
139
140    /// The XR block header describing this block's type and length.
141    pub fn xr_header(&self) -> XRHeader {
142        let mut type_specific = 0x00;
143        if self.loss_reports {
144            type_specific |= 0x80;
145        }
146        if self.duplicate_reports {
147            type_specific |= 0x40;
148        }
149        if self.jitter_reports {
150            type_specific |= 0x20;
151        }
152        type_specific |= (self.ttl_or_hop_limit as u8 & 0x03) << 3;
153
154        XRHeader {
155            block_type: BlockType::StatisticsSummary,
156            type_specific,
157            block_length: (self.raw_size() / 4 - 1) as u16,
158        }
159    }
160}
161
162impl Packet for StatisticsSummaryReportBlock {
163    fn header(&self) -> Header {
164        Header::default()
165    }
166
167    /// destination_ssrc returns an array of ssrc values that this report block refers to.
168    fn destination_ssrc(&self) -> Vec<u32> {
169        vec![self.ssrc]
170    }
171
172    fn raw_size(&self) -> usize {
173        XR_HEADER_LENGTH + SSR_REPORT_BLOCK_LENGTH as usize
174    }
175
176    fn as_any(&self) -> &dyn Any {
177        self
178    }
179    fn equal(&self, other: &dyn Packet) -> bool {
180        other
181            .as_any()
182            .downcast_ref::<StatisticsSummaryReportBlock>()
183            == Some(self)
184    }
185    fn cloned(&self) -> Box<dyn Packet> {
186        Box::new(self.clone())
187    }
188}
189
190impl MarshalSize for StatisticsSummaryReportBlock {
191    fn marshal_size(&self) -> usize {
192        self.raw_size()
193    }
194}
195
196impl Marshal for StatisticsSummaryReportBlock {
197    /// marshal_to encodes the StatisticsSummaryReportBlock in binary
198    fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
199        if buf.remaining_mut() < self.marshal_size() {
200            return Err(Error::BufferTooShort);
201        }
202
203        let h = self.xr_header();
204        let n = h.marshal_to(buf)?;
205        buf = &mut buf[n..];
206
207        // RFC 3611 4.6: "The fields corresponding to unreported parameters MUST be present,
208        // but are set to zero."
209        let reported = self.reported();
210
211        buf.put_u32(reported.ssrc);
212        buf.put_u16(reported.begin_seq);
213        buf.put_u16(reported.end_seq);
214        buf.put_u32(reported.lost_packets);
215        buf.put_u32(reported.dup_packets);
216        buf.put_u32(reported.min_jitter);
217        buf.put_u32(reported.max_jitter);
218        buf.put_u32(reported.mean_jitter);
219        buf.put_u32(reported.dev_jitter);
220        buf.put_u8(reported.min_ttl_or_hl);
221        buf.put_u8(reported.max_ttl_or_hl);
222        buf.put_u8(reported.mean_ttl_or_hl);
223        buf.put_u8(reported.dev_ttl_or_hl);
224
225        Ok(self.marshal_size())
226    }
227}
228
229impl Unmarshal for StatisticsSummaryReportBlock {
230    /// Unmarshal decodes the StatisticsSummaryReportBlock from binary
231    fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
232    where
233        Self: Sized,
234        B: Buf,
235    {
236        if raw_packet.remaining() < XR_HEADER_LENGTH {
237            return Err(Error::PacketTooShort);
238        }
239
240        let xr_header = XRHeader::unmarshal(raw_packet)?;
241        if xr_header.block_type != BlockType::StatisticsSummary {
242            return Err(Error::WrongType);
243        }
244        let block_length = match xr_header.block_length.checked_mul(4) {
245            Some(length) => length,
246            None => return Err(Error::InvalidBlockSize),
247        };
248        if block_length != SSR_REPORT_BLOCK_LENGTH || raw_packet.remaining() < block_length as usize
249        {
250            return Err(Error::PacketTooShort);
251        }
252
253        let loss_reports = xr_header.type_specific & 0x80 != 0;
254        let duplicate_reports = xr_header.type_specific & 0x40 != 0;
255        let jitter_reports = xr_header.type_specific & 0x20 != 0;
256        let ttl_or_hop_limit: TTLorHopLimitType = ((xr_header.type_specific & 0x18) >> 3).into();
257
258        let ssrc = raw_packet.get_u32();
259        let begin_seq = raw_packet.get_u16();
260        let end_seq = raw_packet.get_u16();
261        let lost_packets = raw_packet.get_u32();
262        let dup_packets = raw_packet.get_u32();
263        let min_jitter = raw_packet.get_u32();
264        let max_jitter = raw_packet.get_u32();
265        let mean_jitter = raw_packet.get_u32();
266        let dev_jitter = raw_packet.get_u32();
267        let min_ttl_or_hl = raw_packet.get_u8();
268        let max_ttl_or_hl = raw_packet.get_u8();
269        let mean_ttl_or_hl = raw_packet.get_u8();
270        let dev_ttl_or_hl = raw_packet.get_u8();
271
272        // "The receiver MUST ignore any such field": a field whose flag is clear reads as zero,
273        // whatever the sender put there.
274        Ok(StatisticsSummaryReportBlock {
275            loss_reports,
276            duplicate_reports,
277            jitter_reports,
278            ttl_or_hop_limit,
279
280            ssrc,
281            begin_seq,
282            end_seq,
283            lost_packets,
284            dup_packets,
285            min_jitter,
286            max_jitter,
287            mean_jitter,
288            dev_jitter,
289            min_ttl_or_hl,
290            max_ttl_or_hl,
291            mean_ttl_or_hl,
292            dev_ttl_or_hl,
293        }
294        .reported())
295    }
296}