From 412557ec69d95d52f7b3ca9a15057ab4d614894b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 27 Nov 2026 22:47:39 -0800 Subject: [PATCH 1/6] chore(quest): claim quest/m1/ts-export-audio-rate.md Quest-Claim: abbfd3907579bd88 From e6467eb14eda52aee0b17b99d9cd8be277e4adc1 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 28 Nov 2026 01:28:11 -0800 Subject: [PATCH 2/6] fix(moq-mux): grow a TS export's audio send-ahead before its clock anchors A rate the import measures after the export's tables but before the jitter clock anchors found no margin yet and grew the send-ahead not at all, so a slow multiplex missed audio decode deadlines (ffmpeg_ac3.ts at a 2 s delay). Grow freely until the clock anchors, and use the spare since the anchor until a whole second has passed. Co-Authored-By: Claude Opus 5.5 --- quest/m1/ts-export-audio-rate.md | 45 ++++++++++++++-------- rs/moq-mux/src/container/ts/export.rs | 22 ++++++----- rs/moq-mux/src/container/ts/export_test.rs | 28 ++++++++++++++ rs/moq-mux/src/jitter.rs | 24 +++++++++++- 4 files changed, 92 insertions(+), 27 deletions(-) diff --git a/quest/m1/ts-export-audio-rate.md b/quest/m1/ts-export-audio-rate.md index 9c67485e5a8..131f8d69638 100644 --- a/quest/m1/ts-export-audio-rate.md +++ b/quest/m1/ts-export-audio-rate.md @@ -4,24 +4,37 @@ `moq export ts` of a source whose multiplex rate the import records only after the export's first output (a meter slower than the export's rate wait, or a rate that changes) never fails -with "MPEG-TS output missed a decode deadline" for its audio and drops no frame, at any -`--delay`. +with "MPEG-TS output missed a decode deadline" for its audio, and drops no frame an export +given the rate from the start would keep, at any `--delay`. ## Plan [moq-dev/moq#9636](https://github.com/moq-dev/moq/pull/9636) sends audio ahead in a padded multiplex as far as its decoder buffer fills at the multiplex rate (`audio_reach` in -`rs/moq-mux/src/container/ts/export.rs`), within half the delay, which carries every codec at the -recorded rate plus a packet a slot. A rate learned mid-stream grows that send-ahead only by the -jitter buffer's margin over the last second, and the tables carry it only once the frames keyed -for it are due (`Program::rate_from`). - -What remains: while the margin is still unknown (the first second of output), the send-ahead does -not grow at all, so a slow multiplex misses. `an_ffmpeg_audio_source_exports_at_its_recorded_rate` -with `Rate::Measured` and a 2 s delay reproduces it on `ffmpeg_ac3.ts`. A frame's jitter key is -fixed when it arrives, so a send-ahead that grows has to come out of what the frames have to -spare or move the clock's anchor. - -Decided 2026-11-26 (maintainer, /quest-run): let the send-ahead grow freely until the jitter -buffer releases its first frame, since its clock anchors on the latest track then, and check whether that anchor really stays -open until the first release. +`rs/moq-mux/src/container/ts/export.rs`), within half the delay. A rate learned mid-stream grows +that send-ahead only by the jitter buffer's spare (`jitter::Engine::spare_seen`), less a slot. +[moq-dev/moq#9839](https://github.com/moq-dev/moq/pull/9839) lets it grow freely until the clock +is anchored and uses the spare since the anchor in the first second. That fixed the long-delay +case (`ffmpeg_ac3.ts` at 2 s; the `ffmpeg_*.ts` sweep at 0.5 s, 2 s, and 3 s). + +What remains is short delays. Below FFmpeg's ~186 ms PES packing, the source already makes frames +miss their deadlines, so the spare is negative and the send-ahead cannot grow. The export then +fails fatally at the recorded rate, while one given that rate from the start only drops the late +frames. Measured with `paced_export` and `Rate::Measured`: + +- 100 ms: `aac`, `ac3`, and `mp2` fail with "missed a decode deadline … sent as far ahead as its + decoder buffer holds; raise the multiplex rate". `Rate::Given` drops 31, 38, and 49 frames. +- 300 ms: `mp2` fails (spare −60 ms); `Rate::Given` drops 8. +- 300 ms: `eac3` drops one frame near the fixture's end. The spare over the last second (76 ms) + overestimated what a later frame had, so the growth it allowed made that frame late. This + predates #9839. + +Lifting the cap entirely trades the fatal errors for drops at moderate delays (eac3 at 500 ms and +1 s), so it is not an option. + +## Waiting + +- Maintainer: When a rate is learned mid-stream at a delay too short for the source (frames already miss their deadlines, so the send-ahead cannot grow), what should the export do? + - Fail loud naming the delay (Recommended) - refuse with an error that the delay is shorter than the source sends ahead, instead of the misleading "raise the multiplex rate"; at these delays an export given the rate drops a third of the audio anyway + - Grow anyway once frames already miss - grow to what the rate needs when the spare is negative, matching the given-rate export's drops, at the cost of a rule that flips at zero spare + - Pad above the recorded rate - raise the padded rate to what the capped send-ahead needs, keeping every frame the source delivers on time but departing from the recorded CBR, often several times over diff --git a/rs/moq-mux/src/container/ts/export.rs b/rs/moq-mux/src/container/ts/export.rs index a1d49a23741..70149c1fbf3 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -2752,9 +2752,11 @@ where track.kind = kind; self.refresh(name, track, pid, entry.descriptors.clone()); } - // Every audio track follows the rate, a listed one too. Once the clock runs, a frame keyed - // further ahead has that much less of the delay to arrive in, so the send-ahead grows by - // no more than the frames have had to spare over the last second, less a slot. + // Every audio track follows the rate, a listed one too. Once the clock is anchored, a frame + // keyed further ahead has that much less of the delay to arrive in, so the send-ahead grows + // by no more than the frames have had to spare over the last second (or since the anchor, + // in the first), less a slot. Until then it grows freely, as before the tables: what it + // takes comes out of the delay, and no send-ahead reaches past half of it. // // Frames already keyed for less send-ahead would reach the new rate with too little of // it, so a rate that sends audio further ahead waits twice that send-ahead past the @@ -2762,13 +2764,15 @@ where // ([`Track::release_key`]), and the frames keyed before it are due by the time the first // of those goes out. let window = schedule::slot(self.program.delay.as_nanos()); - let spare = match self.program.psi { + // A replacement acquires its own clock beside the one playing, so it reads its own engine. + let seen = (self.program.psi.as_ref()) + .and(self.program.tracks.values().next()) + .and_then(|track| self.jitter.reading(&track.key).spare_seen()); + let spare = match seen { None => self.program.delay, - Some(_) => self - .jitter - .margin() - .and_then(|margin| Duration::try_from_secs_f64(margin).ok()) - .map_or(Duration::ZERO, |margin| margin.saturating_sub(PCR_INTERVAL)), + Some(seen) => { + Duration::try_from_secs_f64(seen).map_or(Duration::ZERO, |seen| seen.saturating_sub(PCR_INTERVAL)) + } }; let mut grown = Duration::ZERO; for track in self.program.tracks.values_mut() { diff --git a/rs/moq-mux/src/container/ts/export_test.rs b/rs/moq-mux/src/container/ts/export_test.rs index 09e60a8653c..341fd72a61a 100644 --- a/rs/moq-mux/src/container/ts/export_test.rs +++ b/rs/moq-mux/src/container/ts/export_test.rs @@ -13213,6 +13213,34 @@ async fn an_ffmpeg_audio_source_exports_at_its_recorded_rate() { assert!(Clocked::of(&ts).rate() > Clocked::of(data).rate() + crate::container::ts::schedule::PACKET); } +/// An FFmpeg TS of audio alone whose rate the import measures only after the export's tables, while +/// its clock is still acquired, keeps its audio on time at the rate with any delay that carries the +/// source: the send-ahead the rate needs grows before anything is due, since the anchor then gives +/// the track sent latest the delay. A delay shorter than the source's PES packing drops frames at any +/// rate, so it is not swept. +#[tokio::test(start_paused = true)] +async fn an_ffmpeg_audio_source_measured_while_its_clock_is_acquired_keeps_its_audio_on_time() { + for (name, data) in [ + ("aac", &include_bytes!("test_data/ffmpeg_aac.ts")[..]), + ("aac_pulse", include_bytes!("test_data/ffmpeg_aac_pulse.ts")), + ("ac3", include_bytes!("test_data/ffmpeg_ac3.ts")), + ("eac3", include_bytes!("test_data/ffmpeg_eac3.ts")), + ("mp2", include_bytes!("test_data/ffmpeg_mp2.ts")), + ("opus", include_bytes!("test_data/ffmpeg_opus.ts")), + ] { + let recorded = Clocked::of(data).rate(); + for delay in [500, 2000, 3000].map(Duration::from_millis) { + let (ts, dropped) = paced_export(data, Rate::Measured, delay).await; + assert_eq!(dropped, 0, "{name}, {delay:?}: every frame made its deadline"); + let out = Clocked::of(&ts).rate(); + assert!( + out > recorded, + "{name}, {delay:?}: padded to {out} b/s, no more than the recorded {recorded}" + ); + } + } +} + /// Where [`paced_export`] takes its multiplex rate from. #[derive(Clone, Copy, Debug)] enum Rate { diff --git a/rs/moq-mux/src/jitter.rs b/rs/moq-mux/src/jitter.rs index 2148460d78c..7a6f845dad7 100644 --- a/rs/moq-mux/src/jitter.rs +++ b/rs/moq-mux/src/jitter.rs @@ -117,7 +117,8 @@ impl Buffer { } } - fn reading(&self, key: &Q) -> &Engine + /// The engine `key`'s frames go to: a pending replacement's, else the active one. + pub fn reading(&self, key: &Q) -> &Engine where K: Borrow, { @@ -1669,6 +1670,13 @@ impl Engine { self.margin } + /// The least time a frame arrived before its deadline, in seconds: [`Self::margin`], or before + /// a second has passed, so far. `None` until a frame has a deadline, so while the clock is + /// still acquired, which anchors it on whatever the frames hold by then. + pub fn spare_seen(&self) -> Option { + self.margin.or(self.spare.map(|(_, spare)| spare)) + } + /// How far the clock sits off the source's plus the delay, in seconds, as last measured: /// positive holds the track sent latest longer than the delay, negative shorter. The /// clock pulls it shut over [`RESPONSE`]. `None` until the clock has measured the source's @@ -2177,17 +2185,24 @@ mod tests { } /// The margin is the least any frame had to spare over a whole second, a frame that missed - /// its deadline included, and holds until the next second has passed. + /// its deadline included, and holds until the next second has passed. Before a second has + /// passed, the spare seen is the least so far, and nothing while the clock is acquired. #[tokio::test(start_paused = true)] async fn the_margin_is_the_least_spare_over_a_second() { let start = Instant::now(); let at = |ms: u64| start.saturating_add(Duration::from_millis(ms)); + let mut acquiring = Buffer::new(DELAY); + assert_eq!(acquiring.push(1, arrival(at(0), 0, "v0")).unwrap(), Push::Queued); + assert_eq!(acquiring.spare_seen(), None, "no frame has a deadline yet"); + let mut buffer = Buffer::new(DELAY).replay(); assert_eq!(buffer.push(1, arrival(at(0), 0, "v0")).unwrap(), Push::Queued); assert_eq!(buffer.push(1, arrival(at(30), 40, "v40")).unwrap(), Push::Queued); // Due at 180 ms. assert_eq!(buffer.push(1, arrival(at(200), 80, "v80")).unwrap(), Push::Late); assert_eq!(buffer.margin(), None, "no second has passed"); + let seen = buffer.spare_seen().expect("frames have deadlines"); + assert!((seen + 0.020).abs() < 1e-6, "seen {seen} s"); assert_eq!( buffer.push(1, arrival(at(1_000), 1_000, "v1000")).unwrap(), @@ -2195,6 +2210,11 @@ mod tests { ); let margin = buffer.margin().expect("a second has passed"); assert!((margin + 0.020).abs() < 1e-6, "margin {margin} s"); + assert_eq!( + buffer.spare_seen(), + Some(margin), + "the margin, once a second has passed" + ); assert_eq!( buffer.push(1, arrival(at(1_500), 1_500, "v1500")).unwrap(), From c74c895049e0f57d45e1db5f70ae792b568425c7 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 28 Nov 2026 09:06:55 -0800 Subject: [PATCH 3/6] fix(moq-mux): refuse a mid-stream TS rate at a delay shorter than the source sends audio ahead When the rate arrives after the clock anchors and the frames have no slot to spare, the send-ahead cannot grow, and the export used to fail later with a misleading "raise the multiplex rate". It now refuses at once, naming the delay. Co-Authored-By: Claude Opus 5.5 --- doc/bin/ts.md | 5 ++- quest/m1/ts-export-audio-rate.md | 46 ++++++++-------------- rs/moq-mux/src/container/ts/export.rs | 18 ++++++++- rs/moq-mux/src/container/ts/export_test.rs | 26 +++++++++++- 4 files changed, 61 insertions(+), 34 deletions(-) diff --git a/doc/bin/ts.md b/doc/bin/ts.md index e9a79d206e3..30f7ffd20b5 100644 --- a/doc/bin/ts.md +++ b/doc/bin/ts.md @@ -134,7 +134,10 @@ padded multiplex sends audio up to half of `--delay` ahead, spreading it across slots, and a source that sends its audio only at its decode time adds that much latency. A frame that cannot arrive in time at the rate fails the export, as does video whose HRD declares a longer buffer delay than `--delay`; the error -names the delay it needs. +names the delay it needs. So does a rate the import measures only after the +export started, when `--delay` is shorter than the source sends its audio +ahead (FFmpeg's default packs about 186 ms a PES), leaving audio no room to go +out further ahead for it. ## Passthrough mode diff --git a/quest/m1/ts-export-audio-rate.md b/quest/m1/ts-export-audio-rate.md index 131f8d69638..17f514b4301 100644 --- a/quest/m1/ts-export-audio-rate.md +++ b/quest/m1/ts-export-audio-rate.md @@ -1,11 +1,10 @@ -# [M] A TS export that learns its rate mid-stream keeps its audio on time +# [S] A TS export that learns its rate mid-stream keeps its audio on time ## Goal `moq export ts` of a source whose multiplex rate the import records only after the export's -first output (a meter slower than the export's rate wait, or a rate that changes) never fails -with "MPEG-TS output missed a decode deadline" for its audio, and drops no frame an export -given the rate from the start would keep, at any `--delay`. +first output drops no frame that an export given the rate from the start would keep, at any +`--delay` the source's audio fits in. ## Plan @@ -13,28 +12,17 @@ given the rate from the start would keep, at any `--delay`. multiplex as far as its decoder buffer fills at the multiplex rate (`audio_reach` in `rs/moq-mux/src/container/ts/export.rs`), within half the delay. A rate learned mid-stream grows that send-ahead only by the jitter buffer's spare (`jitter::Engine::spare_seen`), less a slot. -[moq-dev/moq#9839](https://github.com/moq-dev/moq/pull/9839) lets it grow freely until the clock -is anchored and uses the spare since the anchor in the first second. That fixed the long-delay -case (`ffmpeg_ac3.ts` at 2 s; the `ffmpeg_*.ts` sweep at 0.5 s, 2 s, and 3 s). - -What remains is short delays. Below FFmpeg's ~186 ms PES packing, the source already makes frames -miss their deadlines, so the spare is negative and the send-ahead cannot grow. The export then -fails fatally at the recorded rate, while one given that rate from the start only drops the late -frames. Measured with `paced_export` and `Rate::Measured`: - -- 100 ms: `aac`, `ac3`, and `mp2` fail with "missed a decode deadline … sent as far ahead as its - decoder buffer holds; raise the multiplex rate". `Rate::Given` drops 31, 38, and 49 frames. -- 300 ms: `mp2` fails (spare −60 ms); `Rate::Given` drops 8. -- 300 ms: `eac3` drops one frame near the fixture's end. The spare over the last second (76 ms) - overestimated what a later frame had, so the growth it allowed made that frame late. This - predates #9839. - -Lifting the cap entirely trades the fatal errors for drops at moderate delays (eac3 at 500 ms and -1 s), so it is not an option. - -## Waiting - -- Maintainer: When a rate is learned mid-stream at a delay too short for the source (frames already miss their deadlines, so the send-ahead cannot grow), what should the export do? - - Fail loud naming the delay (Recommended) - refuse with an error that the delay is shorter than the source sends ahead, instead of the misleading "raise the multiplex rate"; at these delays an export given the rate drops a third of the audio anyway - - Grow anyway once frames already miss - grow to what the rate needs when the spare is negative, matching the given-rate export's drops, at the cost of a rule that flips at zero spare - - Pad above the recorded rate - raise the padded rate to what the capped send-ahead needs, keeping every frame the source delivers on time but departing from the recorded CBR, often several times over +[moq-dev/moq#9839](https://github.com/moq-dev/moq/pull/9839) made two changes: +- The send-ahead grows freely until the clock is anchored, then by the spare seen since the anchor. +- At a delay shorter than the source sends its audio ahead, a rate learned mid-stream is refused + with an error naming the delay (maintainer decision, 2026-11-28). + +What remains: `ffmpeg_eac3.ts` with `Rate::Measured` at a 300 ms delay drops one frame (decode +4088 ms) that `Rate::Given` keeps. The rate arrives about 1.8 s in, and the spare then (76 ms) +allows 51 ms of growth. The dropped frame shares an export PES with an earlier frame, so it takes +that frame's key (`Track::release_key`) and has less spare than any frame in the second before. +The likely cause is that FFmpeg's 186 ms PES cadence beats against the export's ~160 ms PES span +over more than the spare's one-second window. Taking the least spare over two seconds did not help, +since only one second had passed when the rate arrived. Check that hypothesis first, for example by +bounding growth by the spare less the PES span that `Track::pack_start` gives at the delay. +Reproduce it with a `paced_export` sweep over the `ffmpeg_*.ts` fixtures. diff --git a/rs/moq-mux/src/container/ts/export.rs b/rs/moq-mux/src/container/ts/export.rs index 70149c1fbf3..881b404881c 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -2775,7 +2775,7 @@ where } }; let mut grown = Duration::ZERO; - for track in self.program.tracks.values_mut() { + for (name, track) in self.program.tracks.iter_mut() { if let Some(buffer) = track .buffer .as_mut() @@ -2788,11 +2788,25 @@ where } let before = audio_reach(buffer, previous, self.program.delay).min(buffer.reach); let reach = audio_reach(buffer, self.program.mux_rate, self.program.delay); - buffer.reach = reach.min(before + spare); let before = Buffer { reach: before, ..*buffer }; + // Frames with no slot to spare leave the send-ahead nowhere to grow, so the rate would + // miss the audio's deadlines: the delay is shorter than the source sends it ahead. + let wanted = Buffer { reach, ..*buffer }; + if let Some(seen) = seen.filter(|_| spare.is_zero()) + && schedule::ahead(Some(&wanted), window) > schedule::ahead(Some(&before), window) + { + bail_unsupported!( + "MPEG-TS multiplex rate {} b/s arrived mid-stream, but the {:?} delay is shorter than the source sends audio track '{name}' ahead: its frames had {:.0} ms to spare, less than the {:?} slot sending it ahead for the rate takes; raise the delay", + self.program.mux_rate.unwrap_or_default(), + self.program.delay, + seen * 1e3, + PCR_INTERVAL, + ); + } + buffer.reach = reach.min(before.reach + spare); if schedule::ahead(Some(buffer), window) > schedule::ahead(Some(&before), window) { grown = grown.max(buffer.reach); } diff --git a/rs/moq-mux/src/container/ts/export_test.rs b/rs/moq-mux/src/container/ts/export_test.rs index 341fd72a61a..3ca5ef91d9a 100644 --- a/rs/moq-mux/src/container/ts/export_test.rs +++ b/rs/moq-mux/src/container/ts/export_test.rs @@ -13241,6 +13241,23 @@ async fn an_ffmpeg_audio_source_measured_while_its_clock_is_acquired_keeps_its_a } } +/// A rate measured mid-stream at a delay shorter than the source sends its audio ahead leaves the +/// send-ahead nowhere to grow, so the export refuses it naming the delay: FFmpeg packs 186 ms of +/// audio a PES, which a 100 ms delay cannot hold. +#[tokio::test(start_paused = true)] +async fn a_rate_measured_at_a_delay_shorter_than_the_source_sends_ahead_names_the_delay() { + let data = include_bytes!("test_data/ffmpeg_ac3.ts"); + let err = try_paced_export(data, Rate::Measured, Duration::from_millis(100)) + .await + .expect_err("the rate cannot carry the audio") + .to_string(); + assert!( + err.contains("the 100ms delay is shorter than the source sends"), + "{err}" + ); + assert!(!err.contains("raise the multiplex rate"), "{err}"); +} + /// Where [`paced_export`] takes its multiplex rate from. #[derive(Clone, Copy, Debug)] enum Rate { @@ -13256,6 +13273,11 @@ enum Rate { /// it at `rate` and `delay` from the first stretch on until 4 s of media. Returns the output and the frames it /// dropped. async fn paced_export(data: &[u8], rate: Rate, delay: Duration) -> (BytesMut, u64) { + try_paced_export(data, rate, delay).await.expect("exporter error") +} + +/// [`paced_export`], returning the export's error rather than failing on it. +async fn try_paced_export(data: &[u8], rate: Rate, delay: Duration) -> Result<(BytesMut, u64), crate::Error> { let mut broadcast = moq_net::broadcast::Info::new().produce(); let consumer = broadcast.consume(); let catalog = crate::catalog::Producer::new( @@ -13289,13 +13311,13 @@ async fn paced_export(data: &[u8], rate: Rate, delay: Duration) -> (BytesMut, u6 }; let mut ts = BytesMut::new(); // The catalog stays open, so the export pads on: stop with the media. - while let Some(frame) = export.next().await.expect("exporter error") { + while let Some(frame) = export.next().await? { ts.extend_from_slice(&frame.payload); if frame.timestamp >= Timestamp::from_secs(4).unwrap() { break; } } - (ts, export.dropped()) + Ok((ts, export.dropped())) })); } } From 8b9e45ac445ecd50692d195fa9820be148a5624d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 28 Nov 2026 09:12:25 -0800 Subject: [PATCH 4/6] quest(m3): plan moq-cli's ietf22 identities test failing on macOS Co-Authored-By: Claude Opus 5.5 --- quest/m3/cli-identities-ietf22.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 quest/m3/cli-identities-ietf22.md diff --git a/quest/m3/cli-identities-ietf22.md b/quest/m3/cli-identities-ietf22.md new file mode 100644 index 00000000000..ba52d073226 --- /dev/null +++ b/quest/m3/cli-identities-ietf22.md @@ -0,0 +1,18 @@ +# [S] moq-cli's ietf22 identities test reads its group + +## Goal + +`cargo nextest run -p moq-cli --test integration identities::ietf22` passes on every host +`just check` runs on, like its `lite06` and `lite07` siblings. + +## Plan + +On an Apple Silicon macOS host, it fails deterministically on `origin/main` 93523cf38c (five runs +of five). `recv_group` in `read` (`rs/moq-cli/tests/integration/identities.rs:64`) returns +`Err(Old)`. The merge queue's runs on main pass, so first find out what differs between the hosts: +the platform, or timing between the relay and the group's write. Reproduce it, then fix the root +cause in `moq-net`'s IETF path or in the test, whichever is wrong. + +## Hosts + +- macos - where it fails From a9f13bf819f7d82789cf237e85076a3dc31773ff Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 28 Nov 2026 09:19:04 -0800 Subject: [PATCH 5/6] fix(moq-mux): refuse a mid-stream TS rate only on a sustained short delay Refuse only when the rate itself changed and a whole second's frames had less than a slot to spare, so a republished catalog, a joining rendition or one late frame never stops the export. Read the replacement's clock by the program's role. Co-Authored-By: Claude Opus 5.5 --- rs/moq-mux/src/container/ts/export.rs | 25 ++++++++++++++-------- rs/moq-mux/src/container/ts/export_test.rs | 3 ++- rs/moq-mux/src/jitter.rs | 8 +++++-- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/rs/moq-mux/src/container/ts/export.rs b/rs/moq-mux/src/container/ts/export.rs index 881b404881c..ecf135628d3 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -2765,15 +2765,22 @@ where // of those goes out. let window = schedule::slot(self.program.delay.as_nanos()); // A replacement acquires its own clock beside the one playing, so it reads its own engine. - let seen = (self.program.psi.as_ref()) - .and(self.program.tracks.values().next()) - .and_then(|track| self.jitter.reading(&track.key).spare_seen()); - let spare = match seen { + let engine = match self.program.id == self.active_program { + true => Some(&*self.jitter), + false => self.jitter.pending_engine(), + }; + let engine = engine.filter(|_| self.program.psi.is_some()); + let spare = match engine.and_then(|engine| engine.spare_seen()) { None => self.program.delay, Some(seen) => { Duration::try_from_secs_f64(seen).map_or(Duration::ZERO, |seen| seen.saturating_sub(PCR_INTERVAL)) } }; + // A whole second whose frames all had less than a slot to spare: the delay is shorter than + // the source sends its audio ahead, not a moment's late arrival. + let short = engine + .and_then(|engine| engine.margin()) + .filter(|&margin| margin < PCR_INTERVAL.as_secs_f64()); let mut grown = Duration::ZERO; for (name, track) in self.program.tracks.iter_mut() { if let Some(buffer) = track @@ -2792,17 +2799,17 @@ where reach: before, ..*buffer }; - // Frames with no slot to spare leave the send-ahead nowhere to grow, so the rate would - // miss the audio's deadlines: the delay is shorter than the source sends it ahead. + // A rate that needs the audio further ahead than such a delay lets it go would miss its + // deadlines. let wanted = Buffer { reach, ..*buffer }; - if let Some(seen) = seen.filter(|_| spare.is_zero()) + if let Some(margin) = short.filter(|_| self.program.mux_rate != previous) && schedule::ahead(Some(&wanted), window) > schedule::ahead(Some(&before), window) { bail_unsupported!( - "MPEG-TS multiplex rate {} b/s arrived mid-stream, but the {:?} delay is shorter than the source sends audio track '{name}' ahead: its frames had {:.0} ms to spare, less than the {:?} slot sending it ahead for the rate takes; raise the delay", + "MPEG-TS multiplex rate {} b/s arrived mid-stream, but the {:?} delay is shorter than the source sends audio track '{name}' ahead: its frames had {:.0} ms to spare over the last second, less than the {:?} slot sending it ahead for the rate takes; raise the delay", self.program.mux_rate.unwrap_or_default(), self.program.delay, - seen * 1e3, + margin * 1e3, PCR_INTERVAL, ); } diff --git a/rs/moq-mux/src/container/ts/export_test.rs b/rs/moq-mux/src/container/ts/export_test.rs index 3ca5ef91d9a..a644aac4412 100644 --- a/rs/moq-mux/src/container/ts/export_test.rs +++ b/rs/moq-mux/src/container/ts/export_test.rs @@ -13217,7 +13217,8 @@ async fn an_ffmpeg_audio_source_exports_at_its_recorded_rate() { /// its clock is still acquired, keeps its audio on time at the rate with any delay that carries the /// source: the send-ahead the rate needs grows before anything is due, since the anchor then gives /// the track sent latest the delay. A delay shorter than the source's PES packing drops frames at any -/// rate, so it is not swept. +/// rate, so it is not swept; at 300 ms, `eac3` still drops a frame the given rate keeps +/// (`quest/m1/ts-export-audio-rate.md`). #[tokio::test(start_paused = true)] async fn an_ffmpeg_audio_source_measured_while_its_clock_is_acquired_keeps_its_audio_on_time() { for (name, data) in [ diff --git a/rs/moq-mux/src/jitter.rs b/rs/moq-mux/src/jitter.rs index 7a6f845dad7..d73be8fc905 100644 --- a/rs/moq-mux/src/jitter.rs +++ b/rs/moq-mux/src/jitter.rs @@ -86,6 +86,11 @@ impl Buffer { _ => None, } } + /// The engine a replacement acquires its clock on, while it is pending. + pub fn pending_engine(&self) -> Option<&Engine> { + self.pending().map(|pending| &pending.engine) + } + fn committed(&self) -> Option<&Committed> { match self.handoff.as_deref() { Some(Handoff::Committed(committed)) => Some(committed), @@ -117,8 +122,7 @@ impl Buffer { } } - /// The engine `key`'s frames go to: a pending replacement's, else the active one. - pub fn reading(&self, key: &Q) -> &Engine + fn reading(&self, key: &Q) -> &Engine where K: Borrow, { From 176eada47029ae15fe11d8c3df19d09514e836d4 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 28 Nov 2026 09:25:26 -0800 Subject: [PATCH 6/6] fix(moq-mux): read a committed replacement's spare from its promoted clock Co-Authored-By: Claude Opus 5.5 --- rs/moq-mux/src/container/ts/export.rs | 6 +++--- rs/moq-mux/src/jitter.rs | 10 +++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/rs/moq-mux/src/container/ts/export.rs b/rs/moq-mux/src/container/ts/export.rs index ecf135628d3..0d26364bd1c 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -2767,7 +2767,7 @@ where // A replacement acquires its own clock beside the one playing, so it reads its own engine. let engine = match self.program.id == self.active_program { true => Some(&*self.jitter), - false => self.jitter.pending_engine(), + false => self.jitter.replacement_engine(), }; let engine = engine.filter(|_| self.program.psi.is_some()); let spare = match engine.and_then(|engine| engine.spare_seen()) { @@ -2776,8 +2776,8 @@ where Duration::try_from_secs_f64(seen).map_or(Duration::ZERO, |seen| seen.saturating_sub(PCR_INTERVAL)) } }; - // A whole second whose frames all had less than a slot to spare: the delay is shorter than - // the source sends its audio ahead, not a moment's late arrival. + // The least spare over a whole second below a slot: the delay is shorter than the source + // sends its audio ahead, not a moment's late arrival. let short = engine .and_then(|engine| engine.margin()) .filter(|&margin| margin < PCR_INTERVAL.as_secs_f64()); diff --git a/rs/moq-mux/src/jitter.rs b/rs/moq-mux/src/jitter.rs index d73be8fc905..d3355a9e36c 100644 --- a/rs/moq-mux/src/jitter.rs +++ b/rs/moq-mux/src/jitter.rs @@ -86,9 +86,13 @@ impl Buffer { _ => None, } } - /// The engine a replacement acquires its clock on, while it is pending. - pub fn pending_engine(&self) -> Option<&Engine> { - self.pending().map(|pending| &pending.engine) + /// The engine a replacement's clock runs on: its own while it acquires, the active one once + /// committed. `None` before a replacement starts. + pub fn replacement_engine(&self) -> Option<&Engine> { + match self.handoff.as_deref()? { + Handoff::Pending(pending) => Some(&pending.engine), + Handoff::Committed(_) => Some(&self.active), + } } fn committed(&self) -> Option<&Committed> {