From c80ddd5612b16cb482f6ed262bf1ba3ef7172319 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 02:02:41 -0800 Subject: [PATCH 1/6] chore(quest): claim quest/m2/whep-pull-announce.md Quest-Claim: 87e809c6336d3145 From 9870f30a68020a05dc9be42d12473c021e787aae Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 02:18:59 -0800 Subject: [PATCH 2/6] wip: a WHEP pull announces once its catalog exists --- rs/moq-cli/src/webrtc.rs | 67 +++++++++++++++++++++++++++----- rs/moq-webrtc/src/client/mod.rs | 23 ++++++----- rs/moq-webrtc/src/client/whep.rs | 4 +- rs/moq-webrtc/src/ingest.rs | 33 ++++++++-------- rs/moq-webrtc/src/lib.rs | 10 +++-- 5 files changed, 94 insertions(+), 43 deletions(-) diff --git a/rs/moq-cli/src/webrtc.rs b/rs/moq-cli/src/webrtc.rs index c860ad697e6..80e4684d57c 100644 --- a/rs/moq-cli/src/webrtc.rs +++ b/rs/moq-cli/src/webrtc.rs @@ -85,27 +85,38 @@ fn scope_producer(origin: &moq_net::origin::Producer, name: &str) -> anyhow::Res } /// WHEP client: pull a remote broadcast into the Origin under `target.name` (import), -/// announced under `instance`. +/// announced under `instance` once its catalog names a track. pub async fn connect_import(target: ImportTarget, url: Url, instance: moq_net::Instance) -> anyhow::Result<()> { let name = &target.name; - // The WHEP pull fills the tracks as they arrive; announce up front so viewers - // can discover the broadcast while it connects. Held until the pull ends. + // Hidden until the catalog exists: a viewer or export that resolved the broadcast earlier + // would find no catalog track. Held until the pull ends. let route = target .origin .route( name, - moq_net::origin::RouteOptions::default().with_instance(instance, 0), + moq_net::origin::RouteOptions::hidden().with_instance(instance, 0), ) .context("failed to create broadcast")?; + let catalog = moq_mux::catalog::Producer::new(&mut route.broadcast().clone(), target.ingest.catalog())?; + let client = moq_webrtc::Client::new(moq_webrtc::client::Config::default())?; tracing::info!(url = %RedactedUrl::new(&url), %name, "WHEP client pulling"); notify_ready(); - let client = moq_webrtc::Client::new(moq_webrtc::client::Config::default())?; - client - .pull(url, route.broadcast().clone(), target.ingest.catalog()) - .await?; - Ok(()) + let announce = async { + let mut updates = catalog.consume()?; + // The pull ends the catalog when its session ends, and reports why itself. + if let Ok(Some(_)) = updates.next().await { + route.announce().context("failed to announce broadcast")?; + tracing::info!(%name, "publishing WHEP broadcast"); + } + std::future::pending::>().await + }; + + tokio::select! { + end = client.pull(url, route.broadcast(), &catalog) => Ok(end?), + err = announce => err, + } } /// WHIP client: push a broadcast from the Origin to a remote (export). @@ -149,3 +160,41 @@ async fn serve(router: axum::Router, role: &str, listen: Listen) -> anyhow::Resu crate::web::serve(listener, app, None).await } + +#[cfg(test)] +mod tests { + use futures::FutureExt; + + use super::*; + + /// A WHEP pull keeps its broadcast hidden while the session negotiates, since nothing has + /// published its catalog yet, and never announces a pull that fails before then. + #[tokio::test] + async fn a_whep_pull_hides_its_broadcast_until_the_catalog_exists() { + let origin = moq_tokio::origin::spawn(); + let consumer = origin.consume(); + let mut announced = consumer.announced(); + // A WHEP endpoint that takes the offer and never answers it. + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("http://{}/whep", listener.local_addr().unwrap()) + .parse() + .unwrap(); + let target = ImportTarget { + origin, + name: "pulled".to_string(), + ingest: moq_mux::Ingest::default(), + }; + let pull = tokio::spawn(connect_import(target, url, moq_net::Instance::random())); + + let (offer, _) = listener.accept().await.unwrap(); + assert!( + announced.next().now_or_never().is_none(), + "announced before the catalog exists" + ); + + drop(offer); + pull.await.unwrap().expect_err("the endpoint hung up without an answer"); + // The origin ends with the pull, so the only announcement left would be the pull's. + assert!(announced.next().now_or_never().flatten().is_none(), "announced a failed pull"); + } +} diff --git a/rs/moq-webrtc/src/client/mod.rs b/rs/moq-webrtc/src/client/mod.rs index ba18de44886..1913a0d6621 100644 --- a/rs/moq-webrtc/src/client/mod.rs +++ b/rs/moq-webrtc/src/client/mod.rs @@ -50,18 +50,19 @@ impl Client { &self.http } - /// `client pull`: ingest a remote WHEP feed and publish it as `broadcast`, its tracks - /// minted under `catalog`, until the session ends. + /// `client pull`: ingest a remote WHEP feed into `broadcast`, publishing its renditions + /// on `catalog`, until the session ends. /// - /// The caller owns the broadcast, so its announcement and any linger after the end are - /// the caller's too. + /// The catalog gets its first update once the session negotiates a track. The caller owns + /// the broadcast, so its announcement and any linger after the end are the caller's too. + /// Any end, clean or not, aborts the catalog and every track with its cause. pub async fn pull( &self, url: Url, - broadcast: moq_net::broadcast::Producer, - catalog: moq_mux::catalog::Config, + broadcast: &moq_net::broadcast::Producer, + catalog: &moq_mux::catalog::Producer, ) -> crate::Result<()> { - whep::dial(self, url, broadcast, catalog).await + whep::dial(self, url, broadcast.clone(), catalog.clone()).await } /// `client publish`: push the broadcast `source` resolved to a remote WHIP endpoint, @@ -86,16 +87,14 @@ mod tests { let address = listener.local_addr().expect("local address"); drop(listener); - let origin = moq_tokio::origin::spawn(); - let broadcast = origin - .route("test", moq_net::origin::RouteOptions::hidden()) - .expect("create broadcast"); + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = moq_mux::catalog::Producer::new(&mut broadcast, Default::default()).unwrap(); let url = format!("http://user:pass@{address}/whep/test?jwt=secret") .parse() .expect("WHEP URL"); let err = Client::new(Config::default()) .unwrap() - .pull(url, broadcast.broadcast().clone(), Default::default()) + .pull(url, &broadcast, &catalog) .await .expect_err("dial a closed port"); diff --git a/rs/moq-webrtc/src/client/whep.rs b/rs/moq-webrtc/src/client/whep.rs index fe58215d65d..a4050a4a9c7 100644 --- a/rs/moq-webrtc/src/client/whep.rs +++ b/rs/moq-webrtc/src/client/whep.rs @@ -19,9 +19,9 @@ pub(crate) async fn dial( client: &Client, url: Url, broadcast: moq_net::broadcast::Producer, - catalog: moq_mux::catalog::Config, + catalog: moq_mux::catalog::Producer, ) -> Result<()> { - let sink = Box::new(IngestSink::new(broadcast, catalog)?); + let sink = Box::new(IngestSink::new(broadcast, catalog)); let socket = session::bind_udp().await?; let mut rtc = session::rtc_config()?.build(kio::clock::now().into_std()); diff --git a/rs/moq-webrtc/src/ingest.rs b/rs/moq-webrtc/src/ingest.rs index 15b3c1e0101..4de41862f59 100644 --- a/rs/moq-webrtc/src/ingest.rs +++ b/rs/moq-webrtc/src/ingest.rs @@ -18,22 +18,24 @@ pub struct IngestSink { } impl IngestSink { - /// Fill `broadcast`, whose announcement the caller owns. - pub fn new(mut broadcast: moq_net::broadcast::Producer, config: moq_mux::catalog::Config) -> Result { - let catalog = moq_mux::catalog::Producer::new(&mut broadcast, config)?; - Ok(Self { + /// Fill `broadcast` and its `catalog`, whose announcement the caller owns. + pub fn new(broadcast: moq_net::broadcast::Producer, catalog: moq_mux::catalog::Producer) -> Self { + Self { broadcast, catalog, bridges: session::Bridges::new(), _route: None, - }) + } } - /// Fill `route`'s broadcast, keeping the route for the session's life. + /// Fill `route`'s broadcast under a catalog minted from `config`, keeping the route for + /// the session's life. pub fn routed(route: moq_net::origin::Route, config: moq_mux::catalog::Config) -> Result { + let mut broadcast = route.broadcast().clone(); + let catalog = moq_mux::catalog::Producer::new(&mut broadcast, config)?; Ok(Self { - _route: Some(route.clone()), - ..Self::new(route.broadcast().clone(), config)? + _route: Some(route), + ..Self::new(broadcast, catalog) }) } } @@ -104,12 +106,10 @@ mod tests { /// bridges mint, not just on the catalog producer it was set on. #[tokio::test] async fn tracks_inherit_the_configured_max_age() { - let broadcast = moq_net::broadcast::Info::new().produce(); - let mut sink = IngestSink::new( - broadcast.clone(), - moq_mux::catalog::Config::default().with_max_age(Duration::from_secs(3)), - ) - .unwrap(); + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let config = moq_mux::catalog::Config::default().with_max_age(Duration::from_secs(3)); + let catalog = moq_mux::catalog::Producer::new(&mut broadcast, config).unwrap(); + let mut sink = IngestSink::new(broadcast.clone(), catalog); sink.on_track( "0".into(), @@ -127,9 +127,10 @@ mod tests { /// included, so the held sink serves them to a late subscriber for the linger. #[tokio::test] async fn finish_keeps_every_track_for_the_linger() { - let broadcast = moq_net::broadcast::Info::new().produce(); + let mut broadcast = moq_net::broadcast::Info::new().produce(); let consumer = broadcast.consume(); - let mut sink = IngestSink::new(broadcast, moq_mux::catalog::Config::default()).unwrap(); + let catalog = moq_mux::catalog::Producer::new(&mut broadcast, Default::default()).unwrap(); + let mut sink = IngestSink::new(broadcast, catalog); let audio = str0m::media::MediaKind::Audio; sink.on_track("0".into(), audio, str0m::format::Codec::Opus, Some((48_000, 2))) .unwrap(); diff --git a/rs/moq-webrtc/src/lib.rs b/rs/moq-webrtc/src/lib.rs index f66e992365e..58dddccf3e7 100644 --- a/rs/moq-webrtc/src/lib.rs +++ b/rs/moq-webrtc/src/lib.rs @@ -225,8 +225,9 @@ mod tests { let output_consumer = output.broadcast().consume(); let client = Client::new(client::Config::default()).unwrap(); let url = format!("http://{address}/whep/source").parse().expect("WHEP URL"); - let fill = output.broadcast().clone(); - let viewer = tokio::spawn(async move { client.pull(url, fill, Default::default()).await }); + let mut fill = output.broadcast().clone(); + let catalog = moq_mux::catalog::Producer::new(&mut fill, Default::default()).unwrap(); + let viewer = tokio::spawn(async move { client.pull(url, &fill, &catalog).await }); let session = kio::clock::timeout(TIMEOUT, sessions.recv()) .await .expect("WHEP negotiation timed out") @@ -554,8 +555,9 @@ mod tests { let whep = format!("http://user:pass@{address}/whep/ingested?jwt=canary-7f3e") .parse() .expect("WHEP URL"); - let fill = output.broadcast().clone(); - let whep_client = tokio::spawn(async move { client.pull(whep, fill, Default::default()).await }); + let mut fill = output.broadcast().clone(); + let catalog = moq_mux::catalog::Producer::new(&mut fill, Default::default()).unwrap(); + let whep_client = tokio::spawn(async move { client.pull(whep, &fill, &catalog).await }); let (whep_id, whep_task) = kio::clock::timeout(TIMEOUT, sources_rx.recv()) .await .expect("WHEP negotiation timed out") From b248bbc2dee4b8931dd1c72ed1a8361ab2965ef1 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 02:54:50 -0800 Subject: [PATCH 3/6] fix(moq-webrtc): a WHEP ingest waits for its first keyframe and drops late frames Co-Authored-By: Claude Opus 5.5 --- quest/m2/README.md | 3 +- quest/m2/whep-pull-announce.md | 32 ------- rs/moq-webrtc/src/session.rs | 156 +++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 34 deletions(-) delete mode 100644 quest/m2/whep-pull-announce.md diff --git a/quest/m2/README.md b/quest/m2/README.md index 265072e76d1..baee58b1ac2 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -55,8 +55,7 @@ removal, a rename, or a wire, catalog, or format change that is free until [moq-json writes](/quest/m2/json-write-at.md). - **Native media and gateways.** [The video producer](/quest/m2/video-producer.md), [the hole wait](/quest/m2/delay-hole-wait.md), - [moq-vaapi's exports](/quest/m2/vaapi-exports.md), - [the WHEP pull's announcement](/quest/m2/whep-pull-announce.md), and + [moq-vaapi's exports](/quest/m2/vaapi-exports.md), and [upstream str0m](https://github.com/moq-dev/moq/pull/8963), which reverses the 2026-10-30 vendoring. - **JS and browser.** [`@moq/net`](/quest/m2/js-net-api.md), diff --git a/quest/m2/whep-pull-announce.md b/quest/m2/whep-pull-announce.md deleted file mode 100644 index fcdeb28404c..00000000000 --- a/quest/m2/whep-pull-announce.md +++ /dev/null @@ -1,32 +0,0 @@ -# [S] A WHEP pull announces its broadcast once the catalog exists - -## Goal - -`moq import webrtc --connect` announces its broadcast only once the -broadcast has a catalog, so an export in the same process, or a viewer that -arrives first, never resolves the broadcast and then gets `not found` for -its catalog. A WHEP pull chained to another gateway in one process -(`import webrtc --connect -- export rtmp --connect `) restreams -instead of failing at startup. - -## Plan - -Found 2026-11-16 while validating the gateway admission change on the built -binary: with both stages starting together, `export rtmp --connect` failed -with `init FLV export: moq: not found`, `export rtmp --listen` refused a -player with `init catalog check: moq: not found`, and `export srt --connect` -ended at once. Each export resolved the broadcast before its catalog track -existed. - -The cause: `rs/moq-cli/src/webrtc.rs` `connect_import` creates the route -already announced, "so viewers can discover the broadcast while it -connects", and only `moq_webrtc::Client::pull` creates the catalog track -(`IngestSink::new` in `rs/moq-webrtc/src/client/whep.rs`), so the CLI has -nothing to wait on. - -Recommended: `Client::pull` takes the catalog producer, as -`moq_rtsp::Client::pull` does, and the CLI creates the route hidden and -announces it after the catalog's first update, the way -`rs/moq-cli/src/rtsp.rs` does. moq-webrtc is unpublished, so the signature -change is free before the cut, which is why this is m2. A viewer that arrives before the WHEP session -negotiates then waits on the announcement instead of failing. diff --git a/rs/moq-webrtc/src/session.rs b/rs/moq-webrtc/src/session.rs index 7d2df1489a3..4557943b22b 100644 --- a/rs/moq-webrtc/src/session.rs +++ b/rs/moq-webrtc/src/session.rs @@ -33,6 +33,10 @@ const EGRESS_SEND_BUFFER_VIDEO: usize = 3000; /// at most this long, and an encoder that drifted up is pulled back. const REMB_INTERVAL: Duration = Duration::from_secs(1); +/// How often an ingest repeats its keyframe request while a video line waits on its first +/// keyframe, so a lost PLI costs at most this long rather than a whole GOP. +const KEYFRAME_REQUEST_INTERVAL: Duration = Duration::from_secs(1); + /// Receives `MediaData` events from str0m and dispatches to the right codec /// [`Bridge`](codec::Bridge). Used as the per-session sink in [`Session::run`] /// for any flow where RTP arrives from the peer (`server publish` / WHIP @@ -112,6 +116,8 @@ pub struct Session { /// The ingest's cap, metered and steered through REMB. `None` until /// [`Session::with_rate`], so always for egress sessions. rate: Option, + /// Which ingested frames may reach the sink. Unused by egress sessions. + ingest_order: IngestOrder, } impl Session { @@ -127,6 +133,7 @@ impl Session { ingest_clock: IngestClock::default(), egress_clock: EgressClock::default(), rate: None, + ingest_order: IngestOrder::default(), }) } @@ -144,6 +151,7 @@ impl Session { ingest_clock: IngestClock::default(), egress_clock: EgressClock::default(), rate: None, + ingest_order: IngestOrder::default(), }) } @@ -280,6 +288,21 @@ impl Session { } let media_us = media_time_to_micros(&data.time); let timestamp_us = self.ingest_clock.normalize(data.mid, arrival, media_us); + match self + .ingest_order + .admit(data.mid, data.is_keyframe(), timestamp_us, Instant::now()) + { + Admit::Forward => {} + Admit::Drop => return Ok(()), + Admit::Request => { + if let Some(mut writer) = self.agent.rtc_mut().writer(data.mid) { + writer + .request_keyframe(data.rid, str0m::media::KeyframeRequestKind::Pli) + .map_err(Error::rtc)?; + } + return Ok(()); + } + } sink.on_frame( data.mid, codec::Frame { @@ -357,6 +380,9 @@ impl Session { None }; sink.on_track(mid, kind, codec, audio_params)?; + let video = kind == str0m::media::MediaKind::Video; + let pli = negotiated.iter().any(|params| params.fb_pli()); + self.ingest_order.open(mid, video, pli, Instant::now()); // REMB only where the publisher negotiated it; str0m offers it on video alone. if let Some(rate) = &mut self.rate && negotiated.iter().any(|params| params.fb_remb()) @@ -432,6 +458,87 @@ impl IngestRate { } } +/// Which ingested frames may reach the sink, line by line. +/// +/// RTP can join a publisher mid-group, and deliver a packet late, as a retransmission does. A +/// frame that depends on a keyframe never seen cannot open a group, so a video line drops its +/// frames until its first keyframe, asking the publisher for one meanwhile where it negotiated +/// PLI. A late frame would rewind a group already published, so a line drops it too: audio, +/// whose every frame opens a group, anything not after its last frame, and video anything +/// before its last keyframe. +#[derive(Default)] +struct IngestOrder { + lines: HashMap, +} + +/// Where one ingested line stands. +enum IngestLine { + /// Video yet to see a keyframe, with when it may next ask for one: `None` without PLI. + Keyless { request_at: Option }, + /// Video forwarding, refusing anything before its last keyframe at `keyframe`. + Video { keyframe: u64 }, + /// Audio forwarding, refusing anything not after its last frame at `last`. + Audio { last: Option }, +} + +/// What to do with an ingested frame. +#[derive(Debug, PartialEq, Eq)] +enum Admit { + /// Hand it to the sink. + Forward, + /// Drop it. + Drop, + /// Drop it and ask the publisher for a keyframe. + Request, +} + +impl IngestOrder { + /// Start ordering `mid`'s frames; a video line asks for its first keyframe with PLI if `pli`. + fn open(&mut self, mid: str0m::media::Mid, video: bool, pli: bool, now: Instant) { + let line = match video { + true => IngestLine::Keyless { + request_at: pli.then_some(now), + }, + false => IngestLine::Audio { last: None }, + }; + self.lines.insert(mid, line); + } + + /// Whether a frame on `mid` at `timestamp_us` reaches the sink, and whether to ask for a keyframe. + fn admit(&mut self, mid: str0m::media::Mid, keyframe: bool, timestamp_us: u64, now: Instant) -> Admit { + let Some(line) = self.lines.get_mut(&mid) else { + return Admit::Forward; + }; + match line { + IngestLine::Keyless { request_at } if !keyframe => match request_at { + Some(at) if now >= *at => { + *at = now.saturating_add(KEYFRAME_REQUEST_INTERVAL); + Admit::Request + } + _ => Admit::Drop, + }, + IngestLine::Keyless { .. } => { + *line = IngestLine::Video { + keyframe: timestamp_us, + }; + Admit::Forward + } + IngestLine::Video { keyframe: floor } if timestamp_us < *floor => Admit::Drop, + IngestLine::Video { keyframe: floor } => { + if keyframe { + *floor = timestamp_us; + } + Admit::Forward + } + IngestLine::Audio { last: Some(last) } if timestamp_us <= *last => Admit::Drop, + IngestLine::Audio { last } => { + *last = Some(timestamp_us); + Admit::Forward + } + } + } +} + /// Per-session clock that rebases each ingested track's raw RTP timestamps onto /// one timeline so audio and video stay in sync. /// @@ -739,6 +846,55 @@ mod tests { use super::*; + /// A video line drops its frames until its first keyframe, asking for one at once and then + /// at most once an interval, and forwards everything after it; a line without PLI waits + /// without asking. + #[test] + fn video_waits_for_its_first_keyframe() { + let mut order = IngestOrder::default(); + let (video, silent) = (Mid::from("1"), Mid::from("2")); + let t0 = Instant::now(); + order.open(video, true, true, t0); + order.open(silent, true, false, t0); + + assert_eq!(order.admit(video, false, 0, t0), Admit::Request); + let early = t0.saturating_add(KEYFRAME_REQUEST_INTERVAL - Duration::from_millis(1)); + assert_eq!(order.admit(video, false, 33_000, early), Admit::Drop); + let due = t0.saturating_add(KEYFRAME_REQUEST_INTERVAL); + assert_eq!(order.admit(video, false, 66_000, due), Admit::Request); + assert_eq!(order.admit(video, true, 100_000, due), Admit::Forward); + assert_eq!(order.admit(video, false, 133_000, due), Admit::Forward); + + let late = t0.saturating_add(KEYFRAME_REQUEST_INTERVAL * 5); + assert_eq!(order.admit(silent, false, 0, late), Admit::Drop); + assert_eq!(order.admit(silent, true, 33_000, late), Admit::Forward); + } + + /// A late frame is dropped where it would rewind a published group: audio anything not + /// after its last frame, video anything before its last keyframe. A video delta may still + /// dip below another delta, as a reordered picture does. + #[test] + fn late_frames_are_dropped() { + let mut order = IngestOrder::default(); + let (audio, video) = (Mid::from("0"), Mid::from("1")); + let t0 = Instant::now(); + order.open(audio, false, false, t0); + order.open(video, true, false, t0); + + assert_eq!(order.admit(audio, false, 40_000, t0), Admit::Forward); + assert_eq!(order.admit(audio, false, 20_000, t0), Admit::Drop); + assert_eq!(order.admit(audio, false, 40_000, t0), Admit::Drop); + assert_eq!(order.admit(audio, false, 60_000, t0), Admit::Forward); + + assert_eq!(order.admit(video, true, 100_000, t0), Admit::Forward); + assert_eq!(order.admit(video, false, 166_000, t0), Admit::Forward); + assert_eq!(order.admit(video, false, 133_000, t0), Admit::Forward); + assert_eq!(order.admit(video, false, 66_000, t0), Admit::Drop); + assert_eq!(order.admit(video, true, 66_000, t0), Admit::Drop); + assert_eq!(order.admit(video, true, 200_000, t0), Admit::Forward); + assert_eq!(order.admit(video, false, 166_000, t0), Admit::Drop); + } + #[test] fn sender_reports_outside_the_wall_range_are_ignored() { let mut clock = IngestClock::default(); From 939425d7a156dbe31e71d7799dcf591527255d99 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 02:56:36 -0800 Subject: [PATCH 4/6] style: rustfmt --- rs/moq-cli/src/webrtc.rs | 10 +++++----- rs/moq-webrtc/src/session.rs | 4 +--- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/rs/moq-cli/src/webrtc.rs b/rs/moq-cli/src/webrtc.rs index 80e4684d57c..2950eb28bc0 100644 --- a/rs/moq-cli/src/webrtc.rs +++ b/rs/moq-cli/src/webrtc.rs @@ -92,10 +92,7 @@ pub async fn connect_import(target: ImportTarget, url: Url, instance: moq_net::I // would find no catalog track. Held until the pull ends. let route = target .origin - .route( - name, - moq_net::origin::RouteOptions::hidden().with_instance(instance, 0), - ) + .route(name, moq_net::origin::RouteOptions::hidden().with_instance(instance, 0)) .context("failed to create broadcast")?; let catalog = moq_mux::catalog::Producer::new(&mut route.broadcast().clone(), target.ingest.catalog())?; let client = moq_webrtc::Client::new(moq_webrtc::client::Config::default())?; @@ -195,6 +192,9 @@ mod tests { drop(offer); pull.await.unwrap().expect_err("the endpoint hung up without an answer"); // The origin ends with the pull, so the only announcement left would be the pull's. - assert!(announced.next().now_or_never().flatten().is_none(), "announced a failed pull"); + assert!( + announced.next().now_or_never().flatten().is_none(), + "announced a failed pull" + ); } } diff --git a/rs/moq-webrtc/src/session.rs b/rs/moq-webrtc/src/session.rs index 4557943b22b..34d63e5f50c 100644 --- a/rs/moq-webrtc/src/session.rs +++ b/rs/moq-webrtc/src/session.rs @@ -518,9 +518,7 @@ impl IngestOrder { _ => Admit::Drop, }, IngestLine::Keyless { .. } => { - *line = IngestLine::Video { - keyframe: timestamp_us, - }; + *line = IngestLine::Video { keyframe: timestamp_us }; Admit::Forward } IngestLine::Video { keyframe: floor } if timestamp_us < *floor => Admit::Drop, From 185d0ac53e01b227071c3f060ca9b83f962df0ab Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 03:03:50 -0800 Subject: [PATCH 5/6] fix(moq-webrtc): order ingest on RTP time, and abort the catalog on a failed dial Co-Authored-By: Claude Opus 5.5 --- rs/moq-webrtc/src/client/mod.rs | 12 +++++++++++- rs/moq-webrtc/src/session.rs | 9 ++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/rs/moq-webrtc/src/client/mod.rs b/rs/moq-webrtc/src/client/mod.rs index 1913a0d6621..72d2fc67987 100644 --- a/rs/moq-webrtc/src/client/mod.rs +++ b/rs/moq-webrtc/src/client/mod.rs @@ -62,7 +62,13 @@ impl Client { broadcast: &moq_net::broadcast::Producer, catalog: &moq_mux::catalog::Producer, ) -> crate::Result<()> { - whep::dial(self, url, broadcast.clone(), catalog.clone()).await + let result = whep::dial(self, url, broadcast.clone(), catalog.clone()).await; + // A dial that fails before its session runs leaves the catalog open; a session already + // aborted it with its cause, which this keeps. + if let Err(err) = &result { + catalog.clone().abort(moq_net::Error::Transport(err.to_string())); + } + result } /// `client publish`: push the broadcast `source` resolved to a remote WHIP endpoint, @@ -99,6 +105,10 @@ mod tests { .expect_err("dial a closed port"); assert!(matches!(err, crate::Error::Http(_)), "unexpected error: {err}"); + assert!( + catalog.consume().unwrap().next().await.is_err(), + "the failed dial aborts the catalog" + ); let printed = format!("{err} {err:?}"); for secret in ["jwt", "secret", "user:pass"] { assert!(!printed.contains(secret), "error leaked {secret}: {printed}"); diff --git a/rs/moq-webrtc/src/session.rs b/rs/moq-webrtc/src/session.rs index 34d63e5f50c..246b3c36931 100644 --- a/rs/moq-webrtc/src/session.rs +++ b/rs/moq-webrtc/src/session.rs @@ -286,11 +286,12 @@ impl Session { tracing::warn!(cap = cap.rate.as_bps(), "ingest exceeded its cap plus burst; closing"); return Err(Error::RateLimited { cap: cap.rate }); } + // Ordered on the line's own RTP time: the session timeline keeps each line + // strictly increasing, which would hide a late frame. let media_us = media_time_to_micros(&data.time); - let timestamp_us = self.ingest_clock.normalize(data.mid, arrival, media_us); match self .ingest_order - .admit(data.mid, data.is_keyframe(), timestamp_us, Instant::now()) + .admit(data.mid, data.is_keyframe(), media_us, Instant::now()) { Admit::Forward => {} Admit::Drop => return Ok(()), @@ -303,6 +304,7 @@ impl Session { return Ok(()); } } + let timestamp_us = self.ingest_clock.normalize(data.mid, arrival, media_us); sink.on_frame( data.mid, codec::Frame { @@ -504,7 +506,8 @@ impl IngestOrder { self.lines.insert(mid, line); } - /// Whether a frame on `mid` at `timestamp_us` reaches the sink, and whether to ask for a keyframe. + /// Whether a frame on `mid` at its line's RTP time `timestamp_us` reaches the sink, and whether + /// to ask for a keyframe. fn admit(&mut self, mid: str0m::media::Mid, keyframe: bool, timestamp_us: u64, now: Instant) -> Admit { let Some(line) = self.lines.get_mut(&mid) else { return Admit::Forward; From 7d7ede935950eedd3656c26c55ca0035681f8a77 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 04:14:03 -0800 Subject: [PATCH 6/6] chore(quest): follow-up for a chained WHEP pull's clean shutdown Co-Authored-By: Claude Opus 5.5 --- quest/m2/README.md | 3 ++- quest/m2/whep-chain-shutdown.md | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 quest/m2/whep-chain-shutdown.md diff --git a/quest/m2/README.md b/quest/m2/README.md index baee58b1ac2..1d4585c9b16 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -55,7 +55,8 @@ removal, a rename, or a wire, catalog, or format change that is free until [moq-json writes](/quest/m2/json-write-at.md). - **Native media and gateways.** [The video producer](/quest/m2/video-producer.md), [the hole wait](/quest/m2/delay-hole-wait.md), - [moq-vaapi's exports](/quest/m2/vaapi-exports.md), and + [moq-vaapi's exports](/quest/m2/vaapi-exports.md), + [a chained WHEP pull's shutdown](/quest/m2/whep-chain-shutdown.md), and [upstream str0m](https://github.com/moq-dev/moq/pull/8963), which reverses the 2026-10-30 vendoring. - **JS and browser.** [`@moq/net`](/quest/m2/js-net-api.md), diff --git a/quest/m2/whep-chain-shutdown.md b/quest/m2/whep-chain-shutdown.md new file mode 100644 index 00000000000..d18fdd8964f --- /dev/null +++ b/quest/m2/whep-chain-shutdown.md @@ -0,0 +1,39 @@ +# [S] A WHEP pull chained into an export stops cleanly + +## Goal + +Stopping `moq import webrtc --connect -- export rtmp --connect ` +with SIGTERM exits 0 and logs no warning about its own tracks. The RTMP export +does not fail with `the source's clock runs +59.8 ppm off ours, past the 30 ppm +the output's may follow`. The WHEP ingest's tracks (`0.opus`, `1.avc3`, +`catalog.json`, `catalog`) end with `finish()` or `abort()` instead of logging +`track::Producer dropped without finish() or abort()`. Non-goal: orderly +broadcast ends while the process keeps running, which +[export-close](/quest/m3/export-close.md) owns. + +## Plan + +Found 2026-11-17 while validating +[moq-dev/moq#8997](https://github.com/moq-dev/moq/pull/8997) on the built +binary. Its PR body's Validation section has the three-process loopback setup +(an ffmpeg source through `export webrtc --listen`, the chain, and an RTMP +sink), and the stop shows both symptoms in some runs. The maintainer picked +this follow-up when picking #8997 to merge. + +- **Clock drift on stop.** The error comes from the drift estimate in + `rs/moq-mux/src/jitter.rs` (`Error` in `rs/moq-mux/src/error.rs`), judged over + only the few seconds the chain ran, while the ingest's timeline was still + settling from arrival time onto RTCP sender reports (`IngestClock` in + `rs/moq-webrtc/src/session.rs`). First find out whether the stop causes it + (for example, a timestamp jump while draining) or whether a short run would + trip it anyway. Then fix whichever is at fault: the ingest timeline or an + estimate that lacks enough samples to judge drift. +- **Unfinished tracks.** `Session::run` aborts the sink only when its loop + returns. A SIGTERM drops the pull's future instead, which drops the + `IngestSink` and its bridges unfinished. Finish or abort them on drop, the + way the rtsp import closes its catalog after its select. + +## Related + +- [Container export ends cleanly](/quest/m3/export-close.md) - an export's orderly end when its broadcast closes +- [Interop stops publishers cleanly](/quest/m3/interop-shutdown-warnings.md) - the same unfinished-producer warning in the CLI FETCH round