Skip to main content

rtci_rtcp/receiver_report/
mod.rs

1#[cfg(test)]
2mod receiver_report_test;
3
4use crate::{header::*, packet::*, reception_report::*, util::*};
5use shared::{
6    error::{Error, Result},
7    marshal::{Marshal, MarshalSize, Unmarshal},
8};
9
10use bytes::{Buf, BufMut, Bytes};
11use std::any::Any;
12use std::fmt;
13
14pub(super) const RR_SSRC_OFFSET: usize = HEADER_LENGTH;
15pub(super) const RR_REPORT_OFFSET: usize = RR_SSRC_OFFSET + SSRC_LENGTH;
16
17/// A ReceiverReport (RR) packet provides reception quality feedback for an RTP stream
18#[derive(Debug, PartialEq, Eq, Default, Clone)]
19pub struct ReceiverReport {
20    /// The synchronization source identifier for the originator of this RR packet.
21    pub ssrc: u32,
22    /// Zero or more reception report blocks depending on the number of other
23    /// sources heard by this sender since the last report. Each reception report
24    /// block conveys statistics on the reception of RTP packets from a
25    /// single synchronization source.
26    pub reports: Vec<ReceptionReport>,
27    /// Extension contains additional, payload-specific information that needs to
28    /// be reported regularly about the receiver.
29    pub profile_extensions: Bytes,
30}
31
32impl fmt::Display for ReceiverReport {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        let mut out = format!("ReceiverReport from {}\n", self.ssrc);
35        out += "\tSSRC    \tLost\tLastSequence\n";
36        for rep in &self.reports {
37            out += format!(
38                "\t{:x}\t{}/{}\t{}\n",
39                rep.ssrc, rep.fraction_lost, rep.total_lost, rep.last_sequence_number
40            )
41            .as_str();
42        }
43        out += format!("\tProfile Extension Data: {:?}\n", self.profile_extensions).as_str();
44
45        write!(f, "{out}")
46    }
47}
48
49impl Packet for ReceiverReport {
50    /// Header returns the Header associated with this packet.
51    fn header(&self) -> Header {
52        Header {
53            padding: get_padding_size(self.raw_size()) != 0,
54            count: self.reports.len() as u8,
55            packet_type: PacketType::ReceiverReport,
56            length: ((self.marshal_size() / 4) - 1) as u16,
57        }
58    }
59
60    /// destination_ssrc returns an array of SSRC values that this packet refers to.
61    fn destination_ssrc(&self) -> Vec<u32> {
62        self.reports.iter().map(|x| x.ssrc).collect()
63    }
64
65    fn raw_size(&self) -> usize {
66        let mut reps_length = 0;
67        for rep in &self.reports {
68            reps_length += rep.marshal_size();
69        }
70
71        HEADER_LENGTH + SSRC_LENGTH + reps_length + self.profile_extensions.len()
72    }
73
74    fn as_any(&self) -> &dyn Any {
75        self
76    }
77
78    fn equal(&self, other: &dyn Packet) -> bool {
79        other.as_any().downcast_ref::<ReceiverReport>() == Some(self)
80    }
81
82    fn cloned(&self) -> Box<dyn Packet> {
83        Box::new(self.clone())
84    }
85}
86
87impl MarshalSize for ReceiverReport {
88    fn marshal_size(&self) -> usize {
89        let l = self.raw_size();
90        // align to 32-bit boundary
91        l + get_padding_size(l)
92    }
93}
94
95impl Marshal for ReceiverReport {
96    /// marshal_to encodes the packet in binary.
97    fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
98        if self.reports.len() > COUNT_MAX {
99            return Err(Error::TooManyReports);
100        }
101
102        // A profile-specific extension is a whole number of 32-bit words. It is not filled up
103        // with RTCP padding: this packet opens a compound packet, where the P bit must be
104        // clear (RFC 3550 Appendix A.2).
105        if !self.profile_extensions.len().is_multiple_of(4) {
106            return Err(Error::WrongMarshalSize);
107        }
108
109        if buf.remaining_mut() < self.marshal_size() {
110            return Err(Error::BufferTooShort);
111        }
112
113        /*
114         *         0                   1                   2                   3
115         *         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
116         *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
117         * header |V=2|P|    RC   |   PT=RR=201   |             length            |
118         *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
119         *        |                     SSRC of packet sender                     |
120         *        +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
121         * report |                 SSRC_1 (SSRC of first source)                 |
122         * block  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
123         *   1    | fraction lost |       cumulative number of packets lost       |
124         *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
125         *        |           extended highest sequence number received           |
126         *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
127         *        |                      interarrival jitter                      |
128         *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
129         *        |                         last SR (LSR)                         |
130         *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
131         *        |                   delay since last SR (DLSR)                  |
132         *        +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
133         * report |                 SSRC_2 (SSRC of second source)                |
134         * block  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
135         *   2    :                               ...                             :
136         *        +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
137         *        |                  profile-specific extensions                  |
138         *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139         */
140        let h = self.header();
141        let n = h.marshal_to(buf)?;
142        buf = &mut buf[n..];
143
144        buf.put_u32(self.ssrc);
145
146        for report in &self.reports {
147            let n = report.marshal_to(buf)?;
148            buf = &mut buf[n..];
149        }
150
151        buf.put(self.profile_extensions.clone());
152
153        if h.padding {
154            put_padding(buf, self.raw_size());
155        }
156
157        Ok(self.marshal_size())
158    }
159}
160
161impl Unmarshal for ReceiverReport {
162    /// Unmarshal decodes the ReceiverReport from binary
163    fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
164    where
165        Self: Sized,
166        B: Buf,
167    {
168        /*
169         *         0                   1                   2                   3
170         *         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
171         *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
172         * header |V=2|P|    RC   |   PT=RR=201   |             length            |
173         *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
174         *        |                     SSRC of packet sender                     |
175         *        +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
176         * report |                 SSRC_1 (SSRC of first source)                 |
177         * block  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
178         *   1    | fraction lost |       cumulative number of packets lost       |
179         *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
180         *        |           extended highest sequence number received           |
181         *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
182         *        |                      interarrival jitter                      |
183         *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
184         *        |                         last SR (LSR)                         |
185         *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
186         *        |                   delay since last SR (DLSR)                  |
187         *        +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
188         * report |                 SSRC_2 (SSRC of second source)                |
189         * block  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
190         *   2    :                               ...                             :
191         *        +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
192         *        |                  profile-specific extensions                  |
193         *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
194         */
195        // Padding the P bit announces is cut off here: it is not control information
196        // (RFC 3550 6.4.1), so it is not a profile-specific extension either.
197        let (header, mut body) = read_packet(raw_packet)?;
198        if header.packet_type != PacketType::ReceiverReport {
199            return Err(Error::WrongType);
200        }
201        let raw_packet_len = HEADER_LENGTH + body.len();
202        if raw_packet_len < (HEADER_LENGTH + SSRC_LENGTH) {
203            return Err(Error::PacketTooShort);
204        }
205        let raw_packet = &mut body;
206
207        let ssrc = raw_packet.get_u32();
208
209        let mut offset = RR_REPORT_OFFSET;
210        let mut reports = Vec::with_capacity(header.count as usize);
211        for _ in 0..header.count {
212            if offset + RECEPTION_REPORT_LENGTH > raw_packet_len {
213                return Err(Error::PacketTooShort);
214            }
215            let reception_report = ReceptionReport::unmarshal(raw_packet)?;
216            reports.push(reception_report);
217            offset += RECEPTION_REPORT_LENGTH;
218        }
219        // RFC 3550 6.4.1 / 6.4.2: what follows the report blocks is a profile-specific
220        // extension, a whole number of 32-bit words.
221        if !raw_packet.remaining().is_multiple_of(4) {
222            return Err(Error::WrongPadding);
223        }
224        let profile_extensions = raw_packet.copy_to_bytes(raw_packet.remaining());
225
226        Ok(ReceiverReport {
227            ssrc,
228            reports,
229            profile_extensions,
230        })
231    }
232}