use serde::{Deserialize, Serialize};
pub const MOQ_EPOCH_UNIX_MILLIS: u64 = 1_577_836_800_000;
#[serde_with::skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Timeline {
pub track: String,
#[serde(default = "Timeline::default_timescale")]
pub timescale: u32,
pub wall: Option<u64>,
}
impl Timeline {
pub fn default_timescale() -> u32 {
1000
}
pub fn new(track: impl Into<String>) -> Self {
Self {
track: track.into(),
timescale: Self::default_timescale(),
wall: None,
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn defaults_timescale_to_ms() {
let json = r#"{"track":"video0.timeline"}"#;
let decoded: Timeline = serde_json::from_str(json).unwrap();
assert_eq!(decoded.track, "video0.timeline");
assert_eq!(decoded.timescale, 1000);
assert_eq!(decoded.wall, None);
}
#[test]
fn roundtrip_with_wall() {
let mut timeline = Timeline::new("audio0.timeline");
timeline.wall = Some(1_751_846_400_000);
let json = serde_json::to_string(&timeline).unwrap();
assert_eq!(
json,
r#"{"track":"audio0.timeline","timescale":1000,"wall":1751846400000}"#
);
assert_eq!(serde_json::from_str::<Timeline>(&json).unwrap(), timeline);
}
}