use std::time::Duration;
#[derive(Debug, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct SpliceTime {
pub time_specified_flag: u8,
pub pts_time: Option<u64>,
}
impl SpliceTime {
pub fn to_duration(&self) -> Option<Duration> {
self.pts_time.map(|pts| {
let seconds = pts / 90_000;
let nanos = ((pts % 90_000) * 1_000_000_000) / 90_000;
Duration::new(seconds, nanos as u32)
})
}
}
#[derive(Debug, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct BreakDuration {
pub auto_return: u8,
pub reserved: u8,
pub duration: u64,
}
impl BreakDuration {
pub fn to_duration(&self) -> Duration {
let seconds = self.duration / 90_000;
let nanos = ((self.duration % 90_000) * 1_000_000_000) / 90_000;
Duration::new(seconds, nanos as u32)
}
}
impl From<BreakDuration> for Duration {
fn from(break_duration: BreakDuration) -> Self {
break_duration.to_duration()
}
}
impl From<&BreakDuration> for Duration {
fn from(break_duration: &BreakDuration) -> Self {
break_duration.to_duration()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_splice_time_to_duration() {
let splice_time = SpliceTime {
time_specified_flag: 1,
pts_time: Some(90_000), };
assert_eq!(splice_time.to_duration(), Some(Duration::from_secs(1)));
let splice_time = SpliceTime {
time_specified_flag: 0,
pts_time: None,
};
assert_eq!(splice_time.to_duration(), None);
let splice_time = SpliceTime {
time_specified_flag: 1,
pts_time: Some(135_000), };
assert_eq!(splice_time.to_duration(), Some(Duration::from_millis(1500)));
}
#[test]
fn test_break_duration_to_duration() {
let break_duration = BreakDuration {
auto_return: 1,
reserved: 0,
duration: 2_700_000, };
assert_eq!(break_duration.to_duration(), Duration::from_secs(30));
let duration: Duration = break_duration.into();
assert_eq!(duration, Duration::from_secs(30));
let break_duration_ref = &BreakDuration {
auto_return: 0,
reserved: 0,
duration: 450_000, };
let duration: Duration = break_duration_ref.into();
assert_eq!(duration, Duration::from_secs(5));
}
}