From a029d13c6d91af24a4f35ce7a2bf6ad6a2740926 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 18 Nov 2026 14:04:36 -0800 Subject: [PATCH 1/7] chore(quest): claim quest/m1/precut-fixes/transcode-history-key.md Quest-Claim: 6ee838069cc6fa97 From 5eb9c797cfc951712d198171220b57abce8dd528 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 18 Nov 2026 14:22:00 -0800 Subject: [PATCH 2/7] fix(transcode): a cold fetch finds a finer source's group by time A source keyed finer than microseconds (a 1 GHz CMAF track) maps a run of its starts to each output start. The fetch path inverted that to the run's lowest start and fetched it by key, so a group starting anywhere else in the run read as missing and the output group was dropped, though live serves it. A run is now looked up by time: the source is asked for the one group holding the run's last start, which mirrors the output group when it starts inside the run. Co-Authored-By: Claude Opus 5.5 --- .../m1/precut-fixes/transcode-history-key.md | 9 - rs/moq-transcode/src/lib.rs | 85 ++++++++- rs/moq-transcode/src/rung.rs | 176 ++++++++++++++++-- 3 files changed, 245 insertions(+), 25 deletions(-) delete mode 100644 quest/m1/precut-fixes/transcode-history-key.md diff --git a/quest/m1/precut-fixes/transcode-history-key.md b/quest/m1/precut-fixes/transcode-history-key.md deleted file mode 100644 index 2ace6acc3b..0000000000 --- a/quest/m1/precut-fixes/transcode-history-key.md +++ /dev/null @@ -1,9 +0,0 @@ -# [M] Fetch the same transcoded group live and from history - -## Goal - -Historical fills find the source group that produced the requested output group across supported source timescales. - -## Plan - -In the live mapping from [moq-dev/moq#8638](https://github.com/moq-dev/moq/pull/8638), source key 1,000,001 on a 1 GHz CMAF track becomes output key 1,000 in microseconds. The inverse used by the historical exact fetch names source key 1,000,000, so a cache miss can report a real group dropped. The audit verified the arithmetic and traced the lookup. Pin the failure through a cold fill, retain or resolve the correct source identity, and check live and historical output for the same key. diff --git a/rs/moq-transcode/src/lib.rs b/rs/moq-transcode/src/lib.rs index 370fc6d675..137acde3e6 100644 --- a/rs/moq-transcode/src/lib.rs +++ b/rs/moq-transcode/src/lib.rs @@ -394,6 +394,18 @@ mod tests { /// Build a 320x240 avc3 source broadcast: a catalog plus a video track with /// `groups` groups of `frames` gray frames each, encoded with openh264. fn source_broadcast(groups: u64, frames: u64) -> Source { + let info = hang::container::track_info(hang::catalog::PRIORITY.video); + source_broadcast_keyed(groups, frames, info, |sequence, _| sequence) + } + + /// [`source_broadcast`] on a track described by `info`, each group keyed by `key` from its + /// sequence and its first frame's microseconds. + fn source_broadcast_keyed( + groups: u64, + frames: u64, + info: moq_net::track::Info, + key: impl Fn(u64, u64) -> u64, + ) -> Source { let mut broadcast = moq_net::broadcast::Info::default().produce(); let mut catalog = moq_mux::catalog::Producer::new(&mut broadcast, moq_mux::catalog::Config::default()).unwrap(); @@ -409,7 +421,6 @@ mod tests { video.framerate = Some(30.0); catalog.modify().unwrap().video.insert("video", video).unwrap(); - let info = hang::container::track_info(hang::catalog::PRIORITY.video); let track = broadcast.create_track("video", info).unwrap(); let mut encoder = moq_video::encode::Encoder::new(&{ @@ -421,7 +432,8 @@ mod tests { let gray = vec![0x80u8; 320 * 240 * 4]; for sequence in 0..groups { - let mut group = track.create_group(sequence.into()).unwrap(); + let start = key(sequence, sequence * frames * 33_333); + let mut group = track.create_group(start.into()).unwrap(); for index in 0..frames { let timestamp = (sequence * frames + index) * 33_333; if index == 0 { @@ -2053,6 +2065,75 @@ mod tests { } } + /// A source keyed finer than microseconds maps several of its starts to each output start. + /// A cold fetch of an output group still transcodes the source group live serves there, + /// rather than asking for the lowest start that maps to it and dropping a group that exists. + #[tokio::test] + async fn a_finer_source_fetches_the_group_live_serves() { + // Each group starts a nanosecond past its first frame, which the output's microseconds + // can't name: the inverse of the output start is a start the source never used. + let info = hang::container::track_info(hang::catalog::PRIORITY.video).with_timescale(moq_net::Timescale::NANO); + let source = source_broadcast_keyed(2, 5, info, |_, first| first * 1_000 + 1); + let config = Config { + ladder: Ladder::new([Rung::new(120, moq_net::bandwidth::Rate::from_bps(100_000))]).unwrap(), + encoder: moq_video::encode::Kind::Software, + decoder: moq_video::decode::Kind::Software, + source: None, + ..Default::default() + }; + let instance = || async { + let output = moq_net::broadcast::Info::default().produce(); + let consumer = output.consume(); + let transcoder = tokio::spawn(run(source.broadcast.consume(), output, config.clone())); + let track = loop { + match consumer.track(hang::Catalog::DEFAULT_NAME) { + Ok(track) => break track, + Err(moq_net::Error::NotFound) => tokio::task::yield_now().await, + Err(err) => panic!("catalog track: {err}"), + } + }; + let mut catalogs = moq_mux::catalog::hang::Consumer::<()>::new(track); + await_catalog(&mut catalogs, |snapshot| { + snapshot.video.renditions.contains_key("video/120p") + }) + .await; + (consumer.track("video/120p").unwrap(), transcoder) + }; + let timestamps = |mut group: moq_net::group::Consumer| async move { + let mut timestamps = Vec::new(); + while let Some(payload) = group.read_frame().await.unwrap() { + let frame = hang::container::Frame::decode(payload.payload).unwrap(); + timestamps.push(frame.timestamp.as_micros().unwrap()); + } + (group.start, timestamps) + }; + let expected = |sequence: u64| -> Vec { (0..5).map(|i| (sequence * 5 + i) as u128 * 33_333).collect() }; + + // The live loop serves the latest source group, at its first frame's microsecond. + let (rung, live) = instance().await; + let mut subscriber = rung + .subscribe(moq_net::track::Subscription::default().with_ranges([moq_net::track::Range::live()])) + .await + .unwrap() + .ordered(); + let group = loop { + if let Some(group) = subscriber.next_group().await.unwrap().unwrap().group() { + break group; + } + }; + let served = timestamps(group).await; + assert_eq!(served, (166_665, expected(1))); + live.abort(); + + // A fresh instance has neither group cached, so each fetch fills from the source. + let (rung, cold) = instance().await; + let fetched = timestamps(rung.fetch_group(166_665, None).await.unwrap()).await; + assert_eq!(fetched, served, "a cold fetch serves what live served at the same key"); + let first = timestamps(rung.fetch_group(0, None).await.unwrap()).await; + assert_eq!(first, (0, expected(0))); + cold.abort(); + } + /// A source whose codec description changes rebuilds the shared decode, so /// every rung retires with it. The picture may not have moved at all, so shape /// alone would hand the replacements the names that just ended. They have to be diff --git a/rs/moq-transcode/src/rung.rs b/rs/moq-transcode/src/rung.rs index 85385461bb..9686b8ea68 100644 --- a/rs/moq-transcode/src/rung.rs +++ b/rs/moq-transcode/src/rung.rs @@ -155,14 +155,16 @@ pub(crate) fn output_start(start: u64, scale: Option) -> Opt Some(start.convert(hang::container::TIMESCALE).ok()?.value()) } -/// The start of the source group the output group starting at `start` mirrors, for a source in -/// `scale`: the lowest source start [`output_start`] maps there, or `None` when none does. -/// -/// Exact for a source no finer than microseconds. A finer one maps several starts to each output -/// start, and only the lowest is found. -fn source_start(start: u64, scale: Option) -> Option { - let source = source_from(start, scale)?; - (output_start(source, scale) == Some(start)).then_some(source) +/// The source starts [`output_start`] maps to the output start `start`, for a source in `scale`, +/// or `None` when none does: one start for a source no finer than microseconds, and a run of +/// them for a finer one. +fn source_starts(start: u64, scale: Option) -> Option> { + let low = source_from(start, scale)?; + let high = match start.checked_add(1).and_then(|next| source_from(next, scale)) { + Some(next) => next.checked_sub(1)?, + None => u64::MAX, + }; + (low <= high).then_some(low..=high) } /// The lowest source start that [`output_start`] maps at or after the output start `start`, or @@ -757,22 +759,29 @@ async fn fetch(rung: &Rung, fill: &moq_net::track::FillGroups, sequence: u64, li } }, }; - let Some(start) = source_start(sequence, info.timescale) else { + let Some(starts) = source_starts(sequence, info.timescale) else { // No source start maps here: the group can't exist. return drop_gap(rung, &track, fill, sequence, info.timescale).await; }; - let options = moq_net::group::Fetch::default().with_priority(fill.priority()); + // Only a lookup by time can wait on the source's media, which a retired rung's drain can't. + let placing = starts.start() != starts.end(); + let mut retire = rung.retire.clone(); let mut source = tokio::select! { biased; _ = fill.unwanted(sequence) => return abandon(sequence), - source = track.fetch_group(start, options) => match source { - Ok(source) => source, - Err(moq_net::Error::NotFound) => return drop_gap(rung, &track, fill, sequence, info.timescale).await, + source = source_group(&track, starts, fill.priority()) => match source { + Ok(Some(source)) => source, + Ok(None) => return drop_gap(rung, &track, fill, sequence, info.timescale).await, Err(err) => { refuse(err.clone()); return Err(err.into()); } }, + () = retire.fired(), if placing => { + // Nothing says the source lost it: a later reader asks the rung that replaces this one. + refuse(moq_net::Error::Cancel); + return Ok(()); + } }; let _permit = tokio::select! { @@ -824,10 +833,68 @@ async fn fetch(rung: &Rung, fill: &moq_net::track::FillGroups, sequence: u64, li // live path is idle. Reference counted, so overlapping the live session // bills the rendition once rather than twice. let active = rung.active.attach(&rung.rendition); - transcode_group(pipeline, &container, &mut source, Writing(Some(output)), &active).await?; + transcode_group(pipeline, &container, &mut source.group, Writing(Some(output)), &active).await?; Ok(()) } +/// The source group an output group mirrors, with the subscription that delivers it, if any. +struct Source { + group: moq_net::group::Consumer, + /// Held until the group is read, since it is what feeds the group. + _subscriber: Option, +} + +/// The source group starting at one of `starts`, the source starts an output group maps back to, +/// or `None` when the source has none. +/// +/// A single start is fetched by key. A run of them is looked up by time instead, rather than a +/// fetch per start: the group holding the run's last start is the newest starting in or before +/// it, so it is the one asked for, and the run holds a group only if it starts there. A source +/// with two groups inside one output start gives that start to whichever path writes it first. +async fn source_group( + track: &moq_net::track::Consumer, + starts: std::ops::RangeInclusive, + priority: i8, +) -> moq_net::Result> { + use moq_net::track::{Anchor, Delay, Item, Range, Subscription}; + + if starts.start() == starts.end() { + let options = moq_net::group::Fetch::default().with_priority(priority); + return match track.fetch_group(*starts.start(), options).await { + Ok(group) => Ok(Some(Source { + group, + _subscriber: None, + })), + Err(moq_net::Error::NotFound) => Ok(None), + Err(err) => Err(err), + }; + } + + let last = Anchor::Time(*starts.end()); + let subscription = Subscription::default() + .with_ranges([Range { + start: last, + end: Some(last), + }]) + .with_priority(priority) + .with_delay(Delay::default().with_max(None)); + let mut groups = track.subscribe(subscription).await?.ordered(); + while let Some(item) = groups.next_group().await? { + // A group given up on is no source group. + let Item::Group(group) = item else { + continue; + }; + if !starts.contains(&group.start) { + return Ok(None); + } + return Ok(Some(Source { + group, + _subscriber: Some(groups), + })); + } + Ok(None) +} + /// Drop the run of output groups around `sequence`, which the source doesn't have, up to the /// source groups on either side of it. /// @@ -1382,6 +1449,87 @@ mod tests { drop(video); } + /// A source keyed finer than microseconds maps a run of its starts to each output start. A + /// fill asks the source for the one group holding the run's last start, rather than a fetch + /// per start or the groups below it, and serves the group starting inside the run. + #[tokio::test(start_paused = true)] + async fn a_finer_source_is_asked_for_one_group() { + let fetched = fill_finer(Some(1_000_001), false).await; + assert_eq!(fetched.expect("the source has it").start, 1_000); + } + + /// The group holding the run's last start starts below the run, so the source has no group + /// the output group could mirror. + #[tokio::test(start_paused = true)] + async fn a_finer_source_without_the_group_drops_it() { + assert!( + fill_finer(Some(999_999), false).await.is_err(), + "the source doesn't have it" + ); + } + + /// A retired rung's drain can't wait on the source to place a time, so it refuses the group + /// for the rung that replaces it rather than holding the retirement. + #[tokio::test(start_paused = true)] + async fn a_retired_rung_refuses_a_finer_lookup_it_cant_place() { + assert!(fill_finer(None, true).await.is_err(), "nothing places the group yet"); + } + + /// Fetch output group 1000 of a rung over a nanosecond source holding only a later live group, + /// answering the source's lookup with a group at `answer`, and return what the fetch got. + async fn fill_finer(answer: Option, retired: bool) -> moq_net::Result { + let (rung, source, retire) = idle_rung(); + let video = source + .create_track( + "video", + hang::container::track_info(hang::catalog::PRIORITY.video).with_timescale(moq_net::Timescale::NANO), + ) + .unwrap(); + let source_fills = video.requests(); + video + .create_group(moq_net::group::Info { start: 5_000_000_001 }) + .unwrap() + .finish() + .unwrap(); + + let output = moq_net::broadcast::Info::default().produce(); + let track = output + .create_track("video/120p", hang::container::track_info(hang::catalog::PRIORITY.video)) + .unwrap(); + let fills = track.requests(); + let consumer = track.consume(); + let fetching = tokio::spawn(async move { consumer.fetch_group(1_000, None).await }); + let fill = fills.requested_fill().await.map(groups).unwrap(); + + if retired { + retire.send(true).unwrap(); + } + let mut tasks = tokio::task::JoinSet::new(); + spawn_fill(&mut tasks, rung, fill, Arc::new(Semaphore::new(1))); + let wait = std::time::Duration::from_secs(5); + if let Some(answer) = answer { + let moq_net::track::Fill::Lookup(lookup) = kio::clock::timeout(wait, source_fills.requested_fill()) + .await + .expect("the source is asked") + .unwrap() + else { + panic!("a time lookup"); + }; + // The last nanosecond microsecond 1000 holds. + assert_eq!(lookup.time(), 1_000_999); + lookup.accept(answer, None).unwrap().finish().unwrap(); + } + // Not timed: the paused clock runs ahead while a codec thread opens the encoder. + let fetched = fetching.await.unwrap(); + tasks.join_next().await.unwrap().unwrap(); + assert!( + kio::clock::timeout(wait, source_fills.requested_fill()).await.is_err(), + "the source is asked nothing else" + ); + drop(video); + fetched + } + /// Fill output starts 1 to an hour, with source groups at 0 and an hour, or, when `empty`, /// none until the fill has waited on the source. async fn fill_across(order: moq_net::track::Order, retired: bool, empty: bool) { From 220230ce5812beca3ca24a416f74b9e0b33eaab2 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 18 Nov 2026 14:32:33 -0800 Subject: [PATCH 3/7] docs(transcode): a finer source's unanswered lookup reads as a miss The fallback is owned by quest/m5/archive-time-followups.md, which now names this consumer. Co-Authored-By: Claude Opus 5.5 --- quest/m5/archive-time-followups.md | 6 ++++++ rs/moq-transcode/src/rung.rs | 2 ++ 2 files changed, 8 insertions(+) diff --git a/quest/m5/archive-time-followups.md b/quest/m5/archive-time-followups.md index 8fa3d43b78..5abede4aa4 100644 --- a/quest/m5/archive-time-followups.md +++ b/quest/m5/archive-time-followups.md @@ -38,6 +38,12 @@ Also, from [moq-dev/moq#7310](https://github.com/moq-dev/moq/pull/7310)'s follow - A time end walks the history one fill round trip per group. Measure a long range against the in-memory store; if DVR seeks need it, look the end up as well. Otherwise record the cost in the `FillLookup` docs. +- moq-transcode finds a cold source group keyed finer than microseconds by a + `Time` lookup (`source_group` in `rs/moq-transcode/src/rung.rs`), and drops + the output group when the answer starts below the run. A silent fallback + therefore drops a group the archive holds, for that rung's lifetime. With + the refusal, the lookup's subscription error refuses the group instead, so + a later reader retries it. Add a transcode test over a failing lookup. Tests: a failing GET and a malformed object each fail the subscription with the typed error; a handler registered after a group started still resolves diff --git a/rs/moq-transcode/src/rung.rs b/rs/moq-transcode/src/rung.rs index 9686b8ea68..e569a6aa47 100644 --- a/rs/moq-transcode/src/rung.rs +++ b/rs/moq-transcode/src/rung.rs @@ -851,6 +851,8 @@ struct Source { /// fetch per start: the group holding the run's last start is the newest starting in or before /// it, so it is the one asked for, and the run holds a group only if it starts there. A source /// with two groups inside one output start gives that start to whichever path writes it first. +/// A lookup its handler can't answer falls back to the groups held, so it reads as a miss too: +/// `FillLookup` has no refusal yet. async fn source_group( track: &moq_net::track::Consumer, starts: std::ops::RangeInclusive, From 2cd2b6a3d4127503b3230ce4b55cd909809f12a3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 18 Nov 2026 15:25:20 -0800 Subject: [PATCH 4/7] fix(transcode): a rung track takes its source's timescale Each rung's output track is accepted in its source track's timescale, so output group keys equal source keys. The cold-fetch key conversion and its per-microsecond time lookup go away. A rung learns the source's timescale before accepting a request; a retired rung or a closed source refuses the request with Cancel instead of waiting, and a dropped serving future still answers it. Co-Authored-By: Claude Opus 5.5 --- quest/m5/archive-time-followups.md | 6 - rs/moq-transcode/src/feed.rs | 13 +- rs/moq-transcode/src/lib.rs | 30 ++- rs/moq-transcode/src/rung.rs | 374 ++++++++++------------------- 4 files changed, 145 insertions(+), 278 deletions(-) diff --git a/quest/m5/archive-time-followups.md b/quest/m5/archive-time-followups.md index 5abede4aa4..8fa3d43b78 100644 --- a/quest/m5/archive-time-followups.md +++ b/quest/m5/archive-time-followups.md @@ -38,12 +38,6 @@ Also, from [moq-dev/moq#7310](https://github.com/moq-dev/moq/pull/7310)'s follow - A time end walks the history one fill round trip per group. Measure a long range against the in-memory store; if DVR seeks need it, look the end up as well. Otherwise record the cost in the `FillLookup` docs. -- moq-transcode finds a cold source group keyed finer than microseconds by a - `Time` lookup (`source_group` in `rs/moq-transcode/src/rung.rs`), and drops - the output group when the answer starts below the run. A silent fallback - therefore drops a group the archive holds, for that rung's lifetime. With - the refusal, the lookup's subscription error refuses the group instead, so - a later reader retries it. Add a transcode test over a failing lookup. Tests: a failing GET and a malformed object each fail the subscription with the typed error; a handler registered after a group started still resolves diff --git a/rs/moq-transcode/src/feed.rs b/rs/moq-transcode/src/feed.rs index 157d8e552d..eecef840a5 100644 --- a/rs/moq-transcode/src/feed.rs +++ b/rs/moq-transcode/src/feed.rs @@ -36,8 +36,8 @@ const CAPACITY: usize = 16; /// One item of the shared live feed, in stream order. #[derive(Clone)] pub(crate) enum Item { - /// A new source group started; the frames that follow belong to it. Carries the start of the - /// output group mirroring it (see [`crate::rung::output_start`]). + /// A new source group started; the frames that follow belong to it. Carries its key, which + /// the output group mirroring it shares. Group(u64), /// A decoded frame of the current group. Cloning is cheap (`Arc`), and a GPU /// frame stays on the GPU for every receiving rung. @@ -203,16 +203,9 @@ async fn decode(inner: &Inner, sender: &broadcast::Sender) -> Result<(), E let mut subscriber = inner.broadcast.track(&inner.name)?.subscribe(subscription).await?; while let Some(mut group) = subscriber.recv_group().await? { - let Some(start) = crate::rung::output_start(group.start, group.timescale()) else { - tracing::debug!( - group = group.start, - "skipping a source group past the output's timescale" - ); - continue; - }; // Sends only fail with zero receivers, which is fine: teardown aborts // this task at the next await anyway. - let _ = sender.send(Item::Group(start)); + let _ = sender.send(Item::Group(group.start)); let mut first = true; let complete = loop { diff --git a/rs/moq-transcode/src/lib.rs b/rs/moq-transcode/src/lib.rs index 137acde3e6..d44d0d4553 100644 --- a/rs/moq-transcode/src/lib.rs +++ b/rs/moq-transcode/src/lib.rs @@ -490,6 +490,8 @@ mod tests { let request = requests.requested_track().await.unwrap(); assert_eq!(request.name(), name); let mut serving = Box::pin(rung::serve(ladder.rung(name).unwrap(), request)); + // The source track is local, so its timescale is known and this poll accepts the track. + assert!(futures::poll!(&mut serving).is_pending()); subscribers.push((name, subscribing.await.unwrap().ordered())); // With demand present and no source group, this poll attaches the rung // to the shared feed before parking. No startup sleep can guarantee that. @@ -2065,13 +2067,12 @@ mod tests { } } - /// A source keyed finer than microseconds maps several of its starts to each output start. - /// A cold fetch of an output group still transcodes the source group live serves there, - /// rather than asking for the lowest start that maps to it and dropping a group that exists. + /// A source keyed finer than microseconds keeps its keys on every rung, so a cold fetch of an + /// output group transcodes the source group live serves there, rather than a group whose key + /// lost the source's precision. #[tokio::test] async fn a_finer_source_fetches_the_group_live_serves() { - // Each group starts a nanosecond past its first frame, which the output's microseconds - // can't name: the inverse of the output start is a start the source never used. + // Each group starts a nanosecond past its first frame, which microseconds can't name. let info = hang::container::track_info(hang::catalog::PRIORITY.video).with_timescale(moq_net::Timescale::NANO); let source = source_broadcast_keyed(2, 5, info, |_, first| first * 1_000 + 1); let config = Config { @@ -2097,7 +2098,14 @@ mod tests { snapshot.video.renditions.contains_key("video/120p") }) .await; - (consumer.track("video/120p").unwrap(), transcoder) + let rung = consumer.track("video/120p").unwrap(); + let info = rung.info().await.unwrap(); + assert_eq!( + info.timescale, + Some(moq_net::Timescale::NANO), + "the rung keeps its source's keys" + ); + (rung, transcoder) }; let timestamps = |mut group: moq_net::group::Consumer| async move { let mut timestamps = Vec::new(); @@ -2109,7 +2117,7 @@ mod tests { }; let expected = |sequence: u64| -> Vec { (0..5).map(|i| (sequence * 5 + i) as u128 * 33_333).collect() }; - // The live loop serves the latest source group, at its first frame's microsecond. + // The live loop serves the latest source group, at its key. let (rung, live) = instance().await; let mut subscriber = rung .subscribe(moq_net::track::Subscription::default().with_ranges([moq_net::track::Range::live()])) @@ -2122,15 +2130,15 @@ mod tests { } }; let served = timestamps(group).await; - assert_eq!(served, (166_665, expected(1))); + assert_eq!(served, (166_665_001, expected(1))); live.abort(); // A fresh instance has neither group cached, so each fetch fills from the source. let (rung, cold) = instance().await; - let fetched = timestamps(rung.fetch_group(166_665, None).await.unwrap()).await; + let fetched = timestamps(rung.fetch_group(166_665_001, None).await.unwrap()).await; assert_eq!(fetched, served, "a cold fetch serves what live served at the same key"); - let first = timestamps(rung.fetch_group(0, None).await.unwrap()).await; - assert_eq!(first, (0, expected(0))); + let first = timestamps(rung.fetch_group(1, None).await.unwrap()).await; + assert_eq!(first, (1, expected(0))); cold.abort(); } diff --git a/rs/moq-transcode/src/rung.rs b/rs/moq-transcode/src/rung.rs index e569a6aa47..53ff86c64a 100644 --- a/rs/moq-transcode/src/rung.rs +++ b/rs/moq-transcode/src/rung.rs @@ -16,10 +16,10 @@ //! valid after the head that same encode produced. A fetch every caller //! leaves before it is accepted is dropped, so a later one starts clean. //! -//! Output groups mirror the source groups 1:1: each starts where its source group -//! does, in the output's microseconds (see [`output_start`]), so a fetch for an -//! output group maps back to its source group and a player switching renditions -//! lands on the same content. +//! Output groups mirror the source groups 1:1: the output track takes the source +//! track's timescale, so each output group has its source group's key. A fetch for +//! an output group fetches that key from the source, and a player switching +//! renditions, the source's included, lands on the same content. use std::collections::VecDeque; use std::future::Future; @@ -143,47 +143,51 @@ impl Rung { } } -/// The start of the output group mirroring a source group that starts at `start` on a track in -/// `scale`: that start in the output's microseconds, where the group's first frame lands since a -/// transcode keeps its timestamps, or `None` past that timescale's range. A scale-less source's -/// starts pass through. -pub(crate) fn output_start(start: u64, scale: Option) -> Option { - let Some(scale) = scale else { - return Some(start); - }; - let start = moq_net::Timestamp::new(start, scale).ok()?; - Some(start.convert(hang::container::TIMESCALE).ok()?.value()) -} +/// A rung track not yet ended, which a cancelled serving future ends with `Cancel` rather than +/// dropping it unanswered or unfinished. `None` once it ended on purpose. +struct Serving(Option); -/// The source starts [`output_start`] maps to the output start `start`, for a source in `scale`, -/// or `None` when none does: one start for a source no finer than microseconds, and a run of -/// them for a finer one. -fn source_starts(start: u64, scale: Option) -> Option> { - let low = source_from(start, scale)?; - let high = match start.checked_add(1).and_then(|next| source_from(next, scale)) { - Some(next) => next.checked_sub(1)?, - None => u64::MAX, - }; - (low <= high).then_some(low..=high) +/// How far a rung track's answer got. +enum Stage { + /// Waiting on the source track's timescale to accept the request in. + Requested(moq_net::track::Request), + /// Accepted, until the track ends. + Accepted(moq_net::track::Producer), } -/// The lowest source start that [`output_start`] maps at or after the output start `start`, or -/// `None` past the source's range. -fn source_from(start: u64, scale: Option) -> Option { - let Some(scale) = scale else { - return Some(start); - }; - let micros = u128::from(hang::container::TIMESCALE.as_u64()); - u64::try_from((u128::from(start) * u128::from(scale.as_u64())).div_ceil(micros)).ok() -} +impl Serving { + /// Accept the request in `info`, returning the track. + fn accept(&mut self, info: moq_net::track::Info) -> moq_net::track::Producer { + let Some(Stage::Requested(request)) = self.0.take() else { + unreachable!("a rung track is accepted once, before anything else ends it"); + }; + let producer = request.accept(info); + self.0 = Some(Stage::Accepted(producer.clone())); + producer + } -/// Keeps a cancelled serving future from dropping an unfinished output track. -struct Serving(Option); + /// Let go of a track that ended on its own, releasing the last clone without aborting it. + fn release(&mut self) { + self.0 = None; + } + + /// Refuse the request with `err`. Only a request still waiting can be. + fn reject(&mut self, err: moq_net::Error) { + let Some(Stage::Requested(request)) = self.0.take() else { + unreachable!("a rung track is refused only before it is accepted"); + }; + request.reject(err); + } +} impl Drop for Serving { fn drop(&mut self) { - if let Some(producer) = self.0.take() { - let _ = producer.abort(moq_net::Error::Cancel); + match self.0.take() { + Some(Stage::Requested(request)) => request.reject(moq_net::Error::Cancel), + Some(Stage::Accepted(producer)) => { + let _ = producer.abort(moq_net::Error::Cancel); + } + None => {} } } } @@ -235,11 +239,20 @@ pub(crate) fn serve(rung: Rung, request: moq_net::track::Request) -> impl Future // Grab the group-request handle before accepting: a Request is dynamic from // birth, so a fetch racing the acceptance queues instead of failing. let dynamic = request.requests(); - let info = hang::container::track_info(hang::catalog::PRIORITY.video); - let mut producer = request.accept(info); - let serving = Serving(Some(producer.clone())); + // Held before the future is polled, so dropping it unpolled still answers the request. + let mut serving = Serving(Some(Stage::Requested(request))); async move { - let mut serving = serving; + let scale = match timescale(&rung).await { + Ok(Some(scale)) => scale, + // `serving` refuses the request with `Cancel` as it drops. + Ok(None) => return Ok(()), + Err(err) => { + serving.reject(err.clone()); + return Err(err.into()); + } + }; + let info = hang::container::track_info(hang::catalog::PRIORITY.video).with_timescale(scale); + let mut producer = serving.accept(info); let (finished, mut finishing) = tokio::sync::watch::channel(false); let live = async { @@ -261,9 +274,8 @@ pub(crate) fn serve(rung: Rung, request: moq_net::track::Request) -> impl Future // to count, so finishing then would reject the very group it was fetching. match (live, fetches) { (Ok(Ended::Finished), Ok(())) => { - // Released with `serving`, the last clone, which then no longer aborts it. drop(producer.finish()?); - serving.0 = None; + serving.release(); Ok(()) } // `serving` aborts the track on drop, so subscribers see an end rather than a stall. @@ -272,6 +284,22 @@ pub(crate) fn serve(rung: Rung, request: moq_net::track::Request) -> impl Future } } +/// The timescale to accept a rung track in: its source track's, so each output group has its +/// source group's key. `None` when the rung retires or its source closes first, since the source +/// may never say: a later request reaches the rung that replaces this one. +/// +/// A scale-less source's keys aren't times, but the frames decoded from it are, so its rungs are +/// in the container's microseconds, and its keys still pass through as they are. +async fn timescale(rung: &Rung) -> moq_net::Result> { + let track = rung.broadcast.track(&rung.source)?; + let mut retire = rung.retire.clone(); + tokio::select! { + info = track.info() => Ok(Some(info?.timescale.unwrap_or(hang::container::TIMESCALE))), + () = retire.fired() => Ok(None), + () = rung.broadcast.closed() => Ok(None), + } +} + /// Wait until some subscriber wants the track, or none does, returning at once if /// that already holds. A fresh clone reports the current value first, so a /// change seen through [`moq_net::track::Demand::wanted`] instead is never missed. @@ -747,41 +775,19 @@ async fn fetch(rung: &Rung, fill: &moq_net::track::FillGroups, sequence: u64, li return Err(err.into()); } }; - // The source's timescale names the source group this output group mirrors. - let info = tokio::select! { - biased; - _ = fill.unwanted(sequence) => return abandon(sequence), - info = track.info() => match info { - Ok(info) => info, - Err(err) => { - refuse(err.clone()); - return Err(err.into()); - } - }, - }; - let Some(starts) = source_starts(sequence, info.timescale) else { - // No source start maps here: the group can't exist. - return drop_gap(rung, &track, fill, sequence, info.timescale).await; - }; - // Only a lookup by time can wait on the source's media, which a retired rung's drain can't. - let placing = starts.start() != starts.end(); - let mut retire = rung.retire.clone(); + // The output group has its source group's key. + let options = moq_net::group::Fetch::default().with_priority(fill.priority()); let mut source = tokio::select! { biased; _ = fill.unwanted(sequence) => return abandon(sequence), - source = source_group(&track, starts, fill.priority()) => match source { - Ok(Some(source)) => source, - Ok(None) => return drop_gap(rung, &track, fill, sequence, info.timescale).await, + source = track.fetch_group(sequence, options) => match source { + Ok(source) => source, + Err(moq_net::Error::NotFound) => return drop_gap(rung, &track, fill, sequence).await, Err(err) => { refuse(err.clone()); return Err(err.into()); } }, - () = retire.fired(), if placing => { - // Nothing says the source lost it: a later reader asks the rung that replaces this one. - refuse(moq_net::Error::Cancel); - return Ok(()); - } }; let _permit = tokio::select! { @@ -833,79 +839,18 @@ async fn fetch(rung: &Rung, fill: &moq_net::track::FillGroups, sequence: u64, li // live path is idle. Reference counted, so overlapping the live session // bills the rendition once rather than twice. let active = rung.active.attach(&rung.rendition); - transcode_group(pipeline, &container, &mut source.group, Writing(Some(output)), &active).await?; + transcode_group(pipeline, &container, &mut source, Writing(Some(output)), &active).await?; Ok(()) } -/// The source group an output group mirrors, with the subscription that delivers it, if any. -struct Source { - group: moq_net::group::Consumer, - /// Held until the group is read, since it is what feeds the group. - _subscriber: Option, -} - -/// The source group starting at one of `starts`, the source starts an output group maps back to, -/// or `None` when the source has none. -/// -/// A single start is fetched by key. A run of them is looked up by time instead, rather than a -/// fetch per start: the group holding the run's last start is the newest starting in or before -/// it, so it is the one asked for, and the run holds a group only if it starts there. A source -/// with two groups inside one output start gives that start to whichever path writes it first. -/// A lookup its handler can't answer falls back to the groups held, so it reads as a miss too: -/// `FillLookup` has no refusal yet. -async fn source_group( - track: &moq_net::track::Consumer, - starts: std::ops::RangeInclusive, - priority: i8, -) -> moq_net::Result> { - use moq_net::track::{Anchor, Delay, Item, Range, Subscription}; - - if starts.start() == starts.end() { - let options = moq_net::group::Fetch::default().with_priority(priority); - return match track.fetch_group(*starts.start(), options).await { - Ok(group) => Ok(Some(Source { - group, - _subscriber: None, - })), - Err(moq_net::Error::NotFound) => Ok(None), - Err(err) => Err(err), - }; - } - - let last = Anchor::Time(*starts.end()); - let subscription = Subscription::default() - .with_ranges([Range { - start: last, - end: Some(last), - }]) - .with_priority(priority) - .with_delay(Delay::default().with_max(None)); - let mut groups = track.subscribe(subscription).await?.ordered(); - while let Some(item) = groups.next_group().await? { - // A group given up on is no source group. - let Item::Group(group) = item else { - continue; - }; - if !starts.contains(&group.start) { - return Ok(None); - } - return Ok(Some(Source { - group, - _subscriber: Some(groups), - })); - } - Ok(None) -} - /// Drop the run of output groups around `sequence`, which the source doesn't have, up to the /// source groups on either side of it. /// -/// Output starts are microseconds, so a source keyed by its media time leaves most of them -/// empty between two of its groups. Fetching each would cost one request per tick of the gap; -/// one bounded read of the source, in key order, finds the groups around the miss instead, and -/// the span between them is dropped at once. Filling in descending order, the span below the -/// miss is still owed, so the read starts at the fill's first group; ascending, the span below -/// was already answered, so it starts at the miss. +/// Keys are media times, so most of them name no group between two of the source's. Fetching +/// each would cost one request per tick of the gap; one bounded read of the source, in key order, +/// finds the groups around the miss instead, and the span between them is dropped at once. +/// Filling in descending order, the span below the miss is still owed, so the read starts at the +/// fill's first group; ascending, the span below was already answered, so it starts at the miss. /// /// The miss itself is dropped first. A source holding no group at all resolves no time until it /// holds one, as it would for any reader of it, so the lookup for the rest then waits for the @@ -917,7 +862,6 @@ async fn drop_gap( track: &moq_net::track::Consumer, fill: &moq_net::track::FillGroups, sequence: u64, - scale: Option, ) -> Result<(), Error> { use moq_net::track::{Anchor, Delay, Order, Range, Subscription}; @@ -937,22 +881,13 @@ async fn drop_gap( let Some(next) = next else { return Ok(()); }; - // The source starts that map into the fill, below the miss and from it. - let (Some(miss), Some(low)) = (source_from(sequence, scale), source_from(first, scale)) else { - fill.drop_groups(rest); - return Ok(()); - }; - let high = source_from(end, scale).map_or(u64::MAX, |high| high.saturating_sub(1)); - if !below && high < miss { - // No source start maps between the miss and the fill's end. - fill.drop_groups(rest); - return Ok(()); - } - let from = if below { low } else { miss }; + // The fill holds the miss, so it has a last key. + let last = end - 1; + let from = if below { first } else { sequence }; let subscription = Subscription::default() .with_ranges([Range { start: Anchor::Time(from), - end: Some(Anchor::Time(high)), + end: Some(Anchor::Time(last)), }]) .with_order(Order::Ascending) .with_priority(fill.priority()) @@ -967,10 +902,10 @@ async fn drop_gap( let moq_net::track::Item::Group(group) = item else { continue; }; - if group.start >= miss { - return Ok::<_, moq_net::Error>((before, output_start(group.start, scale))); + if group.start >= sequence { + return Ok::<_, moq_net::Error>((before, Some(group.start))); } - before = output_start(group.start, scale); + before = Some(group.start); } Ok((before, None)) }; @@ -1321,16 +1256,12 @@ mod tests { async fn a_group_nobody_reads_releases_its_fill() { use std::task::Poll; let (rung, source, _retire) = idle_rung(); - let source_fills = source - .create_track("video", hang::container::track_info(hang::catalog::PRIORITY.video)) - .unwrap() - .requests(); + // A rung's media max age, as `serve` accepts it, so its history is still fresh to fill. + let timed = + hang::container::track_info(hang::catalog::PRIORITY.video).with_timescale(moq_net::Timescale::MILLI); + let source_fills = source.create_track("video", timed.clone()).unwrap().requests(); let output = moq_net::broadcast::Info::default().produce(); - // A rung's media max age, as `serve` accepts it, so its history is still fresh to fill. - let timed = moq_net::track::Info::default() - .with_timescale(moq_net::Timescale::MILLI) - .with_max_age(hang::container::MAX_AGE); let mut track = output.create_track("video/120p", timed).unwrap(); let fills = track.requests(); let ascending = |from| { @@ -1387,7 +1318,7 @@ mod tests { } /// A fill spanning a long stretch without source groups drops it in one go, rather than - /// asking the source for every microsecond in it. + /// asking the source for every key in it. #[tokio::test(start_paused = true)] async fn a_fill_drops_a_gap_between_source_groups_at_once() { fill_across_a_gap(moq_net::track::Order::Ascending, false).await; @@ -1399,7 +1330,7 @@ mod tests { fill_across_a_gap(moq_net::track::Order::Descending, false).await; } - /// A retired rung's drain drops the gap at once too, rather than a microsecond at a time. + /// A retired rung's drain drops the gap at once too, rather than a key at a time. #[tokio::test(start_paused = true)] async fn a_retired_fill_drops_a_gap_between_source_groups_at_once() { fill_across_a_gap(moq_net::track::Order::Ascending, true).await; @@ -1451,85 +1382,31 @@ mod tests { drop(video); } - /// A source keyed finer than microseconds maps a run of its starts to each output start. A - /// fill asks the source for the one group holding the run's last start, rather than a fetch - /// per start or the groups below it, and serves the group starting inside the run. - #[tokio::test(start_paused = true)] - async fn a_finer_source_is_asked_for_one_group() { - let fetched = fill_finer(Some(1_000_001), false).await; - assert_eq!(fetched.expect("the source has it").start, 1_000); - } - - /// The group holding the run's last start starts below the run, so the source has no group - /// the output group could mirror. - #[tokio::test(start_paused = true)] - async fn a_finer_source_without_the_group_drops_it() { - assert!( - fill_finer(Some(999_999), false).await.is_err(), - "the source doesn't have it" - ); - } - - /// A retired rung's drain can't wait on the source to place a time, so it refuses the group - /// for the rung that replaces it rather than holding the retirement. + /// A retired rung can't wait on its source to say what timescale to accept a request in, so + /// it refuses the request for the rung that replaces it rather than holding it. #[tokio::test(start_paused = true)] - async fn a_retired_rung_refuses_a_finer_lookup_it_cant_place() { - assert!(fill_finer(None, true).await.is_err(), "nothing places the group yet"); - } - - /// Fetch output group 1000 of a rung over a nanosecond source holding only a later live group, - /// answering the source's lookup with a group at `answer`, and return what the fetch got. - async fn fill_finer(answer: Option, retired: bool) -> moq_net::Result { + async fn a_retired_rung_refuses_a_request_it_cant_describe() { let (rung, source, retire) = idle_rung(); - let video = source - .create_track( - "video", - hang::container::track_info(hang::catalog::PRIORITY.video).with_timescale(moq_net::Timescale::NANO), - ) - .unwrap(); - let source_fills = video.requests(); - video - .create_group(moq_net::group::Info { start: 5_000_000_001 }) - .unwrap() - .finish() - .unwrap(); - + // Nothing answers the source's track request, so its info never resolves. + let _unanswered = source.requests(); let output = moq_net::broadcast::Info::default().produce(); - let track = output - .create_track("video/120p", hang::container::track_info(hang::catalog::PRIORITY.video)) - .unwrap(); - let fills = track.requests(); - let consumer = track.consume(); - let fetching = tokio::spawn(async move { consumer.fetch_group(1_000, None).await }); - let fill = fills.requested_fill().await.map(groups).unwrap(); - - if retired { - retire.send(true).unwrap(); - } - let mut tasks = tokio::task::JoinSet::new(); - spawn_fill(&mut tasks, rung, fill, Arc::new(Semaphore::new(1))); + let mut dynamic = output.requests(); + let view = output.consume().track("video/120p").unwrap(); + let track = tokio::spawn(async move { view.info().await.map(drop) }); + let request = dynamic.requested_track().await.unwrap(); + let mut running = tokio::spawn(serve(rung, request)); let wait = std::time::Duration::from_secs(5); - if let Some(answer) = answer { - let moq_net::track::Fill::Lookup(lookup) = kio::clock::timeout(wait, source_fills.requested_fill()) - .await - .expect("the source is asked") - .unwrap() - else { - panic!("a time lookup"); - }; - // The last nanosecond microsecond 1000 holds. - assert_eq!(lookup.time(), 1_000_999); - lookup.accept(answer, None).unwrap().finish().unwrap(); - } - // Not timed: the paused clock runs ahead while a codec thread opens the encoder. - let fetched = fetching.await.unwrap(); - tasks.join_next().await.unwrap().unwrap(); assert!( - kio::clock::timeout(wait, source_fills.requested_fill()).await.is_err(), - "the source is asked nothing else" + kio::clock::timeout(wait, &mut running).await.is_err(), + "the rung waits on its source" ); - drop(video); - fetched + retire.send(true).unwrap(); + kio::clock::timeout(wait, running) + .await + .expect("retirement ends the wait") + .unwrap() + .unwrap(); + assert!(matches!(track.await.unwrap(), Err(moq_net::Error::Cancel))); } /// Fill output starts 1 to an hour, with source groups at 0 and an hour, or, when `empty`, @@ -1619,15 +1496,11 @@ mod tests { async fn retirement_serves_a_fill_past_a_reader_that_stopped() { use std::task::Poll; let (rung, source, retire) = idle_rung(); - let source_fills = source - .create_track("video", hang::container::track_info(hang::catalog::PRIORITY.video)) - .unwrap() - .requests(); + let timed = + hang::container::track_info(hang::catalog::PRIORITY.video).with_timescale(moq_net::Timescale::MILLI); + let source_fills = source.create_track("video", timed.clone()).unwrap().requests(); let output = moq_net::broadcast::Info::default().produce(); - let timed = moq_net::track::Info::default() - .with_timescale(moq_net::Timescale::MILLI) - .with_max_age(hang::container::MAX_AGE); let mut track = output.create_track("video/120p", timed).unwrap(); let fills = track.requests(); let mut reader = track.subscribe( @@ -1703,8 +1576,8 @@ mod tests { let request = dynamic.requested_track().await.unwrap(); let running = tokio::spawn(serve(rung, request)); let mut track = subscribing.await.unwrap(); - // The track is admitted before `serve` is polled; the feed's subscription proves the - // live path is parked waiting for media rather than never started. + // The feed's subscription proves the live path is parked waiting for media rather than + // never started. let mut demand = video.demand(); while !demand.wanted_changed().await.unwrap() {} running.abort(); @@ -1731,7 +1604,7 @@ mod tests { assert!(matches!(observed.closed().await, Err(moq_net::Error::Cancel))); } - /// The track is admitted before the serving future is spawned, so it also owns cleanup + /// The request is held before the serving future is spawned, so it also owns the answer /// when its caller abandons that future without polling it at all. #[tokio::test(start_paused = true)] async fn an_unpolled_rung_reports_cancel() { @@ -1739,12 +1612,11 @@ mod tests { let output = moq_net::broadcast::Info::default().produce(); let mut dynamic = output.requests(); let view = output.consume(); - let subscribing = tokio::spawn(async move { view.track("rung").unwrap().subscribe(None).await.unwrap() }); + let subscribing = tokio::spawn(async move { view.track("rung").unwrap().subscribe(None).await.map(drop) }); let request = dynamic.requested_track().await.unwrap(); let running = serve(rung, request); - let mut track = subscribing.await.unwrap(); drop(running); - assert!(matches!(track.recv_group().await, Err(moq_net::Error::Cancel))); + assert!(matches!(subscribing.await.unwrap(), Err(moq_net::Error::Cancel))); } /// A source switching color space mid-stream (an SDR insert in an HDR From b4b02f48a4296f6e4ffe8deb437d5695bb055de3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 18 Nov 2026 15:48:19 -0800 Subject: [PATCH 5/7] fix(transcode): the ladder resolves its source's timescale once A rung request used to wait on the source track's info before accepting, holding a source handle per request. The ladder now resolves the timescale once per source rendition (when built, and when it follows the source onto another track or stream) and hands it to every rung, so `serve` accepts at once, as on main. Co-Authored-By: Claude Opus 5.5 --- rs/moq-transcode/src/lib.rs | 2 - rs/moq-transcode/src/pipeline.rs | 79 +++++++++++++++++++ rs/moq-transcode/src/rung.rs | 125 ++++++------------------------- 3 files changed, 100 insertions(+), 106 deletions(-) diff --git a/rs/moq-transcode/src/lib.rs b/rs/moq-transcode/src/lib.rs index d44d0d4553..1556281541 100644 --- a/rs/moq-transcode/src/lib.rs +++ b/rs/moq-transcode/src/lib.rs @@ -490,8 +490,6 @@ mod tests { let request = requests.requested_track().await.unwrap(); assert_eq!(request.name(), name); let mut serving = Box::pin(rung::serve(ladder.rung(name).unwrap(), request)); - // The source track is local, so its timescale is known and this poll accepts the track. - assert!(futures::poll!(&mut serving).is_pending()); subscribers.push((name, subscribing.await.unwrap().ordered())); // With demand present and no source group, this poll attaches the rung // to the shared feed before parking. No startup sleep can guarantee that. diff --git a/rs/moq-transcode/src/pipeline.rs b/rs/moq-transcode/src/pipeline.rs index ccc83f678d..89bd36262f 100644 --- a/rs/moq-transcode/src/pipeline.rs +++ b/rs/moq-transcode/src/pipeline.rs @@ -53,6 +53,8 @@ pub(crate) struct Pipeline { /// The catalog ID of the source rendition the rungs are sized against. name: String, rendition: VideoConfig, + /// Its track's timescale, which every rung's output takes. + scale: moq_net::Timescale, /// The shared live decode of that rendition: one subscription and one /// decoder for every rung serving off it. feed: Feed, @@ -83,6 +85,7 @@ impl Pipeline { name: String, rendition: VideoConfig, ) -> Result { + let scale = timescale(&source, rendition.track(&name)).await?; // One shared live decode for every rung of this source: N active rungs // share one subscription and one decoder instead of N. let feed = Feed::new( @@ -99,6 +102,7 @@ impl Pipeline { decoders, name, rendition, + scale, feed, rungs: Vec::new(), names: Names::default(), @@ -201,6 +205,7 @@ impl Pipeline { feed: self.feed.clone(), broadcast: self.source.clone(), config: self.rendition.clone(), + scale: self.scale, transcode: self.config.clone(), active: self.active.clone(), rendition: self.active.rendition(&published.rung), @@ -235,6 +240,17 @@ impl Pipeline { true => Vec::new(), false => self.rungs.clone(), }; + // Another track, or another stream on it, may key its groups in another timescale. + let scale = match rebuilt { + true => match timescale(&self.source, rendition.track(&name)).await { + Ok(scale) => scale, + Err(err) => { + tracing::warn!(%err, source = %name, "could not resolve the new source's timescale"); + return Ok(()); + } + }, + false => self.scale, + }; // Resolve against the new source before committing to it, so a failure // leaves the ladder exactly as it was and the next snapshot tries again. @@ -263,6 +279,7 @@ impl Pipeline { } self.name = name; self.rendition = rendition; + self.scale = scale; // Retire whatever the new ladder does not carry forward: a height it has no // room for, and a height it kept at a picture it is no longer serving, @@ -285,6 +302,21 @@ impl Pipeline { } } +/// The timescale of the source track `track`, for its rungs to key their groups in, so each output +/// group has its source group's key. Resolved once per source rendition rather than per request, +/// so a rung request is answered at once and an idle rung holds nothing of its source. +/// +/// A scale-less source's keys aren't times, but the frames decoded from it are, so its rungs are +/// in the container's microseconds, and its keys pass through as they are. +async fn timescale(source: &moq_net::broadcast::Consumer, track: &str) -> Result { + let track = source.track(track)?; + let info = tokio::select! { + info = track.info() => info?, + () = source.closed() => return Err(moq_net::Error::Cancel.into()), + }; + Ok(info.timescale.unwrap_or(hang::container::TIMESCALE)) +} + #[cfg(test)] mod tests { use super::*; @@ -310,6 +342,9 @@ mod tests { #[tokio::test] async fn a_flapping_source_probes_each_shape_once() { let source = moq_net::broadcast::Info::default().produce(); + let _video = source + .create_track("video", hang::container::track_info(hang::catalog::PRIORITY.video)) + .unwrap(); let config = Config { ladder: Ladder::new([Rung::new(120, moq_net::bandwidth::Rate::from_bps(100_000))]).unwrap(), encoder: moq_video::encode::Kind::Software, @@ -345,4 +380,48 @@ mod tests { assert_eq!(cursor.try_next().unwrap().rendition.name(), "video/120p.5"); assert!(cursor.try_next().is_none()); } + + /// Each rung keys its groups in its source track's timescale, so a ladder following the source + /// onto another track takes that track's. + #[tokio::test] + async fn a_rung_takes_its_source_tracks_timescale() { + let source = moq_net::broadcast::Info::default().produce(); + let info = hang::container::track_info(hang::catalog::PRIORITY.video); + let _micro = source.create_track("video", info.clone()).unwrap(); + let _nano = source + .create_track("nano", info.with_timescale(moq_net::Timescale::NANO)) + .unwrap(); + let config = Config { + ladder: Ladder::new([Rung::new(120, moq_net::bandwidth::Rate::from_bps(100_000))]).unwrap(), + encoder: moq_video::encode::Kind::Software, + decoder: moq_video::decode::Kind::Software, + source: None, + ..Default::default() + }; + let decoders = catalog::Decoders::new(config.decoder()); + let mut pipeline = Pipeline::new( + source.consume(), + config, + active::Producer::default(), + decoders, + "video".into(), + video(640, 360), + ) + .await + .unwrap(); + let scale = |pipeline: &mut Pipeline| { + let name = pipeline.rungs()[0].rung.name.clone(); + pipeline.rung(&name).unwrap().scale + }; + assert_eq!(scale(&mut pipeline), hang::container::TIMESCALE); + + let mut snapshot = Video::default(); + snapshot.insert("nano", video(640, 360)).unwrap(); + pipeline.follow(&snapshot).await.unwrap(); + assert_eq!( + pipeline.name, "nano", + "the ladder follows the source onto its new track" + ); + assert_eq!(scale(&mut pipeline), moq_net::Timescale::NANO); + } } diff --git a/rs/moq-transcode/src/rung.rs b/rs/moq-transcode/src/rung.rs index 53ff86c64a..0bb63b4445 100644 --- a/rs/moq-transcode/src/rung.rs +++ b/rs/moq-transcode/src/rung.rs @@ -82,6 +82,9 @@ pub(crate) struct Rung { pub broadcast: moq_net::broadcast::Consumer, /// The source rendition's catalog entry (codec + container). pub config: VideoConfig, + /// The source track's timescale, which the output track takes so each output group has its + /// source group's key. + pub scale: moq_net::Timescale, /// The codec implementations, devices, and resize the transcode was configured with. pub transcode: crate::Config, /// Where to report that this rung is encoding. @@ -143,51 +146,13 @@ impl Rung { } } -/// A rung track not yet ended, which a cancelled serving future ends with `Cancel` rather than -/// dropping it unanswered or unfinished. `None` once it ended on purpose. -struct Serving(Option); - -/// How far a rung track's answer got. -enum Stage { - /// Waiting on the source track's timescale to accept the request in. - Requested(moq_net::track::Request), - /// Accepted, until the track ends. - Accepted(moq_net::track::Producer), -} - -impl Serving { - /// Accept the request in `info`, returning the track. - fn accept(&mut self, info: moq_net::track::Info) -> moq_net::track::Producer { - let Some(Stage::Requested(request)) = self.0.take() else { - unreachable!("a rung track is accepted once, before anything else ends it"); - }; - let producer = request.accept(info); - self.0 = Some(Stage::Accepted(producer.clone())); - producer - } - - /// Let go of a track that ended on its own, releasing the last clone without aborting it. - fn release(&mut self) { - self.0 = None; - } - - /// Refuse the request with `err`. Only a request still waiting can be. - fn reject(&mut self, err: moq_net::Error) { - let Some(Stage::Requested(request)) = self.0.take() else { - unreachable!("a rung track is refused only before it is accepted"); - }; - request.reject(err); - } -} +/// Keeps a cancelled serving future from dropping an unfinished output track. +struct Serving(Option); impl Drop for Serving { fn drop(&mut self) { - match self.0.take() { - Some(Stage::Requested(request)) => request.reject(moq_net::Error::Cancel), - Some(Stage::Accepted(producer)) => { - let _ = producer.abort(moq_net::Error::Cancel); - } - None => {} + if let Some(producer) = self.0.take() { + let _ = producer.abort(moq_net::Error::Cancel); } } } @@ -239,20 +204,12 @@ pub(crate) fn serve(rung: Rung, request: moq_net::track::Request) -> impl Future // Grab the group-request handle before accepting: a Request is dynamic from // birth, so a fetch racing the acceptance queues instead of failing. let dynamic = request.requests(); - // Held before the future is polled, so dropping it unpolled still answers the request. - let mut serving = Serving(Some(Stage::Requested(request))); + // In the source's timescale, so each output group has its source group's key. + let info = hang::container::track_info(hang::catalog::PRIORITY.video).with_timescale(rung.scale); + let mut producer = request.accept(info); + let serving = Serving(Some(producer.clone())); async move { - let scale = match timescale(&rung).await { - Ok(Some(scale)) => scale, - // `serving` refuses the request with `Cancel` as it drops. - Ok(None) => return Ok(()), - Err(err) => { - serving.reject(err.clone()); - return Err(err.into()); - } - }; - let info = hang::container::track_info(hang::catalog::PRIORITY.video).with_timescale(scale); - let mut producer = serving.accept(info); + let mut serving = serving; let (finished, mut finishing) = tokio::sync::watch::channel(false); let live = async { @@ -274,8 +231,9 @@ pub(crate) fn serve(rung: Rung, request: moq_net::track::Request) -> impl Future // to count, so finishing then would reject the very group it was fetching. match (live, fetches) { (Ok(Ended::Finished), Ok(())) => { + // Released with `serving`, the last clone, which then no longer aborts it. drop(producer.finish()?); - serving.release(); + serving.0 = None; Ok(()) } // `serving` aborts the track on drop, so subscribers see an end rather than a stall. @@ -284,22 +242,6 @@ pub(crate) fn serve(rung: Rung, request: moq_net::track::Request) -> impl Future } } -/// The timescale to accept a rung track in: its source track's, so each output group has its -/// source group's key. `None` when the rung retires or its source closes first, since the source -/// may never say: a later request reaches the rung that replaces this one. -/// -/// A scale-less source's keys aren't times, but the frames decoded from it are, so its rungs are -/// in the container's microseconds, and its keys still pass through as they are. -async fn timescale(rung: &Rung) -> moq_net::Result> { - let track = rung.broadcast.track(&rung.source)?; - let mut retire = rung.retire.clone(); - tokio::select! { - info = track.info() => Ok(Some(info?.timescale.unwrap_or(hang::container::TIMESCALE))), - () = retire.fired() => Ok(None), - () = rung.broadcast.closed() => Ok(None), - } -} - /// Wait until some subscriber wants the track, or none does, returning at once if /// that already holds. A fresh clone reports the current value first, so a /// change seen through [`moq_net::track::Demand::wanted`] instead is never missed. @@ -1239,6 +1181,7 @@ mod tests { ), broadcast, config, + scale: hang::container::TIMESCALE, transcode: crate::Config { encoder: moq_video::encode::Kind::Software, decoder: moq_video::decode::Kind::Software, @@ -1382,33 +1325,6 @@ mod tests { drop(video); } - /// A retired rung can't wait on its source to say what timescale to accept a request in, so - /// it refuses the request for the rung that replaces it rather than holding it. - #[tokio::test(start_paused = true)] - async fn a_retired_rung_refuses_a_request_it_cant_describe() { - let (rung, source, retire) = idle_rung(); - // Nothing answers the source's track request, so its info never resolves. - let _unanswered = source.requests(); - let output = moq_net::broadcast::Info::default().produce(); - let mut dynamic = output.requests(); - let view = output.consume().track("video/120p").unwrap(); - let track = tokio::spawn(async move { view.info().await.map(drop) }); - let request = dynamic.requested_track().await.unwrap(); - let mut running = tokio::spawn(serve(rung, request)); - let wait = std::time::Duration::from_secs(5); - assert!( - kio::clock::timeout(wait, &mut running).await.is_err(), - "the rung waits on its source" - ); - retire.send(true).unwrap(); - kio::clock::timeout(wait, running) - .await - .expect("retirement ends the wait") - .unwrap() - .unwrap(); - assert!(matches!(track.await.unwrap(), Err(moq_net::Error::Cancel))); - } - /// Fill output starts 1 to an hour, with source groups at 0 and an hour, or, when `empty`, /// none until the fill has waited on the source. async fn fill_across(order: moq_net::track::Order, retired: bool, empty: bool) { @@ -1576,8 +1492,8 @@ mod tests { let request = dynamic.requested_track().await.unwrap(); let running = tokio::spawn(serve(rung, request)); let mut track = subscribing.await.unwrap(); - // The feed's subscription proves the live path is parked waiting for media rather than - // never started. + // The track is admitted before `serve` is polled; the feed's subscription proves the + // live path is parked waiting for media rather than never started. let mut demand = video.demand(); while !demand.wanted_changed().await.unwrap() {} running.abort(); @@ -1604,7 +1520,7 @@ mod tests { assert!(matches!(observed.closed().await, Err(moq_net::Error::Cancel))); } - /// The request is held before the serving future is spawned, so it also owns the answer + /// The track is admitted before the serving future is spawned, so it also owns cleanup /// when its caller abandons that future without polling it at all. #[tokio::test(start_paused = true)] async fn an_unpolled_rung_reports_cancel() { @@ -1612,11 +1528,12 @@ mod tests { let output = moq_net::broadcast::Info::default().produce(); let mut dynamic = output.requests(); let view = output.consume(); - let subscribing = tokio::spawn(async move { view.track("rung").unwrap().subscribe(None).await.map(drop) }); + let subscribing = tokio::spawn(async move { view.track("rung").unwrap().subscribe(None).await.unwrap() }); let request = dynamic.requested_track().await.unwrap(); let running = serve(rung, request); + let mut track = subscribing.await.unwrap(); drop(running); - assert!(matches!(subscribing.await.unwrap(), Err(moq_net::Error::Cancel))); + assert!(matches!(track.recv_group().await, Err(moq_net::Error::Cancel))); } /// A source switching color space mid-stream (an SDR insert in an HDR From 3804fd12304286d96d5b4861dd6eb828350d07e9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 18 Nov 2026 16:00:01 -0800 Subject: [PATCH 6/7] fix(transcode): a source move waits on its timescale off the service loop `follow` returns a move onto another track or stream instead of awaiting that track's info inline, and `run` resolves it beside rung requests and catalog updates. A newer snapshot drops a pending move, so a track that never answers no longer stalls the transcoder. The prologue races its resolve against catalog updates the same way. Co-Authored-By: Claude Opus 5.5 --- rs/moq-transcode/src/lib.rs | 128 +++++++++++++++++++++++++++++-- rs/moq-transcode/src/pipeline.rs | 90 ++++++++++++++-------- 2 files changed, 180 insertions(+), 38 deletions(-) diff --git a/rs/moq-transcode/src/lib.rs b/rs/moq-transcode/src/lib.rs index 1556281541..f8b964acff 100644 --- a/rs/moq-transcode/src/lib.rs +++ b/rs/moq-transcode/src/lib.rs @@ -127,18 +127,33 @@ impl Transcoder { } = &mut self; // The source catalog drives everything; wait for a snapshot with a usable - // video rendition (the first may precede the source publishing its video). + // video rendition (the first may precede the source publishing its video), + // and for its track's timescale, which a newer snapshot supersedes. let mut catalogs = moq_mux::catalog::hang::Consumer::<()>::new(source.track(hang::Catalog::DEFAULT_NAME)?); let mut decoders = catalog::Decoders::new(config.decoder()); - let (source_name, source_config, snapshot) = loop { - let Some(snapshot) = catalogs.next().await? else { + let mut next = catalogs.next().await?; + let (source_name, source_config, scale, snapshot) = loop { + let Some(snapshot) = next else { return Err(Error::NoSource); }; match catalog::choose_source(&snapshot.video, &mut decoders).await { - Ok((name, config)) => break (name, config, snapshot), + Ok((name, config)) => { + let resolving = pipeline::timescale(source, hang::catalog::Reference::track(&config, &name)); + tokio::select! { + scale = resolving => match scale { + Ok(scale) => break (name, config, scale, snapshot), + Err(err) => tracing::debug!(%err, source = %name, "no timescale for the source yet; waiting for a catalog update"), + }, + update = catalogs.next() => { + next = update?; + continue; + } + } + } Err(Error::NoSource) => tracing::debug!("no transcodable rendition yet; waiting for a catalog update"), Err(err) => return Err(err), } + next = catalogs.next().await?; }; // The ladder, the shared decode behind it, and the rungs serving off it. // Resolved again on every source catalog snapshot, so a source that resizes @@ -150,6 +165,7 @@ impl Transcoder { decoders, source_name, source_config, + scale, ) .await?; @@ -164,6 +180,9 @@ impl Transcoder { // Serve rung requests and follow source catalog updates until the source ends. let mut tasks = tokio::task::JoinSet::new(); + // A move onto another source track, waiting on its timescale, with the snapshot that + // asked for it. A newer snapshot drops it. + let mut moving: Option = None; loop { tokio::select! { request = dynamic.requested_track() => { @@ -176,7 +195,11 @@ impl Transcoder { }, update = catalogs.next() => match update { Ok(Some(snapshot)) => { - ladder.follow(&snapshot.video).await?; + moving = None; + if let Some(next) = ladder.follow(&snapshot.video).await { + let scale = Box::pin(pipeline::timescale(source, next.track())); + moving = Some(Moving { next, scale, snapshot: snapshot.clone() }); + } let mut guard = derived.modify()?; catalog::populate(&mut guard, &snapshot, ladder.rungs(), config.source.as_ref())?; guard.commit()?; @@ -188,6 +211,19 @@ impl Transcoder { break; } }, + scale = Moving::scale(&mut moving) => { + let Moving { next, snapshot, .. } = moving.take().expect("resolved a move"); + match scale { + Ok(scale) => { + ladder.adopt(next, scale).await; + let mut guard = derived.modify()?; + catalog::populate(&mut guard, &snapshot, ladder.rungs(), config.source.as_ref())?; + guard.commit()?; + } + // Keep serving the ladder that works; the next snapshot tries again. + Err(err) => tracing::warn!(%err, "could not resolve the new source's timescale"), + } + }, Some(result) = tasks.join_next() => match result { Ok(Ok(())) => {} Ok(Err(err)) => tracing::warn!(%err, "rung failed"), @@ -210,6 +246,24 @@ impl Transcoder { } } +/// A move onto another source track, waiting on that track's timescale. +struct Moving { + next: pipeline::Move, + scale: std::pin::Pin> + Send>>, + /// The source catalog snapshot to publish the moved ladder with. + snapshot: moq_mux::catalog::hang::Catalog, +} + +impl Moving { + /// The timescale of the move in `moving`, never resolving without one. + async fn scale(moving: &mut Option) -> Result { + match moving { + Some(moving) => moving.scale.as_mut().await, + None => std::future::pending().await, + } + } +} + impl Drop for Transcoder { fn drop(&mut self) { // The owner exists before `run` is polled. A cancelled prologue must end the @@ -474,6 +528,7 @@ mod tests { decoders, "video".into(), rendition, + hang::container::TIMESCALE, ) .await .unwrap(); @@ -2140,6 +2195,69 @@ mod tests { cold.abort(); } + /// A source moving onto a track that never answers keeps the transcoder serving: rung requests + /// are still answered, and a newer catalog snapshot drops the move. + #[tokio::test] + async fn an_unanswered_source_track_leaves_the_ladder_serving() { + let mut source = source_catalog(320, 240); + // Serves any track nothing created, and answers none of them. + let _withheld = source.broadcast.requests(); + let config = Config { + ladder: Ladder::new([Rung::new(120, moq_net::bandwidth::Rate::from_bps(100_000))]).unwrap(), + encoder: moq_video::encode::Kind::Software, + decoder: moq_video::decode::Kind::Software, + source: Some(moq_net::path::Relative::new(".").unwrap()), + ..Default::default() + }; + let output = moq_net::broadcast::Info::default().produce(); + let consumer = output.consume(); + let transcoder = tokio::spawn(run(source.broadcast.consume(), output, config)); + let track = loop { + match consumer.track(hang::Catalog::DEFAULT_NAME) { + Ok(track) => break track, + Err(moq_net::Error::NotFound) => tokio::task::yield_now().await, + Err(err) => panic!("catalog track: {err}"), + } + }; + let mut catalogs = moq_mux::catalog::hang::Consumer::<()>::new(track); + await_catalog(&mut catalogs, |snapshot| { + snapshot.video.renditions.contains_key("video/120p") + }) + .await; + + // The source rendition moves onto a track whose info never resolves. + let mut moved = source.catalog.snapshot().video.renditions["video"].clone(); + moved.track = Some("withheld".into()); + source + .catalog + .modify() + .unwrap() + .video + .renditions + .insert("video".into(), moved); + let track = |snapshot: &moq_mux::catalog::hang::Catalog| snapshot.video.renditions["video"].track.clone(); + let wait = std::time::Duration::from_secs(5); + let derived = kio::clock::timeout(wait, await_catalog(&mut catalogs, |snapshot| track(snapshot).is_some())) + .await + .expect("the transcoder follows the snapshot"); + assert!( + derived.video.renditions.contains_key("video/120p"), + "the ladder keeps serving until the move resolves" + ); + + kio::clock::timeout(wait, consumer.track("video/120p").unwrap().info()) + .await + .expect("a rung request is answered while the move waits") + .unwrap(); + + source.resize(320, 240); + kio::clock::timeout(wait, await_catalog(&mut catalogs, |snapshot| track(snapshot).is_none())) + .await + .expect("a newer snapshot drops the move"); + + transcoder.abort(); + } + /// A source whose codec description changes rebuilds the shared decode, so /// every rung retires with it. The picture may not have moved at all, so shape /// alone would hand the replacements the names that just ended. They have to be diff --git a/rs/moq-transcode/src/pipeline.rs b/rs/moq-transcode/src/pipeline.rs index 89bd36262f..2dce95b365 100644 --- a/rs/moq-transcode/src/pipeline.rs +++ b/rs/moq-transcode/src/pipeline.rs @@ -84,8 +84,8 @@ impl Pipeline { decoders: catalog::Decoders, name: String, rendition: VideoConfig, + scale: moq_net::Timescale, ) -> Result { - let scale = timescale(&source, rendition.track(&name)).await?; // One shared live decode for every rung of this source: N active rungs // share one subscription and one decoder instead of N. let feed = Feed::new( @@ -215,20 +215,24 @@ impl Pipeline { } /// Resolve the ladder again against a new source catalog snapshot. - pub(crate) async fn follow(&mut self, video: &Video) -> Result<(), Error> { + /// + /// A move onto another track, or another stream on it, waits on that track's timescale, which + /// may take a round trip or never come. So it is returned rather than awaited, for the caller + /// to resolve with [`timescale`] while it keeps serving and to finish with [`Self::adopt`]. + pub(crate) async fn follow(&mut self, video: &Video) -> Option { let (name, rendition) = match catalog::follow_source(video, &self.name, &mut self.decoders).await { Ok(chosen) => chosen, // Nothing transcodable in this snapshot: keep serving the ladder we // have rather than tearing it down over an edit the source may undo. Err(err) => { tracing::debug!(%err, "no transcodable rendition in the catalog update"); - return Ok(()); + return None; } }; if name == self.name && same_picture(&rendition, &self.rendition) { catalog::inherit(&mut self.rungs, &rendition); self.rendition = rendition; - return Ok(()); + return None; } // A different track, or a different codec on the same one: every rung is @@ -236,21 +240,25 @@ impl Pipeline { // and none of their names carries forward. let rebuilt = rendition.track(&name) != self.rendition.track(&self.name) || !catalog::same_stream(&self.rendition, &rendition); + if rebuilt { + // It may key its groups in another timescale. + return Some(Move { name, rendition }); + } + self.apply(name, rendition, false, self.scale).await; + None + } + + /// Finish a move [`Self::follow`] returned, in the timescale its track turned out to have. + pub(crate) async fn adopt(&mut self, next: Move, scale: moq_net::Timescale) { + self.apply(next.name, next.rendition, true, scale).await; + } + + /// Move the ladder onto a source rendition, rebuilding the shared decode when `rebuilt`. + async fn apply(&mut self, name: String, rendition: VideoConfig, rebuilt: bool, scale: moq_net::Timescale) { let carry = match rebuilt { true => Vec::new(), false => self.rungs.clone(), }; - // Another track, or another stream on it, may key its groups in another timescale. - let scale = match rebuilt { - true => match timescale(&self.source, rendition.track(&name)).await { - Ok(scale) => scale, - Err(err) => { - tracing::warn!(%err, source = %name, "could not resolve the new source's timescale"); - return Ok(()); - } - }, - false => self.scale, - }; // Resolve against the new source before committing to it, so a failure // leaves the ladder exactly as it was and the next snapshot tries again. @@ -261,7 +269,7 @@ impl Pipeline { Ok(rungs) => rungs, Err(err) => { tracing::warn!(%err, source = %name, "could not resolve a ladder for the new source"); - return Ok(()); + return; } }; @@ -298,7 +306,19 @@ impl Pipeline { tracing::info!(source = %self.name, rungs = rungs.len(), "source changed; ladder resolved again"); self.active.declare(rungs.iter().map(|published| &published.rung)); self.rungs = rungs; - Ok(()) + } +} + +/// A source rendition the ladder moves onto once its track's timescale is known. +pub(crate) struct Move { + name: String, + rendition: VideoConfig, +} + +impl Move { + /// The source track whose timescale the move waits on. + pub(crate) fn track(&self) -> &str { + self.rendition.track(&self.name) } } @@ -308,13 +328,15 @@ impl Pipeline { /// /// A scale-less source's keys aren't times, but the frames decoded from it are, so its rungs are /// in the container's microseconds, and its keys pass through as they are. -async fn timescale(source: &moq_net::broadcast::Consumer, track: &str) -> Result { - let track = source.track(track)?; - let info = tokio::select! { - info = track.info() => info?, - () = source.closed() => return Err(moq_net::Error::Cancel.into()), - }; - Ok(info.timescale.unwrap_or(hang::container::TIMESCALE)) +pub(crate) fn timescale( + source: &moq_net::broadcast::Consumer, + track: &str, +) -> impl Future> + Send + 'static { + let track = source.track(track); + async move { + let info = track?.info().await?; + Ok(info.timescale.unwrap_or(hang::container::TIMESCALE)) + } } #[cfg(test)] @@ -342,9 +364,6 @@ mod tests { #[tokio::test] async fn a_flapping_source_probes_each_shape_once() { let source = moq_net::broadcast::Info::default().produce(); - let _video = source - .create_track("video", hang::container::track_info(hang::catalog::PRIORITY.video)) - .unwrap(); let config = Config { ladder: Ladder::new([Rung::new(120, moq_net::bandwidth::Rate::from_bps(100_000))]).unwrap(), encoder: moq_video::encode::Kind::Software, @@ -361,6 +380,7 @@ mod tests { decoders, "video".into(), video(640, 360), + hang::container::TIMESCALE, ) .await .unwrap(); @@ -368,7 +388,7 @@ mod tests { for (width, height) in [(480, 360), (640, 360), (480, 360), (640, 360)] { let mut snapshot = Video::default(); snapshot.insert("video", video(width, height)).unwrap(); - pipeline.follow(&snapshot).await.unwrap(); + assert!(pipeline.follow(&snapshot).await.is_none(), "a resize keeps the stream"); } // A fresh name per incarnation, but only one probe per picture. @@ -406,6 +426,7 @@ mod tests { decoders, "video".into(), video(640, 360), + hang::container::TIMESCALE, ) .await .unwrap(); @@ -417,11 +438,14 @@ mod tests { let mut snapshot = Video::default(); snapshot.insert("nano", video(640, 360)).unwrap(); - pipeline.follow(&snapshot).await.unwrap(); - assert_eq!( - pipeline.name, "nano", - "the ladder follows the source onto its new track" - ); + let next = pipeline + .follow(&snapshot) + .await + .expect("another track waits on its timescale"); + assert_eq!(scale(&mut pipeline), hang::container::TIMESCALE, "nothing moved yet"); + let resolved = timescale(&source.consume(), next.track()).await.unwrap(); + pipeline.adopt(next, resolved).await; + assert_eq!(pipeline.name, "nano"); assert_eq!(scale(&mut pipeline), moq_net::Timescale::NANO); } } From 18409d39d9fa0c6227b0897d7b9bed5ca12a21da Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 18 Nov 2026 16:05:36 -0800 Subject: [PATCH 7/7] fix(transcode): a source closing during the prologue ends the transcoder Closing a broadcast leaves its catalog track open, so the prologue's snapshot and timescale waits race the source closing. Co-Authored-By: Claude Opus 5.5 --- rs/moq-transcode/src/lib.rs | 88 +++++++++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 19 deletions(-) diff --git a/rs/moq-transcode/src/lib.rs b/rs/moq-transcode/src/lib.rs index f8b964acff..391d08f6bc 100644 --- a/rs/moq-transcode/src/lib.rs +++ b/rs/moq-transcode/src/lib.rs @@ -131,29 +131,39 @@ impl Transcoder { // and for its track's timescale, which a newer snapshot supersedes. let mut catalogs = moq_mux::catalog::hang::Consumer::<()>::new(source.track(hang::Catalog::DEFAULT_NAME)?); let mut decoders = catalog::Decoders::new(config.decoder()); - let mut next = catalogs.next().await?; - let (source_name, source_config, scale, snapshot) = loop { - let Some(snapshot) = next else { - return Err(Error::NoSource); - }; - match catalog::choose_source(&snapshot.video, &mut decoders).await { - Ok((name, config)) => { - let resolving = pipeline::timescale(source, hang::catalog::Reference::track(&config, &name)); - tokio::select! { - scale = resolving => match scale { - Ok(scale) => break (name, config, scale, snapshot), - Err(err) => tracing::debug!(%err, source = %name, "no timescale for the source yet; waiting for a catalog update"), - }, - update = catalogs.next() => { - next = update?; - continue; + let prologue = async { + let mut next = catalogs.next().await?; + loop { + let Some(snapshot) = next else { + return Err(Error::NoSource); + }; + match catalog::choose_source(&snapshot.video, &mut decoders).await { + Ok((name, config)) => { + let resolving = pipeline::timescale(source, hang::catalog::Reference::track(&config, &name)); + tokio::select! { + scale = resolving => match scale { + Ok(scale) => return Ok((name, config, scale, snapshot)), + Err(err) => tracing::debug!(%err, source = %name, "no timescale for the source yet; waiting for a catalog update"), + }, + update = catalogs.next() => { + next = update?; + continue; + } } } + Err(Error::NoSource) => { + tracing::debug!("no transcodable rendition yet; waiting for a catalog update") + } + Err(err) => return Err(err), } - Err(Error::NoSource) => tracing::debug!("no transcodable rendition yet; waiting for a catalog update"), - Err(err) => return Err(err), + next = catalogs.next().await?; } - next = catalogs.next().await?; + }; + // A closed source may keep its catalog open, so neither a snapshot nor the timescale ends the + // wait on their own. + let (source_name, source_config, scale, snapshot) = tokio::select! { + result = prologue => result?, + () = source.closed() => return Err(moq_net::Error::Cancel.into()), }; // The ladder, the shared decode behind it, and the rungs serving off it. // Resolved again on every source catalog snapshot, so a source that resizes @@ -2258,6 +2268,46 @@ mod tests { transcoder.abort(); } + /// A source that closes while the transcoder waits on its track's timescale ends the + /// transcoder, even though its catalog track stays open. + #[tokio::test] + async fn a_source_closed_before_its_timescale_ends_the_transcoder() { + let mut source = source_catalog(320, 240); + let mut withheld = source.broadcast.requests(); + let mut moved = source.catalog.snapshot().video.renditions["video"].clone(); + moved.track = Some("withheld".into()); + source + .catalog + .modify() + .unwrap() + .video + .renditions + .insert("video".into(), moved); + let config = Config { + ladder: Ladder::new([Rung::new(120, moq_net::bandwidth::Rate::from_bps(100_000))]).unwrap(), + encoder: moq_video::encode::Kind::Software, + decoder: moq_video::decode::Kind::Software, + source: None, + ..Default::default() + }; + let output = moq_net::broadcast::Info::default().produce(); + let transcoder = tokio::spawn(run(source.broadcast.consume(), output, config)); + + let wait = std::time::Duration::from_secs(5); + let request = kio::clock::timeout(wait, withheld.requested_track()) + .await + .expect("the transcoder asks for the source track") + .unwrap(); + assert_eq!(request.name(), "withheld"); + source.broadcast.close(); + let result = kio::clock::timeout(wait, transcoder) + .await + .expect("closing the source ends the transcoder") + .unwrap(); + assert!(result.is_err(), "a source gone before its ladder is no clean end"); + drop(request); + } + /// A source whose codec description changes rebuilds the shared decode, so /// every rung retires with it. The picture may not have moved at all, so shape /// alone would hand the replacements the names that just ended. They have to be