diff --git a/quest/m1/ts-reliability/README.md b/quest/m1/ts-reliability/README.md index b53ef4216a9..28aa3e660ea 100644 --- a/quest/m1/ts-reliability/README.md +++ b/quest/m1/ts-reliability/README.md @@ -100,7 +100,6 @@ catalog linger (#5305). ## Required - [A small step back keeps the TS export's video](/quest/m1/ts-reliability/step-back.md) - the export re-acquires on a sub-group step back from any source instead of dropping its video -- [Shared PID stats](/quest/m1/ts-reliability/shared-pid-stats.md) - `ts::stats` counts a shared PID once in the shared parser, so its timing counts survive program changes ## Related diff --git a/quest/m1/ts-reliability/shared-pid-stats.md b/quest/m1/ts-reliability/shared-pid-stats.md deleted file mode 100644 index c9675bbfaf2..00000000000 --- a/quest/m1/ts-reliability/shared-pid-stats.md +++ /dev/null @@ -1,32 +0,0 @@ -# [M] TS stats count a shared PID once - -## Goal - -`ts::stats` counts each PID once, in the parser every program shares, so a -PID two programs carry has one continuous set of TR 101 290 counters across -program starts and retirements. The multiplex totals read those counters -directly instead of rebuilding them from per-program copies. - -## Plan - -Found while landing [moq-dev/moq#6689](https://github.com/moq-dev/moq/pull/6689): - -- Each program keeps its own counters for the PIDs it reads, and the - multiplex totals merge them, then subtract what a retiring program still - shares with a running one. Most of that PR's review churn came from this - merge-and-subtract logic. -- A program that starts after another on a shared PID retired counts that - PID's timing checks only from its own start. Where both run at once, the - larger count stands. - -Decided 2026-10-28 (maintainer): one quest, since counting in the shared -parser both closes the timing gap and deletes the merge-and-subtract logic. -Keep #6689's outcomes: a retired program's final picture still counts, a -missing PMT still refuses after 0.5 s of program clock, and an unfinished -section on a PMT PID gets its bad CRC counted once. Extend the `ts_programs` -bench if the shared counters change its cost. - -Rust: `rs/moq-mux` `container/ts/stats.rs`, `programs.rs`, `unclaimed.rs`. - -Public API: none expected; if `ts::stats`' per-program view changes shape, -say how to migrate. Wire: none. diff --git a/rs/moq-mux/src/container/ts/health.rs b/rs/moq-mux/src/container/ts/health.rs index 4c1bda7352b..52c55e84b38 100644 --- a/rs/moq-mux/src/container/ts/health.rs +++ b/rs/moq-mux/src/container/ts/health.rs @@ -47,6 +47,18 @@ pub(super) struct Errors { pids: BTreeMap, } +/// The checks a program times on a PID by the way it reads it, which programs reading the PID +/// the same way count alike. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub(super) enum Checks { + /// The PTS cadence of an elementary stream it carries, and the PAT's interval on PID 0. + Stream, + /// The PCR checks on the PID it takes its clock from. + Clock, + /// The PMT interval on the PID it reads its PMT from. + Table, +} + #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] struct PidErrors { continuity_count_error: u64, @@ -83,6 +95,31 @@ impl Errors { } } + /// The `checks` counted on `pid` alone. + pub(super) fn only(&self, pid: u16, checks: Checks) -> Self { + let all = self.pids.get(&pid).copied().unwrap_or_default(); + let mut errors = PidErrors::default(); + let mut pat_error = 0; + match checks { + Checks::Stream => { + errors.pts_error = all.pts_error; + if pid == Pid::PAT { + pat_error = self.pat_error; + } + } + Checks::Clock => { + errors.pcr_repetition_error = all.pcr_repetition_error; + errors.pcr_discontinuity_indicator_error = all.pcr_discontinuity_indicator_error; + } + Checks::Table => errors.pmt_error = all.pmt_error, + } + Self { + pat_error, + pids: BTreeMap::from([(pid, errors)]), + ..Default::default() + } + } + /// Write the stream-wide totals into `stats`, and each elementary stream's own counts into /// its row. pub(super) fn report(&self, stats: &mut Snapshot) { @@ -173,7 +210,7 @@ impl Multiplex { } /// The TR 101 290 checks timed on one program's clock: table, PCR and PTS intervals. -#[derive(Default)] +#[derive(Clone, Default)] pub(super) struct Health { errors: Errors, pcr_pid: Option, @@ -274,7 +311,7 @@ impl Health { pub(super) fn pmt_pids(&mut self, pids: &[u16], now: Option) { self.pmt.retain(|pid, _| pids.contains(pid)); for &pid in pids { - self.pmt.entry(pid).or_insert(Timer { last: now }); + self.pmt.entry(pid).or_insert(Timer::at(now)); } } @@ -301,6 +338,32 @@ impl Health { pub(super) fn restart(&mut self, pid: u16) { self.pts.remove(&pid); } + + /// Carry what this checker has timed on `pid` over to `to`, which reads the same packets and + /// counts `pid` from here on, where `to` has not timed it yet: the PAT's interval on PID 0, + /// the PMT's on a PMT PID `to` reads, and the PTS cadence when `to` grades one there. So an + /// interval running across the handover counts once, from where it began, even from before + /// `to`'s clock started. `now` and `then` are this checker's clock and `to`'s; nothing + /// carries over before both run. + pub(super) fn hand_over(&self, pid: u16, to: &mut Self, pts: bool, now: Option, then: Option) { + let (Some(now), Some(then)) = (now, then) else { + return; + }; + let carry = |timer: &Timer, theirs: &mut Timer| { + if theirs.last.is_none() { + theirs.last = timer.last.map(|last| last + ticks(then) - ticks(now)); + } + }; + if pid == Pid::PAT { + carry(&self.pat, &mut to.pat); + } + if let (Some(timer), Some(theirs)) = (self.pmt.get(&pid), to.pmt.get_mut(&pid)) { + carry(timer, theirs); + } + if pts && let Some(timer) = self.pts.get(&pid) { + carry(timer, to.pts.entry(pid).or_default()); + } + } } /// The step between two readings of a clock that wraps: forward by up to half the range, @@ -338,33 +401,43 @@ fn section_start(pkt: &[u8; TsPacket::SIZE]) -> Option { } /// How long since a table or a PTS last occurred, on the program clock. -#[derive(Default)] +#[derive(Clone, Copy, Default)] struct Timer { - /// `None` until the clock starts. - last: Option, + /// `None` until the clock starts. Signed, since one [handed over](Health::hand_over) from + /// an earlier clock may predate this one's start. + last: Option, } impl Timer { + fn at(now: Option) -> Self { + Self { last: now.map(ticks) } + } + fn reset(&mut self, now: Option) { if now.is_some() { - self.last = now; + *self = Self::at(now); } } /// How many times `limit` ran out since the last occurrence, restarting the wait at each, /// so an outage counts once per limit it lasts rather than once in all. fn expired(&mut self, now: u64, limit: u64) -> u64 { - let last = self.last.get_or_insert(now); - let late = now.saturating_sub(*last); + let last = self.last.get_or_insert(ticks(now)); + let late = u64::try_from(ticks(now) - *last).unwrap_or(0); if late <= limit { return 0; } let count = (late - 1) / limit; - *last += count * limit; + *last += ticks(count * limit); count } } +/// A program clock reading as a signed tick count, which 27 MHz keeps in range for millennia. +fn ticks(now: u64) -> i64 { + i64::try_from(now).unwrap_or(i64::MAX) +} + /// `TS_sync_loss` and `Sync_byte_error`, on a grid of its own. /// /// The importer's framer re-scans at the first byte that is not a sync byte, which is right diff --git a/rs/moq-mux/src/container/ts/import.rs b/rs/moq-mux/src/container/ts/import.rs index 3c69406971f..cc7c23cfccc 100644 --- a/rs/moq-mux/src/container/ts/import.rs +++ b/rs/moq-mux/src/container/ts/import.rs @@ -348,8 +348,18 @@ impl Import { self } - /// Whether this importer reassembles the PMT sections on `pid`, and counts their CRC errors. - pub(super) fn reads_pmt(&self, pid: u16) -> bool { + /// The PID this importer reads its PMT from. + pub(super) fn pmt_pid(&self) -> Option { + self.pmt.as_ref().map(|(pid, _)| pid.as_u16()) + } + + /// The PID this importer takes its program clock from. + pub(super) fn pcr_pid(&self) -> Option { + self.pcr_pid.map(|pid| pid.as_u16()) + } + + /// Whether this importer reassembles the PMT sections on `pid`. + fn reads_pmt(&self, pid: u16) -> bool { self.pmt.as_ref().is_some_and(|(pmt, _)| pmt.as_u16() == pid) } @@ -358,11 +368,20 @@ impl Import { self.sections.contains_key(&pid) || Pid::new(pid).is_ok_and(|pid| self.streams.contains_key(&pid)) } - /// Whether this importer holds a PMT section begun on `pid`, which it counts if corrupt. - pub(super) fn assembling_pmt(&self, pid: u16) -> bool { - self.pmt - .as_ref() - .is_some_and(|(pmt, sections)| pmt.as_u16() == pid && sections.assembling()) + /// The TR 101 290 intervals this importer times, and its clock, for another to take over. + pub(super) fn timing(&self) -> (&Health, Option) { + (&self.health, self.liveness.now()) + } + + /// Take over the TR 101 290 intervals `health` timed on `pid` at clock reading `now`, from a + /// program that read the same packets, to count the PID from here on. See + /// [`Health::hand_over`]. + pub(super) fn take_over(&mut self, pid: u16, (health, now): (&Health, Option)) { + let cadenced = Pid::new(pid) + .ok() + .and_then(|pid| self.streams.get(&pid)) + .is_some_and(Stream::cadenced); + health.hand_over(pid, &mut self.health, cadenced, now, self.liveness.now()); } /// A reservation on this program's timebase, so every stream shifts by its offset. @@ -1243,9 +1262,7 @@ impl Import { let is_video = stream.video(); let is_clock = matches!(stream, Stream::Clock); - // Verbatim streams with no cadence (SCTE-35, DVB subtitles) are left ungraded - // rather than watched against an invented one. - if pes.pts.is_some() && (is_video || !matches!(stream, Stream::PesPacket(_) | Stream::Ignored)) { + if pes.pts.is_some() && stream.cadenced() { self.health.pts(pid.as_u16(), self.liveness.now()); } // Resolve the wrap at PES start so every elementary stream and private section shares @@ -1945,13 +1962,14 @@ impl Import { stats } - /// The TR 101 290 counts this program timed, for merging programs that read the same - /// multiplex. + /// The TR 101 290 counts this program timed, for [`Programs`](super::Programs) to count each + /// PID once. pub(super) fn errors(&self) -> &super::health::Errors { self.health.errors() } - /// PMT sections dropped for a bad CRC per PID, for merging programs that read the same PMT. + /// PMT sections dropped for a bad CRC per PID. + #[cfg(test)] pub(super) fn crc_errors(&self) -> &BTreeMap { &self.crc_errors } @@ -3029,21 +3047,9 @@ pub(super) struct SectionReassembler { span: Option, /// Shared with the PES path: a broken chain drops the partial either way. continuity: Continuity, - /// Whether the first section the latest packet completed began in an earlier packet. - resumed: bool, } impl SectionReassembler { - /// Whether the first section the latest packet completed began in an earlier packet. - pub(super) fn resumed(&self) -> bool { - self.resumed - } - - /// Whether a partial section is left assembling. - pub(super) fn assembling(&self) -> bool { - !self.acc.is_empty() - } - /// Consume one 188-byte TS packet, appending every completed section to `out`. pub(super) fn push(&mut self, pkt: &[u8], out: &mut Vec>) { let mut completed = Vec::new(); @@ -3053,7 +3059,6 @@ impl SectionReassembler { fn push_at(&mut self, pkt: &[u8], source: Option<(u64, &Placement)>, out: &mut Vec<(Option, Vec)>) { let pkt: &[u8; 188] = pkt.try_into().expect("section packet must be 188 bytes"); - self.resumed = false; match self.continuity.observe(pkt) { Continuation::Duplicate => return, // A packet flagged corrupt takes its own payload down with the partial: this is @@ -3094,9 +3099,7 @@ impl SectionReassembler { // progress (stream just joined, or a reset dropped the partial) these // bytes are an orphaned fragment, so skip straight to the pointer_field. self.acc.extend_from_slice(&payload[1..1 + ptr]); - let completed = out.len(); self.drain(out, source, end - (payload.len() - 1 - ptr) as u64); - self.resumed = out.len() > completed; } // The pointer_field is a hard section boundary: drop any leftover partial // and start the section it points to. @@ -3111,9 +3114,7 @@ impl SectionReassembler { // discontinuity, or corrupt pointer dropped the partial, rather than // resuming on stray bytes that merely look like a section. self.acc.extend_from_slice(payload); - let completed = out.len(); self.drain(out, source, end); - self.resumed = out.len() > completed; } } @@ -3453,6 +3454,12 @@ impl Stream { } } + /// Whether the stream's PTS keeps a cadence `PTS_error` grades. Verbatim streams with none + /// (SCTE-35, DVB subtitles) are left ungraded rather than watched against an invented one. + fn cadenced(&self) -> bool { + self.video() || !matches!(self, Stream::PesPacket(_) | Stream::Ignored) + } + /// This route's track and the frame sync it has lost (only the self-describing audio /// codecs scan for it), or `None` for a PID that is dropped rather than carried. fn stats(&self) -> Option { diff --git a/rs/moq-mux/src/container/ts/programs.rs b/rs/moq-mux/src/container/ts/programs.rs index 70c21848905..d95014566c2 100644 --- a/rs/moq-mux/src/container/ts/programs.rs +++ b/rs/moq-mux/src/container/ts/programs.rs @@ -6,6 +6,7 @@ use anyhow::Context; use mpeg2ts::ts::Pid; use super::demux::{Demux, Packet}; +use super::health::{Checks, Health}; use super::import::SectionReassembler; use super::unclaimed::Unclaimed; use super::{Ext, Import, catalog::SI_PIDS, health, psi, stats}; @@ -33,6 +34,15 @@ use crate::catalog; /// PID no program reads, the PAT's PMT PIDs, the SI PIDs and the null PID aside, is counted once /// for the multiplex (see [`stats::Snapshot::streams`]). Counting waits until every program the /// PAT lists has its PMT, since until then a PID may be a program's still to come. +/// +/// Each PID is counted once, so a PID several programs read has one continuous set of +/// counters while they start and retire. The PMT sections are reassembled here, once per PMT +/// PID, so a bad CRC counts once however many programs read the PID. Every other count of a PID +/// is taken from one program at a time for each way it is read: its row and PTS +/// cadence from a program carrying it, or else one reading it, its PCR checks from one taking +/// its clock there, and its PMT interval from one reading its PMT there. When that program stops +/// reading it so, the earliest running one that does takes over the intervals it was timing, +/// and counts on from the total it leaves. pub struct Programs { origin: moq_net::origin::Producer, name: moq_net::Path, @@ -41,10 +51,16 @@ pub struct Programs { /// The latest whole PAT, from the first that lists a program, which seeds every program. pat: Option, /// 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. + /// for them, with how long each has waited. + waiting: HashMap>, + /// The PMT sections on each PMT PID the latest PAT lists, reassembled once for every program. + pmts: HashMap, + /// Whether each PID, by index, is one of [`Self::pmts`], read without hashing every packet. + pmt_pids: Vec, + /// PMT sections dropped for a bad CRC, by PID. crc_errors: BTreeMap, + /// Each PID's counts, but for its PMT sections' CRC, by the way it is read. + tallies: BTreeMap<(u16, Checks), Tally>, /// 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 @@ -54,14 +70,10 @@ pub struct Programs { touched: Vec, /// Whether a retired program had announced. announced: bool, - /// What retired programs counted beyond the programs still running. - retired: Counts, /// Times the programs waiting for their PMT. clock: Clock, /// The [`Clock`] reading past which the longest-waiting program is late, while one waits. deadline: Option, - /// Every PMT PID the latest PAT lists, which the PAT claims whether or not a program reads it. - pmt_pids: HashSet, /// The packets on PIDs no program reads. unclaimed: Unclaimed, } @@ -69,14 +81,6 @@ pub struct Programs { /// `PMT_error_2`: a PMT recurs at least every 0.5 s of program clock. const PMT_LIMIT: u64 = super::passthrough::PCR_HZ / 2; -/// The programs waiting for their PMT on one PID. -#[derive(Default)] -struct Waiting { - sections: SectionReassembler, - /// Each program's number, with how long it has waited. - programs: HashMap, -} - /// How long a listed program has waited for its PMT, on the [`Clock`]. #[derive(Clone, Copy)] enum Wait { @@ -128,6 +132,8 @@ struct Program { pending: Option<(moq_net::origin::Route, moq_net::track::Consumer)>, /// The PIDs this program is listed under in [`Programs::readers`], sorted. claims: Vec, + /// The ways it reads each PID, sorted, but for the PAT and the SI PIDs every program reads. + reads: Vec<(u16, Checks)>, /// Whether the current [`Programs::decode`] call routed it a packet. touched: bool, } @@ -152,62 +158,147 @@ impl Program { } } -/// Counters merged across programs. -#[derive(Default)] -struct Counts { - streams: BTreeMap, - /// PMT sections dropped for a bad CRC, by PID. - crc_errors: BTreeMap, +/// What one program counted on one PID in one way: for [`Checks::Stream`] its row too. +#[derive(Clone, Default)] +struct Count { + row: Option, errors: health::Errors, } -impl Counts { - /// 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) { - 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); +impl Count { + /// `pid`'s `checks` in a program's `stats` and `errors`. + fn read(stats: &stats::Snapshot, errors: &health::Errors, (pid, checks): (u16, Checks)) -> Self { + Self { + row: (checks == Checks::Stream) + .then(|| stats.streams.get(&pid).cloned()) + .flatten(), + errors: errors.only(pid, checks), } - 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()); + /// What these counts gained since `from`, an earlier reading of the same program. + fn since(mut self, from: &Self) -> Self { + if let (Some(row), Some(from)) = (&mut self.row, &from.row) { + row.combine(from, u64::saturating_sub); } - for (&pid, &count) in &earlier.crc_errors { - *self.crc_errors.entry(pid).or_default() += count; + self.errors.combine(&from.errors, u64::saturating_sub); + self + } + + /// Add what `earlier` counted, keeping this row's track, class and quiet. + fn after(mut self, earlier: &Self) -> Self { + match (&mut self.row, &earlier.row) { + (Some(row), Some(earlier)) => row.combine(earlier, u64::saturating_add), + (None, Some(earlier)) => self.row = Some(earlier.clone()), + _ => {} } self.errors.combine(&earlier.errors, u64::saturating_add); + self } +} - /// 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); - } +/// A program's counts and timers just before it read a packet on its PMT PID, which may move +/// its counts to another program. +struct Before { + stats: stats::Snapshot, + errors: health::Errors, + health: Health, + now: Option, +} + +impl Before { + fn of(import: &Import) -> Self { + let (health, now) = import.timing(); + Self { + stats: import.stats(), + errors: import.errors().clone(), + health: health.clone(), + now, + } + } +} + +/// One PID's counts in one way for the multiplex. +#[derive(Default)] +struct Tally { + /// What the programs that counted it before the current one counted. + kept: Count, + /// The program counting it now, by index, with its reading when it took over. + owner: Option<(usize, Count)>, +} + +impl Tally { + /// The counts, given the owner's reading now. + fn total(&self, now: Option) -> Count { + match (&self.owner, now) { + (Some((_, from)), Some(now)) => now.since(from).after(&self.kept), + _ => self.kept.clone(), } - for (pid, count) in &mut self.crc_errors { - *count = count.saturating_sub(running.crc_errors.get(pid).copied().unwrap_or(0)); + } + + /// The owner stops counting, its reading at the end `ended`. + fn release(&mut self, ended: Count) { + self.kept = self.total(Some(ended)); + self.owner = None; + // Nothing is expected on a PID no program counts, so it is not quiet. + if let Some(row) = &mut self.kept.row { + row.quiet = None; } - self.errors.combine(&running.errors, u64::saturating_sub); - self } } +/// Whether every program reads `pid`: the PAT, and the SI PIDs. +fn everyone(pid: u16) -> bool { + pid == Pid::PAT || SI_PIDS.contains(&pid) +} + +/// The ways `import` reads the PIDs it `claims`, sorted. +fn reads(import: &Import, claims: &[u16]) -> Vec<(u16, Checks)> { + let mut reads: Vec<_> = (claims.iter().map(|&pid| (pid, Checks::Stream))) + .chain(import.pcr_pid().map(|pid| (pid, Checks::Clock))) + .chain(import.pmt_pid().map(|pid| (pid, Checks::Table))) + .collect(); + reads.sort_unstable(); + reads +} + +/// The `reads` of sorted `ours` not in sorted `theirs`. +fn difference(ours: &[(u16, Checks)], theirs: &[(u16, Checks)]) -> Vec<(u16, Checks)> { + (ours.iter()) + .filter(|read| theirs.binary_search(read).is_err()) + .copied() + .collect() +} + +/// Which running program counts `pid` in the way `checks`, given the one that does now: one +/// carrying an elementary stream there counts its row before one only reading the PID, and +/// otherwise the current one carries on while it reads the PID so, then the earliest that does. +fn counter( + programs: &[Program], + readers: &[Vec], + (pid, checks): (u16, Checks), + current: Option, +) -> Option { + let everyone = everyone(pid); + let reads = |index: usize| everyone || programs[index].reads.binary_search(&(pid, checks)).is_ok(); + let candidates = || -> Box + '_> { + match everyone { + true => Box::new(0..programs.len()), + false => Box::new(readers[usize::from(pid)].iter().copied()), + } + }; + let current = current.filter(|&index| reads(index)); + if checks == Checks::Stream && !everyone { + let carries = |index: usize| programs[index].import.carries(pid); + if !current.is_some_and(carries) + && let Some(carrier) = candidates().find(|&index| carries(index)) + { + return Some(carrier); + } + } + current.or_else(|| candidates().find(|&index| reads(index))) +} + 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 Into, config: catalog::Config) -> Self { @@ -218,15 +309,16 @@ impl Programs { demux: Demux::new(true), pat: None, waiting: HashMap::new(), + pmts: HashMap::new(), + pmt_pids: vec![false; 1 << 13], crc_errors: BTreeMap::new(), + tallies: BTreeMap::new(), programs: Vec::new(), readers: vec![Vec::new(); 1 << 13], touched: Vec::new(), announced: false, - retired: Counts::default(), clock: Clock::default(), deadline: None, - pmt_pids: HashSet::new(), unclaimed: Unclaimed::default(), } } @@ -244,7 +336,7 @@ impl Programs { while let Some(packet) = demux.next() { if let Some(pat) = &packet.pat { self.follow(pat)?; - } else if !self.waiting.is_empty() { + } else if self.pmt_pids[usize::from(packet.pid)] { self.describe(&packet)?; } // Every program reads the PAT, and the SI sections the parser reassembled. @@ -261,15 +353,20 @@ impl Programs { self.touched.push(index); } let import = &mut program.import; + // A PMT moves counts between programs as it stood before the PMT, so a tail its + // end flushes or a refusal on the way to a PID counts once, and an interval the + // PMT stops timing carries on. + let before = (self.pmt_pids[usize::from(packet.pid)] && import.pmt_pid() == Some(packet.pid)) + .then(|| Before::of(import)); import.packet(&packet)?; self.clock.offer(index, import); if import.claims_changed() { - changed.push(index); + changed.push((index, before)); } } drop(packet); // Only a PAT or PMT moves claims or restarts a route. - for index in changed { + for (index, before) in changed { for pid in self.programs[index].import.restarts() { // Another program still reading the PID judges its partial bytes against // this chain, so only the PID's sole reader restarts it. @@ -277,7 +374,7 @@ impl Programs { demux.restart(pid); } } - self.reclaim(index); + self.reclaim(index, before.as_ref()); } // The waiting programs have no clock of their own until their PMT names one. self.clock.advance(&self.programs); @@ -329,7 +426,7 @@ impl Programs { self.retire(&listed)?; let mut moved = Vec::new(); self.waiting.retain(|pid, waiting| { - waiting.programs.retain(|number, _| match listed.get(number) { + waiting.retain(|number, _| match listed.get(number) { Some(listed) if listed == pid => true, Some(&listed) => { moved.push((*number, listed)); @@ -337,22 +434,18 @@ impl Programs { } None => false, }); - !waiting.programs.is_empty() + !waiting.is_empty() }); // A PMT on a new PID is due 0.5 s from the PAT that names it. let now = Wait::Since(self.clock.ticks); for (number, pid) in moved { - self.waiting.entry(pid).or_default().programs.insert(number, now); + self.waiting.entry(pid).or_default().insert(number, now); } let known: HashSet = self .programs .iter() .map(|program| program.number) - .chain( - self.waiting - .values() - .flat_map(|waiting| waiting.programs.keys().copied()), - ) + .chain(self.waiting.values().flat_map(|waiting| waiting.keys().copied())) .collect(); for (&number, &pid) in &listed { if known.contains(&number) { @@ -361,9 +454,18 @@ 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, now); + self.waiting.entry(pid).or_default().insert(number, now); + } + // A PMT PID the PAT now names starts reassembling at its next section. + let named: HashSet = listed.values().copied().collect(); + for &pid in self.pmts.keys() { + self.pmt_pids[usize::from(pid)] = false; + } + self.pmts.retain(|pid, _| named.contains(pid)); + for pid in named { + self.pmts.entry(pid).or_default(); + self.pmt_pids[usize::from(pid)] = true; } - self.pmt_pids = listed.values().copied().collect(); self.pat = Some(pat.clone()); self.time_waits(); Ok(()) @@ -374,7 +476,7 @@ impl Programs { self.deadline = self .waiting .values() - .flat_map(|waiting| waiting.programs.values()) + .flat_map(HashMap::values) .filter_map(|wait| match wait { Wait::Since(since) => Some(since + PMT_LIMIT), Wait::Described => None, @@ -388,7 +490,7 @@ impl Programs { let mut late: Vec = self .waiting .values() - .flat_map(|waiting| &waiting.programs) + .flat_map(|waiting| waiting.iter()) .filter(|(_, wait)| matches!(wait, Wait::Since(since) if self.clock.ticks > since + PMT_LIMIT)) .map(|(&number, _)| number) .collect(); @@ -403,9 +505,8 @@ impl Programs { /// 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, - /// a PID one of them still carries as it stood before the finish: that program holds the - /// tail the finish flushed, and counts it when it delivers it. + /// its end, and the other programs carry on. Each PID a retired program counted stays + /// counted, and its next reader counts on from there. /// /// 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 @@ -415,10 +516,11 @@ 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); - } + // What each leaver had counted before its finish, by index. + let before: HashMap = (self.programs.iter().enumerate()) + .filter(|(_, program)| leaving(program)) + .map(|(index, program)| (index, (program.import.stats(), program.import.errors().clone()))) + .collect(); // 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!( @@ -445,10 +547,15 @@ impl Programs { self.readers[usize::from(pid)].clear(); } } - let (gone, programs): (Vec<_>, Vec<_>) = std::mem::take(&mut self.programs) - .into_iter() - .partition(|program| leaving(program)); - self.programs = programs; + let mut gone = HashMap::new(); + for (index, program) in std::mem::take(&mut self.programs).into_iter().enumerate() { + match moved[index] { + Some(_) => self.programs.push(program), + None => { + gone.insert(index, (program, None)); + } + } + } for (index, program) in self.programs.iter().enumerate() { for &pid in &program.claims { self.readers[usize::from(pid)].push(index); @@ -466,33 +573,43 @@ impl Programs { .clock .reference .and_then(|(index, last)| Some((moved[index]?, last))); - let mut counts = Counts::default(); - for program in &gone { - program.broadcast.close(); - counts.merge(&program.import); - } - counts.streams.retain(|&pid, row| { - let carried = self.readers[usize::from(pid)] - .iter() - .any(|&index| self.programs[index].import.carries(pid)); - if !carried { - return true; + // Each count a leaver kept passes to the next program reading the PID so. One a running + // program carrying the PID takes ends at the leaver's count before its finish: that + // program holds the tail the finish flushed, and counts it when it delivers it. + let mut readings = HashMap::new(); + for (&read, tally) in &mut self.tallies { + let Some((owner, _)) = &mut tally.owner else { + continue; + }; + if let Some(to) = moved[*owner] { + *owner = to; + continue; } - match before.streams.remove(&pid) { - Some(earlier) => { - *row = earlier; - true + let (program, after) = gone.get_mut(owner).expect("a leaver counted it"); + let next = counter(&self.programs, &self.readers, read, None); + let holds_tail = + read.1 == Checks::Stream && next.is_some_and(|next| self.programs[next].import.carries(read.0)); + let ended = match holds_tail { + true => { + let (stats, errors) = &before[owner]; + Count::read(stats, errors, read) } - None => false, + false => { + let stats = after.get_or_insert_with(|| program.import.stats()); + Count::read(stats, program.import.errors(), read) + } + }; + tally.release(ended); + if let Some(next) = next { + let to = &mut self.programs[next].import; + to.take_over(read.0, program.import.timing()); + let reading = readings.entry(next).or_insert_with(|| to.stats()); + tally.owner = Some((next, Count::read(reading, to.errors(), read))); } - }); - let mut retired = counts.beyond(&self.running()); - retired.add(&self.retired); - // Nothing is expected on a retired PID, so it is not quiet. - for row in retired.streams.values_mut() { - row.quiet = None; } - self.retired = retired; + for (program, _) in gone.values() { + program.broadcast.close(); + } Ok(()) } @@ -500,7 +617,7 @@ impl Programs { /// PMT may still claim it. fn unclaimed(&mut self, packet: &Packet) { // A packet flagged corrupt may carry another PID's number. - if packet.pkt[1] & 0x80 != 0 || Unclaimed::fixed(packet.pid, true) || self.pmt_pids.contains(&packet.pid) { + if packet.pkt[1] & 0x80 != 0 || Unclaimed::fixed(packet.pid, true) || self.pmt_pids[usize::from(packet.pid)] { return; } // A program still waiting for its PMT may claim it, and a late one refuses the multiplex. @@ -509,71 +626,64 @@ impl Programs { } } - /// Read a packet on a waiting PMT PID, and start each program whose PMT it completes with a - /// stream listed. + /// Reassemble a packet on a PMT PID, count the PMT sections it completes with a bad CRC, and + /// start each waiting program whose PMT it completes with a stream listed. /// /// A started program reads this packet itself, from the reassembler as it stood before it, - /// so its own PMT path applies the PMT, counts the packet's corrupt sections, and starts its - /// clock from the PCR already seen. Of several revisions in the packet, the last decides. + /// so its own PMT path applies the PMT and starts its clock from the PCR already seen. Of + /// several revisions in the packet, the last decides. fn describe(&mut self, packet: &Packet) -> anyhow::Result<()> { - let Some(waiting) = self.waiting.get_mut(&packet.pid) else { + let Some(sections) = self.pmts.get_mut(&packet.pid) else { return Ok(()); }; - let before = waiting.sections.clone(); - let readers = &self.readers[usize::from(packet.pid)]; - // A program reading the PMT here reads this packet next, so counts what it begins. One - // already assembling a section read its start, so counts that one when it ends here. - let read = readers - .iter() - .any(|&index| self.programs[index].import.reads_pmt(packet.pid)); - let began = readers - .iter() - .any(|&index| self.programs[index].import.assembling_pmt(packet.pid)); - let mut sections = Vec::new(); - waiting.sections.push(packet.pkt, &mut sections); - let resumed = waiting.sections.resumed(); - // Corrupt sections no program read from their start. + let mut waiting = self.waiting.get_mut(&packet.pid); + let before = waiting.is_some().then(|| sections.clone()); + let mut completed = Vec::new(); + sections.push(packet.pkt, &mut completed); let mut corrupt = 0; let mut latest: Vec = Vec::new(); - for (index, section) in sections.into_iter().enumerate() { + for section in completed { // Other tables may share the PID. if section.first() != Some(&psi::PMT_TABLE_ID) { continue; } if !psi::crc_ok(§ion) { - let read = if index == 0 && resumed { began } else { read }; - corrupt += u64::from(!read); + corrupt += 1; continue; } - if let Some(pmt) = psi::Pmt::parse(§ion) - && waiting.programs.contains_key(&pmt.program_number) + if let Some(waiting) = &waiting + && let Some(pmt) = psi::Pmt::parse(§ion) + && waiting.contains_key(&pmt.program_number) { latest.retain(|seen| seen.program_number != pmt.program_number); latest.push(pmt); } } + if corrupt > 0 { + *self.crc_errors.entry(packet.pid).or_default() += corrupt; + tracing::warn!(pid = packet.pid, "dropped a PSI section with a bad CRC"); + } + let (Some(waiting), Some(before)) = (waiting.as_mut(), before) else { + return Ok(()); + }; let mut described = false; for pmt in latest.iter().filter(|pmt| pmt.streams.is_empty()) { described |= matches!( - waiting.programs.insert(pmt.program_number, Wait::Described), + waiting.insert(pmt.program_number, Wait::Described), Some(Wait::Since(_)) ); } latest.retain(|pmt| !pmt.streams.is_empty()); if latest.is_empty() { - if corrupt > 0 { - *self.crc_errors.entry(packet.pid).or_default() += corrupt; - tracing::warn!(pid = packet.pid, "dropped a PSI section with a bad CRC"); - } if described { self.time_waits(); } return Ok(()); } for pmt in &latest { - waiting.programs.remove(&pmt.program_number); + waiting.remove(&pmt.program_number); } - if waiting.programs.is_empty() { + if waiting.is_empty() { self.waiting.remove(&packet.pid); } for pmt in latest { @@ -615,14 +725,22 @@ impl Programs { catalog, pending: Some((route, listed)), claims: Vec::new(), + reads: Vec::new(), touched: false, }); - self.reclaim(self.programs.len() - 1); + let index = self.programs.len() - 1; + self.reclaim(index, None); + let mut readings = HashMap::new(); + for pid in std::iter::once(Pid::PAT).chain(SI_PIDS.iter().copied()) { + self.settle((pid, Checks::Stream), index, None, &mut readings); + } Ok(()) } - /// Move program `index` in [`Self::readers`] to the PIDs it claims now. - fn reclaim(&mut self, index: usize) { + /// Move program `index` in [`Self::readers`] to the PIDs it claims now, and settle who + /// counts each PID in each way it reads it now or did, given its counts `before` the packet + /// that moved them, if that was a PMT. + fn reclaim(&mut self, index: usize, before: Option<&Before>) { let program = &mut self.programs[index]; let claims = program.import.claims(); for &pid in program.claims.iter().filter(|pid| claims.binary_search(pid).is_err()) { @@ -633,44 +751,99 @@ impl Programs { let at = readers.partition_point(|&reader| reader < index); readers.insert(at, index); } + let reads = reads(&program.import, &claims); program.claims = claims; + let mut settled = difference(&program.reads, &reads); + settled.extend_from_slice(&reads); + program.reads = reads; + let mut readings = HashMap::new(); + for read in settled { + self.settle(read, index, before, &mut readings); + } + } + + /// Move `read`'s count to the program [`counter`] picks, if not the one counting it now. The + /// next counts on from its reading now, and the last counts up to its reading now, but for + /// program `index`, which read the packet that moved the count: its reading `before` it + /// stands where it starts counting, so what that packet counted on the way to the PID counts, + /// and where it stops in favour of a program carrying the PID, which holds the tail its + /// route's end flushed. `readings` caches the programs' stats. + fn settle( + &mut self, + read: (u16, Checks), + index: usize, + before: Option<&Before>, + readings: &mut HashMap, + ) { + let tally = self.tallies.entry(read).or_default(); + let current = tally.owner.as_ref().map(|&(owner, _)| owner); + let next = counter(&self.programs, &self.readers, read, current); + if next == current { + return; + } + let programs = &self.programs; + let mut reading = |at: usize, earlier: bool| match before.filter(|_| earlier && at == index) { + Some(before) => Count::read(&before.stats, &before.errors, read), + None => { + let import = &programs[at].import; + let stats = readings.entry(at).or_insert_with(|| import.stats()); + Count::read(stats, import.errors(), read) + } + }; + if let Some(current) = current { + let holds_tail = read.1 == Checks::Stream && next.is_some_and(|next| programs[next].import.carries(read.0)); + tally.release(reading(current, holds_tail)); + } + let Some(next) = next else { + return; + }; + tally.owner = Some((next, reading(next, true))); + let Some(current) = current else { + return; + }; + match before.filter(|_| current == index) { + Some(before) => (self.programs[next].import).take_over(read.0, (&before.health, before.now)), + None => { + let [from, to] = self + .programs + .get_disjoint_mut([current, next]) + .expect("a count moves to another program"); + to.import.take_over(read.0, from.import.timing()); + } + } } - /// Every program's counters in one map, a PID several programs read counted once. + /// Every program's counters in one map, each PID 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 - /// count its faults once too. PMT sections dropped before any program read their PID are - /// 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. So are the packets on - /// PIDs no program reads, in the row of the program that reads the PID whenever one does. + /// a dropped section counts once however many read it, and a PID several programs read is + /// counted by one of them at a time. What a retired program counted stays in the totals, so + /// no total goes back when a program leaves or returns. So are the packets on PIDs no + /// program reads, in the row of the program that reads the PID whenever one does. pub fn stats(&self) -> stats::Snapshot { - let mut counts = self.running(); - counts.add(&self.retired); + let mut readings: Vec> = (0..self.programs.len()).map(|_| None).collect(); let mut errors = self.demux.errors().clone(); - errors.max(&counts.errors); - let mut stats = stats::Snapshot { - streams: counts.streams, - ..Default::default() - }; + let mut stats = stats::Snapshot::default(); + for (&read, tally) in &self.tallies { + let now = tally.owner.as_ref().map(|&(index, _)| { + let import = &self.programs[index].import; + let reading = readings[index].get_or_insert_with(|| import.stats()); + Count::read(reading, import.errors(), read) + }); + let count = tally.total(now); + if let Some(row) = count.row { + stats.streams.insert(read.0, row); + } + errors.combine(&count.errors, u64::saturating_add); + } self.unclaimed.report(&mut stats.streams, |pid| { - Unclaimed::fixed(pid, true) || self.pmt_pids.contains(&pid) || !self.readers[usize::from(pid)].is_empty() + Unclaimed::fixed(pid, true) || self.pmt_pids[usize::from(pid)] || !self.readers[usize::from(pid)].is_empty() }); - stats.crc_error = - counts.crc_errors.values().chain(self.crc_errors.values()).sum::() + self.demux.pat_crc_error(); + stats.crc_error = 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 running program's broadcast; a retired one has already closed. The finished @@ -1035,6 +1208,159 @@ mod test { } } + /// A PTS outage on a shared PID counts the same whether or not the program timing it retires + /// or drops the PID partway: the program reading on takes over the interval running since + /// the last PTS, which it never saw and which began before its own clock started, rather + /// than timing the PID only from its own next PTS. + #[test] + fn a_pts_outage_counts_on_across_a_handover() { + #[derive(PartialEq)] + enum Leave { + Stay, + Retire, + Drop, + } + let outage = |leave: Leave| { + let origin = crate::source::produce_origin(); + let mut programs = Programs::new( + origin, + moq_net::Path::from_static("event.hang"), + catalog::Config::default(), + ); + let both = [(1, 0x100), (2, 0x200)]; + let mut mux = Mux::default(); + mux.pat(&both) + .pmt(0x100, 1, 0x1ff, &[(MP2, 0x61)], 0) + .packet(0x1ff, Flags::pcr(27_000_000)); + mux.out.extend(mp2_pes(0x61, 0, 90_000, [0x61; 2])); + // Program 2 starts 0.5 s after the last PTS on the PID they share, and program 1 + // leaves before that PTS is late. + for ms in (100..=1_500).step_by(100) { + let left = ms >= 700; + let listed = if left && leave == Leave::Retire { + &both[1..] + } else { + &both[..] + }; + mux.pat(listed); + match left && leave == Leave::Drop { + true => mux.pmt_at(1, 0x100, 1, 0x1ff, &[(MP2, 0x62, &[])], 0), + false => mux.pmt(0x100, 1, 0x1ff, &[(MP2, 0x61)], 0), + }; + if ms >= 500 { + mux.pmt(0x200, 2, 0x1ff, &[(MP2, 0x61)], 0); + } + mux.packet(0x1ff, Flags::pcr(27_000_000 + ms * MS)); + programs.decode(&std::mem::take(&mut mux.out)).unwrap(); + } + assert_eq!(programs.programs.len(), if leave == Leave::Retire { 1 } else { 2 }); + programs.stats().streams[&0x61].pts_error + }; + assert_eq!(outage(Leave::Stay), 2, "the 1.5 s since the PTS"); + assert_eq!(outage(Leave::Retire), 2, "counted on by the program left"); + assert_eq!(outage(Leave::Drop), 2, "counted on by the program still carrying it"); + } + + /// A section stream refused for want of a PCR keeps its refusal in the totals once, though + /// the PMT that adds it counts it before its program reads the PID, while the programs + /// listing it run and as one leaves. + #[test] + fn a_stream_refused_without_a_pcr_counts_once() { + const SECTIONS: u8 = 0x05; + let origin = crate::source::produce_origin(); + let mut programs = Programs::new( + origin, + moq_net::Path::from_static("event.hang"), + catalog::Config::default(), + ); + let both = [(1, 0x100), (2, 0x200)]; + let mut mux = Mux::default(); + for listed in [&both[..], &both[1..]] { + mux.pat(listed); + for (program, pid) in both { + mux.pmt(pid, program, 0x1fff, &[(MP2, 0x61), (SECTIONS, 0x72)], 0); + } + programs.decode(&std::mem::take(&mut mux.out)).unwrap(); + assert_eq!(programs.programs.len(), listed.len()); + assert_eq!(programs.stats().streams[&0x72].refused, 1, "{} running", listed.len()); + } + } + + /// A PID one program takes its clock from and a later one carries counts the carrier's + /// units and the clock's PCR checks, each from the program reading it that way. + #[test] + fn a_pid_read_as_a_clock_and_a_stream_counts_both() { + let origin = crate::source::produce_origin(); + let mut programs = Programs::new( + origin, + moq_net::Path::from_static("event.hang"), + catalog::Config::default(), + ); + let mut mux = Mux::default(); + let both = [(1, 0x100), (2, 0x200)]; + mux.pat(&both).pmt(0x100, 1, 0x61, &[(MP2, 0x62)], 0); + programs.decode(&std::mem::take(&mut mux.out)).unwrap(); + mux.pat(&both).pmt(0x200, 2, 0x1f2, &[(MP2, 0x61)], 0); + programs.decode(&std::mem::take(&mut mux.out)).unwrap(); + assert_eq!(programs.programs.len(), 2); + // PCRs every 50 ms but for a 150 ms gap on 0x61, which breaks the 100 ms bound once, and + // a PES of two frames on each stream every 150 ms. + let mut cc = 0; + for ms in (0..=450).step_by(50) { + let pcr = Flags::pcr(27_000_000 + ms * MS); + if !(200..=250).contains(&ms) { + mux.packet(0x61, pcr); + } + mux.packet(0x1f2, pcr); + if ms % 150 == 0 { + for pid in [0x61, 0x62] { + mux.out.extend(mp2_pes(pid, cc, 90_000 + ms * 90, [pid as u8; 2])); + } + cc += 1; + } + } + programs.decode(&std::mem::take(&mut mux.out)).unwrap(); + programs.finish().unwrap(); + let stats = programs.stats(); + assert_eq!(stats.streams[&0x61].units, 8, "program 2 delivers every frame"); + assert_eq!(stats.pcr_repetition_error, 1, "program 1 grades its clock"); + } + + /// A PMT that drops a shared H.264 PID from the program counting it flushes the picture it + /// held, which the program still carrying the PID delivers itself at the next picture, so + /// the picture counts once. + #[test] + fn a_picture_a_dropped_route_flushed_counts_once() { + const H264: u8 = 0x1b; + let origin = crate::source::produce_origin(); + let mut programs = Programs::new( + origin, + moq_net::Path::from_static("event.hang"), + catalog::Config::default(), + ); + let mut mux = Mux::default(); + mux.pat(&[(1, 0x100), (2, 0x200)]) + .pmt(0x100, 1, 0x1f1, &[(H264, 0x50)], 0) + .pmt(0x200, 2, 0x1f2, &[(H264, 0x50)], 0) + .packet(0x1f1, Flags::pcr(27_000_000)) + .packet(0x1f2, Flags::pcr(27_000_000)); + 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.pmt_at(1, 0x100, 1, 0x1f1, &[(MP2, 0x61, &[])], 0); + programs.decode(&std::mem::take(&mut mux.out)).unwrap(); + 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(); + assert_eq!(units(&programs), 3); + } + /// 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]