Skip to main content

playout_runtime/
lib.rs

1//! Sans-IO linear channel playout: a schedule of programmes, ads and slates,
2//! the plan for each join between two sources, and the SCTE 35
3//! `splice_insert()` each break edge implies.
4//!
5//! A channel is an ordered [`Schedule`]; [`next_transition`] finds the next
6//! join and plans it: the channel-timeline instant, the PTS-rebase offset
7//! that lands the incoming source's clock continuously on the channel
8//! timeline (unwrapping a 33-bit source PTS when asked), and whether a codec
9//! configuration change must be signalled as a discontinuity.
10//! [`build_splice_insert`] turns a break edge into an ANSI/SCTE 35 2023r1
11//! `splice_insert()` (§9.7.3, Table 10) whose splice time is conditioned
12//! against real boundaries by `ssai-runtime`, and [`to_section`] wraps it in
13//! a clear `splice_info_section` built by `scte35-splice`.
14//!
15//! The schedule is an in-memory model of this crate (no wire format, and
16//! SCTE 224 is deliberately not adopted). The crate computes numbers and
17//! cues only: `transmux` applies a rebase to samples, a playlist writer acts
18//! on a discontinuity, and the caller owns the clock and the media. No HTTP,
19//! no async runtime, no transcoding; `no_std` + `alloc` without `std`.
20//!
21//! # Quickstart
22//!
23//! Schedule a programme and a 30 s ad, plan the join, and build the cue that
24//! enters the break:
25//!
26//! ```rust
27//! use broadcast_common::clock33::Clock33;
28//! use broadcast_core::timescale::Timescale;
29//! use playout_runtime::{
30//!     BreakEdge, CodecConfigId, EntryKind, Playhead, Schedule, ScheduleEntry,
31//!     build_splice_insert, next_transition,
32//! };
33//! use std::time::Duration;
34//!
35//! let mut schedule = Schedule::new();
36//! schedule
37//!     .push(ScheduleEntry {
38//!         id: "news".into(),
39//!         kind: EntryKind::Programme,
40//!         planned_start: 0,
41//!         source_start_pts: 0,
42//!         codec_config: CodecConfigId(1),
43//!     })
44//!     .unwrap();
45//! schedule
46//!     .push(ScheduleEntry {
47//!         id: "ad".into(),
48//!         kind: EntryKind::Ad,
49//!         planned_start: 54_000_000,
50//!         source_start_pts: 5_000,
51//!         codec_config: CodecConfigId(2),
52//!     })
53//!     .unwrap();
54//!
55//! let t = next_transition(&schedule, Playhead::new(0, Timescale::MICROS)).unwrap();
56//! assert_eq!(t.plan.at_pts, 54_000_000);
57//! assert!(t.plan.discontinuity);
58//! assert_eq!(t.plan.rebase(5_000), Some(54_000_000));
59//!
60//! let clock = |ticks| Clock33::try_new(ticks).unwrap();
61//! let (point, insert) = build_splice_insert(
62//!     BreakEdge::Enter,
63//!     1,
64//!     clock(t.plan.at_pts),
65//!     &[clock(53_998_000), clock(54_003_000)],
66//!     Duration::from_millis(60),
67//!     Some(Duration::from_secs(30)),
68//! )
69//! .unwrap();
70//! assert_eq!(point.snapped_pts, clock(53_998_000));
71//! assert!(insert.out_of_network_indicator);
72//! ```
73//!
74//! # How do I...
75//!
76//! - Build a channel schedule of programmes, ads and slates: [`Schedule`],
77//!   [`ScheduleEntry`], [`EntryKind`], `examples/schedule_and_transition.rs`.
78//! - Walk every join the schedule implies, with its rebase offset and
79//!   discontinuity: [`next_transition`], [`Playhead`],
80//!   `examples/schedule_and_transition.rs`.
81//! - Plan the join between two given entries: [`TransitionPlan::plan`],
82//!   `examples/rebase_and_return.rs`.
83//! - Put an incoming source's timestamps on the channel timeline, including a
84//!   33-bit PTS that wraps: [`TransitionPlan::rebase`],
85//!   [`TransitionPlan::rebase_wrapped`], `examples/rebase_and_return.rs`.
86//! - Ask which entry is on air at an instant: [`Schedule::active_at`],
87//!   `examples/rebase_and_return.rs`.
88//! - Emit the `splice_insert()` that enters a break, snapped to a real
89//!   keyframe, and serialise it: [`build_splice_insert`], [`BreakEdge`],
90//!   [`to_section`], `examples/emit_splice_from_real_cue.rs`.
91//! - Emit the `splice_insert()` that returns to the network:
92//!   [`build_splice_insert`] with [`BreakEdge::Return`],
93//!   `examples/rebase_and_return.rs`.
94//!
95//! # Modules
96//!
97//! - [`schedule`]: the schedule model and the playhead.
98//! - [`transition`]: join planning and PTS rebasing.
99//! - [`scte35`]: the `splice_insert()` a break edge implies.
100//! - [`error`]: [`Error`] and [`Result`].
101//!
102//! # Feature flags
103//!
104//! - `std` (default): link the standard library; without it the crate is
105//!   `no_std` + `alloc`.
106//! - `serde`: serde derives on the schedule, transition and decision types.
107//!
108//! # Spec citations
109//!
110//! ANSI/SCTE 35 2023r1 §9.7.3 Table 10 (`splice_insert()`) and §9.8
111//! (`splice_time()`, `break_duration()`), transcribed in
112//! `private/specs-verified/scte/scte35-splice/`.
113#![no_std]
114#![forbid(unsafe_code)]
115#![cfg_attr(docsrs, feature(doc_cfg))]
116#![deny(missing_docs)]
117
118extern crate alloc;
119
120#[cfg(doctest)]
121#[doc = include_str!("../README.md")]
122struct ReadmeDoctests;
123
124pub mod error;
125pub mod schedule;
126pub mod scte35;
127pub mod transition;
128
129pub use error::{Error, Result};
130pub use schedule::{CodecConfigId, EntryKind, Playhead, Schedule, ScheduleEntry};
131pub use scte35::{BreakEdge, build_splice_insert, to_section};
132pub use transition::{PlannedTransition, TransitionPlan, next_transition};