[domistream] Audio packetizer: RTP datagram per Opus packet, AES-128-CBC (AudioCipher::encrypt), no parity until U16; first sequence must start a block

This commit is contained in:
2026-12-04 12:24:45 +01:00
parent b62443ef28
commit 0d3a9a7917
4 changed files with 102 additions and 34 deletions
+14 -8
View File
@@ -99,19 +99,19 @@ fn send(tx: &mut AudioPacketizer, payloads: &[Vec<u8>], duration: u8) -> (Vec<Ve
#[test]
fn fixed_vector_plaintext() {
let mut tx = sender(false, 0x0102, 0x0A0B_0C0D, 5);
let mut tx = sender(false, 0x0104, 0x0A0B_0C0D, 5);
let out = tx.packetize(&[0xFC, 0xFF, 0xFE]).unwrap();
#[rustfmt::skip]
let want = vec![
0x80, // RTP byte 0: version 2, no extension (6.2)
0x61, // payload type 97
0x01, 0x02, // sequence 0x0102, BE
0x01, 0x04, // sequence 0x0104, BE
0x0A, 0x0B, 0x0C, 0x0D, // timestamp (milliseconds), BE
0x00, 0x00, 0x00, 0x00, // SSRC 0
0xFC, 0xFF, 0xFE, // the Opus packet
];
assert_eq!(out, vec![want]);
assert_eq!((tx.next_sequence(), tx.next_timestamp_ms()), (0x0103, 0x0A0B_0C12));
assert_eq!((tx.next_sequence(), tx.next_timestamp_ms()), (0x0105, 0x0A0B_0C12));
}
#[test]
@@ -168,14 +168,14 @@ fn round_trip_plaintext() {
#[test]
fn sequence_wraps_from_ffff_to_0() {
let mut tx = sender(true, 0xFFFE, 100, 10);
let mut tx = sender(true, 0xFFFC, 100, 10);
let mut seen = Vec::new();
for _ in 0..4 {
for _ in 0..6 {
let d = tx.packetize(&[1, 2, 3]).unwrap().remove(0);
let Ok(AudioPacket::Data { rtp, .. }) = parse_audio_packet(&d) else { panic!("data packet") };
seen.push((rtp.sequence, rtp.timestamp));
}
assert_eq!(seen, vec![(0xFFFE, 100), (0xFFFF, 110), (0x0000, 120), (0x0001, 130)]);
assert_eq!(seen, vec![(0xFFFC, 100), (0xFFFD, 110), (0xFFFE, 120), (0xFFFF, 130), (0x0000, 140), (0x0001, 150)]);
}
#[test]
@@ -205,7 +205,7 @@ fn size_caps() {
assert_eq!(MAX_OPUS_PLAIN_BYTES, MAX_AUDIO_PACKET_BYTES - 12);
assert_eq!(MAX_OPUS_ENCRYPTED_BYTES, 2031, "2031 bytes pad to 2032; 12 + 2032 = 2044 <= 2048");
for (encrypt, max) in [(false, MAX_OPUS_PLAIN_BYTES), (true, MAX_OPUS_ENCRYPTED_BYTES)] {
let mut tx = sender(encrypt, 5, 50, 5);
let mut tx = sender(encrypt, 4, 50, 5);
assert_eq!(tx.max_opus_len(), max);
let state = (tx.next_sequence(), tx.next_timestamp_ms());
assert_err!(tx.packetize(&[]), Error::Truncated { .. }, "an Opus packet has at least its TOC byte");
@@ -228,6 +228,12 @@ fn size_caps() {
#[test]
fn config_refusals() {
assert_err!(AudioPacketizer::new(cfg(true, 0, 0, 0)), Error::Malformed { .. }, "zero packet duration");
// Blocks start at multiples of 4 (6.4), and the receiver counts from the first block's start: a stream that
// began at 5 would make it report 4 as lost.
for first in [1u16, 2, 3, 5, 0xFFFF] {
assert_err!(AudioPacketizer::new(cfg(true, first, 0, 5)), Error::Malformed { .. }, "first sequence {first}");
}
assert!(AudioPacketizer::new(cfg(true, 0xFFFC, 0, 5)).is_ok());
assert_eq!(AudioParity::default(), AudioParity::None);
}
@@ -246,7 +252,7 @@ proptest! {
encrypt in any::<bool>(),
) {
let mut rng = SeqRng::new(seed);
let mut tx = sender(encrypt, first_sequence, first_ts, duration);
let mut tx = sender(encrypt, first_sequence & !3, first_ts, duration);
let max = tx.max_opus_len();
let payloads: Vec<Vec<u8>> = (0..count).map(|_| opus(&mut rng, max)).collect();
let (datagrams, want) = send(&mut tx, &payloads, duration);