Skip to main content

rtci_rtcp/transport_feedbacks/ecn_feedback/
mod.rs

1#[cfg(test)]
2mod ecn_feedback_test;
3
4use crate::{header::*, packet::*, util::*};
5use shared::{
6    error::{Error, Result},
7    marshal::{Marshal, MarshalSize, Unmarshal},
8};
9
10use bytes::{Buf, BufMut};
11use std::any::Any;
12use std::fmt;
13
14/// The octets of the counters an ECN Feedback Report and an ECN Summary Report data block
15/// share (RFC 6679 sections 5.1 and 5.2): ECT(0), ECT(1), ECN-CE, not-ECT, lost, duplicates.
16pub const ECN_COUNTERS_LENGTH: usize = 16;
17/// The octets of an ECN Feedback Report (RFC 6679 section 5.1, Figure 2): the extended
18/// highest sequence number and the counters.
19pub const ECN_FEEDBACK_REPORT_LENGTH: usize = 4 + ECN_COUNTERS_LENGTH;
20
21/// The cumulative ECN counters of one RTP source as a receiver keeps them (RFC 6679 section
22/// 5.1). The two ECT counters are 32 bits on the wire, the others the 16 least significant bits
23/// of the receiver's count ("the field will wrap if more than 65535 ... have been received").
24/// "The counters SHALL be initiated to 0 for each new SSRC received."
25#[derive(Debug, PartialEq, Eq, Default, Clone, Copy)]
26pub struct EcnCounters {
27    /// RTP packets received with ECT(0).
28    pub ect0: u32,
29    /// RTP packets received with ECT(1).
30    pub ect1: u32,
31    /// RTP packets received with ECN-CE, duplicates included.
32    pub ce: u16,
33    /// RTP packets received not-ECT.
34    pub not_ect: u16,
35    /// Packets expected minus packets received that are not duplicates; late packets are not
36    /// lost.
37    pub lost: u16,
38    /// Packets received that duplicate one already received.
39    pub duplicates: u16,
40}
41
42impl EcnCounters {
43    pub(crate) fn put(&self, buf: &mut &mut [u8]) {
44        buf.put_u32(self.ect0);
45        buf.put_u32(self.ect1);
46        buf.put_u16(self.ce);
47        buf.put_u16(self.not_ect);
48        buf.put_u16(self.lost);
49        buf.put_u16(self.duplicates);
50    }
51
52    pub(crate) fn get<B: Buf>(buf: &mut B) -> Self {
53        Self {
54            ect0: buf.get_u32(),
55            ect1: buf.get_u32(),
56            ce: buf.get_u16(),
57            not_ect: buf.get_u16(),
58            lost: buf.get_u16(),
59            duplicates: buf.get_u16(),
60        }
61    }
62}
63
64/// The RTP/AVPF transport-layer ECN Feedback packet (RFC 6679 section 5.1): RTPFB, FMT 8,
65/// one ECN Feedback Report about one RTP source. Negotiated with `a=rtcp-fb:<pt> nack ecn`
66/// (section 6.2); sent early or immediately when ECN-CE marks or losses need to reach the
67/// sender quickly.
68///
69/// ```text
70///  0                   1                   2                   3
71///  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
72/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73/// |V=2|P|  FMT=8  |  PT=RTPFB=205 |          length               |
74/// |                  SSRC of packet sender                        |
75/// |                  SSRC of media source                         |
76/// | Extended Highest Sequence Number                              |
77/// | ECT (0) Counter                                               |
78/// | ECT (1) Counter                                               |
79/// | ECN-CE Counter                | not-ECT Counter               |
80/// | Lost Packets Counter          | Duplication Counter           |
81/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
82/// ```
83#[derive(Debug, PartialEq, Eq, Default, Clone)]
84pub struct EcnFeedback {
85    /// SSRC of the packet sender.
86    pub sender_ssrc: u32,
87    /// SSRC of the media source the report is about.
88    pub media_ssrc: u32,
89    /// The 32-bit extended highest sequence number received (RFC 3550): the highest
90    /// sequence number the report relates to.
91    pub extended_highest_sequence_number: u32,
92    /// The counters.
93    pub counters: EcnCounters,
94}
95
96impl fmt::Display for EcnFeedback {
97    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98        write!(
99            f,
100            "EcnFeedback {:x} {:x} {} {:?}",
101            self.sender_ssrc, self.media_ssrc, self.extended_highest_sequence_number, self.counters
102        )
103    }
104}
105
106impl Packet for EcnFeedback {
107    fn header(&self) -> Header {
108        Header {
109            padding: get_padding_size(self.raw_size()) != 0,
110            count: FORMAT_ECN,
111            packet_type: PacketType::TransportSpecificFeedback,
112            length: ((self.marshal_size() / 4) - 1) as u16,
113        }
114    }
115
116    fn destination_ssrc(&self) -> Vec<u32> {
117        vec![self.media_ssrc]
118    }
119
120    fn raw_size(&self) -> usize {
121        HEADER_LENGTH + SSRC_LENGTH * 2 + ECN_FEEDBACK_REPORT_LENGTH
122    }
123
124    fn as_any(&self) -> &dyn Any {
125        self
126    }
127
128    fn equal(&self, other: &dyn Packet) -> bool {
129        other.as_any().downcast_ref::<EcnFeedback>() == Some(self)
130    }
131
132    fn cloned(&self) -> Box<dyn Packet> {
133        Box::new(self.clone())
134    }
135}
136
137impl MarshalSize for EcnFeedback {
138    fn marshal_size(&self) -> usize {
139        let l = self.raw_size();
140        l + get_padding_size(l)
141    }
142}
143
144impl Marshal for EcnFeedback {
145    fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
146        if buf.remaining_mut() < self.marshal_size() {
147            return Err(Error::BufferTooShort);
148        }
149        let h = self.header();
150        let n = h.marshal_to(buf)?;
151        buf = &mut buf[n..];
152        buf.put_u32(self.sender_ssrc);
153        buf.put_u32(self.media_ssrc);
154        buf.put_u32(self.extended_highest_sequence_number);
155        self.counters.put(&mut buf);
156        Ok(self.marshal_size())
157    }
158}
159
160impl Unmarshal for EcnFeedback {
161    /// Decodes the packet. Its FCI is one ECN Feedback Report ("Each ECN Feedback Report
162    /// corresponds to a single RTP source"): an FCI of another length is refused.
163    fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
164    where
165        Self: Sized,
166        B: Buf,
167    {
168        let (sender_ssrc, media_ssrc, mut fci) = read_feedback(
169            raw_packet,
170            PacketType::TransportSpecificFeedback,
171            FORMAT_ECN,
172        )?;
173        if fci.len() != ECN_FEEDBACK_REPORT_LENGTH {
174            return Err(Error::PacketTooShort);
175        }
176        let extended_highest_sequence_number = fci.get_u32();
177        let counters = EcnCounters::get(&mut fci);
178        Ok(EcnFeedback {
179            sender_ssrc,
180            media_ssrc,
181            extended_highest_sequence_number,
182            counters,
183        })
184    }
185}