Skip to main content

scte35_splice/commands/
time_signal.rs

1//! time_signal() — ANSI/SCTE 35 2023r1 §9.7.4, Table 11
2//! (splice_command_type 0x06).
3//!
4//! A single `splice_time()` structure; its payload (segmentation, etc.) rides
5//! in the section descriptor loop. When `time_specified_flag` is 0 the command
6//! is interpreted as immediate.
7
8use crate::error::{Error, Result};
9use crate::time::SpliceTime;
10use crate::traits::CommandDef;
11use broadcast_common::{Parse, Serialize};
12
13/// `splice_command_type` for time_signal (§9.6.1, Table 7).
14pub const COMMAND_TYPE: u8 = 0x06;
15
16/// time_signal() — §9.7.4, Table 11.
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
18#[cfg_attr(feature = "serde", derive(serde::Serialize))]
19pub struct TimeSignal {
20    /// The splice time (§9.8.1).
21    pub splice_time: SpliceTime,
22}
23
24impl<'a> Parse<'a> for TimeSignal {
25    type Error = Error;
26    fn parse(bytes: &'a [u8]) -> Result<Self> {
27        Ok(Self {
28            splice_time: SpliceTime::parse(bytes)?,
29        })
30    }
31}
32
33impl Serialize for TimeSignal {
34    type Error = Error;
35    fn serialized_len(&self) -> usize {
36        self.splice_time.serialized_len()
37    }
38    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
39        self.splice_time.serialize_into(buf)
40    }
41}
42
43impl<'a> CommandDef<'a> for TimeSignal {
44    const COMMAND_TYPE: u8 = COMMAND_TYPE;
45    const NAME: &'static str = "TIME_SIGNAL";
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn round_trip_with_time() {
54        let cmd = TimeSignal {
55            splice_time: SpliceTime::with_pts(0x0_1234_5678),
56        };
57        let bytes = cmd.to_bytes();
58        let back = TimeSignal::parse(&bytes).unwrap();
59        assert_eq!(cmd, back);
60        assert_eq!(back.to_bytes(), bytes);
61    }
62
63    #[test]
64    fn round_trip_immediate() {
65        let cmd = TimeSignal::default();
66        let bytes = cmd.to_bytes();
67        assert_eq!(bytes.len(), 1);
68        assert_eq!(TimeSignal::parse(&bytes).unwrap(), cmd);
69    }
70}