From 9b9ceb07bc995c403f9c2b5b0a161c049a09a700 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 16 Oct 2026 22:35:23 -0700 Subject: [PATCH 1/2] fix(mux): put a live export's budget on its first SUBSCRIBE A live export source subscribed with a zero budget to land on the newest group, then raised the budget after reading its first frame. Until that update reached every hop, each one dropped any group a newer one superseded. MP2 imported from TS is a group per frame, written five at a time per PES, so a joiner lost the groups right after its start: the hole `just test ts --full` reported in the MP2 PID. Subscribe with the real budget from the start and pick the live edge locally: the consumer starts at the newest group announced and discards the older ones the budget also delivers. Co-Authored-By: Claude Opus 5.5 --- rs/moq-mux/src/container/consumer.rs | 25 +++++- rs/moq-mux/src/container/source.rs | 99 ++++++++++++++-------- rs/moq-mux/src/container/ts/export_test.rs | 7 +- rs/moq-mux/src/jitter.rs | 6 +- 4 files changed, 94 insertions(+), 43 deletions(-) diff --git a/rs/moq-mux/src/container/consumer.rs b/rs/moq-mux/src/container/consumer.rs index ed459e587eb..330d4d872ab 100644 --- a/rs/moq-mux/src/container/consumer.rs +++ b/rs/moq-mux/src/container/consumer.rs @@ -85,6 +85,9 @@ pub struct Consumer { // subscription names no start. Cleared once a first group is chosen. startup: bool, + // Latch onto the newest group to arrive instead, discarding what arrived below it. + newest: bool, + // How far we may drift from the live edge before skipping a group. max_delay: std::time::Duration, @@ -166,6 +169,7 @@ impl Consumer { current: start.unwrap_or(0), pending: VecDeque::new(), startup: start.is_none(), + newest: false, max_delay, edge: None, start: None, @@ -177,6 +181,15 @@ impl Consumer { } } + /// Start at the newest group to arrive, the live edge, rather than the floor or the oldest + /// group the publisher serves. The subscription keeps its budget, so every group after the + /// start arrives as it would for any reader; what arrived below the start is discarded. + pub(crate) fn live(mut self) -> Self { + self.startup = true; + self.newest = true; + self + } + /// The group sequence of the last event read. pub(crate) fn group(&self) -> u64 { self.group @@ -274,7 +287,17 @@ impl Consumer { !group.empty }); if any_frame { - self.current = self.pending.front().expect("a group has a frame").sequence; + // A live reader takes the newest group announced so far, though its frames may + // still be on their way, and discards the older ones the budget also delivers. + let start = match self.newest { + true => self.pending.back(), + false => self.pending.front(), + } + .expect("a group has a frame") + .sequence; + // Nothing has been read, so discarding what lies below the start is no gap. + self.pending.retain(|group| group.sequence >= start); + self.current = start; self.startup = false; } } diff --git a/rs/moq-mux/src/container/source.rs b/rs/moq-mux/src/container/source.rs index 09c4a701c2c..00f8055f70a 100644 --- a/rs/moq-mux/src/container/source.rs +++ b/rs/moq-mux/src/container/source.rs @@ -72,8 +72,7 @@ pub(crate) struct ExportSource { /// Wire format, consumed when the subscription resolves into a consumer. media: Option, max_delay: std::time::Duration, - /// Start at the newest group, rather than the oldest the max delay still reaches, and - /// apply the max delay once it is read. + /// Start at the newest group, rather than the oldest the max delay still reaches. live: bool, /// The first group to read, so a resubscription skips groups already read. start: Option, @@ -252,9 +251,10 @@ impl ExportSource { }) } - /// Start at the newest group the publisher has, the live edge, rather than the oldest - /// one the max delay still reaches; the max delay applies from the first frame on, to the - /// groups that follow. + /// Start at the newest group to arrive, the live edge, rather than the oldest one the max + /// delay still reaches. The subscription carries the max delay from the start: under a zero + /// budget every hop drops each group a newer one supersedes, and raising it later cannot + /// recover the groups that follow the edge. pub fn live(mut self) -> Self { self.live = true; self @@ -367,12 +367,7 @@ impl ExportSource { }; (ready!(pending.poll_broadcast(waiter))?, name.clone()) }; - // A zero budget asks only for the newest group. - let max_delay = match self.live { - true => std::time::Duration::ZERO, - false => self.max_delay, - }; - let mut subscription = moq_net::track::Subscription::default().with_max_delay(max_delay); + let mut subscription = moq_net::track::Subscription::default().with_max_delay(self.max_delay); if let Some(start) = self.start { subscription = subscription.with_groups(start..); } @@ -392,7 +387,12 @@ impl ExportSource { .media .take() .expect("media present until the subscription resolves"); - self.state = SourceState::Active(Box::new(Consumer::new(track, media))); + let consumer = Consumer::new(track, media); + let consumer = match self.live { + true => consumer.live(), + false => consumer, + }; + self.state = SourceState::Active(Box::new(consumer)); } loop { @@ -402,12 +402,7 @@ impl ExportSource { let SourceState::Active(consumer) = &mut self.state else { unreachable!("subscription resolved into an Active consumer"); }; - let event = ready!(consumer.poll_item(waiter))?; - if std::mem::take(&mut self.live) { - // Started at the live edge: from here the budget tolerates reordering. - consumer.set_max_delay(self.max_delay); - } - match event { + match ready!(consumer.poll_item(waiter))? { Some(Item::Read(Read::Frame { group, frame })) => { if self.ends.is_some_and(|(end_group, _)| end_group != group) { self.finish_ends(); @@ -659,26 +654,60 @@ mod tests { } } - /// The requested budget must be present on the first SUBSCRIBE. Updating it - /// after SUBSCRIBE_OK cannot recover backlog the publisher already skipped. + /// The requested budget must be present on the first SUBSCRIBE, a live start's too. Updating + /// it after SUBSCRIBE_OK cannot recover backlog the publisher already skipped, nor the groups a + /// zero budget drops as superseded before the update lands. #[tokio::test] async fn latency_is_sent_with_the_initial_subscription() { - let live = Live::avc3(); - let max_delay = std::time::Duration::from_secs(10); - let mut export = ExportSource::for_video(&live.source().await, live.track.name(), &video(None), max_delay) - .unwrap() - .expect("fixture should produce a video rendition"); - - let observed = kio::wait(|waiter| { - let _ = export.poll_read(waiter); - match live.track.subscription() { - Some(subscription) => Poll::Ready(subscription.max_delay), - None => Poll::Pending, - } - }) - .await; + for live_start in [false, true] { + let live = Live::avc3(); + let max_delay = std::time::Duration::from_secs(10); + let export = ExportSource::for_video(&live.source().await, live.track.name(), &video(None), max_delay) + .unwrap() + .expect("fixture should produce a video rendition"); + let mut export = match live_start { + true => export.live(), + false => export, + }; + + let observed = kio::wait(|waiter| { + let _ = export.poll_read(waiter); + match live.track.subscription() { + Some(subscription) => Poll::Ready(subscription.max_delay), + None => Poll::Pending, + } + }) + .await; + + assert_eq!(observed, max_delay, "live start: {live_start}"); + } + } - assert_eq!(observed, max_delay); + /// A live start reads from the newest group, though the budget delivers older ones too. + #[tokio::test(start_paused = true)] + async fn a_live_start_reads_from_the_newest_group() { + use crate::container::test_util::raw_frame; + + let mut live = Live::audio(audio(None)); + for timestamp in [0, 24_000, 48_000] { + live.track.write(raw_frame(timestamp, b"audio", true)).unwrap(); + } + let mut export = ExportSource::for_audio( + &live.source().await, + live.track.name(), + &audio(None), + std::time::Duration::from_secs(10), + ) + .unwrap() + .unwrap() + .live(); + + let frame = kio::wait(|waiter| export.poll_read(waiter)).await.unwrap().unwrap(); + assert_eq!(frame.timestamp.as_micros(), 48_000); + live.track.write(raw_frame(72_000, b"audio", true)).unwrap(); + let frame = kio::wait(|waiter| export.poll_read(waiter)).await.unwrap().unwrap(); + assert_eq!(frame.timestamp.as_micros(), 72_000); + assert_eq!(export.skips(), 0, "nothing below the start counts as skipped"); } async fn audio_export() -> (moq_net::track::Producer, ExportSource) { use std::time::Duration; diff --git a/rs/moq-mux/src/container/ts/export_test.rs b/rs/moq-mux/src/container/ts/export_test.rs index 1d9d9c59b85..724c3c1bf93 100644 --- a/rs/moq-mux/src/container/ts/export_test.rs +++ b/rs/moq-mux/src/container/ts/export_test.rs @@ -5626,10 +5626,9 @@ async fn tune_in_does_not_wait_on_dropped_audio() { assert!(!out.is_empty(), "the tune-in waited on audio it had dropped"); } -/// A join whose first group the publisher cuts short, as a zero-budget live-edge subscribe -/// is cut when the next group lands before the raised budget reaches it (moq-dev/moq#5565), -/// starts at the next group: nothing of the cut-off start goes out, the next keyframe leads -/// the audio, and the rest is intact. +/// A join whose first group the publisher cuts short (moq-dev/moq#5565) starts at the next +/// group: nothing of the cut-off start goes out, the next keyframe leads the audio, and the +/// rest is intact. #[tokio::test(start_paused = true)] async fn a_start_group_cut_short_starts_at_the_next() { use crate::container::Container as _; diff --git a/rs/moq-mux/src/jitter.rs b/rs/moq-mux/src/jitter.rs index 79369f3fae3..9cec7808f58 100644 --- a/rs/moq-mux/src/jitter.rs +++ b/rs/moq-mux/src/jitter.rs @@ -77,9 +77,9 @@ pub(crate) enum Push { /// /// A track starts on a whole group. Until a frame of it has gone out, a loss (a frame /// dropped here, or the rest of a group its source cut short) takes everything the track -/// holds with it, and the track starts at its next sync frame. Whole groups missing after one -/// that ended take nothing: a joiner whose zero-budget live-edge pick has the -/// publisher cut its group short starts at the next group rather than on the cut-off start. +/// holds with it, and the track starts at its next sync frame, so a joiner whose start group +/// was cut short starts at the next group rather than on the cut-off start. Whole groups +/// missing after one that ended take nothing. /// What goes before a track's first frame out is [`Buffer::trimmed`], not lost; what drops /// after it is [`Buffer::dropped`]. /// From 57f9d7497e7a80931dc3cfe1b9c84e6bcc6615cb Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 16 Oct 2026 22:35:23 -0700 Subject: [PATCH 2/2] test(ts): probe the whole capture when grading holes ffprobe stops describing streams after 5 MB. A capture whose first audio PES lands past that gets its stream type's guess (mp3 for MP2), and the second frame of that PES repeats the first's timestamp, which holes.py read as a 24 ms hole that the capture never had. Co-Authored-By: Claude Opus 5.5 --- test/ts/holes.py | 7 ++++++- test/ts/holes.test.py | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/test/ts/holes.py b/test/ts/holes.py index 564a5e63d80..7bec6d0bd3a 100755 --- a/test/ts/holes.py +++ b/test/ts/holes.py @@ -20,6 +20,7 @@ """ import json +import os import subprocess import sys from collections import Counter @@ -57,8 +58,12 @@ def cuts(source: Span, capture: Span, frame: Fraction) -> list[str]: def probe(path: str) -> dict[str, tuple[dict, list[int], Span]]: """Each audio and video stream by PID: the stream, its decode timestamps, and its span.""" + # Probe the whole file, not FFmpeg's first 5 MB: a stream whose first frame comes after that + # is never described, so the second frame of its first PES repeats the first's timestamp + # and every later one in that PES runs a frame behind, which reads as a hole. report = json.loads(subprocess.run( - ["ffprobe", "-v", "error", "-show_entries", "stream=index,id,codec_type,codec_name,time_base:packet=stream_index,dts,pts", + ["ffprobe", "-v", "error", "-probesize", str(os.path.getsize(path)), "-analyzeduration", str(2**62), + "-show_entries", "stream=index,id,codec_type,codec_name,time_base:packet=stream_index,dts,pts", "-of", "json", path], check=True, capture_output=True, text=True, ).stdout) diff --git a/test/ts/holes.test.py b/test/ts/holes.test.py index ffe03f05b7a..c6923e9172d 100644 --- a/test/ts/holes.test.py +++ b/test/ts/holes.test.py @@ -1,5 +1,7 @@ import importlib.util import pathlib +import subprocess +import tempfile import unittest from fractions import Fraction @@ -57,5 +59,23 @@ def test_lost_prefix(self): capture = (Fraction(10), *self.SOURCE[1:]) self.assertEqual(len(holes.cuts(self.SOURCE, capture, self.FRAME)), 1) + +class Probe(unittest.TestCase): + def test_audio_starting_past_the_default_probe(self): + # Intra-only video ahead of an MP2 stream that starts 3 s in, past FFmpeg's default 5 MB + # probe: probed that far only, its first PES reads as a hole. + with tempfile.TemporaryDirectory() as tmp: + path = str(pathlib.Path(tmp, "late.ts")) + subprocess.run([ + "ffmpeg", "-v", "error", + "-f", "lavfi", "-i", "testsrc2=size=1280x720:rate=25:duration=4", + "-itsoffset", "3", "-f", "lavfi", "-i", "sine=sample_rate=48000:duration=1", + "-map", "0:v", "-map", "1:a", "-c:v", "mpeg2video", "-g", "1", "-q:v", "1", + "-c:a", "mp2", "-f", "mpegts", path, + ], check=True) + streams = holes.probe(path) + _, dts, _ = streams["0x101"] + self.assertEqual(holes.holes(dts, holes.step(dts)), []) + if __name__ == "__main__": unittest.main()