Skip to main content

timed_metadata/convert/
emsg.rs

1//! SCTE-35 ↔ DASH `emsg` conversion (scheme `urn:scte:scte35:2013:bin`).
2//!
3//! Carriage is **ANSI/SCTE 214-3 §8.3.3** for the inband `emsg` form used
4//! here. (The MPD `EventStream` form, `urn:scte:scte35:2014:xml+bin`, is
5//! SCTE 214-1 §6.8.4 — a different part for a different carriage.)
6//!
7//! #951 recorded this crate and `mp4-emsg` citing contradictory parts. This
8//! crate was right. Settled from DASH-IF IOP v4.3 §5.4.3 p.163, which is
9//! freely published and states the split explicitly, so no paywalled SCTE
10//! document was needed after all.
11use crate::error::{Error, Result};
12use alloc::{string::String, vec::Vec};
13use broadcast_common::timescale::Timescale;
14use mp4_emsg::{EmsgBox, PresentationTime};
15
16/// The SCTE-35 binary carriage scheme for inband DASH `emsg`
17/// (ANSI/SCTE 214-3 §8.3.3).
18pub const SCTE35_SCHEME: &str = "urn:scte:scte35:2013:bin";
19
20/// Parameters for emitting a SCTE-35-carrying `emsg`.
21#[derive(Debug, Clone)]
22#[non_exhaustive]
23pub struct EmsgConfig {
24    /// `timescale` (ticks/second) for the emsg time fields.
25    pub timescale: Timescale,
26    /// `presentation_time_delta` (v0) or `presentation_time` (v1).
27    pub presentation: PresentationTime,
28    /// `event_duration` in `timescale` units (0 if unknown).
29    pub event_duration: u32,
30    /// `value` string (often the segmentation type id, as text).
31    pub value: String,
32    /// `id` — unique event identifier (u32).
33    pub id: u32,
34}
35
36impl EmsgConfig {
37    /// Build the emsg parameters from their required values.
38    #[must_use]
39    pub fn new(
40        timescale: Timescale,
41        presentation: PresentationTime,
42        event_duration: u32,
43        value: String,
44        id: u32,
45    ) -> Self {
46        Self {
47            timescale,
48            presentation,
49            event_duration,
50            value,
51            id,
52        }
53    }
54}
55
56/// Wrap a verbatim `splice_info_section` as a SCTE-35 `emsg` box (serialized bytes).
57pub fn scte35_to_emsg(splice_raw: &[u8], cfg: &EmsgConfig) -> Result<Vec<u8>> {
58    let boxx = EmsgBox {
59        flags: mp4_emsg::EMSG_FLAGS,
60        scheme_id_uri: SCTE35_SCHEME,
61        value: &cfg.value,
62        timescale: cfg.timescale,
63        presentation_time: cfg.presentation,
64        event_duration: cfg.event_duration,
65        id: cfg.id,
66        message_data: splice_raw,
67    };
68    Ok(boxx.to_vec()?)
69}
70
71/// Extract the verbatim `splice_info_section` from a SCTE-35 `emsg` box.
72pub fn emsg_to_scte35(emsg_bytes: &[u8]) -> Result<Vec<u8>> {
73    let boxx = EmsgBox::parse(emsg_bytes)?;
74    if !boxx.is_scte35() {
75        return Err(Error::UnsupportedScheme {
76            scheme: String::from(boxx.scheme_id_uri),
77        });
78    }
79    Ok(boxx.message_data.to_vec())
80}
81
82#[cfg(test)]
83mod tests {
84    fn timescale(value: u32) -> Timescale {
85        Timescale::try_new(value).expect("a non-zero timescale")
86    }
87
88    use super::*;
89    use alloc::string::ToString;
90
91    fn splice_2002() -> alloc::vec::Vec<u8> {
92        test_fixtures::read("scte35/splice-insert-2002.bin")
93    }
94
95    #[test]
96    fn scte35_to_emsg_embeds_splice_verbatim_then_round_trips() {
97        let splice = splice_2002();
98        let cfg = EmsgConfig {
99            timescale: timescale(90_000),
100            presentation: PresentationTime::Delta(0),
101            event_duration: 2_160_000,
102            value: "1".to_string(),
103            id: 1,
104        };
105        let emsg = scte35_to_emsg(&splice, &cfg).unwrap();
106        // message_data must equal the splice verbatim:
107        let extracted = emsg_to_scte35(&emsg).unwrap();
108        assert_eq!(extracted, splice);
109    }
110}