rtci-rtcp 0.2.1

RTCP packet parsing and serialization (RFC 3550, RFC 4585, RFC 5104, RFC 3611, RFC 8888) for WebRTC
Documentation
use super::*;
use bytes::Bytes;

#[test]
fn test_slice_loss_indication_unmarshal() {
    let tests = vec![
        (
            "valid",
            Bytes::from_static(&[
                0x82u8, 0xce, 0x0, 0x3, // SliceLossIndication (PSFB, FMT=2)
                0x90, 0x2f, 0x9e, 0x2e, // sender=0x902f9e2e
                0x90, 0x2f, 0x9e, 0x2e, // media=0x902f9e2e
                0x55, 0x50, 0x00, 0x2C, // nack 0xAAAA, 0x5555
            ]),
            SliceLossIndication {
                sender_ssrc: 0x902f9e2e,
                media_ssrc: 0x902f9e2e,
                sli_entries: vec![SliEntry {
                    first: 0xaaa,
                    number: 0,
                    picture: 0x2C,
                }],
            },
            None,
        ),
        (
            "short report",
            Bytes::from_static(&[
                0x82, 0xce, 0x0, 0x2, // ssrc=0x902f9e2e
                0x90, 0x2f, 0x9e, 0x2e,
                // report ends early
            ]),
            SliceLossIndication::default(),
            Some(Error::PacketTooShort),
        ),
        (
            "wrong type",
            Bytes::from_static(&[
                // v=2, p=0, count=1, SR, len=7
                0x81, 0xc8, 0x0, 0x7, // ssrc=0x902f9e2e
                0x90, 0x2f, 0x9e, 0x2e, // ssrc=0xbc5e9a40
                0xbc, 0x5e, 0x9a, 0x40, // fracLost=0, totalLost=0
                0x0, 0x0, 0x0, 0x0, // lastSeq=0x46e1
                0x0, 0x0, 0x46, 0xe1, // jitter=273
                0x0, 0x0, 0x1, 0x11, // lsr=0x9f36432
                0x9, 0xf3, 0x64, 0x32, // delay=150137
                0x0, 0x2, 0x4a, 0x79,
            ]),
            SliceLossIndication::default(),
            Some(Error::WrongType),
        ),
        (
            "nil",
            Bytes::from_static(&[]),
            SliceLossIndication::default(),
            Some(Error::PacketTooShort),
        ),
    ];

    for (name, mut data, want, want_error) in tests {
        let got = SliceLossIndication::unmarshal(&mut data);

        assert_eq!(
            got.is_err(),
            want_error.is_some(),
            "Unmarshal {name} rr: err = {got:?}, want {want_error:?}"
        );

        if let Some(err) = want_error {
            let got_err = got.err().unwrap();
            assert_eq!(
                err, got_err,
                "Unmarshal {name} rr: err = {got_err:?}, want {err:?}",
            );
        } else {
            let actual = got.unwrap();
            assert_eq!(
                actual, want,
                "Unmarshal {name} rr: got {actual:?}, want {want:?}"
            );
        }
    }
}

#[test]
fn test_slice_loss_indication_roundtrip() {
    let tests: Vec<(&str, SliceLossIndication, Option<Error>)> = vec![(
        "valid",
        SliceLossIndication {
            sender_ssrc: 0x902f9e2e,
            media_ssrc: 0x902f9e2e,
            sli_entries: vec![
                SliEntry {
                    first: 1,
                    number: 0xAA,
                    picture: 0x1F,
                },
                SliEntry {
                    first: 1034,
                    number: 0x05,
                    picture: 0x6,
                },
            ],
        },
        None,
    )];

    for (name, want, want_error) in tests {
        let got = want.marshal();

        assert_eq!(
            got.is_ok(),
            want_error.is_none(),
            "Marshal {name}: err = {got:?}, want {want_error:?}"
        );

        if let Some(err) = want_error {
            let got_err = got.err().unwrap();
            assert_eq!(
                err, got_err,
                "Unmarshal {name} rr: err = {got_err:?}, want {err:?}",
            );
        } else {
            let mut data = got.ok().unwrap();
            let actual = SliceLossIndication::unmarshal(&mut data)
                .unwrap_or_else(|_| panic!("Unmarshal {name}"));

            assert_eq!(
                actual, want,
                "{name} round trip: got {actual:?}, want {want:?}"
            )
        }
    }
}

/// RFC 4585 ยง6.3.2: SLI is payload-specific feedback. The compound-packet parser dispatches it
/// from PT=206, but `SliceLossIndication` checked for 205 and marshalled 205, so a received SLI
/// never parsed and a sent one was not an SLI.
#[test]
fn test_slice_loss_indication_round_trips_through_the_packet_parser() {
    let sli = SliceLossIndication {
        sender_ssrc: 0x902f9e2e,
        media_ssrc: 0x902f9e2e,
        sli_entries: vec![SliEntry {
            first: 0xaaa,
            number: 0,
            picture: 0x2c,
        }],
    };
    let raw = sli.marshal().unwrap();
    assert_eq!(raw[1], 206, "SLI must be sent as payload-specific feedback");
    let packets = crate::packet::unmarshal(&mut raw.clone()).unwrap();
    assert_eq!(packets.len(), 1);
    assert_eq!(
        packets[0].as_any().downcast_ref::<SliceLossIndication>(),
        Some(&sli)
    );
}

/// RFC 7741 section 5.2: a VP8 receiver "MAY set the First parameter to 0, and the Number
/// parameter to the total number of macroblocks per frame"; PictureID is "the six least
/// significant bits" of the VP8 PictureID. A 640x480 frame is 1200 macroblocks; PictureID
/// 0x12F4 is 0x34 in six bits; it reads back as such.
#[test]
fn a_vp8_whole_frame_sli_names_every_macroblock_and_six_bits_of_the_picture_id() {
    let sli = SliceLossIndication::vp8_frame(1, 2, 0x12F4, 640, 480);
    assert_eq!(
        vec![SliEntry {
            first: 0,
            number: 1200,
            picture: 0x34,
        }],
        sli.sli_entries
    );
    let raw = sli.marshal().unwrap();
    let read = SliceLossIndication::unmarshal(&mut raw.freeze()).unwrap();
    assert_eq!(sli, read);
}