diff --git a/.github/workflows/ts.yml b/.github/workflows/ts.yml index 46c6795ac0e..de682c95480 100644 --- a/.github/workflows/ts.yml +++ b/.github/workflows/ts.yml @@ -86,6 +86,13 @@ jobs: MOQ_TEST_RUNS: ${{ runner.temp }}/moq-test-runs shell: bash -leo pipefail {0} + - name: TS fragmented PES headers + if: ${{ !cancelled() }} + run: nix develop --command just test ts --fragment-pes + env: + MOQ_TEST_RUNS: ${{ runner.temp }}/moq-test-runs + shell: bash -leo pipefail {0} + # The same round-trip at 2 Mb/s, where every keyframe outgrows a 25 ms PCR slot, # so the export has to spread it over the slots before its DTS and still keep # the constant-rate schedule. diff --git a/doc/bin/cli.md b/doc/bin/cli.md index a2e58664e29..62b22fca323 100644 --- a/doc/bin/cli.md +++ b/doc/bin/cli.md @@ -85,7 +85,11 @@ an error; run it again to publish anew. A flagged MPEG-TS discontinuity that jumps forward continues the broadcast and is declared on the exported clock. MPEG-TS import carries H.264/H.265 and AAC/MP2/AC-3/E-AC-3, and passes SCTE-35 -and subtitle PIDs through as tracks. It takes one program: a multi-program +and subtitle PIDs through as tracks. SCTE-35 cues wait for a media timestamp; +input ending or declaring a clock break before that timestamp is refused. +PES and untimed section staging share a bounded budget, so a source that never +supplies a PES boundary or clock fails rather than retaining bytes indefinitely. +It takes one program: a multi-program stream is refused until `--program` picks one, or `--program all` publishes each as its own broadcast (`event.hang` becomes `event/1.hang`, `event/2.hang`). A damaged packet is dropped on its own PID, so video freezes diff --git a/quest/m1/ts-reliability/README.md b/quest/m1/ts-reliability/README.md index 92dcd32392e..c70f555e76d 100644 --- a/quest/m1/ts-reliability/README.md +++ b/quest/m1/ts-reliability/README.md @@ -55,7 +55,6 @@ remain with their current quests. ## Required -- [Bounded PES assembly](/quest/m1/ts-reliability/pes.md) - split headers work and damaged input cannot grow staging indefinitely - [Plan: semantic fidelity](/quest/m1/ts-reliability/fidelity.md) - explicit profiles and PMT lifecycle contracts produce implementation quests - [Plan: PES timing and headers](/quest/m1/plan-ts-pes-untimed.md) - private PES semantics have a settled carriage and placement contract - [Loss and recovery tests](/quest/m1/ts-reliability/recovery.md) - actual MoQ omissions exercise reacquisition and unaffected tracks diff --git a/quest/m1/ts-reliability/pes.md b/quest/m1/ts-reliability/pes.md deleted file mode 100644 index 87c98177f83..00000000000 --- a/quest/m1/ts-reliability/pes.md +++ /dev/null @@ -1,44 +0,0 @@ -# [M] TS import assembles PES headers across packets with bounded staging - -## Goal - -Legal clear PES headers can span TS packets, and malformed or incomplete -input cannot grow importer staging indefinitely. Damage cannot concatenate -bytes across a lost packet into a plausible access unit. - -## Plan - -The audit found `Import::handle_pes` parses the first packet's payload as a -complete PES header. `PesStart::parse` needs the complete optional header, -so a legal clear header split across TS packets is discarded. PES payload -continuations and sections waiting for timing also have no explicit byte -bound in their staging paths. Raw passthrough already has explicit bounds; -semantic demux needs equally clear limits appropriate to its own units. - -Reassemble the header before parsing or delivering its payload. Keep the -state machine small and distinguish incomplete input from malformed input. -Honor declared lengths, continuity damage, discontinuity, duplicate packets, -new PUSI, and end of input. Special PES stream IDs have different header -rules and must not be forced through the ordinary optional-header layout. - -Bound retained bytes while waiting for a complete header, an unbounded PES's -next boundary, or missing clock/timestamp information. Derive protocol limits -where they exist, and use the repository's resource-limit conventions for -the rest. Refuse unsupported input explicitly instead of allowing unbounded -growth or silently inventing timing. A public configuration change needs its -own API decision if existing limits cannot express the policy. - -Regression tests fragment legal headers at every packet boundary and prove -payload/timestamps survive. Negative cases cover header length overruns, -truncation, continuity loss, and streams that never supply the awaited -boundary or clock. Assert retained memory is bounded and no damaged unit -escapes. Run these in the Rust CI tests; add a generated fragmented-header -round trip to the automated TSDuck/FFmpeg harness. - -The nonzero PES length underflow is already fixed separately in #5323; -retain its regression rather than duplicating that fix. - -## Related - -- [Automated validation](/test/ts/README.md) - independent parsing and decoding of the reassembled output -- [PES timing and headers](/quest/m1/plan-ts-pes-untimed.md) - defines semantics retained after assembly diff --git a/rs/moq-mux/src/container/ts/import.rs b/rs/moq-mux/src/container/ts/import.rs index 8f8267c6b68..7ee91575e51 100644 --- a/rs/moq-mux/src/container/ts/import.rs +++ b/rs/moq-mux/src/container/ts/import.rs @@ -15,6 +15,8 @@ //! (see [`si`](super::si)), so both survive the round-trip. use std::collections::{BTreeMap, HashMap, HashSet}; +use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; use anyhow::Context; use mpeg2ts::es::StreamType; @@ -37,6 +39,10 @@ use moq_net::Timestamp; /// ADTS-framed and is dropped. Each codec stream is fed to its importer, which /// manages the track, catalog config, and keyframe-based group boundaries. /// +/// Partial header contents hold at most 264 bytes per PID. PES payloads and sections awaiting a +/// timestamp share half a MoQ group's byte budget; held sections also share half its frame +/// budget. Exceeding either limit refuses the input instead of retaining more bytes. +/// /// Elementary streams we don't decode are carried verbatim, one MoQ track per /// PID, when the catalog `E` carries the [`mpegts`](super::Mpegts) section: PES-framed /// streams ride the normal PES reassembly, section-framed streams (SCTE-35, marked @@ -98,6 +104,10 @@ pub struct Import { liveness: Liveness, /// In-progress PES reassembly, keyed by elementary PID. pending: HashMap, + /// Partial clear PES headers, at most 9 + 255 bytes per elementary PID. + headers: HashMap>, + /// One program-wide budget for PES payloads and sections awaiting their clock. + staging: Arc, /// The TR 101 290 checks, and the per-PID continuity state routing reads. health: Health, /// PID the PMT designates as carrying the program clock reference. An @@ -190,6 +200,8 @@ impl Import { damage_log: Default::default(), liveness: Liveness::default(), pending: HashMap::new(), + headers: HashMap::new(), + staging: Arc::default(), health: Health::default(), pcr_pid: None, mux_rate: Default::default(), @@ -483,19 +495,43 @@ impl Import { } }; if pkt[1] & 0x40 != 0 { - match PesStart::parse(payload) { - Ok(pes) => self.handle_pes_start(pid, pes), - Err(err) => { - // The start still ends the PES before it, which is whole. - self.flush(pid)?; - self.damage(pid, &err) - } + // The new start ends the preceding PES even while its own header is incomplete. + self.flush(pid)?; + if self.headers.remove(&pid).is_some() { + self.damage(pid, &anyhow::anyhow!("PES header truncated at next start"))?; } + return match PesStart::header_len(payload) { + Ok(len) if payload.len() >= len => match PesStart::parse(payload) { + Ok(pes) => self.handle_pes_start(pid, pes), + Err(err) => self.damage(pid, &err), + }, + Ok(_) => self.pes_header(pid, payload.to_vec()), + Err(err) => self.damage(pid, &err), + }; + } + if let Some(mut header) = self.headers.remove(&pid) { + header.extend_from_slice(payload); + self.pes_header(pid, header) } else { self.handle_pes_continuation(pid, payload) } } + fn pes_header(&mut self, pid: Pid, header: Vec) -> anyhow::Result<()> { + let len = match PesStart::header_len(&header) { + Ok(len) => len, + Err(err) => return self.damage(pid, &err), + }; + if header.len() < len { + self.headers.insert(pid, header); + return Ok(()); + } + match PesStart::parse(&header) { + Ok(pes) => self.handle_pes_start(pid, pes), + Err(err) => self.damage(pid, &err), + } + } + fn ensure_stream(&mut self, pid: Pid, stream_type: u8, descriptors: &[catalog::Descriptor]) -> anyhow::Result<()> { // A later PMT can remap a PID that was section-framed (intercepted in // `decode`) to a PES codec/verbatim stream. Drop the stale section route first, @@ -505,6 +541,7 @@ impl Import { if let Some(mut section) = self.sections.remove(&pid.as_u16()) { section.finish()?; self.pending.remove(&pid); + self.headers.remove(&pid); } if self.streams.contains_key(&pid) { return Ok(()); @@ -644,6 +681,7 @@ impl Import { } // This PID is becoming section-framed; drop any partial PES a prior codec left pending. self.pending.remove(&pid); + self.headers.remove(&pid); self.health.restart(pid.as_u16()); if !self.supports_mpegts { // Always route to Ignored, replacing any prior codec on this PID (a later PMT @@ -661,13 +699,14 @@ impl Import { // A prior PMT may have routed this PID to Ignored; drop it so the PID has one route. self.retire_stream(pid); let descriptors = descriptors.to_vec(); - let stream = SectionStream::new( + let mut stream = SectionStream::new( self.broadcast.clone(), self.catalog.clone(), pid.as_u16(), stream_type, descriptors, )?; + stream.retained = Retained::new(self.staging.clone()); self.sections.insert(pid.as_u16(), stream); self.liveness.register(pid.as_u16()); self.initialized = true; @@ -692,7 +731,7 @@ impl Import { } fn handle_pes_start(&mut self, pid: Pid, pes: PesStart) -> anyhow::Result<()> { - // A new PES start means the previous one for this PID is complete. + // Finish the preceding PES before accepting another header. if self.pending.contains_key(&pid) { self.flush(pid)?; } @@ -754,15 +793,19 @@ impl Import { return Ok(()); } + let data = &pes.data[..pes.data_len.unwrap_or(pes.data.len()).min(pes.data.len())]; + let mut retained = Retained::new(self.staging.clone()); + retained.grow(data.len(), 0)?; let mut pending = Pending { + retained, pts: pes.pts, offset, dts: pes.dts, stream_id: pes.stream_id, - data: Vec::with_capacity(pes.data.len()), + data: Vec::with_capacity(data.len()), data_len: pes.data_len, }; - pending.data.extend_from_slice(pes.data); + pending.data.extend_from_slice(data); let complete = matches!(pes.data_len, Some(len) if pending.data.len() >= len); self.pending.insert(pid, pending); @@ -775,7 +818,9 @@ impl Import { /// Publish the sections held for the timebase's offset, now that it is known. They are stamped /// with the section clock at release: no timeline existed when they arrived. fn release_sections(&mut self, offset: Offset) -> anyhow::Result<()> { - let pts = self.last_pts.or(self.start_pts); + let Some(pts) = self.last_pts.or(self.start_pts) else { + return Ok(()); + }; for (pid, section) in &mut self.sections { let units = section.release(pts, offset)?; self.published |= units > 0; @@ -788,7 +833,12 @@ impl Import { let Some(pending) = self.pending.get_mut(&pid) else { return Ok(()); }; - pending.data.extend_from_slice(data); + let len = pending + .data_len + .map_or(data.len(), |len| len - pending.data.len()) + .min(data.len()); + pending.retained.grow(len, 0)?; + pending.data.extend_from_slice(&data[..len]); if matches!(pending.data_len, Some(len) if pending.data.len() >= len) { self.flush(pid)?; } @@ -798,6 +848,7 @@ impl Import { /// Refuse one damaged unit without changing any other PID or the program clock. fn damage(&mut self, pid: Pid, err: &anyhow::Error) -> anyhow::Result<()> { self.pending.remove(&pid); + self.headers.remove(&pid); match self.streams.get_mut(&pid) { // Nothing is imported from it, so there is nothing to refuse or report. Some(Stream::Ignored) => return Ok(()), @@ -821,6 +872,14 @@ impl Import { let Some(stream) = self.streams.get_mut(&pid) else { return Ok(()); }; + let truncated = pending.data_len.is_some_and(|len| pending.data.len() < len); + if truncated && !stream.salvages_partial_pes() { + return self.damage( + pid, + &anyhow::anyhow!("PES payload truncated before its declared length"), + ); + } + // Its PES start anchored the timebase. Release the reservation only now, so the first // snapshot carries the final clock: an Opus config comes from the PMT and would otherwise // publish first. Every stream in the initial program reserved at its PMT, so any stream's @@ -833,6 +892,9 @@ impl Import { Err(err) if err.is::() => return self.damage(pid, &err), Err(err) => return Err(err), }; + if truncated { + stream.lost()?; + } self.published |= units > 0; self.liveness.delivered(pid.as_u16(), units); @@ -854,10 +916,14 @@ impl Import { /// Publish the truncated PES on `pid` only where its bytes stand on their own, and drop /// whatever is left mid-unit. fn salvage(&mut self, pid: Pid) -> anyhow::Result<()> { + if self.headers.remove(&pid).is_some() { + self.damage(pid, &anyhow::anyhow!("PES header truncated by packet loss"))?; + } if self.streams.get(&pid).is_some_and(Stream::salvages_partial_pes) { self.flush(pid) } else { self.pending.remove(&pid); + self.headers.remove(&pid); Ok(()) } } @@ -876,6 +942,10 @@ impl Import { /// PCR/PTS rollover sets nothing and is unwrapped rather than reset; neither is a program /// break and neither reaches here. fn timebase_break(&mut self) -> anyhow::Result<()> { + anyhow::ensure!( + self.sections.values().all(|section| section.held.is_empty()), + "TS sections reached a clock break without a media timestamp" + ); // First, so the salvage below cannot publish the first catalog with the old clock's rate. self.rearm_mux_rate(); // The bytes still accumulating belong to the old clock, and their continuation is @@ -1057,14 +1127,23 @@ impl Import { /// Flush any buffered PES and finish every track. pub fn finish(&mut self) -> anyhow::Result<()> { + for pid in self.headers.keys().copied().collect::>() { + self.damage(pid, &anyhow::anyhow!("PES header truncated at end of input"))?; + } let pids: Vec = self.pending.keys().copied().collect(); for pid in pids { self.flush(pid)?; } + if let Some(offset) = self.timebase.offset() { + self.release_sections(offset)?; + } + anyhow::ensure!( + self.sections.values().all(|section| section.held.is_empty()), + "TS sections reached end of input without a media timestamp" + ); // EOF releases an unmeasured short source as well as its decoder reservations. self.mux_rate_reservation = None; self.initial_reservation = None; - self.release_sections(self.timebase.offset().unwrap_or_default())?; for (pid, stream) in &mut self.streams { let units = stream.finish()?; self.liveness.delivered(pid.as_u16(), units); @@ -1163,8 +1242,72 @@ fn list_programs(programs: &[u16]) -> String { programs.iter().map(u16::to_string).collect::>().join(", ") } +// Match the raw TS accumulator: half a MoQ group bounds one program's staged input. +const STAGING_BYTES_MAX: usize = (moq_net::group::MAX_CACHE_BYTES / 2) as usize; +const STAGING_UNITS_MAX: usize = moq_net::group::MAX_GROUP_FRAMES / 2; + +#[derive(Default)] +struct Staging { + bytes: AtomicUsize, + units: AtomicUsize, +} + +/// Accounting follows ownership through flush, loss, remapping, and importer drop. +struct Retained { + staging: Arc, + bytes: usize, + units: usize, +} + +impl Retained { + fn new(staging: Arc) -> Self { + Self { + staging, + bytes: 0, + units: 0, + } + } + + fn grow(&mut self, bytes: usize, units: usize) -> anyhow::Result<()> { + let reserve = |used: &AtomicUsize, add: usize, max: usize| { + if add == 0 { + return true; + } + used.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |used| { + used.checked_add(add).filter(|&sum| sum <= max) + }) + .is_ok() + }; + anyhow::ensure!( + reserve(&self.staging.bytes, bytes, STAGING_BYTES_MAX), + "TS staging exceeds {STAGING_BYTES_MAX} bytes while waiting for a PES boundary or timestamp" + ); + if !reserve(&self.staging.units, units, STAGING_UNITS_MAX) { + self.staging.bytes.fetch_sub(bytes, Ordering::Relaxed); + anyhow::bail!("TS staging exceeds {STAGING_UNITS_MAX} sections while waiting for a timestamp"); + } + self.bytes += bytes; + self.units += units; + Ok(()) + } + + fn clear(&mut self) { + self.staging.bytes.fetch_sub(self.bytes, Ordering::Relaxed); + self.staging.units.fetch_sub(self.units, Ordering::Relaxed); + self.bytes = 0; + self.units = 0; + } +} + +impl Drop for Retained { + fn drop(&mut self) { + self.clear(); + } +} + /// A reassembled PES packet awaiting routing to its codec importer. struct Pending { + retained: Retained, /// Raw 90 kHz PTS, before wrap-unwrapping. pts: Option, /// The timebase's offset, which the unwrapped PTS shifts by onto the catalog clock. @@ -1183,6 +1326,7 @@ impl Pending { /// A PES carrying nothing, used to drain a stream's carried tail at end of stream. fn empty() -> Self { Self { + retained: Retained::new(Arc::default()), pts: None, offset: Offset::default(), dts: None, @@ -1280,6 +1424,7 @@ struct SectionStream { reassembler: SectionReassembler, /// Sections completed before the timebase's offset was known, which they must absorb. held: Vec>, + retained: Retained, } impl SectionStream { @@ -1307,6 +1452,7 @@ impl SectionStream { _entry: entry, reassembler: SectionReassembler::default(), held: Vec::new(), + retained: Retained::new(Arc::default()), }) } @@ -1317,16 +1463,21 @@ impl SectionStream { /// completed sections are held for [`release`](Self::release): the timebase may have its offset /// from another importer before this one sees a timestamp. fn packet(&mut self, pkt: &[u8], clock: Option<(Timestamp, Offset)>) -> anyhow::Result { - self.reassembler.push(pkt, &mut self.held); + let mut completed = Vec::new(); + self.reassembler.push(pkt, &mut completed); + self.retained + .grow(completed.iter().map(Vec::len).sum(), completed.len())?; + self.held.extend(completed); match clock { - Some((pts, offset)) => self.release(Some(pts), offset), + Some((pts, offset)) => self.release(pts, offset), None => Ok(0), } } /// Publish every held section shifted by `offset`, returning how many. - fn release(&mut self, pts: Option, offset: Offset) -> anyhow::Result { + fn release(&mut self, pts: Timestamp, offset: Offset) -> anyhow::Result { let published = self.held.len() as u64; + self.retained.clear(); for mut section in std::mem::take(&mut self.held) { adjust_splice(&mut section, offset); self.emit(section, pts)?; @@ -1339,8 +1490,8 @@ impl SectionStream { /// The clock follows the video's PTS, so it only steps back by a reorder: a B-frame presents before the P-frame decoded ahead of it. Such a cue lands on /// the edge rather than shifting every cue after it, which would drift the cue track off /// the video by the reorder depth at each one. - fn emit(&mut self, section: Vec, pts: Option) -> anyhow::Result<()> { - let timestamp = pts.max(self.track.live_edge()).unwrap_or(Timestamp::ZERO); + fn emit(&mut self, section: Vec, pts: Timestamp) -> anyhow::Result<()> { + let timestamp = self.track.live_edge().map_or(pts, |edge| pts.max(edge)); let frame = crate::container::Frame { timestamp, duration: None, @@ -3776,8 +3927,15 @@ pub(super) mod test { let mut import = super::Import::new(broadcast, catalog.reserve()); let mut bytes = bytes::BytesMut::new(); - bytes.extend_from_slice(&synth_pmt(&[(StreamType::Dts8ChannelLosslessAudio, 0x21)], true)); + bytes.extend_from_slice(&synth_pmt( + &[ + (StreamType::Mpeg2Video, PCR_PID), + (StreamType::Dts8ChannelLosslessAudio, 0x21), + ], + true, + )); bytes.extend_from_slice(&packet(true, 0, 0, &CUE)); + bytes.extend_from_slice(&pes_packet(PCR_PID, 90_000)); import.decode(&bytes).unwrap(); import.finish().unwrap(); @@ -3849,7 +4007,10 @@ pub(super) mod test { // First PMT lacks CUEI: the 0x86 PID is ambiguous and routes to Ignored. let mut bytes = bytes::BytesMut::new(); bytes.extend_from_slice(&synth_pmt( - &[(StreamType::Dts8ChannelLosslessAudio, SECTION_PID)], + &[ + (StreamType::Mpeg2Video, PCR_PID), + (StreamType::Dts8ChannelLosslessAudio, SECTION_PID), + ], false, )); import.decode(&bytes).unwrap(); @@ -3860,8 +4021,15 @@ pub(super) mod test { // Second PMT carries CUEI: upgrade to a cue track, then a section on the same PID. let mut bytes = bytes::BytesMut::new(); - bytes.extend_from_slice(&synth_pmt(&[(StreamType::Dts8ChannelLosslessAudio, SECTION_PID)], true)); + bytes.extend_from_slice(&synth_pmt( + &[ + (StreamType::Mpeg2Video, PCR_PID), + (StreamType::Dts8ChannelLosslessAudio, SECTION_PID), + ], + true, + )); bytes.extend_from_slice(&packet(true, 0, 0, &CUE)); + bytes.extend_from_slice(&pes_packet(PCR_PID, 90_000)); import.decode(&bytes).unwrap(); assert!( @@ -6473,7 +6641,7 @@ pub(super) mod test { } #[tokio::test(start_paused = true)] - async fn timebase_reset_after_section_only_publication() { + async fn timebase_reset_after_anchored_section_publication() { use crate::catalog::hang::Catalog; use crate::container::ts::catalog::Ext; let mut broadcast = moq_net::broadcast::Info::new().produce(); @@ -6497,10 +6665,13 @@ pub(super) mod test { ) .unwrap(); import.decode(packet(true, 0, 0, &CUE).as_slice()).unwrap(); + import.decode(pes_packet(PCR_PID, 90_000).as_slice()).unwrap(); import.decode(clock_break_packet(PCR_PID).as_slice()).unwrap(); import.decode(packet(true, 1, 0, &CUE).as_slice()).unwrap(); + import.decode(pes_packet(PCR_PID, 90_000).as_slice()).unwrap(); import.decode(clock_break_packet(PCR_PID).as_slice()).unwrap(); import.decode(packet(true, 2, 0, &CUE).as_slice()).unwrap(); + import.decode(pes_packet(PCR_PID, 90_000).as_slice()).unwrap(); import.finish().unwrap(); let name = catalog.snapshot().ext.mpegts.tracks.keys().next().unwrap().clone(); let (frames, breaks) = read_breaks(&consumer, &name).await; @@ -6511,6 +6682,73 @@ pub(super) mod test { ); } + #[test] + fn unanchored_sections_are_refused_at_eof_even_with_an_old_offset() { + use crate::catalog::hang::Catalog; + use crate::container::ts::catalog::Ext; + + for clock in ["none", "shared offset", "clock break"] { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = crate::catalog::Producer::new( + &mut broadcast, + crate::catalog::Config::default().with_catalog(Catalog::::default()), + ) + .unwrap(); + let mut import = super::Import::new(broadcast, catalog.reserve()); + import + .decode(&synth_pmt( + &[ + (StreamType::Mpeg2Video, PCR_PID), + (StreamType::Dts8ChannelLosslessAudio, 0x21), + ], + true, + )) + .unwrap(); + if clock == "shared offset" { + // A sibling importer can anchor the shared timebase before this source has PTS. + import + .timebase + .anchor(Timestamp::from_micros(1_000_000).unwrap()) + .unwrap(); + } else if clock == "clock break" { + import.decode(&pes_packet(PCR_PID, 90_000)).unwrap(); + import.decode(&clock_break_packet(PCR_PID)).unwrap(); + } + import.decode(&packet(true, 0, 0, &CUE)).unwrap(); + let err = import.finish().unwrap_err(); + assert!(err.to_string().contains("without a media timestamp"), "{clock}: {err}"); + assert_eq!(import.sections[&0x21].held.len(), 1, "{clock}: no cue emitted"); + assert!(!import.published, "{clock}: no publication without an anchor"); + } + } + + #[test] + fn unanchored_sections_cannot_cross_a_clock_break() { + use crate::catalog::hang::Catalog; + use crate::container::ts::catalog::Ext; + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = crate::catalog::Producer::new( + &mut broadcast, + crate::catalog::Config::default().with_catalog(Catalog::::default()), + ) + .unwrap(); + let mut import = super::Import::new(broadcast, catalog.reserve()); + import + .decode(&synth_pmt( + &[ + (StreamType::Mpeg2Video, PCR_PID), + (StreamType::Dts8ChannelLosslessAudio, 0x21), + ], + true, + )) + .unwrap(); + import.decode(&packet(true, 0, 0, &CUE)).unwrap(); + let err = import.decode(&clock_break_packet(PCR_PID)).unwrap_err(); + assert!(err.to_string().contains("clock break without a media timestamp")); + assert_eq!(import.sections[&0x21].held.len(), 1); + assert!(!import.published); + } + #[tokio::test(start_paused = true)] async fn duplicate_payload_clock_packet_declares_one_break() { let (consumer, catalog, mut import) = two_stream_import(); @@ -6606,6 +6844,345 @@ pub(super) mod test { assert!(is_rewind(&err), "{err:?}"); } + /// Adaptation stuffing leaves exactly these bytes in a TS payload. + fn pes_fragment(pid: u16, pusi: bool, cc: u8, data: &[u8]) -> [u8; 188] { + assert!(!data.is_empty() && data.len() <= 183); + let mut packet = [0xff; 188]; + packet[..5].copy_from_slice(&[ + 0x47, + ((pid >> 8) as u8 & 0x1f) | if pusi { 0x40 } else { 0 }, + pid as u8, + 0x30 | (cc & 15), + (183 - data.len()) as u8, + ]); + if packet[4] != 0 { + packet[5] = 0; + } + packet[188 - data.len()..].copy_from_slice(data); + packet + } + + #[test] + fn pes_headers_fragment_at_every_byte_boundary() { + const PID: u16 = 0x50; + for (optional, paired) in [(5, false), (10, true), (255, true)] { + let mut header = vec![0, 0, 1, 0xe0, 0, 0, 0x80, if paired { 0xc0 } else { 0x80 }, optional]; + header.extend_from_slice(&pes_timestamp(if paired { 3 } else { 2 }, 90_000)); + if paired { + header.extend_from_slice(&pes_timestamp(1, 87_000)); + } + header.resize(9 + usize::from(optional), 0xff); + let data = [0xde, 0xad, 0xbe, 0xef]; + for split in 1..header.len() { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); + let mut import = super::Import::new(broadcast, catalog.reserve()); + let pid = mpeg2ts::ts::Pid::new(PID).unwrap(); + import.streams.insert(pid, super::Stream::Ignored); + let mut cc = 0; + for part in [&header[..split], &header[split..], &data] { + for chunk in part.chunks(183) { + import.pes_packet(pid, &pes_fragment(PID, cc == 0, cc, chunk)).unwrap(); + cc += 1; + } + } + let pending = import.pending.get(&pid).expect("a split clear PES is retained"); + assert_eq!(pending.pts, Some(90_000), "optional={optional} split={split}"); + assert_eq!(pending.dts, paired.then_some(87_000)); + assert_eq!(pending.data, data, "optional={optional} split={split}"); + } + } + } + + fn ignored_pes_import() -> super::Import { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); + let mut import = super::Import::new(broadcast, catalog.reserve()); + for raw in [0x50, 0x51] { + import.streams.insert(Pid::new(raw).unwrap(), super::Stream::Ignored); + } + import + } + + #[test] + fn pes_fragmented_header_resets_on_loss_new_start_and_eof() { + let pid = Pid::new(0x50).unwrap(); + let header = [0, 0, 1, 0xe0, 0, 0, 0x80, 0x80, 5]; + for end in ["loss", "start", "eof"] { + let mut import = ignored_pes_import(); + import.pes_packet(pid, &pes_fragment(0x50, true, 0, &header)).unwrap(); + assert_eq!(import.headers[&pid].len(), 9); + match end { + "loss" => import.broken(pid).unwrap(), + "start" => import + .pes_packet( + pid, + &pes_fragment(0x50, true, 1, &[0, 0, 1, 0xe0, 0, 0, 0x80, 0, 0, 0xaa]), + ) + .unwrap(), + _ => import.finish().unwrap(), + } + assert!(!import.headers.contains_key(&pid)); + if end == "start" { + assert_eq!(import.pending[&pid].data, [0xaa]); + } else { + import + .pes_packet(pid, &pes_fragment(0x50, false, 1, &[0xff; 5])) + .unwrap(); + assert!( + !import.pending.contains_key(&pid), + "orphan bytes cannot complete the lost header" + ); + } + } + } + + #[test] + fn pes_header_duplicate_and_discontinuity_follow_the_packet_chain() { + let pid = Pid::new(0x50).unwrap(); + let mut header = vec![0, 0, 1, 0xe0, 0, 0, 0x80, 0x80, 5]; + header.extend_from_slice(&pes_timestamp(2, 90_000)); + header.extend_from_slice(&[0xde, 0xad]); + for damage in ["duplicate", "loss", "discontinuity"] { + let mut import = ignored_pes_import(); + import.streams.remove(&pid); + import.ensure_stream(pid, StreamType::AdtsAac as u8, &[]).unwrap(); + let first = pes_fragment(0x50, true, 0, &header[..7]); + import.decode(&first).unwrap(); + if damage == "duplicate" { + import.decode(&first).unwrap(); + } + let mut next = pes_fragment(0x50, false, if damage == "loss" { 2 } else { 1 }, &header[7..]); + if damage == "discontinuity" { + next[5] = 0x80; + } + import.decode(&next).unwrap(); + if damage == "duplicate" { + assert_eq!(import.pending[&pid].pts, Some(90_000)); + assert_eq!(import.pending[&pid].data, [0xde, 0xad]); + } else { + assert!(!import.pending.contains_key(&pid)); + } + } + } + + #[test] + fn pes_special_stream_ids_assemble_without_optional_fields() { + let pid = Pid::new(0x50).unwrap(); + for id in [0xbc, 0xbe, 0xbf, 0xf0, 0xf1, 0xf2, 0xf8, 0xff] { + for split in 1..6 { + let mut import = ignored_pes_import(); + let header = [0, 0, 1, id, 0, 3]; + import + .pes_packet(pid, &pes_fragment(0x50, true, 0, &header[..split])) + .unwrap(); + import + .pes_packet(pid, &pes_fragment(0x50, false, 1, &header[split..])) + .unwrap(); + assert_eq!(import.pending[&pid].stream_id, id); + assert_eq!(import.pending[&pid].data_len, Some(3)); + assert_eq!(import.pending[&pid].pts, None); + } + } + assert_eq!( + super::PesStart::parse(&[0, 0, 1, 0xbe, 0, 0]).unwrap().data_len, + Some(0) + ); + } + + #[test] + fn malformed_pes_prefix_is_refused_without_waiting_for_more_bytes() { + let pid = Pid::new(0x50).unwrap(); + for bad in [&[1][..], &[0, 1][..], &[0, 0, 2][..]] { + let mut import = ignored_pes_import(); + import.pes_packet(pid, &pes_fragment(0x50, true, 0, bad)).unwrap(); + assert!(import.headers.is_empty()); + assert!(import.pending.is_empty()); + } + } + + #[test] + fn pes_length_overruns_fail_before_waiting_for_the_optional_header() { + let pid = Pid::new(0x50).unwrap(); + let mut import = ignored_pes_import(); + import + .pes_packet( + pid, + &pes_fragment(0x50, true, 0, &[0, 0, 1, 0xe0, 0, 4, 0x80, 0x80, 255]), + ) + .unwrap(); + assert!(import.headers.is_empty()); + assert!(import.pending.is_empty()); + } + + #[tokio::test(start_paused = true)] + async fn a_short_declared_video_pes_is_refused_at_eof_or_the_next_start() { + let pid = Pid::new(0x50).unwrap(); + for end in ["eof", "start"] { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); + let mut import = super::Import::new(broadcast, catalog.reserve()); + import.ensure_stream(pid, StreamType::H264 as u8, &[]).unwrap(); + let data = annexb_au(true); + let len = 8 + data.len() + 1; + let mut pes = vec![0, 0, 1, 0xe0, (len >> 8) as u8, len as u8, 0x80, 0x80, 5]; + pes.extend_from_slice(&pes_timestamp(2, 90_000)); + pes.extend_from_slice(&data); + for (cc, chunk) in pes.chunks(183).enumerate() { + import + .pes_packet(pid, &pes_fragment(0x50, cc == 0, cc as u8, chunk)) + .unwrap(); + } + if end == "eof" { + import.finish().unwrap(); + } else { + let mut header = vec![0, 0, 1, 0xe0, 0, 0, 0x80, 0x80, 5]; + header.extend_from_slice(&pes_timestamp(2, 180_000)); + import.pes_packet(pid, &pes_fragment(0x50, true, 1, &header)).unwrap(); + } + assert_eq!( + import.stats().streams[&0x50].units, + 0, + "a missing byte invalidates the declared access unit at {end}" + ); + assert_eq!(import.stats().streams[&0x50].damaged, 1); + } + } + + #[test] + fn pes_retention_budget_is_shared_and_released_on_damage() { + let mut import = ignored_pes_import(); + let first = Pid::new(0x50).unwrap(); + let second = Pid::new(0x51).unwrap(); + for pid in [first, second] { + import + .pes_packet( + pid, + &pes_fragment(pid.as_u16(), true, 0, &[0, 0, 1, 0xe0, 0, 0, 0x80, 0, 0]), + ) + .unwrap(); + import + .handle_pes_continuation(pid, &vec![0; super::STAGING_BYTES_MAX / 2]) + .unwrap(); + } + assert_eq!( + import.staging.bytes.load(super::Ordering::Relaxed), + super::STAGING_BYTES_MAX + ); + assert!( + import + .handle_pes_continuation(first, &[1]) + .unwrap_err() + .to_string() + .contains("staging exceeds") + ); + assert_eq!(import.pending[&first].data.len(), super::STAGING_BYTES_MAX / 2); + import.damage(first, &anyhow::anyhow!("loss")).unwrap(); + assert_eq!( + import.staging.bytes.load(super::Ordering::Relaxed), + super::STAGING_BYTES_MAX / 2 + ); + import.handle_pes_continuation(second, &[1]).unwrap(); + drop(import.pending.remove(&second)); + assert_eq!(import.staging.bytes.load(super::Ordering::Relaxed), 0); + } + + #[test] + fn untimed_sections_stop_at_the_shared_budget_and_release_after_a_clock() { + use crate::catalog::hang::Catalog; + use crate::container::ts::catalog::Ext; + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = crate::catalog::Producer::new( + &mut broadcast, + crate::catalog::Config::default().with_catalog(Catalog::::default()), + ) + .unwrap(); + let mut import = super::Import::new(broadcast, catalog.reserve()); + import + .decode(&synth_pmt( + &[ + (StreamType::Mpeg2Video, 0x50), + (StreamType::Dts8ChannelLosslessAudio, 0x21), + ], + true, + )) + .unwrap(); + for i in 0..super::STAGING_UNITS_MAX { + import.decode(&packet(true, (i % 16) as u8, 0, &CUE)).unwrap(); + } + let err = import + .decode(&packet(true, (super::STAGING_UNITS_MAX % 16) as u8, 0, &CUE)) + .unwrap_err(); + assert!(err.to_string().contains("sections while waiting for a timestamp")); + assert_eq!(import.sections[&0x21].held.len(), super::STAGING_UNITS_MAX); + assert_eq!( + import.staging.bytes.load(super::Ordering::Relaxed), + CUE.len() * super::STAGING_UNITS_MAX + ); + import.decode(&pes_packet(0x50, 90_000)).unwrap(); + assert!(import.sections[&0x21].held.is_empty()); + assert_eq!(import.staging.bytes.load(super::Ordering::Relaxed), 0); + assert_eq!(import.staging.units.load(super::Ordering::Relaxed), 0); + } + + #[test] + fn section_retention_budget_counts_units_and_rolls_back_failed_reservations() { + let staging = std::sync::Arc::::default(); + let mut held = super::Retained::new(staging.clone()); + held.grow(10, super::STAGING_UNITS_MAX).unwrap(); + assert!(held.grow(100, 1).is_err()); + assert_eq!(staging.bytes.load(super::Ordering::Relaxed), 10); + assert_eq!(staging.units.load(super::Ordering::Relaxed), super::STAGING_UNITS_MAX); + held.clear(); + assert_eq!(staging.bytes.load(super::Ordering::Relaxed), 0); + assert_eq!(staging.units.load(super::Ordering::Relaxed), 0); + } + + #[test] + #[ignore = "benchmark"] + fn benchmark_pes_staging() { + println!("pids,payload_bytes,fragmented,ns_per_pes"); + for pids in [1u16, 8, 64] { + for size in [184, 16 * 1024, 256 * 1024] { + for fragmented in [false, true] { + let mut import = ignored_pes_import(); + for raw in 0x50..0x50 + pids { + import.streams.insert(Pid::new(raw).unwrap(), super::Stream::Ignored); + } + let data = vec![0xaa; size]; + let header = [0, 0, 1, 0xe0, 0, 0, 0x80, 0, 0]; + let start = std::time::Instant::now(); + for _ in 0..16 { + for raw in 0x50..0x50 + pids { + let pid = Pid::new(raw).unwrap(); + if fragmented { + import + .pes_packet(pid, &pes_fragment(raw, true, 0, &header[..1])) + .unwrap(); + import + .pes_packet(pid, &pes_fragment(raw, false, 1, &header[1..])) + .unwrap(); + } else { + import.pes_packet(pid, &pes_fragment(raw, true, 0, &header)).unwrap(); + } + for chunk in data.chunks(184) { + import + .handle_pes_continuation(pid, std::hint::black_box(chunk)) + .unwrap(); + } + import.flush(pid).unwrap(); + } + } + println!( + "{pids},{size},{fragmented},{}", + start.elapsed().as_nanos() / (16 * u128::from(pids)) + ); + assert_eq!(import.staging.bytes.load(super::Ordering::Relaxed), 0); + } + } + } + } + /// A 5-byte PES timestamp field with the 4-bit `prefix` (0b0010 PTS only, 0b0011 PTS of a /// pair, 0b0001 DTS). fn pes_timestamp(prefix: u8, t: u64) -> [u8; 5] { diff --git a/rs/moq-mux/src/container/ts/psi.rs b/rs/moq-mux/src/container/ts/psi.rs index e58c6bce98c..db3c5e90655 100644 --- a/rs/moq-mux/src/container/ts/psi.rs +++ b/rs/moq-mux/src/container/ts/psi.rs @@ -270,8 +270,7 @@ fn descriptors(mut data: &[u8]) -> Option> { Some(out) } -/// The start of a PES packet: its header, and the payload bytes that follow it in this -/// TS packet. +/// A complete PES header and the payload bytes that follow it. pub(super) struct PesStart<'a> { pub stream_id: u8, /// Raw 90 kHz. @@ -285,33 +284,57 @@ pub(super) struct PesStart<'a> { } impl<'a> PesStart<'a> { - /// Parse the PES header at the start of a payload-unit-start packet's payload. + /// The next header boundary needed before parsing, capped by the 8-bit optional length. + pub fn header_len(payload: &[u8]) -> anyhow::Result { + let prefix = payload.len().min(3); + anyhow::ensure!( + payload[..prefix] == [0, 0, 1][..prefix], + "missing PES start code prefix" + ); + if payload.len() < 6 { + return Ok(6); + } + if matches!(payload[3], 0xbc | 0xbe | 0xbf | 0xf0 | 0xf1 | 0xf2 | 0xf8 | 0xff) { + return Ok(6); + } + if payload.len() < 9 { + return Ok(9); + } + anyhow::ensure!(payload[6] & 0xc0 == 0x80, "unexpected PES header marker bits"); + anyhow::ensure!(payload[6] & 0x30 == 0, "scrambled PES is not supported"); + anyhow::ensure!(payload[7] & 0xc0 != 0x40, "PES DTS without a PTS"); + let len = usize::from(payload[8]); + let packet_len = usize::from(u16::from_be_bytes([payload[4], payload[5]])); + anyhow::ensure!( + packet_len == 0 || packet_len >= 3 + len, + "PES packet length shorter than its header" + ); + Ok(9 + len) + } + + /// Parse a complete clear PES header and its following payload. /// /// Optional fields this demux has no use for (ESCR, ES rate, trick mode, copy info, the - /// PES CRC, extensions, stuffing) are skipped by `PES_header_data_length`. A payload that - /// is not a PES header at all is an error, as it was when an external reader parsed it. + /// PES CRC, extensions, stuffing) are skipped by `PES_header_data_length`. Incomplete headers + /// must be assembled before parsing; malformed headers are refused. pub fn parse(payload: &'a [u8]) -> anyhow::Result { - anyhow::ensure!(payload.len() >= 6, "PES header truncated"); - anyhow::ensure!(payload[..3] == [0, 0, 1], "missing PES start code prefix"); + let header_len = Self::header_len(payload)?; + anyhow::ensure!(payload.len() >= header_len, "PES header truncated"); let stream_id = payload[3]; let packet_len = usize::from(u16::from_be_bytes([payload[4], payload[5]])); // Streams that carry no optional header (ISO/IEC 13818-1 table 2-21). - if matches!(stream_id, 0xbc | 0xbe | 0xbf | 0xf0 | 0xf1 | 0xf2 | 0xf8 | 0xff) { + if header_len == 6 { return Ok(Self { stream_id, pts: None, dts: None, - data_len: Some(packet_len).filter(|&len| len != 0), + data_len: Some(packet_len), data: &payload[6..], }); } - anyhow::ensure!(payload.len() >= 9, "PES header truncated"); - anyhow::ensure!(payload[6] & 0xc0 == 0x80, "unexpected PES header marker bits"); - anyhow::ensure!(payload[6] & 0x30 == 0, "scrambled PES is not supported"); let (pts_flag, dts_flag) = (payload[7] & 0x80 != 0, payload[7] & 0x40 != 0); - anyhow::ensure!(pts_flag || !dts_flag, "PES DTS without a PTS"); let header_len = usize::from(payload[8]); let optional = payload.get(9..9 + header_len).context("PES header truncated")?; let data_len = match packet_len { diff --git a/sh/gh/nightly.sh b/sh/gh/nightly.sh index dd245f38d7d..45d5a9e9f04 100755 --- a/sh/gh/nightly.sh +++ b/sh/gh/nightly.sh @@ -29,7 +29,7 @@ case "${1:-}" in --bench http_idle --bench auth --bench producer_tick --bench decode --bench aggregate \ --bench catalog --bench ts_schedule --bench export --bench chunk_interleaving -- --test run just rs test -p moq-archive -p moq-mux --no-fail-fast \ - -E 'test(benchmark_timeline_fetch_fanout) | test(benchmark_damage_pid_fanout)' \ + -E 'test(benchmark_timeline_fetch_fanout) | test(benchmark_damage_pid_fanout) | test(benchmark_pes_staging)' \ --run-ignored only --success-output immediate # moq-gst's catalog bench shares a target name with moq-mux's, so it runs on its own. run just rs bench-gst-catalog -- --quick diff --git a/test/justfile b/test/justfile index d3d6056e18e..96ea568ec76 100644 --- a/test/justfile +++ b/test/justfile @@ -130,6 +130,7 @@ drill-sensitivity *args: # Check the subscriber's MPEG-TS output for IRD compliance. [positional-arguments] ts *args: + python3 ./ts/fragment_pes.test.py python3 ./ts/pcr-timing.test.py ./ts/run.sh "$@" diff --git a/test/ts/README.md b/test/ts/README.md index 0787694060f..af49738fb62 100644 --- a/test/ts/README.md +++ b/test/ts/README.md @@ -38,9 +38,15 @@ just test ts --hrd # 1080p video filling a broadcast-sized 9 Mbit C just test ts --headroom # the same with four audio PIDs, muxed above the video's Rx just test ts --leak # 1080p video with no HRD, filling its level's 24 Mb/s leak just test ts --full # the same with two audio PIDs, filling its multiplex with no nulls +just test ts --fragment-pes # legal headers split across packets, with FFmpeg/TSDuck grading just test ts --delay 1s # pass the exporter's --delay ``` +`--fragment-pes` shifts generated audio/video PES bursts into their following +null slots after splitting the header. It cycles through header-byte boundaries, +preserves packet count, PCR positions, payload bytes, and PID continuity, then +runs the same independent decode/content and transport checks as the default arm. + `--live` swaps the analyzer, not the rig: the same round-trip runs, but the subscriber's stdout goes straight into `pcr-timing.py` instead of a capture file. That is the only arm that can see release timing at all, and it is what nightly diff --git a/test/ts/fragment_pes.py b/test/ts/fragment_pes.py new file mode 100644 index 00000000000..3019aaf6e39 --- /dev/null +++ b/test/ts/fragment_pes.py @@ -0,0 +1,129 @@ +#!/usr/bin/env python3 +"""Split generated PES headers into adjacent null slots without moving PCRs.""" + +import argparse +import collections +import json +from pathlib import Path + +SIZE = 188 + + +def pid(packet): + return ((packet[1] & 31) << 8) | packet[2] + + +def payload(packet): + control = (packet[3] >> 4) & 3 + if control == 1: + return packet[4:] + if control == 3: + start = 5 + packet[4] + if start <= SIZE: + return packet[start:] + return b"" + + +def packet(original, body, start, counter): + out = bytearray(b"\xff" * SIZE) + out[:4] = original[:4] + out[1] = (out[1] & ~0x40) | (0x40 if start else 0) + if len(body) == 184: + out[3] = 0x10 | (counter & 15) + out[4:] = body + return bytes(out) + out[3] = 0x30 | (counter & 15) + out[4] = 183 - len(body) + if out[4]: + out[5] = 0 + # Keep PCR and every other adaptation field at its original byte position. + if original[3] & 0x20: + out[5 : 5 + original[4]] = original[5 : 5 + original[4]] + out[SIZE - len(body) :] = body + return bytes(out) + + +def fragment(data): + if len(data) % SIZE or any(data[at] != 0x47 for at in range(0, len(data), SIZE)): + raise ValueError("expected aligned 188-byte TS packets") + packets = [data[at : at + SIZE] for at in range(0, len(data), SIZE)] + extra = collections.Counter() + counts = collections.Counter() + splits = collections.defaultdict(set) + classes = set() + at = 0 + while at < len(packets): + original = packets[at] + current = bytearray(original) + route = pid(original) + current[3] = (current[3] & 0xF0) | ((current[3] + extra[route]) & 15) + packets[at] = bytes(current) + body = payload(original) + media = len(body) >= 9 and body[:3] == b"\0\0\1" and body[3] & 0xE0 == 0xC0 + video = len(body) >= 9 and body[:3] == b"\0\0\1" and body[3] & 0xF0 == 0xE0 + if original[1] & 0x40 and (media or video): + # FFmpeg emits a PES's packets in a burst, then nulls. Find a slot + # before the next start on this PID and shift payloads into it, keeping + # all other PIDs and every PCR at the same byte position. + slot = at + 1 + while slot < len(packets): + candidate = packets[slot] + if pid(candidate) == 0x1FFF: + break + if pid(candidate) == route and candidate[1] & 0x40: + slot = len(packets) + break + slot += 1 + if slot < len(packets): + header = 9 + body[8] + if body[6] & 0xC0 != 0x80 or header > len(body): + raise ValueError("generated source lacks a complete clear PES header") + split = 1 + counts[route] % (header - 1) + counter = current[3] & 15 + packets[at] = packet(original, body[:split], True, counter) + carried = body[split:] + for follow in range(at + 1, slot): + original_follow = packets[follow] + follow_pid = pid(original_follow) + follow_counter = (original_follow[3] + extra[follow_pid]) & 15 + if follow_pid == route and payload(original_follow): + capacity = len(payload(original_follow)) + carried += payload(original_follow) + packets[follow] = packet(original_follow, carried[:capacity], False, follow_counter) + carried = carried[capacity:] + counter = follow_counter + else: + updated = bytearray(original_follow) + updated[3] = (updated[3] & 0xF0) | follow_counter + packets[follow] = bytes(updated) + base = bytes([0x47, (route >> 8) & 31, route & 255, 0x10]) + b"\xff" * 184 + packets[slot] = packet(base, carried, False, counter + 1) + extra[route] += 1 + counts[route] += 1 + splits[route].add(split) + classes.add("video" if video else "audio") + at = slot + at += 1 + if classes != {"audio", "video"}: + raise ValueError(f"fragmented headers must cover generated audio and video, got {classes}") + return b"".join(packets), { + "counts": dict(counts), + "splits": {route: sorted(values) for route, values in splits.items()}, + } + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("source", type=Path) + parser.add_argument("output", type=Path) + parser.add_argument("--report", type=Path) + args = parser.parse_args() + data, report = fragment(args.source.read_bytes()) + args.output.write_bytes(data) + if args.report: + args.report.write_text(json.dumps(report, indent=2) + "\n") + print("fragmented PES headers:", report["counts"]) + + +if __name__ == "__main__": + main() diff --git a/test/ts/fragment_pes.test.py b/test/ts/fragment_pes.test.py new file mode 100644 index 00000000000..d4e5334f56d --- /dev/null +++ b/test/ts/fragment_pes.test.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 +"""Controls for the generated fragmented-header fixture.""" + +import unittest + +from fragment_pes import fragment, packet, payload, pid + + +class FragmentedHeaders(unittest.TestCase): + def test_payload_pcr_packet_count_and_continuity_survive(self): + source = [] + expected = {} + for counter in range(2): + for route, stream in [(0x50, 0xE0), (0x51, 0xC0)]: + header = bytes([0, 0, 1, stream, 0, 0, 0x80, 0x80, 5]) + body = header + b"\x21\0\x01\0\x01" + bytes(range(40)) + base = bytes([0x47, 0x40, route, 0x10]) + b"\xff" * 184 + original = bytearray(packet(base, body, True, counter)) + if route == 0x50: + original[5] = 0x10 + original[6:12] = b"\x01\x02\x03\x04\x7e\0" + source.extend([bytes(original), b"\x47\x1f\xff\x10" + b"\xff" * 184]) + expected[(route, counter)] = body + result, report = fragment(b"".join(source)) + self.assertEqual(len(result), len(source) * 188) + actual = [result[at : at + 188] for at in range(0, len(result), 188)] + for at in range(0, len(actual), 2): + first, second = actual[at : at + 2] + route = pid(first) + self.assertEqual(pid(second), route) + original_counter = source[at][3] & 15 + self.assertEqual(payload(first) + payload(second), expected[(route, original_counter)]) + self.assertTrue(first[1] & 0x40) + self.assertFalse(second[1] & 0x40) + self.assertEqual(first[3] & 15, original_counter * 2) + self.assertEqual(second[3] & 15, original_counter * 2 + 1) + if route == 0x50: + self.assertEqual(first[5:12], source[at][5:12]) + self.assertEqual(second[5], 0) + self.assertEqual(report["counts"], {0x50: 2, 0x51: 2}) + self.assertEqual(report["splits"], {0x50: [1, 2], 0x51: [1, 2]}) + + def test_a_full_pes_burst_shifts_into_the_following_null_slot(self): + body = bytes([0, 0, 1, 0xE0, 0, 0, 0x80, 0x80, 5]) + b"\x21\0\x01\0\x01" + b"v" * 170 + first = bytes([0x47, 0x40, 0x50, 0x10]) + body + continuation = bytearray(packet(bytes([0x47, 0, 0x50, 0x10]) + b"\xff" * 184, b"c" * 170, False, 1)) + continuation[5] = 0x10 + continuation[6:12] = b"\x01\x02\x03\x04\x7e\0" + audio_body = bytes([0, 0, 1, 0xC0, 0, 0, 0x80, 0x80, 5]) + b"\x21\0\x01\0\x01" + audio = packet(bytes([0x47, 0x40, 0x51, 0x10]) + b"\xff" * 184, audio_body, True, 0) + null = b"\x47\x1f\xff\x10" + b"\xff" * 184 + result, _ = fragment(first + continuation + null + audio + null) + packets = [result[at : at + 188] for at in range(0, len(result), 188)] + self.assertEqual(b"".join(payload(p) for p in packets if pid(p) == 0x50), body + b"c" * 170) + self.assertEqual(packets[1][5:12], continuation[5:12]) + self.assertEqual([p[3] & 15 for p in packets[:3]], [0, 1, 2]) + + def test_missing_media_null_slots_and_misalignment_are_refused(self): + for data in [b"", b"\x47" * 187, b"\x47\x1f\xff\x10" + b"\xff" * 184]: + with self.assertRaises(ValueError): + fragment(data) + + +if __name__ == "__main__": + unittest.main() diff --git a/test/ts/run.sh b/test/ts/run.sh index e81dcf6c878..7df732f7144 100755 --- a/test/ts/run.sh +++ b/test/ts/run.sh @@ -23,6 +23,7 @@ # ./run.sh --headroom # the same with four audio PIDs, muxed above the video's Rx # ./run.sh --leak # 1080p video with no HRD, filling its level's 24 Mb/s leak # ./run.sh --full # the same with two audio PIDs, filling its multiplex with no nulls +# ./run.sh --fragment-pes # split generated clear PES headers across packets # ./run.sh --delay 1s # pass the exporter's --delay # `--live` swaps the analyzer, not the rig. compliance.py grades a captured file @@ -69,15 +70,16 @@ BITRATE="${TSC_BITRATE:-10000000}" PORT="${TSC_PORT:-}" PROFILE="${TSC_PROFILE:-debug}" STRICT="" -WITH_EIT="" # add a synthetic EPG to the source and report which SI survived -LIVE="" # grade the exporter's stdout as it arrives, rather than a capture -PAIR="" # subscribe twice and grade the two captures against each other -OPEN_GOP="" # publish open GOP with leading pictures, and grade them through the round-trip -HRD="" # publish video that fills a broadcast-sized CPB, as a contribution encoder does -HEADROOM="" # the same plus four audio PIDs, in a multiplex faster than the video's Rx -LEAK="" # publish video with no HRD at its level's leak, as a full-rate feed does -FULL="" # the same with audio, in a multiplex the source fills with no nulls -EXPORT=() # extra `export ts` flags (--delay) +WITH_EIT="" # add a synthetic EPG to the source and report which SI survived +LIVE="" # grade the exporter's stdout as it arrives, rather than a capture +PAIR="" # subscribe twice and grade the two captures against each other +OPEN_GOP="" # publish open GOP with leading pictures, and grade them through the round-trip +HRD="" # publish video that fills a broadcast-sized CPB, as a contribution encoder does +HEADROOM="" # the same plus four audio PIDs, in a multiplex faster than the video's Rx +LEAK="" # publish video with no HRD at its level's leak, as a full-rate feed does +FULL="" # the same with audio, in a multiplex the source fills with no nulls +FRAGMENT_PES="" # use following null slots for fragmented PES headers +EXPORT=() # extra `export ts` flags (--delay) # How far into the run the second subscriber joins. A late join is the point: two # exporters started together can share a cadence by starting together, which is # exactly the thing under test. @@ -157,6 +159,10 @@ while [[ $# -gt 0 ]]; do FULL=1 shift ;; + --fragment-pes) + FRAGMENT_PES=1 + shift + ;; --delay) EXPORT=(--delay "$2") shift 2 @@ -225,6 +231,11 @@ if [[ -n "$OPEN_GOP" && -n "$ANALYZE_ONLY$LIVE$PAIR" ]]; then exit 1 fi +if [[ -n "$FRAGMENT_PES" && -n "$SOURCE$ANALYZE_ONLY$HRD$HEADROOM$LEAK$FULL$OPEN_GOP$PAIR$LIVE" ]]; then + echo "error: --fragment-pes requires the generated closed-GOP H.264/AAC profile" >&2 + exit 1 +fi + URL="" # set once a port is reserved, below have() { command -v "$1" >/dev/null 2>&1; } @@ -403,6 +414,12 @@ else -f mpegts -muxrate "$BITRATE" -pcr_period 20 -pes_payload_size 0 "$SRC_TS" fi +if [[ -n "$FRAGMENT_PES" ]]; then + python3 "$DIR/fragment_pes.py" "$SRC_TS" "$HARNESS_RUN/source-fragmented.ts" \ + --report "$HARNESS_RUN/fragmented-pes.json" + mv "$HARNESS_RUN/source-fragmented.ts" "$SRC_TS" +fi + # No capture in this repository carries EIT, so the import path's EIT handling is # otherwise untestable. Synthesise one, and report below which SI PIDs came back. if [[ -n "$WITH_EIT" ]]; then