pub struct SectionReassembler { /* private fields */ }Expand description
Reassembles PSI/SI sections from TS packets on a single PID.
Feed each TS packet’s payload with feed. Complete sections are
appended to an internal queue; drain them with pop_section. A
crate::unit::UnitReassembler with the PSI framing and options.
Implementations§
Source§impl SectionReassembler
impl SectionReassembler
Sourcepub fn feed(&mut self, payload: &[u8], pusi: bool)
pub fn feed(&mut self, payload: &[u8], pusi: bool)
Feed a TS payload and whether its packet had PUSI set.
Extracts complete SI sections into the internal queue. A single call
can produce zero, one, or several sections — a payload may
concatenate multiple complete sections after the pointer_field
(EN 300 468 §5.1.4; common on EMM PIDs). Drain with a
while let Some(s) = r.pop_section() loop, not a single if let.
Examples found in repository?
45fn main() {
46 let pkt = TsPacket::parse(&PAT_PACKET).expect("hardcoded packet must parse");
47 println!(
48 "TS packet: pid=0x{:04X} pusi={} has_payload={}",
49 pkt.header.pid.value(),
50 pkt.header.pusi,
51 pkt.header.has_payload()
52 );
53 assert_eq!(pkt.header.pid, well_known::PAT);
54
55 let mut reasm = SectionReassembler::default();
56
57 if let Some(payload) = pkt.payload {
58 reasm.feed(payload, pkt.header.pusi);
59 }
60
61 let mut units = UnitReassembler::new(
62 PatFraming,
63 UnitOptions::new(
64 AfterContinuation::DiscardRest,
65 PointerCheck::Authoritative,
66 Duplicates::Any,
67 std::num::NonZeroUsize::new(1024).unwrap(),
68 ),
69 );
70
71 while let Some(section) = reasm.pop_section() {
72 let mut packets = Vec::new();
73 let count =
74 SectionPacketiser::new(well_known::PAT).packetise_into(&[§ion], &mut packets);
75 println!(
76 "re-packetised {count} PAT packet(s), section: {} bytes, table_id=0x{:02X}",
77 section.len(),
78 section[0]
79 );
80 // The PAT section bytes: table_id=0x00, transport_stream_id at bytes 3-4.
81 if section.len() >= 5 {
82 let ts_id = u16::from_be_bytes([section[3], section[4]]);
83 println!(" transport_stream_id={ts_id}");
84 }
85 if let Some(payload) = pkt.payload {
86 units.feed(payload, pkt.header.pusi, None);
87 if let Some(unit) = units.pop() {
88 println!(" unit reassembled: {} bytes", unit.len());
89 }
90 }
91 }
92}Sourcepub fn feed_with_continuity(
&mut self,
payload: &[u8],
pusi: bool,
continuity_counter: ContinuityCounter,
discontinuity: bool,
)
pub fn feed_with_continuity( &mut self, payload: &[u8], pusi: bool, continuity_counter: ContinuityCounter, discontinuity: bool, )
Like feed, additionally taking the packet’s
continuity_counter so a legally retransmitted packet is discarded
rather than appended twice.
ISO/IEC 13818-1 §2.4.3.3 (continuity_counter semantics, see
private/specs-verified/mpeg/mpeg-ts/tables/h222_0/transport_packet.md)
permits a packet to be repeated once with the same counter; a
continuation packet whose counter and payload repeat the previous one
is dropped. Call this
for payload-bearing packets only: the counter does not advance on
packets without payload. discontinuity is the packet’s
adaptation-field discontinuity_indicator (ISO/IEC 13818-1 §2.4.3.5):
when it is set an unchanged continuity counter is legal and the packet
is not a duplicate.
Sourcepub fn pop_section(&mut self) -> Option<Bytes>
pub fn pop_section(&mut self) -> Option<Bytes>
Pop one complete section. Returns None when the queue is empty.
Examples found in repository?
45fn main() {
46 let pkt = TsPacket::parse(&PAT_PACKET).expect("hardcoded packet must parse");
47 println!(
48 "TS packet: pid=0x{:04X} pusi={} has_payload={}",
49 pkt.header.pid.value(),
50 pkt.header.pusi,
51 pkt.header.has_payload()
52 );
53 assert_eq!(pkt.header.pid, well_known::PAT);
54
55 let mut reasm = SectionReassembler::default();
56
57 if let Some(payload) = pkt.payload {
58 reasm.feed(payload, pkt.header.pusi);
59 }
60
61 let mut units = UnitReassembler::new(
62 PatFraming,
63 UnitOptions::new(
64 AfterContinuation::DiscardRest,
65 PointerCheck::Authoritative,
66 Duplicates::Any,
67 std::num::NonZeroUsize::new(1024).unwrap(),
68 ),
69 );
70
71 while let Some(section) = reasm.pop_section() {
72 let mut packets = Vec::new();
73 let count =
74 SectionPacketiser::new(well_known::PAT).packetise_into(&[§ion], &mut packets);
75 println!(
76 "re-packetised {count} PAT packet(s), section: {} bytes, table_id=0x{:02X}",
77 section.len(),
78 section[0]
79 );
80 // The PAT section bytes: table_id=0x00, transport_stream_id at bytes 3-4.
81 if section.len() >= 5 {
82 let ts_id = u16::from_be_bytes([section[3], section[4]]);
83 println!(" transport_stream_id={ts_id}");
84 }
85 if let Some(payload) = pkt.payload {
86 units.feed(payload, pkt.header.pusi, None);
87 if let Some(unit) = units.pop() {
88 println!(" unit reassembled: {} bytes", unit.len());
89 }
90 }
91 }
92}