From 5a695c0438eaed506d52987d1d212882defbba85 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 25 Oct 2026 02:23:33 -0700 Subject: [PATCH 1/7] chore(quest): claim quest/m1/name-reuse.md Quest-Claim: 4821bd0bad16ee1b From 5d349e63477238719c7a0c6cbdbd024f15b5a030 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 25 Oct 2026 03:07:16 -0700 Subject: [PATCH 2/7] fix: continue a re-published track past its predecessor's groups A name always means the same content, so a track published again under the same name must number its groups past its predecessor's. - moq-gst: a named pad's track continues past the groups the run already published under that name (renegotiation, a re-requested pad, a resume). - moq-mux: finish() after an unfollowed seek ends past the seek, through an empty group at it, instead of below the predecessor's groups. import::Track and container::Producer expose next_sequence(). - moq-net, @moq/net: a non-TRACK_ENDED PUBLISH_DONE drains, then aborts the copy instead of declaring an end the track runs past. Co-Authored-By: Claude Opus 5.5 --- js/net/src/ietf/publish.ts | 5 +- js/net/src/ietf/subscriber.ts | 5 + js/net/src/ietf/tail.test.ts | 16 ++ quest/m1/README.md | 1 - quest/m1/name-reuse.md | 18 --- rs/moq-gst/src/sink/imp.rs | 23 ++- rs/moq-gst/src/sink/pad.rs | 217 +++++++++++++++++++++++++-- rs/moq-mux/src/codec/aac/import.rs | 5 + rs/moq-mux/src/codec/av1/import.rs | 5 + rs/moq-mux/src/codec/flac/import.rs | 5 + rs/moq-mux/src/codec/h264/import.rs | 5 + rs/moq-mux/src/codec/h265/import.rs | 5 + rs/moq-mux/src/codec/mp3.rs | 5 + rs/moq-mux/src/codec/opus/import.rs | 5 + rs/moq-mux/src/codec/vp8/import.rs | 5 + rs/moq-mux/src/codec/vp9/import.rs | 5 + rs/moq-mux/src/container/producer.rs | 35 ++++- rs/moq-mux/src/import/track.rs | 6 + rs/moq-net/src/ietf/subscriber.rs | 34 +++-- 19 files changed, 353 insertions(+), 52 deletions(-) delete mode 100644 quest/m1/name-reuse.md diff --git a/js/net/src/ietf/publish.ts b/js/net/src/ietf/publish.ts index b963eb3eca5..b159aafcea8 100644 --- a/js/net/src/ietf/publish.ts +++ b/js/net/src/ietf/publish.ts @@ -216,7 +216,10 @@ export const PublishDoneStatus = { UPDATE_FAILED: 0x8, } as const; -/** Whether a PUBLISH_DONE status ends the track cleanly rather than aborting it. */ +/** + * Whether a PUBLISH_DONE status ends the subscription cleanly, so its data streams in flight still + * drain, rather than aborting it. Only TRACK_ENDED also ends the track. + */ export function publishDoneClean(statusCode: number, version: IetfVersion): boolean { if (statusCode === PublishDoneStatus.TRACK_ENDED) return true; if (statusCode !== PublishDoneStatus.SUBSCRIPTION_ENDED) return false; diff --git a/js/net/src/ietf/subscriber.ts b/js/net/src/ietf/subscriber.ts index 62147a9ccbd..83d9cd0ab96 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -1034,6 +1034,11 @@ export class Subscriber { const { tail, track } = subscription; const complete = () => count > 0n && BigInt(tail.streams) >= count; await tail.settle(complete, track.closed); + // SUBSCRIPTION_ENDED ends just this subscription, so the copy fails rather than declare an + // end the track may run past: a later reader subscribes afresh, and its groups continue. + if (done.statusCode !== PublishDoneStatus.TRACK_ENDED) { + throw new Error(`publish done: subscription ended reason=${done.reasonPhrase}`); + } } /** diff --git a/js/net/src/ietf/tail.test.ts b/js/net/src/ietf/tail.test.ts index 23a16bf55f8..d15837bea8b 100644 --- a/js/net/src/ietf/tail.test.ts +++ b/js/net/src/ietf/tail.test.ts @@ -18,6 +18,7 @@ const ALIAS = 9n; const TRACK_ENDED = 0x2; const INTERNAL_ERROR = 0x0; const UNAUTHORIZED = 0x1; +const SUBSCRIPTION_ENDED = 0x3; // A plain subgroup stream: no extensions, no subgroup id, end of group on FIN. const FLAGS: GroupFlags = { @@ -162,6 +163,21 @@ test("a PUBLISH_DONE with an error status aborts the track", async () => { expect(await reader.closed).toBeInstanceOf(Error); }); +// SUBSCRIPTION_ENDED ends only the subscription: a later one continues the track past these +// groups, so the copy delivers what was in flight and then fails rather than declare an end. +test("a SUBSCRIPTION_ENDED PUBLISH_DONE drains, then aborts the track", async () => { + const { subscriber, reader, done } = await subscribed(); + const group = groupStream(subscriber, 0); + group.write(object("0.0")); + group.finish(); + await group.handled; + await done(SUBSCRIPTION_ENDED, 1n); + + expect(await readAll(await reader.recvGroup())).toEqual(["0.0"]); + expect(await reader.closed).toBeInstanceOf(Error); + expect(reader.end()).toBeUndefined(); +}); + test("an UNAUTHORIZED PUBLISH_DONE reads as the revocation a moq-lite peer sends", async () => { const { reader, done } = await subscribed(); await done(UNAUTHORIZED, 0n); diff --git a/quest/m1/README.md b/quest/m1/README.md index 9ad0bfc1971..f996ce51163 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -103,7 +103,6 @@ Catalog HLS field. - [Empty segments refused](/quest/m1/empty-segments.md) - `room//cam` fails loud instead of naming `room/cam` - [In-band auth](/quest/m1/auth/README.md) - a session tells its peer what it may publish and subscribe to, unions tokens presented in band, and fails loud on an out-of-scope publish - [Cluster routing](/quest/m1/cluster-routing/README.md) - prove shared routing contracts, implement per-origin routes, and connect outbound local clusters -- [No reused track names](/quest/m1/name-reuse.md) - no first-party publisher re-creates a finished track under its old name - [Routing guide](/quest/m1/routing-guide.md) - concepts and practical outbound federation setup - [One stuck check per packet](/quest/m1/quic-stuck-scan.md) - a RESET_STREAM storm into a shrunk window costs one stream scan per packet - [Generated C bindings](/quest/m1/c/README.md) - C generated from moq-ffi ships as `moq-c` 0.8.0 and replaces the hand-written libmoq diff --git a/quest/m1/name-reuse.md b/quest/m1/name-reuse.md deleted file mode 100644 index 5f029ea6b49..00000000000 --- a/quest/m1/name-reuse.md +++ /dev/null @@ -1,18 +0,0 @@ -# [M] Publishers never reuse a finished track's name - -## Goal - -No first-party publisher finishes a track and then publishes different -content under the same name. Each either aborts the track or numbers its -groups past its predecessor's. - -## Plan - -moq-transcode already finishes a rung only when its source finished. Others still -finish and re-create: moq-gst's sink on caps renegotiation and `release_pad` -(`rs/moq-gst/src/sink/imp.rs`). Also audit moq-mux's timeline error path, -moq-cli echo retire, moq-rtsp, moq-rtmp, `cpp/obs`, `js/publish`, and the IETF -SUBSCRIPTION_ENDED path in moq-net. Fix each with a test that a restart -never repeats a group sequence. - -Public API: none expected. Wire: none. diff --git a/rs/moq-gst/src/sink/imp.rs b/rs/moq-gst/src/sink/imp.rs index e295cfb5fc3..a2807a0c8b9 100644 --- a/rs/moq-gst/src/sink/imp.rs +++ b/rs/moq-gst/src/sink/imp.rs @@ -20,7 +20,7 @@ use gst::prelude::*; use gst::subclass::prelude::*; use hang::moq_net; -use super::pad::{CapsOutcome, ProducerOptions, PushOutcome, caps_supported}; +use super::pad::{CapsOutcome, Continuations, ProducerOptions, PushOutcome, caps_supported}; use super::request_pad::{MoqSinkPad, Notifications, PadLifecycle}; use super::session::{ CAT, Completion, CompletionHandle, ConnectionStatus, RUNTIME, ResolvedSettings, Session, SessionRegistration, @@ -70,6 +70,8 @@ struct State { broadcast: moq_net::broadcast::Producer, catalog: Option, wall: std::sync::Arc, + /// Where each named track this run ended continues, if published again. + continuations: Continuations, /// How long a clean end keeps the finished broadcast up before its EOS. linger: Duration, /// Whether the finished broadcast closed, so the EOS it owes may go out. @@ -435,7 +437,9 @@ impl ElementImpl for MoqSink { if let Some(state) = control.live.as_ref() && let Some(catalog) = state.catalog.as_ref() { - pad.lifecycle().media.reserve(catalog, state.wall.clone()); + pad.lifecycle() + .media + .reserve(catalog, state.wall.clone(), &state.continuations); } control.admissions.insert(pad.clone()); } @@ -582,6 +586,7 @@ impl MoqSink { broadcast, catalog: Some(catalog), wall, + continuations: Continuations::default(), linger, drained: false, lingering: None, @@ -598,7 +603,7 @@ impl MoqSink { let changes = lifecycle.reset(); lifecycle.completion = Some(completion.clone()); if !lifecycle.releasing { - lifecycle.media.reserve(catalog, wall.clone()); + lifecycle.media.reserve(catalog, wall.clone(), &state.continuations); } changes }; @@ -885,7 +890,9 @@ impl MoqSink { if !lifecycle.releasing { lifecycle.ended = false; if let Some(catalog) = &state.catalog { - lifecycle.media.reserve(catalog, state.wall.clone()); + lifecycle + .media + .reserve(catalog, state.wall.clone(), &state.continuations); } } } @@ -911,7 +918,9 @@ impl MoqSink { lifecycle.media.flush(); let catalog = state.catalog.as_ref(); if let Some(catalog) = catalog { - lifecycle.media.reserve(catalog, state.wall.clone()); + lifecycle + .media + .reserve(catalog, state.wall.clone(), &state.continuations); } let caps = lifecycle.media.resumable_caps(); caps.zip(catalog).and_then(|(caps, catalog)| { @@ -1205,7 +1214,9 @@ fn observe_caps( lifecycle: &mut PadLifecycle, caps: &gst::Caps, ) -> (Option, bool) { - lifecycle.media.reserve(catalog, state.wall.clone()); + lifecycle + .media + .reserve(catalog, state.wall.clone(), &state.continuations); let requested = lifecycle.requested().map(str::to_owned); let mut options = ProducerOptions::new(caps).with_container(lifecycle.container().into()); if let Some(track) = requested.as_deref() { diff --git a/rs/moq-gst/src/sink/pad.rs b/rs/moq-gst/src/sink/pad.rs index 46ec68abebf..13e35805bf8 100644 --- a/rs/moq-gst/src/sink/pad.rs +++ b/rs/moq-gst/src/sink/pad.rs @@ -34,7 +34,45 @@ enum PadState { enum Sink { Media(Box), Text(Box), - Opaque(Box), + Opaque(Box), +} + +impl Sink { + /// The track name, as the broadcast knows it. + fn name(&self) -> String { + match self { + Sink::Media(media) => media.track.name(), + Sink::Text(text) => text.producer.name().to_owned(), + Sink::Opaque(opaque) => opaque.producer.name().to_owned(), + } + } + + /// Where a replacement under the same name continues. + fn next_sequence(&self) -> u64 { + match self { + Sink::Media(media) => media.track.next_sequence(), + Sink::Text(text) => text.producer.next_sequence(), + Sink::Opaque(opaque) => opaque.next_sequence(), + } + } +} + +/// Where each named track a run ended continues, so the name published again in that run +/// (renegotiated caps, a pad requested again, a resumed stream) numbers its groups past its +/// predecessor's rather than restarting at 0: a name always means the same content. +/// +/// Shared by every pad of a run, and holding only names the pads were configured with. +#[derive(Clone, Default)] +pub(super) struct Continuations(std::sync::Arc>>); + +impl Continuations { + fn get(&self, name: &str) -> Option { + self.0.lock().unwrap().get(name).copied() + } + + fn record(&self, sink: &Sink) { + self.0.lock().unwrap().insert(sink.name(), sink.next_sequence()); + } } /// Keep finished rendition owners until a whole run's catalog has closed. @@ -170,6 +208,43 @@ impl Text { } } +/// An opaque data pad: one raw frame per group, with no container and no catalog entry. +struct Opaque { + producer: moq_net::track::Producer, + /// Where the first group opens when this track continues an ended one of the same name. + pending: Option, +} + +impl Opaque { + fn write(&mut self, timestamp: hang::container::Timestamp, data: &Bytes) -> Result<()> { + let Some(sequence) = self.pending.take() else { + return Ok(self.producer.write_frame(timestamp, data)?); + }; + let mut group = self.producer.create_group(moq_net::group::Info { sequence })?; + if let Err(err) = group.write_frame(timestamp, data) { + let _ = group.abort(err.clone()); + return Err(err.into()); + } + Ok(group.finish()?) + } + + /// Finish past a predecessor's groups even when nothing was written: the pending group opens + /// empty, since an end past the live edge alone would leave readers waiting on the gap. + fn finish(&mut self) -> Result<()> { + if let Some(sequence) = self.pending.take() { + self.producer + .create_group(moq_net::group::Info { sequence })? + .finish()?; + } + Ok(self.producer.finish()?) + } + + fn next_sequence(&self) -> u64 { + self.pending + .unwrap_or_else(|| self.producer.largest().map_or(0, |largest| largest.group + 1)) + } +} + /// Format microseconds as a WebVTT `HH:MM:SS.mmm` timestamp. fn format_timestamp(micros: u64) -> String { let ms = micros / 1000; @@ -237,7 +312,11 @@ pub struct Pad { /// The requested pad gates the first catalog even before CAPS can name its rendition. reserved: Option, wall: Option>, + /// The run's, once reserved; until then this pad's own, which still covers renegotiation. + continuations: Continuations, track: Option, + /// Whether `track` has a configured name, which may be published again. A generated one never is. + named: bool, caps: Option, /// Set once a producer build rejects this pad's caps or bitstream; further buffers are dropped and /// the track stays finalized. Isolated to the pad, so the session and other pads keep going. The @@ -258,7 +337,9 @@ impl Pad { Self { reserved: None, wall: None, + continuations: Continuations::default(), track: None, + named: false, caps: None, failed: false, state: PadState::NoSegment, @@ -269,8 +350,14 @@ impl Pad { } /// Fill a run's slot once, including a pad visible before its admission confirms. - pub(super) fn reserve(&mut self, catalog: &moq_mux::catalog::Producer, wall: std::sync::Arc) { + pub(super) fn reserve( + &mut self, + catalog: &moq_mux::catalog::Producer, + wall: std::sync::Arc, + continuations: &Continuations, + ) { self.wall = Some(wall); + self.continuations = continuations.clone(); if self.reserved.is_none() && self.track.is_none() && !self.failed { self.reserved = Some(catalog.reserve()); } @@ -350,6 +437,7 @@ impl Pad { let structure = caps.structure(0).context("empty caps")?; // Renegotiation: finalize the previous producer before replacing it (closed once, not abandoned). self.finalize()?; + self.named = requested.is_some(); // Opaque data has no codec importer and no catalog entry, so it never reaches the codec match. if structure.name() == "application/octet-stream" { // A generated name would leave the track unfindable: nothing advertises it. @@ -364,7 +452,10 @@ impl Pad { let info = moq_net::track::Info::default() .with_timescale(moq_net::Timescale::MICRO) .with_max_age(std::time::Duration::from_secs(5)); - self.track = Some(Sink::Opaque(Box::new(request.accept(info)))); + self.track = Some(Sink::Opaque(Box::new(Opaque { + producer: request.accept(info), + pending: self.continuations.get(&name), + }))); self.caps = Some(caps.clone()); return Ok(name); } @@ -378,18 +469,17 @@ impl Pad { // text with a presentation time, so there is nothing to parse, only a text rendition to declare. if structure.name().as_str() == "text/x-raw" { let name = Self::track_name(&broadcast, requested, ".vtt"); - self.track = Some(Sink::Text(Box::new(Self::reserve_text( - &mut broadcast, - reserved, - name.clone(), - structure, - )?))); + let mut text = Self::reserve_text(&mut broadcast, reserved, name.clone(), structure)?; + if let Some(sequence) = self.continuations.get(&name) { + text.producer.seek(sequence)?; + } + self.track = Some(Sink::Text(Box::new(text))); self.caps = Some(caps.clone()); return Ok(name); } let mut pre_skip = None; - let (track, audio, name): (import::Track, bool, String) = match structure.name().as_str() { + let (mut track, audio, name): (import::Track, bool, String) = match structure.name().as_str() { "video/x-h264" => { let (track, name) = Self::reserve_video( &mut broadcast, @@ -507,6 +597,9 @@ impl Pad { } other => anyhow::bail!("unsupported caps: {other}"), }; + if let Some(sequence) = self.continuations.get(&name) { + track.seek(sequence)?; + } self.track = Some(Sink::Media(Box::new(Media { track, audio, @@ -788,12 +881,12 @@ impl Pad { }, Err(err) => Err(anyhow::anyhow!("subtitle cue is not valid UTF-8: {err}")), }, - Sink::Opaque(producer) => { + Sink::Opaque(opaque) => { let Some(ts) = hang::container::Timestamp::from_micros(micros).ok() else { gst::warning!(CAT, "dropping frame: timestamp out of range"); return Ok(PushOutcome::Dropped); }; - producer.write_frame(ts, &data).map_err(Into::into) + opaque.write(ts, &data) } }; match result { @@ -840,8 +933,11 @@ impl Pad { let result = match track { Sink::Media(media) => media.track.finish().map_err(anyhow::Error::from), Sink::Text(text) => text.producer.finish().map_err(anyhow::Error::from), - Sink::Opaque(producer) => producer.clone().finish().map_err(anyhow::Error::from), + Sink::Opaque(opaque) => opaque.finish(), }; + if self.named { + self.continuations.record(track); + } self.failed |= result.is_err(); (closed, result.map(|()| true)) } @@ -1308,6 +1404,101 @@ mod tests { assert!(!pad.is_failed()); } + /// A name always means the same content, so the renegotiated producer numbers its groups past + /// the ones its predecessor published under the name instead of restarting at 0. + #[tokio::test] + async fn renegotiation_continues_past_the_predecessor_groups() { + gst::init().unwrap(); + let (broadcast, catalog) = producers(); + let mut pad = Pad::new(); + pad.observe_caps(&broadcast, &catalog, producer_options(&h264_caps(), Some("camera"))); + pad.observe_segment(time_segment()); + for ms in [0, 40] { + let pushed = pad.push_buffer(h264_keyframe_au(), Some(gst::ClockTime::from_mseconds(ms)), None, None); + assert_eq!(pushed, Ok(PushOutcome::Published)); + } + + let renegotiated = gst::Caps::builder("video/x-h264") + .field("stream-format", "byte-stream") + .field("alignment", "au") + .field("width", 1280i32) + .build(); + pad.observe_caps(&broadcast, &catalog, producer_options(&renegotiated, Some("camera"))); + let mut subscriber = broadcast + .consume() + .track("camera") + .unwrap() + .subscribe(None) + .await + .unwrap(); + let pushed = pad.push_buffer(h264_keyframe_au(), Some(gst::ClockTime::from_mseconds(80)), None, None); + assert_eq!(pushed, Ok(PushOutcome::Published)); + + let group = subscriber.recv_group().await.unwrap().expect("a group"); + assert_eq!(group.sequence, 2, "groups 0 and 1 belong to the predecessor"); + } + + /// A pad requested again under a released pad's name continues past its groups, even one that + /// ends without writing: its end still lands past them. + #[tokio::test] + async fn a_name_published_again_continues_past_its_predecessor() { + gst::init().unwrap(); + let (broadcast, catalog) = producers(); + let continuations = Continuations::default(); + let publish = |frames: &[u64]| { + let mut pad = Pad::new(); + pad.reserve(&catalog, super::super::wall::Wall::new(&catalog), &continuations); + pad.observe_caps(&broadcast, &catalog, producer_options(&opaque_caps(), Some("levels"))); + pad.observe_segment(time_segment()); + for ms in frames { + let pushed = pad.push_buffer( + Bytes::from_static(b"level"), + Some(gst::ClockTime::from_mseconds(*ms)), + None, + None, + ); + assert_eq!(pushed, Ok(PushOutcome::Published)); + } + pad + }; + + let mut first = publish(&[0, 40]); + assert!(first.finalize().unwrap()); + + let mut second = publish(&[]); + let mut subscriber = broadcast + .consume() + .track("levels") + .unwrap() + .subscribe(None) + .await + .unwrap(); + let pushed = second.push_buffer( + Bytes::from_static(b"level"), + Some(gst::ClockTime::from_mseconds(80)), + None, + None, + ); + assert_eq!(pushed, Ok(PushOutcome::Published)); + assert_eq!(subscriber.recv_group().await.unwrap().expect("a group").sequence, 2); + assert!(second.finalize().unwrap()); + + let mut empty = publish(&[]); + let mut subscriber = broadcast + .consume() + .track("levels") + .unwrap() + .subscribe(None) + .await + .unwrap(); + assert!(empty.finalize().unwrap()); + assert_eq!(subscriber.recv_group().await.unwrap().expect("a group").sequence, 3); + assert!( + subscriber.recv_group().await.unwrap().is_none(), + "the track ends past group 3" + ); + } + // AAC carries its config in caps; without codec_data the producer cannot be built. #[test] fn aac_without_codec_data_fails_the_pad() { diff --git a/rs/moq-mux/src/codec/aac/import.rs b/rs/moq-mux/src/codec/aac/import.rs index 4fc34784137..ce59e90fd80 100644 --- a/rs/moq-mux/src/codec/aac/import.rs +++ b/rs/moq-mux/src/codec/aac/import.rs @@ -80,6 +80,11 @@ impl Import { Ok(()) } + /// Where the next group opens: past every group published so far, or where a seek asked. + pub(crate) fn next_sequence(&self) -> u64 { + self.track.next_sequence() + } + /// Close the current group and open the next one at `sequence`. pub fn seek(&mut self, sequence: u64) -> crate::Result<()> { self.track.seek(sequence)?; diff --git a/rs/moq-mux/src/codec/av1/import.rs b/rs/moq-mux/src/codec/av1/import.rs index f5f568b384c..4d4826045e7 100644 --- a/rs/moq-mux/src/codec/av1/import.rs +++ b/rs/moq-mux/src/codec/av1/import.rs @@ -233,6 +233,11 @@ impl Import { self.track.discontinuity() } + /// Where the next group opens: past every group published so far, or where a seek asked. + pub(crate) fn next_sequence(&self) -> u64 { + self.track.next_sequence() + } + /// Close the current group and open the next one at `sequence`. pub fn seek(&mut self, sequence: u64) -> Result<()> { self.track.seek(sequence)?; diff --git a/rs/moq-mux/src/codec/flac/import.rs b/rs/moq-mux/src/codec/flac/import.rs index 2527837cc6c..2e5ac7533fc 100644 --- a/rs/moq-mux/src/codec/flac/import.rs +++ b/rs/moq-mux/src/codec/flac/import.rs @@ -68,6 +68,11 @@ impl Import { self.track.discontinuity() } + /// Where the next group opens: past every group published so far, or where a seek asked. + pub(crate) fn next_sequence(&self) -> u64 { + self.track.next_sequence() + } + /// Close the current group and open the next one at `sequence`. pub fn seek(&mut self, sequence: u64) -> crate::Result<()> { self.track.seek(sequence)?; diff --git a/rs/moq-mux/src/codec/h264/import.rs b/rs/moq-mux/src/codec/h264/import.rs index 786a7299d6c..8ecfa806d16 100644 --- a/rs/moq-mux/src/codec/h264/import.rs +++ b/rs/moq-mux/src/codec/h264/import.rs @@ -158,6 +158,11 @@ impl Import { Ok(()) } + /// Where the next group opens: past every group published so far, or where a seek asked. + pub(crate) fn next_sequence(&self) -> u64 { + self.track.next_sequence() + } + /// Close the current group and open the next one at `sequence`. pub fn seek(&mut self, sequence: u64) -> Result<()> { self.track.seek(sequence)?; diff --git a/rs/moq-mux/src/codec/h265/import.rs b/rs/moq-mux/src/codec/h265/import.rs index 5d1c6100c29..4e2765f23bb 100644 --- a/rs/moq-mux/src/codec/h265/import.rs +++ b/rs/moq-mux/src/codec/h265/import.rs @@ -161,6 +161,11 @@ impl Import { Ok(()) } + /// Where the next group opens: past every group published so far, or where a seek asked. + pub(crate) fn next_sequence(&self) -> u64 { + self.track.next_sequence() + } + /// Close the current group and open the next one at `sequence`. pub fn seek(&mut self, sequence: u64) -> Result<()> { self.track.seek(sequence)?; diff --git a/rs/moq-mux/src/codec/mp3.rs b/rs/moq-mux/src/codec/mp3.rs index f3b5d881075..d4f33491cf3 100644 --- a/rs/moq-mux/src/codec/mp3.rs +++ b/rs/moq-mux/src/codec/mp3.rs @@ -157,6 +157,11 @@ impl Import { self.track.discontinuity() } + /// Where the next group opens: past every group published so far, or where a seek asked. + pub(crate) fn next_sequence(&self) -> u64 { + self.track.next_sequence() + } + /// Close the current group and open the next one at `sequence`. pub fn seek(&mut self, sequence: u64) -> crate::Result<()> { self.track.seek(sequence)?; diff --git a/rs/moq-mux/src/codec/opus/import.rs b/rs/moq-mux/src/codec/opus/import.rs index a897c7cb4c6..6cf6fa5d943 100644 --- a/rs/moq-mux/src/codec/opus/import.rs +++ b/rs/moq-mux/src/codec/opus/import.rs @@ -73,6 +73,11 @@ impl Import { Ok(()) } + /// Where the next group opens: past every group published so far, or where a seek asked. + pub(crate) fn next_sequence(&self) -> u64 { + self.track.next_sequence() + } + /// Close the current group and open the next one at `sequence`. pub fn seek(&mut self, sequence: u64) -> crate::Result<()> { self.track.seek(sequence)?; diff --git a/rs/moq-mux/src/codec/vp8/import.rs b/rs/moq-mux/src/codec/vp8/import.rs index 4d96d603979..b344e81d4c7 100644 --- a/rs/moq-mux/src/codec/vp8/import.rs +++ b/rs/moq-mux/src/codec/vp8/import.rs @@ -120,6 +120,11 @@ impl Import { self.track.discontinuity() } + /// Where the next group opens: past every group published so far, or where a seek asked. + pub(crate) fn next_sequence(&self) -> u64 { + self.track.next_sequence() + } + /// Close the current group and open the next one at `sequence`. pub fn seek(&mut self, sequence: u64) -> crate::Result<()> { self.track.seek(sequence)?; diff --git a/rs/moq-mux/src/codec/vp9/import.rs b/rs/moq-mux/src/codec/vp9/import.rs index f65b4764301..8e3164a8f73 100644 --- a/rs/moq-mux/src/codec/vp9/import.rs +++ b/rs/moq-mux/src/codec/vp9/import.rs @@ -121,6 +121,11 @@ impl Import { self.track.discontinuity() } + /// Where the next group opens: past every group published so far, or where a seek asked. + pub(crate) fn next_sequence(&self) -> u64 { + self.track.next_sequence() + } + /// Close the current group and open the next one at `sequence`. pub fn seek(&mut self, sequence: u64) -> crate::Result<()> { self.track.seek(sequence)?; diff --git a/rs/moq-mux/src/container/producer.rs b/rs/moq-mux/src/container/producer.rs index c8e9648db1b..ea7d7e00c79 100644 --- a/rs/moq-mux/src/container/producer.rs +++ b/rs/moq-mux/src/container/producer.rs @@ -343,9 +343,15 @@ where self.group.is_none() } - /// One past the track's largest group, even an empty one: where a group continues after - /// what the track holds. - pub(crate) fn next_sequence(&self) -> u64 { + /// Where the next group opens: the sequence a [`seek`](Self::seek) asked for, else one past + /// the track's largest group, even an empty one. A replacement under the same track name + /// seeks here. + pub fn next_sequence(&self) -> u64 { + self.pending_sequence.unwrap_or_else(|| self.after_largest()) + } + + /// One past the track's largest group, even an empty one. + fn after_largest(&self) -> u64 { self.inner.largest().map_or(0, |largest| largest.group + 1) } @@ -751,6 +757,13 @@ where /// Finish the track, flushing any buffered frames and closing any open group. pub fn finish(&mut self) -> crate::Result<()> { self.cut(None)?; + // A seek no group followed still ends past it, so a track continuing past a predecessor's + // groups never declares an end below them. The seek's group opens empty: an end past the + // live edge alone would leave readers waiting on the skipped sequences. + let next = self.after_largest(); + if let Some(sequence) = self.pending_sequence.take_if(|sequence| *sequence > next) { + self.inner.create_group(moq_net::group::Info { sequence })?.finish()?; + } self.inner.finish()?; self.publish_estimate()?; // A finished track may be kept around, but its media stops holding the other tracks back. @@ -1434,6 +1447,22 @@ mod tests { assert_eq!(collect_sequences(consumer).await, vec![5, 6]); } + /// A track that continues past a predecessor's groups and finishes before writing any still + /// ends past them, so a relay holding the predecessor's groups accepts the end. + #[tokio::test] + async fn finish_after_seek_ends_past_the_seek() { + let track = track_producer("test", hang::container::track_info(hang::catalog::PRIORITY.video)); + let consumer = track.subscribe(replay()); + let mut producer = Producer::new(track, Container::Legacy(crate::container::Kind::Data)); + + producer.seek(7).unwrap(); + assert_eq!(producer.next_sequence(), 7); + producer.finish().unwrap(); + + assert_eq!(producer.next_sequence(), 8); + assert_eq!(collect_sequences(consumer).await, vec![7]); + } + /// Records the frames handed to each `write`, so tests can inspect the /// durations the producer backfilled. Write-only. #[derive(Clone, Default)] diff --git a/rs/moq-mux/src/import/track.rs b/rs/moq-mux/src/import/track.rs index 2df16450b0a..ac65f37fe28 100644 --- a/rs/moq-mux/src/import/track.rs +++ b/rs/moq-mux/src/import/track.rs @@ -464,6 +464,12 @@ impl Track { import!(&mut self.kind, import => import.seek(sequence)) } + /// Where the next group opens: past every group published so far, finished or not, or where a + /// [`seek`](Self::seek) asked. A replacement under the same track name seeks here. + pub fn next_sequence(&self) -> u64 { + import!(&self.kind, import => import.next_sequence()) + } + /// Drop the partial access unit of a format split from a byte stream. fn reset_split(&mut self) { match &mut self.kind { diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index e58bd3dc828..927ed9c5194 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -3619,10 +3619,11 @@ impl Subscriber { End::Done(res) => { // Only TRACK_ENDED ends a listing entry quietly; a clean end of another // kind, such as SUBSCRIPTION_ENDED, leaves the track's fate unknown. - ended = Some(match &res { + let end = match &res { Ok(done) => tracks::listing_end(done, self.version), Err(err) => Err(err.clone()), - }); + }; + ended = Some(end.clone()); match res.map(|done| done.stream_count) { Ok(count) => { tracing::debug!(broadcast = %self.origin.absolute(broadcast_path), track = %track_name, "subscribe complete"); @@ -3659,12 +3660,21 @@ impl Subscriber { fill.end(); } } - // A no-op once an END_OF_TRACK declared the end, whose readers wait - // for a missing group only as long as the grace. - let _ = track.finish(); - // Only TRACK_ENDED ends the track: after SUBSCRIPTION_ENDED a - // later reader subscribes afresh. - finished = matches!(ended, Some(Ok(()))); + // Only TRACK_ENDED ends the track. Any other status ends just this + // subscription, so the copy fails rather than declare an end the + // track may run past: a later reader subscribes afresh to the same + // name, and its groups continue. + match end { + // A no-op once an END_OF_TRACK declared the end, whose readers + // wait for a missing group only as long as the grace. + Ok(()) => { + let _ = track.finish(); + finished = true; + } + Err(err) => { + let _ = track.clone().abort(err); + } + } } Err(err) => { tracing::debug!(%err, "subscribe ended with error"); @@ -16328,7 +16338,9 @@ mod joining_fetch_tests { } /// SUBSCRIPTION_ENDED ends only our subscription, not the track, so the copy is released - /// for a later reader to subscribe afresh rather than lingering as if the track ended. + /// for a later reader to subscribe afresh rather than lingering as if the track ended. Its + /// reader sees an error, not a clean end: the fresh copy's groups continue past this one's, + /// so an end here would be one the track runs past. #[moq_clock::test] async fn a_subscription_ended_copy_does_not_linger() { for version in JOINING_DRAFTS { @@ -16345,12 +16357,14 @@ mod joining_fetch_tests { version, )); let mut run = JoinRun::start(version, None, ok, Vec::new()).await; - let _subscriber = (&mut run._hold.2).await.expect("subscribed"); + let mut subscriber = (&mut run._hold.2).await.expect("subscribed"); while !run.subscriber.state.lock().subscribes.is_empty() { settle().await; } settle().await; assert!(run.serving.is_finished(), "{version}: the copy is released"); + let end = subscriber.recv_group().await.map(|group| group.is_some()); + assert!(end.is_err(), "{version}: the copy ended cleanly: {end:?}"); } } From b1e0b5b2afb089f10b64aa3f5b6220b0738e9c18 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 25 Oct 2026 03:13:29 -0700 Subject: [PATCH 3/7] fix(mux): abort a failed timeline track with its cause A rejected report dropped the timeline track, so readers saw Dropped, and a failed publish finished it cleanly, which reads as a complete index. Both now abort with the cause: a moq-net error as is, anything else as StreamError::Internal. Co-Authored-By: Claude Opus 5.5 --- rs/moq-mux/src/timeline.rs | 45 +++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/rs/moq-mux/src/timeline.rs b/rs/moq-mux/src/timeline.rs index df03b0bdac4..76da688303d 100644 --- a/rs/moq-mux/src/timeline.rs +++ b/rs/moq-mux/src/timeline.rs @@ -433,6 +433,8 @@ impl Iterator for Segmenter { /// Records must be pushed in sequence order, starting at the window's next index. pub struct Producer { sink: moq_json::window::Producer, + /// The window's track, kept to abort it with the cause of a failure. + track: moq_net::track::Producer, } impl Producer { @@ -448,10 +450,22 @@ impl Producer { /// Publish onto `track`, starting at record zero. pub fn new(track: moq_net::track::Producer) -> Self { Self { - sink: moq_json::window::Producer::new(track, Self::config()), + sink: moq_json::window::Producer::new(track.clone(), Self::config()), + track, } } + /// Abort the timeline track with `err`, so its readers see a failure rather than an end. + fn abort(self, err: &crate::Error) { + // A cause outside moq-net has no wire code, so readers get a generic failure. + let reason = match err { + crate::Error::Moq(err) => err.clone(), + _ => moq_net::StreamError::Internal.into(), + }; + // A track that already closed keeps its first reason. + let _ = self.track.abort(reason); + } + fn config() -> moq_json::window::ProducerConfig { moq_json::window::ProducerConfig::default() .with_compression(true) @@ -529,9 +543,10 @@ impl Live { continue; }; if let Err(err) = output.push(&record) { - tracing::warn!(%err, "timeline publish failed; dropping the timeline track"); - let _ = output.finish(); - self.output = None; + tracing::warn!(%err, "timeline publish failed; aborting the timeline track"); + if let Some(output) = self.output.take() { + output.abort(&err); + } } } if let Some(duration) = self.segmenter.declared_duration() @@ -691,10 +706,12 @@ impl Recorder { pub fn frame(&mut self, position: Position, pts: Timestamp, keyframe: bool) { let mut live = self.live.lock().unwrap(); if let Err(err) = live.segmenter.frame(position, pts, keyframe) { - tracing::warn!(%err, "timeline report rejected; dropping the timeline track"); + tracing::warn!(%err, "timeline report rejected; aborting the timeline track"); live.segmenter.close(); - // Dropped without a finish, so subscribers see the timeline fail rather than end. - live.output = None; + // Subscribers see the timeline fail rather than end. + if let Some(output) = live.output.take() { + output.abort(&err); + } } live.publish(); } @@ -1392,7 +1409,19 @@ mod test { match consumer.next().await { Ok(Some(_)) => continue, Ok(None) => panic!("the timeline ended cleanly despite omitting a frame"), - Err(_) => break, + // Aborted with the failure, not left to read as a producer that went away. + Err(err) => { + assert!( + matches!( + err, + crate::Error::Json(moq_json::Error::Net(moq_net::Error::Stream( + moq_net::StreamError::Internal + ))) + ), + "{err:?}" + ); + break; + } } } } From 0d1b3ef2e129b962784a1a8ba50c6ef15613061e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 25 Oct 2026 15:33:04 -0700 Subject: [PATCH 4/7] fix(mux): consume a stale seek when finishing A seek at or below the largest group that no group followed stayed pending after finish, so next_sequence reported it and a replacement would reopen below the predecessor's groups. Co-Authored-By: Claude Opus 5.5 --- rs/moq-mux/src/container/producer.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/rs/moq-mux/src/container/producer.rs b/rs/moq-mux/src/container/producer.rs index ea7d7e00c79..8d14e89a593 100644 --- a/rs/moq-mux/src/container/producer.rs +++ b/rs/moq-mux/src/container/producer.rs @@ -760,8 +760,10 @@ where // A seek no group followed still ends past it, so a track continuing past a predecessor's // groups never declares an end below them. The seek's group opens empty: an end past the // live edge alone would leave readers waiting on the skipped sequences. - let next = self.after_largest(); - if let Some(sequence) = self.pending_sequence.take_if(|sequence| *sequence > next) { + // A seek at or below the largest group is consumed too, so `next_sequence` stays past it. + if let Some(sequence) = self.pending_sequence.take() + && sequence > self.after_largest() + { self.inner.create_group(moq_net::group::Info { sequence })?.finish()?; } self.inner.finish()?; @@ -1463,6 +1465,23 @@ mod tests { assert_eq!(collect_sequences(consumer).await, vec![7]); } + /// A seek below the track's groups that nothing followed adds no group, and still reports + /// past the largest one. + #[tokio::test] + async fn finish_after_stale_seek_ends_past_the_largest() { + let track = track_producer("test", hang::container::track_info(hang::catalog::PRIORITY.video)); + let consumer = track.subscribe(replay()); + let mut producer = Producer::new(track, Container::Legacy(crate::container::Kind::Data)); + + producer.seek(5).unwrap(); + producer.write(frame(0, true)).unwrap(); // seq 5 + producer.seek(3).unwrap(); + producer.finish().unwrap(); + + assert_eq!(producer.next_sequence(), 6); + assert_eq!(collect_sequences(consumer).await, vec![5]); + } + /// Records the frames handed to each `write`, so tests can inspect the /// durations the producer backfilled. Write-only. #[derive(Clone, Default)] From 13a4e0ca4293468a6684201afba3aa4a3ff94578 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 25 Oct 2026 16:11:57 -0700 Subject: [PATCH 5/7] fix(gst): keep a renegotiated track live, mint a name for new content Caps that build the same importer (video configures in band, opaque data has no config) now keep the live producer, so a reader subscribed before the renegotiation continues into the new caps. Anything that needs a new producer for a name the run already ended (a new codec config, a pad requested again, a stream resumed after EOS) publishes as /, since a reader that saw the clean end never follows the name into a successor. This replaces the continue-past-the-predecessor approach, so the moq-mux seek-on-finish and next_sequence additions are reverted. Co-Authored-By: Claude Opus 5.5 --- doc/bin/gstreamer.md | 6 +- rs/moq-gst/src/sink/imp.rs | 25 +-- rs/moq-gst/src/sink/pad.rs | 299 ++++++++++++--------------- rs/moq-mux/src/codec/aac/import.rs | 5 - rs/moq-mux/src/codec/av1/import.rs | 5 - rs/moq-mux/src/codec/flac/import.rs | 5 - rs/moq-mux/src/codec/h264/import.rs | 5 - rs/moq-mux/src/codec/h265/import.rs | 5 - rs/moq-mux/src/codec/mp3.rs | 5 - rs/moq-mux/src/codec/opus/import.rs | 5 - rs/moq-mux/src/codec/vp8/import.rs | 5 - rs/moq-mux/src/codec/vp9/import.rs | 5 - rs/moq-mux/src/container/producer.rs | 54 +---- rs/moq-mux/src/import/track.rs | 6 - 14 files changed, 152 insertions(+), 283 deletions(-) diff --git a/doc/bin/gstreamer.md b/doc/bin/gstreamer.md index f56cba540d5..edb94e52407 100644 --- a/doc/bin/gstreamer.md +++ b/doc/bin/gstreamer.md @@ -63,7 +63,11 @@ Audio packets share groups of at least 20 ms while forwarding as they arrive. Each `sink_%u` request pad is one track. Pad properties: `track` names it (default: after the codec), `container=loc` publishes it as [LOC](/concept/standard#loc) instead of the legacy hang container, and -`track-status`/`track-error` report its lifecycle. Element properties: +`track-status`/`track-error` report its lifecycle. Caps that only change what +the bitstream already carries (a video resolution) keep the track live. A run +never publishes a track name it ended again: new codec config, a pad requested +again, or a stream resumed after EOS publishes as `/`, which `track` +reads back. Element properties: `url`, `broadcast`, `linger`, `connect-version`, `tls-disable-verify`, `quic-idle-timeout`, `quic-keylog`, `quic-keep-alive`, and read-only `status`, `connected`, `moq-version`, and `estimated-send-rate`, `estimated-recv-rate`, `connection-stats`, and diff --git a/rs/moq-gst/src/sink/imp.rs b/rs/moq-gst/src/sink/imp.rs index a2807a0c8b9..1619da5996e 100644 --- a/rs/moq-gst/src/sink/imp.rs +++ b/rs/moq-gst/src/sink/imp.rs @@ -20,7 +20,7 @@ use gst::prelude::*; use gst::subclass::prelude::*; use hang::moq_net; -use super::pad::{CapsOutcome, Continuations, ProducerOptions, PushOutcome, caps_supported}; +use super::pad::{CapsOutcome, Names, ProducerOptions, PushOutcome, caps_supported}; use super::request_pad::{MoqSinkPad, Notifications, PadLifecycle}; use super::session::{ CAT, Completion, CompletionHandle, ConnectionStatus, RUNTIME, ResolvedSettings, Session, SessionRegistration, @@ -70,8 +70,8 @@ struct State { broadcast: moq_net::broadcast::Producer, catalog: Option, wall: std::sync::Arc, - /// Where each named track this run ended continues, if published again. - continuations: Continuations, + /// The configured track names this run ended, which it never publishes again. + names: Names, /// How long a clean end keeps the finished broadcast up before its EOS. linger: Duration, /// Whether the finished broadcast closed, so the EOS it owes may go out. @@ -439,7 +439,7 @@ impl ElementImpl for MoqSink { { pad.lifecycle() .media - .reserve(catalog, state.wall.clone(), &state.continuations); + .reserve(catalog, state.wall.clone(), &state.names); } control.admissions.insert(pad.clone()); } @@ -586,7 +586,7 @@ impl MoqSink { broadcast, catalog: Some(catalog), wall, - continuations: Continuations::default(), + names: Names::default(), linger, drained: false, lingering: None, @@ -603,7 +603,7 @@ impl MoqSink { let changes = lifecycle.reset(); lifecycle.completion = Some(completion.clone()); if !lifecycle.releasing { - lifecycle.media.reserve(catalog, wall.clone(), &state.continuations); + lifecycle.media.reserve(catalog, wall.clone(), &state.names); } changes }; @@ -892,7 +892,7 @@ impl MoqSink { if let Some(catalog) = &state.catalog { lifecycle .media - .reserve(catalog, state.wall.clone(), &state.continuations); + .reserve(catalog, state.wall.clone(), &state.names); } } } @@ -920,7 +920,7 @@ impl MoqSink { if let Some(catalog) = catalog { lifecycle .media - .reserve(catalog, state.wall.clone(), &state.continuations); + .reserve(catalog, state.wall.clone(), &state.names); } let caps = lifecycle.media.resumable_caps(); caps.zip(catalog).and_then(|(caps, catalog)| { @@ -1216,7 +1216,7 @@ fn observe_caps( ) -> (Option, bool) { lifecycle .media - .reserve(catalog, state.wall.clone(), &state.continuations); + .reserve(catalog, state.wall.clone(), &state.names); let requested = lifecycle.requested().map(str::to_owned); let mut options = ProducerOptions::new(caps).with_container(lifecycle.container().into()); if let Some(track) = requested.as_deref() { @@ -1606,7 +1606,8 @@ mod tests { assert!(completion_of(&run.0).is_open()); } - // GStreamer keeps CAPS sticky across a flush, so a resumed pad gets no new CAPS event. + // GStreamer keeps CAPS sticky across a flush, so a resumed pad gets no new CAPS event. EOS ended its + // track, so the resumed one takes a fresh name. #[test] fn an_unresolved_pad_resumed_after_eos_publishes_without_new_caps() { use futures::FutureExt; @@ -1636,7 +1637,7 @@ mod tests { let std::task::Poll::Ready(Ok(Some(next))) = next_catalog(&mut catalog) else { panic!("the resumed pad must publish its rendition ({resume})") }; - assert!(next.video.renditions.contains_key("video"), "{resume}"); + assert!(next.video.renditions.contains_key("video/1"), "{resume}"); let broadcast = run .0 .imp() @@ -1649,7 +1650,7 @@ mod tests { .broadcast .consume(); let mut track = broadcast - .track("video") + .track("video/1") .expect("the resumed track is published") .subscribe(None) .now_or_never() diff --git a/rs/moq-gst/src/sink/pad.rs b/rs/moq-gst/src/sink/pad.rs index 13e35805bf8..b1910ec3ab1 100644 --- a/rs/moq-gst/src/sink/pad.rs +++ b/rs/moq-gst/src/sink/pad.rs @@ -34,7 +34,7 @@ enum PadState { enum Sink { Media(Box), Text(Box), - Opaque(Box), + Opaque(Box), } impl Sink { @@ -43,35 +43,40 @@ impl Sink { match self { Sink::Media(media) => media.track.name(), Sink::Text(text) => text.producer.name().to_owned(), - Sink::Opaque(opaque) => opaque.producer.name().to_owned(), - } - } - - /// Where a replacement under the same name continues. - fn next_sequence(&self) -> u64 { - match self { - Sink::Media(media) => media.track.next_sequence(), - Sink::Text(text) => text.producer.next_sequence(), - Sink::Opaque(opaque) => opaque.next_sequence(), + Sink::Opaque(producer) => producer.name().to_owned(), } } } -/// Where each named track a run ended continues, so the name published again in that run -/// (renegotiated caps, a pad requested again, a resumed stream) numbers its groups past its -/// predecessor's rather than restarting at 0: a name always means the same content. +/// The configured track names a run ended, plus the names it minted to replace them. /// -/// Shared by every pad of a run, and holding only names the pads were configured with. +/// A name always means the same content, and a reader that saw a track end (here or at a relay) +/// never follows the name into a successor. So a run never publishes an ended name again: a pad that +/// would (a new codec config, a pad requested again, a stream resumed after EOS) publishes under the +/// first free `/` instead, as js/publish does. Generated names are never reused, so they +/// are not recorded. #[derive(Clone, Default)] -pub(super) struct Continuations(std::sync::Arc>>); - -impl Continuations { - fn get(&self, name: &str) -> Option { - self.0.lock().unwrap().get(name).copied() +pub(super) struct Names(std::sync::Arc>>); + +impl Names { + /// `name`, unless the run ended it: then the first `name/` the run has not used. + fn claim(&self, name: &str) -> String { + let mut taken = self.0.lock().unwrap(); + if !taken.contains(name) { + return name.to_owned(); + } + let mut n = 1u64; + loop { + let minted = format!("{name}/{n}"); + if taken.insert(minted.clone()) { + return minted; + } + n += 1; + } } - fn record(&self, sink: &Sink) { - self.0.lock().unwrap().insert(sink.name(), sink.next_sequence()); + fn end(&self, name: String) { + self.0.lock().unwrap().insert(name); } } @@ -208,43 +213,6 @@ impl Text { } } -/// An opaque data pad: one raw frame per group, with no container and no catalog entry. -struct Opaque { - producer: moq_net::track::Producer, - /// Where the first group opens when this track continues an ended one of the same name. - pending: Option, -} - -impl Opaque { - fn write(&mut self, timestamp: hang::container::Timestamp, data: &Bytes) -> Result<()> { - let Some(sequence) = self.pending.take() else { - return Ok(self.producer.write_frame(timestamp, data)?); - }; - let mut group = self.producer.create_group(moq_net::group::Info { sequence })?; - if let Err(err) = group.write_frame(timestamp, data) { - let _ = group.abort(err.clone()); - return Err(err.into()); - } - Ok(group.finish()?) - } - - /// Finish past a predecessor's groups even when nothing was written: the pending group opens - /// empty, since an end past the live edge alone would leave readers waiting on the gap. - fn finish(&mut self) -> Result<()> { - if let Some(sequence) = self.pending.take() { - self.producer - .create_group(moq_net::group::Info { sequence })? - .finish()?; - } - Ok(self.producer.finish()?) - } - - fn next_sequence(&self) -> u64 { - self.pending - .unwrap_or_else(|| self.producer.largest().map_or(0, |largest| largest.group + 1)) - } -} - /// Format microseconds as a WebVTT `HH:MM:SS.mmm` timestamp. fn format_timestamp(micros: u64) -> String { let ms = micros / 1000; @@ -284,7 +252,8 @@ fn escape_cue(text: &str) -> String { /// lets the caller report a status instead of only logging one. #[derive(Debug, Clone, PartialEq, Eq)] pub enum CapsOutcome { - /// Already publishing under these caps, or already failed: no producer was built. + /// The live producer keeps publishing (the caps build the same importer), or the pad already + /// failed: no producer was built. Unchanged, /// A producer was built, reserving this track name. Active(String), @@ -313,9 +282,9 @@ pub struct Pad { reserved: Option, wall: Option>, /// The run's, once reserved; until then this pad's own, which still covers renegotiation. - continuations: Continuations, + names: Names, track: Option, - /// Whether `track` has a configured name, which may be published again. A generated one never is. + /// Whether `track` has a configured name, which may be requested again. A generated one never is. named: bool, caps: Option, /// Set once a producer build rejects this pad's caps or bitstream; further buffers are dropped and @@ -337,7 +306,7 @@ impl Pad { Self { reserved: None, wall: None, - continuations: Continuations::default(), + names: Names::default(), track: None, named: false, caps: None, @@ -354,10 +323,10 @@ impl Pad { &mut self, catalog: &moq_mux::catalog::Producer, wall: std::sync::Arc, - continuations: &Continuations, + names: &Names, ) { self.wall = Some(wall); - self.continuations = continuations.clone(); + self.names = names.clone(); if self.reserved.is_none() && self.track.is_none() && !self.failed { self.reserved = Some(catalog.reserve()); } @@ -402,14 +371,21 @@ impl Pad { /// (Re)build the producer when the pad's caps change, under `track` when the pad was given a name. /// A build failure invalidates only this pad; the caller keeps the session and other pads alive. - /// Identical caps re-sent as a sticky event keep the live producer. + /// Caps that build the same importer keep the live producer, so its readers continue. pub(super) fn observe_caps( &mut self, broadcast: &moq_net::broadcast::Producer, catalog: &moq_mux::catalog::Producer, options: ProducerOptions<'_>, ) -> CapsOutcome { - if self.failed || (self.track.is_some() && self.caps.as_deref() == Some(options.caps)) { + if self.failed { + return CapsOutcome::Unchanged; + } + if self.track.is_some() + && let Some(caps) = &self.caps + && same_importer(caps, options.caps) + { + self.caps = Some(options.caps.clone()); return CapsOutcome::Unchanged; } let reserved = self.reserved.take().unwrap_or_else(|| catalog.reserve()); @@ -435,9 +411,18 @@ impl Pad { requested, } = options; let structure = caps.structure(0).context("empty caps")?; - // Renegotiation: finalize the previous producer before replacing it (closed once, not abandoned). + // Renegotiation to new content: finalize the previous producer before replacing it (closed once, + // not abandoned), so its name has ended and the replacement takes a fresh one. self.finalize()?; self.named = requested.is_some(); + let requested = requested.map(|name| { + let claimed = self.names.claim(name); + if claimed != name { + gst::info!(CAT, "track {name} already ended in this run, publishing as {claimed}"); + } + claimed + }); + let requested = requested.as_deref(); // Opaque data has no codec importer and no catalog entry, so it never reaches the codec match. if structure.name() == "application/octet-stream" { // A generated name would leave the track unfindable: nothing advertises it. @@ -452,10 +437,7 @@ impl Pad { let info = moq_net::track::Info::default() .with_timescale(moq_net::Timescale::MICRO) .with_max_age(std::time::Duration::from_secs(5)); - self.track = Some(Sink::Opaque(Box::new(Opaque { - producer: request.accept(info), - pending: self.continuations.get(&name), - }))); + self.track = Some(Sink::Opaque(Box::new(request.accept(info)))); self.caps = Some(caps.clone()); return Ok(name); } @@ -469,17 +451,18 @@ impl Pad { // text with a presentation time, so there is nothing to parse, only a text rendition to declare. if structure.name().as_str() == "text/x-raw" { let name = Self::track_name(&broadcast, requested, ".vtt"); - let mut text = Self::reserve_text(&mut broadcast, reserved, name.clone(), structure)?; - if let Some(sequence) = self.continuations.get(&name) { - text.producer.seek(sequence)?; - } - self.track = Some(Sink::Text(Box::new(text))); + self.track = Some(Sink::Text(Box::new(Self::reserve_text( + &mut broadcast, + reserved, + name.clone(), + structure, + )?))); self.caps = Some(caps.clone()); return Ok(name); } let mut pre_skip = None; - let (mut track, audio, name): (import::Track, bool, String) = match structure.name().as_str() { + let (track, audio, name): (import::Track, bool, String) = match structure.name().as_str() { "video/x-h264" => { let (track, name) = Self::reserve_video( &mut broadcast, @@ -597,9 +580,6 @@ impl Pad { } other => anyhow::bail!("unsupported caps: {other}"), }; - if let Some(sequence) = self.continuations.get(&name) { - track.seek(sequence)?; - } self.track = Some(Sink::Media(Box::new(Media { track, audio, @@ -881,12 +861,12 @@ impl Pad { }, Err(err) => Err(anyhow::anyhow!("subtitle cue is not valid UTF-8: {err}")), }, - Sink::Opaque(opaque) => { + Sink::Opaque(producer) => { let Some(ts) = hang::container::Timestamp::from_micros(micros).ok() else { gst::warning!(CAT, "dropping frame: timestamp out of range"); return Ok(PushOutcome::Dropped); }; - opaque.write(ts, &data) + producer.write_frame(ts, &data).map_err(Into::into) } }; match result { @@ -933,10 +913,10 @@ impl Pad { let result = match track { Sink::Media(media) => media.track.finish().map_err(anyhow::Error::from), Sink::Text(text) => text.producer.finish().map_err(anyhow::Error::from), - Sink::Opaque(opaque) => opaque.finish(), + Sink::Opaque(producer) => producer.clone().finish().map_err(anyhow::Error::from), }; if self.named { - self.continuations.record(track); + self.names.end(track.name()); } self.failed |= result.is_err(); (closed, result.map(|()| true)) @@ -1228,6 +1208,23 @@ pub fn caps_supported(caps: &gst::CapsRef) -> bool { ) } +/// Whether `next` builds the same importer as `current`, so the live track continues under it. +/// +/// Video configures in band and opaque data carries no config, so only their media type counts. An +/// audio or text pad builds from its whole caps (the codec config, rate, channels, language), which +/// is what a subscriber decodes or selects by, so any change there is new content. +fn same_importer(current: &gst::CapsRef, next: &gst::CapsRef) -> bool { + let (Some(current_structure), Some(next_structure)) = (current.structure(0), next.structure(0)) else { + return false; + }; + match next_structure.name().as_str() { + "video/x-h264" | "video/x-h265" | "video/x-av1" | "video/x-vp8" | "video/x-vp9" | "application/octet-stream" => { + current_structure.name() == next_structure.name() + } + _ => current == next, + } +} + fn segment_info(segment: &gst::Segment) -> SegmentInfo { match segment.downcast_ref::() { Some(time) => SegmentInfo { @@ -1380,50 +1377,52 @@ mod tests { assert!(first.finalize().unwrap(), "the first producer is still live"); } - // Renegotiation re-reserves the same explicit name. `build` finalizes the old producer first and the - // broadcast reclaims closed entries on insert, so the pad does not collide with itself. - #[test] - fn renegotiation_keeps_the_explicit_name() { + /// Caps the importer does not configure from keep the track live, so a reader subscribed before + /// the renegotiation reads on into the new caps rather than seeing the track end. + #[tokio::test] + async fn renegotiation_keeps_an_established_reader() { gst::init().unwrap(); let (broadcast, catalog) = producers(); let mut pad = Pad::new(); - assert_eq!( - pad.observe_caps(&broadcast, &catalog, producer_options(&h264_caps(), Some("camera")),), - CapsOutcome::Active("camera".to_string()) - ); + pad.observe_caps(&broadcast, &catalog, producer_options(&h264_caps(), Some("camera"))); + pad.observe_segment(time_segment()); + let mut subscriber = broadcast + .consume() + .track("camera") + .unwrap() + .subscribe(None) + .await + .unwrap(); + for ms in [0, 40] { + let pushed = pad.push_buffer(h264_keyframe_au(), Some(gst::ClockTime::from_mseconds(ms)), None, None); + assert_eq!(pushed, Ok(PushOutcome::Published)); + } + let renegotiated = gst::Caps::builder("video/x-h264") .field("stream-format", "byte-stream") .field("alignment", "au") .field("width", 1280i32) .build(); assert_eq!( - pad.observe_caps(&broadcast, &catalog, producer_options(&renegotiated, Some("camera")),), - CapsOutcome::Active("camera".to_string()), - "the same name is reserved again, not rejected as a duplicate" + pad.observe_caps(&broadcast, &catalog, producer_options(&renegotiated, Some("camera"))), + CapsOutcome::Unchanged ); - assert!(!pad.is_failed()); + let pushed = pad.push_buffer(h264_keyframe_au(), Some(gst::ClockTime::from_mseconds(80)), None, None); + assert_eq!(pushed, Ok(PushOutcome::Published)); + + // The reader may skip to the latest group, but never sees the track end before the new caps. + while subscriber.recv_group().await.unwrap().expect("the track continues").sequence != 2 {} } - /// A name always means the same content, so the renegotiated producer numbers its groups past - /// the ones its predecessor published under the name instead of restarting at 0. + /// A new codec config is new content: the old track ends on its name, and the replacement takes a + /// fresh one rather than reusing it. #[tokio::test] - async fn renegotiation_continues_past_the_predecessor_groups() { + async fn a_new_codec_config_takes_a_fresh_name() { gst::init().unwrap(); let (broadcast, catalog) = producers(); let mut pad = Pad::new(); pad.observe_caps(&broadcast, &catalog, producer_options(&h264_caps(), Some("camera"))); pad.observe_segment(time_segment()); - for ms in [0, 40] { - let pushed = pad.push_buffer(h264_keyframe_au(), Some(gst::ClockTime::from_mseconds(ms)), None, None); - assert_eq!(pushed, Ok(PushOutcome::Published)); - } - - let renegotiated = gst::Caps::builder("video/x-h264") - .field("stream-format", "byte-stream") - .field("alignment", "au") - .field("width", 1280i32) - .build(); - pad.observe_caps(&broadcast, &catalog, producer_options(&renegotiated, Some("camera"))); let mut subscriber = broadcast .consume() .track("camera") @@ -1431,72 +1430,36 @@ mod tests { .subscribe(None) .await .unwrap(); - let pushed = pad.push_buffer(h264_keyframe_au(), Some(gst::ClockTime::from_mseconds(80)), None, None); + let pushed = pad.push_buffer(h264_keyframe_au(), Some(gst::ClockTime::ZERO), None, None); assert_eq!(pushed, Ok(PushOutcome::Published)); - let group = subscriber.recv_group().await.unwrap().expect("a group"); - assert_eq!(group.sequence, 2, "groups 0 and 1 belong to the predecessor"); + let vp8 = gst::Caps::builder("video/x-vp8").build(); + assert_eq!( + pad.observe_caps(&broadcast, &catalog, producer_options(&vp8, Some("camera"))), + CapsOutcome::Active("camera/1".to_string()) + ); + assert_eq!(subscriber.recv_group().await.unwrap().expect("a group").sequence, 0); + assert!(subscriber.recv_group().await.unwrap().is_none(), "the old track ended"); } - /// A pad requested again under a released pad's name continues past its groups, even one that - /// ends without writing: its end still lands past them. - #[tokio::test] - async fn a_name_published_again_continues_past_its_predecessor() { + /// A run never publishes a name it ended again, whoever asks for it next: each replacement mints + /// the next free name. + #[test] + fn an_ended_name_is_never_published_again() { gst::init().unwrap(); let (broadcast, catalog) = producers(); - let continuations = Continuations::default(); - let publish = |frames: &[u64]| { + let names = Names::default(); + let publish = || { let mut pad = Pad::new(); - pad.reserve(&catalog, super::super::wall::Wall::new(&catalog), &continuations); - pad.observe_caps(&broadcast, &catalog, producer_options(&opaque_caps(), Some("levels"))); - pad.observe_segment(time_segment()); - for ms in frames { - let pushed = pad.push_buffer( - Bytes::from_static(b"level"), - Some(gst::ClockTime::from_mseconds(*ms)), - None, - None, - ); - assert_eq!(pushed, Ok(PushOutcome::Published)); - } - pad + pad.reserve(&catalog, super::super::wall::Wall::new(&catalog), &names); + let outcome = pad.observe_caps(&broadcast, &catalog, producer_options(&opaque_caps(), Some("levels"))); + assert!(pad.finalize().unwrap()); + outcome }; - let mut first = publish(&[0, 40]); - assert!(first.finalize().unwrap()); - - let mut second = publish(&[]); - let mut subscriber = broadcast - .consume() - .track("levels") - .unwrap() - .subscribe(None) - .await - .unwrap(); - let pushed = second.push_buffer( - Bytes::from_static(b"level"), - Some(gst::ClockTime::from_mseconds(80)), - None, - None, - ); - assert_eq!(pushed, Ok(PushOutcome::Published)); - assert_eq!(subscriber.recv_group().await.unwrap().expect("a group").sequence, 2); - assert!(second.finalize().unwrap()); - - let mut empty = publish(&[]); - let mut subscriber = broadcast - .consume() - .track("levels") - .unwrap() - .subscribe(None) - .await - .unwrap(); - assert!(empty.finalize().unwrap()); - assert_eq!(subscriber.recv_group().await.unwrap().expect("a group").sequence, 3); - assert!( - subscriber.recv_group().await.unwrap().is_none(), - "the track ends past group 3" - ); + assert_eq!(publish(), CapsOutcome::Active("levels".to_string())); + assert_eq!(publish(), CapsOutcome::Active("levels/1".to_string())); + assert_eq!(publish(), CapsOutcome::Active("levels/2".to_string())); } // AAC carries its config in caps; without codec_data the producer cannot be built. diff --git a/rs/moq-mux/src/codec/aac/import.rs b/rs/moq-mux/src/codec/aac/import.rs index ce59e90fd80..4fc34784137 100644 --- a/rs/moq-mux/src/codec/aac/import.rs +++ b/rs/moq-mux/src/codec/aac/import.rs @@ -80,11 +80,6 @@ impl Import { Ok(()) } - /// Where the next group opens: past every group published so far, or where a seek asked. - pub(crate) fn next_sequence(&self) -> u64 { - self.track.next_sequence() - } - /// Close the current group and open the next one at `sequence`. pub fn seek(&mut self, sequence: u64) -> crate::Result<()> { self.track.seek(sequence)?; diff --git a/rs/moq-mux/src/codec/av1/import.rs b/rs/moq-mux/src/codec/av1/import.rs index 4d4826045e7..f5f568b384c 100644 --- a/rs/moq-mux/src/codec/av1/import.rs +++ b/rs/moq-mux/src/codec/av1/import.rs @@ -233,11 +233,6 @@ impl Import { self.track.discontinuity() } - /// Where the next group opens: past every group published so far, or where a seek asked. - pub(crate) fn next_sequence(&self) -> u64 { - self.track.next_sequence() - } - /// Close the current group and open the next one at `sequence`. pub fn seek(&mut self, sequence: u64) -> Result<()> { self.track.seek(sequence)?; diff --git a/rs/moq-mux/src/codec/flac/import.rs b/rs/moq-mux/src/codec/flac/import.rs index 2e5ac7533fc..2527837cc6c 100644 --- a/rs/moq-mux/src/codec/flac/import.rs +++ b/rs/moq-mux/src/codec/flac/import.rs @@ -68,11 +68,6 @@ impl Import { self.track.discontinuity() } - /// Where the next group opens: past every group published so far, or where a seek asked. - pub(crate) fn next_sequence(&self) -> u64 { - self.track.next_sequence() - } - /// Close the current group and open the next one at `sequence`. pub fn seek(&mut self, sequence: u64) -> crate::Result<()> { self.track.seek(sequence)?; diff --git a/rs/moq-mux/src/codec/h264/import.rs b/rs/moq-mux/src/codec/h264/import.rs index 8ecfa806d16..786a7299d6c 100644 --- a/rs/moq-mux/src/codec/h264/import.rs +++ b/rs/moq-mux/src/codec/h264/import.rs @@ -158,11 +158,6 @@ impl Import { Ok(()) } - /// Where the next group opens: past every group published so far, or where a seek asked. - pub(crate) fn next_sequence(&self) -> u64 { - self.track.next_sequence() - } - /// Close the current group and open the next one at `sequence`. pub fn seek(&mut self, sequence: u64) -> Result<()> { self.track.seek(sequence)?; diff --git a/rs/moq-mux/src/codec/h265/import.rs b/rs/moq-mux/src/codec/h265/import.rs index 4e2765f23bb..5d1c6100c29 100644 --- a/rs/moq-mux/src/codec/h265/import.rs +++ b/rs/moq-mux/src/codec/h265/import.rs @@ -161,11 +161,6 @@ impl Import { Ok(()) } - /// Where the next group opens: past every group published so far, or where a seek asked. - pub(crate) fn next_sequence(&self) -> u64 { - self.track.next_sequence() - } - /// Close the current group and open the next one at `sequence`. pub fn seek(&mut self, sequence: u64) -> Result<()> { self.track.seek(sequence)?; diff --git a/rs/moq-mux/src/codec/mp3.rs b/rs/moq-mux/src/codec/mp3.rs index d4f33491cf3..f3b5d881075 100644 --- a/rs/moq-mux/src/codec/mp3.rs +++ b/rs/moq-mux/src/codec/mp3.rs @@ -157,11 +157,6 @@ impl Import { self.track.discontinuity() } - /// Where the next group opens: past every group published so far, or where a seek asked. - pub(crate) fn next_sequence(&self) -> u64 { - self.track.next_sequence() - } - /// Close the current group and open the next one at `sequence`. pub fn seek(&mut self, sequence: u64) -> crate::Result<()> { self.track.seek(sequence)?; diff --git a/rs/moq-mux/src/codec/opus/import.rs b/rs/moq-mux/src/codec/opus/import.rs index 6cf6fa5d943..a897c7cb4c6 100644 --- a/rs/moq-mux/src/codec/opus/import.rs +++ b/rs/moq-mux/src/codec/opus/import.rs @@ -73,11 +73,6 @@ impl Import { Ok(()) } - /// Where the next group opens: past every group published so far, or where a seek asked. - pub(crate) fn next_sequence(&self) -> u64 { - self.track.next_sequence() - } - /// Close the current group and open the next one at `sequence`. pub fn seek(&mut self, sequence: u64) -> crate::Result<()> { self.track.seek(sequence)?; diff --git a/rs/moq-mux/src/codec/vp8/import.rs b/rs/moq-mux/src/codec/vp8/import.rs index b344e81d4c7..4d96d603979 100644 --- a/rs/moq-mux/src/codec/vp8/import.rs +++ b/rs/moq-mux/src/codec/vp8/import.rs @@ -120,11 +120,6 @@ impl Import { self.track.discontinuity() } - /// Where the next group opens: past every group published so far, or where a seek asked. - pub(crate) fn next_sequence(&self) -> u64 { - self.track.next_sequence() - } - /// Close the current group and open the next one at `sequence`. pub fn seek(&mut self, sequence: u64) -> crate::Result<()> { self.track.seek(sequence)?; diff --git a/rs/moq-mux/src/codec/vp9/import.rs b/rs/moq-mux/src/codec/vp9/import.rs index 8e3164a8f73..f65b4764301 100644 --- a/rs/moq-mux/src/codec/vp9/import.rs +++ b/rs/moq-mux/src/codec/vp9/import.rs @@ -121,11 +121,6 @@ impl Import { self.track.discontinuity() } - /// Where the next group opens: past every group published so far, or where a seek asked. - pub(crate) fn next_sequence(&self) -> u64 { - self.track.next_sequence() - } - /// Close the current group and open the next one at `sequence`. pub fn seek(&mut self, sequence: u64) -> crate::Result<()> { self.track.seek(sequence)?; diff --git a/rs/moq-mux/src/container/producer.rs b/rs/moq-mux/src/container/producer.rs index 8d14e89a593..c8e9648db1b 100644 --- a/rs/moq-mux/src/container/producer.rs +++ b/rs/moq-mux/src/container/producer.rs @@ -343,15 +343,9 @@ where self.group.is_none() } - /// Where the next group opens: the sequence a [`seek`](Self::seek) asked for, else one past - /// the track's largest group, even an empty one. A replacement under the same track name - /// seeks here. - pub fn next_sequence(&self) -> u64 { - self.pending_sequence.unwrap_or_else(|| self.after_largest()) - } - - /// One past the track's largest group, even an empty one. - fn after_largest(&self) -> u64 { + /// One past the track's largest group, even an empty one: where a group continues after + /// what the track holds. + pub(crate) fn next_sequence(&self) -> u64 { self.inner.largest().map_or(0, |largest| largest.group + 1) } @@ -757,15 +751,6 @@ where /// Finish the track, flushing any buffered frames and closing any open group. pub fn finish(&mut self) -> crate::Result<()> { self.cut(None)?; - // A seek no group followed still ends past it, so a track continuing past a predecessor's - // groups never declares an end below them. The seek's group opens empty: an end past the - // live edge alone would leave readers waiting on the skipped sequences. - // A seek at or below the largest group is consumed too, so `next_sequence` stays past it. - if let Some(sequence) = self.pending_sequence.take() - && sequence > self.after_largest() - { - self.inner.create_group(moq_net::group::Info { sequence })?.finish()?; - } self.inner.finish()?; self.publish_estimate()?; // A finished track may be kept around, but its media stops holding the other tracks back. @@ -1449,39 +1434,6 @@ mod tests { assert_eq!(collect_sequences(consumer).await, vec![5, 6]); } - /// A track that continues past a predecessor's groups and finishes before writing any still - /// ends past them, so a relay holding the predecessor's groups accepts the end. - #[tokio::test] - async fn finish_after_seek_ends_past_the_seek() { - let track = track_producer("test", hang::container::track_info(hang::catalog::PRIORITY.video)); - let consumer = track.subscribe(replay()); - let mut producer = Producer::new(track, Container::Legacy(crate::container::Kind::Data)); - - producer.seek(7).unwrap(); - assert_eq!(producer.next_sequence(), 7); - producer.finish().unwrap(); - - assert_eq!(producer.next_sequence(), 8); - assert_eq!(collect_sequences(consumer).await, vec![7]); - } - - /// A seek below the track's groups that nothing followed adds no group, and still reports - /// past the largest one. - #[tokio::test] - async fn finish_after_stale_seek_ends_past_the_largest() { - let track = track_producer("test", hang::container::track_info(hang::catalog::PRIORITY.video)); - let consumer = track.subscribe(replay()); - let mut producer = Producer::new(track, Container::Legacy(crate::container::Kind::Data)); - - producer.seek(5).unwrap(); - producer.write(frame(0, true)).unwrap(); // seq 5 - producer.seek(3).unwrap(); - producer.finish().unwrap(); - - assert_eq!(producer.next_sequence(), 6); - assert_eq!(collect_sequences(consumer).await, vec![5]); - } - /// Records the frames handed to each `write`, so tests can inspect the /// durations the producer backfilled. Write-only. #[derive(Clone, Default)] diff --git a/rs/moq-mux/src/import/track.rs b/rs/moq-mux/src/import/track.rs index ac65f37fe28..2df16450b0a 100644 --- a/rs/moq-mux/src/import/track.rs +++ b/rs/moq-mux/src/import/track.rs @@ -464,12 +464,6 @@ impl Track { import!(&mut self.kind, import => import.seek(sequence)) } - /// Where the next group opens: past every group published so far, finished or not, or where a - /// [`seek`](Self::seek) asked. A replacement under the same track name seeks here. - pub fn next_sequence(&self) -> u64 { - import!(&self.kind, import => import.next_sequence()) - } - /// Drop the partial access unit of a format split from a byte stream. fn reset_split(&mut self) { match &mut self.kind { From 482a3c22c0bfe6a095427b566fbc1304f52a76cf Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 25 Oct 2026 16:59:10 -0700 Subject: [PATCH 6/7] style(gst): simplify same_importer and format Co-Authored-By: Claude Opus 5.5 --- rs/moq-gst/src/sink/imp.rs | 16 ++++------------ rs/moq-gst/src/sink/pad.rs | 17 +++++++++-------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/rs/moq-gst/src/sink/imp.rs b/rs/moq-gst/src/sink/imp.rs index 6018a79ad5f..a2a3e7c63ec 100644 --- a/rs/moq-gst/src/sink/imp.rs +++ b/rs/moq-gst/src/sink/imp.rs @@ -437,9 +437,7 @@ impl ElementImpl for MoqSink { if let Some(state) = control.live.as_ref() && let Some(catalog) = state.catalog.as_ref() { - pad.lifecycle() - .media - .reserve(catalog, state.wall.clone(), &state.names); + pad.lifecycle().media.reserve(catalog, state.wall.clone(), &state.names); } control.admissions.insert(pad.clone()); } @@ -890,9 +888,7 @@ impl MoqSink { if !lifecycle.releasing { lifecycle.ended = false; if let Some(catalog) = &state.catalog { - lifecycle - .media - .reserve(catalog, state.wall.clone(), &state.names); + lifecycle.media.reserve(catalog, state.wall.clone(), &state.names); } } } @@ -918,9 +914,7 @@ impl MoqSink { lifecycle.media.flush(); let catalog = state.catalog.as_ref(); if let Some(catalog) = catalog { - lifecycle - .media - .reserve(catalog, state.wall.clone(), &state.names); + lifecycle.media.reserve(catalog, state.wall.clone(), &state.names); } let caps = lifecycle.media.resumable_caps(); caps.zip(catalog).and_then(|(caps, catalog)| { @@ -1215,9 +1209,7 @@ fn observe_caps( lifecycle: &mut PadLifecycle, caps: &gst::Caps, ) -> (Option, bool) { - lifecycle - .media - .reserve(catalog, state.wall.clone(), &state.names); + lifecycle.media.reserve(catalog, state.wall.clone(), &state.names); let requested = lifecycle.requested().map(str::to_owned); let mut options = ProducerOptions::new(caps).with_container(lifecycle.container().into()); if let Some(track) = requested.as_deref() { diff --git a/rs/moq-gst/src/sink/pad.rs b/rs/moq-gst/src/sink/pad.rs index b1910ec3ab1..9ebbb7e5602 100644 --- a/rs/moq-gst/src/sink/pad.rs +++ b/rs/moq-gst/src/sink/pad.rs @@ -1214,15 +1214,11 @@ pub fn caps_supported(caps: &gst::CapsRef) -> bool { /// audio or text pad builds from its whole caps (the codec config, rate, channels, language), which /// is what a subscriber decodes or selects by, so any change there is new content. fn same_importer(current: &gst::CapsRef, next: &gst::CapsRef) -> bool { - let (Some(current_structure), Some(next_structure)) = (current.structure(0), next.structure(0)) else { + let (Some(was), Some(now)) = (current.structure(0), next.structure(0)) else { return false; }; - match next_structure.name().as_str() { - "video/x-h264" | "video/x-h265" | "video/x-av1" | "video/x-vp8" | "video/x-vp9" | "application/octet-stream" => { - current_structure.name() == next_structure.name() - } - _ => current == next, - } + let media = now.name().as_str(); + was.name() == now.name() && (media.starts_with("video/") || media == "application/octet-stream" || current == next) } fn segment_info(segment: &gst::Segment) -> SegmentInfo { @@ -1411,7 +1407,12 @@ mod tests { assert_eq!(pushed, Ok(PushOutcome::Published)); // The reader may skip to the latest group, but never sees the track end before the new caps. - while subscriber.recv_group().await.unwrap().expect("the track continues").sequence != 2 {} + loop { + let group = subscriber.recv_group().await.unwrap().expect("the track continues"); + if group.sequence == 2 { + break; + } + } } /// A new codec config is new content: the old track ends on its name, and the replacement takes a From 53848dbe9548f1dabeab86b2a347ec43cc52c72a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 25 Oct 2026 17:09:04 -0700 Subject: [PATCH 7/7] fix(gst): skip live names when minting a fresh track name A run now records every configured name it publishes, not only the ended ones, so restarting camera while another pad publishes camera/1 takes camera/2 instead of failing on the duplicate. Co-Authored-By: Claude Opus 5.5 --- rs/moq-gst/src/sink/pad.rs | 41 ++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/rs/moq-gst/src/sink/pad.rs b/rs/moq-gst/src/sink/pad.rs index 9ebbb7e5602..b817cd559d0 100644 --- a/rs/moq-gst/src/sink/pad.rs +++ b/rs/moq-gst/src/sink/pad.rs @@ -48,27 +48,31 @@ impl Sink { } } -/// The configured track names a run ended, plus the names it minted to replace them. +/// Every configured track name a run has published, and whether it ended. /// /// A name always means the same content, and a reader that saw a track end (here or at a relay) /// never follows the name into a successor. So a run never publishes an ended name again: a pad that /// would (a new codec config, a pad requested again, a stream resumed after EOS) publishes under the -/// first free `/` instead, as js/publish does. Generated names are never reused, so they -/// are not recorded. +/// first `/` the run has not used instead, as js/publish does. Generated names are never +/// reused, so they are not recorded. #[derive(Clone, Default)] -pub(super) struct Names(std::sync::Arc>>); +pub(super) struct Names(std::sync::Arc>>); impl Names { /// `name`, unless the run ended it: then the first `name/` the run has not used. + /// + /// A name another pad still publishes is returned as is, so the duplicate fails loudly. fn claim(&self, name: &str) -> String { - let mut taken = self.0.lock().unwrap(); - if !taken.contains(name) { + let mut names = self.0.lock().unwrap(); + if names.get(name) != Some(&true) { + names.entry(name.to_owned()).or_insert(false); return name.to_owned(); } let mut n = 1u64; loop { let minted = format!("{name}/{n}"); - if taken.insert(minted.clone()) { + if let std::collections::hash_map::Entry::Vacant(entry) = names.entry(minted.clone()) { + entry.insert(false); return minted; } n += 1; @@ -76,7 +80,7 @@ impl Names { } fn end(&self, name: String) { - self.0.lock().unwrap().insert(name); + self.0.lock().unwrap().insert(name, true); } } @@ -1463,6 +1467,27 @@ mod tests { assert_eq!(publish(), CapsOutcome::Active("levels/2".to_string())); } + /// A fresh name skips one another pad publishes, rather than colliding with it. + #[test] + fn a_fresh_name_skips_a_live_one() { + gst::init().unwrap(); + let (broadcast, catalog) = producers(); + let names = Names::default(); + let publish = |name| { + let mut pad = Pad::new(); + pad.reserve(&catalog, super::super::wall::Wall::new(&catalog), &names); + let outcome = pad.observe_caps(&broadcast, &catalog, producer_options(&opaque_caps(), Some(name))); + (pad, outcome) + }; + + let (mut levels, _) = publish("levels"); + let (_live, outcome) = publish("levels/1"); + assert_eq!(outcome, CapsOutcome::Active("levels/1".to_string())); + assert!(levels.finalize().unwrap()); + let (_again, outcome) = publish("levels"); + assert_eq!(outcome, CapsOutcome::Active("levels/2".to_string())); + } + // AAC carries its config in caps; without codec_data the producer cannot be built. #[test] fn aac_without_codec_data_fails_the_pad() {