use crate::error::Result;
use alloc::{string::String, vec::Vec};
use scte35_splice::{
SpliceInfoSection,
commands::AnyCommand,
descriptors::{AnySpliceDescriptor, SegmentationTypeId},
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MediaTime(pub u64);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MediaDuration(pub u64);
impl MediaDuration {
pub fn as_seconds_f64(self) -> f64 {
self.0 as f64 / crate::PTS_HZ as f64
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum EventKind {
BreakStart,
BreakEnd,
Chapter,
Unspecified,
}
impl EventKind {
pub fn name(&self) -> &'static str {
match self {
EventKind::BreakStart => "break_start",
EventKind::BreakEnd => "break_end",
EventKind::Chapter => "chapter",
EventKind::Unspecified => "unspecified",
}
}
}
broadcast_common::impl_spec_display!(EventKind);
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum SourcePayload {
Scte35 { raw: Vec<u8> },
Emsg {
scheme_id_uri: String,
value: String,
raw: Vec<u8>,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TimedEvent {
pub id: Option<u32>,
pub kind: EventKind,
pub at: Option<MediaTime>,
pub duration: Option<MediaDuration>,
pub source: SourcePayload,
}
impl TimedEvent {
pub fn from_scte35(section: &SpliceInfoSection, raw: &[u8]) -> Result<Self> {
let mut id = None;
let mut kind = EventKind::Unspecified;
let mut at = None;
let mut duration = None;
let adjust =
|pts: u64| MediaTime(broadcast_common::clock33::add(pts, section.pts_adjustment));
if let Some(clear) = §ion.clear {
match &clear.command {
AnyCommand::SpliceInsert(si) => {
id = Some(si.splice_event_id);
kind = if si.splice_event_cancel_indicator {
EventKind::Unspecified
} else if si.out_of_network_indicator {
EventKind::BreakStart
} else {
EventKind::BreakEnd
};
if let Some(st) = &si.splice_time {
at = st.pts_time.map(adjust);
}
if let Some(bd) = &si.break_duration {
duration = Some(MediaDuration(bd.duration));
}
}
AnyCommand::TimeSignal(ts) => {
at = ts.splice_time.pts_time.map(adjust);
for d in section.descriptors() {
if let Ok(AnySpliceDescriptor::Segmentation(seg)) = d {
if seg.segmentation_event_cancel_indicator {
continue;
}
id = Some(seg.segmentation_event_id);
kind = segmentation_kind(seg.segmentation_type_id);
duration = seg.segmentation_duration.map(MediaDuration);
break;
}
}
}
_ => {}
}
}
Ok(TimedEvent {
id,
kind,
at,
duration,
source: SourcePayload::Scte35 { raw: raw.to_vec() },
})
}
}
fn segmentation_kind(type_id: SegmentationTypeId) -> EventKind {
use SegmentationTypeId as T;
match type_id {
T::ProgramStart | T::ProgramEnd | T::ChapterStart | T::ChapterEnd => EventKind::Chapter,
T::BreakStart
| T::ProviderAdvertisementStart
| T::DistributorAdvertisementStart
| T::ProviderPlacementOpportunityStart
| T::DistributorPlacementOpportunityStart
| T::ProviderAdBlockStart
| T::DistributorAdBlockStart => EventKind::BreakStart,
T::BreakEnd
| T::ProviderAdvertisementEnd
| T::DistributorAdvertisementEnd
| T::ProviderPlacementOpportunityEnd
| T::DistributorPlacementOpportunityEnd
| T::ProviderAdBlockEnd
| T::DistributorAdBlockEnd => EventKind::BreakEnd,
_ => EventKind::Unspecified,
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec::Vec;
use broadcast_common::traits::Parse;
use scte35_splice::SpliceInfoSection;
fn splice_2002() -> Vec<u8> {
let hex = "FC302100000000000000FFF01005000007D27FEF7F7E0020F580C0000000000088B9661D";
(0..hex.len())
.step_by(2)
.map(|i| u8::from_str_radix(&hex[i..i + 2], 16).unwrap())
.collect()
}
#[test]
fn from_scte35_extracts_break_start_and_duration() {
let raw = splice_2002();
let section = SpliceInfoSection::parse(&raw).unwrap();
let ev = TimedEvent::from_scte35(§ion, &raw).unwrap();
assert_eq!(ev.id, Some(2002));
assert_eq!(ev.kind, EventKind::BreakStart); assert_eq!(ev.at, None); assert_eq!(ev.duration, Some(MediaDuration(2_160_000)));
assert!((ev.duration.unwrap().as_seconds_f64() - 24.0).abs() < 1e-9);
match &ev.source {
SourcePayload::Scte35 { raw: r } => assert_eq!(r, &raw), _ => panic!("expected Scte35 payload"),
}
}
#[test]
fn event_kind_labels() {
assert_eq!(EventKind::BreakStart.name(), "break_start");
assert_eq!(alloc::format!("{}", EventKind::BreakEnd), "break_end");
}
#[test]
fn segmentation_kind_classifies_by_group() {
use SegmentationTypeId as T;
let cases: &[(SegmentationTypeId, EventKind)] = &[
(T::ProgramStart, EventKind::Chapter),
(T::ProgramEnd, EventKind::Chapter),
(T::ChapterStart, EventKind::Chapter),
(T::ChapterEnd, EventKind::Chapter),
(T::BreakStart, EventKind::BreakStart),
(T::BreakEnd, EventKind::BreakEnd),
(T::ProviderAdvertisementStart, EventKind::BreakStart),
(T::DistributorAdvertisementEnd, EventKind::BreakEnd),
(T::ProviderPlacementOpportunityStart, EventKind::BreakStart),
(T::DistributorPlacementOpportunityEnd, EventKind::BreakEnd),
(T::ProviderAdBlockStart, EventKind::BreakStart),
(T::DistributorAdBlockEnd, EventKind::BreakEnd),
(T::OpeningCreditStart, EventKind::Unspecified),
(T::ClosingCreditEnd, EventKind::Unspecified),
(T::ProviderPromoStart, EventKind::Unspecified),
(T::DistributorPromoEnd, EventKind::Unspecified),
(T::UnscheduledEventStart, EventKind::Unspecified),
(T::AlternateContentOpportunityEnd, EventKind::Unspecified),
(
T::ProviderOverlayPlacementOpportunityStart,
EventKind::Unspecified,
),
(
T::DistributorOverlayPlacementOpportunityEnd,
EventKind::Unspecified,
),
(T::NetworkStart, EventKind::Unspecified),
(T::NetworkEnd, EventKind::Unspecified),
(T::Reserved(0x60), EventKind::Unspecified),
];
for (type_id, want) in cases {
assert_eq!(
segmentation_kind(*type_id),
*want,
"segmentation_type_id {type_id:?} ({}) must classify as {want:?}",
type_id.name()
);
}
}
}