Skip to main content

rtci_rtcp/payload_feedbacks/
application_layer_feedback.rs

1//! Application layer feedback (AFB, [RFC 4585] §6.4) that is not REMB.
2//!
3//! [RFC 4585]: https://datatracker.ietf.org/doc/html/rfc4585#section-6.4
4use crate::{header::*, packet::*, util::*};
5use shared::{
6    error::{Error, Result},
7    marshal::{Marshal, MarshalSize, Unmarshal},
8};
9
10use bytes::{Buf, BufMut, Bytes};
11use std::fmt;
12
13/// Application layer feedback (PSFB FMT=15): a message from one application to its peer that
14/// RTCP only carries.
15///
16/// The application identifies its own messages by what it puts in `data`; REMB, which opens
17/// its data with `REMB`, is parsed as
18/// [`ReceiverEstimatedMaximumBitrate`](crate::payload_feedbacks::receiver_estimated_maximum_bitrate::ReceiverEstimatedMaximumBitrate)
19/// instead.
20#[derive(Debug, PartialEq, Eq, Default, Clone)]
21pub struct ApplicationLayerFeedback {
22    /// The source that sends the message.
23    pub sender_ssrc: u32,
24    /// The media source the message is about.
25    pub media_ssrc: u32,
26    /// The application's message. Its length is a multiple of four octets: the application
27    /// pads it, in a way its peer can tell from the data (RFC 4585 §6.4).
28    pub data: Bytes,
29}
30
31impl fmt::Display for ApplicationLayerFeedback {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        write!(
34            f,
35            "ApplicationLayerFeedback {:x} {:x} {} octets",
36            self.sender_ssrc,
37            self.media_ssrc,
38            self.data.len()
39        )
40    }
41}
42
43impl Packet for ApplicationLayerFeedback {
44    fn header(&self) -> Header {
45        Header {
46            padding: false,
47            count: FORMAT_AFB,
48            packet_type: PacketType::PayloadSpecificFeedback,
49            length: ((self.marshal_size() / 4) - 1) as u16,
50        }
51    }
52
53    fn destination_ssrc(&self) -> Vec<u32> {
54        vec![self.media_ssrc]
55    }
56
57    fn raw_size(&self) -> usize {
58        HEADER_LENGTH + FEEDBACK_SSRCS_LENGTH + self.data.len()
59    }
60
61    packet_any!(ApplicationLayerFeedback);
62}
63
64impl MarshalSize for ApplicationLayerFeedback {
65    fn marshal_size(&self) -> usize {
66        let l = self.raw_size();
67        l + get_padding_size(l)
68    }
69}
70
71impl Marshal for ApplicationLayerFeedback {
72    fn marshal_to(&self, buf: &mut [u8]) -> Result<usize> {
73        if !self.data.len().is_multiple_of(4) {
74            return Err(Error::WrongMarshalSize);
75        }
76        let size = self.raw_size();
77        let mut buf = put_feedback_start(
78            buf,
79            PacketType::PayloadSpecificFeedback,
80            FORMAT_AFB,
81            size,
82            self.sender_ssrc,
83            self.media_ssrc,
84        )?;
85        buf.put_slice(&self.data);
86        Ok(size)
87    }
88}
89
90impl Unmarshal for ApplicationLayerFeedback {
91    fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
92    where
93        Self: Sized,
94        B: Buf,
95    {
96        let (sender_ssrc, media_ssrc, data) =
97            read_feedback(raw_packet, PacketType::PayloadSpecificFeedback, FORMAT_AFB)?;
98        if !data.len().is_multiple_of(4) {
99            return Err(Error::WrongPadding);
100        }
101        Ok(ApplicationLayerFeedback {
102            sender_ssrc,
103            media_ssrc,
104            data,
105        })
106    }
107}
108
109#[cfg(test)]
110mod test {
111    use super::*;
112
113    #[test]
114    fn application_layer_feedback_round_trips() {
115        let afb = ApplicationLayerFeedback {
116            sender_ssrc: 1,
117            media_ssrc: 2,
118            data: Bytes::from_static(b"ABCD\x01\x02\x03\x04"),
119        };
120        let raw = afb.marshal().unwrap();
121        assert_eq!(
122            &[
123                0x8f, 0xce, 0x00, 0x04, 0, 0, 0, 1, 0, 0, 0, 2, b'A', b'B', b'C', b'D', 1, 2, 3, 4
124            ][..],
125            &raw[..]
126        );
127        assert_eq!(
128            afb,
129            ApplicationLayerFeedback::unmarshal(&mut raw.freeze()).unwrap()
130        );
131
132        let empty = ApplicationLayerFeedback::default();
133        let raw = empty.marshal().unwrap();
134        assert_eq!(12, raw.len());
135        assert_eq!(
136            empty,
137            ApplicationLayerFeedback::unmarshal(&mut raw.freeze()).unwrap()
138        );
139    }
140
141    // RFC 4585 6.4: the message is padded to 32 bits, by the application.
142    #[test]
143    fn data_that_is_not_a_whole_number_of_words_is_refused() {
144        let afb = ApplicationLayerFeedback {
145            data: Bytes::from_static(b"ABC"),
146            ..Default::default()
147        };
148        assert_eq!(Err(Error::WrongMarshalSize), afb.marshal());
149
150        // RTCP padding of three octets leaves data of five.
151        let mut raw = Bytes::from_static(&[
152            0xaf, 0xce, 0x00, 0x04, 0, 0, 0, 1, 0, 0, 0, 2, 1, 2, 3, 4, 5, 0, 0, 3,
153        ]);
154        assert_eq!(
155            Err(Error::WrongPadding),
156            ApplicationLayerFeedback::unmarshal(&mut raw)
157        );
158        // Another feedback format is not AFB.
159        let mut raw = Bytes::from_static(&[0x81, 0xce, 0, 2, 0, 0, 0, 1, 0, 0, 0, 2]);
160        assert_eq!(
161            Err(Error::WrongType),
162            ApplicationLayerFeedback::unmarshal(&mut raw)
163        );
164    }
165}