scte104 0.4.0

ANSI/SCTE 104 2023 — Automation System to Compression System Communications API (single/multiple operation messages + DPI operations).
Documentation
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! provisioning_request_data() — ANSI/SCTE 104 2023 §10.5.1, Table 10-3
//! (opID 0x000B). PAMS ==> AS.
//!
//! Basic request. The PAMS notifies the Automation System of all Injectors
//! ready for use in DPI service: a loop of services, each with a nested loop
//! of DPI PID entries and an optional `injector_component_list()`
//! (§10.8.1, Table 10-9).
//!
//! Transcribed in `docs/ansi_scte_104/pams_operations.md` (verified via
//! `pdf2md` against ANSI/SCTE 104 2023 pp. 75-76 and p. 80-81).

use alloc::vec::Vec;

use crate::error::{Error, Result};
use crate::traits::OperationDef;
use broadcast_common::{Parse, Serialize};

/// `opID` for provisioning_request (§8.3, Table 8-3).
pub const OP_ID: u16 = 0x000B;

/// Fixed byte width of the `service_name` / `injector_service_name` string
/// fields (Table 10-3 §10.5.1 / Table 10-5 §10.6.1) — NUL-terminated,
/// zero-padded to this width.
pub const SERVICE_NAME_LEN: usize = 32;

/// Fixed wire size of one `DPI_PID_index`/`shared_PID`/`event_id_compliance_flag`
/// loop entry (Table 10-3).
pub const DPI_PID_ENTRY_LEN: usize = 4;

/// One entry in the `provisioning_request_data()` DPI-PID loop — Table 10-3.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct DpiPidEntry {
    /// `DPI_PID_index` — 2 bytes. PID index for a specific DPI service (§8.2.1).
    pub dpi_pid_index: u16,
    /// `shared_PID` — 1 byte. Zero = unique; one = intentionally duplicated.
    pub shared_pid: u8,
    /// `event_id_compliance_flag` — 1 byte.
    pub event_id_compliance_flag: u8,
}

impl DpiPidEntry {
    fn parse_one(bytes: &[u8]) -> Result<Self> {
        if bytes.len() < DPI_PID_ENTRY_LEN {
            return Err(Error::BufferTooShort {
                need: DPI_PID_ENTRY_LEN,
                have: bytes.len(),
                what: "provisioning_request_data DPI PID entry",
            });
        }
        Ok(Self {
            dpi_pid_index: u16::from_be_bytes([bytes[0], bytes[1]]),
            shared_pid: bytes[2],
            event_id_compliance_flag: bytes[3],
        })
    }

    fn write_one(&self, buf: &mut [u8]) {
        buf[0..2].copy_from_slice(&self.dpi_pid_index.to_be_bytes());
        buf[2] = self.shared_pid;
        buf[3] = self.event_id_compliance_flag;
    }
}

/// `injector_component_list()` — §10.8.1, Table 10-9. Referenced
/// conditionally from `provisioning_request_data()` when `component_mode != 0`.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct InjectorComponentList {
    /// `video_component_tag` — 1 byte. `component_tag` of the video stream.
    pub video_component_tag: u8,
    /// `audio_component_tag` loop — one entry per audio stream.
    pub audio_component_tags: Vec<u8>,
    /// `data_component_tag` loop — one entry per data service.
    pub data_component_tags: Vec<u8>,
}

impl InjectorComponentList {
    /// Parse from the front of `bytes`; returns the value and the number of
    /// bytes consumed (the caller has more fields possibly following).
    fn parse_prefix(bytes: &[u8]) -> Result<(Self, usize)> {
        if bytes.len() < 2 {
            return Err(Error::BufferTooShort {
                need: 2,
                have: bytes.len(),
                what: "injector_component_list header",
            });
        }
        let video_component_tag = bytes[0];
        let n_audio = bytes[1] as usize;
        let mut pos = 2;
        if bytes.len() < pos + n_audio + 1 {
            return Err(Error::BufferTooShort {
                need: pos + n_audio + 1,
                have: bytes.len(),
                what: "injector_component_list audio_component_tags",
            });
        }
        let audio_component_tags = bytes[pos..pos + n_audio].to_vec();
        pos += n_audio;
        let n_data = bytes[pos] as usize;
        pos += 1;
        if bytes.len() < pos + n_data {
            return Err(Error::BufferTooShort {
                need: pos + n_data,
                have: bytes.len(),
                what: "injector_component_list data_component_tags",
            });
        }
        let data_component_tags = bytes[pos..pos + n_data].to_vec();
        pos += n_data;
        Ok((
            Self {
                video_component_tag,
                audio_component_tags,
                data_component_tags,
            },
            pos,
        ))
    }

    fn serialized_len(&self) -> usize {
        2 + self.audio_component_tags.len() + 1 + self.data_component_tags.len()
    }

    /// Write into the front of `buf`; returns bytes written.
    fn write_prefix(&self, buf: &mut [u8]) -> usize {
        let n_audio = self.audio_component_tags.len();
        let n_data = self.data_component_tags.len();
        buf[0] = self.video_component_tag;
        buf[1] = n_audio as u8;
        let mut pos = 2;
        buf[pos..pos + n_audio].copy_from_slice(&self.audio_component_tags);
        pos += n_audio;
        buf[pos] = n_data as u8;
        pos += 1;
        buf[pos..pos + n_data].copy_from_slice(&self.data_component_tags);
        pos += n_data;
        pos
    }
}

/// One service entry within `provisioning_request_data()` — Table 10-3.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct ProvisioningService {
    /// `injector_IP_address` — 4 bytes. Zero if not using TCP/IP.
    pub injector_ip_address: u32,
    /// `injector_socket_number` — 2 bytes. Zero if not using TCP/IP.
    pub injector_socket_number: u16,
    /// `service_name` — 32-byte NUL-terminated string field.
    pub service_name: [u8; SERVICE_NAME_LEN],
    /// `DPI_PID_index`/`shared_PID`/`event_id_compliance_flag` loop.
    pub dpi_pids: Vec<DpiPidEntry>,
    /// `component_mode` — 1 byte. Acts as the presence flag for
    /// `injector_component_list()` per the syntax table's
    /// `if (component_mode != 0)` condition (see the module doc note on the
    /// prose/syntax-table discrepancy for this field).
    pub component_mode: u8,
    /// `injector_component_list()`, present iff `component_mode != 0`.
    pub injector_component_list: Option<InjectorComponentList>,
}

impl Default for ProvisioningService {
    fn default() -> Self {
        Self {
            injector_ip_address: 0,
            injector_socket_number: 0,
            service_name: [0; SERVICE_NAME_LEN],
            dpi_pids: Vec::new(),
            component_mode: 0,
            injector_component_list: None,
        }
    }
}

impl ProvisioningService {
    fn parse_prefix(bytes: &[u8]) -> Result<(Self, usize)> {
        const HEAD_LEN: usize = 4 + 2 + SERVICE_NAME_LEN + 1; // up to number_of_DPI_PIDs
        if bytes.len() < HEAD_LEN {
            return Err(Error::BufferTooShort {
                need: HEAD_LEN,
                have: bytes.len(),
                what: "provisioning_request_data service header",
            });
        }
        let injector_ip_address = u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
        let injector_socket_number = u16::from_be_bytes([bytes[4], bytes[5]]);
        let mut service_name = [0u8; SERVICE_NAME_LEN];
        service_name.copy_from_slice(&bytes[6..6 + SERVICE_NAME_LEN]);
        let number_of_dpi_pids = bytes[6 + SERVICE_NAME_LEN] as usize;
        let mut pos = HEAD_LEN;

        let mut dpi_pids = Vec::with_capacity(number_of_dpi_pids);
        for _ in 0..number_of_dpi_pids {
            let entry = DpiPidEntry::parse_one(&bytes[pos..])?;
            dpi_pids.push(entry);
            pos += DPI_PID_ENTRY_LEN;
        }

        if bytes.len() < pos + 1 {
            return Err(Error::BufferTooShort {
                need: pos + 1,
                have: bytes.len(),
                what: "provisioning_request_data component_mode",
            });
        }
        let component_mode = bytes[pos];
        pos += 1;

        let injector_component_list = if component_mode != 0 {
            let (list, consumed) = InjectorComponentList::parse_prefix(&bytes[pos..])?;
            pos += consumed;
            Some(list)
        } else {
            None
        };

        Ok((
            Self {
                injector_ip_address,
                injector_socket_number,
                service_name,
                dpi_pids,
                component_mode,
                injector_component_list,
            },
            pos,
        ))
    }

    fn serialized_len(&self) -> usize {
        let mut len = 4 + 2 + SERVICE_NAME_LEN + 1 + self.dpi_pids.len() * DPI_PID_ENTRY_LEN + 1;
        if let Some(list) = &self.injector_component_list {
            len += list.serialized_len();
        }
        len
    }

    fn write_prefix(&self, buf: &mut [u8]) -> usize {
        buf[0..4].copy_from_slice(&self.injector_ip_address.to_be_bytes());
        buf[4..6].copy_from_slice(&self.injector_socket_number.to_be_bytes());
        buf[6..6 + SERVICE_NAME_LEN].copy_from_slice(&self.service_name);
        buf[6 + SERVICE_NAME_LEN] = self.dpi_pids.len() as u8;
        let mut pos = 4 + 2 + SERVICE_NAME_LEN + 1;
        for entry in &self.dpi_pids {
            entry.write_one(&mut buf[pos..pos + DPI_PID_ENTRY_LEN]);
            pos += DPI_PID_ENTRY_LEN;
        }
        buf[pos] = self.component_mode;
        pos += 1;
        if let Some(list) = &self.injector_component_list {
            pos += list.write_prefix(&mut buf[pos..]);
        }
        pos
    }
}

/// provisioning_request_data() — §10.5.1, Table 10-3. Variable-length: a
/// `service_count`-prefixed loop of [`ProvisioningService`] entries.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct ProvisioningRequest {
    /// The `service_count`-prefixed loop of services.
    pub services: Vec<ProvisioningService>,
}

impl<'a> Parse<'a> for ProvisioningRequest {
    type Error = Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        if bytes.is_empty() {
            return Err(Error::BufferTooShort {
                need: 1,
                have: 0,
                what: "provisioning_request_data service_count",
            });
        }
        let service_count = bytes[0] as usize;
        let mut pos = 1;
        let mut services = Vec::with_capacity(service_count);
        for _ in 0..service_count {
            let (service, consumed) = ProvisioningService::parse_prefix(&bytes[pos..])?;
            services.push(service);
            pos += consumed;
        }
        Ok(Self { services })
    }
}

impl Serialize for ProvisioningRequest {
    type Error = Error;
    fn serialized_len(&self) -> usize {
        1 + self
            .services
            .iter()
            .map(ProvisioningService::serialized_len)
            .sum::<usize>()
    }
    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let need = self.serialized_len();
        if buf.len() < need {
            return Err(Error::OutputBufferTooSmall {
                need,
                have: buf.len(),
            });
        }
        buf[0] = self.services.len() as u8;
        let mut pos = 1;
        for service in &self.services {
            pos += service.write_prefix(&mut buf[pos..]);
        }
        Ok(pos)
    }
}

impl OperationDef<'_> for ProvisioningRequest {
    const OP_ID: u16 = OP_ID;
    const NAME: &'static str = "PROVISIONING_REQUEST";
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::vec;

    fn sample_name(s: &str) -> [u8; SERVICE_NAME_LEN] {
        let mut name = [0u8; SERVICE_NAME_LEN];
        name[..s.len()].copy_from_slice(s.as_bytes());
        name
    }

    #[test]
    fn round_trip_no_services() {
        let op = ProvisioningRequest::default();
        let bytes = op.to_bytes();
        assert_eq!(bytes, vec![0]);
        let back = ProvisioningRequest::parse(&bytes).unwrap();
        assert_eq!(op, back);
    }

    #[test]
    fn round_trip_one_service_no_component_list() {
        let op = ProvisioningRequest {
            services: vec![ProvisioningService {
                injector_ip_address: 0xC0A8_0001,
                injector_socket_number: 5167,
                service_name: sample_name("svc-1"),
                dpi_pids: vec![DpiPidEntry {
                    dpi_pid_index: 1,
                    shared_pid: 0,
                    event_id_compliance_flag: 1,
                }],
                component_mode: 0,
                injector_component_list: None,
            }],
        };
        let bytes = op.to_bytes();
        let back = ProvisioningRequest::parse(&bytes).unwrap();
        assert_eq!(op, back);
        let b2 = back.to_bytes();
        assert_eq!(bytes, b2);
    }

    #[test]
    fn round_trip_two_services_with_component_list() {
        let op = ProvisioningRequest {
            services: vec![
                ProvisioningService {
                    injector_ip_address: 0x0A00_0001,
                    injector_socket_number: 8000,
                    service_name: sample_name("svc-a"),
                    dpi_pids: vec![
                        DpiPidEntry {
                            dpi_pid_index: 1,
                            shared_pid: 0,
                            event_id_compliance_flag: 1,
                        },
                        DpiPidEntry {
                            dpi_pid_index: 2,
                            shared_pid: 1,
                            event_id_compliance_flag: 0,
                        },
                    ],
                    component_mode: 1,
                    injector_component_list: Some(InjectorComponentList {
                        video_component_tag: 0x10,
                        audio_component_tags: vec![0x20, 0x21],
                        data_component_tags: vec![0x30],
                    }),
                },
                ProvisioningService {
                    injector_ip_address: 0,
                    injector_socket_number: 0,
                    service_name: sample_name("svc-b"),
                    dpi_pids: vec![],
                    component_mode: 0,
                    injector_component_list: None,
                },
            ],
        };
        let bytes = op.to_bytes();
        let back = ProvisioningRequest::parse(&bytes).unwrap();
        assert_eq!(op, back);
        let b2 = back.to_bytes();
        assert_eq!(bytes, b2);
    }

    #[test]
    fn mutate_field_changes_output() {
        let op = ProvisioningRequest {
            services: vec![ProvisioningService {
                injector_ip_address: 1,
                injector_socket_number: 1,
                service_name: sample_name("svc"),
                dpi_pids: vec![],
                component_mode: 0,
                injector_component_list: None,
            }],
        };
        let bytes = op.to_bytes();
        let mut op2 = op.clone();
        op2.services[0].injector_ip_address = 999;
        assert_ne!(op2.to_bytes(), bytes);
    }

    #[test]
    fn truncated_buffer_rejected() {
        let op = ProvisioningRequest {
            services: vec![ProvisioningService {
                injector_ip_address: 1,
                injector_socket_number: 1,
                service_name: sample_name("svc"),
                dpi_pids: vec![DpiPidEntry {
                    dpi_pid_index: 1,
                    shared_pid: 0,
                    event_id_compliance_flag: 0,
                }],
                component_mode: 0,
                injector_component_list: None,
            }],
        };
        let bytes = op.to_bytes();
        assert!(matches!(
            ProvisioningRequest::parse(&bytes[..bytes.len() - 1]),
            Err(Error::BufferTooShort { .. })
        ));
    }
}