timed_metadata/lib.rs
1//! Lossless conversion of DPI / timed-metadata signalling between SCTE-35,
2//! HLS `#EXT-X-DATERANGE` and DASH `emsg` (ISO/IEC 23009-1 §5.10.3.3,
3//! ANSI/SCTE 214-1:2024 §7.7). `no_std` + `alloc`; it depends on
4//! `broadcast-common`, `broadcast-core`, `broadcast-hls`, `isobmff`, `jiff`
5//! and `scte35-splice`. `caption-convert`, `transmux`, `hls-runtime`,
6//! `media-plane`, `multimux`, `media-doctor` and `compliance-probe` use it.
7//! The original `splice_info_section` bytes are carried verbatim, so an
8//! event can always be rendered back to its source form.
9//!
10//! # Quickstart
11//!
12//! Take one real SCTE-35 cue, render it as an HLS date range and a DASH
13//! `emsg`, then recover the original bytes from the `emsg`.
14//!
15//! ```rust
16//! use broadcast_core::timescale::Timescale;
17//! use isobmff::emsg::PresentationTime;
18//! use timed_metadata::convert::{EmsgConfig, emsg_to_scte35};
19//! use timed_metadata::{TimeAnchor, Timeline};
20//!
21//! let hex = "FC302100000000000000FFF01005000007D27FEF7F7E0020F580C0000000000088B9661D";
22//! let raw: Vec<u8> = (0..hex.len())
23//! .step_by(2)
24//! .map(|index| u8::from_str_radix(&hex[index..index + 2], 16).unwrap())
25//! .collect();
26//! let anchor = TimeAnchor { pts_90k: 0, utc_epoch_ms: 1_705_320_000_000 };
27//! let mut timeline = Timeline::new(anchor);
28//! let event = timeline.push_scte35(&raw).unwrap();
29//!
30//! let line = timeline.to_daterange(&event).unwrap().to_tag_line().unwrap();
31//! assert!(line.starts_with("#EXT-X-DATERANGE:"));
32//!
33//! let config = EmsgConfig::new(
34//! Timescale::try_new(90_000).expect("a non-zero timescale"),
35//! PresentationTime::Absolute(90_000),
36//! Some(24 * 90_000),
37//! "34".to_string(),
38//! event.id.unwrap_or(0),
39//! );
40//! let emsg = timeline.to_emsg(&event, &config).unwrap();
41//! assert_eq!(emsg_to_scte35(&emsg).unwrap(), raw);
42//! ```
43//!
44//! # How do I...
45//!
46//! - Convert SCTE-35 to a date range: [`convert::scte35_to_daterange`] or
47//! [`Timeline::to_daterange`], `examples/scte35_to_hls.rs`.
48//! - Convert SCTE-35 to a DASH `emsg`: [`convert::scte35_to_emsg`] or
49//! [`Timeline::to_emsg`], `examples/scte35_to_dash.rs`.
50//! - Recover SCTE-35 bytes from an `emsg`: [`convert::emsg_to_scte35`],
51//! `examples/scte35_to_dash.rs`.
52//! - Re-time an `emsg` between versions 0 and 1: [`convert::emsg_to_v0`] and
53//! [`convert::emsg_to_v1`], `examples/scte35_to_dash.rs`.
54//! - Map PTS to UTC and unwrap 33-bit timestamps: [`Timeline`] and
55//! [`TimeAnchor`], `examples/scte35_to_hls.rs`.
56//! - Unroll PTS outside a session: [`PtsUnroller`], `examples/scte35_to_hls.rs`.
57//! - Read a neutral event's id, kind and source: [`TimedEvent`] and
58//! [`SourcePayload`], `examples/scte35_to_hls.rs`.
59//! - Handle typed conversion failures: [`Error`], `examples/scte35_to_hls.rs`.
60//!
61//! # Modules
62//!
63//! Session: [`timeline`] (PTS/UTC timeline and event session), [`anchor`]
64//! (PTS-to-wall-clock anchor).
65//!
66//! Conversion: [`convert`] (pure format conversions and their configuration),
67//! [`event`] (neutral event model).
68//!
69//! Shared vocabulary: [`error`] (typed conversion failures).
70//!
71//! # Feature flags
72//!
73//! - `std` (default): `std::error::Error` impls and `std` in dependencies.
74//! - `serde`: `Serialize`/`Deserialize` on supported public types.
75//!
76//! # Spec citations
77//!
78//! ANSI/SCTE 35 §7.7 (`splice_info_section`), ANSI/SCTE 214-1:2024 §7.7.1(2),
79//! §7.7.3 (`private/specs-verified/scte/dash/scte-214-1-2024.md`); RFC 8216bis
80//! §4.4.5.1 (`EXT-X-DATERANGE`); ISO/IEC 23009-1 §5.10.3.3 (`emsg`).
81#![deny(missing_docs)]
82#![no_std]
83#![forbid(unsafe_code)]
84#![cfg_attr(docsrs, feature(doc_cfg))]
85
86extern crate alloc;
87
88#[cfg(doctest)]
89#[doc = include_str!("../README.md")]
90struct ReadmeDoctests;
91
92pub mod anchor;
93pub mod convert;
94pub mod error;
95pub mod event;
96pub mod timeline;
97
98pub use anchor::TimeAnchor;
99pub use error::{Error, Result};
100pub use event::{EventKind, MediaDuration, MediaTime, SourcePayload, TimedEvent};
101pub use timeline::{PtsUnroller, Timeline};