Skip to main content

timed_metadata/
lib.rs

1//! Timed-metadata / DPI signalling conversion core.
2//!
3//! Translates SCTE-35 splice information to the carriages used in OTT
4//! delivery: HLS `EXT-X-DATERANGE` (RFC 8216 / draft-pantos-hls-rfc8216bis
5//! §4.4.5.1) and DASH `emsg` (ANSI/SCTE 214-3 §8.3.3, scheme
6//! `urn:scte:scte35:2013:bin`).
7//! The DASH `emsg` conversion is bidirectional (SCTE-35 to and from `emsg`);
8//! the HLS `EXT-X-DATERANGE` conversion is currently one-way (SCTE-35 to
9//! DATERANGE only) — the reverse edge is out of scope for now, see
10//! [`convert::scte35_to_daterange`] doc.
11//!
12//! Conversions are lossless: the original `splice_info_section` bytes are
13//! carried verbatim (DATERANGE `SCTE35-OUT` hex, emsg `message_data`).
14//!
15//! Pure functions live in [`convert`]; the stateful [`Timeline`] session adds a
16//! wall-clock [`TimeAnchor`] and 33-bit PTS wrap-unrolling.
17//!
18//! [`webvtt`] converts a different kind of timed metadata — CEA-608/708
19//! closed captions — to WebVTT cues (W3C WebVTT + RFC 8216 §3.5
20//! `X-TIMESTAMP-MAP`), feature `cc-data` for the CEA-608/708 extraction half.
21//! Lossy by design (see its module docs).
22//!
23//! # What it does / what it does not
24//!
25//! The original `splice_info_section` bytes travel verbatim, so nothing is
26//! re-encoded. It does **not** parse SCTE-35 itself (`scte35-splice`), parse or
27//! write whole HLS playlists (`broadcast-hls`) or MP4 files, or make ad
28//! decisions. Caption conversion is lossy (no placement or styling); for SRT /
29//! TTML use the `caption-convert` crate.
30//!
31//! # Standards supported
32//!
33//! | Standard | Edition | Scope | Status |
34//! |---|---|---|---|
35//! | ANSI/SCTE 35 | 2023r1 (via `scte35-splice`) | section carried verbatim | Full |
36//! | RFC 8216 / draft-pantos-hls-rfc8216bis | draft -22 | §4.4.5.1 `EXT-X-DATERANGE` | Partial (SCTE-35 to DATERANGE, tag-line parse/render; no DATERANGE to SCTE-35) |
37//! | ANSI/SCTE 214-3 | unverified (not vendored) | §8.3.3 inband `emsg`, `urn:scte:scte35:2013:bin` | Full (binary scheme only) |
38//! | ISO/IEC 23009-1 | unverified | §5.10.3.3 `emsg`, v0 <-> v1 re-timing | Full |
39//! | W3C WebVTT + RFC 8216 §3.5 | curated rules | cue syntax, `X-TIMESTAMP-MAP` | Partial |
40//! | CTA-608-E / CTA-708-E (decode in `cc-data`) | CTA not vendored (708 cross-source: GY/T 270-2013, vendored, a Chinese adaptation) | CC1, 708 service | Partial, feature `cc-data` |
41//! | ETSI EN 300 706 / EN 301 775 §4.5 | V1.2.1 | Level-1 subtitle pages | Partial, feature `teletext` |
42//!
43//! | From \ To | `EXT-X-DATERANGE` | `emsg` | SCTE-35 bytes |
44//! |---|---|---|---|
45//! | SCTE-35 | [`convert::scte35_to_daterange`] | [`convert::scte35_to_emsg`] | identity |
46//! | `emsg` | not provided | [`convert::emsg_to_v0`] / [`convert::emsg_to_v1`] | [`convert::emsg_to_scte35`] |
47//! | `EXT-X-DATERANGE` | [`DateRange::parse_tag_line`] / [`DateRange::to_tag_line`] | not provided | raw bytes on the parsed [`DateRange`] |
48//!
49//! # Feature flags
50//!
51//! | Feature | Default | Effect |
52//! |---|---|---|
53//! | `std` | on | `std` in all dependencies; off = `no_std` + `alloc` |
54//! | `serde` | on | serde support on public types |
55//! | `chrono` | on | `chrono` dependency |
56//! | `cc-data` | off | CEA-608/708 to WebVTT extractors |
57//! | `teletext` | off | EBU Teletext to WebVTT extractor |
58//!
59//! # Quick start
60//!
61//! ```
62//! use mp4_emsg::PresentationTime;
63//! use timed_metadata::convert::{EmsgConfig, emsg_to_scte35};
64//! use timed_metadata::{TimeAnchor, Timeline};
65//!
66//! // A real Unified Streaming splice_info_section (splice id 2002, 24 s break).
67//! let hex = "FC302100000000000000FFF01005000007D27FEF7F7E0020F580C0000000000088B9661D";
68//! let raw: Vec<u8> = (0..hex.len())
69//!     .step_by(2)
70//!     .map(|i| u8::from_str_radix(&hex[i..i + 2], 16).unwrap())
71//!     .collect();
72//!
73//! // Wall-clock anchor: PTS 0 == 2024-01-15T12:00:00 UTC.
74//! let anchor = TimeAnchor { pts_90k: 0, utc_epoch_ms: 1_705_320_000_000 };
75//! let mut timeline = Timeline::with_anchor(anchor);
76//! let event = timeline.push_scte35(&raw).unwrap();
77//!
78//! // Edge 1: SCTE-35 -> HLS EXT-X-DATERANGE.
79//! let line = timeline.to_daterange(&event).unwrap().to_tag_line().unwrap();
80//! assert!(line.starts_with("#EXT-X-DATERANGE:"));
81//! assert!(line.contains("SCTE35-"));
82//!
83//! // Edge 2: SCTE-35 -> DASH emsg, and Edge 3: back to the verbatim section.
84//! let cfg = EmsgConfig {
85//!     timescale: 90_000,
86//!     presentation: PresentationTime::Delta(0),
87//!     event_duration: 2_160_000, // 24 s * 90000
88//!     value: "34".to_string(),
89//!     id: event.id.unwrap_or(0),
90//! };
91//! let emsg = timeline.to_emsg(&event, &cfg).unwrap();
92//! assert_eq!(emsg_to_scte35(&emsg).unwrap(), raw);
93//! ```
94//!
95//! # Design notes and related crates
96//!
97//! `#![forbid(unsafe_code)]`, `no_std` + `alloc` without `std`, `Error` is
98//! `#[non_exhaustive]`, checked time arithmetic. Depends on `scte35-splice`,
99//! `mp4-emsg`, `broadcast-hls`, `broadcast-common`, optionally `cc-data` /
100//! `dvb-vbi`; used by `caption-convert`, `transmux`, `hls-runtime`,
101//! `media-plane` and `multimux`. MSRV 1.95.0; MIT OR Apache-2.0.
102#![no_std]
103#![forbid(unsafe_code)]
104#![cfg_attr(docsrs, feature(doc_cfg))]
105
106extern crate alloc;
107
108pub mod anchor;
109pub mod convert;
110pub mod daterange;
111pub mod error;
112pub mod event;
113pub mod timeline;
114pub mod webvtt;
115
116pub use anchor::TimeAnchor;
117pub use daterange::DateRange;
118pub use error::{Error, Result};
119pub use event::{EventKind, MediaDuration, MediaTime, SourcePayload, TimedEvent};
120pub use timeline::Timeline;
121
122/// 90 kHz — the SCTE-35 / MPEG-2 PTS clock.
123pub const PTS_HZ: u64 = 90_000;