moq_mux/catalog/
timebase.rs1use std::sync::{Arc, OnceLock};
2
3use super::Producer;
4use super::hang::CatalogExt;
5
6pub struct Timebase<E: CatalogExt = ()> {
24 pub(super) catalog: Producer<E>,
25 offset: Arc<OnceLock<Offset>>,
26}
27
28impl<E: CatalogExt> Timebase<E> {
29 pub(super) fn new(catalog: Producer<E>) -> Self {
30 Self {
31 catalog,
32 offset: Default::default(),
33 }
34 }
35
36 pub fn place(&self, pts: moq_net::Timestamp, wall: kio::clock::Wall) -> crate::Result<()> {
45 self.catalog.clone().anchor(&self.offset, pts, Some(wall))?;
46 Ok(())
47 }
48
49 pub(crate) fn anchor(&self, pts: moq_net::Timestamp) -> crate::Result<Offset> {
53 match self.offset.get() {
54 Some(offset) => Ok(*offset),
55 None => self.catalog.clone().anchor(&self.offset, pts, None),
56 }
57 }
58
59 pub(crate) fn offset(&self) -> Option<Offset> {
61 self.offset.get().copied()
62 }
63
64 pub fn shift(&self, pts: moq_net::Timestamp) -> crate::Result<moq_net::Timestamp> {
68 self.anchor(pts)?.apply(pts)
69 }
70}
71
72impl<E: CatalogExt> Clone for Timebase<E> {
74 fn clone(&self) -> Self {
75 Self {
76 catalog: self.catalog.clone(),
77 offset: self.offset.clone(),
78 }
79 }
80}
81
82#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
84pub(crate) struct Offset(i64);
85
86impl Offset {
87 pub(super) fn from_micros(micros: i128) -> crate::Result<Self> {
88 let micros = i64::try_from(micros).map_err(|_| moq_net::TimeOverflow)?;
89 Ok(Self(micros))
90 }
91
92 pub(crate) fn ticks(self, units_per_second: u64) -> i128 {
95 (self.0 as i128 * units_per_second as i128).div_euclid(1_000_000)
96 }
97
98 pub(crate) fn apply(self, pts: moq_net::Timestamp) -> crate::Result<moq_net::Timestamp> {
101 let scale = pts.scale().ok_or(moq_net::TimeOverflow)?;
102 if self.0 == 0 {
103 return Ok(pts);
104 }
105 let value = pts.value() as i128 + self.ticks(scale.as_u64());
106 let value = u64::try_from(value).map_err(|_| {
107 crate::Error::UnmappableTimestamp(format!(
108 "{pts:?} shifted by {} µs lands before the broadcast clock's zero",
109 self.0
110 ))
111 })?;
112 Ok(moq_net::Timestamp::new(value, scale)?)
113 }
114}
115
116#[cfg(test)]
117mod test {
118 use super::*;
119
120 fn us(micros: u64) -> moq_net::Timestamp {
121 moq_net::Timestamp::from_micros(micros).unwrap()
122 }
123
124 #[test]
125 fn an_offset_keeps_the_scale_and_refuses_below_zero() {
126 let offset = Offset(-1_500_000);
127 let pts = moq_net::Timestamp::from_scale(270_000, 90_000).unwrap();
128 assert_eq!(
129 offset.apply(pts).unwrap(),
130 moq_net::Timestamp::from_scale(135_000, 90_000).unwrap()
131 );
132 assert_eq!(offset.apply(us(2_000_000)).unwrap(), us(500_000));
133 assert!(matches!(
134 offset.apply(us(1_000_000)),
135 Err(crate::Error::UnmappableTimestamp(_))
136 ));
137 assert_eq!(Offset::default().apply(us(7)).unwrap(), us(7));
138 assert!(Offset::default().apply(moq_net::Timestamp::unscaled(7)).is_err());
139 }
140
141 #[test]
143 fn ticks_round_down() {
144 assert_eq!(Offset(15).ticks(90_000), 1);
145 assert_eq!(Offset(-15).ticks(90_000), -2);
146 assert_eq!(Offset(1_000_000).ticks(48_000), 48_000);
147 }
148}