Skip to main content

rtci_rtcp/extended_report/
unknown.rs

1use super::*;
2
3/// UnknownReportBlock stores a report block that is not parsed: one of a type this crate does
4/// not model, or one of a known type that is malformed.
5///
6/// It keeps what the block header said, so the block is written back as what it was.
7#[derive(Debug, Default, PartialEq, Eq, Clone)]
8pub struct UnknownReportBlock {
9    /// The block's type.
10    pub block_type: BlockType,
11    /// The type-specific octet of the block header.
12    pub type_specific: TypeSpecificField,
13    /// The block's bytes after its header, left unparsed. A whole number of 32-bit words.
14    pub bytes: Bytes,
15}
16
17impl fmt::Display for UnknownReportBlock {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        write!(f, "{self:?}")
20    }
21}
22
23impl UnknownReportBlock {
24    /// The XR block header describing this block's type and length.
25    pub fn xr_header(&self) -> XRHeader {
26        XRHeader {
27            block_type: self.block_type,
28            type_specific: self.type_specific,
29            block_length: (self.raw_size() / 4 - 1) as u16,
30        }
31    }
32}
33
34impl Packet for UnknownReportBlock {
35    fn header(&self) -> Header {
36        Header::default()
37    }
38
39    /// destination_ssrc returns an array of ssrc values that this report block refers to.
40    fn destination_ssrc(&self) -> Vec<u32> {
41        vec![]
42    }
43
44    fn raw_size(&self) -> usize {
45        XR_HEADER_LENGTH + self.bytes.len()
46    }
47
48    fn as_any(&self) -> &dyn Any {
49        self
50    }
51    fn equal(&self, other: &dyn Packet) -> bool {
52        other.as_any().downcast_ref::<UnknownReportBlock>() == Some(self)
53    }
54    fn cloned(&self) -> Box<dyn Packet> {
55        Box::new(self.clone())
56    }
57}
58
59impl MarshalSize for UnknownReportBlock {
60    fn marshal_size(&self) -> usize {
61        self.raw_size()
62    }
63}
64
65impl Marshal for UnknownReportBlock {
66    /// marshal_to encodes the UnknownReportBlock in binary
67    fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
68        // RFC 3611 3: the block length counts 32-bit words, in 16 bits.
69        if !self.bytes.len().is_multiple_of(4) || self.bytes.len() / 4 > u16::MAX as usize {
70            return Err(Error::InvalidBlockSize);
71        }
72        if buf.remaining_mut() < self.marshal_size() {
73            return Err(Error::BufferTooShort);
74        }
75
76        let h = self.xr_header();
77        let n = h.marshal_to(buf)?;
78        buf = &mut buf[n..];
79
80        buf.put(self.bytes.clone());
81
82        Ok(self.marshal_size())
83    }
84}
85
86impl Unmarshal for UnknownReportBlock {
87    /// Unmarshal decodes the UnknownReportBlock from binary
88    fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
89    where
90        Self: Sized,
91        B: Buf,
92    {
93        if raw_packet.remaining() < XR_HEADER_LENGTH {
94            return Err(Error::PacketTooShort);
95        }
96
97        let xr_header = XRHeader::unmarshal(raw_packet)?;
98        let block_length = match xr_header.block_length.checked_mul(4) {
99            Some(length) => length,
100            None => return Err(Error::InvalidBlockSize),
101        };
102        if raw_packet.remaining() < block_length as usize {
103            return Err(Error::PacketTooShort);
104        }
105
106        let bytes = raw_packet.copy_to_bytes(block_length as usize);
107
108        Ok(UnknownReportBlock {
109            block_type: xr_header.block_type,
110            type_specific: xr_header.type_specific,
111            bytes,
112        })
113    }
114}