Skip to main content

ssai_runtime/
lib.rs

1//! Sans-IO server-side ad insertion (SSAI) session core for SCTE 35 cues:
2//! where a break splices, which break each viewer session is in, and what
3//! that viewer's HLS playlist says about it.
4//!
5//! A cue's nominal splice time is conditioned against the real segment or
6//! keyframe boundaries, with the drift reported rather than hidden; an
7//! [`AdDecisionProvider`] the caller implements chooses the interstitial
8//! content; a bounded [`SessionStore`] keeps one small [`BreakState`] per
9//! viewer; and the playlist layer renders an HLS Interstitial
10//! (`EXT-X-DATERANGE` with `CLASS="com.apple.hls.interstitial"`, Appendix D
11//! of the HLS draft-pantos-hls-rfc8216bis dated 1 May 2026) into each
12//! viewer's media playlist. Cue semantics follow ANSI/SCTE 35 2023r1, but the
13//! core takes plain 90 kHz ticks; decoding the cue is `scte35-splice`.
14//!
15//! It builds on `broadcast-core`, `broadcast-common` (the 33-bit clock) and
16//! `broadcast-hls` (the playlist and `EXT-X-DATERANGE` model); a live origin
17//! such as `multimux` drives it, and `playout-runtime` reuses its splice-point
18//! conditioning. There is no HTTP client, no VAST or VMAP, no clock read and
19//! no media splicing (that is `transmux`). `no_std` + `alloc` without `std`.
20//!
21//! # Quickstart
22//!
23//! Condition a cue, track the break for one viewer, and render the
24//! interstitial tag for their playlist:
25//!
26//! ```rust
27//! use broadcast_common::clock33::Clock33;
28//! use ssai_runtime::{
29//!     AdBreakDecision, BreakState, InterstitialDateRange, SessionLimits, SessionStore,
30//!     condition_splice_point,
31//! };
32//! use std::time::Duration;
33//!
34//! let clock = |ticks| Clock33::try_new(ticks).unwrap();
35//! let boundaries = [clock(810_000), clock(897_000), clock(904_500)];
36//! let point =
37//!     condition_splice_point(clock(900_000), &boundaries, Duration::from_millis(60)).unwrap();
38//! assert_eq!(point.snapped_pts, clock(897_000));
39//! assert_eq!(point.delta_ticks, 3_000);
40//!
41//! let decision = AdBreakDecision::single_asset("ad-1", "https://ads.example/ad-1.m3u8");
42//! let mut store = SessionStore::new(SessionLimits {
43//!     max_sessions: 1024,
44//!     max_decision_items: 16,
45//!     max_decision_text_bytes: 4096,
46//! });
47//! store
48//!     .begin_break("viewer-42", BreakState::new(decision.clone(), point))
49//!     .unwrap();
50//!
51//! let tag = InterstitialDateRange::from_decision(&decision, "2026-01-01T00:00:10.000Z", Some(30.0))
52//!     .to_tag_line()
53//!     .unwrap();
54//! assert!(tag.contains("CLASS=\"com.apple.hls.interstitial\""));
55//! ```
56//!
57//! # How do I...
58//!
59//! - Snap a cue's splice time to a real boundary and see the drift:
60//!   [`condition_splice_point`], [`ConditionedSplicePoint::delta_ticks`],
61//!   `examples/condition_real_cue.rs`.
62//! - Plug in my ad decision service: [`AdDecisionProvider`],
63//!   [`BreakContext`], [`AdBreakDecision`], `examples/session_playlist.rs`.
64//! - Track which break a viewer session is in: [`SessionStore`],
65//!   [`BreakState`], [`SessionLimits`], `examples/session_playlist.rs`.
66//! - Render one viewer's playlist with the interstitial tag:
67//!   [`render_session_playlist`], [`InterstitialDateRange`],
68//!   `examples/session_playlist.rs`.
69//! - Render the shared playlist once and splice each viewer's tag in:
70//!   [`SessionPlaylistBase`], `examples/many_viewers.rs`.
71//! - Point a break at an asset list instead of one asset, and set snap
72//!   rules: [`AssetSource`], [`SnapMode`], `examples/many_viewers.rs`.
73//! - Resume, end and expire breaks on my own clock:
74//!   [`SessionStore::resume`], [`SessionStore::end`],
75//!   [`SessionStore::advance`], [`SessionStore::expire_idle`],
76//!   `examples/many_viewers.rs`.
77//! - Read an interstitial tag back: [`InterstitialDateRange::parse_tag_line`],
78//!   `examples/many_viewers.rs`.
79//!
80//! # Modules
81//!
82//! - [`splice`]: splice-point conditioning on the 33-bit PTS circle.
83//! - [`decision`]: the ad-decision extension point and the decision model.
84//! - [`session`]: the bounded per-session break store.
85//! - [`playlist`]: the Interstitial `EXT-X-DATERANGE` and per-viewer
86//!   playlist rendering.
87//! - [`error`]: [`Error`] and [`Result`].
88//!
89//! # Feature flags
90//!
91//! - `std` (default): link the standard library; without it the crate is
92//!   `no_std` + `alloc`.
93//! - `serde`: serde derives on the decision and break-state types.
94//!
95//! # Spec citations
96//!
97//! draft-pantos-hls-rfc8216bis Appendix D §D.2 and §4.4.5.1
98//! (`private/specs-verified/hls/ietf/rfc8216bis-interstitials.md`); ANSI/SCTE
99//! 35 2023r1 `splice_insert()` and `break_duration()`
100//! (`private/specs-verified/scte/scte35-splice/`).
101#![no_std]
102#![forbid(unsafe_code)]
103#![cfg_attr(docsrs, feature(doc_cfg))]
104#![deny(missing_docs)]
105
106extern crate alloc;
107
108#[cfg(doctest)]
109#[doc = include_str!("../README.md")]
110struct ReadmeDoctests;
111
112pub mod decision;
113pub mod error;
114pub mod playlist;
115pub mod session;
116pub mod splice;
117
118pub use decision::{
119    AdBreakDecision, AdDecisionProvider, AssetSource, BreakContext, RestrictMode, SnapMode,
120};
121pub use error::{Error, Result};
122pub use playlist::{
123    INTERSTITIAL_CLASS, InterstitialDateRange, SessionPlaylistBase, render_session_playlist,
124};
125pub use session::{BreakState, SessionLimits, SessionStore};
126pub use splice::{ConditionedSplicePoint, SnapDirection, condition_splice_point};