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
//! RFC 7941 sections 4.2.3, 4.2.4.1 and 4.2.4.3: the SDES header extensions go out until the
//! receiver's report shows they arrived, again on a new SSRC and after a change.

use super::*;

const IDS: SdesExtensionIds = SdesExtensionIds {
    mid: 1,
    rid: 2,
    rrid: 3,
};
/// Another extension (a transport-wide sequence number, say), which the policy leaves alone.
const OTHER: u8 = 5;

fn header(ssrc: u32, sequence_number: u16, mid: &str, rid: Option<&str>) -> rtp::Header {
    let mut header = rtp::Header {
        version: 2,
        ssrc,
        sequence_number,
        ..Default::default()
    };
    header
        .set_extension(IDS.mid, Bytes::copy_from_slice(mid.as_bytes()))
        .unwrap();
    if let Some(rid) = rid {
        header
            .set_extension(IDS.rid, Bytes::copy_from_slice(rid.as_bytes()))
            .unwrap();
    }
    header
        .set_extension(OTHER, Bytes::from_static(&[0xaa, 0xbb]))
        .unwrap();
    header
}

/// Sends `header` through the policy; returns what went on the wire: (MID, RID, other).
fn send(
    policy: &mut SdesSendPolicy,
    mut header: rtp::Header,
) -> (Option<Bytes>, Option<Bytes>, Option<Bytes>) {
    // Precondition: what is sent carries the elements before the policy sees it.
    assert!(header.get_extension(IDS.mid).is_some());
    policy.on_send(&mut header, IDS);
    (
        header.get_extension(IDS.mid),
        header.get_extension(IDS.rid),
        header.get_extension(OTHER),
    )
}

fn has_mid(sent: &(Option<Bytes>, Option<Bytes>, Option<Bytes>)) -> bool {
    sent.0.is_some()
}

/// Section 4.2.3: "use feedback, such as RTCP, to determine when the information in the
/// header extensions has reached all receivers and to stop further repetitions". Every packet
/// of a new SSRC carries the MID and the RID, however many go out, until a report block for
/// that SSRC covers the first of them; then neither goes out, and the other extensions stay.
#[test]
fn the_elements_go_out_until_a_report_covers_them() {
    let mut policy = SdesSendPolicy::default();
    for sequence_number in 100..200 {
        let sent = send(&mut policy, header(0x1111, sequence_number, "0", Some("h")));
        assert!(has_mid(&sent), "packet {sequence_number}: no report yet");
        assert_eq!(sent.1.as_deref(), Some(&b"h"[..]));
    }

    policy.on_report_block(0x1111, 150);
    let sent = send(&mut policy, header(0x1111, 200, "0", Some("h")));
    assert_eq!(None, sent.0, "the MID stops once acknowledged");
    assert_eq!(None, sent.1, "and the RID");
    assert_eq!(
        sent.2.as_deref(),
        Some(&[0xaa, 0xbb][..]),
        "other extensions stay"
    );
    // And it stays stopped.
    assert!(!has_mid(&send(
        &mut policy,
        header(0x1111, 201, "0", Some("h"))
    )));
}

/// A report about another SSRC, or about packets before the first one sent with the elements
/// (a receiver that reports on what it had before), acknowledges nothing.
#[test]
fn a_report_that_does_not_cover_the_first_packet_acknowledges_nothing() {
    let mut policy = SdesSendPolicy::default();
    send(&mut policy, header(0x1111, 1000, "0", None));
    send(&mut policy, header(0x1111, 1001, "0", None));

    policy.on_report_block(0x2222, 5000);
    assert!(has_mid(&send(&mut policy, header(0x1111, 1002, "0", None))));
    policy.on_report_block(0x1111, 999);
    assert!(has_mid(&send(&mut policy, header(0x1111, 1003, "0", None))));

    policy.on_report_block(0x1111, 1000);
    assert!(!has_mid(&send(
        &mut policy,
        header(0x1111, 1004, "0", None)
    )));
}

/// Section 4.2.4.1: a new SSRC is "completely new for everyone". One stream's
/// acknowledgement does not stop the elements on another: a repair stream, or the SSRC a
/// stream moved to, carries them until its own SSRC is reported on.
#[test]
fn a_new_ssrc_starts_over() {
    let mut policy = SdesSendPolicy::default();
    send(&mut policy, header(0x1111, 10, "0", Some("h")));
    policy.on_report_block(0x1111, 10);
    assert!(!has_mid(&send(
        &mut policy,
        header(0x1111, 11, "0", Some("h"))
    )));

    // A second SSRC (the repair stream, or a new one) carries them again.
    assert!(has_mid(&send(
        &mut policy,
        header(0x3333, 500, "0", Some("h"))
    )));
    assert!(has_mid(&send(
        &mut policy,
        header(0x3333, 501, "0", Some("h"))
    )));
    policy.on_report_block(0x3333, 500);
    assert!(!has_mid(&send(
        &mut policy,
        header(0x3333, 502, "0", Some("h"))
    )));
}

/// Section 4.2.4.3: "if the SDES information needs to be updated ... header extension
/// transmission strategies with high probability of delivery should be chosen". A changed
/// value goes out again, and a report on packets sent before the change does not stop it.
#[test]
fn a_changed_value_goes_out_until_a_report_covers_it() {
    let mut policy = SdesSendPolicy::default();
    send(&mut policy, header(0x1111, 10, "0", None));
    policy.on_report_block(0x1111, 10);
    assert!(!has_mid(&send(&mut policy, header(0x1111, 11, "0", None))));

    // The MID changes on packet 12.
    let sent = send(&mut policy, header(0x1111, 12, "1", None));
    assert_eq!(sent.0.as_deref(), Some(&b"1"[..]), "the new MID goes out");
    policy.on_report_block(0x1111, 11);
    assert!(
        has_mid(&send(&mut policy, header(0x1111, 13, "1", None))),
        "a report on packets before the change does not acknowledge it"
    );
    policy.on_report_block(0x1111, 13);
    assert!(!has_mid(&send(&mut policy, header(0x1111, 14, "1", None))));
}

/// The sequence numbers wrap (RFC 3550 appendix A.1): a report whose extended highest
/// sequence number has counted a cycle covers a first packet sent just before the wrap, and a
/// report from before the wrap does not cover one sent after it.
#[test]
fn reports_are_compared_across_the_sequence_number_wrap() {
    let mut policy = SdesSendPolicy::default();
    send(&mut policy, header(0x1111, 65_530, "0", None));
    for sequence_number in [65_531, 65_535, 0, 1, 2] {
        send(&mut policy, header(0x1111, sequence_number, "0", None));
    }
    policy.on_report_block(0x1111, (1 << 16) | 2);
    assert!(!has_mid(&send(&mut policy, header(0x1111, 3, "0", None))));

    let mut policy = SdesSendPolicy::default();
    send(&mut policy, header(0x2222, 65_534, "0", None));
    send(&mut policy, header(0x2222, 65_535, "1", None));
    send(&mut policy, header(0x2222, 0, "1", None));
    send(&mut policy, header(0x2222, 2, "2", None));
    // The receiver's highest is 0 (one cycle): before the change at 2.
    policy.on_report_block(0x2222, 1 << 16);
    assert!(has_mid(&send(&mut policy, header(0x2222, 3, "2", None))));
}

/// `with_sdes_header_extensions_on_every_packet(true)`: nothing is taken out.
#[test]
fn on_every_packet_nothing_is_taken_out() {
    let mut policy = SdesSendPolicy::new(true);
    send(&mut policy, header(0x1111, 10, "0", Some("h")));
    policy.on_report_block(0x1111, 10);
    let sent = send(&mut policy, header(0x1111, 11, "0", Some("h")));
    assert!(has_mid(&sent));
    assert!(sent.1.is_some());
}

/// A packet that carries no SDES element is left as it is, and does not reset the stream.
#[test]
fn a_packet_without_the_elements_is_left_alone() {
    let mut policy = SdesSendPolicy::default();
    send(&mut policy, header(0x1111, 10, "0", None));
    policy.on_report_block(0x1111, 10);

    let mut bare = rtp::Header {
        version: 2,
        ssrc: 0x1111,
        sequence_number: 11,
        ..Default::default()
    };
    bare.set_extension(OTHER, Bytes::from_static(&[0xaa]))
        .unwrap();
    let before = bare.clone();
    assert!(!policy.on_send(&mut bare, IDS));
    assert_eq!(before, bare);
    assert!(!has_mid(&send(&mut policy, header(0x1111, 12, "0", None))));
}

/// A packet whose only extensions were the SDES elements goes without an extension block.
#[test]
fn a_packet_left_with_no_element_has_no_extension_block() {
    let mut policy = SdesSendPolicy::default();
    let only_mid = |sequence_number| {
        let mut header = rtp::Header {
            version: 2,
            ssrc: 0x1111,
            sequence_number,
            ..Default::default()
        };
        header
            .set_extension(IDS.mid, Bytes::from_static(b"0"))
            .unwrap();
        header
    };
    let mut first = only_mid(10);
    policy.on_send(&mut first, IDS);
    assert!(first.extension);
    policy.on_report_block(0x1111, 10);
    let mut next = only_mid(11);
    assert!(policy.on_send(&mut next, IDS));
    assert!(!next.extension);
    assert!(next.extensions.is_empty());
}

/// The report blocks of SRs and RRs both count.
#[test]
fn sender_and_receiver_report_blocks_both_acknowledge() {
    use rtcp::reception_report::ReceptionReport;
    let block = |ssrc, last_sequence_number| ReceptionReport {
        ssrc,
        last_sequence_number,
        ..Default::default()
    };
    let mut policy = SdesSendPolicy::default();
    send(&mut policy, header(0x1111, 10, "0", None));
    send(&mut policy, header(0x2222, 20, "0", None));
    let packets: Vec<Box<dyn rtcp::Packet>> = vec![
        Box::new(rtcp::sender_report::SenderReport {
            ssrc: 9,
            reports: vec![block(0x1111, 10)],
            ..Default::default()
        }),
        Box::new(rtcp::receiver_report::ReceiverReport {
            ssrc: 9,
            reports: vec![block(0x2222, 20)],
            ..Default::default()
        }),
    ];
    policy.on_rtcp(&packets);
    assert!(!has_mid(&send(&mut policy, header(0x1111, 11, "0", None))));
    assert!(!has_mid(&send(&mut policy, header(0x2222, 21, "0", None))));
}