From 51d550c5456c7ac0e5f3b05aa70f27c32d798231 Mon Sep 17 00:00:00 2001 From: Thomas Drapier Date: Sat, 24 Oct 2026 21:34:53 +0100 Subject: [PATCH 1/5] chore(quest): claim quest/m1/ts-reliability/pat-lifecycle.md Quest-Claim: e06d267f329540b7 From 2deada294f61a6115c53157ccce4176e7a956349 Mon Sep 17 00:00:00 2001 From: t0ms Date: Sat, 24 Oct 2026 22:10:34 +0100 Subject: [PATCH 2/5] fix(moq-mux): TS programs follow the PAT as it changes Co-authored-by: Claude Opus 5.5 --- doc/bin/cli.md | 6 +- doc/bin/srt.md | 3 +- quest/m1/ts-reliability/README.md | 4 +- quest/m1/ts-reliability/pat-lifecycle.md | 73 ++-- rs/moq-mux/src/container/ts/health.rs | 28 +- rs/moq-mux/src/container/ts/import.rs | 97 +++-- rs/moq-mux/src/container/ts/programs.rs | 436 ++++++++++++++++++++--- rs/moq-mux/src/container/ts/stats.rs | 17 + 8 files changed, 533 insertions(+), 131 deletions(-) diff --git a/doc/bin/cli.md b/doc/bin/cli.md index 235a015c52d..6fe0a44162e 100644 --- a/doc/bin/cli.md +++ b/doc/bin/cli.md @@ -108,8 +108,10 @@ 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 -until its next keyframe while the other tracks carry on. The importer also +`event/2.hang`), following the PAT: a program it adds publishes, one it drops +ends, and one that returns publishes as a new instance. A damaged packet is +dropped on its own PID, so video freezes until its next keyframe while the +other tracks carry on. The importer also logs feed health (stalled streams, TR 101 290 errors) without changing what it publishes. `--passthrough` publishes the multiplex whole instead, every packet in order on one [`m2ts`](/concept/hang#transport-streams) track with every diff --git a/doc/bin/srt.md b/doc/bin/srt.md index 1e0069edfc4..8de4d1518ec 100644 --- a/doc/bin/srt.md +++ b/doc/bin/srt.md @@ -37,7 +37,8 @@ With the default `--linger 0s`, that catalog must already be available. A multi-program feed is refused unless `--program` picks one: `--program 2` imports program 2 alone, and `--program all` publishes each program as its own broadcast (`event.hang` becomes -`event/1.hang`, `event/2.hang`, and so on). +`event/1.hang`, `event/2.hang`, and so on), following the PAT as programs +join and leave. ```bash moq --connect https://relay.example.com/anon --broadcast event.hang import srt --listen '[::]:9000' --program all diff --git a/quest/m1/ts-reliability/README.md b/quest/m1/ts-reliability/README.md index 9a95475434b..c16189ef425 100644 --- a/quest/m1/ts-reliability/README.md +++ b/quest/m1/ts-reliability/README.md @@ -73,7 +73,7 @@ refused means an error or a counted drop. | DVB SI on 0x0010, 0x0011, 0x0012, 0x0014 | Preserved: snapshot track per PID and `table_id` | landed | | Program identity, program and ES descriptors | Preserved: descriptors follow each PMT revision; an identity change refuses | landed | | PMT add, remove, retype; PID reuse | Normalized without a new epoch: a changed or reused PID ends its track and continues under a fresh name, and the export rebuilds the PMT at the next `version_number` | landed | -| PAT changes: moved PMT PID, programs added or removed | Normalized: followed; a removed program retires, and returns as a new epoch. Today ignored, or the whole multiplex stops | [PAT changes](/quest/m1/ts-reliability/pat-lifecycle.md) | +| PAT changes: moved PMT PID, programs added or removed | Normalized: followed; a removed program retires, and returns as a new epoch. Today a running export keeps the PMT on its first PID | [PAT changes](/quest/m1/ts-reliability/pat-lifecycle.md) | | Programs without video | Preserved when the PMT names a PCR PID; refused without one | landed | | PCR | Normalized: rebased, carried on the first video PID, else audio, else a dedicated PCR-only PID | landed | | Mux rate, null stuffing | Normalized: re-padded to `muxRate` while the source is CBR | landed | @@ -100,7 +100,7 @@ remain with their current quests. - [State bound](/quest/m1/ts-reliability/state-bound.md) - SI and revision state retire with what the source stops repeating - [Access units](/quest/m1/ts-reliability/pes-access-units.md) - a PES carrying several access units publishes each at its own time -- [PAT changes](/quest/m1/ts-reliability/pat-lifecycle.md) - a revised PAT never leaves a broadcast meaning another program +- [PAT changes](/quest/m1/ts-reliability/pat-lifecycle.md) - a running export follows a moved PMT PID - [Stream routes](/quest/m1/ts-reliability/stream-routes.md) - every PID gets a declared route, and what is not carried is counted ## Related diff --git a/quest/m1/ts-reliability/pat-lifecycle.md b/quest/m1/ts-reliability/pat-lifecycle.md index 37d58b1e598..b101662f07a 100644 --- a/quest/m1/ts-reliability/pat-lifecycle.md +++ b/quest/m1/ts-reliability/pat-lifecycle.md @@ -1,49 +1,36 @@ -# [M] Live PAT changes keep programs honest +# [S] A live export follows a moved PMT PID ## Goal -A live TS source whose PAT changes mid-stream keeps every broadcast meaning -one program. A moved PMT PID, a program added, and a program removed or -returning each have one observable outcome: followed, published, retired, -or published as a new epoch. None is silently ignored. +A running `export ts` whose catalog moves `program.pmtPid` follows it: a PAT +at the next `version_number` names the new PID before any PMT goes out +there, and nothing goes out on the old PID after it. The output stays one +program throughout, and no two PATs at one version name different PIDs. ## Plan -What the code does today: - -- A PAT naming another PMT PID for the program adds a reassembler but never - drops the old one, and `program.pmtPid` keeps the first. -- `Programs` ignores a program a later PAT adds, without a warning. -- Under `--program all`, the shared parser hands a later PAT that removes one - program to every program's `Import`; the removed program's `handle_pat` - errors, `Programs::decode` propagates it, and the whole multiplex ingest - stops, including unrelated programs. `Programs` sees each whole PAT first - (`Packet::pat`), so it can retire or start programs there instead. A - program still waiting for its PMT is already dropped there, or moved to - the PMT PID the later PAT names. -- A PAT naming another `transport_stream_id`, or a single-program import's - PAT naming another program, already refuses the import. - -Decided 2026-10-20 by the maintainer: - -- A new PMT PID for the same program moves the reassembler and updates - `program.pmtPid`, which the export follows with a new PAT. `Programs` - publishes a program a later PAT adds as its own broadcast, as it does for - the first PAT, rather than ignoring it. -- A later PAT that removes a program retires that program's broadcast: its - tracks finish and it is unannounced, while the other programs carry on. - If the program returns, it publishes a new epoch of its broadcast, never - the retired instance's names for new content. A single-program import - whose selected program leaves the PAT ends, as today. -- Open: a PAT that names a program whose PMT never arrives. `Programs` - creates a program's broadcast only once its PMT lists a stream, so such a - program waits holding only its PAT entry. Decide whether the import refuses - it instead, and which clock bounds the wait (found in - [moq-dev/moq#6122](https://github.com/moq-dev/moq/pull/6122)). - -Rust: `rs/moq-mux` TS `Import::handle_pat` and `Programs`. - -Tests use the generated `fixture::Mux` builder: a PMT PID move, and a -`--program all` MPTS whose PAT adds a program, drops one and later restores -it: the other program runs uninterrupted, and the returning one publishes a -new epoch. +The import follows the PAT: a moved PMT PID moves its reassembler and +`program.pmtPid`, and `Programs` publishes, retires and republishes programs +as the PAT adds, drops and restores them. A running export still keeps the +PMT on the PID its tables first went out on, whatever `program.pmtPid` later +records: its output stays consistent, but a move is followed only by an +export started after it. + +The maintainer's plan, from closing +[moq-dev/moq#6301](https://github.com/moq-dev/moq/pull/6301): the export +follows a new `program.pmtPid` with a new PAT, built on the table rework +that lays the PAT and PMT from the current table state when their slot is +laid instead of riding queued units, so a move is one more state change +rather than a rewrite of queued copies. That rework has not landed. + +The draft then says that `pmtPid` can change in place, and when a consumer +rebuilding the stream moves the PMT. + +Rust: `rs/moq-mux` TS export (`pmt_pid`, `build_psi`, the PSI schedule). + +Tests: a live export whose catalog moves `pmtPid` emits PATs at versions 0 +then 1, with nothing on the new PID before the moving PAT and nothing on the +old one after it, and a move while every copy is still queued goes out as +one PAT naming the new PID. `revision_test`'s +`a_track_recorded_on_the_pmt_pid_after_the_catalog_moves_it_takes_another` +then pins the move instead of the PMT staying put. diff --git a/rs/moq-mux/src/container/ts/health.rs b/rs/moq-mux/src/container/ts/health.rs index e0ca98f5537..84fa1bf5f09 100644 --- a/rs/moq-mux/src/container/ts/health.rs +++ b/rs/moq-mux/src/container/ts/health.rs @@ -61,19 +61,25 @@ impl Errors { /// Fold in another checker's count of the same input. Programs sharing a PAT, PMT or PCR PID /// time the same packets, so each check takes the larger count rather than the sum. pub(super) fn max(&mut self, other: &Self) { - self.ts_sync_loss = self.ts_sync_loss.max(other.ts_sync_loss); - self.sync_byte_error = self.sync_byte_error.max(other.sync_byte_error); - self.pat_error = self.pat_error.max(other.pat_error); + self.combine(other, u64::max); + } + + /// Set each count to `f` of it and `other`'s, check by check and PID by PID. + pub(super) fn combine(&mut self, other: &Self, f: impl Fn(u64, u64) -> u64) { + self.ts_sync_loss = f(self.ts_sync_loss, other.ts_sync_loss); + self.sync_byte_error = f(self.sync_byte_error, other.sync_byte_error); + self.pat_error = f(self.pat_error, other.pat_error); for (pid, theirs) in &other.pids { let ours = self.pids.entry(*pid).or_default(); - ours.continuity_count_error = ours.continuity_count_error.max(theirs.continuity_count_error); - ours.transport_error = ours.transport_error.max(theirs.transport_error); - ours.pmt_error = ours.pmt_error.max(theirs.pmt_error); - ours.pcr_repetition_error = ours.pcr_repetition_error.max(theirs.pcr_repetition_error); - ours.pcr_discontinuity_indicator_error = ours - .pcr_discontinuity_indicator_error - .max(theirs.pcr_discontinuity_indicator_error); - ours.pts_error = ours.pts_error.max(theirs.pts_error); + ours.continuity_count_error = f(ours.continuity_count_error, theirs.continuity_count_error); + ours.transport_error = f(ours.transport_error, theirs.transport_error); + ours.pmt_error = f(ours.pmt_error, theirs.pmt_error); + ours.pcr_repetition_error = f(ours.pcr_repetition_error, theirs.pcr_repetition_error); + ours.pcr_discontinuity_indicator_error = f( + ours.pcr_discontinuity_indicator_error, + theirs.pcr_discontinuity_indicator_error, + ); + ours.pts_error = f(ours.pts_error, theirs.pts_error); } } diff --git a/rs/moq-mux/src/container/ts/import.rs b/rs/moq-mux/src/container/ts/import.rs index 85960ce6019..b84e5a4e7a5 100644 --- a/rs/moq-mux/src/container/ts/import.rs +++ b/rs/moq-mux/src/container/ts/import.rs @@ -117,12 +117,12 @@ pub struct Import { demux: Demux, /// PIDs whose route changed while handling the current packet, for the parser to restart. restarts: Vec, - /// Whether a PAT or PMT was handled since [`claims_changed`](Self::claims_changed) last - /// said so. + /// Whether a PAT moved the PMT, or a PMT was handled, since + /// [`claims_changed`](Self::claims_changed) last said so. claims_changed: bool, - /// The PMT PIDs PATs have named for the imported program, each with its reassembler. + /// The PMT PID the latest PAT names for the imported program, with its reassembler. /// Several programs' PMTs may share one. - pmt_sections: HashMap, + pmt: Option<(Pid, SectionReassembler)>, /// PMT sections dropped for a CRC mismatch, keyed by PID. crc_errors: BTreeMap, /// Per elementary-stream-PID codec routing. @@ -241,7 +241,7 @@ impl Import { demux: Demux::new(supports_mpegts), restarts: Vec::new(), claims_changed: false, - pmt_sections: HashMap::new(), + pmt: None, crc_errors: BTreeMap::new(), streams: HashMap::new(), retired_stats: BTreeMap::new(), @@ -300,13 +300,13 @@ impl Import { /// Carry on the PMT `sections` began on `pid`, for an importer that starts mid-stream on the /// packet after the ones `sections` read. pub(super) fn with_pmt(mut self, pid: Pid, sections: SectionReassembler) -> Self { - self.pmt_sections.insert(pid, sections); + self.pmt = Some((pid, sections)); self } /// Whether this importer reassembles the PMT sections on `pid`, and counts their CRC errors. pub(super) fn reads_pmt(&self, pid: u16) -> bool { - Pid::new(pid).is_ok_and(|pid| self.pmt_sections.contains_key(&pid)) + self.pmt.as_ref().is_some_and(|(pmt, _)| pmt.as_u16() == pid) } /// A reservation on this program's timebase, so every stream shifts by its offset. @@ -360,8 +360,9 @@ impl Import { /// elementary streams and its clock. pub(super) fn claims(&self) -> Vec { let mut pids: Vec = self - .pmt_sections - .keys() + .pmt + .iter() + .map(|(pid, _)| pid) .chain(self.streams.keys()) .chain(self.pcr_pid.iter()) .map(Pid::as_u16) @@ -513,7 +514,7 @@ impl Import { if let Some(pat) = &packet.pat { self.handle_pat(pat)?; } - } else if self.pmt_sections.contains_key(&pid) { + } else if self.pmt.as_ref().is_some_and(|(pmt, _)| *pmt == pid) { self.pmt_packet(pid, packet)?; } else { // PIDs we don't decode and don't carry (`Stream::Ignored`: a base catalog's @@ -528,13 +529,12 @@ impl Import { Ok(()) } - /// Feed one packet on a PMT PID to its reassembler, applying every good PMT section. + /// Feed one packet on the PMT PID to its reassembler, applying every good PMT section. fn pmt_packet(&mut self, pid: Pid, packet: &Packet) -> anyhow::Result<()> { let mut sections = Vec::new(); - self.pmt_sections - .entry(pid) - .or_default() - .push(packet.pkt, &mut sections); + if let Some((_, reassembler)) = &mut self.pmt { + reassembler.push(packet.pkt, &mut sections); + } for section in sections { // Other tables may share the PID; only a PMT is this path's to check. if section.first() != Some(&psi::PMT_TABLE_ID) { @@ -1536,7 +1536,8 @@ impl Import { } /// Pick the imported program from a whole PAT and learn its PMT PID, so `decode` - /// reassembles that PMT and no other. + /// reassembles that PMT and no other. A PAT naming another PID for the program moves the + /// reassembler there, and the catalog's `program.pmtPid` with it. /// /// Every PAT is checked, so a program added mid-stream ends an unselected import, a /// selected program that disappears ends a selected one, and a PAT naming the program under @@ -1569,11 +1570,23 @@ impl Import { identity.1 ); } - self.pmt_sections.entry(entry.pmt_pid).or_default(); - self.claims_changed = true; + let first = self.identity.replace(identity).is_none(); self.health.pmt_pids(&[entry.pmt_pid.as_u16()], self.liveness.now()); - if self.identity.is_none() { - self.identity = Some(identity); + let moved = self.pmt.as_ref().is_none_or(|(pid, _)| *pid != entry.pmt_pid); + if moved { + if let Some((from, _)) = &self.pmt { + tracing::info!( + program = entry.program_number, + from = from.as_u16(), + to = entry.pmt_pid.as_u16(), + "TS PMT moved to another PID" + ); + } + // A section begun on the old PID no longer speaks for the program. + self.pmt = Some((entry.pmt_pid, SectionReassembler::default())); + self.claims_changed = true; + } + if first || moved { self.record_program_identity(pat.transport_stream_id, entry)?; } Ok(()) @@ -10194,6 +10207,50 @@ pub(super) mod test { } } + /// A PAT that moves the PMT to another PID moves the import with it: the catalog records + /// the new PID, the PMT there revises the program, and the old PID no longer speaks for it. + #[test] + fn a_moved_pmt_is_followed() { + use super::super::fixture::Mux; + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = crate::catalog::Producer::new( + &mut broadcast, + crate::catalog::Config::default() + .with_catalog(crate::catalog::hang::Catalog::::default()), + ) + .unwrap(); + let mut import = super::Import::new(broadcast, catalog.reserve()); + let pmt_pid = || { + catalog + .snapshot() + .ext + .mpegts + .program + .as_ref() + .map(|program| program.pmt_pid) + }; + + let mut mux = Mux::default(); + mux.pat(&[(1, 0x100)]).pmt(0x100, 1, 0x61, &[(MP2, 0x61)], 0); + mux.out.extend(mp2_pes(0x61, 0, 90_000, [0xAA, 0xBB])); + import.decode(&std::mem::take(&mut mux.out)).unwrap(); + assert_eq!(pmt_pid(), Some(0x100)); + + mux.pat(&[(1, 0x200)]) + .pmt(0x200, 1, 0x61, &[(MP2, 0x61), (MP2, 0x62)], 0) + .pmt(0x100, 1, 0x61, &[], 0); + mux.out.extend(mp2_pes(0x61, 1, 108_000, [0xAA, 0xBB])); + mux.out.extend(mp2_pes(0x62, 0, 108_000, [0xCC, 0xDD])); + import.decode(&mux.out).unwrap(); + import.finish().unwrap(); + assert_eq!(pmt_pid(), Some(0x200)); + assert_eq!( + catalog.snapshot().audio.renditions.len(), + 2, + "the PMT on the new PID adds a stream, and the one on the old PID removes none" + ); + } + /// A section-framed PID the PMT drops keeps its stats row, so what it delivered stays counted. #[test] fn a_dropped_section_route_keeps_its_stats_row() { diff --git a/rs/moq-mux/src/container/ts/programs.rs b/rs/moq-mux/src/container/ts/programs.rs index 4ff1e9291b7..6731c9b238a 100644 --- a/rs/moq-mux/src/container/ts/programs.rs +++ b/rs/moq-mux/src/container/ts/programs.rs @@ -7,18 +7,28 @@ use mpeg2ts::ts::Pid; use super::demux::{Demux, Packet}; use super::import::SectionReassembler; -use super::{Ext, Import, catalog::SI_PIDS, psi, stats}; +use super::{Ext, Import, catalog::SI_PIDS, health, psi, stats}; use crate::catalog; -/// Imports every program the first PAT lists, each as its own broadcast on `origin`, with its -/// own clock and catalog. +/// PATs a listed program waits through for a PMT listing a stream before it is warned about, +/// once. Twenty PAT intervals span at least the 0.5 s within which TR 101 290 expects each PMT +/// to repeat, unless the PAT itself repeats more often than every 25 ms. +const LATE_PATS: u32 = 20; + +/// Imports every program the PAT lists, each as its own broadcast on `origin`, with its own +/// clock and catalog. /// /// Program `n` of `event.hang` publishes on `event/n.hang`, keeping the catalog suffix last so /// format detection still sees it; a name without one gains a plain `/n`. A program starts once /// its PMT lists an elementary stream, so one the PAT names but the input never describes costs -/// nothing. Input ahead of that PMT is dropped, as a single [`Import`] drops media ahead of its -/// PSI; its SI arrives with the next repetition, as for an import joining mid-stream. A later PAT moves a waiting program to the PMT PID it names and drops one it no longer -/// lists. A program a later PAT adds is not published; removing a started one ends the import. +/// nothing, and is warned about once if still waiting twenty PATs later. Input ahead of that PMT +/// is dropped, as a single [`Import`] drops media ahead of its PSI; its SI arrives with the next +/// repetition, as for an import joining mid-stream. +/// +/// The PAT is followed as it changes. A program a later PAT adds waits for its PMT as the first +/// PAT's do, and each program follows its PMT to the PID the latest PAT names. One a later PAT +/// drops is retired: its tracks finish and its broadcast is unannounced, while the other +/// programs carry on. A program that returns publishes a new instance, under a new epoch. /// /// The multiplex is parsed once: framing, continuity, the PAT and the SI sections are shared, /// and each program reads only the packets on the PIDs its PAT entry and PMT claim. @@ -29,25 +39,30 @@ pub struct Programs { demux: Demux, /// The latest whole PAT, from the first that lists a program, which seeds every program. pat: Option, - /// The programs the first PAT lists and no PMT has started yet, by the PMT PID the latest - /// PAT names for them. + /// The programs the latest PAT lists and no PMT has started yet, by the PMT PID it names + /// for them. waiting: HashMap, /// PMT sections dropped for a bad CRC on a PID no program read, by PID. crc_errors: BTreeMap, - /// Empty until a PMT starts a program. + /// Empty until a PMT starts a program, and again once a PAT retires every one. programs: Vec, /// The programs reading each PID they claim, indexed by PID, in program order. The PAT and /// the SI PIDs go to every program instead. readers: Vec>, /// The programs the current [`decode`](Self::decode) call routed a packet to. touched: Vec, + /// Whether a retired program had announced. + announced: bool, + /// What retired programs counted beyond the programs still running. + retired: Counts, } /// The programs waiting for their PMT on one PID. #[derive(Default)] struct Waiting { sections: SectionReassembler, - programs: HashSet, + /// Each program's number, with the PATs it has waited through. + programs: HashMap, /// Whether a program read this PID when the section still assembling began, and so counts /// it if it is corrupt. began_read: bool, @@ -90,6 +105,57 @@ impl Program { } } +/// Counters merged across programs. +#[derive(Default)] +struct Counts { + streams: BTreeMap, + /// PMT sections dropped for a bad CRC, by PID. + crc_errors: BTreeMap, + errors: health::Errors, +} + +impl Counts { + /// Fold in a program's counters. Programs sharing a PMT or PCR PID count the same sections + /// and packets, so each count takes the larger. + fn merge(&mut self, import: &Import) { + self.streams.extend(import.stats().streams); + for (&pid, &count) in import.crc_errors() { + let merged = self.crc_errors.entry(pid).or_default(); + *merged = (*merged).max(count); + } + self.errors.max(import.errors()); + } + + /// Add what `earlier` counted on input these counts never read. A row both hold keeps this + /// one's track. + fn add(&mut self, earlier: &Self) { + for (&pid, row) in &earlier.streams { + self.streams + .entry(pid) + .and_modify(|ours| ours.combine(row, u64::saturating_add)) + .or_insert_with(|| row.clone()); + } + for (&pid, &count) in &earlier.crc_errors { + *self.crc_errors.entry(pid).or_default() += count; + } + self.errors.combine(&earlier.errors, u64::saturating_add); + } + + /// What these counts hold beyond `running`'s, which read the same multiplex. + fn beyond(mut self, running: &Self) -> Self { + for (pid, row) in &mut self.streams { + if let Some(theirs) = running.streams.get(pid) { + row.combine(theirs, u64::saturating_sub); + } + } + for (pid, count) in &mut self.crc_errors { + *count = count.saturating_sub(running.crc_errors.get(pid).copied().unwrap_or(0)); + } + self.errors.combine(&running.errors, u64::saturating_sub); + self + } +} + impl Programs { /// Split the multiplex into broadcasts named after `name`, each catalog built from `config`. pub fn new(origin: moq_net::origin::Producer, name: impl moq_net::AsPath, config: catalog::Config) -> Self { @@ -104,6 +170,8 @@ impl Programs { programs: Vec::new(), readers: vec![Vec::new(); 1 << 13], touched: Vec::new(), + announced: false, + retired: Counts::default(), } } @@ -119,7 +187,7 @@ impl Programs { demux.push(data); while let Some(packet) = demux.next() { if let Some(pat) = &packet.pat { - self.wait(pat); + self.follow(pat); } else if !self.waiting.is_empty() { self.describe(&packet)?; } @@ -170,9 +238,11 @@ impl Programs { Ok(()) } - /// Wait for the PMT of each program the first whole PAT listing one names. A later PAT moves - /// a waiting program to the PMT PID it names, and drops one it no longer lists. - fn wait(&mut self, pat: &psi::Pat) { + /// Follow a whole PAT, from the first that lists a program: retire each started program it + /// no longer lists, move each waiting one to the PMT PID it names or drop it, and wait for + /// the PMT of each it adds. A started program follows its own PMT PID. + fn follow(&mut self, pat: &psi::Pat) { + self.wait_longer(); // The PAT repeats several times a second, mostly unchanged. if self.pat.as_ref() == Some(pat) { return; @@ -184,33 +254,121 @@ impl Programs { listed.entry(entry.program_number).or_insert(entry.pmt_pid.as_u16()); } } - if self.pat.is_none() { - if listed.is_empty() { - return; - } - for (&number, &pid) in &listed { - self.waiting.entry(pid).or_default().programs.insert(number); + if self.pat.is_none() && listed.is_empty() { + return; + } + let mut index = 0; + while index < self.programs.len() { + match listed.contains_key(&self.programs[index].number) { + true => index += 1, + false => self.retire(index), } - } else { - let mut moved = Vec::new(); - self.waiting.retain(|pid, waiting| { - waiting.programs.retain(|number| match listed.get(number) { - Some(listed) if listed == pid => true, - Some(&listed) => { - moved.push((*number, listed)); - false - } - None => false, - }); - !waiting.programs.is_empty() + } + let mut moved = Vec::new(); + self.waiting.retain(|pid, waiting| { + waiting.programs.retain(|number, pats| match listed.get(number) { + Some(listed) if listed == pid => true, + Some(&listed) => { + moved.push((*number, listed, *pats)); + false + } + None => false, }); - for (number, pid) in moved { - self.waiting.entry(pid).or_default().programs.insert(number); + !waiting.programs.is_empty() + }); + for (number, pid, pats) in moved { + self.waiting.entry(pid).or_default().programs.insert(number, pats); + } + let known: HashSet = self + .programs + .iter() + .map(|program| program.number) + .chain( + self.waiting + .values() + .flat_map(|waiting| waiting.programs.keys().copied()), + ) + .collect(); + for (&number, &pid) in &listed { + if known.contains(&number) { + continue; } + if self.pat.is_some() { + tracing::info!(program = number, pmt_pid = pid, "TS program joined the PAT"); + } + self.waiting.entry(pid).or_default().programs.insert(number, 0); } self.pat = Some(pat.clone()); } + /// Count a PAT against every waiting program, and warn once about those it makes late. + fn wait_longer(&mut self) { + let mut late = Vec::new(); + for waiting in self.waiting.values_mut() { + for (&number, pats) in &mut waiting.programs { + *pats = pats.saturating_add(1); + if *pats == LATE_PATS { + late.push(number); + } + } + } + if late.is_empty() { + return; + } + late.sort_unstable(); + tracing::warn!( + count = late.len(), + programs = ?&late[..late.len().min(10)], + pats = LATE_PATS, + "TS programs the PAT lists have no PMT listing a stream yet; they wait, unannounced" + ); + } + + /// End program `index`, which the PAT no longer lists: finish its tracks, then its catalog, + /// and close its broadcast, so it is unannounced now rather than lingering. Subscribers + /// already reading a track read it to its end, and the other programs carry on whatever + /// this one left unfinished. What it counted beyond them is kept. + fn retire(&mut self, index: usize) { + let mut program = self.programs.remove(index); + for &pid in &program.claims { + self.readers[usize::from(pid)].retain(|&reader| reader != index); + } + // Each later program moves down a place, in order, so no two share an index meanwhile. + for (at, later) in self.programs.iter().enumerate().skip(index) { + for &pid in &later.claims { + for reader in &mut self.readers[usize::from(pid)] { + if *reader == at + 1 { + *reader = at; + } + } + } + } + self.touched.retain(|&touched| touched != index); + for touched in &mut self.touched { + if *touched > index { + *touched -= 1; + } + } + tracing::info!( + program = program.number, + "TS program left the PAT; retiring its broadcast" + ); + self.announced |= program.pending.is_none(); + let finished = program + .import + .finish() + .and_then(|()| program.catalog.finish().map_err(anyhow::Error::from)); + if let Err(err) = finished { + tracing::warn!(program = program.number, error = %err, "TS program retired unfinished"); + } + program.broadcast.close(); + let mut counts = Counts::default(); + counts.merge(&program.import); + let mut retired = counts.beyond(&self.running()); + retired.add(&self.retired); + self.retired = retired; + } + /// Read a packet on a waiting PMT PID, and start each program whose PMT it completes with a /// stream listed. /// @@ -246,7 +404,7 @@ impl Programs { continue; } if let Some(pmt) = psi::Pmt::parse(§ion) - && waiting.programs.contains(&pmt.program_number) + && waiting.programs.contains_key(&pmt.program_number) { latest.retain(|seen| seen.program_number != pmt.program_number); latest.push(pmt); @@ -334,34 +492,42 @@ impl Programs { /// The multiplex is graded once. Programs sharing a PMT PID read the same PMT sections, so /// a dropped section counts once however many read it, and programs sharing a PCR PID /// count its faults once too. PMT sections dropped before any program read their PID are - /// counted here. + /// counted here, and so is what a retired program counted beyond the programs still + /// running, so no total goes back when a program leaves or returns. pub fn stats(&self) -> stats::Snapshot { - let mut stats = stats::Snapshot::default(); - let mut crc_errors = BTreeMap::::new(); + let mut counts = self.running(); + counts.add(&self.retired); let mut errors = self.demux.errors().clone(); - for program in &self.programs { - stats.streams.extend(program.import.stats().streams); - for (&pid, &count) in program.import.crc_errors() { - let merged = crc_errors.entry(pid).or_default(); - *merged = (*merged).max(count); - } - errors.max(program.import.errors()); - } - stats.crc_error = crc_errors.values().chain(self.crc_errors.values()).sum::() + self.demux.pat_crc_error(); + errors.max(&counts.errors); + let mut stats = stats::Snapshot { + streams: counts.streams, + ..Default::default() + }; + stats.crc_error = + counts.crc_errors.values().chain(self.crc_errors.values()).sum::() + self.demux.pat_crc_error(); errors.report(&mut stats); stats } + /// The running programs' counters, merged. + fn running(&self) -> Counts { + let mut counts = Counts::default(); + for program in &self.programs { + counts.merge(&program.import); + } + counts + } + /// Finish each program's tracks, then its catalog while it still lists them. /// - /// Returns each program's broadcast. The finished tracks stay servable while this importer - /// is held, so hand both to a [`Linger`](crate::Linger) to keep them up for late - /// subscribers. Fails when the input ended before its PAT listed a program or any program - /// declared a track. + /// Returns each running program's broadcast; a retired one has already closed. The finished + /// tracks stay servable while this importer is held, so hand both to a + /// [`Linger`](crate::Linger) to keep them up for late subscribers. Fails when the input + /// ended before its PAT listed a program or any program declared a track. pub fn finish(&mut self) -> anyhow::Result> { self.demux.finish(); anyhow::ensure!(self.pat.is_some(), "input ended before its PAT listed a program"); - let mut announced = false; + let mut announced = self.announced; for program in &mut self.programs { program.import.finish()?; announced |= program.announce_if_listed()?; @@ -398,10 +564,176 @@ mod test { use super::*; use crate::catalog::hang::Container; use crate::container::Consumer; + use crate::container::ts::fixture::Mux; use crate::container::ts::import::test::{ MP2, corrupt, fifty_programs, mp2_pes, pat_section, pmt_section, psi_packets, two_programs, two_section_pat, }; + /// The epoch each running program publishes under, announced yet or not. + fn epochs(programs: &Programs) -> Vec<(u16, moq_net::Epoch)> { + programs + .programs + .iter() + .map(|program| { + let route = program.broadcast.route(); + let route = route.as_ref().or(program.pending.as_ref().map(|(route, _)| route)); + ( + program.number, + route.and_then(|route| route.epoch.clone()).expect("an epoch"), + ) + }) + .collect() + } + + /// Steps of a two-program multiplex: each a PAT listing `listed`, both programs' PMTs, and an + /// MP2 PES of two frames on each program's audio, whether or not the PAT lists it. + struct Steps { + mux: Mux, + cc: [u8; 2], + pts: u64, + } + + impl Steps { + fn new() -> Self { + Self { + mux: Mux::default(), + cc: [0; 2], + pts: 90_000, + } + } + + fn step(&mut self, programs: &mut Programs, listed: &[(u16, u16)]) { + self.mux + .pat(listed) + .pmt(0x100, 1, 0x61, &[(MP2, 0x61)], 0) + .pmt(0x200, 2, 0x71, &[(MP2, 0x71)], 0); + for (cc, pid) in self.cc.iter_mut().zip([0x61, 0x71]) { + self.mux.out.extend(mp2_pes(pid, *cc, self.pts, [pid as u8; 2])); + *cc = (*cc + 1) & 0x0f; + } + // Past both 72 ms frames. + self.pts += 18_000; + programs.decode(&std::mem::take(&mut self.mux.out)).unwrap(); + } + } + + /// A program a later PAT adds publishes, one it drops retires alone, its catalog finished and + /// its broadcast closed, and one it restores publishes a new instance that counts on from the + /// retired one, while the program no PAT touched runs throughout. + #[tokio::test] + async fn programs_follow_the_pat() { + let origin = crate::source::produce_origin(); + let mut programs = Programs::new(origin, "event.hang", catalog::Config::default()); + let mut steps = Steps::new(); + + steps.step(&mut programs, &[(1, 0x100)]); + let one = epochs(&programs); + assert_eq!(one.len(), 1); + + steps.step(&mut programs, &[(1, 0x100), (2, 0x200)]); + let both = epochs(&programs); + assert_eq!(both[0], one[0], "program 1 carries on"); + assert_eq!(both[1].0, 2, "the added program publishes"); + let two = programs.programs[1].broadcast.consume(); + let mut catalog = hang::catalog::Catalog::<()>::subscribe(&two).await.unwrap(); + + steps.step(&mut programs, &[(1, 0x100)]); + assert_eq!(epochs(&programs), one, "program 1 carries on"); + assert!(two.is_closed(), "the dropped program is unannounced"); + while catalog.next().await.unwrap().is_some() {} + assert_eq!( + programs.stats().streams[&0x71].units, + 2, + "the retired program's frames stay counted" + ); + + steps.step(&mut programs, &[(1, 0x100), (2, 0x200)]); + let back = epochs(&programs); + assert_eq!(back[0], one[0], "program 1 carries on"); + assert_eq!(back[1].0, 2); + assert_ne!(back[1].1, both[1].1, "the returning program is a new instance"); + + programs.finish().unwrap(); + let stats = programs.stats(); + assert_eq!(stats.streams[&0x61].units, 8, "program 1 delivered every frame"); + assert_eq!(stats.streams[&0x71].units, 4, "the returning program counts on"); + } + + /// A PAT that drops a program reaches no importer of it, so the multiplex carries on rather + /// than ending on that program's refusal, and the program after it keeps reading its PIDs. + #[test] + fn a_dropped_program_does_not_end_the_multiplex() { + let origin = crate::source::produce_origin(); + let mut programs = Programs::new(origin, "event.hang", catalog::Config::default()); + let mut steps = Steps::new(); + steps.step(&mut programs, &[(1, 0x100), (2, 0x200)]); + // Fed a packet at a time, so the PAT that drops program 1 completes in a call of its own. + steps.mux.pat(&[(2, 0x200)]); + steps.mux.out.extend(mp2_pes(0x71, 1, 108_000, [0x71; 2])); + for packet in std::mem::take(&mut steps.mux.out).chunks(188) { + programs.decode(packet).unwrap(); + } + let running: Vec<_> = epochs(&programs).into_iter().map(|(number, _)| number).collect(); + assert_eq!(running, [2]); + programs.finish().unwrap(); + assert_eq!(programs.stats().streams[&0x71].units, 4, "program 2 read on"); + } + + /// A program whose PMT never arrives waits, unannounced, holding up neither the others nor + /// the end of the input, and is warned about once it has waited through enough PATs. + #[test] + #[tracing_test::traced_test] + fn a_program_without_a_pmt_waits_and_is_warned_about_once() { + let origin = crate::source::produce_origin(); + let mut programs = Programs::new(origin, "event.hang", catalog::Config::default()); + let mut steps = Steps::new(); + const WARNING: &str = "no PMT listing a stream yet"; + for _ in 0..LATE_PATS { + steps.step(&mut programs, &[(1, 0x100), (3, 0x300)]); + } + assert!(!logs_contain(WARNING), "warned before it was late"); + for _ in 0..LATE_PATS { + steps.step(&mut programs, &[(1, 0x100), (3, 0x300)]); + } + programs.finish().unwrap(); + let running: Vec<_> = epochs(&programs).into_iter().map(|(number, _)| number).collect(); + assert_eq!(running, [1], "program 3 publishes nothing"); + logs_assert(|lines: &[&str]| { + let warned: Vec<_> = lines.iter().filter(|line| line.contains(WARNING)).collect(); + match warned.as_slice() { + [line] if line.contains("[3]") => Ok(()), + _ => Err(format!("expected one warning naming program 3, got {warned:?}")), + } + }); + } + + /// A retired program's counts stay in the totals, once: a PMT PID it shared with a program + /// still running is that program's to count, and its own PMT PID keeps its count. + #[test] + fn a_retired_program_keeps_what_it_alone_counted() { + let origin = crate::source::produce_origin(); + let mut programs = Programs::new(origin, "event.hang", catalog::Config::default()); + let pmt = |program: u16| pmt_section(program, 0, &[(MP2, 0x0060 + program, &[])]); + let mut cc = [0; 3]; + let listed = pat_section(&[(1, 0x0100), (2, 0x0100), (3, 0x0300)]); + let mut data = psi_packets(0, &mut cc[0], &[], &listed); + let mut shared = pmt(1); + shared.extend(pmt(2)); + data.extend(psi_packets(0x0100, &mut cc[1], &[], &shared)); + data.extend(psi_packets(0x0300, &mut cc[2], &[], &pmt(3))); + data.extend(psi_packets(0x0100, &mut cc[1], &[], &corrupt(pmt(2)))); + data.extend(psi_packets(0x0300, &mut cc[2], &[], &corrupt(pmt(3)))); + programs.decode(&data).unwrap(); + assert_eq!(programs.programs.len(), 3); + assert_eq!(programs.stats().crc_error, 2); + + programs + .decode(&psi_packets(0, &mut cc[0], &[], &pat_section(&[(1, 0x0100)]))) + .unwrap(); + assert_eq!(programs.programs.len(), 1); + assert_eq!(programs.stats().crc_error, 2, "each dropped section still counts once"); + } + #[tokio::test(start_paused = true)] async fn internal_program_abort_preserves_the_catalog_error() { let origin = crate::source::produce_origin(); diff --git a/rs/moq-mux/src/container/ts/stats.rs b/rs/moq-mux/src/container/ts/stats.rs index f86c790ee02..1ff0bc71b52 100644 --- a/rs/moq-mux/src/container/ts/stats.rs +++ b/rs/moq-mux/src/container/ts/stats.rs @@ -202,6 +202,23 @@ impl Stream { self.discarded += current.discarded; self.unconfirmed += current.unconfirmed; } + + /// Set each delivery, damage and frame-sync count to `f` of it and `other`'s, keeping this + /// row's track, class and quiet. The TR 101 290 shares are left alone: they are written + /// from the merged checks. + pub(super) fn combine(&mut self, other: &Self, f: impl Fn(u64, u64) -> u64) { + for (ours, theirs) in [ + (&mut self.units, other.units), + (&mut self.resyncs, other.resyncs), + (&mut self.discarded, other.discarded), + (&mut self.unconfirmed, other.unconfirmed), + (&mut self.damaged, other.damaged), + (&mut self.unplaced, other.unplaced), + (&mut self.refused, other.refused), + ] { + *ours = f(*ours, theirs); + } + } } /// Audio, video, or other data, as import or export already resolved the PID. From 7227f8ac15d8e1d98b0cdadd1b31e9f20dbf2ade Mon Sep 17 00:00:00 2001 From: t0ms Date: Sat, 24 Oct 2026 23:01:01 +0100 Subject: [PATCH 3/5] fix(moq-mux): a PAT change refuses as an import does, and shared TS rows count once Co-authored-by: Claude Opus 5.5 --- rs/moq-mux/src/container/ts/fixture.rs | 7 +- rs/moq-mux/src/container/ts/import.rs | 2 +- rs/moq-mux/src/container/ts/programs.rs | 141 +++++++++++++++++++----- 3 files changed, 123 insertions(+), 27 deletions(-) diff --git a/rs/moq-mux/src/container/ts/fixture.rs b/rs/moq-mux/src/container/ts/fixture.rs index 126d9eaa25e..1d6831c0afd 100644 --- a/rs/moq-mux/src/container/ts/fixture.rs +++ b/rs/moq-mux/src/container/ts/fixture.rs @@ -182,11 +182,16 @@ impl Mux { } pub(super) fn pat(&mut self, programs: &[(u16, u16)]) -> &mut Self { + self.pat_in(1, programs) + } + + /// A [`Self::pat`] naming `transport_stream_id`. + pub(super) fn pat_in(&mut self, transport_stream_id: u16, programs: &[(u16, u16)]) -> &mut Self { let body: Vec = programs .iter() .flat_map(|&(number, pmt)| [(number >> 8) as u8, number as u8, 0xe0 | (pmt >> 8) as u8, pmt as u8]) .collect(); - self.section(0, &psi::section(psi::PAT_TABLE_ID, 1, 0, 0, 0, &body)) + self.section(0, &psi::section(psi::PAT_TABLE_ID, transport_stream_id, 0, 0, 0, &body)) } /// A PMT whose `program_info` holds `info` bytes of private descriptors, so a large one diff --git a/rs/moq-mux/src/container/ts/import.rs b/rs/moq-mux/src/container/ts/import.rs index b84e5a4e7a5..ab80917efb3 100644 --- a/rs/moq-mux/src/container/ts/import.rs +++ b/rs/moq-mux/src/container/ts/import.rs @@ -4301,7 +4301,7 @@ pub(super) mod test { const RECORDING_MAX_AGE: std::time::Duration = Duration::from_secs(30); // libklvanc public-sample cue: table_id 0xFC, section_length 0x1b (27), 30 bytes total. - const CUE: [u8; 30] = [ + pub(in crate::container::ts) const CUE: [u8; 30] = [ 0xfc, 0x30, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xf0, 0x0a, 0x05, 0x00, 0x00, 0x2b, 0xb4, 0x7f, 0xdf, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xad, 0x25, 0xe8, 0x39, ]; diff --git a/rs/moq-mux/src/container/ts/programs.rs b/rs/moq-mux/src/container/ts/programs.rs index 6731c9b238a..d1c0daa3334 100644 --- a/rs/moq-mux/src/container/ts/programs.rs +++ b/rs/moq-mux/src/container/ts/programs.rs @@ -115,10 +115,15 @@ struct Counts { } impl Counts { - /// Fold in a program's counters. Programs sharing a PMT or PCR PID count the same sections - /// and packets, so each count takes the larger. + /// Fold in a program's counters. Programs sharing a PMT, PCR or elementary PID count the + /// same sections and packets, each since it started, so each count takes the larger. fn merge(&mut self, import: &Import) { - self.streams.extend(import.stats().streams); + for (pid, row) in import.stats().streams { + self.streams + .entry(pid) + .and_modify(|ours| ours.combine(&row, u64::max)) + .or_insert(row); + } for (&pid, &count) in import.crc_errors() { let merged = self.crc_errors.entry(pid).or_default(); *merged = (*merged).max(count); @@ -187,7 +192,7 @@ impl Programs { demux.push(data); while let Some(packet) = demux.next() { if let Some(pat) = &packet.pat { - self.follow(pat); + self.follow(pat)?; } else if !self.waiting.is_empty() { self.describe(&packet)?; } @@ -241,11 +246,22 @@ impl Programs { /// Follow a whole PAT, from the first that lists a program: retire each started program it /// no longer lists, move each waiting one to the PMT PID it names or drop it, and wait for /// the PMT of each it adds. A started program follows its own PMT PID. - fn follow(&mut self, pat: &psi::Pat) { + /// + /// Fails on a PAT naming another transport stream while a program runs, as each program's + /// importer would, and when a retired program cannot finish. + fn follow(&mut self, pat: &psi::Pat) -> anyhow::Result<()> { self.wait_longer(); // The PAT repeats several times a second, mostly unchanged. if self.pat.as_ref() == Some(pat) { - return; + return Ok(()); + } + if let Some(running) = self.pat.as_ref().filter(|_| !self.programs.is_empty()) { + anyhow::ensure!( + running.transport_stream_id == pat.transport_stream_id, + "the PAT changed transport_stream_id from {} to {} while its programs run, which their carried service information does not describe", + running.transport_stream_id, + pat.transport_stream_id + ); } let mut listed = HashMap::new(); for entry in &pat.programs { @@ -255,13 +271,13 @@ impl Programs { } } if self.pat.is_none() && listed.is_empty() { - return; + return Ok(()); } let mut index = 0; while index < self.programs.len() { match listed.contains_key(&self.programs[index].number) { true => index += 1, - false => self.retire(index), + false => self.retire(index)?, } } let mut moved = Vec::new(); @@ -299,6 +315,7 @@ impl Programs { self.waiting.entry(pid).or_default().programs.insert(number, 0); } self.pat = Some(pat.clone()); + Ok(()) } /// Count a PAT against every waiting program, and warn once about those it makes late. @@ -326,10 +343,20 @@ impl Programs { /// End program `index`, which the PAT no longer lists: finish its tracks, then its catalog, /// and close its broadcast, so it is unannounced now rather than lingering. Subscribers - /// already reading a track read it to its end, and the other programs carry on whatever - /// this one left unfinished. What it counted beyond them is kept. - fn retire(&mut self, index: usize) { - let mut program = self.programs.remove(index); + /// already reading a track read it to its end, and the other programs carry on. What it + /// counted beyond them is kept. + /// + /// Fails when the program cannot finish, as an import does at the end of its input. + fn retire(&mut self, index: usize) -> anyhow::Result<()> { + let program = &mut self.programs[index]; + tracing::info!( + program = program.number, + "TS program left the PAT; retiring its broadcast" + ); + // A refusal leaves the program listed, so the caller's abort reaches it with the rest. + program.import.finish()?; + program.catalog.finish()?; + let program = self.programs.remove(index); for &pid in &program.claims { self.readers[usize::from(pid)].retain(|&reader| reader != index); } @@ -349,24 +376,14 @@ impl Programs { *touched -= 1; } } - tracing::info!( - program = program.number, - "TS program left the PAT; retiring its broadcast" - ); self.announced |= program.pending.is_none(); - let finished = program - .import - .finish() - .and_then(|()| program.catalog.finish().map_err(anyhow::Error::from)); - if let Err(err) = finished { - tracing::warn!(program = program.number, error = %err, "TS program retired unfinished"); - } program.broadcast.close(); let mut counts = Counts::default(); counts.merge(&program.import); let mut retired = counts.beyond(&self.running()); retired.add(&self.retired); self.retired = retired; + Ok(()) } /// Read a packet on a waiting PMT PID, and start each program whose PMT it completes with a @@ -564,9 +581,10 @@ mod test { use super::*; use crate::catalog::hang::Container; use crate::container::Consumer; - use crate::container::ts::fixture::Mux; + use crate::container::ts::fixture::{Flags, Mux}; use crate::container::ts::import::test::{ - MP2, corrupt, fifty_programs, mp2_pes, pat_section, pmt_section, psi_packets, two_programs, two_section_pat, + CUE, MP2, corrupt, fifty_programs, mp2_pes, pat_section, pmt_section, psi_packets, two_programs, + two_section_pat, }; /// The epoch each running program publishes under, announced yet or not. @@ -734,6 +752,79 @@ mod test { assert_eq!(programs.stats().crc_error, 2, "each dropped section still counts once"); } + /// Programs sharing an elementary PID count its units once, each from its own start, so the + /// earliest reader's count stands, while they all run and as they leave. + #[test] + fn programs_sharing_a_stream_count_it_once() { + let origin = crate::source::produce_origin(); + let mut programs = Programs::new(origin, "event.hang", catalog::Config::default()); + let all = [(1, 0x100), (2, 0x200), (3, 0x300)]; + let mut mux = Mux::default(); + // Each PMT arrives a PES after the one before, so each program counts less. + for (cc, &(program, pid)) in (0..).zip(&all) { + mux.pat(&all).pmt(pid, program, 0x61, &[(MP2, 0x61)], 0); + mux.out + .extend(mp2_pes(0x61, cc, 90_000 + u64::from(cc) * 18_000, [0x61; 2])); + programs.decode(&std::mem::take(&mut mux.out)).unwrap(); + } + assert_eq!(programs.programs.len(), 3); + assert_eq!(programs.stats().streams[&0x61].units, 6); + for listed in [&all[1..], &all[2..]] { + mux.pat(listed); + programs.decode(&std::mem::take(&mut mux.out)).unwrap(); + assert_eq!(programs.programs.len(), listed.len()); + assert_eq!(programs.stats().streams[&0x61].units, 6, "{} running", listed.len()); + } + } + + /// A PAT naming another transport stream is refused while a program runs, before it retires + /// one, even when it lists none of them. + #[test] + fn a_pat_naming_another_transport_stream_is_refused() { + let origin = crate::source::produce_origin(); + let mut programs = Programs::new(origin, "event.hang", catalog::Config::default()); + let mut steps = Steps::new(); + steps.step(&mut programs, &[(1, 0x100)]); + steps.mux.pat_in(2, &[(2, 0x200)]); + let err = programs.decode(&std::mem::take(&mut steps.mux.out)).unwrap_err(); + assert!(format!("{err:#}").contains("transport_stream_id"), "{err:#}"); + assert_eq!(programs.programs.len(), 1, "a refused PAT retires nothing"); + } + + /// A dropped program that cannot finish refuses the multiplex, as at the end of the input, + /// and stays listed for the abort that follows, so its subscribers see the cause. + #[tokio::test(start_paused = true)] + async fn a_retired_program_that_cannot_finish_refuses_the_multiplex() { + let origin = crate::source::produce_origin(); + let mut programs = Programs::new(origin, "event.hang", catalog::Config::default()); + // Program 2 carries a clock on 0x71 and SCTE-35 on 0x72, and no PTS to place a cue by. + let mut cuei = vec![0xe0, 0x71, 0xf0, 6, 0x05, 4]; + cuei.extend_from_slice(b"CUEI"); + cuei.extend_from_slice(&[0x02, 0xe0, 0x71, 0xf0, 0, 0x86, 0xe0, 0x72, 0xf0, 0]); + let mut mux = Mux::default(); + mux.pat(&[(1, 0x100), (2, 0x200)]) + .pmt(0x100, 1, 0x61, &[(MP2, 0x61)], 0) + .section(0x200, &psi::section(psi::PMT_TABLE_ID, 2, 0, 0, 0, &cuei)) + .packet(0x71, Flags::pcr(27_000_000)) + .section(0x72, &CUE); + mux.out.extend(mp2_pes(0x61, 0, 90_000, [0x61; 2])); + programs.decode(&std::mem::take(&mut mux.out)).unwrap(); + assert_eq!(programs.programs.len(), 2); + let mut catalog = programs.programs[1].catalog.consume().unwrap(); + + mux.pat(&[(1, 0x100)]); + let err = programs.decode(&std::mem::take(&mut mux.out)).unwrap_err(); + assert!(format!("{err:#}").contains("without a media timestamp"), "{err:#}"); + programs.abort(moq_net::Error::Transport("decode failed".into())); + let err = loop { + match catalog.next().await { + Ok(Some(_)) => {} + other => break other.unwrap_err(), + } + }; + assert!(matches!(err, crate::Error::Moq(moq_net::Error::Transport(ref reason)) if reason == "decode failed")); + } + #[tokio::test(start_paused = true)] async fn internal_program_abort_preserves_the_catalog_error() { let origin = crate::source::produce_origin(); From 384e7aa962d34abf5c51d88b151ceaf9afeec6e7 Mon Sep 17 00:00:00 2001 From: t0ms Date: Sat, 24 Oct 2026 23:44:56 +0100 Subject: [PATCH 4/5] fix(moq-mux): retire every program a PAT drops in one pass, announced first Co-authored-by: Claude Opus 5.5 --- rs/moq-mux/src/container/ts/programs.rs | 144 +++++++++++++++++------- 1 file changed, 104 insertions(+), 40 deletions(-) diff --git a/rs/moq-mux/src/container/ts/programs.rs b/rs/moq-mux/src/container/ts/programs.rs index d1c0daa3334..8bb83da03b6 100644 --- a/rs/moq-mux/src/container/ts/programs.rs +++ b/rs/moq-mux/src/container/ts/programs.rs @@ -273,13 +273,7 @@ impl Programs { if self.pat.is_none() && listed.is_empty() { return Ok(()); } - let mut index = 0; - while index < self.programs.len() { - match listed.contains_key(&self.programs[index].number) { - true => index += 1, - false => self.retire(index)?, - } - } + self.retire(&listed)?; let mut moved = Vec::new(); self.waiting.retain(|pid, waiting| { waiting.programs.retain(|number, pats| match listed.get(number) { @@ -341,45 +335,66 @@ impl Programs { ); } - /// End program `index`, which the PAT no longer lists: finish its tracks, then its catalog, - /// and close its broadcast, so it is unannounced now rather than lingering. Subscribers - /// already reading a track read it to its end, and the other programs carry on. What it - /// counted beyond them is kept. + /// End every started program `listed` no longer names: finish its tracks, announcing it + /// as the end of the input would, then its catalog, and close its broadcast, so it is + /// unannounced now rather than lingering. Subscribers already reading a track read it to + /// its end, and the other programs carry on. What the retired counted beyond them is kept. /// - /// Fails when the program cannot finish, as an import does at the end of its input. - fn retire(&mut self, index: usize) -> anyhow::Result<()> { - let program = &mut self.programs[index]; - tracing::info!( - program = program.number, - "TS program left the PAT; retiring its broadcast" - ); - // A refusal leaves the program listed, so the caller's abort reaches it with the rest. - program.import.finish()?; - program.catalog.finish()?; - let program = self.programs.remove(index); - for &pid in &program.claims { - self.readers[usize::from(pid)].retain(|&reader| reader != index); + /// The programs and the PIDs they read are rebuilt once, so a PAT costs what the programs + /// claim however many it drops. Fails when a program cannot finish, as an import does at + /// the end of its input. + fn retire(&mut self, listed: &HashMap) -> anyhow::Result<()> { + let leaving = |program: &Program| !listed.contains_key(&program.number); + if !self.programs.iter().any(leaving) { + return Ok(()); } - // Each later program moves down a place, in order, so no two share an index meanwhile. - for (at, later) in self.programs.iter().enumerate().skip(index) { - for &pid in &later.claims { - for reader in &mut self.readers[usize::from(pid)] { - if *reader == at + 1 { - *reader = at; - } - } + // A refusal leaves every program listed, so the caller's abort reaches them all. + for program in self.programs.iter_mut().filter(|program| leaving(program)) { + tracing::info!( + program = program.number, + "TS program left the PAT; retiring its broadcast" + ); + program.import.finish()?; + self.announced |= program.announce_if_listed()?; + program.catalog.finish()?; + } + let mut kept = 0; + let moved: Vec> = self + .programs + .iter() + .map(|program| { + (!leaving(program)).then(|| { + kept += 1; + kept - 1 + }) + }) + .collect(); + for program in &self.programs { + for &pid in &program.claims { + self.readers[usize::from(pid)].clear(); } } - self.touched.retain(|&touched| touched != index); - for touched in &mut self.touched { - if *touched > index { - *touched -= 1; + let (gone, programs): (Vec<_>, Vec<_>) = std::mem::take(&mut self.programs) + .into_iter() + .partition(|program| leaving(program)); + self.programs = programs; + for (index, program) in self.programs.iter().enumerate() { + for &pid in &program.claims { + self.readers[usize::from(pid)].push(index); } } - self.announced |= program.pending.is_none(); - program.broadcast.close(); + self.touched.retain_mut(|index| match moved[*index] { + Some(to) => { + *index = to; + true + } + None => false, + }); let mut counts = Counts::default(); - counts.merge(&program.import); + for program in &gone { + program.broadcast.close(); + counts.merge(&program.import); + } let mut retired = counts.beyond(&self.running()); retired.add(&self.retired); self.retired = retired; @@ -504,7 +519,7 @@ impl Programs { program.claims = claims; } - /// Every program's counters in one map: PIDs are unique across a multiplex. + /// Every program's counters in one map, a PID several programs read counted once. /// /// The multiplex is graded once. Programs sharing a PMT PID read the same PMT sections, so /// a dropped section counts once however many read it, and programs sharing a PCR PID @@ -777,6 +792,55 @@ mod test { } } + /// One PAT dropping programs on either side of those it keeps rebuilds who reads each PID + /// at once: the programs left read on, their own PIDs and the shared ones alike. + #[test] + fn a_pat_dropping_several_programs_keeps_the_rest_reading() { + let origin = crate::source::produce_origin(); + let mut programs = Programs::new(origin, "event.hang", catalog::Config::default()); + let mut mux = Mux::default(); + let step = |mux: &mut Mux, listed: &[(u16, u16)], cc: u8| { + mux.pat(listed); + for (program, pid) in [(1, 0x100), (2, 0x100), (3, 0x300), (4, 0x100)] { + mux.pmt(pid, program, 0x61, &[(MP2, 0x61), (MP2, 0x70 + program)], 0); + } + for pid in [0x61, 0x71, 0x72, 0x73, 0x74] { + mux.out + .extend(mp2_pes(pid, cc, 90_000 + u64::from(cc) * 18_000, [pid as u8; 2])); + } + }; + step(&mut mux, &[(1, 0x100), (2, 0x100), (3, 0x300), (4, 0x100)], 0); + programs.decode(&std::mem::take(&mut mux.out)).unwrap(); + assert_eq!(programs.programs.len(), 4); + step(&mut mux, &[(2, 0x100), (4, 0x100)], 1); + programs.decode(&std::mem::take(&mut mux.out)).unwrap(); + programs.finish().unwrap(); + let running: Vec<_> = epochs(&programs).into_iter().map(|(number, _)| number).collect(); + assert_eq!(running, [2, 4]); + let units = |pid| programs.stats().streams[&pid].units; + assert_eq!([0x61, 0x72, 0x74].map(units), [4; 3], "the programs kept read on"); + assert_eq!( + [0x71, 0x73].map(units), + [2; 2], + "the dropped programs' frames stay counted" + ); + } + + /// A program that starts and leaves within one call is announced as it retires, as the + /// same bytes fed in two calls announce it, so the input declared a track. + #[test] + fn a_program_that_starts_and_leaves_in_one_call_is_announced() { + let origin = crate::source::produce_origin(); + let mut programs = Programs::new(origin, "event.hang", catalog::Config::default()); + let mut mux = Mux::default(); + mux.pat(&[(1, 0x100)]).pmt(0x100, 1, 0x61, &[(MP2, 0x61)], 0); + mux.out.extend(mp2_pes(0x61, 0, 90_000, [0x61; 2])); + mux.pat(&[]); + programs.decode(&mux.out).unwrap(); + assert!(programs.programs.is_empty()); + programs.finish().unwrap(); + } + /// A PAT naming another transport stream is refused while a program runs, before it retires /// one, even when it lists none of them. #[test] From 5523803ec71bc5152a05e1c2884c659409430ebf Mon Sep 17 00:00:00 2001 From: t0ms Date: Sun, 25 Oct 2026 19:11:42 +0000 Subject: [PATCH 5/5] fix(mux): refuse a listed program whose PMT is 0.5 s late, and count a shared tail once A program the PAT lists whose PMT has not arrived 0.5 s of program clock later refuses the multiplex, the TR 101 290 PMT_error_2 bound, timed on a running program's clock; one whose PMT arrived listing no stream waits. A retired program's rows for a PID a running program still reads are taken before its finish, since that program delivers the tail the finish flushed. Co-authored-by: Claude Opus 5.5 --- doc/bin/cli.md | 3 +- rs/moq-mux/src/container/ts/health.rs | 2 +- rs/moq-mux/src/container/ts/import.rs | 9 +- rs/moq-mux/src/container/ts/programs.rs | 217 +++++++++++++++++------- 4 files changed, 169 insertions(+), 62 deletions(-) diff --git a/doc/bin/cli.md b/doc/bin/cli.md index 4c386824a91..6990eee71bc 100644 --- a/doc/bin/cli.md +++ b/doc/bin/cli.md @@ -115,7 +115,8 @@ 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`), following the PAT: a program it adds publishes, one it drops -ends, and one that returns publishes as a new instance. A damaged packet is +ends, and one that returns publishes as a new instance. A listed program whose +PMT does not arrive within 0.5 s of program clock ends the import. A damaged packet is dropped on its own PID, so video freezes until its next keyframe while the other tracks carry on. The importer also logs feed health (stalled streams, TR 101 290 errors) without changing what it diff --git a/rs/moq-mux/src/container/ts/health.rs b/rs/moq-mux/src/container/ts/health.rs index 84fa1bf5f09..766f37036c2 100644 --- a/rs/moq-mux/src/container/ts/health.rs +++ b/rs/moq-mux/src/container/ts/health.rs @@ -24,7 +24,7 @@ use super::stats::Snapshot; const PCR_HZ: u64 = 27_000_000; /// `PAT_error_2` and `PMT_error_2`: each table recurs at least every 0.5 s. -const TABLE_LIMIT: u64 = PCR_HZ / 2; +pub(super) const TABLE_LIMIT: u64 = PCR_HZ / 2; /// `PCR_repetition_error` and `PCR_discontinuity_indicator_error`: 100 ms. The 40 ms limit /// left TS 101 154 in 2005. const PCR_LIMIT: u64 = PCR_HZ / 10; diff --git a/rs/moq-mux/src/container/ts/import.rs b/rs/moq-mux/src/container/ts/import.rs index db6bbb7f68a..b93b37d4421 100644 --- a/rs/moq-mux/src/container/ts/import.rs +++ b/rs/moq-mux/src/container/ts/import.rs @@ -383,6 +383,11 @@ impl Import { pids } + /// PCR ticks elapsed on the program clock since its first PCR, or `None` before one. + pub(super) fn clock(&self) -> Option { + self.liveness.now() + } + /// Whether [`claims`](Self::claims) may have changed since the last call. pub(super) fn claims_changed(&mut self) -> bool { std::mem::take(&mut self.claims_changed) @@ -5759,7 +5764,7 @@ pub(super) mod test { } /// Annex-B bytes for one access unit: SPS + PPS + IDR for a keyframe, else a delta slice. - fn annexb_au(keyframe: bool) -> Vec { + pub(in crate::container::ts) fn annexb_au(keyframe: bool) -> Vec { use crate::container::test_util::{IDR, PPS, SPS}; let nals: &[&[u8]] = if keyframe { &[SPS, PPS, IDR] @@ -8389,7 +8394,7 @@ pub(super) mod test { /// A PUSI TS packet on `pid` carrying one bounded video PES (stream_id 0xE0) with `pts` /// and, when it differs, `dts`, sized to complete on this packet. - fn video_pes(pid: u16, cc: u8, pts: u64, dts: Option, payload: &[u8]) -> Vec { + pub(in crate::container::ts) fn video_pes(pid: u16, cc: u8, pts: u64, dts: Option, payload: &[u8]) -> Vec { let mut header = Vec::new(); match dts { Some(dts) => { diff --git a/rs/moq-mux/src/container/ts/programs.rs b/rs/moq-mux/src/container/ts/programs.rs index 8bb83da03b6..f7a7dcc5954 100644 --- a/rs/moq-mux/src/container/ts/programs.rs +++ b/rs/moq-mux/src/container/ts/programs.rs @@ -10,20 +10,17 @@ use super::import::SectionReassembler; use super::{Ext, Import, catalog::SI_PIDS, health, psi, stats}; use crate::catalog; -/// PATs a listed program waits through for a PMT listing a stream before it is warned about, -/// once. Twenty PAT intervals span at least the 0.5 s within which TR 101 290 expects each PMT -/// to repeat, unless the PAT itself repeats more often than every 25 ms. -const LATE_PATS: u32 = 20; - /// Imports every program the PAT lists, each as its own broadcast on `origin`, with its own /// clock and catalog. /// /// Program `n` of `event.hang` publishes on `event/n.hang`, keeping the catalog suffix last so /// format detection still sees it; a name without one gains a plain `/n`. A program starts once -/// its PMT lists an elementary stream, so one the PAT names but the input never describes costs -/// nothing, and is warned about once if still waiting twenty PATs later. Input ahead of that PMT -/// is dropped, as a single [`Import`] drops media ahead of its PSI; its SI arrives with the next -/// repetition, as for an import joining mid-stream. +/// its PMT lists an elementary stream; until then it publishes nothing. One whose PMT has not +/// arrived 0.5 s of program clock after the PAT listed it, the bound TR 101 290 sets for +/// `PMT_error_2`, refuses the multiplex. The clock is a running program's, so nothing is late +/// before one runs, and one whose PMT arrived listing no stream yet waits. Input ahead of that +/// PMT is dropped, as a single [`Import`] drops media ahead of its PSI; its SI arrives with the +/// next repetition, as for an import joining mid-stream. /// /// The PAT is followed as it changes. A program a later PAT adds waits for its PMT as the first /// PAT's do, and each program follows its PMT to the PID the latest PAT names. One a later PAT @@ -55,19 +52,62 @@ pub struct Programs { announced: bool, /// What retired programs counted beyond the programs still running. retired: Counts, + /// Times the programs waiting for their PMT. + clock: Clock, } /// The programs waiting for their PMT on one PID. #[derive(Default)] struct Waiting { sections: SectionReassembler, - /// Each program's number, with the PATs it has waited through. - programs: HashMap, + /// Each program's number, with how long it has waited. + programs: HashMap, /// Whether a program read this PID when the section still assembling began, and so counts /// it if it is corrupt. began_read: bool, } +/// How long a listed program has waited for its PMT, on the [`Clock`]. +#[derive(Clone, Copy)] +enum Wait { + /// Since this reading, or, before any clock runs, since the first. + Since(Option), + /// Its PMT arrived listing no stream yet, so it is not late. + Described, +} + +/// A program clock for the programs that have none yet: the ticks a running program's clock +/// has advanced while it was the reference, which passes to another when it retires. +#[derive(Default)] +struct Clock { + ticks: u64, + /// The reference program's number, and its clock when last read. + reference: Option<(u16, u64)>, +} + +impl Clock { + /// Advance by what the reference program's clock moved since the last read, and return the + /// ticks, or `None` while no running program has a clock. + fn read(&mut self, programs: &[Program]) -> Option { + let clock = |number| programs.iter().find(|program| program.number == number)?.import.clock(); + match self + .reference + .and_then(|(number, last)| Some((number, last, clock(number)?))) + { + Some((number, last, now)) => { + self.ticks += now.saturating_sub(last); + self.reference = Some((number, now)); + } + None => { + self.reference = programs + .iter() + .find_map(|program| Some((program.number, program.import.clock()?))); + } + } + self.reference.map(|_| self.ticks) + } +} + /// One program's broadcast. struct Program { /// The PAT's program number. @@ -177,6 +217,7 @@ impl Programs { touched: Vec::new(), announced: false, retired: Counts::default(), + clock: Clock::default(), } } @@ -248,12 +289,12 @@ impl Programs { /// the PMT of each it adds. A started program follows its own PMT PID. /// /// Fails on a PAT naming another transport stream while a program runs, as each program's - /// importer would, and when a retired program cannot finish. + /// importer would, when a retired program cannot finish, and when a listed program is late. fn follow(&mut self, pat: &psi::Pat) -> anyhow::Result<()> { - self.wait_longer(); + let now = self.clock.read(&self.programs); // The PAT repeats several times a second, mostly unchanged. if self.pat.as_ref() == Some(pat) { - return Ok(()); + return self.refuse_late(now); } if let Some(running) = self.pat.as_ref().filter(|_| !self.programs.is_empty()) { anyhow::ensure!( @@ -276,18 +317,23 @@ impl Programs { self.retire(&listed)?; let mut moved = Vec::new(); self.waiting.retain(|pid, waiting| { - waiting.programs.retain(|number, pats| match listed.get(number) { + waiting.programs.retain(|number, _| match listed.get(number) { Some(listed) if listed == pid => true, Some(&listed) => { - moved.push((*number, listed, *pats)); + moved.push((*number, listed)); false } None => false, }); !waiting.programs.is_empty() }); - for (number, pid, pats) in moved { - self.waiting.entry(pid).or_default().programs.insert(number, pats); + // A PMT on a new PID is due 0.5 s from the PAT that names it. + for (number, pid) in moved { + self.waiting + .entry(pid) + .or_default() + .programs + .insert(number, Wait::Since(now)); } let known: HashSet = self .programs @@ -306,39 +352,49 @@ impl Programs { if self.pat.is_some() { tracing::info!(program = number, pmt_pid = pid, "TS program joined the PAT"); } - self.waiting.entry(pid).or_default().programs.insert(number, 0); + self.waiting + .entry(pid) + .or_default() + .programs + .insert(number, Wait::Since(now)); } self.pat = Some(pat.clone()); - Ok(()) + self.refuse_late(now) } - /// Count a PAT against every waiting program, and warn once about those it makes late. - fn wait_longer(&mut self) { + /// Refuse the multiplex once a listed program has waited more than 0.5 s of program clock + /// for its PMT, at `now`. A wait begun before any clock ran counts from this reading. + fn refuse_late(&mut self, now: Option) -> anyhow::Result<()> { + let Some(now) = now else { + return Ok(()); + }; let mut late = Vec::new(); for waiting in self.waiting.values_mut() { - for (&number, pats) in &mut waiting.programs { - *pats = pats.saturating_add(1); - if *pats == LATE_PATS { + for (&number, wait) in &mut waiting.programs { + if let Wait::Since(since) = wait + && now.saturating_sub(*since.get_or_insert(now)) > health::TABLE_LIMIT + { late.push(number); } } } if late.is_empty() { - return; + return Ok(()); } late.sort_unstable(); - tracing::warn!( - count = late.len(), - programs = ?&late[..late.len().min(10)], - pats = LATE_PATS, - "TS programs the PAT lists have no PMT listing a stream yet; they wait, unannounced" - ); + anyhow::bail!( + "{} TS programs the PAT lists sent no PMT within 0.5 s of program clock, the TR 101 290 PMT_error_2 bound: {:?}", + late.len(), + &late[..late.len().min(10)] + ) } /// End every started program `listed` no longer names: finish its tracks, announcing it /// as the end of the input would, then its catalog, and close its broadcast, so it is /// unannounced now rather than lingering. Subscribers already reading a track read it to - /// its end, and the other programs carry on. What the retired counted beyond them is kept. + /// its end, and the other programs carry on. What the retired counted beyond them is kept, + /// a PID one of them still reads as it stood before the finish: that program holds the tail + /// the finish flushed, and counts it when it delivers it. /// /// The programs and the PIDs they read are rebuilt once, so a PAT costs what the programs /// claim however many it drops. Fails when a program cannot finish, as an import does at @@ -348,6 +404,10 @@ impl Programs { if !self.programs.iter().any(leaving) { return Ok(()); } + let mut before = Counts::default(); + for program in self.programs.iter().filter(|program| leaving(program)) { + before.merge(&program.import); + } // A refusal leaves every program listed, so the caller's abort reaches them all. for program in self.programs.iter_mut().filter(|program| leaving(program)) { tracing::info!( @@ -395,7 +455,20 @@ impl Programs { program.broadcast.close(); counts.merge(&program.import); } - let mut retired = counts.beyond(&self.running()); + let running = self.running(); + counts.streams.retain(|pid, row| { + if !running.streams.contains_key(pid) { + return true; + } + match before.streams.remove(pid) { + Some(earlier) => { + *row = earlier; + true + } + None => false, + } + }); + let mut retired = counts.beyond(&running); retired.add(&self.retired); self.retired = retired; Ok(()) @@ -442,6 +515,9 @@ impl Programs { latest.push(pmt); } } + for pmt in latest.iter().filter(|pmt| pmt.streams.is_empty()) { + waiting.programs.insert(pmt.program_number, Wait::Described); + } latest.retain(|pmt| !pmt.streams.is_empty()); if latest.is_empty() { if corrupt > 0 { @@ -596,10 +672,10 @@ mod test { use super::*; use crate::catalog::hang::Container; use crate::container::Consumer; - use crate::container::ts::fixture::{Flags, Mux}; + use crate::container::ts::fixture::{Flags, MS, Mux}; use crate::container::ts::import::test::{ - CUE, MP2, corrupt, fifty_programs, mp2_pes, pat_section, pmt_section, psi_packets, two_programs, - two_section_pat, + CUE, MP2, annexb_au, corrupt, fifty_programs, mp2_pes, pat_section, pmt_section, psi_packets, two_programs, + two_section_pat, video_pes, }; /// The epoch each running program publishes under, announced yet or not. @@ -712,32 +788,57 @@ mod test { assert_eq!(programs.stats().streams[&0x71].units, 4, "program 2 read on"); } - /// A program whose PMT never arrives waits, unannounced, holding up neither the others nor - /// the end of the input, and is warned about once it has waited through enough PATs. + /// A program the PAT lists whose PMT never arrives refuses the multiplex at the first PAT + /// past 0.5 s of program clock, timed on the program that runs, and not before; one whose + /// PMT arrives listing no stream waits. #[test] - #[tracing_test::traced_test] - fn a_program_without_a_pmt_waits_and_is_warned_about_once() { + fn a_program_without_a_pmt_is_refused_after_half_a_second() { let origin = crate::source::produce_origin(); let mut programs = Programs::new(origin, "event.hang", catalog::Config::default()); - let mut steps = Steps::new(); - const WARNING: &str = "no PMT listing a stream yet"; - for _ in 0..LATE_PATS { - steps.step(&mut programs, &[(1, 0x100), (3, 0x300)]); - } - assert!(!logs_contain(WARNING), "warned before it was late"); - for _ in 0..LATE_PATS { - steps.step(&mut programs, &[(1, 0x100), (3, 0x300)]); + let mut mux = Mux::default(); + let mut step = |programs: &mut Programs, ms: u64| { + mux.packet(0x1ff, Flags::pcr(27_000_000 + ms * MS)) + .pat(&[(1, 0x100), (3, 0x300), (4, 0x400)]) + .pmt(0x100, 1, 0x1ff, &[(MP2, 0x61)], 0) + .pmt(0x400, 4, 0x1ff, &[], 0); + programs.decode(&std::mem::take(&mut mux.out)) + }; + // Program 1's clock starts at its first PCR, 100 ms in, and the wait with it. + for ms in (0..=600).step_by(100) { + step(&mut programs, ms).unwrap(); } + let err = step(&mut programs, 700).unwrap_err(); + assert!(format!("{err:#}").ends_with("bound: [3]"), "{err:#}"); + } + + /// A program leaving the PAT flushes the picture it shares with a program still running, + /// which that program delivers itself at the next picture, so the picture counts once. + #[test] + fn a_picture_a_retired_program_flushed_counts_once() { + const H264: u8 = 0x1b; + let origin = crate::source::produce_origin(); + let mut programs = Programs::new(origin, "event.hang", catalog::Config::default()); + let mut mux = Mux::default(); + let both = [(1, 0x100), (2, 0x200)]; + mux.pat(&both) + .pmt(0x100, 1, 0x50, &[(H264, 0x50)], 0) + .pmt(0x200, 2, 0x50, &[(H264, 0x50)], 0); + let mut pictures = + (0..).map(|n: u8| video_pes(0x50, n & 0x0f, 90_000 + u64::from(n) * 3_000, None, &annexb_au(n == 0))); + mux.out.extend(pictures.next().unwrap()); + mux.out.extend(pictures.next().unwrap()); + programs.decode(&std::mem::take(&mut mux.out)).unwrap(); + let units = |programs: &Programs| programs.stats().streams[&0x50].units; + assert_eq!(units(&programs), 1, "the second picture ends at the third"); + + mux.pat(&both[1..]); + programs.decode(&std::mem::take(&mut mux.out)).unwrap(); + assert_eq!(programs.programs.len(), 1); + assert_eq!(units(&programs), 1, "program 2 still holds the second picture"); + programs.decode(&pictures.next().unwrap()).unwrap(); + assert_eq!(units(&programs), 2, "the second picture counts once"); programs.finish().unwrap(); - let running: Vec<_> = epochs(&programs).into_iter().map(|(number, _)| number).collect(); - assert_eq!(running, [1], "program 3 publishes nothing"); - logs_assert(|lines: &[&str]| { - let warned: Vec<_> = lines.iter().filter(|line| line.contains(WARNING)).collect(); - match warned.as_slice() { - [line] if line.contains("[3]") => Ok(()), - _ => Err(format!("expected one warning naming program 3, got {warned:?}")), - } - }); + assert_eq!(units(&programs), 3); } /// A retired program's counts stay in the totals, once: a PMT PID it shared with a program