From c4a6dc6585a83d765e8e909064d7875bae49658e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 15 Nov 2026 04:48:41 -0800 Subject: [PATCH 1/3] chore(quest): claim quest/m1/ts-republish.md Quest-Claim: aad1dc9a150fa44b From 17c71fd4b5c3e577b54a10b2db24c020c55982ae Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 15 Nov 2026 06:13:03 -0800 Subject: [PATCH 2/3] feat(mux): one TS republish, shared by import ts and import srt ts::Republish imports one program onto a path and republishes it under a new instance at each change of source, returning the step as a value. Each instance stays hidden until its catalog goes out and carries the measured mux rate, which moq-srt's copy did neither of. Co-Authored-By: Claude Opus 5.5 --- doc/bin/srt.md | 7 +- doc/bin/ts.md | 5 +- quest/m1/ts-republish.md | 34 -- quest/m3/ts-multi-program-restart.md | 7 +- rs/moq-cli/src/publish.rs | 446 ++++++++++------------- rs/moq-mux/src/container/ts/import.rs | 20 +- rs/moq-mux/src/container/ts/mod.rs | 2 + rs/moq-mux/src/container/ts/republish.rs | 394 ++++++++++++++++++++ rs/moq-mux/src/container/ts/step.rs | 4 +- rs/moq-mux/src/container/ts/step_test.rs | 6 +- rs/moq-srt/src/ts.rs | 192 +++------- 11 files changed, 684 insertions(+), 433 deletions(-) delete mode 100644 quest/m1/ts-republish.md create mode 100644 rs/moq-mux/src/container/ts/republish.rs diff --git a/doc/bin/srt.md b/doc/bin/srt.md index eca541716c4..3cf1d8ead57 100644 --- a/doc/bin/srt.md +++ b/doc/bin/srt.md @@ -26,8 +26,11 @@ moq --connect https://relay.example.com/anon --broadcast event.hang import srt - Each connection publishes under a fresh [instance](/concept/moq-lite#publisher-instances), so an encoder that reconnects -while its stale connection is still open replaces it at once: viewers see the -broadcast restart, and their next subscribe reaches the new feed. An +while its stale connection is still open replaces it once the new feed has +declared its tracks: viewers see the broadcast restart, and their next +subscribe reaches the new feed. A source that steps onto another timeline +republishes on the same connection, as [`import ts`](/bin/ts#programs-and-changes) +does. An `export srt` stream ends with its broadcast; `--follow` and `--stitch` follow a return or a replacement on the same connection, as they do for [`export ts`](/bin/cli#retention-and-latency). diff --git a/doc/bin/ts.md b/doc/bin/ts.md index daa351a2626..d0e64a56ff9 100644 --- a/doc/bin/ts.md +++ b/doc/bin/ts.md @@ -6,7 +6,8 @@ description: Carry a transport stream through MoQ, demultiplexed or whole, and w # MPEG-TS `moq import ts` reads a transport stream on stdin and `moq export ts` writes one -to stdout. [SRT](/bin/srt) carries the same import and export over the network. +to stdout. [SRT](/bin/srt) carries the semantic import and export over the +network; it has no passthrough mode. There are two modes, and they make different promises. ```bash @@ -92,7 +93,7 @@ track and continues under a fresh name; the export announces each layout at its next PMT `version_number`. A PAT that moves the PMT is followed, and the export moves its own PMT behind a new PAT. -The import republishes as a new instance when the source moves onto another +The import, over stdin or SRT, republishes as a new instance when the source moves onto another timeline: the PCR and every stream stepping back, flagged or not, a step more than 10 s forward without `discontinuity_indicator`, or a PAT naming another `transport_stream_id` or program number. The new stream carries nothing over diff --git a/quest/m1/ts-republish.md b/quest/m1/ts-republish.md deleted file mode 100644 index 2bb189033ea..00000000000 --- a/quest/m1/ts-republish.md +++ /dev/null @@ -1,34 +0,0 @@ -# [S] One TS republish, owned by moq-mux - -## Goal - -`moq import srt` and `moq import ts` republish a stepped single-program TS -source through one moq-mux implementation: the new instance keeps the -measured mux rate, stays hidden until its catalog is out, and the step reaches -the caller as a typed value rather than an error it downcasts. - -## Plan - -Found 2026-11-14 by the [feature review](/quest/m3/release/feature-review.md). -Three places mint TS instances: `container::ts::Programs` in moq-mux, the -CLI's `Republish` (`rs/moq-cli/src/publish.rs`), and moq-srt's `Instance` -(`rs/moq-srt/src/ts.rs`). [moq-dev/moq#7293](https://github.com/moq-dev/moq/pull/7293) -and [moq-dev/moq#7780](https://github.com/moq-dev/moq/pull/7780) fixed only -the CLI copy, so moq-srt's `Instance::publish` builds a default catalog on a -default, visible route, and `Import::new` re-arms the 2 s mux-rate hold on -every step. The guide (`doc/bin/ts.md`, Programs and changes) says a -republished stream "keeps the measured mux rate", which is false for SRT, -the path a live contributor uses. Both callers also find the step by -`err.downcast::()`, which AGENTS.md's "never match on errors" -forbids. - -- Move the CLI's `Republish` into `container::ts` as the single-program - sibling of `Programs`, returning the step as a variant of what decode - returns, and have both callers use it. -- Fix `doc/bin/ts.md`'s claim that SRT carries "the same import and export": - SRT has no passthrough. -- [Multi-program restart](/quest/m3/ts-multi-program-restart.md) then reuses - this instead of a fourth copy. - -Public API: moq-mux gains the republishing import; the `ts::Restart` error -goes if nothing else returns it. Wire: none. diff --git a/quest/m3/ts-multi-program-restart.md b/quest/m3/ts-multi-program-restart.md index 8409b74735f..f040776c5b8 100644 --- a/quest/m3/ts-multi-program-restart.md +++ b/quest/m3/ts-multi-program-restart.md @@ -23,8 +23,11 @@ restart path, so a changed `transport_stream_id` still ends the whole import. - Reuse #7780's step judgement and its "carry nothing" rule rather than a - second mechanism; decide whether the switch restarts every program or only - the ones whose identity or clock changed, and say why in the PR. + second mechanism, and the single-program `ts::Republish` + (`rs/moq-mux/src/container/ts/republish.rs`) for minting each program's next + instance with its measured mux rate, rather than a fourth copy; decide + whether the switch restarts every program or only the ones whose identity or + clock changed, and say why in the PR. - Keep the [MPEG-TS guide](/doc/bin/ts.md#programs-and-changes)'s rules for PAT changes that aren't a source switch (a moved PMT PID, a program added or removed) as they are. diff --git a/rs/moq-cli/src/publish.rs b/rs/moq-cli/src/publish.rs index 47d1d079fa9..25cf84a62a9 100644 --- a/rs/moq-cli/src/publish.rs +++ b/rs/moq-cli/src/publish.rs @@ -177,9 +177,9 @@ enum PublishDecoder { import: Box, }, Fmp4(Box), - // TS carries undecoded elementary streams (SCTE-35, teletext, DVB AC-3, ...) - // verbatim, so it uses the `mpegts` catalog extension rather than the media-only `()`. - Ts(Box>), + /// `import ts`: one program, owning its route and catalog, republished on each change of + /// source. + Ts(Box), /// `import ts --program all`: one importer, broadcast, and catalog per program. TsPrograms(Box), /// `import ts --passthrough`: the multiplex whole, on one track. @@ -191,19 +191,22 @@ impl PublishDecoder { /// Decode a chunk of stdin bytes. Each importer buffers any partial trailing /// frame internally, so the caller feeds fresh chunks rather than an /// accumulating buffer. - fn decode_chunk(&mut self, chunk: &[u8]) -> anyhow::Result<()> { + /// + /// A TS import whose source changed returns the change before reading on; see + /// [`ts::Republish::decode`]. + fn decode_chunk(&mut self, chunk: &[u8]) -> anyhow::Result> { match self { Self::Avc3 { split, import } => { let frames = split.decode(chunk, None)?; import.decode(frames)?; } Self::Fmp4(d) => d.decode(chunk)?, - Self::Ts(d) => d.decode(chunk).map_err(suggest_program)?, + Self::Ts(d) => return d.decode(chunk).map_err(suggest_program), Self::TsPrograms(d) => d.decode(chunk)?, Self::TsPassthrough(d) => d.decode(chunk)?, Self::Flv(d) => d.decode(chunk)?, } - Ok(()) + Ok(None) } /// What each elementary stream has delivered and the audio frame sync lost so far, for @@ -253,16 +256,17 @@ impl PublishDecoder { enum FinishedDecoder { Avc3(Box), Fmp4(Box), - Ts(Box>), + Ts(Box), TsPrograms(Box), TsPassthrough(Box), Flv(Box), } impl FinishedDecoder { - /// The broadcasts `import ts --program all` owns; see [`ts::Programs::finish`]. + /// The broadcasts a TS import owns; see [`ts::Republish::finish`] and [`ts::Programs::finish`]. fn broadcasts(&self) -> Vec { match self { + Self::Ts(d) => vec![d.broadcast().clone()], Self::TsPrograms(d) => d.broadcasts(), _ => Vec::new(), } @@ -298,20 +302,19 @@ fn suggest_program(err: anyhow::Error) -> anyhow::Error { } } -/// The catalog a stdin decoder publishes into. TS carries the `mpegts` extension. +/// The catalog a stdin decoder publishes into. enum PublishCatalog { - Media(moq_mux::catalog::Producer), - Ts(moq_mux::catalog::Producer), - /// `import ts --program all`: each program's catalog, finished by [`ts::Programs::finish`]. - TsPrograms, + Media(Box), + /// A TS import's: each instance's or program's, with the `mpegts` extension, finished by + /// [`ts::Republish::finish`] or [`ts::Programs::finish`]. + Ts, } impl PublishCatalog { fn abort(self, err: moq_net::Error) { match self { Self::Media(catalog) => catalog.abort(err), - Self::Ts(catalog) => catalog.abort(err), - Self::TsPrograms => {} + Self::Ts => {} } } @@ -319,42 +322,10 @@ impl PublishCatalog { fn finish(&mut self) -> anyhow::Result<()> { match self { Self::Media(catalog) => catalog.finish()?, - Self::Ts(catalog) => catalog.finish()?, - Self::TsPrograms => {} + Self::Ts => {} } Ok(()) } - - /// The multiplex rate a TS catalog records. - fn mux_rate(&self) -> Option { - match self { - Self::Ts(catalog) => catalog.snapshot().ext.mpegts.mux_rate, - Self::Media(_) | Self::TsPrograms => None, - } - } -} - -/// Build the importer and `mpegts` catalog for one TS broadcast. -/// -/// TS carries undecoded elementary streams (SCTE-35, teletext, DVB AC-3, ...) verbatim, so it -/// uses the `mpegts` catalog extension rather than the media-only `()`. The catalog producer -/// owns the broadcast's catalog tracks, so each broadcast gets exactly one. -fn ts_import( - broadcast: &mut moq_net::broadcast::Producer, - config: moq_mux::catalog::Config, - program: Option, - mux_rate: Option, -) -> anyhow::Result<(ts::Import, moq_mux::catalog::Producer)> { - let mut initial = moq_mux::catalog::hang::Catalog::::default(); - // A rate already measured on this multiplex releases the catalog with the first media. - initial.ext.mpegts.mux_rate = mux_rate; - let config = config.with_catalog(initial); - let catalog = moq_mux::catalog::Producer::new(broadcast, config)?; - let mut import = ts::Import::new(broadcast.clone(), catalog.timebase().reserve()); - if let Some(program) = program { - import = import.with_program(program); - } - Ok((import, catalog)) } #[cfg_attr( @@ -401,8 +372,8 @@ pub struct Publish { report: Option, // How long a stdin import keeps its broadcast up after EOF; see [`Self::with_linger`]. linger: std::time::Duration, - // How a stdin TS import republishes when its source changes; see [`Self::stdin`]. - republish: Option, + // What a stdin TS import sets on each instance it republishes; see [`Self::stdin`]. + sections: Sections, } /// A broadcast's announcement, held until its catalog first goes out. @@ -447,23 +418,10 @@ impl Publish { format: &PublishFormat, config: moq_mux::catalog::Config, ) -> anyhow::Result { - // TS builds its `Ext` catalog instead of the shared `()` below. - if let PublishFormat::Ts { program } = *format { - let (ts, catalog) = ts_import(&mut broadcast, config, program, None)?; - return Ok(Self { - source: Source::Stream { - decoder: PublishDecoder::Ts(Box::new(ts)), - catalog: PublishCatalog::Ts(catalog), - }, - broadcast: Some(broadcast), - route: None, - announce: false, - report: None, - linger: moq_mux::Linger::DEFAULT, - republish: None, - }); - } - + anyhow::ensure!( + !matches!(format, PublishFormat::Ts { .. }), + "a TS import publishes through an origin, which republishes it as its source changes" + ); let catalog = moq_mux::catalog::Producer::new(&mut broadcast, config)?; let decoder = match format { PublishFormat::Avc3 => { @@ -480,7 +438,7 @@ impl Publish { let fmp4 = fmp4::Import::new(broadcast.clone(), timebase.reserve()).with_cmaf(*cmaf); PublishDecoder::Fmp4(Box::new(fmp4)) } - PublishFormat::Ts { .. } => unreachable!("TS is handled above with the mpegts catalog extension"), + PublishFormat::Ts { .. } => unreachable!("refused above"), // The `m2ts` section is in the base catalog: nothing about the multiplex is described // beyond the track that carries it. PublishFormat::TsPassthrough { pcr_pid } => { @@ -503,23 +461,24 @@ impl Publish { Ok(Self { source: Source::Stream { decoder, - catalog: PublishCatalog::Media(catalog), + catalog: PublishCatalog::Media(Box::new(catalog)), }, broadcast: Some(broadcast), route: None, announce: false, report: None, linger: moq_mux::Linger::DEFAULT, - republish: None, + sections: Sections::default(), }) } /// Build a publisher decoding `format` from stdin into a new broadcast at `name` on /// `origin` under `instance`, retaining its route alongside the decoder's fill handle. /// - /// A TS import whose source changes ([`ts::Restart`]), such as a changeover between two - /// encoders whose clocks were never locked or whose PATs name another program, finishes the broadcast - /// and republishes the rest of the input at `name` under a new instance. + /// A TS import whose source changes, such as a changeover between two encoders whose clocks + /// were never locked or whose PATs name another program, finishes the broadcast and republishes + /// the rest of the input at `name` under a new instance ([`ts::Republish`]). Its first + /// instance is `instance`. pub(super) fn stdin( origin: &moq_net::origin::Producer, name: &str, @@ -527,23 +486,30 @@ impl Publish { format: &PublishFormat, config: moq_mux::catalog::Config, ) -> anyhow::Result { + if let PublishFormat::Ts { program } = *format { + let mut republish = + ts::Republish::new(origin.clone(), moq_net::Path::new(name)?, config)?.with_instance(instance)?; + if let Some(program) = program { + republish = republish.with_program(program); + } + return Ok(Self { + source: Source::Stream { + decoder: PublishDecoder::Ts(Box::new(republish)), + catalog: PublishCatalog::Ts, + }, + broadcast: None, + route: None, + announce: false, + report: None, + linger: moq_mux::Linger::DEFAULT, + sections: Sections::default(), + }); + } let route = origin .route(name, moq_net::origin::RouteOptions::hidden().with_instance(instance, 0)) .context("failed to create broadcast")?; - let republish = match *format { - PublishFormat::Ts { program } => Some(Republish { - origin: origin.clone(), - name: name.to_string(), - config: config.clone(), - program, - report: None, - echo: None, - }), - _ => None, - }; let mut result = Self::new(route.broadcast().clone(), format, config)?; result.route = Some(route); - result.republish = republish; Ok(result) } @@ -561,14 +527,14 @@ impl Publish { Self { source: Source::Stream { decoder: PublishDecoder::TsPrograms(Box::new(ts::Programs::new(origin, name, config))), - catalog: PublishCatalog::TsPrograms, + catalog: PublishCatalog::Ts, }, broadcast: None, route: None, announce: false, report: None, linger: moq_mux::Linger::DEFAULT, - republish: None, + sections: Sections::default(), } } @@ -613,7 +579,7 @@ impl Publish { announce: false, report: None, linger: moq_mux::Linger::DEFAULT, - republish: None, + sections: Sections::default(), }) } @@ -623,20 +589,21 @@ impl Publish { /// /// Call it before the importer declares its tracks, so the section rides the first catalog. pub fn with_report(mut self, connection: Option) -> anyhow::Result { - let Some(broadcast) = self.broadcast.as_mut() else { - anyhow::bail!("`import ts --program all` builds a catalog per program"); - }; - if let Some(republish) = &mut self.republish { - republish.report = Some(connection.clone()); - } let report = match &self.source { - Source::Stream { catalog, .. } => match catalog { - PublishCatalog::Media(catalog) => crate::report::Stats::new(broadcast, catalog, connection)?, - PublishCatalog::Ts(catalog) => crate::report::Stats::new(broadcast, catalog, connection)?, - PublishCatalog::TsPrograms => anyhow::bail!("`import ts --program all` builds a catalog per program"), + Source::Stream { decoder, catalog } => match (decoder, catalog, self.broadcast.as_mut()) { + (_, PublishCatalog::Media(catalog), Some(broadcast)) => { + crate::report::Stats::new(broadcast, catalog, connection)? + } + (PublishDecoder::Ts(republish), _, _) => { + self.sections.report = Some(connection.clone()); + report(republish, connection)? + } + _ => anyhow::bail!("`import ts --program all` builds a catalog per program"), }, #[cfg(feature = "capture")] - Source::Capture { catalog, .. } => crate::report::Stats::new(broadcast, catalog, connection)?, + Source::Capture { broadcast, catalog, .. } => { + crate::report::Stats::new(&mut broadcast.clone(), catalog, connection)? + } }; self.report = Some(report); Ok(self) @@ -646,21 +613,20 @@ impl Publish { /// /// Call it before the importer declares its tracks, so the section rides the first catalog. pub fn with_echo(mut self, echo: hang::catalog::Echo) -> anyhow::Result { - let catalog = match &mut self.source { - Source::Stream { catalog, .. } => catalog, + match &mut self.source { + Source::Stream { decoder, catalog } => match (decoder, catalog) { + (_, PublishCatalog::Media(catalog)) => catalog.mutate(|catalog| catalog.echo = Some(echo))?, + (PublishDecoder::Ts(republish), _) => { + self.sections.echo = Some(echo.clone()); + republish + .catalog() + .clone() + .mutate(|catalog| catalog.echo = Some(echo))?; + } + _ => anyhow::bail!("`import ts --program all` builds a catalog per program"), + }, #[cfg(feature = "capture")] - Source::Capture { catalog, .. } => { - catalog.mutate(|catalog| catalog.echo = Some(echo))?; - return Ok(self); - } - }; - if let Some(republish) = &mut self.republish { - republish.echo = Some(echo.clone()); - } - match catalog { - PublishCatalog::Media(catalog) => catalog.mutate(|catalog| catalog.echo = Some(echo))?, - PublishCatalog::Ts(catalog) => catalog.mutate(|catalog| catalog.echo = Some(echo))?, - PublishCatalog::TsPrograms => anyhow::bail!("`import ts --program all` builds a catalog per program"), + Source::Capture { catalog, .. } => catalog.mutate(|catalog| catalog.echo = Some(echo))?, } Ok(self) } @@ -700,7 +666,7 @@ impl Publish { pending, report: self.report, linger: self.linger, - republish: self.republish, + sections: self.sections, }; decode(decoder, catalog, tokio::io::stdin(), publication).await } @@ -793,55 +759,43 @@ struct Publication { report: Option, /// How long the finished broadcasts stay up after EOF. linger: std::time::Duration, - /// How a change of source republishes, or `None` to end the import there. - republish: Option, + /// What a TS import sets on each instance it republishes. + sections: Sections, } -/// How a stdin TS import republishes its path when the source changes ([`ts::Restart`]): as a -/// new broadcast under a new instance, with the same catalog sections. -struct Republish { - origin: moq_net::origin::Producer, - name: String, - config: moq_mux::catalog::Config, - program: Option, +/// The catalog sections a stdin TS import sets again on each instance it republishes. +#[derive(Clone, Default)] +struct Sections { /// The connection `--report` measures, when the import reports. report: Option>, echo: Option, } -impl Republish { - /// A new instance of the broadcast under a new instance, announced once its catalog goes out. - /// - /// `mux_rate` is the previous instance's measurement of the same multiplex, which the new - /// catalog carries from its first media rather than waiting on a fresh window. - fn instance(&self, mux_rate: Option) -> anyhow::Result { - let control = self - .origin - .route(&self.name, moq_net::origin::RouteOptions::hidden()) - .context("failed to create broadcast")?; - let mut broadcast = control.broadcast().clone(); - let (import, mut catalog) = ts_import(&mut broadcast, self.config.clone(), self.program, mux_rate)?; - // Before the import declares a track, so the sections ride the first catalog. - let report = match &self.report { - Some(connection) => Some(crate::report::Stats::new(&mut broadcast, &catalog, connection.clone())?), - None => None, - }; +impl Sections { + /// Set these sections on the instance `republish` just opened, before it declares a track so + /// they ride its first catalog, returning its report. + fn apply(&self, republish: &ts::Republish) -> anyhow::Result> { if let Some(echo) = &self.echo { - catalog.mutate(|catalog| catalog.echo = Some(echo.clone()))?; + republish + .catalog() + .clone() + .mutate(|catalog| catalog.echo = Some(echo.clone()))?; } - tracing::info!(instance = %control.instance(), "publisher instance"); - let pending = Pending::new(control.clone())?; - Ok(Instance { - decoder: PublishDecoder::Ts(Box::new(import)), - catalog: PublishCatalog::Ts(catalog), - control: Some(control), - broadcast: Some(broadcast), - pending: Some(pending), - report, - }) + self.report + .as_ref() + .map(|connection| report(republish, connection.clone())) + .transpose() } } +/// A `--report` track on the current instance of `republish`. +fn report( + republish: &ts::Republish, + connection: Option, +) -> anyhow::Result { + crate::report::Stats::new(&mut republish.broadcast().clone(), republish.catalog(), connection) +} + /// One publisher instance of a stdin import: the decoder filling its broadcast, and what /// announces and reports it. struct Instance { @@ -872,8 +826,8 @@ struct Reader { /// The finished broadcasts then stay servable for the linger, so a subscriber that saw the /// final catalog only at EOF can still request them. /// -/// A change of source ([`ts::Restart`]) ends the instance there. With `republish`, the rest of the -/// input publishes a new instance on the same path; otherwise the import fails with it. +/// A change of source ([`ts::Republished`]) ends a TS import's instance there, and the rest of the +/// input publishes a new instance on the same path. async fn decode( decoder: PublishDecoder, catalog: PublishCatalog, @@ -886,7 +840,7 @@ async fn decode( pending, report, linger, - republish, + sections, } = publication; let mut instance = Instance { decoder, @@ -908,15 +862,12 @@ async fn decode( }; let result = loop { - let restart = match instance.read(&mut reader).await { - Ok(Some(restart)) => restart, + let republished = match instance.read(&mut reader).await { + Ok(Some(republished)) => republished, Ok(None) => break Ok(()), Err(err) => break Err(err), }; - let Some(republish) = &republish else { - break Err(restart.into()); - }; - if let Err(err) = instance.restart(republish, restart, &mut reader.log) { + if let Err(err) = instance.republished(§ions, republished, &mut reader.log) { break Err(err); } }; @@ -929,7 +880,7 @@ impl Instance { async fn read( &mut self, reader: &mut Reader, - ) -> anyhow::Result> { + ) -> anyhow::Result> { let Self { decoder, pending, @@ -946,8 +897,8 @@ impl Instance { if n == 0 { return Ok(None); // EOF } - if let Some(restart) = feed(decoder, pending, &reader.buffer)? { - return Ok(Some(restart)); + if let Some(republished) = feed(decoder, pending, &reader.buffer)? { + return Ok(Some(republished)); } if reader.sampled.elapsed() >= ts::stats::Log::INTERVAL { @@ -970,39 +921,29 @@ impl Instance { } } - /// The source changed: finish this instance before the change, and replace it with a new one - /// on the same path that takes the rest of the input. - fn restart(&mut self, republish: &Republish, restart: ts::Restart, log: &mut ts::stats::Log) -> anyhow::Result<()> { - let mut restart = restart; + /// The source changed, and the importer republished: end this instance's report, and set the + /// sections on the new instance before it reads the rest of the input. + fn republished( + &mut self, + sections: &Sections, + republished: ts::Republished, + log: &mut ts::stats::Log, + ) -> anyhow::Result<()> { + let mut republished = republished; loop { - tracing::warn!(step = %restart.step, "the source changed; republishing"); - // Hidden until its catalog goes out, so it announces only after this instance finished. - let next = republish.instance(self.catalog.mux_rate())?; - let Self { - decoder, - mut catalog, - mut report, - control, - broadcast, - pending, - } = std::mem::replace(self, next); - let decoder = match decoder.finish() { - Ok(decoder) => decoder, - Err(err) => return Err(fail(catalog, report, err)), + let PublishDecoder::Ts(republish) = &self.decoder else { + unreachable!("only a TS import republishes"); }; - if let Some(stats) = decoder.stats() { - report_mpegts(&report, &stats); - log.finish(&stats); - } - if let Some(report) = &mut report { + tracing::warn!(step = %republished.step, instance = %republish.instance(), "the source changed; republishing"); + report_mpegts(&self.report, &republished.stats); + log.finish(&republished.stats); + if let Some(report) = &mut self.report { report.finish(); } - catalog.finish()?; - // The finished broadcast ends as its handles drop, withdrawing its announcement. - drop((decoder, catalog, report, control, broadcast, pending)); + self.report = sections.apply(republish)?; *log = ts::stats::Log::default(); - match feed(&mut self.decoder, &mut self.pending, &restart.remainder)? { - Some(next) => restart = next, + match feed(&mut self.decoder, &mut self.pending, &[])? { + Some(next) => republished = next, None => return Ok(()), } } @@ -1100,9 +1041,9 @@ fn feed( decoder: &mut PublishDecoder, pending: &mut Option, chunk: &[u8], -) -> anyhow::Result> { - if let Err(err) = decoder.decode_chunk(chunk) { - return err.downcast::().map(Some); +) -> anyhow::Result> { + if let Some(republished) = decoder.decode_chunk(chunk)? { + return Ok(Some(republished)); } if let Some(announce) = pending && announce.announce()? @@ -1527,6 +1468,35 @@ pub(crate) mod tests { } /// A source for the broadcast at `path`, resolved through `origin`. + /// A stdin TS import of `demo` on an origin of its own, and a reader of its first instance's + /// broadcast. + fn ts_stdin() -> (Publish, moq_net::broadcast::Consumer) { + let (origin, driver) = moq_net::origin::Producer::new(moq_net::origin::Config::default()); + if tokio::runtime::Handle::try_current().is_ok() { + tokio::spawn(moq_net::time::run(driver)); + } else { + // Nothing polls the driver in a sync test, and dropping it would tear the origin down. + std::mem::forget(driver); + } + let publish = Publish::stdin( + &origin, + "demo", + moq_net::Instance::random(), + &PublishFormat::Ts { program: None }, + Default::default(), + ) + .unwrap(); + let Source::Stream { + decoder: PublishDecoder::Ts(republish), + .. + } = &publish.source + else { + panic!("a TS import"); + }; + let consumer = republish.broadcast().consume(); + (publish, consumer) + } + async fn resolved(origin: &moq_net::origin::Producer, path: &str) -> moq_mux::Source { let consumer = origin.consume(); let broadcast = consumer.request(path, None).await.unwrap(); @@ -1557,9 +1527,10 @@ pub(crate) mod tests { // fire instantly instead of waiting on the wall clock. let input = manufacture_input().await; - // Publish side: `Publish::new(Ts)` builds a `ts::Import`, so the verbatim - // streams land in the broadcast instead of being dropped by the media-only path. - // The broadcast is created on a throwaway origin so the exporter can resolve it by path. + // Publish side: a TS import uses the `mpegts` catalog, so the verbatim streams land in + // the broadcast instead of being dropped by the media-only path. The broadcast is created + // on a throwaway origin so the exporter can resolve it by path, announced once its + // catalog goes out. let origin = moq_tokio::origin::spawn(); let publish = Publish::stdin( &origin, @@ -1569,8 +1540,6 @@ pub(crate) mod tests { Default::default(), ) .unwrap(); - publish.route.as_ref().unwrap().announce().unwrap(); - settle().await; #[cfg_attr( not(feature = "capture"), expect( @@ -1941,9 +1910,7 @@ pub(crate) mod tests { /// clock on it, so the first frame is live on arrival. #[tokio::test] async fn ts_import_anchors_the_catalog_clock_on_arrival() { - let broadcast = moq_net::broadcast::Info::new().produce(); - let consumer = broadcast.consume(); - let publish = Publish::new(broadcast, &PublishFormat::Ts { program: None }, Default::default()).unwrap(); + let (publish, consumer) = ts_stdin(); #[cfg_attr( not(feature = "capture"), expect( @@ -1988,13 +1955,9 @@ pub(crate) mod tests { /// would announce a stdin import before it can serve anything. #[tokio::test] async fn echo_rides_the_first_catalog() { - let broadcast = moq_net::broadcast::Info::new().produce(); - let consumer = broadcast.consume(); + let (publish, consumer) = ts_stdin(); let echo = hang::catalog::Echo::new(moq_net::path::Relative::new("viewers").unwrap()); - let publish = Publish::new(broadcast, &PublishFormat::Ts { program: None }, Default::default()) - .unwrap() - .with_echo(echo.clone()) - .unwrap(); + let publish = publish.with_echo(echo.clone()).unwrap(); let mut track = consumer.track(Catalog::DEFAULT_NAME).unwrap(); let mut published = || { matches!( @@ -2050,8 +2013,7 @@ pub(crate) mod tests { } } - let broadcast = moq_net::broadcast::Info::new().produce(); - let publish = Publish::new(broadcast, &PublishFormat::Ts { program: None }, Default::default()).unwrap(); + let (publish, _) = ts_stdin(); #[cfg_attr( not(feature = "capture"), expect( @@ -2095,8 +2057,7 @@ pub(crate) mod tests { } file.flush().unwrap(); let input = tokio::fs::File::open(file.path()).await.unwrap(); - let broadcast = moq_net::broadcast::Info::new().produce(); - let publish = Publish::new(broadcast, &PublishFormat::Ts { program: None }, Default::default()).unwrap(); + let (publish, _) = ts_stdin(); #[cfg_attr( not(feature = "capture"), expect( @@ -2119,9 +2080,7 @@ pub(crate) mod tests { /// subscriber reads a finished broadcast rather than its tracks being removed. #[tokio::test(start_paused = true)] async fn eof_finishes_the_catalog_with_its_renditions() { - let broadcast = moq_net::broadcast::Info::new().produce(); - let consumer = broadcast.consume(); - let publish = Publish::new(broadcast, &PublishFormat::Ts { program: None }, Default::default()).unwrap(); + let (publish, consumer) = ts_stdin(); let mut catalogs = hang::catalog::Catalog::<()>::subscribe(&consumer).await.unwrap(); #[cfg_attr( @@ -2157,9 +2116,7 @@ pub(crate) mod tests { #[tokio::test(start_paused = true)] async fn a_finished_import_serves_its_tracks_for_the_linger() { const LINGER: Duration = Duration::from_secs(30); - let broadcast = moq_net::broadcast::Info::new().produce(); - let consumer = broadcast.consume(); - let publish = Publish::new(broadcast, &PublishFormat::Ts { program: None }, Default::default()).unwrap(); + let (publish, consumer) = ts_stdin(); let mut catalogs = hang::catalog::Catalog::<()>::subscribe(&consumer).await.unwrap(); #[cfg_attr( @@ -2232,7 +2189,8 @@ pub(crate) mod tests { Default::default(), ) .unwrap(); - let broadcast = publish.route.clone().unwrap(); + // A TS import announces through its own route. + let pending = publish.route.clone().map(|route| Pending::new(route).unwrap()); #[cfg_attr( not(feature = "capture"), expect( @@ -2243,10 +2201,9 @@ pub(crate) mod tests { let Source::Stream { decoder, catalog } = publish.source else { panic!("expected a stream source"); }; - let pending = Pending::new(broadcast).unwrap(); let (mut stdin, input_end) = tokio::io::duplex(input.len()); let publication = Publication { - pending: Some(pending), + pending, ..Default::default() }; let mut import = std::pin::pin!(decode(decoder, catalog, input_end, publication)); @@ -2295,15 +2252,8 @@ pub(crate) mod tests { std::task::Poll::Ready(Err(std::io::Error::other("stdin failed"))) } } - let broadcast = moq_net::broadcast::Info::new().produce(); - let publish = Publish::new( - broadcast.clone(), - &PublishFormat::Ts { program: None }, - Default::default(), - ) - .unwrap(); - let mut catalog_reader = broadcast - .consume() + let (publish, consumer) = ts_stdin(); + let mut catalog_reader = consumer .track(hang::Catalog::DEFAULT_NAME) .unwrap() .subscribe(None) @@ -2366,12 +2316,8 @@ pub(crate) mod tests { let Source::Stream { decoder, catalog } = publish.source else { panic!("expected a stream source"); }; - let control = publish.route.unwrap(); let publication = Publication { - pending: Some(Pending::new(control.clone()).unwrap()), - control: Some(control), - broadcast: publish.broadcast, - republish: publish.republish, + sections: publish.sections, ..Default::default() }; let (mut stdin, input_end) = tokio::io::duplex(2 * CLIP.len() + flag.len()); @@ -2442,6 +2388,7 @@ pub(crate) mod tests { const CLIP: &[u8] = include_bytes!("../../moq-mux/src/container/ts/test_data/bbb5s_cbr.ts"); let origin = moq_tokio::origin::spawn(); + let mut announced = origin.consume().announced(); let publish = Publish::stdin( &origin, "demo", @@ -2457,24 +2404,28 @@ pub(crate) mod tests { let Source::Stream { decoder, catalog } = publish.source else { panic!("a stream source"); }; - let control = publish.route.unwrap(); let mut instance = Instance { decoder, catalog, - pending: Some(Pending::new(control.clone()).unwrap()), - control: Some(control), - broadcast: publish.broadcast, + pending: None, + control: None, + broadcast: None, report: None, }; - let republish = publish.republish.unwrap(); let mut log = ts::stats::Log::default(); let mut follower = None; + let mut listed = false; let mut output = [Vec::new(), Vec::new()]; for chunk in CLIP.iter().chain(CLIP).copied().collect::>().chunks(8 * 188) { - if let Some(restart) = feed(&mut instance.decoder, &mut instance.pending, chunk).unwrap() { - instance.restart(&republish, restart, &mut log).unwrap(); + if let Some(republished) = feed(&mut instance.decoder, &mut instance.pending, chunk).unwrap() { + instance + .republished(&Sections::default(), republished, &mut log) + .unwrap(); } - if follower.is_none() && instance.pending.is_none() { + while let Some(event) = announced.try_next() { + listed |= matches!(event, moq_net::announce::Event::Present(_)); + } + if follower.is_none() && listed { let export = open_export( resolved(&origin, "demo").await, CatalogFormat::Hang, @@ -2590,7 +2541,8 @@ pub(crate) mod tests { Default::default(), ) .unwrap(); - let broadcast = publish.route.clone().unwrap(); + // A TS import announces through its own route. + let pending = publish.route.clone().map(|route| Pending::new(route).unwrap()); #[cfg_attr( not(feature = "capture"), expect( @@ -2601,9 +2553,8 @@ pub(crate) mod tests { let Source::Stream { decoder, catalog } = publish.source else { panic!("expected a stream source"); }; - let pending = Pending::new(broadcast).unwrap(); let publication = Publication { - pending: Some(pending), + pending, ..Default::default() }; let err = decode(decoder, catalog, &input[..], publication).await.unwrap_err(); @@ -2710,8 +2661,7 @@ pub(crate) mod tests { /// Plain `import ts` refuses a multiplex, naming its programs and the flag that picks one. #[test] fn a_multiplex_is_refused_with_the_programs_and_the_flag() { - let broadcast = moq_net::broadcast::Info::new().produce(); - let mut publish = Publish::new(broadcast, &PublishFormat::Ts { program: None }, Default::default()).unwrap(); + let (mut publish, _) = ts_stdin(); #[cfg_attr( not(feature = "capture"), expect( diff --git a/rs/moq-mux/src/container/ts/import.rs b/rs/moq-mux/src/container/ts/import.rs index 62b19008ec0..7aca31889f2 100644 --- a/rs/moq-mux/src/container/ts/import.rs +++ b/rs/moq-mux/src/container/ts/import.rs @@ -428,7 +428,17 @@ impl Import { /// /// Input after [`finish`](Self::finish) is refused with [`Finished`](crate::Error::Finished), /// or with the cause of a finish that failed. + /// + /// [`Republish`](super::Republish) carries on past a restart instead, on a path of an origin. pub fn decode(&mut self, data: &[u8]) -> anyhow::Result<()> { + match self.decode_or_restart(data)? { + Some(restart) => Err(restart.into()), + None => Ok(()), + } + } + + /// [`decode`](Self::decode), returning a restart rather than failing with it. + pub(super) fn decode_or_restart(&mut self, data: &[u8]) -> anyhow::Result> { self.input.check()?; let mut demux = std::mem::take(&mut self.demux); let result = self.decode_with(&mut demux, data); @@ -436,7 +446,7 @@ impl Import { result } - fn decode_with(&mut self, demux: &mut Demux, data: &[u8]) -> anyhow::Result<()> { + fn decode_with(&mut self, demux: &mut Demux, data: &[u8]) -> anyhow::Result> { demux.push(data); // Route one whole packet at a time, so a PMT is parsed (and any PID it declares // registered) before the packets that follow it in the same chunk route. @@ -473,13 +483,14 @@ impl Import { // Held input keeps its arrival, and is graded once routed. super::step::Verdict::Hold => { demux.drain(); - return self.flush_batch(); + self.flush_batch()?; + return Ok(None); } super::step::Verdict::Restart(step) => { let remainder = demux.remainder(); demux.drain(); self.flush_batch()?; - return Err(super::Restart { step, remainder }.into()); + return Ok(Some(super::Restart { step, remainder })); } } let Some(packet) = demux.next() else { break }; @@ -488,7 +499,8 @@ impl Import { // Every packet this chunk completed is read: grade the input through its arrival. self.health.flush(); demux.drain(); - self.flush_batch() + self.flush_batch()?; + Ok(None) } /// Cut the snapshot groups for whatever SI committed since the last call, and flush the diff --git a/rs/moq-mux/src/container/ts/mod.rs b/rs/moq-mux/src/container/ts/mod.rs index 68ffe216303..d589f6edd73 100644 --- a/rs/moq-mux/src/container/ts/mod.rs +++ b/rs/moq-mux/src/container/ts/mod.rs @@ -35,6 +35,7 @@ pub mod passthrough; pub mod programs; mod psi; mod release; +pub mod republish; mod schedule; mod si; mod step; @@ -52,6 +53,7 @@ pub use import::*; pub use passthrough::Passthrough; pub use programs::Programs; pub use release::Release; +pub use republish::{Republish, Republished}; pub use step::{Restart, Step}; pub mod stats; diff --git a/rs/moq-mux/src/container/ts/republish.rs b/rs/moq-mux/src/container/ts/republish.rs new file mode 100644 index 00000000000..47c52b4d1fc --- /dev/null +++ b/rs/moq-mux/src/container/ts/republish.rs @@ -0,0 +1,394 @@ +//! One program of a multiplex, republished on its path whenever the source changes. + +use anyhow::Context; +use bytes::{Bytes, BytesMut}; + +use super::{Ext, Import, Step, stats}; +use crate::catalog; + +/// Imports one program of a multiplex as a broadcast on `origin`, republishing the path under a +/// new instance each time the source changes. +/// +/// The single-program sibling of [`Programs`](super::Programs). Each instance is created hidden +/// and announced once its catalog first goes out: a relay queries a broadcast's tracks the moment +/// it is announced, so one announced any sooner would be asked for tracks it cannot serve yet. +/// +/// A change of source is a [`Step`]: the source's clock moving onto another timeline, or its PAT +/// naming another program, as [`Import::decode`] judges it. [`decode`](Self::decode) then +/// finishes the instance before the step, withdrawing it, and opens the next under a new +/// instance, returning the step as [`Republished`]. Nothing crosses it but the measured mux rate: +/// the new catalog records it from its first media rather than holding for a fresh measurement +/// window, since the multiplex is the same. The new instance waits for the source's own PAT, PMT +/// and next keyframe. +pub struct Republish { + target: Target, + instance: Instance, + /// The input after the last step, which the current instance has not read yet. + held: Bytes, +} + +/// The source changed, so [`Republish`] finished the instance before the change and opened the +/// next one, hidden until its catalog goes out. +#[derive(Clone, Debug)] +#[non_exhaustive] +pub struct Republished { + /// How the source changed. + pub step: Step, + /// What the finished instance's elementary streams delivered; see [`Import::stats`]. + pub stats: stats::Snapshot, +} + +/// Where and how each instance publishes. +struct Target { + origin: moq_net::origin::Producer, + name: moq_net::Path, + config: catalog::Config, + program: Option, +} + +impl Target { + /// Open an instance with `options`, its catalog recording `mux_rate` when the multiplex was + /// measured already. + fn open(&self, options: moq_net::origin::RouteOptions, mux_rate: Option) -> anyhow::Result { + let route = self + .origin + .route(self.name.as_str(), options) + .with_context(|| format!("failed to create broadcast {}", self.name))?; + // TS carries undecoded elementary streams verbatim, so the catalog carries the `mpegts` + // extension. + let mut initial = catalog::hang::Catalog::::default(); + initial.ext.mpegts.mux_rate = mux_rate; + let config = self.config.clone().with_catalog(initial); + let catalog = catalog::Producer::new(&mut route.broadcast().clone(), config)?; + let mut import = Import::new(route.broadcast().clone(), catalog.reserve()); + if let Some(program) = self.program { + import = import.with_program(program); + } + let pending = Some(route.broadcast().consume().track(hang::Catalog::DEFAULT_NAME)?); + Ok(Instance { + route, + import, + catalog, + pending, + }) + } +} + +/// One instance of the broadcast. +struct Instance { + /// Created hidden: dropping it retracts the broadcast the importer fills. + route: moq_net::origin::Route, + import: Import, + catalog: catalog::Producer, + /// The catalog track until the catalog first goes out, when the route announces. + pending: Option, +} + +impl Instance { + /// Announce the broadcast once its catalog has gone out, returning whether it has. + fn announce_if_listed(&mut self) -> anyhow::Result { + listed(&mut self.pending, &self.route) + } + + /// Finish the media tracks, then the catalog while it still lists them, aborting both with the + /// cause of a failure. + /// + /// The `last` instance announces if its drain released the first catalog, and fails when the + /// input declared no track. One a step ended is withdrawn as it drops, so it does neither: a + /// source can step before its first catalog goes out, such as within the mux rate's window. + fn finish(mut self, last: bool) -> anyhow::Result { + let import = match self.import.finish() { + Ok(import) => import, + Err(err) => { + self.catalog.abort(moq_net::Error::Transport(format!("{err:#}"))); + return Err(err); + } + }; + // The drain at end of input can release the first catalog. Without one the input declared + // no track, and announcing would only advertise an empty broadcast. + let declared = match last { + true => listed(&mut self.pending, &self.route).and_then(|declared| { + anyhow::ensure!(declared, "input ended before it declared a track"); + Ok(()) + }), + false => Ok(()), + }; + let declared = declared.and_then(|()| Ok(self.catalog.finish()?)); + if let Err(err) = declared { + let reason = moq_net::Error::Transport(format!("{err:#}")); + import.abort(reason.clone()); + self.catalog.abort(reason); + return Err(err); + } + Ok(Finished { + import, + _catalog: self.catalog, + route: self.route, + }) + } +} + +impl Republish { + /// Import the multiplex onto `name`, its catalogs built from `config`, under a fresh + /// instance created now, so its [`catalog`](Self::catalog) can be filled before any input. + pub fn new( + origin: moq_net::origin::Producer, + name: impl Into, + config: catalog::Config, + ) -> anyhow::Result { + let target = Target { + origin, + name: name.into(), + config, + program: None, + }; + let instance = target.open(moq_net::origin::RouteOptions::hidden(), None)?; + Ok(Self { + target, + instance, + held: Bytes::new(), + }) + } + + /// Import only the program with this PAT program number; see [`Import::with_program`]. + pub fn with_program(mut self, program: u16) -> Self { + self.target.program = Some(program); + self.instance.import = self.instance.import.with_program(program); + self + } + + /// Publish the first instance as `instance` rather than a fresh one, such as one a run shares + /// across its broadcasts; see [`RouteOptions::with_instance`](moq_net::origin::RouteOptions::with_instance). + /// + /// Call it before filling the [`catalog`](Self::catalog): it replaces the first instance. + pub fn with_instance(mut self, instance: moq_net::Instance) -> anyhow::Result { + let options = moq_net::origin::RouteOptions::hidden().with_instance(instance, 0); + self.instance = self.target.open(options, None)?; + Ok(self) + } + + /// The current instance's broadcast, such as for a track of the caller's own. + pub fn broadcast(&self) -> &moq_net::broadcast::Producer { + self.instance.route.broadcast() + } + + /// The current instance's catalog. Sections set before the importer declares a track ride + /// the first catalog; set them again on each new instance after [`Republished`]. + pub fn catalog(&self) -> &catalog::Producer { + &self.instance.catalog + } + + /// The current instance's publisher instance. + pub fn instance(&self) -> moq_net::Instance { + self.instance.route.instance() + } + + /// Demux a chunk of the multiplex into the current instance, announcing it once its catalog + /// goes out. A trailing partial packet is retained for the next call. + /// + /// A change of source finishes the instance there and opens the next, returning + /// [`Republished`] before the next reads anything: the rest of `data` is held for it, read at + /// the start of the next call. Call again, with no data if there is none, until it returns + /// `None`. A failure to finish the instance before the step returns that failure, with the + /// next instance current. + pub fn decode(&mut self, data: &[u8]) -> anyhow::Result> { + if !self.held.is_empty() { + let held = std::mem::take(&mut self.held); + if let Some(republished) = self.feed(&held)? { + if !data.is_empty() { + let mut rest = BytesMut::from(std::mem::take(&mut self.held)); + rest.extend_from_slice(data); + self.held = rest.freeze(); + } + return Ok(Some(republished)); + } + } + self.feed(data) + } + + /// Demux `data` into the current instance, or republish at the step it reaches. + fn feed(&mut self, data: &[u8]) -> anyhow::Result> { + let Some(restart) = self.instance.import.decode_or_restart(data)? else { + self.instance.announce_if_listed()?; + return Ok(None); + }; + let mux_rate = self.instance.catalog.snapshot().ext.mpegts.mux_rate; + let next = self.target.open(moq_net::origin::RouteOptions::hidden(), mux_rate)?; + self.held = restart.remainder; + // The finished instance ends as it drops, withdrawing its announcement. + let finished = std::mem::replace(&mut self.instance, next).finish(false)?; + Ok(Some(Republished { + step: restart.step, + stats: finished.stats(), + })) + } + + /// What every elementary stream of the current instance has delivered; see [`Import::stats`]. + pub fn stats(&self) -> stats::Snapshot { + self.instance.import.stats() + } + + /// Finish the current instance's media tracks, then its catalog while it still lists them, + /// and return the [`Finished`] broadcast. + /// + /// Input still held after a [`Republished`] is read first, republishing again at any step in + /// it. Fails when the input ended before the instance declared a track, so nothing was + /// announced. A finish that fails aborts every track with the cause. + pub fn finish(mut self) -> anyhow::Result { + while let Some(republished) = self.decode(&[])? { + tracing::warn!(step = %republished.step, "the source changed at end of input; republishing"); + } + self.instance.finish(true) + } + + /// Abort the current instance's catalog and media tracks with `err`. Consumes the importer. + pub fn abort(self, err: moq_net::Error) { + let Instance { import, catalog, .. } = self.instance; + catalog.abort(err.clone()); + import.abort(err); + } +} + +/// Announce `route` once the catalog track `pending` has a group, returning whether it has. +fn listed(pending: &mut Option, route: &moq_net::origin::Route) -> anyhow::Result { + let Some(listed) = pending else { + return Ok(true); + }; + if !matches!( + listed.poll_largest(&kio::Waiter::noop()), + std::task::Poll::Ready(Some(_)) + ) { + return Ok(false); + } + route.announce().context("failed to announce broadcast")?; + *pending = None; + Ok(true) +} + +/// A [`Republish`] [finished](Republish::finish): every track of its last instance has ended, +/// and they stay servable while it is held. +#[must_use = "dropping it releases the tracks and withdraws the broadcast"] +pub struct Finished { + import: super::Finished, + _catalog: catalog::Producer, + route: moq_net::origin::Route, +} + +impl Finished { + /// The finished broadcast, such as for a [`Linger`](crate::Linger). + pub fn broadcast(&self) -> &moq_net::broadcast::Producer { + self.route.broadcast() + } + + /// What every elementary stream delivered; see [`Import::stats`]. + pub fn stats(&self) -> stats::Snapshot { + self.import.stats() + } + + /// Release every track's cache, reporting `err` to readers that have not drained it. + pub fn abort(self, err: moq_net::Error) { + self.import.abort(err); + } +} + +#[cfg(test)] +mod tests { + use super::super::step_test::{SWITCH, shift}; + use super::*; + + /// 5 s of H.264 and AAC at a constant 400 kb/s, so the first instance measures a rate. + const CLIP: &[u8] = include_bytes!("test_data/bbb5s_cbr.ts"); + + /// Whether `stats` counts a delivered unit. + fn delivered(stats: &stats::Snapshot) -> bool { + stats.streams.values().any(|stream| stream.units > 0) + } + + /// A switch to an encoder whose clock started hours earlier comes back from `decode` as a + /// value. The instance before it finishes, and the next opens under a new instance, its + /// catalog carrying the measured mux rate, hidden until that catalog goes out. However the + /// input is split, the next instance reads it from the step on. + #[test] + fn a_switch_republishes_under_a_new_instance_with_the_rate() { + let open = || { + let origin = crate::source::produce_origin(); + Republish::new(origin, moq_net::Path::from_static("demo"), catalog::Config::default()).unwrap() + }; + let mut measured = open(); + assert!(measured.decode(CLIP).unwrap().is_none()); + let rate = measured.catalog().snapshot().ext.mpegts.mux_rate; + assert!(rate.is_some(), "the clip measures a rate"); + + let mut data = CLIP.to_vec(); + data.extend(shift(CLIP, |_| -SWITCH)); + for chunk in [data.len(), 188 * 7, 1_000] { + let mut republish = open(); + let first = republish.instance(); + let mut republished = Vec::new(); + for piece in data.chunks(chunk) { + let mut next = republish.decode(piece).unwrap(); + while let Some(step) = next { + assert!( + !republish.instance.route.is_announced(), + "{chunk}: announced before its catalog" + ); + assert_eq!( + republish.catalog().snapshot().ext.mpegts.mux_rate, + rate, + "{chunk}: the next catalog carries the measured rate" + ); + republished.push(step); + next = republish.decode(&[]).unwrap(); + } + } + let [step] = &republished[..] else { + panic!("{chunk}: one switch: {republished:?}"); + }; + assert!( + matches!(step.step, Step::Rewind { flagged: false, .. }), + "{chunk}: {step:?}" + ); + assert!(delivered(&step.stats), "{chunk}: the first instance's counts"); + assert_ne!(republish.instance(), first, "{chunk}: a new instance"); + assert!( + republish.instance.route.is_announced(), + "{chunk}: the next instance announced with its catalog" + ); + let finished = republish.finish().unwrap(); + assert!( + delivered(&finished.stats()), + "{chunk}: the next instance read the input after the step" + ); + } + } + + /// A source that steps within the mux rate's measurement window, before its first catalog went + /// out, republishes rather than failing: only the last instance has to declare a track. + #[test] + fn a_switch_before_the_first_catalog_republishes() { + // 1.5 s: short of the two-second window that holds the first catalog. + let mut data = CLIP[..400 * 188].to_vec(); + data.extend(shift(CLIP, |_| -SWITCH)); + let origin = crate::source::produce_origin(); + let mut republish = + Republish::new(origin, moq_net::Path::from_static("demo"), catalog::Config::default()).unwrap(); + let first = republish.instance.route.clone(); + let step = republish.decode(&data).unwrap().expect("a switch"); + assert!(matches!(step.step, Step::Rewind { .. }), "{step:?}"); + assert!(!first.is_announced(), "the first instance never went out"); + assert!(republish.decode(&[]).unwrap().is_none()); + let finished = republish.finish().unwrap(); + assert!(delivered(&finished.stats())); + } + + /// Input that ends before declaring a track fails, and nothing was announced. + #[test] + fn an_input_with_no_track_fails_unannounced() { + let origin = crate::source::produce_origin(); + let republish = Republish::new(origin, moq_net::Path::from_static("demo"), catalog::Config::default()).unwrap(); + let route = republish.instance.route.clone(); + let err = republish.finish().err().expect("nothing was declared"); + assert!(err.to_string().contains("declared"), "{err:#}"); + assert!(!route.is_announced()); + } +} diff --git a/rs/moq-mux/src/container/ts/step.rs b/rs/moq-mux/src/container/ts/step.rs index aabbc7bcdf5..8fa7ef6466a 100644 --- a/rs/moq-mux/src/container/ts/step.rs +++ b/rs/moq-mux/src/container/ts/step.rs @@ -39,8 +39,8 @@ const PTS_WRAP: u64 = 1 << 33; /// The source's clock stepped onto another timeline, or its PAT named another program, so the /// [`Import`](super::Import) ended its instance there. /// -/// What follows is another broadcast. A caller that carries on finishes this import and -/// publishes [`remainder`](Self::remainder) into a new one, as a new instance under a new instance. +/// What follows is another broadcast. [`Republish`](super::Republish) carries on into a new +/// instance on the same path. #[derive(Clone, Debug, thiserror::Error)] #[non_exhaustive] #[error("the source {step}; what follows is a new broadcast")] diff --git a/rs/moq-mux/src/container/ts/step_test.rs b/rs/moq-mux/src/container/ts/step_test.rs index 99eccca1324..800c78d48fe 100644 --- a/rs/moq-mux/src/container/ts/step_test.rs +++ b/rs/moq-mux/src/container/ts/step_test.rs @@ -14,11 +14,11 @@ const CLIP: &[u8] = include_bytes!("test_data/scte35/bbb5s.ts"); const HZ: i64 = 90_000; const WRAP: i64 = 1 << 33; /// The step the issue's contribution took: two encoders started about 4 h 40 m apart. -const SWITCH: i64 = 16_844 * HZ + HZ / 2; +pub(super) const SWITCH: i64 = 16_844 * HZ + HZ / 2; /// Which clock a timestamp belongs to. #[derive(Clone, Copy, PartialEq, Eq)] -enum Clock { +pub(super) enum Clock { Pcr, Pes(u16), } @@ -76,7 +76,7 @@ fn write_timestamp(field: &mut [u8], t: i64) { } /// `ts` with each PCR and PES timestamp moved by `by(clock)` 90 kHz ticks, modulo the wrap. -fn shift(ts: &[u8], by: impl Fn(Clock) -> i64) -> Vec { +pub(super) fn shift(ts: &[u8], by: impl Fn(Clock) -> i64) -> Vec { let mut out = ts.to_vec(); for pkt in out.as_chunks_mut::<188>().0 { let pid = pid(pkt); diff --git a/rs/moq-srt/src/ts.rs b/rs/moq-srt/src/ts.rs index 2bd24354791..7a3014f7aba 100644 --- a/rs/moq-srt/src/ts.rs +++ b/rs/moq-srt/src/ts.rs @@ -34,13 +34,14 @@ pub enum Program { /// Each chunk is handed straight to the TS importer, which consumes whole /// transport packets and retains any partial trailing packet internally for the /// next call (the same pattern `moq-cli import ... stdin ts` uses against stdin). +/// Each broadcast is announced once its catalog goes out. /// [`Self::finish`] keeps the finished broadcasts up for a linger; dropping the /// publisher ends them and unannounces their paths. /// -/// A step of the source's clock onto another timeline ([`ts::Restart`]), such as a -/// changeover between two encoders whose clocks were never locked, finishes the broadcast -/// and republishes the rest of the input on the same path under a new instance, on the same -/// connection. [`Program::All`] still ends the ingest there. +/// A change of source ([`ts::Republished`]), such as a changeover between two encoders whose +/// clocks were never locked, finishes the broadcast and republishes the rest of the input on +/// the same path under a new instance, on the same connection. [`Program::All`] still ends the +/// ingest there. pub struct Publisher { importer: Importer, // The importer's per-stream counters, logged as `moq import ts` logs them, under a @@ -51,90 +52,21 @@ pub struct Publisher { } enum Importer { - One(Box), + One(Box), /// Each program's broadcast is created once the first PAT names it, and announced once its PMT /// declares its tracks. All(Box), } -/// One program's broadcast, and what the next instance publishes with when the source's -/// clock steps. -struct One { - instance: Instance, - origin: origin::Producer, - path: String, - config: moq_mux::catalog::Config, - program: Option, -} - -/// One program's broadcast, under one instance. -struct Instance { - // TS carries undecoded elementary streams (SCTE-35, teletext, DVB AC-3, ...) - // verbatim, so the importer uses the `mpegts` catalog extension rather than the - // media-only `()`, which would route those PIDs to `Stream::Ignored` and drop them. - import: ts::Import, - // Finished at a clean end, so the finished broadcast keeps serving the catalog. - catalog: moq_mux::catalog::Producer, - // Keeps the path announced while the importer fills it, through the linger after a clean end. - route: moq_net::origin::Route, -} - -impl Instance { - /// Publish `path` under a new instance, importing `program` of the input, or its only one. - /// - /// Each instance is its own publisher, so an encoder reconnecting under the same stream id, - /// or a source whose clock stepped, replaces the stale broadcast instead of resuming into it. - fn publish( - origin: &origin::Producer, - path: &str, - config: moq_mux::catalog::Config, - program: Option, - ) -> Result<(Self, moq_net::Instance)> { - let route = origin.route(path, moq_net::origin::RouteOptions::default())?; - let id = route.instance(); - let config = config.with_catalog(moq_mux::catalog::hang::Catalog::::default()); - let catalog = moq_mux::catalog::Producer::new(&mut route.broadcast().clone(), config)?; - let mut import = ts::Import::new(route.broadcast().clone(), catalog.reserve()); - if let Some(program) = program { - import = import.with_program(program); - } - let instance = Self { import, catalog, route }; - Ok((instance, id)) - } - - /// Flush the buffered media and finish every track, the catalog included. - fn finish(self) -> Result { - let Self { - import, - mut catalog, - route, - } = self; - let import = import.finish().map_err(moq_mux::Error::from)?; - catalog.finish()?; - Ok(Finished { - import, - _catalog: catalog, - route, - }) - } -} - -/// One program's broadcast once its tracks finished, served while held. -struct Finished { - import: ts::Finished, - _catalog: moq_mux::catalog::Producer, - route: moq_net::origin::Route, -} - /// What a finished [`Publisher`] holds to keep serving its broadcasts. enum Ended { - One { _finished: Box }, + One { _finished: Box }, All { _finished: Box }, } impl Publisher { - /// Wire up the TS importer and catalog for `path` on `origin`, announcing the broadcast - /// now, or each program's once its PMT declares its tracks for [`Program::All`]. + /// Wire up the TS importer and catalog for `path` on `origin`, announcing the broadcast once + /// its catalog goes out, or each program's for [`Program::All`]. /// /// `config` is the catalog the importer publishes into: max age /// (`with_max_age`) and the connection allocator passthrough tracks claim on @@ -145,30 +77,23 @@ impl Publisher { config: moq_mux::catalog::Config, program: Option, ) -> Result { + let name = moq_net::Path::new(path).map_err(moq_net::Error::from)?; let importer = match program { // `ts::Programs` mints an instance per program. Some(Program::All) => { tracing::info!(%path, ?program, "publishing ingest broadcasts"); - Importer::All(Box::new(ts::Programs::new( - origin.clone(), - moq_net::Path::new(path).map_err(moq_net::Error::from)?, - config, - ))) + Importer::All(Box::new(ts::Programs::new(origin.clone(), name, config))) } Some(Program::One(_)) | None => { - let program = match program { - Some(Program::One(program)) => Some(program), - _ => None, - }; - let (instance, id) = Instance::publish(origin, path, config.clone(), program)?; - tracing::info!(%path, ?program, instance = %id, "publishing ingest broadcast"); - Importer::One(Box::new(One { - instance, - origin: origin.clone(), - path: path.to_string(), - config, - program, - })) + // Each instance is its own publisher, so an encoder reconnecting under the same + // stream id, or a source whose clock stepped, replaces the stale broadcast instead + // of resuming into it. + let mut republish = ts::Republish::new(origin.clone(), name, config).map_err(moq_mux::Error::from)?; + if let Some(Program::One(program)) = program { + republish = republish.with_program(program); + } + tracing::info!(%path, ?program, instance = %republish.instance(), "publishing ingest broadcast"); + Importer::One(Box::new(republish)) } }; @@ -188,27 +113,16 @@ impl Publisher { /// per-stream counters. pub fn feed(&mut self, data: Bytes) -> Result<()> { match &mut self.importer { - Importer::One(one) => { - let One { - instance, - origin, - path, - config, - program, - } = &mut **one; - let mut data = data; - while let Err(err) = instance.import.decode(&data) { - let restart = err.downcast::().map_err(moq_mux::Error::from)?; - // The rest of the input republishes the path under a new instance, and the - // instance before the step finishes. - let (next, id) = Instance::publish(origin, path, config.clone(), *program)?; - let previous = std::mem::replace(instance, next).finish()?; - self.span.in_scope(|| self.log.finish(&previous.import.stats())); - tracing::warn!(%path, step = %restart.step, instance = %id, "the source's clock stepped; republishing"); - // The finished broadcast ends with its route. - drop(previous); + Importer::One(republish) => { + let mut data = &data[..]; + while let Some(republished) = republish.decode(data).map_err(moq_mux::Error::from)? { + let instance = republish.instance(); + self.span.in_scope(|| { + self.log.finish(&republished.stats); + tracing::warn!(step = %republished.step, %instance, "the source changed; republishing"); + }); self.log = ts::stats::Log::default(); - data = restart.remainder; + data = &[]; } } Importer::All(programs) => programs.decode(&data).map_err(moq_mux::Error::from)?, @@ -223,7 +137,7 @@ impl Publisher { fn stats(&self) -> ts::stats::Snapshot { match &self.importer { - Importer::One(one) => one.instance.import.stats(), + Importer::One(republish) => republish.stats(), Importer::All(programs) => programs.stats(), } } @@ -235,10 +149,10 @@ impl Publisher { importer, log, span, .. } = self; let (ended, broadcasts, stats) = match importer { - Importer::One(one) => { - let finished = one.instance.finish()?; - let broadcasts = vec![finished.route.broadcast().clone()]; - let stats = finished.import.stats(); + Importer::One(republish) => { + let finished = republish.finish().map_err(moq_mux::Error::from)?; + let broadcasts = vec![finished.broadcast().clone()]; + let stats = finished.stats(); ( Ended::One { _finished: Box::new(finished), @@ -270,11 +184,7 @@ impl Publisher { /// Consumes the publisher and closes the broadcasts. pub fn abort(self, err: moq_net::Error) { match self.importer { - Importer::One(one) => { - let Instance { import, route, .. } = one.instance; - import.abort(err); - route.broadcast().close(); - } + Importer::One(republish) => republish.abort(err), Importer::All(programs) => programs.abort(err), } } @@ -700,14 +610,15 @@ mod tests { } /// A caller reconnecting under the same stream id while its stale connection is - /// still open restarts the broadcast at once: each connection is its own instance, so - /// a fresh request reaches the reconnect, while the stale viewer stays on the old - /// one until it leaves. + /// still open restarts the broadcast once the reconnect's catalog is out: each connection + /// is its own instance, so a fresh request reaches the reconnect, while the stale viewer + /// stays on the old one until it leaves. #[tokio::test(start_paused = true)] async fn a_reconnect_replaces_the_stale_connection() { let origin = produce_origin(); let consumer = origin.consume(); - let _stale = Publisher::new(&origin, "ingest", Default::default(), None).unwrap(); + let mut stale_publisher = Publisher::new(&origin, "ingest", Default::default(), None).unwrap(); + stale_publisher.feed(Bytes::from_static(BBB5S)).unwrap(); let stale = consumer.request("ingest", None).await.unwrap(); let mut catalog = stale .track(hang::Catalog::DEFAULT_NAME) @@ -716,7 +627,10 @@ mod tests { .await .unwrap(); - let _fresh = Publisher::new(&origin, "ingest", Default::default(), None).unwrap(); + let mut fresh = Publisher::new(&origin, "ingest", Default::default(), None).unwrap(); + // Hidden until its catalog goes out, so a request still reaches the stale connection. + assert!(consumer.request("ingest", None).await.unwrap().is_clone(&stale)); + fresh.feed(Bytes::from_static(BBB5S)).unwrap(); let stale_viewer = kio::clock::timeout(Duration::from_secs(1), async { loop { match catalog.recv_group().await { @@ -802,9 +716,7 @@ mod tests { ) .unwrap(); - let mut ts = psi_packet(0x0000, &pat(&[(1, 0x0100)])); - ts.extend_from_slice(&psi_packet(0x0100, &pmt(1, 0x1b, 0x0101))); - publisher.feed(Bytes::from(ts)).unwrap(); + publisher.feed(Bytes::from_static(BBB5S)).unwrap(); let consumer = origin.consume(); let broadcast = consumer @@ -814,7 +726,15 @@ mod tests { .await .unwrap() .unwrap(); - let info = broadcast.track("0.avc3").unwrap().info().await.unwrap(); + let catalog = hang::catalog::Catalog::::subscribe(&broadcast) + .await + .unwrap() + .next() + .await + .unwrap() + .expect("a catalog"); + let (name, _) = catalog.video.renditions.iter().next().expect("a video rendition"); + let info = broadcast.track(name.as_str()).unwrap().info().await.unwrap(); assert_eq!(info.max_age, Some(Duration::from_secs(3))); } @@ -882,6 +802,8 @@ mod tests { async fn publisher_preserves_scte35_cues() { let origin = produce_origin(); let mut publisher = Publisher::new(&origin, "ingest", Default::default(), None).unwrap(); + // Announced once its catalog goes out. + publisher.feed(bytes::Bytes::from_static(BBB5S)).unwrap(); let consumer = origin.consume(); let broadcast = consumer @@ -892,8 +814,6 @@ mod tests { .unwrap() .unwrap(); - publisher.feed(bytes::Bytes::from_static(BBB5S)).unwrap(); - let mut catalog = moq_mux::catalog::Consumer::::new(&broadcast, CatalogFormat::Hang) .await .unwrap(); From 95845c5b6f3ac6f469a1669839e3059724dd84f4 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 15 Nov 2026 12:02:38 -0800 Subject: [PATCH 3/3] fix(mux): Republish takes its first instance, aborts on a failed finish - Republish::new takes the first moq_net::Instance; with_instance, which opened and discarded a whole instance, is gone. - Program::announce_if_listed reuses republish::listed. - Republish::finish aborts the current instance with the cause when the held input fails to decode, rather than letting Import's drop end its tracks cleanly. - doc/bin/srt.md: --program all ends the ingest at a step. - Restore resolved's doc comment. Co-Authored-By: Claude Opus 5.5 --- doc/bin/srt.md | 2 +- rs/moq-cli/src/publish.rs | 5 +- rs/moq-mux/src/container/ts/programs.rs | 15 +-- rs/moq-mux/src/container/ts/republish.rs | 115 ++++++++++++++++++----- rs/moq-srt/src/ts.rs | 3 +- 5 files changed, 100 insertions(+), 40 deletions(-) diff --git a/doc/bin/srt.md b/doc/bin/srt.md index 3cf1d8ead57..1e75aa8b79f 100644 --- a/doc/bin/srt.md +++ b/doc/bin/srt.md @@ -30,7 +30,7 @@ while its stale connection is still open replaces it once the new feed has declared its tracks: viewers see the broadcast restart, and their next subscribe reaches the new feed. A source that steps onto another timeline republishes on the same connection, as [`import ts`](/bin/ts#programs-and-changes) -does. An +does; under `--program all` it ends the ingest instead. An `export srt` stream ends with its broadcast; `--follow` and `--stitch` follow a return or a replacement on the same connection, as they do for [`export ts`](/bin/cli#retention-and-latency). diff --git a/rs/moq-cli/src/publish.rs b/rs/moq-cli/src/publish.rs index 25cf84a62a9..443187683a1 100644 --- a/rs/moq-cli/src/publish.rs +++ b/rs/moq-cli/src/publish.rs @@ -487,8 +487,7 @@ impl Publish { config: moq_mux::catalog::Config, ) -> anyhow::Result { if let PublishFormat::Ts { program } = *format { - let mut republish = - ts::Republish::new(origin.clone(), moq_net::Path::new(name)?, config)?.with_instance(instance)?; + let mut republish = ts::Republish::new(origin.clone(), moq_net::Path::new(name)?, instance, config)?; if let Some(program) = program { republish = republish.with_program(program); } @@ -1467,7 +1466,6 @@ pub(crate) mod tests { .await } - /// A source for the broadcast at `path`, resolved through `origin`. /// A stdin TS import of `demo` on an origin of its own, and a reader of its first instance's /// broadcast. fn ts_stdin() -> (Publish, moq_net::broadcast::Consumer) { @@ -1497,6 +1495,7 @@ pub(crate) mod tests { (publish, consumer) } + /// A source for the broadcast at `path`, resolved through `origin`. async fn resolved(origin: &moq_net::origin::Producer, path: &str) -> moq_mux::Source { let consumer = origin.consume(); let broadcast = consumer.request(path, None).await.unwrap(); diff --git a/rs/moq-mux/src/container/ts/programs.rs b/rs/moq-mux/src/container/ts/programs.rs index a3df17d2172..bee3ca60e16 100644 --- a/rs/moq-mux/src/container/ts/programs.rs +++ b/rs/moq-mux/src/container/ts/programs.rs @@ -146,20 +146,7 @@ struct Program { impl Program { /// Announce the broadcast once its catalog has gone out. fn announce_if_listed(&mut self) -> anyhow::Result { - let Some(listed) = &mut self.pending else { - return Ok(true); - }; - if !matches!( - listed.poll_largest(&kio::Waiter::noop()), - std::task::Poll::Ready(Some(_)) - ) { - return Ok(false); - } - self.route - .announce() - .with_context(|| format!("failed to announce program {}", self.number))?; - self.pending = None; - Ok(true) + super::republish::listed(&mut self.pending, &self.route).with_context(|| format!("program {}", self.number)) } } diff --git a/rs/moq-mux/src/container/ts/republish.rs b/rs/moq-mux/src/container/ts/republish.rs index 47c52b4d1fc..49b1a093b1d 100644 --- a/rs/moq-mux/src/container/ts/republish.rs +++ b/rs/moq-mux/src/container/ts/republish.rs @@ -129,11 +129,16 @@ impl Instance { } impl Republish { - /// Import the multiplex onto `name`, its catalogs built from `config`, under a fresh - /// instance created now, so its [`catalog`](Self::catalog) can be filled before any input. + /// Import the multiplex onto `name`, its catalogs built from `config`, publishing the first + /// instance as `instance` and each after it under a fresh one. The first is created now, so + /// its [`catalog`](Self::catalog) can be filled before any input. + /// + /// Pass [`Instance::random`](moq_net::Instance::random) unless the instance is shared, such as + /// across a run's broadcasts; see [`RouteOptions::with_instance`](moq_net::origin::RouteOptions::with_instance). pub fn new( origin: moq_net::origin::Producer, name: impl Into, + instance: moq_net::Instance, config: catalog::Config, ) -> anyhow::Result { let target = Target { @@ -142,7 +147,8 @@ impl Republish { config, program: None, }; - let instance = target.open(moq_net::origin::RouteOptions::hidden(), None)?; + let options = moq_net::origin::RouteOptions::hidden().with_instance(instance, 0); + let instance = target.open(options, None)?; Ok(Self { target, instance, @@ -157,16 +163,6 @@ impl Republish { self } - /// Publish the first instance as `instance` rather than a fresh one, such as one a run shares - /// across its broadcasts; see [`RouteOptions::with_instance`](moq_net::origin::RouteOptions::with_instance). - /// - /// Call it before filling the [`catalog`](Self::catalog): it replaces the first instance. - pub fn with_instance(mut self, instance: moq_net::Instance) -> anyhow::Result { - let options = moq_net::origin::RouteOptions::hidden().with_instance(instance, 0); - self.instance = self.target.open(options, None)?; - Ok(self) - } - /// The current instance's broadcast, such as for a track of the caller's own. pub fn broadcast(&self) -> &moq_net::broadcast::Producer { self.instance.route.broadcast() @@ -235,10 +231,18 @@ impl Republish { /// it. Fails when the input ended before the instance declared a track, so nothing was /// announced. A finish that fails aborts every track with the cause. pub fn finish(mut self) -> anyhow::Result { - while let Some(republished) = self.decode(&[])? { - tracing::warn!(step = %republished.step, "the source changed at end of input; republishing"); + loop { + match self.decode(&[]) { + Ok(Some(republished)) => { + tracing::warn!(step = %republished.step, "the source changed at end of input; republishing") + } + Ok(None) => return self.instance.finish(true), + Err(err) => { + self.abort(moq_net::Error::Transport(format!("{err:#}"))); + return Err(err); + } + } } - self.instance.finish(true) } /// Abort the current instance's catalog and media tracks with `err`. Consumes the importer. @@ -250,7 +254,10 @@ impl Republish { } /// Announce `route` once the catalog track `pending` has a group, returning whether it has. -fn listed(pending: &mut Option, route: &moq_net::origin::Route) -> anyhow::Result { +pub(super) fn listed( + pending: &mut Option, + route: &moq_net::origin::Route, +) -> anyhow::Result { let Some(listed) = pending else { return Ok(true); }; @@ -312,7 +319,13 @@ mod tests { fn a_switch_republishes_under_a_new_instance_with_the_rate() { let open = || { let origin = crate::source::produce_origin(); - Republish::new(origin, moq_net::Path::from_static("demo"), catalog::Config::default()).unwrap() + Republish::new( + origin, + moq_net::Path::from_static("demo"), + moq_net::Instance::random(), + catalog::Config::default(), + ) + .unwrap() }; let mut measured = open(); assert!(measured.decode(CLIP).unwrap().is_none()); @@ -370,8 +383,13 @@ mod tests { let mut data = CLIP[..400 * 188].to_vec(); data.extend(shift(CLIP, |_| -SWITCH)); let origin = crate::source::produce_origin(); - let mut republish = - Republish::new(origin, moq_net::Path::from_static("demo"), catalog::Config::default()).unwrap(); + let mut republish = Republish::new( + origin, + moq_net::Path::from_static("demo"), + moq_net::Instance::random(), + catalog::Config::default(), + ) + .unwrap(); let first = republish.instance.route.clone(); let step = republish.decode(&data).unwrap().expect("a switch"); assert!(matches!(step.step, Step::Rewind { .. }), "{step:?}"); @@ -381,11 +399,66 @@ mod tests { assert!(delivered(&finished.stats())); } + /// Held input that fails to decode at the finish aborts the instance reading it, so its + /// readers get the cause rather than a clean end. + #[tokio::test(start_paused = true)] + async fn a_failure_in_held_input_aborts_the_instance() { + let mut data = CLIP.to_vec(); + data.extend(shift(CLIP, |_| -SWITCH)); + // A second program appearing mid-stream ends an import that picked none. + data.extend( + super::super::fixture::Mux::default() + .pat(&[(1, 0x100), (2, 0x200)]) + .bytes(), + ); + let origin = crate::source::produce_origin(); + let mut republish = Republish::new( + origin, + moq_net::Path::from_static("demo"), + moq_net::Instance::random(), + catalog::Config::default(), + ) + .unwrap(); + republish.decode(&data).unwrap().expect("a switch"); + // The next instance's media tracks only exist within the finish, so read its catalog, which + // is aborted with them. A held copy keeps a mere drop from ending it. + let _copy = republish.catalog().clone(); + let mut catalog = republish + .broadcast() + .consume() + .track(hang::Catalog::DEFAULT_NAME) + .unwrap() + .subscribe(None) + .await + .unwrap(); + let err = republish.finish().err().expect("two programs"); + assert!( + err.downcast_ref::().is_some(), + "{err:#}" + ); + let ended = kio::clock::timeout(std::time::Duration::from_secs(1), async { + loop { + if let Err(err) = catalog.recv_group().await.transpose()? { + return Some(err); + } + } + }) + .await + .expect("the catalog ends"); + assert!(ended.is_some(), "the catalog ends with the cause, not cleanly"); + } + /// Input that ends before declaring a track fails, and nothing was announced. #[test] fn an_input_with_no_track_fails_unannounced() { let origin = crate::source::produce_origin(); - let republish = Republish::new(origin, moq_net::Path::from_static("demo"), catalog::Config::default()).unwrap(); + let republish = Republish::new( + origin, + moq_net::Path::from_static("demo"), + moq_net::Instance::random(), + catalog::Config::default(), + ) + .unwrap(); let route = republish.instance.route.clone(); let err = republish.finish().err().expect("nothing was declared"); assert!(err.to_string().contains("declared"), "{err:#}"); diff --git a/rs/moq-srt/src/ts.rs b/rs/moq-srt/src/ts.rs index 7a3014f7aba..b8492fe0a5e 100644 --- a/rs/moq-srt/src/ts.rs +++ b/rs/moq-srt/src/ts.rs @@ -88,7 +88,8 @@ impl Publisher { // Each instance is its own publisher, so an encoder reconnecting under the same // stream id, or a source whose clock stepped, replaces the stale broadcast instead // of resuming into it. - let mut republish = ts::Republish::new(origin.clone(), name, config).map_err(moq_mux::Error::from)?; + let mut republish = ts::Republish::new(origin.clone(), name, moq_net::Instance::random(), config) + .map_err(moq_mux::Error::from)?; if let Some(Program::One(program)) = program { republish = republish.with_program(program); }