Skip to main content

rtc_rtcp/extended_report/
unknown.rs

1use super::*;
2
3/// UnknownReportBlock is used to store bytes for any report block
4/// that has an unknown Report Block Type.
5#[derive(Debug, Default, PartialEq, Eq, Clone)]
6pub struct UnknownReportBlock {
7    /// The block's bytes, left unparsed.
8    pub bytes: Bytes,
9}
10
11impl fmt::Display for UnknownReportBlock {
12    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13        write!(f, "{self:?}")
14    }
15}
16
17impl UnknownReportBlock {
18    /// The XR block header describing this block's type and length.
19    pub fn xr_header(&self) -> XRHeader {
20        XRHeader {
21            block_type: BlockType::Unknown,
22            type_specific: 0,
23            block_length: (self.raw_size() / 4 - 1) as u16,
24        }
25    }
26}
27
28impl Packet for UnknownReportBlock {
29    fn header(&self) -> Header {
30        Header::default()
31    }
32
33    /// destination_ssrc returns an array of ssrc values that this report block refers to.
34    fn destination_ssrc(&self) -> Vec<u32> {
35        vec![]
36    }
37
38    fn raw_size(&self) -> usize {
39        XR_HEADER_LENGTH + self.bytes.len()
40    }
41
42    fn as_any(&self) -> &dyn Any {
43        self
44    }
45    fn equal(&self, other: &dyn Packet) -> bool {
46        other.as_any().downcast_ref::<UnknownReportBlock>() == Some(self)
47    }
48    fn cloned(&self) -> Box<dyn Packet> {
49        Box::new(self.clone())
50    }
51}
52
53impl MarshalSize for UnknownReportBlock {
54    fn marshal_size(&self) -> usize {
55        self.raw_size()
56    }
57}
58
59impl Marshal for UnknownReportBlock {
60    /// marshal_to encodes the UnknownReportBlock in binary
61    fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
62        if buf.remaining_mut() < self.marshal_size() {
63            return Err(Error::BufferTooShort);
64        }
65
66        let h = self.xr_header();
67        let n = h.marshal_to(buf)?;
68        buf = &mut buf[n..];
69
70        buf.put(self.bytes.clone());
71
72        Ok(self.marshal_size())
73    }
74}
75
76impl Unmarshal for UnknownReportBlock {
77    /// Unmarshal decodes the UnknownReportBlock from binary
78    fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
79    where
80        Self: Sized,
81        B: Buf,
82    {
83        if raw_packet.remaining() < XR_HEADER_LENGTH {
84            return Err(Error::PacketTooShort);
85        }
86
87        let xr_header = XRHeader::unmarshal(raw_packet)?;
88        let block_length = match xr_header.block_length.checked_mul(4) {
89            Some(length) => length,
90            None => return Err(Error::InvalidBlockSize),
91        };
92        if raw_packet.remaining() < block_length as usize {
93            return Err(Error::PacketTooShort);
94        }
95
96        let bytes = raw_packet.copy_to_bytes(block_length as usize);
97
98        Ok(UnknownReportBlock { bytes })
99    }
100}