1use crate::error::Result;
3use alloc::{string::String, vec::Vec};
4use scte35_splice::{
5 SpliceInfoSection,
6 commands::AnyCommand,
7 descriptors::{AnySpliceDescriptor, SegmentationTypeId},
8};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
13pub struct MediaTime(pub u64);
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18pub struct MediaDuration(pub u64);
19
20impl MediaDuration {
21 pub fn as_seconds_f64(self) -> f64 {
23 self.0 as f64 / crate::PTS_HZ as f64
24 }
25}
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
30#[non_exhaustive]
31pub enum EventKind {
32 BreakStart,
34 BreakEnd,
36 Chapter,
38 Unspecified,
40}
41
42impl EventKind {
43 pub fn name(&self) -> &'static str {
45 match self {
46 EventKind::BreakStart => "break_start",
47 EventKind::BreakEnd => "break_end",
48 EventKind::Chapter => "chapter",
49 EventKind::Unspecified => "unspecified",
50 }
51 }
52}
53broadcast_common::impl_spec_display!(EventKind);
54
55#[derive(Debug, Clone, PartialEq, Eq)]
57#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
58#[non_exhaustive]
59pub enum SourcePayload {
60 Scte35 { raw: Vec<u8> },
62 Emsg {
64 scheme_id_uri: String,
65 value: String,
66 raw: Vec<u8>,
67 },
68}
69
70#[derive(Debug, Clone, PartialEq, Eq)]
72#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
73pub struct TimedEvent {
74 pub id: Option<u32>,
76 pub kind: EventKind,
78 pub at: Option<MediaTime>,
80 pub duration: Option<MediaDuration>,
82 pub source: SourcePayload,
84}
85
86impl TimedEvent {
87 pub fn from_scte35(section: &SpliceInfoSection, raw: &[u8]) -> Result<Self> {
95 let mut id = None;
96 let mut kind = EventKind::Unspecified;
97 let mut at = None;
98 let mut duration = None;
99
100 let adjust =
101 |pts: u64| MediaTime(broadcast_common::clock33::add(pts, section.pts_adjustment));
102
103 if let Some(clear) = §ion.clear {
104 match &clear.command {
105 AnyCommand::SpliceInsert(si) => {
106 id = Some(si.splice_event_id);
107 kind = if si.splice_event_cancel_indicator {
108 EventKind::Unspecified
111 } else if si.out_of_network_indicator {
112 EventKind::BreakStart
113 } else {
114 EventKind::BreakEnd
115 };
116 if let Some(st) = &si.splice_time {
117 at = st.pts_time.map(adjust);
118 }
119 if let Some(bd) = &si.break_duration {
120 duration = Some(MediaDuration(bd.duration));
121 }
122 }
123 AnyCommand::TimeSignal(ts) => {
124 at = ts.splice_time.pts_time.map(adjust);
125 for d in section.descriptors() {
131 if let Ok(AnySpliceDescriptor::Segmentation(seg)) = d {
132 if seg.segmentation_event_cancel_indicator {
133 continue;
134 }
135 id = Some(seg.segmentation_event_id);
136 kind = segmentation_kind(seg.segmentation_type_id);
137 duration = seg.segmentation_duration.map(MediaDuration);
138 break;
139 }
140 }
141 }
142 _ => {}
143 }
144 }
145
146 Ok(TimedEvent {
147 id,
148 kind,
149 at,
150 duration,
151 source: SourcePayload::Scte35 { raw: raw.to_vec() },
152 })
153 }
154}
155
156fn segmentation_kind(type_id: SegmentationTypeId) -> EventKind {
173 use SegmentationTypeId as T;
174 match type_id {
175 T::ProgramStart | T::ProgramEnd | T::ChapterStart | T::ChapterEnd => EventKind::Chapter,
176 T::BreakStart
177 | T::ProviderAdvertisementStart
178 | T::DistributorAdvertisementStart
179 | T::ProviderPlacementOpportunityStart
180 | T::DistributorPlacementOpportunityStart
181 | T::ProviderAdBlockStart
182 | T::DistributorAdBlockStart => EventKind::BreakStart,
183 T::BreakEnd
184 | T::ProviderAdvertisementEnd
185 | T::DistributorAdvertisementEnd
186 | T::ProviderPlacementOpportunityEnd
187 | T::DistributorPlacementOpportunityEnd
188 | T::ProviderAdBlockEnd
189 | T::DistributorAdBlockEnd => EventKind::BreakEnd,
190 _ => EventKind::Unspecified,
191 }
192}
193
194#[cfg(test)]
195mod tests {
196 use super::*;
197 use alloc::vec::Vec;
198 use broadcast_common::traits::Parse;
199 use scte35_splice::SpliceInfoSection;
200
201 fn splice_2002() -> Vec<u8> {
203 test_fixtures::read("scte35/splice-insert-2002.bin")
204 }
205
206 #[test]
207 fn from_scte35_extracts_break_start_and_duration() {
208 let raw = splice_2002();
209 let section = SpliceInfoSection::parse(&raw).unwrap();
210 let ev = TimedEvent::from_scte35(§ion, &raw).unwrap();
211 assert_eq!(ev.id, Some(2002));
212 assert_eq!(ev.kind, EventKind::BreakStart); assert_eq!(ev.at, None); assert_eq!(ev.duration, Some(MediaDuration(2_160_000)));
215 assert!((ev.duration.unwrap().as_seconds_f64() - 24.0).abs() < 1e-9);
216 match &ev.source {
217 SourcePayload::Scte35 { raw: r } => assert_eq!(r, &raw), _ => panic!("expected Scte35 payload"),
219 }
220 }
221
222 #[test]
223 fn event_kind_labels() {
224 assert_eq!(EventKind::BreakStart.name(), "break_start");
225 assert_eq!(alloc::format!("{}", EventKind::BreakEnd), "break_end");
226 }
227
228 #[test]
238 fn segmentation_kind_classifies_by_group() {
239 use SegmentationTypeId as T;
240 let cases: &[(SegmentationTypeId, EventKind)] = &[
241 (T::ProgramStart, EventKind::Chapter),
243 (T::ProgramEnd, EventKind::Chapter),
244 (T::ChapterStart, EventKind::Chapter),
245 (T::ChapterEnd, EventKind::Chapter),
246 (T::BreakStart, EventKind::BreakStart),
248 (T::BreakEnd, EventKind::BreakEnd),
249 (T::ProviderAdvertisementStart, EventKind::BreakStart),
250 (T::DistributorAdvertisementEnd, EventKind::BreakEnd),
251 (T::ProviderPlacementOpportunityStart, EventKind::BreakStart),
252 (T::DistributorPlacementOpportunityEnd, EventKind::BreakEnd),
253 (T::ProviderAdBlockStart, EventKind::BreakStart),
254 (T::DistributorAdBlockEnd, EventKind::BreakEnd),
255 (T::OpeningCreditStart, EventKind::Unspecified),
257 (T::ClosingCreditEnd, EventKind::Unspecified),
258 (T::ProviderPromoStart, EventKind::Unspecified),
259 (T::DistributorPromoEnd, EventKind::Unspecified),
260 (T::UnscheduledEventStart, EventKind::Unspecified),
261 (T::AlternateContentOpportunityEnd, EventKind::Unspecified),
262 (
265 T::ProviderOverlayPlacementOpportunityStart,
266 EventKind::Unspecified,
267 ),
268 (
269 T::DistributorOverlayPlacementOpportunityEnd,
270 EventKind::Unspecified,
271 ),
272 (T::NetworkStart, EventKind::Unspecified),
274 (T::NetworkEnd, EventKind::Unspecified),
275 (T::Reserved(0x60), EventKind::Unspecified),
276 ];
277 for (type_id, want) in cases {
278 assert_eq!(
279 segmentation_kind(*type_id),
280 *want,
281 "segmentation_type_id {type_id:?} ({}) must classify as {want:?}",
282 type_id.name()
283 );
284 }
285 }
286}