dvb-si 17.0.0

ETSI EN 300 468 DVB Service Information parser + builder. MPEG-2 PSI included.
Documentation
//! Follow a DSM-CC data carousel in a real capture: frame its sections, type
//! the DSI/DII/DDB messages and hand them to a [`ModuleReassembler`].
//!
//! Run with: `cargo run -p dvb-si --example carousel_module`
//!
//! The capture is the M6 HbbTV object carousel of a French TNT multiplex on
//! PID 0x00AB. Each DII announces the modules of a download; the reassembler
//! arms one slot per module and fills it from the DDB blocks, returning the
//! [`Module`](dvb_si::carousel::Module) bytes once every block has arrived.
//! The capture is a few seconds long, so it ends before most modules
//! complete; on a live feed the carousel repeats and every module completes.

use std::num::NonZeroUsize;

use broadcast_core::Parse;
use dvb_si::carousel::{DownloadDataBlock, ModuleReassembler, ReassemblerLimits, UnMessage};
use dvb_si::tables::dsmcc::DsmccSection;
use mpeg_ts::pid::Pid;
use mpeg_ts::reader::PacketReader;
use mpeg_ts::resync::{LOCK_CONFIRMATIONS, PacketStride, ResyncConfig, Unlocked};
use mpeg_ts::ts::{SectionReassembler, TsPacket};

const CAROUSEL_PID: Pid = Pid::from_const(0x00AB);
const DSMCC_CONTROL: u8 = 0x3B;
const DSMCC_DATA: u8 = 0x3C;
const READ_CHUNK_LEN: NonZeroUsize = NonZeroUsize::MIN.saturating_add((1 << 20) - 1);
const RESYNC: ResyncConfig = ResyncConfig::new(
    NonZeroUsize::MIN.saturating_add(LOCK_CONFIRMATIONS - 1),
    &PacketStride::ALL,
    Unlocked::KeepWindow,
);
const LIMITS: ReassemblerLimits = ReassemblerLimits {
    max_module_size: 16 << 20,
    max_total_bytes: 64 << 20,
    max_slots: 4096,
    max_idle: 1 << 20,
};

fn handle_section(
    bytes: &[u8],
    reassembler: &mut ModuleReassembler,
) -> Result<(), Box<dyn std::error::Error>> {
    let section = DsmccSection::parse(bytes)?;
    match section.table_id {
        DSMCC_CONTROL => match UnMessage::parse(section.payload)? {
            UnMessage::Dsi(dsi) => {
                println!("DSI transaction {:#010X}", dsi.transaction_id);
            }
            UnMessage::Dii(dii) => {
                println!(
                    "DII download {:#010X}: {} module(s), block size {}",
                    dii.download_id,
                    dii.modules.len(),
                    dii.block_size
                );
                reassembler.note_dii(&dii);
            }
            _ => {}
        },
        DSMCC_DATA => {
            let ddb = DownloadDataBlock::parse(section.payload)?;
            if let Some(module) = reassembler.feed_ddb(&ddb) {
                println!(
                    "module {} v{} complete: {} bytes",
                    module.key.module_id,
                    module.key.module_version,
                    module.data.len()
                );
            }
        }
        _ => {}
    }
    Ok(())
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let path = test_fixtures::path("ts/m6-single.ts");
    let file = std::fs::File::open(&path)?;

    let mut sections = SectionReassembler::default();
    let mut reassembler = ModuleReassembler::new(LIMITS);

    for packet in PacketReader::new(file, READ_CHUNK_LEN, RESYNC) {
        let packet = packet?;
        let packet = TsPacket::parse(&packet)?;
        if packet.header.pid != CAROUSEL_PID {
            continue;
        }
        let Some(payload) = packet.payload else {
            continue;
        };
        sections.feed(payload, packet.header.pusi);
        while let Some(section) = sections.pop_section() {
            handle_section(&section, &mut reassembler)?;
        }
    }

    println!(
        "\n{} module(s) still collecting ({} bytes held)",
        reassembler.pending(),
        reassembler.pending_bytes()
    );
    Ok(())
}