rtci 0.5.0

A sans-I/O WebRTC implementation: peer connections with ICE, DTLS-SRTP, SCTP data channels, RTP/RTCP and JSEP negotiation
Documentation
use super::*;

#[test]
fn test_h264_fmtp_parse() {
    let tests: Vec<(&str, &str, Box<dyn Fmtp>)> = vec![
        (
            "OneParam",
            "key-name=value",
            Box::new(H264Fmtp {
                parameters: [("key-name".to_owned(), "value".to_owned())]
                    .iter()
                    .cloned()
                    .collect(),
            }),
        ),
        (
            "OneParamWithWhiteSpeces",
            "\tkey-name=value ",
            Box::new(H264Fmtp {
                parameters: [("key-name".to_owned(), "value".to_owned())]
                    .iter()
                    .cloned()
                    .collect(),
            }),
        ),
        (
            "TwoParams",
            "key-name=value;key2=value2",
            Box::new(H264Fmtp {
                parameters: [
                    ("key-name".to_owned(), "value".to_owned()),
                    ("key2".to_owned(), "value2".to_owned()),
                ]
                .iter()
                .cloned()
                .collect(),
            }),
        ),
        (
            "TwoParamsWithWhiteSpeces",
            "key-name=value;  \n\tkey2=value2 ",
            Box::new(H264Fmtp {
                parameters: [
                    ("key-name".to_owned(), "value".to_owned()),
                    ("key2".to_owned(), "value2".to_owned()),
                ]
                .iter()
                .cloned()
                .collect(),
            }),
        ),
    ];

    for (name, input, expected) in tests {
        let f = parse("video/h264", input);
        assert_eq!(&f, &expected, "{name} failed");

        assert_eq!(f.mime_type(), "video/h264");
    }
}

#[test]
fn test_h264_fmtp_compare() {
    let consist_string: HashMap<bool, String> = [
        (true, "consist".to_owned()),
        (false, "inconsist".to_owned()),
    ]
    .iter()
    .cloned()
    .collect();

    let tests = vec![
        (
            "Equal",
            "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f",
            "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f",
            true,
        ),
        (
            "EqualWithWhitespaceVariants",
            "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f",
            "  level-asymmetry-allowed=1;  \npacketization-mode=1;\t\nprofile-level-id=42e01f",
            true,
        ),
        (
            "EqualWithCase",
            "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f",
            "level-asymmetry-allowed=1;packetization-mode=1;PROFILE-LEVEL-ID=42e01f",
            true,
        ),
        (
            "OneHasExtraParam",
            "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f",
            "packetization-mode=1;profile-level-id=42e01f",
            true,
        ),
        (
            "DifferentProfileLevelIDVersions",
            "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f",
            "packetization-mode=1;profile-level-id=42e029",
            true,
        ),
        (
            "Inconsistent",
            "packetization-mode=1;profile-level-id=42e029",
            "packetization-mode=0;profile-level-id=42e029",
            false,
        ),
        (
            "Inconsistent_MissingPacketizationMode",
            "packetization-mode=1;profile-level-id=42e029",
            "profile-level-id=42e029",
            false,
        ),
        (
            "Inconsistent_MissingProfileLevelID",
            "packetization-mode=1;profile-level-id=42e029",
            "packetization-mode=1",
            false,
        ),
        (
            "Inconsistent_InvalidProfileLevelID",
            "packetization-mode=1;profile-level-id=42e029",
            "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=41e029",
            false,
        ),
    ];

    for (name, a, b, consist) in tests {
        let check = |a, b| {
            let aa = parse("video/h264", a);
            let bb = parse("video/h264", b);

            // test forward case here
            let c = aa.match_fmtp(&*bb);
            assert_eq!(
                c,
                consist,
                "{}: '{}' and '{}' are expected to be {:?}, but treated as {:?}",
                name,
                a,
                b,
                consist_string.get(&consist),
                consist_string.get(&c),
            );

            // test reverse case here
            let c = bb.match_fmtp(&*aa);
            assert_eq!(
                c,
                consist,
                "{}: '{}' and '{}' are expected to be {:?}, but treated as {:?}",
                name,
                a,
                b,
                consist_string.get(&consist),
                consist_string.get(&c),
            );
        };

        check(a, b);
    }
}

/// RFC 6184 section 8.1: an absent `packetization-mode` is 0 (single NAL unit mode), and an
/// absent `profile-level-id` is the Baseline profile "without additional constraints at
/// Level 1" (`42000a`). A format that leaves them out is that format.
#[test]
fn absent_h264_parameters_take_their_rfc_6184_defaults() {
    let h264 = |line: &str| parse("video/h264", line);
    assert!(h264("").match_fmtp(&*h264("packetization-mode=0;profile-level-id=42000a")));
    assert!(
        h264("profile-level-id=42001f")
            .match_fmtp(&*h264("packetization-mode=0;profile-level-id=42001f"))
    );
    assert!(
        h264("packetization-mode=1")
            .match_fmtp(&*h264("packetization-mode=1;profile-level-id=420029"))
    );
    assert!(!h264("").match_fmtp(&*h264("packetization-mode=1")));
    assert!(
        !h264("").match_fmtp(&*h264("profile-level-id=42e01f")),
        "Constrained Baseline is not Baseline"
    );
}

/// RFC 6184 section 8.1, Table 5: the `profile_idc` / `profile-iop` combinations that stand for
/// one profile are that profile, whatever the level (section 8.2.2).
#[test]
fn h264_profile_level_ids_of_one_table_5_profile_match() {
    let plid = |id: &str| {
        parse(
            "video/h264",
            &format!("packetization-mode=1;profile-level-id={id}"),
        )
    };
    for (a, b, same) in [
        // Constrained Baseline as Baseline, Main and Extended.
        ("42e01f", "4d801f", true),
        ("42e01f", "58c01f", true),
        ("424015", "4dc028", true),
        // Baseline as Baseline and Extended; not Constrained Baseline.
        ("42001f", "588029", true),
        ("42001f", "42e01f", false),
        // Main is not Constrained Baseline, nor High.
        ("4d001f", "4d801f", false),
        ("4d001f", "64001f", false),
        // High at two levels; High is not High 10 Intra.
        ("640032", "64001f", true),
        ("6e0032", "6e1032", false),
        // Combinations Table 5 does not list compare as their two bytes.
        ("640c1f", "640c34", true),
        ("640c1f", "64001f", false),
    ] {
        assert_eq!(same, plid(a).match_fmtp(&*plid(b)), "{a} and {b}");
    }
}

/// RFC 6184 section 8.2.2: "A receiver SHOULD understand all media type parameters". Every
/// parameter section 8.1 registers has its Table 6 row, and the rows give each its column.
#[test]
fn every_rfc_6184_parameter_has_its_table_6_role() {
    let section_8_1 = [
        "profile-level-id",
        "max-recv-level",
        "max-mbps",
        "max-smbps",
        "max-fs",
        "max-cpb",
        "max-dpb",
        "max-br",
        "redundant-pic-cap",
        "sprop-parameter-sets",
        "sprop-level-parameter-sets",
        "use-level-src-parameter-sets",
        "in-band-parameter-sets",
        "level-asymmetry-allowed",
        "packetization-mode",
        "sprop-interleaving-depth",
        "sprop-deint-buf-req",
        "deint-buf-cap",
        "sprop-init-buf-time",
        "sprop-max-don-diff",
        "max-rcmd-nalu-size",
        "sar-understood",
        "sar-supported",
    ];
    assert_eq!(PARAMETERS.len(), section_8_1.len());
    for name in section_8_1 {
        assert!(
            PARAMETERS.iter().any(|r| r.name == name),
            "{name} has no Table 6 row"
        );
    }
    let role = |name: &str| PARAMETERS.iter().find(|r| r.name == name).unwrap();
    assert_eq!(role("profile-level-id").role, ParameterRole::Configuration);
    assert!(role("profile-level-id").recvonly && role("profile-level-id").sendonly);
    assert_eq!(role("max-fs").role, ParameterRole::ReceiverCapability);
    assert!(role("max-fs").recvonly && !role("max-fs").sendonly);
    assert_eq!(
        role("sprop-init-buf-time").role,
        ParameterRole::StreamProperty
    );
    assert!(!role("sprop-init-buf-time").recvonly && role("sprop-init-buf-time").sendonly);
    assert_eq!(
        role("sprop-parameter-sets").role,
        ParameterRole::ParameterSets
    );
    assert_eq!(
        role("level-asymmetry-allowed").role,
        ParameterRole::OfferAnswer
    );
}

/// What a description of each direction keeps of an H.264 line: no receiver capability in
/// sendonly, no stream property or parameter sets in recvonly, `level-asymmetry-allowed`
/// everywhere, a parameter outside the media type and every parameter of sendrecv / inactive
/// as given (the line itself when nothing is left out).
#[test]
fn an_h264_line_keeps_what_the_direction_uses() {
    let line = "level-asymmetry-allowed=1;packetization-mode=2;profile-level-id=42e01f;\
                max-fs=8160;sprop-interleaving-depth=4;x-private=1";
    let h264 = |sends, receives| written_parameters("video/H264", line, sends, receives, true);
    assert_eq!(h264(true, true), line);
    assert_eq!(h264(false, false), line);
    assert_eq!(
        h264(true, false),
        "level-asymmetry-allowed=1;packetization-mode=2;profile-level-id=42e01f;\
         sprop-interleaving-depth=4;x-private=1"
    );
    assert_eq!(
        h264(false, true),
        "level-asymmetry-allowed=1;packetization-mode=2;profile-level-id=42e01f;max-fs=8160;\
         x-private=1"
    );
    // Parameter names compare without regard to case.
    assert_eq!(
        written_parameters(
            "video/h264",
            "profile-level-id=42e01f;MAX-FS=1",
            true,
            false,
            false
        ),
        "profile-level-id=42e01f"
    );
    // Another media type is not touched.
    assert_eq!(
        written_parameters("video/VP8", "max-fs=1", true, false, true),
        "max-fs=1"
    );
}

/// RFC 6184 section 8.1: Level 1b is "a level higher than Level 1.0 but lower than Level
/// 1.1", signalled for Baseline, Main and Extended by level_idc 11 with constraint_set3_flag
/// (bit 4 of profile-iop) and for other profiles by level_idc 9; `max-recv-level` the same
/// way. Levels order with 1b in its place, and a level is written back as the profile signals
/// it (the flag set for 1b and cleared for 1.1, level_idc 9 for High).
#[test]
fn level_1b_is_read_and_written_as_each_profile_signals_it() {
    let level = |id: &str| H264Level::of_profile_level_id(id).unwrap();
    assert_eq!(level("42f00b"), H264Level::LEVEL_1B);
    assert_eq!(level("4d100b"), H264Level::LEVEL_1B);
    assert_eq!(level("640009"), H264Level::LEVEL_1B);
    assert_ne!(level("42e00b"), H264Level::LEVEL_1B, "1.1: no flag");
    assert_ne!(
        level("64100b"),
        H264Level::LEVEL_1B,
        "High: the flag is not 1b"
    );
    assert!(level("42e00a") < level("42f00b"));
    assert!(level("42f00b") < level("42e00b"));
    assert!(
        level("64000a") < level("640009"),
        "1.0 is below 1b, whose level_idc is 9"
    );
    assert!(level("640009") < level("64000b"));
    assert_eq!(level("42e01f"), level("64001f"));

    let recv = |v: &str| H264Level::of_max_recv_level(v).unwrap();
    assert_eq!(recv("100b"), H264Level::LEVEL_1B);
    assert_eq!(recv("0009"), H264Level::LEVEL_1B);
    assert_eq!(recv("e028"), level("42e028"));

    let write = |l: H264Level, id: &str| l.in_profile_level_id(id).unwrap();
    assert_eq!(write(H264Level::LEVEL_1B, "42e01f"), "42f00b");
    assert_eq!(write(level("42e00b"), "42f00b"), "42e00b");
    assert_eq!(write(H264Level::LEVEL_1B, "640028"), "640009");
    assert_eq!(write(level("42e01f"), "42E028"), "42E01f");
}