Skip to main content

dvb_si/collect/
nit.rs

1use crate::descriptors::DescriptorRegistry;
2use crate::tables::nit;
3use alloc::vec::Vec;
4
5use super::{CompleteSectionSet, ParsedDescriptorLoop};
6
7/// Transport-stream entry in a complete NIT.
8#[derive(Debug)]
9#[non_exhaustive]
10pub struct CompleteNitTransportStream<'a> {
11    /// transport_stream_id of the described TS.
12    pub transport_stream_id: u16,
13    /// original_network_id of the described TS.
14    pub original_network_id: u16,
15    /// Typed descriptor loop for this transport stream.
16    pub descriptors: ParsedDescriptorLoop<'a>,
17}
18
19/// Complete logical Network Information Table.
20#[derive(Debug)]
21#[non_exhaustive]
22pub struct CompleteNit<'a> {
23    /// Variant discriminator.
24    pub kind: nit::NitKind,
25    /// Network identifier.
26    pub network_id: u16,
27    /// 5-bit version_number.
28    pub version_number: u8,
29    /// current_next_indicator bit.
30    pub current_next_indicator: bool,
31    /// Network-wide descriptors, concatenated across every section that
32    /// carries a `network_descriptors_loop` (EN 300 468 ยง5.2.1: each section
33    /// has its own loop, so a network name/linkage split across sections is
34    /// not lost).
35    pub network_descriptors: ParsedDescriptorLoop<'a>,
36    /// Transport-stream loop entries from all sections in wire order.
37    pub transport_streams: Vec<CompleteNitTransportStream<'a>>,
38}
39
40impl<'a> CompleteNit<'a> {
41    pub(crate) fn parse(
42        set: &'a CompleteSectionSet,
43        registry: Option<&'a DescriptorRegistry>,
44    ) -> crate::Result<Self> {
45        let sections: Vec<nit::NitSection<'a>> = set.parse_sections()?;
46        let first = sections.first().ok_or(crate::Error::BufferTooShort {
47            need: 1,
48            have: 0,
49            what: "CompleteNit sections",
50        })?;
51        let mut transport_streams = Vec::new();
52        for section in &sections {
53            transport_streams.extend(section.transport_streams.iter().map(|ts| {
54                CompleteNitTransportStream {
55                    transport_stream_id: ts.transport_stream_id,
56                    original_network_id: ts.original_network_id,
57                    descriptors: ParsedDescriptorLoop::parse(ts.descriptors, registry),
58                }
59            }));
60        }
61        Ok(Self {
62            kind: first.kind,
63            network_id: first.network_id,
64            version_number: first.version_number,
65            current_next_indicator: first.current_next_indicator,
66            network_descriptors: ParsedDescriptorLoop::parse_all(
67                sections.iter().map(|section| section.network_descriptors),
68                registry,
69            ),
70            transport_streams,
71        })
72    }
73}