rtci-rtcp 0.4.1

RTCP packet parsing and serialization (RFC 3550, RFC 4585, RFC 5104, RFC 3611, RFC 8888) for WebRTC
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Reference Picture Selection Indication (RPSI, [RFC 4585] ยง6.3.3).
//!
//! [RFC 4585]: https://datatracker.ietf.org/doc/html/rfc4585#section-6.3.3
use crate::{header::*, packet::*, util::*};
use shared::{
    error::{Error, Result},
    marshal::{Marshal, MarshalSize, Unmarshal},
};

use bytes::{Buf, BufMut, Bytes};
use std::fmt;

/// The PB octet and the payload type octet that precede the bit string.
const RPSI_FCI_HEADER_LENGTH: usize = 2;
const RPSI_PAYLOAD_TYPE_MAX: u8 = 0x7f;

/// Reference Picture Selection Indication (PSFB FMT=3): the receiver names a reference picture
/// in the codec's own terms. Exactly one indication per packet.
///
/// ```text
///  0                   1                   2                   3
///  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
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |      PB       |0| Payload Type|    Native RPSI bit string     |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |   defined per codec          ...                | Padding (0) |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// ```
#[derive(Debug, PartialEq, Eq, Default, Clone)]
pub struct ReferencePictureSelectionIndication {
    /// The source that sends the indication.
    pub sender_ssrc: u32,
    /// The media sender it is for.
    pub media_ssrc: u32,
    /// The RTP payload type that says how the bit string is to be read, 7 bits.
    pub payload_type: u8,
    /// The native RPSI bit string, as the codec defines it, most significant bit first. When
    /// its length is not a whole number of octets, the last octet's low bits are padding.
    pub native_rpsi: Bytes,
    /// How many low bits of the last octet of `native_rpsi` are not part of the bit string,
    /// 0 to 7. They are sent as zero.
    pub unused_bits: u8,
}

impl ReferencePictureSelectionIndication {
    /// An RPSI for an H.265 stream (RFC 7798 section 8.3): a request that the encoder use the
    /// picture with `nuh_layer_id` (6 bits) and `pic_order_cnt_val` as its reference. The
    /// native bit string is "a base16 representation of the 8 bits consisting of the 2 most
    /// significant bits equal to 0 and 6 bits of nuh_layer_id, ... followed by the 32 bits
    /// representing the value of the PicOrderCntVal (in network byte order)": ten hexadecimal
    /// digits. RFC 7798 deprecates RPSI as a positive acknowledgement; this is the request
    /// form, the only one it allows. A `nuh_layer_id` over 63 is refused.
    pub fn hevc(
        sender_ssrc: u32,
        media_ssrc: u32,
        payload_type: u8,
        nuh_layer_id: u8,
        pic_order_cnt_val: u32,
    ) -> Result<Self> {
        if nuh_layer_id > 0x3f {
            return Err(Error::InvalidHeader);
        }
        if payload_type > RPSI_PAYLOAD_TYPE_MAX {
            return Err(Error::WrongPayloadType);
        }
        let mut octets = [0u8; 5];
        octets[0] = nuh_layer_id;
        octets[1..].copy_from_slice(&pic_order_cnt_val.to_be_bytes());
        let hex: String = octets.iter().map(|o| format!("{o:02X}")).collect();
        Ok(Self {
            sender_ssrc,
            media_ssrc,
            payload_type,
            native_rpsi: Bytes::from(hex.into_bytes()),
            unused_bits: 0,
        })
    }

    /// The `(nuh_layer_id, PicOrderCntVal)` of an H.265 RPSI's native bit string (RFC 7798
    /// section 8.3), `None` when it is not ten hexadecimal digits with the two high bits of
    /// the layer octet zero.
    pub fn hevc_picture(&self) -> Option<(u8, u32)> {
        if self.native_rpsi.len() != 10 || self.unused_bits != 0 {
            return None;
        }
        let text = std::str::from_utf8(&self.native_rpsi).ok()?;
        let layer = u8::from_str_radix(&text[..2], 16).ok()?;
        let poc = u32::from_str_radix(&text[2..], 16).ok()?;
        (layer <= 0x3f).then_some((layer, poc))
    }

    /// An RPSI for a VP8 stream (RFC 7741 section 5.1) or a VP9 stream (RFC 9628 section
    /// 5.1): the native bit string "is simply the (7- or 15-bit) Picture ID of the received
    /// frame", as its payload descriptor carried it: 7 bits for a Picture ID below 128 unless
    /// `extended`, 15 bits otherwise (the descriptor's M bit). The bits are left-aligned in the
    /// octets, the low bit of the last octet unused. A Picture ID above 15 bits is refused.
    pub fn picture_id(
        sender_ssrc: u32,
        media_ssrc: u32,
        payload_type: u8,
        picture_id: u16,
        extended: bool,
    ) -> Result<Self> {
        if picture_id > 0x7fff {
            return Err(Error::InvalidHeader);
        }
        if payload_type > RPSI_PAYLOAD_TYPE_MAX {
            return Err(Error::WrongPayloadType);
        }
        let native_rpsi = if extended || picture_id > 0x7f {
            Bytes::copy_from_slice(&(picture_id << 1).to_be_bytes())
        } else {
            Bytes::copy_from_slice(&[(picture_id as u8) << 1])
        };
        Ok(Self {
            sender_ssrc,
            media_ssrc,
            payload_type,
            native_rpsi,
            unused_bits: 1,
        })
    }

    /// The Picture ID of a VP8 or VP9 RPSI ([`Self::picture_id`]): a native bit string of 7 or
    /// 15 bits, `None` for any other length.
    pub fn vp8_vp9_picture_id(&self) -> Option<u16> {
        match (self.native_rpsi.len(), self.native_rpsi_bits()) {
            (1, 7) => Some(u16::from(self.native_rpsi[0] >> 1)),
            (2, 15) => Some(u16::from_be_bytes([self.native_rpsi[0], self.native_rpsi[1]]) >> 1),
            _ => None,
        }
    }

    /// The length of the native bit string in bits.
    pub fn native_rpsi_bits(&self) -> usize {
        (self.native_rpsi.len() * 8).saturating_sub(self.unused_bits as usize)
    }

    fn padding_octets(&self) -> usize {
        get_padding_size(RPSI_FCI_HEADER_LENGTH + self.native_rpsi.len())
    }
}

impl fmt::Display for ReferencePictureSelectionIndication {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "ReferencePictureSelectionIndication {:x} {:x} pt={} {} bits",
            self.sender_ssrc,
            self.media_ssrc,
            self.payload_type,
            self.native_rpsi_bits()
        )
    }
}

impl Packet for ReferencePictureSelectionIndication {
    fn header(&self) -> Header {
        Header {
            padding: false,
            count: FORMAT_RPSI,
            packet_type: PacketType::PayloadSpecificFeedback,
            length: ((self.marshal_size() / 4) - 1) as u16,
        }
    }

    fn destination_ssrc(&self) -> Vec<u32> {
        vec![self.media_ssrc]
    }

    fn raw_size(&self) -> usize {
        HEADER_LENGTH
            + FEEDBACK_SSRCS_LENGTH
            + RPSI_FCI_HEADER_LENGTH
            + self.native_rpsi.len()
            + self.padding_octets()
    }

    packet_any!(ReferencePictureSelectionIndication);
}

impl MarshalSize for ReferencePictureSelectionIndication {
    fn marshal_size(&self) -> usize {
        self.raw_size()
    }
}

impl Marshal for ReferencePictureSelectionIndication {
    fn marshal_to(&self, buf: &mut [u8]) -> Result<usize> {
        if self.payload_type > RPSI_PAYLOAD_TYPE_MAX {
            return Err(Error::WrongPayloadType);
        }
        if self.unused_bits > 7 || (self.native_rpsi.is_empty() && self.unused_bits != 0) {
            return Err(Error::WrongPadding);
        }

        let size = self.raw_size();
        let mut buf = put_feedback_start(
            buf,
            PacketType::PayloadSpecificFeedback,
            FORMAT_RPSI,
            size,
            self.sender_ssrc,
            self.media_ssrc,
        )?;

        // PB: the number of unused bits that pad the message to a multiple of 32 bits.
        let padding_octets = self.padding_octets();
        buf.put_u8(self.unused_bits + 8 * padding_octets as u8);
        buf.put_u8(self.payload_type);
        if let Some((last, head)) = self.native_rpsi.split_last() {
            buf.put_slice(head);
            // RFC 4585 6.3.3.2: the padding bits are set to 0.
            buf.put_u8(last & (0xffu8 << self.unused_bits));
        }
        buf.put_bytes(0, padding_octets);
        Ok(size)
    }
}

impl Unmarshal for ReferencePictureSelectionIndication {
    fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
    where
        Self: Sized,
        B: Buf,
    {
        let (sender_ssrc, media_ssrc, mut fci) =
            read_feedback(raw_packet, PacketType::PayloadSpecificFeedback, FORMAT_RPSI)?;
        // One indication: PB, payload type, and the string padded to a word.
        if fci.len() < 4 {
            return Err(Error::PacketTooShort);
        }
        if !fci.len().is_multiple_of(4) {
            return Err(Error::WrongPadding);
        }

        let padding_bits = fci.get_u8() as usize;
        // The bit before the payload type is ignored on reception.
        let payload_type = fci.get_u8() & RPSI_PAYLOAD_TYPE_MAX;
        if padding_bits > fci.len() * 8 {
            return Err(Error::WrongPadding);
        }

        let mut native_rpsi = fci.split_to(fci.len() - padding_bits / 8);
        let unused_bits = (padding_bits % 8) as u8;
        if unused_bits != 0 {
            // The padding bits are not part of the string, whatever the sender put there.
            let mut octets = native_rpsi.to_vec();
            if let Some(last) = octets.last_mut() {
                *last &= 0xffu8 << unused_bits;
            }
            native_rpsi = Bytes::from(octets);
        }

        Ok(ReferencePictureSelectionIndication {
            sender_ssrc,
            media_ssrc,
            payload_type,
            native_rpsi,
            unused_bits,
        })
    }
}

#[cfg(test)]
mod hevc_test {
    use super::*;

    /// RFC 7798 section 8.3: the H.265 native RPSI bit string is the base16 text of the layer
    /// octet (two zero bits, six of `nuh_layer_id`) and the 32-bit PicOrderCntVal, network
    /// order; RPSI is a reference picture selection request.
    #[test]
    fn an_hevc_rpsi_names_the_layer_and_picture_order_count_in_base16() {
        let rpsi =
            ReferencePictureSelectionIndication::hevc(0x0102_0304, 0x0a0b_0c0d, 49, 5, 0x0012_34ab)
                .unwrap();
        assert_eq!(&b"05001234AB"[..], &rpsi.native_rpsi[..]);
        assert_eq!(80, rpsi.native_rpsi_bits());
        let raw = rpsi.marshal().unwrap();
        let read = ReferencePictureSelectionIndication::unmarshal(&mut raw.freeze()).unwrap();
        assert_eq!(Some((5, 0x0012_34ab)), read.hevc_picture());
        assert_eq!(49, read.payload_type);

        assert!(ReferencePictureSelectionIndication::hevc(1, 2, 49, 64, 0).is_err());
        let not_hevc = ReferencePictureSelectionIndication {
            native_rpsi: Bytes::from_static(b"C0000000000"),
            ..rpsi
        };
        assert_eq!(None, not_hevc.hevc_picture());
    }
}

#[cfg(test)]
mod picture_id_test {
    use super::*;

    /// RFC 7741 section 5.1 and RFC 9628 section 5.1: the VP8 and VP9 native RPSI bit string
    /// is the 7- or 15-bit Picture ID of the frame, left-aligned, one bit of padding in the
    /// last octet; it reads back after the wire.
    #[test]
    fn a_vp8_or_vp9_rpsi_carries_the_7_or_15_bit_picture_id() {
        let short = ReferencePictureSelectionIndication::picture_id(1, 2, 96, 0x55, false).unwrap();
        assert_eq!(&[0xAA][..], &short.native_rpsi[..]);
        assert_eq!(7, short.native_rpsi_bits());
        let long = ReferencePictureSelectionIndication::picture_id(1, 2, 96, 0x1234, true).unwrap();
        assert_eq!(&[0x24, 0x68][..], &long.native_rpsi[..]);
        assert_eq!(15, long.native_rpsi_bits());
        // A Picture ID that needs 15 bits takes them whatever `extended` says.
        let wide = ReferencePictureSelectionIndication::picture_id(1, 2, 96, 0x80, false).unwrap();
        assert_eq!(15, wide.native_rpsi_bits());
        for (rpsi, id) in [(short, 0x55), (long, 0x1234), (wide, 0x80)] {
            let raw = rpsi.marshal().unwrap();
            let read = ReferencePictureSelectionIndication::unmarshal(&mut raw.freeze()).unwrap();
            assert_eq!(Some(id), read.vp8_vp9_picture_id());
        }
        assert!(ReferencePictureSelectionIndication::picture_id(1, 2, 96, 0x8000, true).is_err());
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn rpsi_round_trips_with_pb_counting_the_padding_bits() {
        // 13 bits of native string: two octets, three bits unused, no padding octet.
        let rpsi = ReferencePictureSelectionIndication {
            sender_ssrc: 0x01020304,
            media_ssrc: 0x0a0b0c0d,
            payload_type: 96,
            native_rpsi: Bytes::from_static(&[0xab, 0xc8]),
            unused_bits: 3,
        };
        let raw = rpsi.marshal().unwrap();
        assert_eq!(
            &[
                0x83, 0xce, 0x00, 0x03, // v=2, FMT=3, PSFB, len=3
                0x01, 0x02, 0x03, 0x04, // sender
                0x0a, 0x0b, 0x0c, 0x0d, // media source
                0x03, 0x60, 0xab, 0xc8, // PB=3, pt 96, 13 bits + 3 padding bits
            ][..],
            &raw[..]
        );
        assert_eq!(13, rpsi.native_rpsi_bits());
        assert_eq!(
            rpsi,
            ReferencePictureSelectionIndication::unmarshal(&mut raw.freeze()).unwrap()
        );

        // Three whole octets: three octets of padding, PB = 24.
        let rpsi = ReferencePictureSelectionIndication {
            native_rpsi: Bytes::from_static(&[1, 2, 3]),
            unused_bits: 0,
            ..rpsi
        };
        let raw = rpsi.marshal().unwrap();
        assert_eq!(&[0x18, 0x60, 1, 2, 3, 0, 0, 0][..], &raw[12..]);
        assert_eq!(
            rpsi,
            ReferencePictureSelectionIndication::unmarshal(&mut raw.freeze()).unwrap()
        );
    }

    // RFC 4585 6.3.3.2: padding bits are zero when sent and are not part of the string.
    #[test]
    fn padding_bits_are_sent_as_zero_and_ignored_when_received() {
        let dirty = ReferencePictureSelectionIndication {
            sender_ssrc: 1,
            media_ssrc: 2,
            payload_type: 96,
            native_rpsi: Bytes::from_static(&[0xff, 0xff]),
            unused_bits: 4,
        };
        let raw = dirty.marshal().unwrap();
        assert_eq!(&[0x04, 0x60, 0xff, 0xf0][..], &raw[12..]);

        // Padding bits set, and the zero bit before the payload type set.
        let mut raw = Bytes::from_static(&[
            0x83, 0xce, 0x00, 0x03, 0, 0, 0, 1, 0, 0, 0, 2, 0x04, 0xe0, 0xff, 0xff,
        ]);
        let rpsi = ReferencePictureSelectionIndication::unmarshal(&mut raw).unwrap();
        assert_eq!(96, rpsi.payload_type);
        assert_eq!(&[0xff, 0xf0][..], &rpsi.native_rpsi[..]);
        assert_eq!(4, rpsi.unused_bits);
    }

    #[test]
    fn malformed_indications_are_refused() {
        let bad_pt = ReferencePictureSelectionIndication {
            payload_type: 128,
            ..Default::default()
        };
        assert_eq!(Err(Error::WrongPayloadType), bad_pt.marshal());
        let bad_bits = ReferencePictureSelectionIndication {
            unused_bits: 1,
            ..Default::default()
        };
        assert_eq!(Err(Error::WrongPadding), bad_bits.marshal());

        // No FCI.
        let mut raw = Bytes::from_static(&[0x83, 0xce, 0, 2, 0, 0, 0, 1, 0, 0, 0, 2]);
        assert_eq!(
            Err(Error::PacketTooShort),
            ReferencePictureSelectionIndication::unmarshal(&mut raw)
        );
        // PB claims more padding bits than the message has after its two fixed octets.
        let mut raw = Bytes::from_static(&[0x83, 0xce, 0, 3, 0, 0, 0, 1, 0, 0, 0, 2, 17, 96, 0, 0]);
        assert_eq!(
            Err(Error::WrongPadding),
            ReferencePictureSelectionIndication::unmarshal(&mut raw)
        );
    }
}