use std::error::Error;
use std::fmt;
use crate::rtp_::{extend_u7, extend_u8, extend_u15};
use super::{BitRead, CodecExtra, Depacketizer, PacketError, Packetizer};
pub const VP8_HEADER_SIZE: usize = 1;
const PICTURE_ID_SHORT_MAX: u16 = 0x7f; const PICTURE_ID_MAX: u16 = 0x7fff; const PICTURE_ID_SHORT_MASK: u8 = PICTURE_ID_SHORT_MAX as u8;
const KEY_IDX_MASK: u8 = 0x1f; const VP8_PATCH_BYTES: usize = 4;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Vp8Patch {
len: u8,
offsets: [u8; VP8_PATCH_BYTES],
bytes: [u8; VP8_PATCH_BYTES],
}
impl Vp8Patch {
fn new() -> Self {
Self {
len: 0,
offsets: [0; VP8_PATCH_BYTES],
bytes: [0; VP8_PATCH_BYTES],
}
}
fn push(&mut self, offset: usize, byte: u8) {
let index = usize::from(self.len);
debug_assert!(index < self.offsets.len());
debug_assert!(offset <= usize::from(u8::MAX));
self.offsets[index] = offset as u8;
self.bytes[index] = byte;
self.len += 1;
}
pub(crate) fn copy_to(&self, payload: &[u8], dst: &mut [u8]) {
assert_eq!(payload.len(), dst.len());
dst.copy_from_slice(payload);
for i in 0..usize::from(self.len) {
dst[usize::from(self.offsets[i])] = self.bytes[i];
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Vp8DescriptorError {
ShortPayload,
MalformedDescriptor,
}
impl fmt::Display for Vp8DescriptorError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ShortPayload => write!(f, "VP8 payload is too short"),
Self::MalformedDescriptor => write!(f, "VP8 payload descriptor is malformed"),
}
}
}
impl Error for Vp8DescriptorError {}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Vp8PatchError {
PictureIdMissing,
PictureIdTooLarge,
Tl0PicIdxMissing,
KeyIdxMissing,
KeyIdxTooLarge,
}
impl fmt::Display for Vp8PatchError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::PictureIdMissing => write!(f, "VP8 payload descriptor has no PictureID"),
Self::PictureIdTooLarge => write!(f, "VP8 PictureID does not fit descriptor width"),
Self::Tl0PicIdxMissing => write!(f, "VP8 payload descriptor has no TL0PICIDX"),
Self::KeyIdxMissing => write!(f, "VP8 payload descriptor has no KEYIDX"),
Self::KeyIdxTooLarge => write!(f, "VP8 KEYIDX must fit in 5 bits"),
}
}
}
impl Error for Vp8PatchError {}
#[derive(Clone, Copy, Debug)]
pub struct Vp8PatchBuilder {
descriptor: Vp8Descriptor,
picture_id: Option<u16>,
tl0_pic_idx: Option<u8>,
key_idx: Option<u8>,
}
impl Vp8PatchBuilder {
pub fn picture_id(mut self, picture_id: u16) -> Self {
self.picture_id = Some(picture_id);
self
}
pub fn tl0_pic_idx(mut self, tl0_pic_idx: u8) -> Self {
self.tl0_pic_idx = Some(tl0_pic_idx);
self
}
pub fn key_idx(mut self, key_idx: u8) -> Self {
self.key_idx = Some(key_idx);
self
}
pub fn build(self) -> Result<Vp8Patch, Vp8PatchError> {
let mut patch = Vp8Patch::new();
if let Some(picture_id) = self.picture_id {
let Some(parsed) = self.descriptor.picture_id else {
return Err(Vp8PatchError::PictureIdMissing);
};
if picture_id > PICTURE_ID_MAX {
return Err(Vp8PatchError::PictureIdTooLarge);
}
if !parsed.is_15_bit && picture_id > PICTURE_ID_SHORT_MAX {
return Err(Vp8PatchError::PictureIdTooLarge);
}
if parsed.is_15_bit {
patch.push(
parsed.offset,
0x80 | ((picture_id >> 8) as u8 & PICTURE_ID_SHORT_MASK),
);
patch.push(parsed.offset + 1, picture_id as u8);
} else {
patch.push(parsed.offset, picture_id as u8);
}
}
if let Some(tl0_pic_idx) = self.tl0_pic_idx {
let Some(parsed) = self.descriptor.tl0_pic_idx else {
return Err(Vp8PatchError::Tl0PicIdxMissing);
};
patch.push(parsed.offset, tl0_pic_idx);
}
if let Some(key_idx) = self.key_idx {
let Some(parsed) = self.descriptor.key_idx else {
return Err(Vp8PatchError::KeyIdxMissing);
};
if key_idx > KEY_IDX_MASK {
return Err(Vp8PatchError::KeyIdxTooLarge);
}
patch.push(parsed.offset, (parsed.value & !KEY_IDX_MASK) | key_idx);
}
Ok(patch)
}
}
#[derive(Clone, Copy, Debug)]
pub struct Vp8Descriptor {
payload_offset: usize,
picture_id: Option<Vp8PictureId>,
tl0_pic_idx: Option<Vp8Field>,
key_idx: Option<Vp8Field>,
}
#[derive(Clone, Copy, Debug)]
struct Vp8Field {
value: u8,
offset: usize,
}
#[derive(Clone, Copy, Debug)]
struct Vp8PictureId {
value: u16,
offset: usize,
is_15_bit: bool,
}
impl Vp8Descriptor {
pub fn parse(payload: &[u8]) -> Result<Self, Vp8DescriptorError> {
let Some(required) = payload.first() else {
return Err(Vp8DescriptorError::ShortPayload);
};
let mut descriptor = Self {
payload_offset: 1,
picture_id: None,
tl0_pic_idx: None,
key_idx: None,
};
if required & 0x80 != 0 {
let extension = read_vp8_descriptor_byte(payload, &mut descriptor.payload_offset)?;
let has_picture_id = extension & 0x80 != 0;
let has_tl0_pic_idx = extension & 0x40 != 0;
let has_tid = extension & 0x20 != 0;
let has_key_idx = extension & 0x10 != 0;
if has_tl0_pic_idx && !has_tid {
return Err(Vp8DescriptorError::MalformedDescriptor);
}
if has_picture_id {
let picture_id_offset = descriptor.payload_offset;
let picture_id = read_vp8_descriptor_byte(payload, &mut descriptor.payload_offset)?;
let is_15_bit = picture_id & 0x80 != 0;
let value = if is_15_bit {
let picture_id_low =
read_vp8_descriptor_byte(payload, &mut descriptor.payload_offset)?;
(u16::from(picture_id & PICTURE_ID_SHORT_MASK) << 8) | u16::from(picture_id_low)
} else {
u16::from(picture_id & PICTURE_ID_SHORT_MASK)
};
descriptor.picture_id = Some(Vp8PictureId {
value,
offset: picture_id_offset,
is_15_bit,
});
}
if has_tl0_pic_idx {
let offset = descriptor.payload_offset;
let value = read_vp8_descriptor_byte(payload, &mut descriptor.payload_offset)?;
descriptor.tl0_pic_idx = Some(Vp8Field { value, offset });
}
if has_tid || has_key_idx {
let offset = descriptor.payload_offset;
let value = read_vp8_descriptor_byte(payload, &mut descriptor.payload_offset)?;
if has_key_idx {
descriptor.key_idx = Some(Vp8Field { value, offset });
}
}
}
if descriptor.payload_offset >= payload.len() {
return Err(Vp8DescriptorError::ShortPayload);
}
Ok(descriptor)
}
pub const fn picture_id(&self) -> Option<u16> {
match self.picture_id {
Some(picture_id) => Some(picture_id.value),
None => None,
}
}
pub const fn tl0_pic_idx(&self) -> Option<u8> {
match self.tl0_pic_idx {
Some(tl0_pic_idx) => Some(tl0_pic_idx.value),
None => None,
}
}
pub const fn key_idx(&self) -> Option<u8> {
match self.key_idx {
Some(key_idx) => Some(key_idx.value & KEY_IDX_MASK),
None => None,
}
}
pub fn starts_keyframe(&self, payload: &[u8]) -> bool {
let Some(required) = payload.first() else {
return false;
};
if required & 0x10 == 0 || required & 0x07 != 0 {
return false;
}
payload
.get(self.payload_offset)
.is_some_and(|header| header & 0x01 == 0)
}
pub fn patch(&self) -> Vp8PatchBuilder {
Vp8PatchBuilder {
descriptor: *self,
picture_id: None,
tl0_pic_idx: None,
key_idx: None,
}
}
}
fn read_vp8_descriptor_byte(payload: &[u8], offset: &mut usize) -> Result<u8, Vp8DescriptorError> {
let byte = payload
.get(*offset)
.ok_or(Vp8DescriptorError::ShortPayload)?;
*offset += 1;
Ok(*byte)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Vp8CodecExtra {
pub discardable: bool,
pub sync: bool,
pub layer_index: u8,
pub picture_id: Option<u64>,
pub tl0_picture_id: Option<u64>,
pub is_keyframe: bool,
}
pub fn detect_vp8_keyframe(payload: &[u8]) -> bool {
if payload.is_empty() {
return false;
}
let b0 = payload[0];
let s = (b0 & 0x10) >> 4; let pid = b0 & 0x07; if s != 1 || pid != 0 {
return false;
}
let x = (b0 & 0x80) >> 7; let mut idx = 1;
if x == 1 {
if idx >= payload.len() {
return false;
}
let ext = payload[idx];
idx += 1;
let i = (ext & 0x80) >> 7; let l = (ext & 0x40) >> 6; let t = (ext & 0x20) >> 5; let k = (ext & 0x10) >> 4; if i == 1 {
if idx >= payload.len() {
return false;
}
if payload[idx] & 0x80 != 0 {
idx += 2; } else {
idx += 1; }
}
if l == 1 {
idx += 1; }
if t == 1 || k == 1 {
idx += 1; }
}
if idx >= payload.len() {
return false;
}
payload[idx] & 0x01 == 0
}
#[derive(Default, Debug, Copy, Clone)]
pub struct Vp8Packetizer {
enable_picture_id: bool,
picture_id: u16,
}
impl Packetizer for Vp8Packetizer {
fn packetize(&mut self, mtu: usize, payload: &[u8]) -> Result<Vec<Vec<u8>>, PacketError> {
if payload.is_empty() || mtu == 0 {
return Ok(vec![]);
}
let using_header_size = if self.enable_picture_id {
if self.picture_id == 0 || self.picture_id < 128 {
VP8_HEADER_SIZE + 2
} else {
VP8_HEADER_SIZE + 3
}
} else {
VP8_HEADER_SIZE
};
let max_fragment_size = mtu as isize - using_header_size as isize;
let mut payload_data_remaining = payload.len() as isize;
let mut payload_data_index: usize = 0;
let mut payloads = vec![];
if std::cmp::min(max_fragment_size, payload_data_remaining) <= 0 {
return Ok(payloads);
}
let mut first = true;
while payload_data_remaining > 0 {
let current_fragment_size =
std::cmp::min(max_fragment_size, payload_data_remaining) as usize;
let mut out = Vec::with_capacity(using_header_size + current_fragment_size);
let mut buf = [0u8; 4];
if first {
buf[0] = 0x10;
first = false;
}
if self.enable_picture_id {
if using_header_size == VP8_HEADER_SIZE + 2 {
buf[0] |= 0x80;
buf[1] |= 0x80;
buf[2] |= (self.picture_id & u16::from(PICTURE_ID_SHORT_MASK)) as u8;
} else if using_header_size == VP8_HEADER_SIZE + 3 {
buf[0] |= 0x80;
buf[1] |= 0x80;
buf[2] |=
0x80 | ((self.picture_id >> 8) & u16::from(PICTURE_ID_SHORT_MASK)) as u8;
buf[3] |= (self.picture_id & 0xFF) as u8;
}
}
out.extend_from_slice(&buf[..using_header_size]);
out.extend_from_slice(
&payload[payload_data_index..payload_data_index + current_fragment_size],
);
payloads.push(out);
payload_data_remaining -= current_fragment_size as isize;
payload_data_index += current_fragment_size;
}
self.picture_id += 1;
self.picture_id &= PICTURE_ID_MAX;
Ok(payloads)
}
fn is_marker(&mut self, _data: &[u8], _previous: Option<&[u8]>, last: bool) -> bool {
last
}
}
#[derive(PartialEq, Eq, Debug, Default, Clone)]
pub struct Vp8Depacketizer {
pub x: u8,
pub n: u8,
pub s: u8,
pub pid: u8,
pub i: u8,
pub l: u8,
pub t: u8,
pub k: u8,
pub picture_id: u16,
pub extended_pid: Option<u64>,
pub tl0_pic_idx: u8,
pub extended_tl0_pic_idx: Option<u64>,
pub tid: u8,
pub y: u8,
pub key_idx: u8,
pub p: u8,
}
impl Depacketizer for Vp8Depacketizer {
fn out_size_hint(&self, packets_size: usize) -> Option<usize> {
Some(packets_size)
}
fn depacketize(
&mut self,
packet: &[u8],
out: &mut Vec<u8>,
extra: &mut CodecExtra,
) -> Result<(), PacketError> {
let payload_len = packet.len();
let mut reader = (packet, 0);
let mut payload_index = 0;
let mut b = reader.get_u8().ok_or(PacketError::ErrShortPacket)?;
payload_index += 1;
self.x = (b & 0x80) >> 7;
self.n = (b & 0x20) >> 5;
self.s = (b & 0x10) >> 4;
self.pid = b & 0x07;
let (i, l, t, k) = if self.x == 1 {
b = reader.get_u8().ok_or(PacketError::ErrShortPacket)?;
payload_index += 1;
let i = (b & 0x80) >> 7;
let l = (b & 0x40) >> 6;
let t = (b & 0x20) >> 5;
let k = (b & 0x10) >> 4;
if l == 1 && t == 0 {
return Err(PacketError::ErrVP8CorruptedPacket);
}
(i, l, t, k)
} else {
(0, 0, 0, 0)
};
self.i = i;
self.l = l;
self.t = t;
self.k = k;
if self.i == 1 {
b = reader.get_u8().ok_or(PacketError::ErrShortPacket)?;
payload_index += 1;
let previous_picture_id = u64::from(self.picture_id);
let previous = self.extended_pid.map(|_| previous_picture_id);
let next = if b & 0x80 > 0 {
let x = reader.get_u8().ok_or(PacketError::ErrShortPacket)?;
self.picture_id = (((b & PICTURE_ID_SHORT_MASK) as u16) << 8) | (x as u16);
payload_index += 1;
extend_u15(previous, self.picture_id)
} else {
self.picture_id = b as u16;
extend_u7(previous, b)
};
self.extended_pid = Some(match self.extended_pid {
Some(previous) => previous.wrapping_add(next.wrapping_sub(previous_picture_id)),
None => next,
});
}
if payload_index >= payload_len {
return Err(PacketError::ErrShortPacket);
}
if self.l == 1 {
self.tl0_pic_idx = reader.get_u8().ok_or(PacketError::ErrShortPacket)?;
self.extended_tl0_pic_idx =
Some(extend_u8(self.extended_tl0_pic_idx, self.tl0_pic_idx));
payload_index += 1;
}
if payload_index >= payload_len {
return Err(PacketError::ErrShortPacket);
}
if self.t == 1 || self.k == 1 {
let b = reader.get_u8().ok_or(PacketError::ErrShortPacket)?;
if self.t == 1 {
self.tid = b >> 6;
self.y = (b >> 5) & 0x1;
}
if self.k == 1 {
self.key_idx = b & KEY_IDX_MASK;
}
payload_index += 1;
}
if payload_index >= packet.len() {
return Err(PacketError::ErrShortPacket);
}
out.extend_from_slice(&packet[payload_index..]);
self.p = if self.s == 1 && self.pid == 0 {
b = reader.get_u8().ok_or(PacketError::ErrShortPacket)?;
payload_index += 1;
b & 1
} else {
1
};
let is_keyframe = if let CodecExtra::Vp8(e) = extra {
e.is_keyframe | (self.p == 0)
} else {
self.p == 0
};
*extra = CodecExtra::Vp8(Vp8CodecExtra {
discardable: self.n == 1,
sync: self.y == 1,
layer_index: self.tid,
picture_id: if self.i == 1 { self.extended_pid } else { None },
tl0_picture_id: if self.l == 1 {
self.extended_tl0_pic_idx
} else {
None
},
is_keyframe,
});
let _ = payload_index;
Ok(())
}
fn is_partition_head(&self, payload: &[u8]) -> bool {
if payload.is_empty() {
false
} else {
(payload[0] & 0x10) != 0
}
}
fn is_partition_tail(&self, marker: bool, _payload: &[u8]) -> bool {
marker
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_vp8_unmarshal() -> Result<(), PacketError> {
let mut pck = Vp8Depacketizer::default();
let mut extra = CodecExtra::None;
let empty_bytes = &[];
let mut payload = Vec::new();
let result = pck.depacketize(empty_bytes, &mut payload, &mut extra);
assert!(result.is_err(), "Result should be err in case of error");
let small_bytes = &[0x00, 0x11, 0x22];
let mut payload = Vec::new();
pck.depacketize(small_bytes, &mut payload, &mut extra)
.expect("Small packet");
assert_eq!(payload, [0x11, 0x22]);
let small_bytes = &[0x00];
let mut payload = Vec::new();
let result = pck.depacketize(small_bytes, &mut payload, &mut extra);
assert!(result.is_err(), "Result should be err in case of error");
let raw_bytes = &[0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x90];
let mut payload = Vec::new();
pck.depacketize(raw_bytes, &mut payload, &mut extra)
.expect("Normal packet");
assert!(!payload.is_empty(), "Payload must be not empty");
let raw_bytes = &[0x80, 0x00, 0x00, 0x00];
let mut payload = Vec::new();
pck.depacketize(raw_bytes, &mut payload, &mut extra)
.expect("Only X");
assert!(!payload.is_empty(), "Payload must be not empty");
assert_eq!(pck.x, 1, "X must be 1");
assert_eq!(pck.i, 0, "I must be 0");
assert_eq!(pck.l, 0, "L must be 0");
assert_eq!(pck.t, 0, "T must be 0");
assert_eq!(pck.k, 0, "K must be 0");
assert_eq!(pck.p, 1, "P must be 1");
let raw_bytes = &[0x80, 0x80, 0x81, 0x00, 0x00];
let mut payload = Vec::new();
pck.depacketize(raw_bytes, &mut payload, &mut extra)
.expect("X and I, PID 16bits");
assert!(!payload.is_empty(), "Payload must be not empty");
assert_eq!(pck.x, 1, "X must be 1");
assert_eq!(pck.i, 1, "I must be 1");
assert_eq!(pck.l, 0, "L must be 0");
assert_eq!(pck.t, 0, "T must be 0");
assert_eq!(pck.k, 0, "K must be 0");
assert_eq!(pck.p, 1, "P must be 1");
let raw_bytes = &[0x80, 0x40, 0x00, 0x00];
let mut payload = Vec::new();
assert_eq!(
pck.depacketize(raw_bytes, &mut payload, &mut extra),
Err(PacketError::ErrVP8CorruptedPacket)
);
let raw_bytes = &[0x00, 0x11, 0x22];
let mut payload = Vec::new();
pck.depacketize(raw_bytes, &mut payload, &mut extra)
.expect("valid packet after malformed descriptor");
assert_eq!(payload, [0x11, 0x22]);
let raw_bytes = &[0x80, 0x20, 0x00, 0x00];
let mut payload = Vec::new();
pck.depacketize(raw_bytes, &mut payload, &mut extra)
.expect("X and T");
assert!(!payload.is_empty(), "Payload must be not empty");
assert_eq!(pck.x, 1, "X must be 1");
assert_eq!(pck.i, 0, "I must be 0");
assert_eq!(pck.l, 0, "L must be 0");
assert_eq!(pck.t, 1, "T must be 1");
assert_eq!(pck.k, 0, "K must be 0");
assert_eq!(pck.p, 1, "P must be 1");
let raw_bytes = &[0x80, 0x10, 0x00, 0x00];
let mut payload = Vec::new();
pck.depacketize(raw_bytes, &mut payload, &mut extra)
.expect("X and K");
assert!(!payload.is_empty(), "Payload must be not empty");
assert_eq!(pck.x, 1, "X must be 1");
assert_eq!(pck.i, 0, "I must be 0");
assert_eq!(pck.l, 0, "L must be 0");
assert_eq!(pck.t, 0, "T must be 0");
assert_eq!(pck.k, 1, "K must be 1");
assert_eq!(pck.p, 1, "P must be 1");
let raw_bytes = &[0xff, 0xff, 0x00, 0x00, 0x00, 0x00];
let mut payload = Vec::new();
pck.depacketize(raw_bytes, &mut payload, &mut extra)
.expect("all flags and 8bit picture_id");
assert!(!payload.is_empty(), "Payload must be not empty");
assert_eq!(pck.x, 1, "X must be 1");
assert_eq!(pck.i, 1, "I must be 1");
assert_eq!(pck.l, 1, "L must be 1");
assert_eq!(pck.t, 1, "T must be 1");
assert_eq!(pck.k, 1, "K must be 1");
assert_eq!(pck.p, 1, "P must be 1");
let raw_bytes = &[0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00];
let mut payload = Vec::new();
pck.depacketize(raw_bytes, &mut payload, &mut extra)
.expect("all flags and 16bit picture_id");
assert!(!payload.is_empty(), "Payload must be not empty");
assert_eq!(pck.x, 1, "X must be 1");
assert_eq!(pck.i, 1, "I must be 1");
assert_eq!(pck.l, 1, "L must be 1");
assert_eq!(pck.t, 1, "T must be 1");
assert_eq!(pck.k, 1, "K must be 1");
assert_eq!(pck.p, 1, "P must be 1");
let raw_bytes = &[0x90, 0x80, 0x11, 0x10, 0x00, 0x00, 0x00];
let mut payload = Vec::new();
pck.depacketize(raw_bytes, &mut payload, &mut extra)
.expect("all flags and 16bit picture_id");
assert!(!payload.is_empty(), "Payload must be not empty");
assert_eq!(pck.x, 1, "X must be 1");
assert_eq!(pck.i, 1, "I must be 1");
assert_eq!(pck.l, 0, "L must be 0");
assert_eq!(pck.t, 0, "T must be 0");
assert_eq!(pck.k, 0, "K must be 0");
assert_eq!(pck.p, 0, "P must be 0");
Ok(())
}
#[test]
fn test_vp8_payload() -> Result<(), PacketError> {
let tests: Vec<(&str, Vp8Packetizer, usize, Vec<&[u8]>, Vec<Vec<&[u8]>>)> = vec![
(
"WithoutPictureID",
Vp8Packetizer::default(),
2,
vec![&[0x90, 0x90, 0x90], &[0x91, 0x91]],
vec![
vec![&[0x10, 0x90], &[0x00, 0x90], &[0x00, 0x90]],
vec![&[0x10, 0x91], &[0x00, 0x91]],
],
),
(
"WithPictureID_1byte",
Vp8Packetizer {
enable_picture_id: true,
picture_id: 0x20,
},
5,
vec![&[0x90, 0x90, 0x90], &[0x91, 0x91]],
vec![
vec![&[0x90, 0x80, 0x20, 0x90, 0x90], &[0x80, 0x80, 0x20, 0x90]],
vec![&[0x90, 0x80, 0x21, 0x91, 0x91]],
],
),
(
"WithPictureID_2bytes",
Vp8Packetizer {
enable_picture_id: true,
picture_id: 0x120,
},
6,
vec![&[0x90, 0x90, 0x90], &[0x91, 0x91]],
vec![
vec![
&[0x90, 0x80, 0x81, 0x20, 0x90, 0x90],
&[0x80, 0x80, 0x81, 0x20, 0x90],
],
vec![&[0x90, 0x80, 0x81, 0x21, 0x91, 0x91]],
],
),
];
for (name, mut pck, mtu, payloads, expected) in tests {
for (i, payload) in payloads.iter().enumerate() {
let actual = pck.packetize(mtu, payload)?;
assert_eq!(expected[i], actual, "{name}: Generated packet[{i}] differs");
}
}
Ok(())
}
#[test]
fn test_vp8_payload_eror() -> Result<(), PacketError> {
let mut pck = Vp8Packetizer::default();
let empty = &[];
let payload = &[0x90, 0x90, 0x90];
let result = pck.packetize(1, empty)?;
assert!(result.is_empty(), "Generated payload should be empty");
let result = pck.packetize(1, payload)?;
assert_eq!(result.len(), 0, "Generated payload should be empty");
let result = pck.packetize(2, payload)?;
assert_eq!(
result.len(),
payload.len(),
"Generated payload should be the same size as original payload size"
);
Ok(())
}
#[test]
fn test_vp8_partition_head_checker_is_partition_head() -> Result<(), PacketError> {
let vp8 = Vp8Depacketizer::default();
assert!(
!vp8.is_partition_head(&[0x00]),
"Small packet should not be the head of a new partition"
);
assert!(
vp8.is_partition_head(&[0x10, 0x00, 0x00, 0x00]),
"Packet with S flag should be the head of a new partition"
);
assert!(
!vp8.is_partition_head(&[0x00, 0x00, 0x00, 0x00]),
"Packet without S flag should not be the head of a new partition"
);
Ok(())
}
fn rewrite_payload(
payload: &[u8],
patch: impl FnOnce(Vp8PatchBuilder) -> Vp8PatchBuilder,
) -> Result<Vec<u8>, Vp8PatchError> {
let descriptor = Vp8Descriptor::parse(payload).expect("valid VP8 descriptor");
let patch = patch(descriptor.patch()).build()?;
let mut out = vec![0; payload.len()];
patch.copy_to(payload, &mut out);
Ok(out)
}
#[test]
fn test_vp8_descriptor_exposes_fields() {
let short = Vp8Descriptor::parse(&[0x90, 0x80, 0x42, 0x00]).unwrap();
assert_eq!(short.picture_id(), Some(0x42));
assert_eq!(short.tl0_pic_idx(), None);
assert_eq!(short.key_idx(), None);
let full = Vp8Descriptor::parse(&[0x90, 0xf0, 0x92, 0x34, 0x55, 0xa7, 0x00]).unwrap();
assert_eq!(full.picture_id(), Some(0x1234));
assert_eq!(full.tl0_pic_idx(), Some(0x55));
assert_eq!(full.key_idx(), Some(0x07));
}
#[test]
fn test_vp8_patch_updates_descriptor_fields() {
assert_eq!(
rewrite_payload(&[0x80, 0x80, 0x01, 0x90], |patch| {
patch.picture_id(u16::from(PICTURE_ID_SHORT_MASK))
})
.unwrap(),
[0x80, 0x80, PICTURE_ID_SHORT_MASK, 0x90]
);
assert_eq!(
rewrite_payload(&[0x80, 0x80, 0x81, 0x20, 0x90], |patch| {
patch.picture_id(0x0234)
})
.unwrap(),
[0x80, 0x80, 0x82, 0x34, 0x90]
);
assert_eq!(
rewrite_payload(&[0x80, 0x80, 0x81, 0x20, 0x90], |patch| {
patch.picture_id(PICTURE_ID_MAX)
})
.unwrap(),
[0x80, 0x80, 0xff, 0xff, 0x90]
);
assert_eq!(
rewrite_payload(&[0x80, 0x60, 0x12, 0x00, 0x90], |patch| {
patch.tl0_pic_idx(0xab)
})
.unwrap(),
[0x80, 0x60, 0xab, 0x00, 0x90]
);
assert_eq!(
rewrite_payload(&[0x80, 0x30, 0xaa, 0x90], |patch| {
patch.key_idx(KEY_IDX_MASK)
})
.unwrap(),
[0x80, 0x30, 0xbf, 0x90]
);
}
#[test]
fn test_vp8_patch_rejects_unsupported_descriptor_shapes() {
assert_eq!(
Vp8Descriptor::parse(&[0x80, 0x80]).unwrap_err(),
Vp8DescriptorError::ShortPayload
);
assert_eq!(
rewrite_payload(&[0x00, 0x90], |patch| patch.picture_id(1)).unwrap_err(),
Vp8PatchError::PictureIdMissing
);
assert_eq!(
rewrite_payload(&[0x00, 0x90], |patch| {
patch.picture_id(PICTURE_ID_MAX + 1)
})
.unwrap_err(),
Vp8PatchError::PictureIdMissing
);
assert_eq!(
rewrite_payload(&[0x80, 0x80, 0x81, 0x20, 0x90], |patch| {
patch.tl0_pic_idx(1)
})
.unwrap_err(),
Vp8PatchError::Tl0PicIdxMissing
);
assert_eq!(
rewrite_payload(&[0x80, 0x80, 0x81, 0x20, 0x90], |patch| {
patch.key_idx(1)
})
.unwrap_err(),
Vp8PatchError::KeyIdxMissing
);
assert_eq!(
rewrite_payload(&[0x80, 0x80, 0x81, 0x20, 0x90], |patch| {
patch.key_idx(KEY_IDX_MASK + 1)
})
.unwrap_err(),
Vp8PatchError::KeyIdxMissing
);
assert_eq!(
Vp8Descriptor::parse(&[0x80, 0x40, 0x12, 0x90]).unwrap_err(),
Vp8DescriptorError::MalformedDescriptor
);
}
#[test]
fn test_vp8_patch_rejects_impossible_values() {
assert_eq!(
rewrite_payload(&[0x80, 0x80, 0x01, 0x90], |patch| {
patch.picture_id(0x80)
})
.unwrap_err(),
Vp8PatchError::PictureIdTooLarge
);
assert_eq!(
rewrite_payload(&[0x80, 0x80, 0x81, 0x20, 0x90], |patch| {
patch.picture_id(PICTURE_ID_MAX + 1)
})
.unwrap_err(),
Vp8PatchError::PictureIdTooLarge
);
assert_eq!(
rewrite_payload(&[0x80, 0x10, 0x00, 0x90], |patch| {
patch.key_idx(KEY_IDX_MASK + 1)
})
.unwrap_err(),
Vp8PatchError::KeyIdxTooLarge
);
}
#[test]
fn test_detect_vp8_keyframe() {
assert!(!detect_vp8_keyframe(&[]));
assert!(detect_vp8_keyframe(&[0x10, 0x00]));
assert!(!detect_vp8_keyframe(&[0x10, 0x01]));
assert!(!detect_vp8_keyframe(&[0x00, 0x00]));
assert!(!detect_vp8_keyframe(&[0x11, 0x00]));
assert!(detect_vp8_keyframe(&[0x90, 0x80, 0x42, 0x00]));
assert!(!detect_vp8_keyframe(&[0x90, 0x80, 0x42, 0x01]));
assert!(detect_vp8_keyframe(&[0x90, 0x80, 0x80, 0x42, 0x00]));
assert!(detect_vp8_keyframe(&[
0x90, 0xE0, 0x80, 0x42, 0x01, 0x00, 0x00
]));
assert!(detect_vp8_keyframe(&[0x90, 0x40, 0x01, 0x00]));
assert!(!detect_vp8_keyframe(&[0x90, 0x80]));
assert!(!detect_vp8_keyframe(&[0x90, 0x80, 0x42]));
}
#[test]
fn test_vp8_descriptor_keyframe_detection_matches_helper() {
let payloads: &[&[u8]] = &[
&[],
&[0x10, 0x00],
&[0x10, 0x01],
&[0x00, 0x00],
&[0x11, 0x00],
&[0x90, 0x80, 0x42, 0x00],
&[0x90, 0x80, 0x42, 0x01],
&[0x90, 0x80],
&[0x90, 0x80, 0x42],
];
for payload in payloads {
let descriptor_starts_keyframe = Vp8Descriptor::parse(payload)
.map(|descriptor| descriptor.starts_keyframe(payload))
.unwrap_or(false);
assert_eq!(descriptor_starts_keyframe, detect_vp8_keyframe(payload));
}
}
#[test]
fn packetize_respects_mtu() -> Result<(), PacketError> {
let payload = vec![0xABu8; 2000];
for &mtu in &[100usize, 300, 600, 1200] {
let mut pck = Vp8Packetizer::default();
let pkts = pck.packetize(mtu, &payload)?;
assert!(!pkts.is_empty(), "VP8 produced no packets at mtu {mtu}");
for (i, pkt) in pkts.iter().enumerate() {
assert!(
pkt.len() <= mtu,
"VP8 packet {i} size {} > mtu {mtu}",
pkt.len()
);
}
}
Ok(())
}
fn picture_id_of(depack: &mut Vp8Depacketizer, picture_id: &[u8]) -> Option<u64> {
let mut packet = vec![0x90, 0x80];
packet.extend_from_slice(picture_id);
packet.push(0x01);
let mut payload = Vec::new();
let mut extra = CodecExtra::None;
depack
.depacketize(&packet, &mut payload, &mut extra)
.unwrap();
let CodecExtra::Vp8(extra) = extra else {
panic!("expected VP8 codec extra");
};
extra.picture_id
}
fn frame(keyframe: bool, picture_id: &[u8], tl0_pic_idx: u8) -> Vec<Vec<u8>> {
let mut packet = vec![0x90, 0xE0];
packet.extend_from_slice(picture_id);
packet.extend_from_slice(&[tl0_pic_idx, 0x00, u8::from(!keyframe)]);
vec![packet]
}
fn assert_every_frame_emitted(frames: impl IntoIterator<Item = Vec<Vec<u8>>>) {
use crate::packet::{CodecDepacketizer, DepacketizingBuffer, RtpMeta};
use crate::rtp_::{MediaTime, RtpHeader};
use std::time::Instant;
let mut buf =
DepacketizingBuffer::new(CodecDepacketizer::Vp8(Vp8Depacketizer::default()), 3);
let now = Instant::now();
let mut seq = 0_u64;
for (frame, packets) in frames.into_iter().enumerate() {
let count = packets.len();
let time = frame as u64 * 3000;
for (i, packet) in packets.into_iter().enumerate() {
let meta = RtpMeta {
received: now,
time: MediaTime::from_90khz(time),
seq_no: seq.into(),
last_sender_info: None,
header: RtpHeader {
sequence_number: seq as u16,
timestamp: time as u32,
marker: i == count - 1,
..Default::default()
},
};
buf.push(meta, packet);
seq += 1;
}
let dep = buf
.pop(now, None)
.unwrap_or_else(|| panic!("frame {frame} was not emitted"))
.unwrap();
assert!(dep.contiguous, "frame {frame} is not contiguous");
}
}
#[test]
fn depacketizer_preserves_picture_id_epoch_when_width_grows() {
let mut depack = Vp8Depacketizer::default();
for (picture_id, expected) in [(126_u8, 126_u64), (127, 127), (0, 128), (1, 129)] {
assert_eq!(picture_id_of(&mut depack, &[picture_id]), Some(expected));
}
assert_eq!(picture_id_of(&mut depack, &[0x80, 2]), Some(130));
}
#[test]
fn depacketizing_buffer_emits_frames_when_picture_id_width_grows() {
let frames = [
frame(true, &[126], 10),
frame(false, &[127], 11),
frame(false, &[0], 12),
frame(false, &[1], 13),
frame(false, &[0x80, 2], 14),
frame(false, &[0x80, 3], 15),
];
assert_every_frame_emitted(frames);
}
#[test]
fn depacketizer_preserves_wide_picture_id_after_loss_at_width_growth() {
let mut depack = Vp8Depacketizer::default();
assert_eq!(picture_id_of(&mut depack, &[0]), Some(0));
assert_eq!(picture_id_of(&mut depack, &[0x80, 100]), Some(100));
}
#[test]
fn depacketizing_buffer_recovers_after_loss_at_picture_id_width_growth() {
let frames = std::iter::once(frame(true, &[0], 0))
.chain((100..=130_u8).map(|pid| frame(pid == 100, &[0x80, pid], pid - 99)));
assert_every_frame_emitted(frames);
}
}