use super::*;
#[test]
fn test_opus_unmarshal() -> Result<()> {
let mut pck = OpusPacket;
let empty_bytes = Bytes::from_static(&[]);
let result = pck.depacketize(&empty_bytes);
assert!(result.is_err(), "Result should be err in case of error");
let raw_bytes = Bytes::from_static(&[0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x90]);
let payload = pck.depacketize(&raw_bytes)?;
assert_eq!(&raw_bytes, &payload, "Payload must be same");
Ok(())
}
#[test]
fn test_opus_payload() -> Result<()> {
let mut pck = OpusPayloader;
let empty = Bytes::from_static(&[]);
let payload = Bytes::from_static(&[0x90, 0x90, 0x90]);
let result = pck.payload(1, &empty)?;
assert!(result.is_empty(), "Generated payload should be empty");
let result = pck.payload(1, &payload)?;
assert_eq!(result.len(), 1, "Generated payload should be the 1");
let result = pck.payload(2, &payload)?;
assert_eq!(result.len(), 1, "Generated payload should be the 1");
Ok(())
}
#[test]
fn test_opus_is_partition_head() -> Result<()> {
let opus = OpusPacket;
assert!(
opus.is_partition_head(&Bytes::from_static(&[0x00, 0x00])),
"All OPUS RTP packet should be the head of a new partition"
);
Ok(())
}
#[test]
fn the_payloader_never_fragments_or_aggregates_whatever_the_mtu() -> Result<()> {
let mut payloader = OpusPayloader;
let packet = Bytes::from(vec![0x78u8; 1500]);
assert_eq!(vec![packet.clone()], payloader.payload(100, &packet)?);
Ok(())
}
#[test]
fn the_packet_duration_is_read_from_the_toc() {
assert_eq!(Some(480), opus_packet_samples(&[0x00, 1]));
assert_eq!(Some(2880), opus_packet_samples(&[3 << 3]));
assert_eq!(Some(960), opus_packet_samples(&[13 << 3]));
assert_eq!(
Some(OPUS_DTX_TIMESTAMP_STEP),
opus_packet_samples(&[16 << 3])
);
assert_eq!(Some(1920), opus_packet_samples(&[(31 << 3) | 1]));
assert_eq!(Some(1920), opus_packet_samples(&[(31 << 3) | 2]));
assert_eq!(Some(2880), opus_packet_samples(&[(31 << 3) | 3, 0xC3]));
assert_eq!(
Some(OPUS_MAX_PACKET_SAMPLES),
opus_packet_samples(&[(31 << 3) | 3, 6])
);
assert_eq!(None, opus_packet_samples(&[(31 << 3) | 3, 7]));
assert_eq!(Some(5760), opus_packet_samples(&[(3 << 3) | 1]));
assert_eq!(None, opus_packet_samples(&[(3 << 3) | 3, 3]));
}
#[test]
fn a_malformed_opus_packet_has_no_duration() {
assert_eq!(None, opus_packet_samples(&[]));
assert_eq!(None, opus_packet_samples(&[0x03]));
assert_eq!(None, opus_packet_samples(&[0x03, 0x00]));
}
#[test]
fn every_dtx_gap_the_toc_can_describe_is_a_multiple_of_the_step() {
for toc in 0..=255u8 {
for count in 0..64u8 {
if let Some(samples) = opus_packet_samples(&[toc, count]) {
assert_eq!(0, samples % OPUS_DTX_TIMESTAMP_STEP);
}
}
}
}