use crate::error::{Error, Result};
use alloc::{string::String, vec::Vec};
use broadcast_core::timescale::Timescale;
use isobmff::emsg::{EmsgBox, PresentationTime};
pub const SCTE35_SCHEME: &str = "urn:scte:scte35:2013:bin";
pub const UNKNOWN_EVENT_DURATION: u32 = 0xFFFF_FFFF;
pub const MAX_KNOWN_EVENT_DURATION: u32 = UNKNOWN_EVENT_DURATION - 1;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct EmsgConfig {
pub timescale: Timescale,
pub presentation: PresentationTime,
pub event_duration: Option<u32>,
pub value: String,
pub id: u32,
}
impl EmsgConfig {
#[must_use]
pub fn new(
timescale: Timescale,
presentation: PresentationTime,
event_duration: Option<u32>,
value: String,
id: u32,
) -> Self {
Self {
timescale,
presentation,
event_duration,
value,
id,
}
}
}
pub fn scte35_to_emsg(splice_raw: &[u8], cfg: &EmsgConfig) -> Result<Vec<u8>> {
let PresentationTime::Absolute(presentation_time) = cfg.presentation else {
return Err(Error::Scte35EmsgVersionZero);
};
let boxx = EmsgBox {
flags: isobmff::emsg::EMSG_FLAGS,
scheme_id_uri: SCTE35_SCHEME,
value: &cfg.value,
timescale: Some(cfg.timescale),
presentation_time: PresentationTime::Absolute(presentation_time),
event_duration: cfg.event_duration.unwrap_or(UNKNOWN_EVENT_DURATION),
id: cfg.id,
message_data: splice_raw,
};
Ok(boxx.to_vec()?)
}
pub fn scte35_event_duration(emsg_bytes: &[u8]) -> Result<Option<u32>> {
let boxx = EmsgBox::parse(emsg_bytes)?;
if !boxx.is_scte35() {
return Err(Error::UnsupportedScheme {
scheme: String::from(boxx.scheme_id_uri),
});
}
Ok((boxx.event_duration != UNKNOWN_EVENT_DURATION).then_some(boxx.event_duration))
}
pub fn emsg_to_scte35(emsg_bytes: &[u8]) -> Result<Vec<u8>> {
let boxx = EmsgBox::parse(emsg_bytes)?;
if !boxx.is_scte35() {
return Err(Error::UnsupportedScheme {
scheme: String::from(boxx.scheme_id_uri),
});
}
Ok(boxx.message_data.to_vec())
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::string::ToString;
use test_harness::values::timescale;
fn splice_2002() -> alloc::vec::Vec<u8> {
test_fixtures::read("scte35/splice-insert-2002.bin")
}
#[test]
fn scte35_unknown_duration_writes_all_ones_and_reads_back_as_unknown() {
let splice = splice_2002();
let cfg = EmsgConfig {
timescale: timescale(90_000),
presentation: PresentationTime::Absolute(90_000),
event_duration: None,
value: "1".to_string(),
id: 1,
};
let emsg = scte35_to_emsg(&splice, &cfg).unwrap();
let parsed = isobmff::emsg::EmsgBox::parse(&emsg).unwrap();
assert_eq!(parsed.event_duration, u32::MAX);
assert_eq!(scte35_event_duration(&emsg).unwrap(), None);
}
#[test]
fn scte35_to_emsg_refuses_a_version_zero_presentation_time() {
let splice = splice_2002();
let cfg = EmsgConfig {
timescale: timescale(90_000),
presentation: PresentationTime::Delta(0),
event_duration: Some(2_160_000),
value: "1".to_string(),
id: 1,
};
assert_eq!(
scte35_to_emsg(&splice, &cfg),
Err(Error::Scte35EmsgVersionZero)
);
}
#[test]
fn scte35_to_emsg_emits_version_one() {
let splice = splice_2002();
let cfg = EmsgConfig {
timescale: timescale(90_000),
presentation: PresentationTime::Absolute(0),
event_duration: Some(2_160_000),
value: "1".to_string(),
id: 1,
};
let emsg = scte35_to_emsg(&splice, &cfg).unwrap();
assert_eq!(
isobmff::emsg::EmsgBox::parse(&emsg)
.unwrap()
.presentation_time,
PresentationTime::Absolute(0)
);
}
#[test]
fn scte35_to_emsg_embeds_splice_verbatim_then_round_trips() {
let splice = splice_2002();
let cfg = EmsgConfig {
timescale: timescale(90_000),
presentation: PresentationTime::Absolute(0),
event_duration: Some(2_160_000),
value: "1".to_string(),
id: 1,
};
let emsg = scte35_to_emsg(&splice, &cfg).unwrap();
let extracted = emsg_to_scte35(&emsg).unwrap();
assert_eq!(extracted, splice);
}
}