Define Splice Descriptor using Deku
Implements the generic Splice Descriptor definition using the Deku declarative library macros. I've implemented only the generic variant of the Splice Descriptors to check how it would look like using Deku.
This commit is contained in:
parent
6b13875150
commit
56bc156be4
2 changed files with 71 additions and 17 deletions
84
src/data.rs
84
src/data.rs
|
@ -1,34 +1,25 @@
|
|||
use deku::prelude::*;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, DekuRead, DekuWrite)]
|
||||
#[deku(endian = "big", type="u8", bits = "1")]
|
||||
#[deku(endian = "big", type = "u8", bits = "1")]
|
||||
pub(crate) enum SpliceTime {
|
||||
|
||||
#[deku(id = "1")]
|
||||
TimeSpecified {
|
||||
|
||||
#[deku(bits = "6", assert_eq = "0x3f")]
|
||||
reserved: u8,
|
||||
|
||||
#[deku(bits = "33")]
|
||||
pts_time: u64,
|
||||
|
||||
},
|
||||
|
||||
#[deku(id = "0")]
|
||||
NoTimeSpecified {
|
||||
|
||||
#[deku(bits = "7", assert_eq = "0x7f")]
|
||||
reserved: u8,
|
||||
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
impl Default for SpliceTime {
|
||||
fn default() -> Self {
|
||||
Self::NoTimeSpecified {
|
||||
reserved: 0x7f
|
||||
}
|
||||
Self::NoTimeSpecified { reserved: 0x7f }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -36,11 +27,44 @@ impl SpliceTime {
|
|||
fn new(pts_time: u64) -> Self {
|
||||
Self::TimeSpecified {
|
||||
reserved: 0x3f,
|
||||
pts_time
|
||||
pts_time,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, DekuRead, DekuWrite)]
|
||||
#[deku(type = "u8")]
|
||||
pub(crate) enum SpliceDescriptor {
|
||||
// #[deku(id = "0x02")]
|
||||
// SegmentationDescriptor {
|
||||
// #[deku(update = "deku::rest")]
|
||||
// descriptor_length: u8,
|
||||
// identifier: u32,
|
||||
//
|
||||
// },
|
||||
#[deku(id_pat = "_")]
|
||||
Template(GenericDescriptor),
|
||||
}
|
||||
|
||||
impl SpliceDescriptor {
|
||||
pub(crate) fn update(&mut self) -> Result<(), deku::DekuError> {
|
||||
match self {
|
||||
SpliceDescriptor::Template(s) => s.update(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, DekuRead, DekuWrite)]
|
||||
#[deku(endian = "big")]
|
||||
pub(crate) struct GenericDescriptor {
|
||||
id: u8,
|
||||
#[deku(update = "self.private_bytes.len() + 2")]
|
||||
descriptor_length: u8,
|
||||
identifier: u32,
|
||||
#[deku(count = "descriptor_length - 2")]
|
||||
private_bytes: Vec<u8>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -68,4 +92,36 @@ mod tests {
|
|||
|
||||
assert_eq!(st, SpliceTime::new(0x072bd0050));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_generic_descriptor() {
|
||||
let mut sd = SpliceDescriptor::Template(GenericDescriptor {
|
||||
id: 0xff,
|
||||
descriptor_length: 0,
|
||||
identifier: 0x43554549,
|
||||
private_bytes: vec![0x01],
|
||||
});
|
||||
|
||||
sd.update().unwrap();
|
||||
|
||||
let data: Vec<u8> = sd.try_into().unwrap();
|
||||
|
||||
assert_eq!(hex::encode(data.as_slice()), "ff034355454901");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_generic_descriptor() {
|
||||
let data = hex::decode("ff034355454901").unwrap();
|
||||
let sd = SpliceDescriptor::try_from(data.as_slice()).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
sd,
|
||||
SpliceDescriptor::Template(GenericDescriptor {
|
||||
id: 0xff,
|
||||
descriptor_length: 3,
|
||||
identifier: 0x43554549,
|
||||
private_bytes: vec![0x01]
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -87,7 +87,6 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn encode_time_signal() {
|
||||
|
||||
let mut st = SpliceTime::default();
|
||||
|
||||
let mut data = Vec::new();
|
||||
|
@ -98,7 +97,6 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn encode_time_signal_with_time() {
|
||||
|
||||
let mut st = SpliceTime::from(0x072bd0050);
|
||||
|
||||
let mut data = Vec::new();
|
||||
|
@ -106,4 +104,4 @@ mod test {
|
|||
|
||||
assert_eq!(hex::encode(data.as_slice()), "fe72bd0050")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue