Skip to main content

demux/
demux.rs

1//! Demux a PAT section from a hardcoded TS packet.
2//!
3//! Run with: `cargo run -p mpeg-ts --example demux`
4//!
5//! Demonstrates feeding a 188-byte TS packet into `SectionReassembler` and
6//! recovering the completed PSI section bytes.
7
8use mpeg_ts::ts::{SectionReassembler, TsPacket};
9
10// A real PAT packet extracted from the m6-single.ts test fixture.
11// PID = 0x0000, PUSI = 1, table_id = 0x00 (PAT), transport_stream_id = 1.
12const PAT_PACKET: [u8; 188] = [
13    0x47, 0x40, 0x00, 0x10, 0x00, 0x00, 0xb0, 0x0d, 0x00, 0x01, 0xc1, 0x00, 0x00, 0x04, 0x01, 0xe0,
14    0x64, 0xf9, 0xb4, 0x63, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
15    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
16    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
17    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
18    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
19    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
20    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
21    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
22    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
23    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
24    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
25];
26
27fn main() {
28    let pkt = TsPacket::parse(&PAT_PACKET).expect("hardcoded packet must parse");
29    println!(
30        "TS packet: pid=0x{:04X} pusi={} has_payload={}",
31        pkt.header.pid, pkt.header.pusi, pkt.header.has_payload
32    );
33
34    let mut reasm = SectionReassembler::default();
35
36    if let Some(payload) = pkt.payload {
37        reasm.feed(payload, pkt.header.pusi);
38    }
39
40    while let Some(section) = reasm.pop_section() {
41        println!(
42            "section: {} bytes, table_id=0x{:02X}",
43            section.len(),
44            section[0]
45        );
46        // The PAT section bytes: table_id=0x00, transport_stream_id at bytes 3-4.
47        if section.len() >= 5 {
48            let ts_id = u16::from_be_bytes([section[3], section[4]]);
49            println!("  transport_stream_id={}", ts_id);
50        }
51    }
52}