dash_mpd/
scte35.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//! Support for the SCTE-35 standard for insertion of alternate content
//
// Society of Cable Telecommunications Engineers (SCTE) standard 35 "Digital Program Insertion
// Cueing Message" concerns the messages that specify points in a content stream where alternate
// content (typically advertising or local programming) can be inserted. It is used for example for
// the digital broadcast of TV content or "replay" (VOD) media, and allows the content provider to
// specify timestamps where advertising can be inserted dynamically. The advertising content
// typically comes from a third party ad distributor (Google, Amazon for example) and can be
// customized based on the viewer's location, account, preferences guessed from prior viewing,
// viewing time, etc.
//
//      https://en.wikipedia.org/wiki/SCTE-35
//
// SCTE-35 messages can be included inside the media stream (for example in MPEG TS streams or
// fragmented MP4 segments), in an HLS manifest, or in a DASH manifest, where they are carried in
// DASH Event elements within an ElementStream element. This file provides definitions for the XML
// elements used for DASH support.
//
// You won't often find public DASH streams with SCTE-35 events; they are more often used for
// server-side ad insertion, which helps ensure that viewers benefit from the advertising content
// instead of blocking or skipping it. For this reason, these definitions have not been well tested.
//
// An XML Schema for this embedding is available at
// https://github.com/Comcast/scte35-go/blob/main/docs/scte_35_20220816.xsd


#![allow(non_snake_case)]
use serde::{Serialize, Deserialize};
use serde_with::skip_serializing_none;


pub fn serialize_scte35_ns<S>(os: &Option<String>, serializer: S) -> Result<S::Ok, S::Error>
where S: serde::Serializer {
    if let Some(s) = os {
        serializer.serialize_str(s)
    } else {
        serializer.serialize_str("http://www.scte.org/schemas/35/2016")
    }
}


#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct AvailDescriptor {
    #[serde(rename = "@providerAvailId")]
    pub provider_avail_id: u32,
}

#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct DTMFDescriptor {
    #[serde(rename = "@preroll")]
    pub preroll: Option<u8>,
    #[serde(rename = "@chars")]
    pub chars: Option<String>,
}

#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct TimeDescriptor {
    #[serde(rename = "@taiSeconds")]
    pub tai_seconds: Option<u64>,
    #[serde(rename = "@taiNs")]
    pub tai_ns: Option<u32>,
    #[serde(rename = "@utcOffset")]
    pub utc_offset: Option<u16>,
}

#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct BreakDuration {
    // some buggy MPDs in the wild have this as a 0/1
    #[serde(rename = "@autoReturn")]
    pub auto_return: bool,
    #[serde(rename = "@duration")]
    pub duration: u64,
}

#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct ScteEvent {
    #[serde(rename = "@spliceEventId")]
    pub splice_event_id: Option<u32>,
    #[serde(rename = "@spliceEventCancelIndicator")]
    pub splice_event_cancel_indicator: Option<bool>,
    #[serde(rename = "@outOfNetworkIndicator")]
    pub out_of_network_indicator: Option<bool>,
    #[serde(rename = "@uniqueProgramId")]
    pub unique_program_id: Option<u16>,
    #[serde(rename = "@availNum")]
    pub avail_num: Option<u8>,
    #[serde(rename = "@availsExpected")]
    pub avails_expected: Option<u8>,
    #[serde(rename = "scte35:BreakDuration", alias="BreakDuration")]
    pub break_duration: Option<BreakDuration>,
}

#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct SpliceTime {
    #[serde(rename = "@xmlns")]
    pub xmlns: Option<String>,
    #[serde(rename = "@scte35:ptsTime", alias = "@ptsTime")]
    pub pts_time: Option<u64>,
}

#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct SpliceNull {
}

#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct SpliceSchedule {
    #[serde(rename = "scte35:Event", alias="Event")]
    pub scte_events: Vec<ScteEvent>,
    // TODO: SpliceCount?
}

#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct BandwidthReservation {
}

#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct EncryptedPacket {
    #[serde(rename = "$value")]
    pub content: Option<String>,
}

#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct PrivateBytes {
    #[serde(rename = "$value")]
    pub content: Option<String>,
}

#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct PrivateCommand {
    #[serde(rename = "@identifier")]
    pub identifier: u32,
    #[serde(rename = "scte35:PrivateBytes", alias="PrivateBytes")]
    pub private_bytes: Vec<PrivateBytes>,
}

#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct TimeSignal {
    #[serde(rename = "scte35:SpliceTime", alias="SpliceTime")]
    pub splice_time: Vec<SpliceTime>,
}

#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct SegmentationUpid {
    #[serde(rename = "@xmlns")]
    pub xmlns: Option<String>,
    #[serde(rename = "@segmentationUpidType")]
    pub segmentation_upid_type: Option<u8>,
    #[serde(rename = "@formatIdentifier")]
    pub format_identifier: Option<u32>,
    #[serde(rename = "@segmentationUpidFormat")]
    pub segmentation_upid_format: Option<String>,
    #[serde(rename = "@format")]
    pub format: Option<String>,
    #[serde(rename = "$value")]
    pub content: Option<String>,
}

#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct SegmentationDescriptor {
    #[serde(rename = "@xmlns")]
    pub xmlns: Option<String>,
    #[serde(rename = "@segmentationEventId")]
    pub segmentation_event_id: Option<u32>,
    #[serde(rename = "@segmentationEventCancelIndicator")]
    pub segmentation_event_cancel_indicator: Option<bool>,
    #[serde(rename = "@spliceEventId")]
    pub splice_event_id: Option<u64>,
    #[serde(rename = "@segmentationTypeId")]
    pub segmentation_type_id: Option<u8>,
    #[serde(rename = "@segmentationDuration")]
    pub segmentation_duration: Option<u64>,
    #[serde(rename = "@segmentationUpidType")]
    pub segmentation_upid_type: Option<u8>,
    #[serde(rename = "@segmentationUpid")]
    pub segmentation_upid: Option<u64>,
    #[serde(rename = "@segmentNum")]
    pub segment_num: Option<u8>,
    #[serde(rename = "@segmentsExpected")]
    pub segments_expected: Option<u8>,
    #[serde(rename = "@subSegmentNum")]
    pub sub_segment_num: Option<u8>,
    #[serde(rename = "@subSegmentsExpected")]
    pub sub_segments_expected: Option<u8>,
    #[serde(rename = "scte35:SegmentationUpid", alias="SegmentationUpid")]
    pub segmentation_upids: Vec<SegmentationUpid>,
}

#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct Program {
    #[serde(rename = "scte35:SpliceTime", alias="SpliceTime")]
    pub splice_time: Vec<SpliceTime>,
}

#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct SpliceInsert {
    #[serde(rename = "@spliceEventId")]
    pub splice_event_id: Option<u32>,
    #[serde(rename = "@spliceEventCancelIndicator")]
    pub splice_event_cancel_indicator: Option<bool>,
    #[serde(rename = "@outOfNetworkIndicator")]
    pub out_of_network_indicator: Option<bool>,
    #[serde(rename = "@spliceImmediateFlag")]
    pub splice_immediate_flag: Option<bool>,
    #[serde(rename = "@uniqueProgramId")]
    pub unique_program_id: Option<u16>,
    #[serde(rename = "@availNum")]
    pub avail_num: Option<u8>,
    #[serde(rename = "@availsExpected")]
    pub avails_expected: Option<u8>,
    #[serde(rename = "scte35:BreakDuration", alias="BreakDuration")]
    pub break_duration: Option<BreakDuration>,
}

#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct SpliceInfoSection {
    #[serde(rename = "@xmlns")]
    pub xmlns: Option<String>,
    #[serde(rename = "@sapType")]
    pub sap_type: Option<u16>,
    #[serde(rename = "@preRollMilliSeconds")]
    pub pre_roll_milliseconds: Option<u32>,
    #[serde(rename = "@ptsAdjustment")]
    pub pts_adjustment: Option<u64>,
    #[serde(rename = "@protocolVersion")]
    pub protocol_version: Option<u8>,
    #[serde(rename = "@tier")]
    pub tier: Option<u16>,
    #[serde(rename = "scte35:TimeSignal", alias="TimeSignal")]
    pub time_signal: Option<TimeSignal>,
    #[serde(rename = "scte35:SegmentationDescriptor", alias="SegmentationDescriptor")]
    pub segmentation_descriptor: Option<SegmentationDescriptor>,
    #[serde(rename = "scte35:SpliceNull", alias="SpliceNull")]
    pub splice_null: Option<SpliceNull>,
    #[serde(rename = "scte35:SpliceInsert", alias="SpliceInsert")]
    pub splice_insert: Option<SpliceInsert>,
    #[serde(rename = "scte35:SpliceSchedule", alias="SpliceSchedule")]
    pub splice_schedule: Option<SpliceSchedule>,
    #[serde(rename = "scte35:BandwidthReservation", alias="BandwidthReservation")]
    pub bandwidth_reservation: Option<BandwidthReservation>,
    #[serde(rename = "scte35:PrivateCommand", alias="PrivateCommand")]
    pub private_command: Option<PrivateCommand>,
    #[serde(rename = "scte35:EncryptedPacket", alias="EncryptedPacket")]
    pub encrypted_packet: Option<EncryptedPacket>,
    #[serde(rename = "scte35:AvailDescriptor", alias="AvailDescriptor")]
    pub avail_descriptor: Option<AvailDescriptor>,
    #[serde(rename = "scte35:DTMFDescriptor", alias="DTMFDescriptor")]
    pub dtmf_descriptor: Option<DTMFDescriptor>,
    #[serde(rename = "scte35:TimeDescriptor", alias="TimeDescriptor")]
    pub time_descriptor: Option<TimeDescriptor>,
}

/// A binary representation of a SCTE 35 cue message.
///
/// We don't attempt to decode these, but the `scte35-reader` crate is able to parse a subset of the
/// standard, and the `threefive` Python library provides a full parser.
///
/// Basic messages may just be '/' + base64-encoded string
///   e.g. "/TWFpbiBDb250ZW50" -> "Main Content"
#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct Binary {
    #[serde(rename = "@signalType")]
    pub signal_type: Option<String>,
    #[serde(rename = "$value")]
    pub content: String,
}

#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(default)]
pub struct Signal {
    #[serde(rename = "@xmlns")]
    pub xmlns: Option<String>,
    #[serde(rename = "scte35:SpliceInfoSection", alias="SpliceInfoSection")]
    pub splice_info_section: Option<SpliceInfoSection>,
    #[serde(rename = "scte35:Binary", alias="Binary")]
    pub content: Option<Binary>,
}