rtci-rtp 0.5.0

RTP packets, header extensions (RFC 3550, RFC 8285) and codec payload formats for WebRTC
Documentation
use super::*;

#[test]
fn test_vp8_unmarshal() -> Result<()> {
    let mut pck = Vp8Packet::default();

    // Empty packet
    let empty_bytes = Bytes::from_static(&[]);
    let result = pck.depacketize(&empty_bytes);
    assert!(result.is_err(), "Result should be err in case of error");

    // A descriptor with nothing after it. (Packets of two and three octets used to be refused
    // here too; they are valid, see `a_continuation_packet_of_two_or_three_octets_is_accepted`.)
    let small_bytes = Bytes::from_static(&[0x00]);
    let result = pck.depacketize(&small_bytes);
    assert!(result.is_err(), "Result should be err in case of error");

    // An extended descriptor cut short
    let small_bytes = Bytes::from_static(&[0x80, 0x80]);
    let result = pck.depacketize(&small_bytes);
    assert!(result.is_err(), "Result should be err in case of error");

    // Each descriptor below is followed by distinct VP8 bytes, so the payload's offset shows
    // how much of the packet was read as descriptor (RFC 7741 section 4.2: X, then I, L,
    // T/K in that order, each present only when its bit is set), and every optional field
    // carries a distinct value that is checked.
    struct Case {
        name: &'static str,
        raw: &'static [u8],
        payload: &'static [u8],
        flags: [u8; 5], // X I L T K
        picture_id: u16,
        picture_id_15_bits: bool,
        tl0_pic_idx: u8,
        tid: u8,
        y: u8,
        key_idx: u8,
    }
    let cases = [
        Case {
            name: "no extension",
            raw: &[0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x90],
            payload: &[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x90],
            flags: [0, 0, 0, 0, 0],
            picture_id: 0,
            picture_id_15_bits: false,
            tl0_pic_idx: 0,
            tid: 0,
            y: 0,
            key_idx: 0,
        },
        Case {
            name: "only X",
            raw: &[0x80, 0x00, 0xaa, 0xbb],
            payload: &[0xaa, 0xbb],
            flags: [1, 0, 0, 0, 0],
            picture_id: 0,
            picture_id_15_bits: false,
            tl0_pic_idx: 0,
            tid: 0,
            y: 0,
            key_idx: 0,
        },
        Case {
            name: "X and I, 7-bit PictureID",
            raw: &[0x80, 0x80, 0x45, 0xcc],
            payload: &[0xcc],
            flags: [1, 1, 0, 0, 0],
            picture_id: 0x45,
            picture_id_15_bits: false,
            tl0_pic_idx: 0,
            tid: 0,
            y: 0,
            key_idx: 0,
        },
        Case {
            name: "X and I, 15-bit PictureID",
            raw: &[0x80, 0x80, 0x81, 0x23, 0xcc],
            payload: &[0xcc],
            flags: [1, 1, 0, 0, 0],
            picture_id: 0x0123,
            picture_id_15_bits: true,
            tl0_pic_idx: 0,
            tid: 0,
            y: 0,
            key_idx: 0,
        },
        Case {
            name: "X and L",
            raw: &[0x80, 0x40, 0x37, 0xdd],
            payload: &[0xdd],
            flags: [1, 0, 1, 0, 0],
            picture_id: 0,
            picture_id_15_bits: false,
            tl0_pic_idx: 0x37,
            tid: 0,
            y: 0,
            key_idx: 0,
        },
        // TID 2, Y 1; KEYIDX bits set but K=0, so they are not read.
        Case {
            name: "X and T",
            raw: &[0x80, 0x20, 0xbf, 0xee],
            payload: &[0xee],
            flags: [1, 0, 0, 1, 0],
            picture_id: 0,
            picture_id_15_bits: false,
            tl0_pic_idx: 0,
            tid: 2,
            y: 1,
            key_idx: 0,
        },
        // KEYIDX 0x15; TID and Y bits set but T=0, so they are not read.
        Case {
            name: "X and K",
            raw: &[0x80, 0x10, 0xf5, 0xee],
            payload: &[0xee],
            flags: [1, 0, 0, 0, 1],
            picture_id: 0,
            picture_id_15_bits: false,
            tl0_pic_idx: 0,
            tid: 0,
            y: 0,
            key_idx: 0x15,
        },
        Case {
            name: "all flags, 7-bit PictureID",
            raw: &[0xff, 0xff, 0x7e, 0x12, 0x6b, 0x99, 0x98],
            payload: &[0x99, 0x98],
            flags: [1, 1, 1, 1, 1],
            picture_id: 0x7e,
            picture_id_15_bits: false,
            tl0_pic_idx: 0x12,
            tid: 1,
            y: 1,
            key_idx: 0x0b,
        },
        Case {
            name: "all flags, 15-bit PictureID",
            raw: &[0xff, 0xff, 0xff, 0xfe, 0x12, 0x6b, 0x99],
            payload: &[0x99],
            flags: [1, 1, 1, 1, 1],
            picture_id: 0x7ffe,
            picture_id_15_bits: true,
            tl0_pic_idx: 0x12,
            tid: 1,
            y: 1,
            key_idx: 0x0b,
        },
    ];
    for case in cases {
        let name = case.name;
        let payload = pck
            .depacketize(&Bytes::from_static(case.raw))
            .unwrap_or_else(|err| panic!("{name}: {err}"));
        assert_eq!(&payload[..], case.payload, "{name}: payload offset");
        assert_eq!(
            [pck.x, pck.i, pck.l, pck.t, pck.k],
            case.flags,
            "{name}: X I L T K"
        );
        assert_eq!(pck.picture_id, case.picture_id, "{name}: PictureID");
        assert_eq!(pck.picture_id_15_bits, case.picture_id_15_bits, "{name}: M");
        assert_eq!(pck.tl0_pic_idx, case.tl0_pic_idx, "{name}: TL0PICIDX");
        assert_eq!(pck.tid, case.tid, "{name}: TID");
        assert_eq!(pck.y, case.y, "{name}: Y");
        assert_eq!(pck.key_idx, case.key_idx, "{name}: KEYIDX");
    }
    // The first octet of the all-flags packets: N=1, S=1, PID=7.
    assert_eq!((pck.n, pck.s, pck.pid), (1, 1, 7));

    // A descriptor whose optional fields run past the packet is an error, not a payload.
    for truncated in [
        &[0x80, 0x80, 0x81][..], // M=1 with one octet of a 15-bit PictureID
        &[0x80, 0x40],           // L=1 without TL0PICIDX
        &[0x80, 0x10],           // K=1 without the TID/KEYIDX octet
        &[0x80, 0x10, 0x05],     // K=1 and nothing after the descriptor
    ] {
        assert!(
            pck.depacketize(&Bytes::copy_from_slice(truncated)).is_err(),
            "{truncated:02x?}"
        );
    }

    Ok(())
}

#[test]
fn test_vp8_payload() -> Result<()> {
    let tests = vec![
        (
            "WithoutPictureID",
            Vp8Payloader::default(),
            2,
            vec![
                Bytes::from_static(&[0x90, 0x90, 0x90]),
                Bytes::from_static(&[0x91, 0x91]),
            ],
            vec![
                vec![
                    Bytes::from_static(&[0x10, 0x90]),
                    Bytes::from_static(&[0x00, 0x90]),
                    Bytes::from_static(&[0x00, 0x90]),
                ],
                vec![
                    Bytes::from_static(&[0x10, 0x91]),
                    Bytes::from_static(&[0x00, 0x91]),
                ],
            ],
        ),
        (
            "WithPictureID_1byte",
            Vp8Payloader {
                enable_picture_id: true,
                picture_id: 0x20,
                ..Default::default()
            },
            5,
            vec![
                Bytes::from_static(&[0x90, 0x90, 0x90]),
                Bytes::from_static(&[0x91, 0x91]),
            ],
            vec![
                vec![
                    Bytes::from_static(&[0x90, 0x80, 0x20, 0x90, 0x90]),
                    Bytes::from_static(&[0x80, 0x80, 0x20, 0x90]),
                ],
                vec![Bytes::from_static(&[0x90, 0x80, 0x21, 0x91, 0x91])],
            ],
        ),
        (
            "WithPictureID_2bytes",
            Vp8Payloader {
                enable_picture_id: true,
                picture_id: 0x120,
                ..Default::default()
            },
            6,
            vec![
                Bytes::from_static(&[0x90, 0x90, 0x90]),
                Bytes::from_static(&[0x91, 0x91]),
            ],
            vec![
                vec![
                    Bytes::from_static(&[0x90, 0x80, 0x81, 0x20, 0x90, 0x90]),
                    Bytes::from_static(&[0x80, 0x80, 0x81, 0x20, 0x90]),
                ],
                vec![Bytes::from_static(&[0x90, 0x80, 0x81, 0x21, 0x91, 0x91])],
            ],
        ),
    ];

    for (name, mut pck, mtu, payloads, expected) in tests {
        for (i, payload) in payloads.iter().enumerate() {
            let actual = pck.payload(mtu, payload)?;
            assert_eq!(expected[i], actual, "{name}: Generated packet[{i}] differs");
        }
    }

    Ok(())
}

#[test]
fn test_vp8_payload_error() -> Result<()> {
    let mut pck = Vp8Payloader::default();
    let empty = Bytes::from_static(&[]);
    let payload = Bytes::from_static(&[0x90, 0x90, 0x90]);

    // Positive MTU, empty payload
    let result = pck.payload(1, &empty)?;
    assert!(result.is_empty(), "Generated payload should be empty");

    // Positive MTU, small payload
    let result = pck.payload(1, &payload)?;
    assert_eq!(result.len(), 0, "Generated payload should be empty");

    // Positive MTU, small payload
    let result = pck.payload(2, &payload)?;
    assert_eq!(
        result.len(),
        payload.len(),
        "Generated payload should be the same size as original payload size"
    );

    Ok(())
}

#[test]
fn test_vp8_partition_head_checker_is_partition_head() -> Result<()> {
    let vp8 = Vp8Packet::default();

    //"SmallPacket"
    assert!(
        !vp8.is_partition_head(&Bytes::from_static(&[0x00])),
        "Small packet should not be the head of a new partition"
    );

    //"SFlagON",
    assert!(
        vp8.is_partition_head(&Bytes::from_static(&[0x10, 0x00, 0x00, 0x00])),
        "Packet with S flag should be the head of a new partition"
    );

    //"SFlagOFF"
    assert!(
        !vp8.is_partition_head(&Bytes::from_static(&[0x00, 0x00, 0x00, 0x00])),
        "Packet without S flag should not be the head of a new partition"
    );

    Ok(())
}