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/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 370fc6d675..391d08f6bc 100644 --- a/rs/moq-transcode/src/lib.rs +++ b/rs/moq-transcode/src/lib.rs @@ -127,19 +127,44 @@ 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 { - return Err(Error::NoSource); - }; - match catalog::choose_source(&snapshot.video, &mut decoders).await { - Ok((name, config)) => break (name, config, snapshot), - Err(Error::NoSource) => tracing::debug!("no transcodable rendition yet; waiting for a catalog update"), - Err(err) => return Err(err), + 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), + } + 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 // mid-stream takes the ladder with it. @@ -150,6 +175,7 @@ impl Transcoder { decoders, source_name, source_config, + scale, ) .await?; @@ -164,6 +190,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 +205,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 +221,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 +256,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 @@ -394,6 +458,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 +485,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 +496,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 { @@ -462,6 +538,7 @@ mod tests { decoders, "video".into(), rendition, + hang::container::TIMESCALE, ) .await .unwrap(); @@ -2053,6 +2130,184 @@ mod tests { } } + /// 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 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 { + 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; + 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(); + 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 key. + 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_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_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(1, None).await.unwrap()).await; + assert_eq!(first, (1, expected(0))); + 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 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 diff --git a/rs/moq-transcode/src/pipeline.rs b/rs/moq-transcode/src/pipeline.rs index ccc83f678d..2dce95b365 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, @@ -82,6 +84,7 @@ impl Pipeline { decoders: catalog::Decoders, name: String, rendition: VideoConfig, + scale: moq_net::Timescale, ) -> Result { // One shared live decode for every rung of this source: N active rungs // share one subscription and one decoder instead of N. @@ -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), @@ -210,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 @@ -231,6 +240,21 @@ 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(), @@ -245,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; } }; @@ -263,6 +287,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, @@ -281,7 +306,36 @@ 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) + } +} + +/// 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. +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)) } } @@ -326,6 +380,7 @@ mod tests { decoders, "video".into(), video(640, 360), + hang::container::TIMESCALE, ) .await .unwrap(); @@ -333,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. @@ -345,4 +400,52 @@ 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), + hang::container::TIMESCALE, + ) + .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(); + 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); + } } diff --git a/rs/moq-transcode/src/rung.rs b/rs/moq-transcode/src/rung.rs index 85385461bb..0bb63b4445 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; @@ -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,38 +146,6 @@ 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()) -} - -/// 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 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() -} - /// Keeps a cancelled serving future from dropping an unfinished output track. struct Serving(Option); @@ -233,7 +204,8 @@ 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); + // 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 { @@ -745,29 +717,14 @@ 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(start) = source_start(sequence, info.timescale) else { - // No source start maps here: the group can't exist. - return drop_gap(rung, &track, fill, sequence, info.timescale).await; - }; + // 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 = track.fetch_group(start, options) => match source { + source = track.fetch_group(sequence, options) => match source { Ok(source) => source, - Err(moq_net::Error::NotFound) => return drop_gap(rung, &track, fill, sequence, info.timescale).await, + Err(moq_net::Error::NotFound) => return drop_gap(rung, &track, fill, sequence).await, Err(err) => { refuse(err.clone()); return Err(err.into()); @@ -831,12 +788,11 @@ async fn fetch(rung: &Rung, fill: &moq_net::track::FillGroups, sequence: u64, li /// 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 @@ -848,7 +804,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}; @@ -868,22 +823,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()) @@ -898,10 +844,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)) }; @@ -1235,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, @@ -1252,16 +1199,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| { @@ -1318,7 +1261,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; @@ -1330,7 +1273,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; @@ -1469,15 +1412,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(