mpeg-ts 0.1.2

MPEG-2 Transport Stream framing (ITU-T H.222.0 / ISO/IEC 13818-1): TS packet, adaptation field, PCR, PSI section reassembly + packetization, resync. no_std.
Documentation
//! Demux a PAT section from a hardcoded TS packet.
//!
//! Run with: `cargo run -p mpeg-ts --example demux`
//!
//! Demonstrates feeding a 188-byte TS packet into `SectionReassembler` and
//! recovering the completed PSI section bytes.

use mpeg_ts::ts::{SectionReassembler, TsPacket};

// A real PAT packet extracted from the m6-single.ts test fixture.
// PID = 0x0000, PUSI = 1, table_id = 0x00 (PAT), transport_stream_id = 1.
const PAT_PACKET: [u8; 188] = [
    0x47, 0x40, 0x00, 0x10, 0x00, 0x00, 0xb0, 0x0d, 0x00, 0x01, 0xc1, 0x00, 0x00, 0x04, 0x01, 0xe0,
    0x64, 0xf9, 0xb4, 0x63, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
];

fn main() {
    let pkt = TsPacket::parse(&PAT_PACKET).expect("hardcoded packet must parse");
    println!(
        "TS packet: pid=0x{:04X} pusi={} has_payload={}",
        pkt.header.pid, pkt.header.pusi, pkt.header.has_payload
    );

    let mut reasm = SectionReassembler::default();

    if let Some(payload) = pkt.payload {
        reasm.feed(payload, pkt.header.pusi);
    }

    while let Some(section) = reasm.pop_section() {
        println!(
            "section: {} bytes, table_id=0x{:02X}",
            section.len(),
            section[0]
        );
        // The PAT section bytes: table_id=0x00, transport_stream_id at bytes 3-4.
        if section.len() >= 5 {
            let ts_id = u16::from_be_bytes([section[3], section[4]]);
            println!("  transport_stream_id={}", ts_id);
        }
    }
}