timed_metadata/convert/
emsg.rs1use crate::error::{Error, Result};
10use alloc::{string::String, vec::Vec};
11use broadcast_core::timescale::Timescale;
12use isobmff::emsg::{EmsgBox, PresentationTime};
13
14pub const SCTE35_SCHEME: &str = "urn:scte:scte35:2013:bin";
17
18pub const UNKNOWN_EVENT_DURATION: u32 = 0xFFFF_FFFF;
20
21pub const MAX_KNOWN_EVENT_DURATION: u32 = UNKNOWN_EVENT_DURATION - 1;
23
24#[derive(Debug, Clone)]
26#[non_exhaustive]
27pub struct EmsgConfig {
28 pub timescale: Timescale,
30 pub presentation: PresentationTime,
33 pub event_duration: Option<u32>,
36 pub value: String,
38 pub id: u32,
40}
41
42impl EmsgConfig {
43 #[must_use]
45 pub fn new(
46 timescale: Timescale,
47 presentation: PresentationTime,
48 event_duration: Option<u32>,
49 value: String,
50 id: u32,
51 ) -> Self {
52 Self {
53 timescale,
54 presentation,
55 event_duration,
56 value,
57 id,
58 }
59 }
60}
61
62pub fn scte35_to_emsg(splice_raw: &[u8], cfg: &EmsgConfig) -> Result<Vec<u8>> {
68 let PresentationTime::Absolute(presentation_time) = cfg.presentation else {
69 return Err(Error::Scte35EmsgVersionZero);
70 };
71 let boxx = EmsgBox {
72 flags: isobmff::emsg::EMSG_FLAGS,
73 scheme_id_uri: SCTE35_SCHEME,
74 value: &cfg.value,
75 timescale: Some(cfg.timescale),
76 presentation_time: PresentationTime::Absolute(presentation_time),
77 event_duration: cfg.event_duration.unwrap_or(UNKNOWN_EVENT_DURATION),
78 id: cfg.id,
79 message_data: splice_raw,
80 };
81 Ok(boxx.to_vec()?)
82}
83
84pub fn scte35_event_duration(emsg_bytes: &[u8]) -> Result<Option<u32>> {
87 let boxx = EmsgBox::parse(emsg_bytes)?;
88 if !boxx.is_scte35() {
89 return Err(Error::UnsupportedScheme {
90 scheme: String::from(boxx.scheme_id_uri),
91 });
92 }
93 Ok((boxx.event_duration != UNKNOWN_EVENT_DURATION).then_some(boxx.event_duration))
94}
95
96pub fn emsg_to_scte35(emsg_bytes: &[u8]) -> Result<Vec<u8>> {
98 let boxx = EmsgBox::parse(emsg_bytes)?;
99 if !boxx.is_scte35() {
100 return Err(Error::UnsupportedScheme {
101 scheme: String::from(boxx.scheme_id_uri),
102 });
103 }
104 Ok(boxx.message_data.to_vec())
105}
106
107#[cfg(test)]
108mod tests {
109 use super::*;
110 use alloc::string::ToString;
111 use test_harness::values::timescale;
112
113 fn splice_2002() -> alloc::vec::Vec<u8> {
114 test_fixtures::read("scte35/splice-insert-2002.bin")
115 }
116
117 #[test]
118 fn scte35_unknown_duration_writes_all_ones_and_reads_back_as_unknown() {
119 let splice = splice_2002();
120 let cfg = EmsgConfig {
121 timescale: timescale(90_000),
122 presentation: PresentationTime::Absolute(90_000),
123 event_duration: None,
124 value: "1".to_string(),
125 id: 1,
126 };
127 let emsg = scte35_to_emsg(&splice, &cfg).unwrap();
128 let parsed = isobmff::emsg::EmsgBox::parse(&emsg).unwrap();
129 assert_eq!(parsed.event_duration, u32::MAX);
130 assert_eq!(scte35_event_duration(&emsg).unwrap(), None);
131 }
132
133 #[test]
134 fn scte35_to_emsg_refuses_a_version_zero_presentation_time() {
135 let splice = splice_2002();
136 let cfg = EmsgConfig {
137 timescale: timescale(90_000),
138 presentation: PresentationTime::Delta(0),
139 event_duration: Some(2_160_000),
140 value: "1".to_string(),
141 id: 1,
142 };
143 assert_eq!(
144 scte35_to_emsg(&splice, &cfg),
145 Err(Error::Scte35EmsgVersionZero)
146 );
147 }
148
149 #[test]
150 fn scte35_to_emsg_emits_version_one() {
151 let splice = splice_2002();
152 let cfg = EmsgConfig {
153 timescale: timescale(90_000),
154 presentation: PresentationTime::Absolute(0),
155 event_duration: Some(2_160_000),
156 value: "1".to_string(),
157 id: 1,
158 };
159 let emsg = scte35_to_emsg(&splice, &cfg).unwrap();
160 assert_eq!(
161 isobmff::emsg::EmsgBox::parse(&emsg)
162 .unwrap()
163 .presentation_time,
164 PresentationTime::Absolute(0)
165 );
166 }
167
168 #[test]
169 fn scte35_to_emsg_embeds_splice_verbatim_then_round_trips() {
170 let splice = splice_2002();
171 let cfg = EmsgConfig {
172 timescale: timescale(90_000),
173 presentation: PresentationTime::Absolute(0),
174 event_duration: Some(2_160_000),
175 value: "1".to_string(),
176 id: 1,
177 };
178 let emsg = scte35_to_emsg(&splice, &cfg).unwrap();
179 let extracted = emsg_to_scte35(&emsg).unwrap();
181 assert_eq!(extracted, splice);
182 }
183}