diff --git a/src/info.rs b/src/info.rs index 42a2a8d..371a5a2 100644 --- a/src/info.rs +++ b/src/info.rs @@ -1,3 +1,4 @@ +use std::fmt; use crate::commands::{SpliceCommand, SpliceCommandType}; use crate::descriptors::SpliceDescriptor; use crate::{CueError, TransportPacketWrite}; @@ -7,6 +8,7 @@ use std::fmt::{Display, Formatter}; pub const MPEG_2: Crc = Crc::::new(&CRC_32_MPEG_2); +#[derive(Debug, Clone, PartialEq)] pub struct SpliceInfoSection where C: SpliceCommand, @@ -16,6 +18,7 @@ where encoded: S, } +#[derive(Debug, Clone, PartialEq)] struct SpliceInfoState where C: SpliceCommand, @@ -50,10 +53,12 @@ where pub trait EncodingState {} +#[derive(Debug, Clone, Copy, PartialEq)] struct NotEncoded; impl EncodingState for NotEncoded {} +#[derive(Debug, Clone, PartialEq)] struct EncodedData { section_length: u16, splice_command_length: u16, @@ -199,11 +204,11 @@ impl SpliceInfoSection where C: SpliceCommand, { - pub fn as_base64(&self) -> String { + pub fn to_base64(&self) -> String { base64::encode(self.as_bytes()) } - pub fn as_hex(&self) -> String { + pub fn to_hex(&self) -> String { format!("0x{}", hex::encode(self.as_bytes())) } @@ -342,7 +347,7 @@ mod tests { let splice = SpliceInfoSection::new(SpliceNull::new()); assert_eq!( - splice.into_encoded()?.as_base64(), + splice.into_encoded()?.to_base64(), "/DARAAAAAAAAAP/wAAAAAHpPv/8=".to_string() ); @@ -354,7 +359,7 @@ mod tests { let splice = SpliceInfoSection::new(SpliceNull::new()); assert_eq!( - splice.into_encoded()?.as_hex(), + splice.into_encoded()?.to_hex(), "0xfc301100000000000000fff0000000007a4fbfff".to_string() ); @@ -367,7 +372,7 @@ mod tests { // splice.add_descriptor(SegmentationDescriptor::new().into()); assert_eq!( - splice.into_encoded()?.as_base64(), + splice.into_encoded()?.to_base64(), "/DA0AAAAAAAA///wBQb+cr0AUAAeAhxDVUVJSAAAjn/PAAGlmbAICAAAAAAsoKGKNAIAmsnRfg==" .to_string() ); @@ -381,7 +386,7 @@ mod tests { // 0xFC3034000000000000FFFFF00506FE72BD0050001E021C435545494800008E7FCF0001A599B00808000000002CA0A18A3402009AC9D17E assert_eq!( - splice.into_encoded()?.as_hex(), + splice.into_encoded()?.to_hex(), "0xfc301600000000000000fff005068072bd00500000e9dfc26c".to_string() ); Ok(()) diff --git a/src/lib.rs b/src/lib.rs index d045ce4..e7fd5ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,15 +26,20 @@ pub enum CueError { } pub trait ClockTimeExt { - fn as_90k(&self) -> u64; + fn to_90k(&self) -> u64; } impl ClockTimeExt for Duration { - fn as_90k(&self) -> u64 { - (self.as_secs_f64() * 90_000.0) as u64 + fn to_90k(&self) -> u64 { + (self.as_secs_f64() * 90_000.0).floor() as u64 } } +/// Truncate to 6 decimal positions, as shown in the spec. +fn ticks_to_secs(value: u64) -> f64 { + (value as f64 / 90_000.0 * 1_000_000.0).ceil() as f64 / 1_000_000.0 +} + #[cfg(feature = "serde")] fn serialize_time(value: &u64, serializer: S) -> Result where @@ -43,11 +48,6 @@ where serializer.serialize_f64(ticks_to_secs(*value)) } -/// Truncate to 6 decimal positions, as shown in the spec. -pub fn ticks_to_secs(value: u64) -> f64 { - (value as f64 / 90_000.0 * 1_000_000.0).ceil() as f64 / 1_000_000.0 -} - #[cfg(test)] mod tests { use super::*; @@ -55,12 +55,20 @@ mod tests { #[test] fn test_clock_time() { let duration = Duration::from_secs(1); - assert_eq!(duration.as_90k(), 90_000); + assert_eq!(duration.to_90k(), 90_000); } #[test] fn test_spec_example() { let time = Duration::from_secs_f64(21388.766756); - assert_eq!(time.as_90k(), 0x072bd0050); + assert_eq!(time.to_90k(), 0x072bd0050); + } + + #[test] + fn test_ticks_to_secs() { + let time = Duration::from_secs_f64(21388.766756); + assert_eq!(time.to_90k(), 0x072bd0050); + assert_eq!(ticks_to_secs(0x072bd0050), 21388.766756); + assert_eq!(ticks_to_secs(time.to_90k()), 21388.766756); } }