timed-metadata 0.13.0

Convert DPI / timed-metadata signalling between SCTE-35, HLS EXT-X-DATERANGE and DASH emsg. no_std.
Documentation
//! Stateful conversion session: holds the wall-clock anchor and unrolls 33-bit
//! PTS wrap across a stream of events.
use crate::anchor::TimeAnchor;
use crate::convert::{EmsgConfig, scte35_to_daterange, scte35_to_emsg};
use crate::error::{Error, Result};
use crate::event::{MediaTime, TimedEvent};
use alloc::vec::Vec;
use broadcast_common::modular::{Modulus, Unwrapper};
use broadcast_core::traits::Parse;
use broadcast_hls::DateRange;
use scte35_splice::SpliceInfoSection;

/// The 33-bit PTS modulus.
pub const PTS_WRAP: u64 = broadcast_common::clock33::WRAP_33BIT;

/// A stateful conversion session.
#[derive(Debug, Default)]
pub struct Timeline {
    anchor: Option<TimeAnchor>,
    unroller: PtsUnroller,
}

impl Timeline {
    /// New session with a wall-clock anchor (the one constructor; start from
    /// [`Timeline::default`] for a session that is anchored later with
    /// [`Timeline::set_anchor`]).
    pub fn new(anchor: TimeAnchor) -> Self {
        Timeline {
            anchor: Some(anchor),
            unroller: PtsUnroller::seeded(anchor.pts_90k),
        }
    }

    /// Set or replace the wall-clock anchor, keeping the PTS unroll state of the
    /// events already pushed.
    pub fn set_anchor(&mut self, anchor: TimeAnchor) {
        if self.unroller.timeline.position().is_none() {
            self.unroller = PtsUnroller::seeded(anchor.pts_90k);
        }
        self.anchor = Some(anchor);
    }

    /// Parse a SCTE-35 section; unroll its PTS into an absolute [`MediaTime`].
    pub fn push_scte35(&mut self, bytes: &[u8]) -> Result<TimedEvent> {
        let section = SpliceInfoSection::parse(bytes)?;
        let mut ev = TimedEvent::from_scte35(&section, bytes)?;
        if let Some(at) = ev.at {
            let abs = self.unroller.unroll(at.ticks_90khz());
            ev.at = Some(MediaTime::from_90khz_ticks(abs));
        }
        Ok(ev)
    }

    /// Convert to a DATERANGE (requires an anchor).
    pub fn to_daterange(&self, ev: &TimedEvent) -> Result<DateRange> {
        let anchor = self.anchor.as_ref().ok_or(Error::MissingAnchor)?;
        scte35_to_daterange(ev, anchor)
    }

    /// Convert to a serialized SCTE-35 `emsg` box.
    pub fn to_emsg(&self, ev: &TimedEvent, cfg: &EmsgConfig) -> Result<Vec<u8>> {
        match &ev.source {
            crate::event::SourcePayload::Scte35 { raw } => scte35_to_emsg(raw, cfg),
            crate::event::SourcePayload::Emsg { .. } => Err(Error::AttrParse(
                alloc::string::String::from("event is not SCTE-35-sourced"),
            )),
        }
    }
}

/// Per-signal 33-bit PTS unroller: turns a repeating 90 kHz wire counter into
/// an absolute, ever-growing tick value.
///
/// Thin stateful wrapper around
/// [`broadcast_common::clock33::unwrap_delta`] — the actual wrap-correction
/// math lives there (shared with `transmux`'s demux-edge unroller and
/// `media-doctor`/`compliance-probe`'s wrap-aware comparisons), so a fix
/// there reaches every 33-bit clock consumer in the workspace instead of
/// just this one.
///
/// SCTE-35 cues and caption commit events never legitimately reorder across
/// the 33-bit origin before any sample has been observed (there is no prior
/// sample to wrap from), so `unroll` never sees a negative accumulator in
/// practice; the `max(0)` below is a defensive clamp against malformed input
/// rather than a real code path.
#[derive(Debug)]
pub struct PtsUnroller {
    timeline: Unwrapper,
}

/// The 33-bit PTS counter space.
const PTS_SPACE: Modulus = Modulus::new::<{ PTS_WRAP.trailing_zeros() }>();

impl Default for PtsUnroller {
    fn default() -> Self {
        PtsUnroller {
            timeline: Unwrapper::new(PTS_SPACE),
        }
    }
}

impl PtsUnroller {
    /// An unroller seeded at `origin` (wrap-unrolled 90 kHz ticks).
    #[must_use]
    pub fn seeded(origin: u64) -> Self {
        PtsUnroller {
            timeline: Unwrapper::seeded(PTS_SPACE, i128::from(origin)),
        }
    }

    /// Unroll one raw 33-bit PTS onto this unroller's ever-growing timeline.
    pub fn unroll(&mut self, raw33: u64) -> u64 {
        self.timeline.unwrap(raw33).max(0) as u64
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_cue_just_before_the_origin_clamps_to_the_origin() {
        let mut u = PtsUnroller::seeded(0);
        assert_eq!(u.unroll(PTS_WRAP - 90_000), 0);
    }

    fn splice_2002() -> alloc::vec::Vec<u8> {
        test_fixtures::read("scte35/splice-insert-2002.bin")
    }

    #[test]
    fn push_scte35_returns_event() {
        let mut tl = Timeline::default();
        let ev = tl.push_scte35(&splice_2002()).unwrap();
        assert_eq!(ev.id, Some(2002));
    }

    #[test]
    fn to_daterange_without_anchor_errors() {
        let tl = Timeline::default();
        let ev = Timeline::default().push_scte35(&splice_2002()).unwrap();
        assert!(matches!(
            tl.to_daterange(&ev),
            Err(crate::Error::MissingAnchor)
        ));
    }

    #[test]
    fn set_anchor_adds_and_replaces_the_anchor_like_with_anchor() {
        let first = TimeAnchor {
            pts_90k: 0,
            utc_epoch_ms: 1_700_000_000_000,
        };
        let second = TimeAnchor {
            pts_90k: 0,
            utc_epoch_ms: 1_800_000_000_000,
        };
        let mut tl = Timeline::default();
        let ev = tl.push_scte35(&splice_2002()).unwrap();
        assert!(matches!(
            tl.to_daterange(&ev),
            Err(crate::Error::MissingAnchor)
        ));

        tl.set_anchor(first);
        let anchored = tl.to_daterange(&ev).unwrap();
        let expected = Timeline::new(first).to_daterange(&ev).unwrap();
        assert_eq!(anchored.start(), expected.start());

        tl.set_anchor(second);
        let replaced = tl.to_daterange(&ev).unwrap();
        assert_eq!(
            replaced.start(),
            Timeline::new(second).to_daterange(&ev).unwrap().start()
        );
        assert_ne!(replaced.start(), anchored.start());
    }

    #[test]
    fn wrap_unroll_adds_one_epoch() {
        // A near-max previous value then a small next value crosses one wrap.
        let mut u = PtsUnroller::seeded((1u64 << 33) - 10);
        assert_eq!(u.unroll(5), 5 + (1u64 << 33));
    }

    #[test]
    fn wrap_unroll_forward_delta_keeps_epoch() {
        // A normal forward delta within range must NOT bump the epoch.
        let mut u = PtsUnroller::seeded(1_000);
        assert_eq!(u.unroll(2_000), 2_000);
        // First call (no prior pts) returns the raw value.
        let mut u2 = PtsUnroller::default();
        assert_eq!(u2.unroll(42), 42);
    }

    /// MUTATION VERIFIED: the previous `unroll_pts` (a forward-only epoch
    /// counter) could not distinguish a small backward reorder that
    /// straddles the wrap origin from a huge forward jump — see
    /// `broadcast_common::clock33::unwrap_delta`'s doc comment for the exact
    /// case. Reproduced here at the `PtsUnroller` level: a splice/caption
    /// event at raw tick `2`, then one at raw tick `2^33 - 3` (a legitimate
    /// 5-tick backward step across the origin), must unroll to a small
    /// value, not to `~2^33`.
    #[test]
    fn wrap_unroll_backward_reorder_across_origin_does_not_leap_forward() {
        let mut u = PtsUnroller::default();
        assert_eq!(u.unroll(2), 2);
        let second = u.unroll((1u64 << 33) - 3);
        assert!(
            second < 1000,
            "expected a small value from a 5-tick backward reorder, got {second}"
        );
    }
}