Skip to main content

rtci_rtcp/payload_feedbacks/full_intra_request/
mod.rs

1#[cfg(test)]
2mod full_intra_request_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/// A FIREntry is a (ssrc, seqno) pair, as carried by FullIntraRequest.
15#[derive(Debug, PartialEq, Eq, Default, Clone)]
16pub struct FirEntry {
17    /// The SSRC being asked for an intra frame.
18    pub ssrc: u32,
19    /// A counter incremented per request, so a sender can ignore retransmitted duplicates.
20    pub sequence_number: u8,
21}
22
23/// The FullIntraRequest packet is used to reliably request an Intra frame
24/// in a video stream.  See RFC 5104 Section 3.5.1.  This is not for loss
25/// recovery, which should use PictureLossIndication (PLI) instead.
26#[derive(Debug, PartialEq, Eq, Default, Clone)]
27pub struct FullIntraRequest {
28    /// The SSRC of the requesting receiver.
29    pub sender_ssrc: u32,
30    /// The "SSRC of media source" of the feedback header, as a peer wrote it. RFC 5104 ยง4.3.1.2
31    /// does not use the field and has it sent as 0, so it is written as 0 whatever is here; the
32    /// sources the request is for are the entries of `fir`.
33    pub media_ssrc: u32,
34    /// One entry per SSRC an intra frame is requested from.
35    pub fir: Vec<FirEntry>,
36}
37
38const FIR_OFFSET: usize = 8;
39const FIR_ENTRY_LENGTH: usize = 8;
40
41impl fmt::Display for FullIntraRequest {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        let mut out = format!("FullIntraRequest {} {}", self.sender_ssrc, self.media_ssrc);
44        for e in &self.fir {
45            out += format!(" ({} {})", e.ssrc, e.sequence_number).as_str();
46        }
47        write!(f, "{out}")
48    }
49}
50
51impl Packet for FullIntraRequest {
52    fn header(&self) -> Header {
53        Header {
54            padding: get_padding_size(self.raw_size()) != 0,
55            count: FORMAT_FIR,
56            packet_type: PacketType::PayloadSpecificFeedback,
57            length: ((self.marshal_size() / 4) - 1) as u16,
58        }
59    }
60
61    /// destination_ssrc returns an array of SSRC values that this packet refers to.
62    fn destination_ssrc(&self) -> Vec<u32> {
63        let mut ssrcs: Vec<u32> = Vec::with_capacity(self.fir.len());
64        for entry in &self.fir {
65            ssrcs.push(entry.ssrc);
66        }
67        ssrcs
68    }
69
70    fn raw_size(&self) -> usize {
71        HEADER_LENGTH + FIR_OFFSET + self.fir.len() * 8
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::<FullIntraRequest>() == Some(self)
80    }
81
82    fn cloned(&self) -> Box<dyn Packet> {
83        Box::new(self.clone())
84    }
85}
86
87impl MarshalSize for FullIntraRequest {
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 FullIntraRequest {
96    /// Marshal encodes the FullIntraRequest
97    fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
98        // RFC 5104 4.3.1.1: "The FCI field MUST contain one or more FIR entries."
99        if self.fir.is_empty() {
100            return Err(Error::PacketTooShort);
101        }
102        if buf.remaining_mut() < self.marshal_size() {
103            return Err(Error::BufferTooShort);
104        }
105
106        let h = header_for(
107            PacketType::PayloadSpecificFeedback,
108            FORMAT_FIR,
109            self.raw_size(),
110        )?;
111        let n = h.marshal_to(buf)?;
112        buf = &mut buf[n..];
113
114        buf.put_u32(self.sender_ssrc);
115        // RFC 5104 4.3.1.2: the "SSRC of media source" is not used and SHALL be set to 0; the
116        // sources the request is for are in the FCI.
117        buf.put_u32(0);
118
119        for fir in self.fir.iter() {
120            buf.put_u32(fir.ssrc);
121            buf.put_u8(fir.sequence_number);
122            buf.put_u8(0);
123            buf.put_u16(0);
124        }
125
126        Ok(self.marshal_size())
127    }
128}
129
130impl Unmarshal for FullIntraRequest {
131    /// Unmarshal decodes the FullIntraRequest
132    fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
133    where
134        Self: Sized,
135        B: Buf,
136    {
137        if raw_packet.remaining() < FIR_MIN_OCTET_COUNT {
138            return Err(Error::PacketTooShort);
139        }
140
141        let (sender_ssrc, media_ssrc, mut fci) =
142            read_feedback(raw_packet, PacketType::PayloadSpecificFeedback, FORMAT_FIR)?;
143        // RFC 5104 4.3.1.1: one or more entries, the length being 2 + 2 * N.
144        if fci.is_empty() {
145            return Err(Error::PacketTooShort);
146        }
147        if !fci.len().is_multiple_of(FIR_ENTRY_LENGTH) {
148            return Err(Error::InvalidHeader);
149        }
150
151        let mut fir = Vec::with_capacity(fci.len() / FIR_ENTRY_LENGTH);
152        while fci.has_remaining() {
153            fir.push(FirEntry {
154                ssrc: fci.get_u32(),
155                sequence_number: fci.get_u8(),
156            });
157            // 24 reserved bits, ignored on reception.
158            fci.advance(3);
159        }
160
161        Ok(FullIntraRequest {
162            sender_ssrc,
163            media_ssrc,
164            fir,
165        })
166    }
167}