mpeg2ts_reader/descriptor/
registration.rs

1//! Registration descriptor indicates which kind of syntax any 'private data' within the transport
2//! stream will be following
3
4use super::DescriptorError;
5use crate::descriptor::descriptor_len;
6use crate::smptera::FormatIdentifier;
7use std::fmt;
8
9/// Indicates which kind of syntax any 'private data' within the transport stream will be following
10pub struct RegistrationDescriptor<'buf> {
11    /// the registration data bytes
12    buf: &'buf [u8],
13}
14impl<'buf> RegistrationDescriptor<'buf> {
15    /// The descriptor tag value which identifies the descriptor as a `RegistrationDescriptor`.
16    pub const TAG: u8 = 5;
17    /// Construct a `RegistrationDescriptor` instance that will parse the data from the given
18    /// slice.
19    pub fn new(tag: u8, buf: &'buf [u8]) -> Result<RegistrationDescriptor<'buf>, DescriptorError> {
20        descriptor_len(buf, tag, 4)?;
21        Ok(RegistrationDescriptor { buf })
22    }
23
24    /// Format identifier value assigned by a _Registration Authority_.
25    ///
26    /// Note that the `FormatIdentifier` type defines numerous constants for identifiers registered
27    /// with the SMPTE RA, which you can use in tests like so:
28    ///
29    /// ```rust
30    /// # use mpeg2ts_reader::descriptor::registration::RegistrationDescriptor;
31    /// use mpeg2ts_reader::smptera::FormatIdentifier;
32    /// # let descriptor = RegistrationDescriptor::new(RegistrationDescriptor::TAG, b"CUEI")
33    /// #   .unwrap();
34    /// if descriptor.format_identifier() == FormatIdentifier::CUEI {
35    ///     // perform some interesting action
36    /// }
37    /// ```
38    pub fn format_identifier(&self) -> FormatIdentifier {
39        FormatIdentifier::from(&self.buf[0..4])
40    }
41
42    /// Returns true if the given identifier is equal to the value returned by `format_identifier()`
43    pub fn is_format(&self, id: FormatIdentifier) -> bool {
44        self.format_identifier() == id
45    }
46
47    /// borrows a slice of additional_identification_info bytes, whose meaning is defined by
48    /// the identifier returned by `format_identifier()`.
49    pub fn additional_identification_info(&self) -> &[u8] {
50        &self.buf[4..]
51    }
52}
53impl<'buf> fmt::Debug for RegistrationDescriptor<'buf> {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
55        f.debug_struct("RegistrationDescriptor")
56            .field("format_identifier", &self.format_identifier())
57            .field(
58                "additional_identification_info",
59                &format!("{:x?}", self.additional_identification_info()),
60            )
61            .finish()
62    }
63}
64
65#[cfg(test)]
66mod test {
67    use super::super::{CoreDescriptors, Descriptor};
68    use assert_matches::assert_matches;
69    use hex_literal::*;
70
71    #[test]
72    fn descriptor() {
73        let data = hex!("050443554549");
74        let desc = CoreDescriptors::from_bytes(&data[..]).unwrap();
75        assert_matches!(desc, CoreDescriptors::Registration(reg) => {
76            let expected = crate::smptera::FormatIdentifier::from(&b"CUEI"[..]);
77            assert_eq!(reg.format_identifier(), expected);
78            assert!(reg.is_format(expected));
79            assert!(!format!("{:?}", reg).is_empty())
80        });
81    }
82}