rtci-rtp 0.5.0

RTP packets, header extensions (RFC 3550, RFC 8285) and codec payload formats for WebRTC
Documentation
//! The checks the RTP parser fuzz targets run, one function per target of the same name.
//!
//! They live in the crate so that two callers share them: the cargo-fuzz targets in `fuzz/`
//! (built with `--cfg fuzzing`) and the replay test (`fuzz_regressions_test`), which runs every
//! committed input in `fuzz/regressions/<target>/` through the same function in the ordinary
//! test job. Each function panics when its oracle is violated. Not part of the supported API.
#![allow(missing_docs)]

use bytes::Buf;
use shared::marshal::{Marshal, MarshalSize, Unmarshal};

use crate::header::Header;
use crate::packet::Packet;

/// An RTP packet parsed: no panic.
pub fn parse_packet(data: &[u8]) {
    let mut buf = data;
    let _ = Packet::unmarshal(&mut buf);
}

/// Round trip of an RTP packet and its header (RFC 3550 5.1 and 5.3.1, RFC 8285).
///
/// * The header consumes exactly the octets its own size accounts for: the fixed 12, the
///   CSRCs, and the extension block the length field declares (`marshal_size`).
/// * A packet that parses is written (its size as `marshal_size` says) and the bytes parse to
///   an equal packet: header fields, CSRCs, extension profile, every extension element in order,
///   the extension block's padding, the payload, and the padding count. One exception the
///   audit records (RFC3550-5.1-4): a padding count of 0 with P set is read as "no padding"
///   and the count is not kept, so marshal pads to 32 bits instead; only `padding_size` may
///   then differ, and writing the second reading again gives the same bytes.
/// * A block too long for the 16-bit length field (only reachable with an input over 65535
///   octets) is the one case marshal may refuse.
pub fn header_roundtrip(data: &[u8]) {
    let mut buf = data;
    let Ok(header) = Header::unmarshal(&mut buf) else {
        return;
    };
    let consumed = data.len() - buf.remaining();
    assert_eq!(
        header.marshal_size(),
        consumed,
        "the header's size is what parsing it consumed"
    );

    let mut buf = data;
    let Ok(first) = Packet::unmarshal(&mut buf) else {
        return;
    };
    let written = match first.marshal() {
        Ok(written) => written,
        Err(e) => {
            assert!(data.len() > 65535, "a parsed packet is written: {e}");
            return;
        }
    };
    assert_eq!(
        written.len(),
        first.marshal_size(),
        "marshal_size is what is written"
    );

    let mut reread = &written[..];
    let second = Packet::unmarshal(&mut reread).expect("what was written parses");
    if first.header.padding && first.header.padding_size == 0 {
        let mut normalised = second.clone();
        normalised.header.padding_size = 0;
        assert_eq!(normalised, first, "parse(marshal(parse(x))) == parse(x)");
    } else {
        assert_eq!(second, first, "parse(marshal(parse(x))) == parse(x)");
    }

    let again = second.marshal().expect("and written again");
    assert_eq!(again, written, "marshal is a fixed point");
}