From fe72875f5a0752cc1734f9c06549e21a4d480abe Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 23:30:55 -0800 Subject: [PATCH 01/11] chore(quest): claim quest/m1/explicit-state/ffi-state.md Quest-Claim: b835c566fc74c4ce From e797a4ae92d59a9992062e57cf7cf296bb98e6bc Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 23:53:03 -0800 Subject: [PATCH 02/11] fix(moq-ffi): handles hold one lifecycle state each The video producer's track, bandwidth claim, and follower live in one state under one lock; a failed disable leaves the catalog and encoder agreeing; a driver dropped by shutdown reads Cancelled to sync calls; one-shot requests answer once and report a failed answer. Co-Authored-By: Claude Opus 5.5 --- py/moq-rs/tests/test_socket.py | 2 +- rs/moq-ffi/src/datagram.rs | 28 +- rs/moq-ffi/src/ffi.rs | 170 +++++++++--- rs/moq-ffi/src/media.rs | 73 ++--- rs/moq-ffi/src/origin.rs | 39 ++- rs/moq-ffi/src/producer.rs | 53 ++-- rs/moq-ffi/src/server.rs | 33 +-- rs/moq-ffi/src/socket.rs | 38 +-- rs/moq-ffi/src/test.rs | 54 +++- rs/moq-ffi/src/video.rs | 481 ++++++++++++++++++++------------- 10 files changed, 618 insertions(+), 353 deletions(-) diff --git a/py/moq-rs/tests/test_socket.py b/py/moq-rs/tests/test_socket.py index ed5a6c56b34..ea44a5c8717 100644 --- a/py/moq-rs/tests/test_socket.py +++ b/py/moq-rs/tests/test_socket.py @@ -74,7 +74,7 @@ async def test_refusal_and_cancelled_wait_leave_endpoint_usable(): incoming.reject() with pytest.raises(moq.Error.NotFound): await dial - with pytest.raises(moq.Error.Closed): + with pytest.raises(moq.Error.AlreadyResponded): incoming.reject() client, server = await tcp(a, b) assert client.session == server.session diff --git a/rs/moq-ffi/src/datagram.rs b/rs/moq-ffi/src/datagram.rs index e4be80f3139..49fc204c9fe 100644 --- a/rs/moq-ffi/src/datagram.rs +++ b/rs/moq-ffi/src/datagram.rs @@ -5,7 +5,7 @@ use std::task::{Poll, ready}; use crate::consumer::MoqBroadcastConsumer; use crate::error::MoqError; -use crate::ffi::{Background, Polled}; +use crate::ffi::{Background, OneShot, Polled}; use crate::media::{MoqDatagram, MoqTimestamp}; use crate::producer::{Child, Children, MoqBroadcastProducer}; @@ -264,7 +264,7 @@ impl MoqDatagramRequests { .wait(|requests, waiter| Poll::Ready(Ok(ready!(requests.poll_requested(waiter))?))) .await?; Ok(Arc::new(MoqDatagramRequest { - inner: std::sync::Mutex::new(Some(request)), + inner: OneShot::new(request), children: self.children.clone(), })) } @@ -278,7 +278,7 @@ impl MoqDatagramRequests { /// A requested datagram track awaiting acceptance or rejection. #[derive(uniffi::Object)] pub struct MoqDatagramRequest { - inner: std::sync::Mutex>, + inner: OneShot, children: Children, } @@ -286,34 +286,28 @@ pub struct MoqDatagramRequest { impl MoqDatagramRequest { /// The requested datagram track's name. pub fn name(&self) -> Result { - Ok(self - .inner - .lock() - .unwrap() - .as_ref() - .ok_or(MoqError::Closed)? - .name() - .to_owned()) + self.inner.peek(|request| request.name().to_owned()) } /// The highest priority among the consumers waiting for this track. pub fn priority(&self) -> Result, MoqError> { - Ok(self.inner.lock().unwrap().as_ref().ok_or(MoqError::Closed)?.priority()) + self.inner.peek(|request| request.priority()) } /// Accept the request with its fixed publisher properties. pub fn accept(&self, info: Option) -> Result, MoqError> { let _guard = crate::ffi::enter(); let info = info.unwrap_or_default().try_into()?; - let request = self.inner.lock().unwrap().take().ok_or(MoqError::Closed)?; - MoqDatagramProducer::child(request.accept(info), &self.children) + self.inner + .answer(|request| MoqDatagramProducer::child(request.accept(info), &self.children)) } /// Reject this request with an application error code. pub fn reject(&self, error_code: u16) -> Result<(), MoqError> { let _guard = crate::ffi::enter(); - let request = self.inner.lock().unwrap().take().ok_or(MoqError::Closed)?; - request.reject(moq_net::Error::App(error_code)); - Ok(()) + self.inner.answer(|request| { + request.reject(moq_net::Error::App(error_code)); + Ok(()) + }) } } diff --git a/rs/moq-ffi/src/ffi.rs b/rs/moq-ffi/src/ffi.rs index f35ccbc804f..2d681b037ce 100644 --- a/rs/moq-ffi/src/ffi.rs +++ b/rs/moq-ffi/src/ffi.rs @@ -475,50 +475,66 @@ impl Background { }) } + /// Read the operation's completion, arming `waiter` while it runs. Every reader goes through + /// here, so a synchronous check and an awaited one agree on each state. + fn completion(&self, waiter: &kio::Waiter) -> Completion { + let Some(result) = &self.result else { + return Completion::Detached; + }; + let completed = result.poll(waiter, |state| match &**state { + Some(completed) => Poll::Ready(completed.clone()), + None => Poll::Pending, + }); + match completed { + Poll::Pending => Completion::Running, + Poll::Ready(Ok(completed)) => Completion::Done(completed), + Poll::Ready(Err(_)) => Completion::Dropped, + } + } + pub async fn finished(&self) -> Result<(), MoqError> { - let Some(result) = &self.result else { return Ok(()) }; - let completed = host(kio::wait(|waiter| { - match std::task::ready!(result.poll(waiter, |state| { - match &**state { - Some(completed) => std::task::Poll::Ready(completed.clone()), - None => std::task::Poll::Pending, - } - })) { - Ok(completed) => std::task::Poll::Ready(completed), - Err(_) => std::task::Poll::Ready(Err(moq_net::Error::Cancel.into())), - } + host(kio::wait(|waiter| match self.completion(waiter).result() { + Some(result) => Poll::Ready(result), + None => Poll::Pending, })) - .await; - completed.map_err(crate::session::map_closed_error) + .await + } +} + +/// Where an owned operation stands, as its handle's calls report it. +enum Completion { + /// The handle owns no operation, so nothing can fail it. + Detached, + Running, + Done(moq_tokio::Result<()>), + /// Released without a result: [`shutdown`] dropped it, or it was submitted after and never ran. + Dropped, +} + +impl Completion { + /// What the handle's calls report, or `None` while the operation runs. + fn result(self) -> Option> { + match self { + Self::Detached => Some(Ok(())), + Self::Running => None, + Self::Done(result) => Some(result.map_err(crate::session::map_closed_error)), + Self::Dropped => Some(Err(MoqError::Cancelled)), + } } } impl Background { + /// The operation's failure, if it has failed or was dropped; `Ok` while it runs. pub fn check(&self) -> Result<(), MoqError> { - if let Some(result) = &self.result { - return result - .read() - .clone() - .unwrap_or(Ok(())) - .map_err(crate::session::map_closed_error); - } - Ok(()) + self.completion(&kio::Waiter::noop()).result().unwrap_or(Ok(())) } /// Ready with the owner's failure once it fails; pending while it runs or after it ended /// cleanly, the same split [`Self::watch`] makes. pub fn poll_failed(&self, waiter: &kio::Waiter) -> Poll { - let Some(result) = &self.result else { - return Poll::Pending; - }; - let completed = ready!(result.poll(waiter, |state| match &**state { - Some(completed) => Poll::Ready(completed.clone()), - None => Poll::Pending, - })); - match completed { - Ok(Ok(())) => Poll::Pending, - Ok(Err(err)) => Poll::Ready(crate::session::map_closed_error(err)), - Err(_) => Poll::Ready(crate::session::map_closed_error(moq_net::Error::Cancel.into())), + match self.completion(waiter).result() { + Some(Err(err)) => Poll::Ready(err), + Some(Ok(())) | None => Poll::Pending, } } @@ -778,6 +794,96 @@ impl Drop for Polled { } } +/// A one-shot request a handle answers once: pending, being answered, answered, or failed. +/// +/// Every answer goes through [`Self::answer`] or [`Self::take`], so a failure after the request +/// was taken is what later calls report, rather than looking like a successful answer. +pub(crate) struct OneShot(std::sync::Mutex>); + +enum Answer { + Pending(T), + /// Taken by an answer that has not finished. + Taken, + Answered, + /// The answer failed after taking the request, or stopped before it finished. + Failed(MoqError), +} + +impl OneShot { + pub fn new(request: T) -> Self { + Self(std::sync::Mutex::new(Answer::Pending(request))) + } + + fn lock(&self) -> std::sync::MutexGuard<'_, Answer> { + // Nothing panics under this lock: every answer runs outside it. + self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner) + } + + /// Read the pending request, or fail with why it can no longer be answered. + pub fn peek(&self, f: impl FnOnce(&T) -> R) -> Result { + match &*self.lock() { + Answer::Pending(request) => Ok(f(request)), + Answer::Taken | Answer::Answered => Err(MoqError::AlreadyResponded), + Answer::Failed(err) => Err(err.clone()), + } + } + + /// Answer with `f`, which consumes the request: answered or failed once this returns. + /// + /// Validate the answer's inputs first, so a refused input leaves the request pending. + pub fn answer(&self, f: impl FnOnce(T) -> Result) -> Result { + let (request, answering) = self.take()?; + answering.settle(f(request)) + } + + /// Take the request for an answer that awaits, settling the returned guard with its outcome. + pub fn take(&self) -> Result<(T, Answering<'_, T>), MoqError> { + let mut state = self.lock(); + match std::mem::replace(&mut *state, Answer::Taken) { + Answer::Pending(request) => Ok((request, Answering { owner: Some(self) })), + Answer::Taken => Err(MoqError::AlreadyResponded), + Answer::Answered => { + *state = Answer::Answered; + Err(MoqError::AlreadyResponded) + } + Answer::Failed(err) => { + *state = Answer::Failed(err.clone()); + Err(err) + } + } + } +} + +/// An answer in flight. Dropped unsettled, a cancelled or panicked call, it fails the request. +pub(crate) struct Answering<'a, T> { + /// Taken by [`Self::settle`]. + owner: Option<&'a OneShot>, +} + +impl Answering<'_, T> { + /// Record the answer's outcome and return it. + pub fn settle(mut self, result: Result) -> Result { + if let Some(owner) = self.owner.take() { + *owner.lock() = match &result { + Ok(_) => Answer::Answered, + Err(err) => Answer::Failed(err.clone()), + }; + } + result + } +} + +impl Drop for Answering<'_, T> { + fn drop(&mut self) { + if let Some(owner) = self.owner.take() { + *owner.lock() = Answer::Failed(match std::thread::panicking() { + true => crate::session::map_closed_error(moq_tokio::Error::Panic), + false => MoqError::Cancelled, + }); + } + } +} + /// Counts one [`Polled::wait`] call until it is dropped. struct Call<'a>(&'a AtomicUsize); diff --git a/rs/moq-ffi/src/media.rs b/rs/moq-ffi/src/media.rs index 7753c9b1b3e..9a95920d796 100644 --- a/rs/moq-ffi/src/media.rs +++ b/rs/moq-ffi/src/media.rs @@ -780,14 +780,17 @@ pub enum MoqMediaTarget { } impl MoqMediaTarget { - fn resolve( + /// Import onto the target's track: a newly reserved one, or the requested one, whose later + /// calls then report a failed import. + fn import( self, broadcast: &moq_net::broadcast::Producer, - format: &impl std::fmt::Display, - ) -> Result { + format: impl std::fmt::Display, + import: impl FnOnce(moq_net::track::Request) -> Result, + ) -> Result { match self { - Self::Named { name } => reserve_track(broadcast, name, format), - Self::Requested { request } => request.take(), + Self::Named { name } => import(reserve_track(broadcast, name, &format)?), + Self::Requested { request } => request.answer(import), } } } @@ -1006,14 +1009,14 @@ impl MoqMediaTrackProducer { let _guard = crate::ffi::enter(); broadcast.with_state(|state| { let init: moq_mux::import::AudioInit = init.into(); - let request = target.resolve(&state.broadcast, &init.format)?; - - let import = moq_mux::import::Track::audio(request, state.catalog.reserve(), init).map_err(|err| { - MoqError::Codec { - reason: format!("init failed: {err}"), - } - })?; - MoqMediaTrackProducer::from_import(import, None, &state.children) + target.import(&state.broadcast, init.format, |request| { + let import = moq_mux::import::Track::audio(request, state.catalog.reserve(), init).map_err(|err| { + MoqError::Codec { + reason: format!("init failed: {err}"), + } + })?; + MoqMediaTrackProducer::from_import(import, None, &state.children) + }) }) } } @@ -1034,14 +1037,14 @@ impl MoqMediaTrackProducer { let init = init.resolve()?; broadcast.with_state(|state| { let priority = init.priority; - let request = target.resolve(&state.broadcast, &init.format)?; - - let import = moq_mux::import::Track::video(request, state.catalog.reserve(), init).map_err(|err| { - MoqError::Codec { - reason: format!("init failed: {err}"), - } - })?; - MoqMediaTrackProducer::from_import(import, Some(priority), &state.children) + target.import(&state.broadcast, init.format, |request| { + let import = moq_mux::import::Track::video(request, state.catalog.reserve(), init).map_err(|err| { + MoqError::Codec { + reason: format!("init failed: {err}"), + } + })?; + MoqMediaTrackProducer::from_import(import, Some(priority), &state.children) + }) }) } } @@ -1087,22 +1090,20 @@ impl MoqMediaTrackStreamProducer { let init = init.resolve()?; broadcast.with_state(|state| { let priority = init.priority; - let request = target.resolve(&state.broadcast, &init.format)?; - - let import = - moq_mux::import::TrackStream::video(request, state.catalog.reserve(), init).map_err(|err| { - MoqError::Codec { + target.import(&state.broadcast, init.format, |request| { + let import = moq_mux::import::TrackStream::video(request, state.catalog.reserve(), init).map_err( + |err| MoqError::Codec { reason: format!("init failed: {err}"), - } - })?; - - state.children.adopt(Arc::new(MoqMediaTrackStreamProducer { - inner: std::sync::Mutex::new(Some(MediaStreamProducer { - import: Box::new(import), - priority, - })), - children: state.children.clone(), - })) + }, + )?; + state.children.adopt(Arc::new(MoqMediaTrackStreamProducer { + inner: std::sync::Mutex::new(Some(MediaStreamProducer { + import: Box::new(import), + priority, + })), + children: state.children.clone(), + })) + }) }) } } diff --git a/rs/moq-ffi/src/origin.rs b/rs/moq-ffi/src/origin.rs index 36b262da812..cbec355169c 100644 --- a/rs/moq-ffi/src/origin.rs +++ b/rs/moq-ffi/src/origin.rs @@ -5,7 +5,7 @@ use std::task::{Poll, ready}; use crate::consumer::MoqBroadcastConsumer; use crate::error::MoqError; -use crate::ffi::{Background, Polled}; +use crate::ffi::{Background, OneShot, Polled}; use crate::producer::MoqBroadcastProducer; /// Config used when creating an origin. @@ -160,7 +160,7 @@ pub struct MoqRoute { #[derive(uniffi::Object)] /// A pending broadcast request on a prefix route, answered by serving, routing, or rejecting it. pub struct MoqBroadcastRequest { - inner: std::sync::Mutex>, + inner: OneShot, driver: Background, } @@ -532,23 +532,17 @@ impl MoqRoute { impl MoqBroadcastRequest { fn new(request: moq_net::origin::Request, driver: Background) -> Self { Self { - inner: std::sync::Mutex::new(Some(request)), + inner: OneShot::new(request), driver, } } - - fn take(&self) -> Result { - self.inner.lock().unwrap().take().ok_or(MoqError::Closed) - } } #[uniffi::export] impl MoqBroadcastRequest { /// The requested broadcast path. pub fn path(&self) -> Result { - let guard = self.inner.lock().unwrap(); - let request = guard.as_ref().ok_or(MoqError::Closed)?; - Ok(request.path().to_string()) + self.inner.peek(|request| request.path().to_string()) } /// Answer with the path's own exact route, owning a new broadcast. @@ -562,10 +556,11 @@ impl MoqBroadcastRequest { let _guard = crate::ffi::enter(); let (options, visible) = MoqRoute::exact_options(options)?; self.driver.check()?; - let request = self.take()?; - let result = request.route(options); - self.driver.check()?; - Ok(Arc::new(MoqRoute::exact(result?, self.driver.clone(), visible)?)) + self.inner.answer(|request| { + let result = request.route(options); + self.driver.check()?; + Ok(Arc::new(MoqRoute::exact(result?, self.driver.clone(), visible)?)) + }) } /// Serve a held broadcast under the prefix route's instance without announcing an exact route. @@ -576,17 +571,19 @@ impl MoqBroadcastRequest { let _guard = crate::ffi::enter(); self.driver.check()?; broadcast.driver.check()?; - self.take()?.serve(&broadcast.inner); - self.driver.check()?; - Ok(()) + self.inner.answer(|request| { + request.serve(&broadcast.inner); + self.driver.check() + }) } /// Reject the request with an application error code. pub fn reject(&self, error_code: u16) -> Result<(), MoqError> { let _guard = crate::ffi::enter(); - let request = self.take()?; - request.reject(moq_net::Error::App(error_code)); - Ok(()) + self.inner.answer(|request| { + request.reject(moq_net::Error::App(error_code)); + Ok(()) + }) } } @@ -665,7 +662,7 @@ mod supervision_tests { let (resolved, ()) = tokio::join!(consumer.request("room/camera".into(), None), async { let request = requests.requested().await.unwrap().unwrap(); request.serve(&held).unwrap(); - assert!(matches!(request.serve(&held), Err(MoqError::Closed))); + assert!(matches!(request.serve(&held), Err(MoqError::AlreadyResponded))); }); assert!(resolved.is_ok()); assert!(!held.inner.is_closed(), "serving leaves the caller's consumer usable"); diff --git a/rs/moq-ffi/src/producer.rs b/rs/moq-ffi/src/producer.rs index 6aecedbf9d5..4c25c754d3e 100644 --- a/rs/moq-ffi/src/producer.rs +++ b/rs/moq-ffi/src/producer.rs @@ -8,7 +8,7 @@ use moq_mux::catalog::hang::Extra; use crate::consumer::{MoqBroadcastConsumer, MoqGroupConsumer, MoqOrder, MoqSubscription, MoqTrackConsumer}; use crate::demand::MoqTrackDemand; use crate::error::MoqError; -use crate::ffi::{Background, Polled}; +use crate::ffi::{Background, OneShot, Polled}; use crate::media::MoqFrame; /// A location within a track, mirroring [`moq_net::track::Position`]. @@ -583,22 +583,25 @@ impl MoqFill { /// or [`reject`](Self::reject) it to fail the waiting subscriber. #[derive(uniffi::Object)] pub struct MoqTrackRequest { - inner: std::sync::Mutex>, + inner: OneShot, children: Children, } impl MoqTrackRequest { pub(crate) fn new(request: moq_net::track::Request, children: Children) -> Self { Self { - inner: std::sync::Mutex::new(Some(request)), + inner: OneShot::new(request), children, } } - /// Take the inner request so an importer can accept it (setting the timescale). Used by - /// [`crate::media::MoqMediaTrackProducer::audio`]. - pub(crate) fn take(&self) -> Result { - self.inner.lock().unwrap().take().ok_or(MoqError::Closed) + /// Answer with an importer that accepts the request itself (setting the timescale), as + /// [`crate::media::MoqMediaTrackProducer::audio`] does. + pub(crate) fn answer( + &self, + f: impl FnOnce(moq_net::track::Request) -> Result, + ) -> Result { + self.inner.answer(f) } } @@ -606,15 +609,12 @@ impl MoqTrackRequest { impl MoqTrackRequest { /// The requested track name. pub fn name(&self) -> Result { - let guard = self.inner.lock().unwrap(); - let request = guard.as_ref().ok_or(MoqError::Closed)?; - Ok(request.name().to_string()) + self.inner.peek(|request| request.name().to_string()) } /// Watch this track's subscribers, including after the request is accepted. pub fn demand(&self) -> Result, MoqError> { - let guard = self.inner.lock().unwrap(); - Ok(MoqTrackDemand::new(guard.as_ref().ok_or(MoqError::Closed)?.demand())) + self.inner.peek(|request| MoqTrackDemand::new(request.demand())) } /// Create a handler for uncached group fetches before accepting this track. @@ -624,9 +624,8 @@ impl MoqTrackRequest { /// the transition from request to producer. pub fn requests(&self) -> Result, MoqError> { let _guard = crate::ffi::enter(); - let guard = self.inner.lock().unwrap(); - let request = guard.as_ref().ok_or(MoqError::Closed)?; - Ok(Arc::new(MoqTrackRequests::new(request.requests()))) + self.inner + .peek(|request| Arc::new(MoqTrackRequests::new(request.requests()))) } /// Accept the request as a raw track, fixing its [`MoqTrackInfo`] (timescale, etc.). @@ -638,22 +637,24 @@ impl MoqTrackRequest { pub fn accept(&self, info: Option) -> Result, MoqError> { let _guard = crate::ffi::enter(); let info = raw_track_info(info)?; - let request = self.take()?; - // Without a handle keeping it open, the broadcast has ended. - let Some(_broadcast) = self.children.broadcast() else { - request.reject(moq_net::Error::Unroutable); - return Err(MoqError::Closed); - }; - let track = request.accept(Some(info)); - MoqTrackProducer::child(track, &self.children) + self.inner.answer(|request| { + // Without a handle keeping it open, the broadcast has ended. + let Some(_broadcast) = self.children.broadcast() else { + request.reject(moq_net::Error::Unroutable); + return Err(MoqError::Closed); + }; + let track = request.accept(Some(info)); + MoqTrackProducer::child(track, &self.children) + }) } /// Reject the request with an application error code, failing the waiting subscriber. pub fn reject(&self, error_code: u16) -> Result<(), MoqError> { let _guard = crate::ffi::enter(); - let request = self.take()?; - request.reject(moq_net::Error::App(error_code)); - Ok(()) + self.inner.answer(|request| { + request.reject(moq_net::Error::App(error_code)); + Ok(()) + }) } } diff --git a/rs/moq-ffi/src/server.rs b/rs/moq-ffi/src/server.rs index 3f5b9210f72..d6f30d5ad76 100644 --- a/rs/moq-ffi/src/server.rs +++ b/rs/moq-ffi/src/server.rs @@ -4,7 +4,7 @@ use std::path::PathBuf; use std::sync::Arc; use crate::error::MoqError; -use crate::ffi::Task; +use crate::ffi::{OneShot, Task}; use crate::origin::MoqOriginProducer; use crate::session::{MoqQuicConfig, MoqSession}; @@ -338,7 +338,7 @@ impl MoqServer { } struct RequestState { - request: Option, + request: OneShot, publish: Option>, consume: Option>, } @@ -397,8 +397,8 @@ mod transport_tests { /// An incoming MoQ session that can be accepted or rejected. /// /// Origin arguments are captured when [`accept`](Self::accept) starts. A second -/// response fails with [`MoqError::AlreadyResponded`], and calls after -/// [`cancel`](Self::cancel) fail with [`MoqError::Cancelled`]. +/// response fails with [`MoqError::AlreadyResponded`], or with the first one's error if it +/// failed, and calls after [`cancel`](Self::cancel) fail with [`MoqError::Cancelled`]. #[derive(uniffi::Object)] pub struct MoqRequest { task: Task, @@ -422,7 +422,7 @@ impl MoqRequest { let peer_implementation = request.peer_implementation().map(str::to_string); Ok(Arc::new(Self { task: Task::new(RequestState { - request: Some(request), + request: OneShot::new(request), publish, consume, }), @@ -490,7 +490,8 @@ impl MoqRequest { /// /// A null origin inherits the server's configured origin; a supplied origin replaces it. /// Pass a fresh origin for isolation, or the same fresh origin on both sides to share it. - /// Returns `AlreadyResponded` after a response and `Cancelled` after cancellation. + /// Returns `AlreadyResponded` after a response, the response's error if it failed, and + /// `Cancelled` after cancellation. #[uniffi::method(default(publish = None, consume = None))] pub async fn accept( &self, @@ -500,9 +501,9 @@ impl MoqRequest { // Foreign runtime threads can have a 512 KiB stack. Keep the transport // handshake out of the nested Task::run futures polled on that stack. self.task - .run(move |mut state| { + .run(move |state| { Box::pin(async move { - let request = state.request.take().ok_or(MoqError::AlreadyResponded)?; + let (request, answering) = state.request.take()?; // Materialize both origin sides so the session can publish/subscribe and the // FFI can hand back a publisher/consumer. let (publish, subscribe) = crate::origin::resolve_pair( @@ -516,8 +517,8 @@ impl MoqRequest { .await .map_err(|err| MoqError::Connect { reason: format!("{err}"), - })?; - Ok(Arc::new(MoqSession::accepted(session, publish, subscribe))) + }); + answering.settle(session.map(|session| Arc::new(MoqSession::accepted(session, publish, subscribe)))) }) }) .await @@ -528,19 +529,19 @@ impl MoqRequest { /// Codes 401 and 403 map to the protocol's unauthorized error; every other /// code is sent as an application error. /// - /// Returns `AlreadyResponded` if `accept()` or `reject()` has already been called. + /// Returns `AlreadyResponded` if `accept()` or `reject()` has already been called, or that + /// call's error if it failed. pub async fn reject(&self, code: u16) -> Result<(), MoqError> { self.task - .run(move |mut state| async move { - let request = state.request.take().ok_or(MoqError::AlreadyResponded)?; + .run(move |state| async move { + let (request, answering) = state.request.take()?; let reject = match code { 401 | 403 => moq_net::SessionError::Unauthorized, code => moq_net::SessionError::App(code), }; - request.reject(reject).await.map_err(|err| MoqError::Reject { + answering.settle(request.reject(reject).await.map_err(|err| MoqError::Reject { reason: format!("{err}"), - })?; - Ok(()) + })) }) .await } diff --git a/rs/moq-ffi/src/socket.rs b/rs/moq-ffi/src/socket.rs index 6f78a6d9b1a..5100edbb5a0 100644 --- a/rs/moq-ffi/src/socket.rs +++ b/rs/moq-ffi/src/socket.rs @@ -1,11 +1,11 @@ //! Point-to-point socket endpoints and their two protocol-specific streams. -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use std::task::{Poll, ready}; use crate::consumer::{MoqGroupConsumer, MoqTrackConsumer}; use crate::error::MoqError; -use crate::ffi::{Background, Polled}; +use crate::ffi::{Background, OneShot, Polled}; use crate::origin::MoqOriginProducer; use crate::producer::{MoqGroupProducer, MoqTrackInfo, MoqTrackProducer}; @@ -68,7 +68,7 @@ impl MoqSocketEndpoint { Ok(Arc::new(MoqSocketIncomingTcp { peer: inner.peer().into(), session: inner.session().into(), - inner: Mutex::new(Some(inner)), + inner: OneShot::new(inner), driver: self.driver.clone(), })) }) @@ -100,7 +100,7 @@ impl MoqSocketEndpoint { Ok(Arc::new(MoqSocketIncomingTrack { peer: inner.peer().into(), session: inner.session().into(), - inner: Mutex::new(Some(inner)), + inner: OneShot::new(inner), driver: self.driver.clone(), })) }) @@ -117,7 +117,7 @@ impl Drop for MoqSocketEndpoint { /// An incoming `tcp` dial, consumed by accepting or rejecting it. #[derive(uniffi::Object)] pub struct MoqSocketIncomingTcp { - inner: Mutex>>, + inner: OneShot>, peer: String, session: String, driver: Background, @@ -136,19 +136,21 @@ impl MoqSocketIncomingTcp { /// Verify return routability and accept; cancelling resets the dial. pub async fn accept(&self) -> Result, MoqError> { self.driver.check()?; - let incoming = self.inner.lock().unwrap().take().ok_or(MoqError::Closed)?; + let (incoming, answering) = self.inner.take()?; let accepting = Polled::new(incoming.accept()).supervised(self.driver.clone()); let stream = accepting .wait(|inner, waiter| Poll::Ready(Ok(ready!(inner.poll_ok(waiter))?))) - .await?; - Ok(Arc::new(MoqSocketTcpStream::new(stream, self.driver.clone()))) + .await; + answering.settle(stream.map(|stream| Arc::new(MoqSocketTcpStream::new(stream, self.driver.clone())))) } /// Refuse the dial as not found. pub fn reject(&self) -> Result<(), MoqError> { let _guard = crate::ffi::enter(); self.driver.check()?; - self.inner.lock().unwrap().take().ok_or(MoqError::Closed)?.reject(); - Ok(()) + self.inner.answer(|incoming| { + incoming.reject(); + Ok(()) + }) } } @@ -197,7 +199,7 @@ impl MoqSocketTcpStream { /// An incoming `track` dial, consumed by accepting or rejecting it. #[derive(uniffi::Object)] pub struct MoqSocketIncomingTrack { - inner: Mutex>>, + inner: OneShot>, peer: String, session: String, driver: Background, @@ -217,19 +219,21 @@ impl MoqSocketIncomingTrack { pub async fn accept(&self, info: Option) -> Result, MoqError> { self.driver.check()?; let info = info.map(TryInto::try_into).transpose()?; - let incoming = self.inner.lock().unwrap().take().ok_or(MoqError::Closed)?; + let (incoming, answering) = self.inner.take()?; let accepting = Polled::new(incoming.accept(info)).supervised(self.driver.clone()); let stream = accepting .wait(|inner, waiter| Poll::Ready(Ok(ready!(inner.poll_ok(waiter))?))) - .await?; - Ok(Arc::new(MoqSocketTrackStream::new(stream, self.driver.clone()))) + .await; + answering.settle(stream.map(|stream| Arc::new(MoqSocketTrackStream::new(stream, self.driver.clone())))) } /// Refuse the dial as not found. pub fn reject(&self) -> Result<(), MoqError> { let _guard = crate::ffi::enter(); self.driver.check()?; - self.inner.lock().unwrap().take().ok_or(MoqError::Closed)?.reject(); - Ok(()) + self.inner.answer(|incoming| { + incoming.reject(); + Ok(()) + }) } } @@ -370,7 +374,7 @@ mod tests { let incoming = b.accept_tcp().await.unwrap(); assert_eq!(incoming.peer(), "a"); incoming.reject().unwrap(); - assert!(matches!(incoming.reject(), Err(MoqError::Closed))); + assert!(matches!(incoming.reject(), Err(MoqError::AlreadyResponded))); }); assert!(matches!(dialed, Err(MoqError::NotFound))); let _ = tcp(&a, &b).await; diff --git a/rs/moq-ffi/src/test.rs b/rs/moq-ffi/src/test.rs index 269284b3613..cb875cd0e24 100644 --- a/rs/moq-ffi/src/test.rs +++ b/rs/moq-ffi/src/test.rs @@ -1953,7 +1953,7 @@ async fn track_request_demand_survives_accept() { assert_eq!(demand.name(), "events"); assert!(demand.wanted().is_some()); let track = request.accept(None).unwrap(); - assert!(matches!(request.demand(), Err(MoqError::Closed))); + assert!(matches!(request.demand(), Err(MoqError::AlreadyResponded))); let consumer = subscribe.await.unwrap().unwrap(); demand.until_wanted().await.unwrap(); consumer.cancel(); @@ -2003,7 +2003,7 @@ async fn track_request_demand_ends_on_rejection() { request.reject(404).unwrap(); assert!(subscribe.await.unwrap().is_err()); assert!(demand.until_wanted().await.is_err()); - assert!(matches!(request.demand(), Err(MoqError::Closed))); + assert!(matches!(request.demand(), Err(MoqError::AlreadyResponded))); } #[tokio::test] @@ -2305,7 +2305,7 @@ async fn dynamic_track_request_can_abort() { .unwrap(); track.reject(404).unwrap(); - assert!(matches!(track.name(), Err(MoqError::Closed))); + assert!(matches!(track.name(), Err(MoqError::AlreadyResponded))); let result = kio::clock::timeout(TIMEOUT, subscribe) .await @@ -3128,6 +3128,38 @@ async fn requested_track_keeps_its_name() { subscribe.abort(); } +/// An answered request refuses a second answer as such, and one whose answer failed after taking +/// it reports that failure rather than looking answered. +#[tokio::test] +async fn a_track_request_reports_its_answer() { + let broadcast = MoqBroadcastProducer::new().unwrap(); + let dynamic = broadcast.requests().unwrap(); + let consumer = broadcast.consume().unwrap(); + let subscribe = |name: &'static str| { + let consumer = consumer.clone(); + tokio::spawn(async move { consumer.subscribe_track(name.into(), None).await }) + }; + let _first = subscribe("first"); + let first = kio::clock::timeout(TIMEOUT, dynamic.requested_track()).await.unwrap().unwrap(); + first.accept(None).unwrap(); + let rejected = first.reject(0); + + let _second = subscribe("second"); + let second = kio::clock::timeout(TIMEOUT, dynamic.requested_track()).await.unwrap().unwrap(); + let failed = MoqMediaTrackProducer::audio( + &broadcast, + MoqMediaTarget::Requested { request: second.clone() }, + audio_init(MoqAudioFormat::Opus, vec![1, 2, 3]), + ); + let failed = failed.map(|_| ()); + assert!(matches!(failed, Err(MoqError::Codec { .. })), "{failed:?}"); + let later = second.reject(0); + assert!( + matches!(rejected, Err(MoqError::AlreadyResponded)) && matches!(later, Err(MoqError::Codec { .. })), + "{rejected:?} {later:?}" + ); +} + #[tokio::test] async fn dynamic_track_request_can_publish_media() { let broadcast = MoqBroadcastProducer::new().unwrap(); @@ -3164,7 +3196,7 @@ async fn dynamic_track_request_can_publish_media() { ) .unwrap(); assert_eq!(media.demand().unwrap().name(), "requested-audio"); - assert!(matches!(track.name(), Err(MoqError::Closed))); + assert!(matches!(track.name(), Err(MoqError::AlreadyResponded))); let media_consumer = kio::clock::timeout(TIMEOUT, subscribe) .await @@ -5046,7 +5078,7 @@ async fn dynamic_broadcast_request() { // A re-price keeps the instance the answer was announced under. job.set_cost(7).unwrap(); assert_eq!(job.instance_id(), instance); - assert!(matches!(request.path(), Err(MoqError::Closed))); + assert!(matches!(request.path(), Err(MoqError::AlreadyResponded))); let broadcast = kio::clock::timeout(TIMEOUT, requesting) .await @@ -5623,7 +5655,7 @@ async fn dynamic_broadcast_request_can_reject() { assert_eq!(request.path().unwrap(), "missing"); request.reject(404).unwrap(); - assert!(matches!(request.path(), Err(MoqError::Closed))); + assert!(matches!(request.path(), Err(MoqError::AlreadyResponded))); let result = kio::clock::timeout(TIMEOUT, requesting) .await @@ -8028,6 +8060,16 @@ async fn shutdown_cancels_and_drops_cleanly() { // Idempotent: the thread is already gone. crate::moq_ffi_shutdown(); + // A synchronous call fails like the parked asynchronous ones, on a driver shutdown dropped + // and on one created after it, which never ran. + let late = MoqOriginProducer::new(MoqOriginConfig::default()); + let routed = server_origin.route("late".into(), MoqRouteOptions::default()).map(|_| ()); + let unrun = late.route("late".into(), MoqRouteOptions::default()).map(|_| ()); + assert!( + matches!(routed, Err(MoqError::Cancelled)) && matches!(unrun, Err(MoqError::Cancelled)), + "{routed:?} {unrun:?}" + ); + let parked = kio::clock::timeout(TIMEOUT, parked) .await .expect("the parked read never resolved") diff --git a/rs/moq-ffi/src/video.rs b/rs/moq-ffi/src/video.rs index 2a6945dd17a..08ce3ee4fb0 100644 --- a/rs/moq-ffi/src/video.rs +++ b/rs/moq-ffi/src/video.rs @@ -353,15 +353,58 @@ struct VideoProducer { ceiling: kio::Producer, /// The track priority this rendition was published at; a higher rung takes one below. priority: i8, + /// The bandwidth claim on the current track and its follower, when published against + /// an allocator. Beside the encoder, so a move to a new track, a finish, and an abort + /// change all three together. + follow: Option, } -/// Stops the follow thread when the producer is finished or dropped. +/// A rendition's bandwidth claim on its current track, and the thread retuning the encoder +/// to the claim's grant. +struct Following { + /// Reserves against the track a decoder identity change moves the rendition to. Held + /// so the reservation's registry outlives extra bandwidth handles. + allocator: Arc, + /// The next lower rendition's ceiling, which sets this one's band. + lower: Option>, + reservation: Arc, + follower: Follow, +} + +impl Following { + /// Reserve `ceiling` against `demand` and follow the grant, retuning the producer in `inner`. + fn start( + allocator: Arc, + lower: Option>, + demand: &moq_net::track::Demand, + ceiling: moq_net::bandwidth::Rate, + owner: &MoqVideoProducer, + ) -> Self { + let reservation = allocator.reserve_demand(demand, ceiling.as_bps()); + let follower = spawn_follow(Followed { + inner: owner.inner.clone(), + lower: lower.clone(), + consumer: reservation.consume(), + reservation: Arc::downgrade(&reservation), + ceiling, + applied: owner.applied.clone(), + }); + Self { + allocator, + lower, + reservation, + follower, + } + } +} + +/// The thread following a grant, stopped when its [`Following`] is replaced or dropped. /// -/// `close` participates in the follower's `select!`, so a stop wakes a thread -/// parked in `consumer.changed()` instead of leaking it until the allocator dies. +/// `close` participates in the follower's `select!`, so dropping it wakes a thread parked in +/// `consumer.changed()` instead of leaking it until the allocator dies. struct Follow { - close: Option>, - thread: Option>, + close: oneshot::Sender<()>, + thread: std::thread::JoinHandle<()>, failed: Arc, } @@ -373,12 +416,16 @@ impl Follow { Ok(()) } - fn stop(&mut self) -> Result<(), MoqError> { - drop(self.close.take()); - if let Some(thread) = self.thread.take() { - thread.join().map_err(|_| panic_error())?; + /// Stop the thread and wait for it, reporting its panic. + /// + /// The thread takes the producer's lock, so call this without holding it. + fn stop(self) -> Result<(), MoqError> { + drop(self.close); + self.thread.join().map_err(|_| panic_error())?; + if self.failed.load(Ordering::Acquire) { + return Err(panic_error()); } - self.check() + Ok(()) } } @@ -386,13 +433,16 @@ fn panic_error() -> MoqError { crate::session::map_closed_error(moq_tokio::Error::Panic) } -impl Drop for Follow { - fn drop(&mut self) { - let _ = self.stop(); +impl VideoProducer { + /// Whether this rendition still follows `reservation`, rather than having moved off the + /// track it was reserved on or ended. The [`Weak`](std::sync::Weak) holds the allocation, so + /// a newer reservation never takes its address. + fn follows(&self, reservation: &std::sync::Weak) -> bool { + self.follow + .as_ref() + .is_some_and(|follow| std::ptr::eq(Arc::as_ptr(&follow.reservation), reservation.as_ptr())) } -} -impl VideoProducer { fn write(&mut self, frame: MoqVideoFrame) -> Result<(), MoqError> { // A buffer that isn't one picture at the configured size is rejected here, // by the surface constructors, rather than reinterpreted. @@ -413,6 +463,24 @@ impl VideoProducer { Ok(()) } + /// Retune the live encoder and cap later grants at `bitrate`. + fn set_ceiling(&mut self, bitrate: moq_net::bandwidth::Rate) -> Result<(), MoqError> { + // A disabled rung has no encoder to retune; it reopens at the new ceiling. + if let Some(encoder) = self.encoder.as_mut() { + block_on(encoder.set_bitrate(bitrate))?; + } + // Under the same lock as the retune, so the follower sees both or neither. + let Ok(mut ceiling) = self.ceiling.write() else { + unreachable!("only dropping the producer closes its ceiling") + }; + *ceiling = bitrate; + drop(ceiling); + if let Some(follow) = &self.follow { + follow.reservation.update(bitrate.as_bps()); + } + Ok(()) + } + /// Apply a rate-control target, leaving the rendition as it was on an error. /// /// A disable drains the encoder into the track and closes it; a resume reopens one @@ -424,6 +492,10 @@ impl VideoProducer { let bitrate = match target { moq_mux::rate::Target::Enabled(bitrate) => bitrate, moq_mux::rate::Target::Disabled => { + // The catalog first, the only step that leaves the rendition as it was when it + // fails. After it the encoder goes whatever the drain reports, so the catalog never + // advertises a rendition whose frames are dropped. + self.producer.set_enabled(false)?; if let Some(encoder) = self.encoder.take() { // The encoder is gone either way, so a lost tail only costs those frames. let drained = block_on(encoder.finish()).and_then(|encoded| self.producer.publish(&encoded)); @@ -432,7 +504,7 @@ impl VideoProducer { } self.producer.discontinuity()?; } - return self.producer.set_enabled(false); + return Ok(()); } }; if let Some(encoder) = self.encoder.as_mut() { @@ -457,14 +529,19 @@ impl VideoProducer { retuned } - fn finish(self, children: &crate::producer::Children) -> Result<(), MoqError> { + /// Drain the encoder into the track and end it, returning the follower to stop once the + /// producer's lock is released. + fn finish(self, children: &crate::producer::Children) -> (Result<(), MoqError>, Option) { let Self { encoder, mut producer, ceiling, + follow, .. } = self; - // Ended before the drain, so a rung above stops counting on this rendition now. + // The claim goes before the drain, so siblings can take the room while the last frames go + // out, and the rendition ends before it, so a rung above stops counting on it now. + let follower = follow.map(|follow| follow.follower); drop(ceiling); // Drain the codec into the track before ending it, so the last frames land // in it rather than being dropped with the encoder. @@ -472,7 +549,7 @@ impl VideoProducer { Some(encoder) => block_on(encoder.finish()).and_then(|encoded| producer.publish(&encoded)), None => Ok(()), }; - finalize(producer, drained, children) + (finalize(producer, drained, children), follower) } } @@ -486,45 +563,31 @@ impl VideoProducer { /// `Unroutable`, which a moq-transport reader sees as `Internal`: its wire has no such code. #[derive(uniffi::Object)] pub struct MoqVideoProducer { + /// `None` once finished or aborted. inner: Arc>>, - reservation: std::sync::Mutex>>, - follow: std::sync::Mutex>, /// Last bitrate applied by the follow loop or [`set_bitrate`](Self::set_bitrate). applied: Arc, - /// The next lower rendition's ceiling, kept to follow a new track with. - lower: Option>, - /// Held so the reservation's registry outlives extra bandwidth handles, and to reserve - /// against the track a decoder identity change moves the rendition to. - bandwidth: Option>, children: crate::producer::Children, } impl MoqVideoProducer { - /// Move the bandwidth claim and its follower onto `demand`, the track a decoder identity change - /// moved the rendition to: the old track's demand ended with it. - /// - /// The follower takes the producer's lock, so call this without holding it. - fn follow_track(&self, demand: &moq_net::track::Demand, ceiling: moq_net::bandwidth::Rate) -> Result<(), MoqError> { - let Some(bandwidth) = &self.bandwidth else { - return Ok(()); - }; - let stopped = self - .follow - .lock() - .unwrap() - .take() - .map_or(Ok(()), |mut follow| follow.stop()); - let reservation = bandwidth.reserve_demand(demand, ceiling.as_bps()); - let follow = spawn_follow(Followed { - inner: self.inner.clone(), - lower: self.lower.clone(), - consumer: reservation.consume(), - ceiling, - applied: self.applied.clone(), - }); - *self.reservation.lock().unwrap() = Some(reservation); - *self.follow.lock().unwrap() = Some(follow); - stopped + /// Run `f` on the open rendition, failing with its follower's panic or `Closed`. + fn with(&self, f: impl FnOnce(&mut VideoProducer) -> Result) -> Result { + let mut guard = self.inner.lock().map_err(|_| panic_error())?; + let producer = guard.as_mut().ok_or(MoqError::Closed)?; + if let Some(follow) = &producer.follow { + follow.follower.check()?; + } + f(producer) + } + + fn track_demand(&self) -> Result { + self.with(|producer| Ok(producer.producer.demand())) + } + + #[cfg(test)] + pub(crate) fn applied_bitrate(&self) -> u64 { + self.applied.load(Ordering::SeqCst) } } @@ -532,19 +595,18 @@ impl MoqVideoProducer { impl crate::producer::Child for MoqVideoProducer { fn abort(&self, err: moq_net::Error) { let _guard = crate::ffi::runtime().enter(); - let Some(producer) = self + let producer = self .inner .lock() .unwrap_or_else(std::sync::PoisonError::into_inner) - .take() - else { + .take(); + let Some(VideoProducer { producer, follow, .. }) = producer else { return; }; - if let Some(mut follow) = self.follow.lock().unwrap().take() { - let _ = follow.stop(); + if let Some(follow) = follow { + let _ = follow.follower.stop(); } - self.reservation.lock().unwrap().take(); - producer.producer.abort(err); + producer.abort(err); } } @@ -554,27 +616,6 @@ impl Drop for MoqVideoProducer { } } -impl MoqVideoProducer { - fn check(&self) -> Result<(), MoqError> { - if let Some(follow) = self.follow.lock().unwrap().as_ref() { - follow.check()?; - } - Ok(()) - } - - fn track_demand(&self) -> Result { - self.check()?; - let guard = self.inner.lock().map_err(|_| panic_error())?; - let producer = guard.as_ref().ok_or(MoqError::Closed)?; - Ok(producer.producer.demand()) - } - - #[cfg(test)] - pub(crate) fn applied_bitrate(&self) -> u64 { - self.applied.load(Ordering::SeqCst) - } -} - #[uniffi::export] impl MoqVideoProducer { /// A watch-only handle to whether this video track has subscribers. @@ -589,22 +630,25 @@ impl MoqVideoProducer { /// [`lower`](MoqVideoProducerOptions::lower) rung is disabled for lack of /// bandwidth, the frame is dropped without encoding. pub fn write(&self, frame: MoqVideoFrame) -> Result<(), MoqError> { - self.check()?; let _guard = crate::ffi::runtime().enter(); - let moved = { - let mut guard = self.inner.lock().map_err(|_| panic_error())?; - let producer = guard.as_mut().ok_or(MoqError::Closed)?; + let moved = self.with(|producer| { let before = producer.producer.demand(); producer.write(frame)?; // A frame whose decoder identity changed, such as a resume at a ceiling past the - // declared level, moved the rendition to a newly named track. + // declared level, moved the rendition to a newly named track, and the claim moves + // with it under the same lock. let after = producer.producer.demand(); - (after.name() != before.name()).then(|| (after, *producer.ceiling.read())) - }; - match moved { - Some((demand, ceiling)) => self.follow_track(&demand, ceiling), - None => Ok(()), - } + if after.name() == before.name() { + return Ok(None); + } + let ceiling = *producer.ceiling.read(); + Ok(producer.follow.as_mut().map(|follow| { + let moved = Following::start(follow.allocator.clone(), follow.lower.clone(), &after, ceiling, self); + std::mem::replace(follow, moved).follower + })) + })?; + // Outside the lock, which the old follower takes on its way out. + moved.map_or(Ok(()), Follow::stop) } /// Cut a new group at the next written frame. @@ -624,15 +668,14 @@ impl MoqVideoProducer { /// without the control): nothing is queued, and groups keep falling at the /// configured interval. pub fn cut(&self) -> Result<(), MoqError> { - self.check()?; - let mut guard = self.inner.lock().map_err(|_| panic_error())?; - let producer = guard.as_mut().ok_or(MoqError::Closed)?; - // A keyframe is what a cut is on the wire: the importer closes the open - // group and starts a new one at it. A disabled rung resumes on a keyframe anyway. - if let Some(encoder) = producer.encoder.as_mut() { - block_on(encoder.cut())?; - } - Ok(()) + self.with(|producer| { + // A keyframe is what a cut is on the wire: the importer closes the open + // group and starts a new one at it. A disabled rung resumes on a keyframe anyway. + if let Some(encoder) = producer.encoder.as_mut() { + block_on(encoder.cut())?; + } + Ok(()) + }) } /// Retune the live encoder to `bitrate` bits per second, taking effect from @@ -652,25 +695,8 @@ impl MoqVideoProducer { /// encoder keeps running at its current rate, so stop adapting rather than /// stop publishing. pub fn set_bitrate(&self, bitrate: u64) -> Result<(), MoqError> { - self.check()?; let _guard = crate::ffi::runtime().enter(); - { - let mut guard = self.inner.lock().map_err(|_| panic_error())?; - let producer = guard.as_mut().ok_or(MoqError::Closed)?; - let bitrate = moq_net::bandwidth::Rate::from_bps(bitrate); - // A disabled rung has no encoder to retune; it reopens at the new ceiling. - if let Some(encoder) = producer.encoder.as_mut() { - block_on(encoder.set_bitrate(bitrate))?; - } - // Under the same lock as the retune, so the follower sees both or neither. - let Ok(mut ceiling) = producer.ceiling.write() else { - unreachable!("only dropping the producer closes its ceiling") - }; - *ceiling = bitrate; - } - if let Some(reservation) = self.reservation.lock().unwrap().as_ref() { - reservation.update(bitrate); - } + self.with(|producer| producer.set_ceiling(moq_net::bandwidth::Rate::from_bps(bitrate)))?; self.applied.store(bitrate, Ordering::SeqCst); Ok(()) } @@ -681,7 +707,8 @@ impl MoqVideoProducer { /// decoder identity publishes under a new track name and reserves against that /// track instead, so read this again after [`write`](Self::write). pub fn reservation(&self) -> Option> { - self.reservation.lock().unwrap().clone() + let guard = self.inner.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + Some(guard.as_ref()?.follow.as_ref()?.reservation.clone()) } /// Flush any frames the codec is still holding and finalize the track. @@ -689,23 +716,16 @@ impl MoqVideoProducer { /// The broadcast keeps the finished track until it ends, so its finish still holds it. pub fn finish(&self) -> Result<(), MoqError> { let _guard = crate::ffi::runtime().enter(); - // Stop following and release the share before draining, so siblings can - // take the room while the last frames go out. The follower takes the - // producer's lock, so it stops before the drain holds it. - let followed = self - .follow - .lock() - .unwrap() - .take() - .map_or(Ok(()), |mut follow| follow.stop()); - self.reservation.lock().unwrap().take(); // Held through the drain, so a racing broadcast close waits it out instead of // missing a track that still publishes. let mut inner = self.inner.lock().map_err(|_| panic_error())?; let producer = inner.take().ok_or(MoqError::Closed)?; + let (finished, follower) = producer.finish(&self.children); + drop(inner); // A follower panic only stops rate adaptation, so the encoded frames still // end the track cleanly before the panic is reported. - producer.finish(&self.children)?; + let followed = follower.map_or(Ok(()), Follow::stop); + finished?; followed } } @@ -807,46 +827,29 @@ impl MoqVideoProducer { )?) })?; - let inner = Arc::new(std::sync::Mutex::new(Some(VideoProducer { - encoder: Some(encoder), - producer, - format: input.format, - size: config.size(), - config, - ceiling: kio::Producer::new(ceiling), - priority, - }))); - let applied = Arc::new(AtomicU64::new(ceiling.as_bps())); - let reserved = bandwidth.as_ref().map(|bandwidth| { - let demand = inner - .lock() - .unwrap() - .as_ref() - .expect("just constructed") - .producer - .demand(); - bandwidth.reserve_demand(&demand, ceiling.as_bps()) - }); - let lower = lower.map(|(lower, _, _)| lower); - let follow = reserved.as_ref().map(|reservation| { - spawn_follow(Followed { - inner: inner.clone(), - lower: lower.clone(), - consumer: reservation.consume(), - ceiling, - applied: applied.clone(), - }) - }); - - children.adopt(Arc::new(MoqVideoProducer { - inner, - reservation: std::sync::Mutex::new(reserved), - follow: std::sync::Mutex::new(follow), - applied, - lower, - bandwidth, + let video = MoqVideoProducer { + inner: Arc::new(std::sync::Mutex::new(Some(VideoProducer { + encoder: Some(encoder), + producer, + format: input.format, + size: config.size(), + config, + ceiling: kio::Producer::new(ceiling), + priority, + follow: None, + }))), + applied: Arc::new(AtomicU64::new(ceiling.as_bps())), children: children.clone(), - })) + }; + if let Some(allocator) = bandwidth { + // Its follower takes this lock, so it waits until the claim is in place. + let mut guard = video.inner.lock().map_err(|_| panic_error())?; + let producer = guard.as_mut().expect("just constructed"); + let lower = lower.map(|(lower, _, _)| lower); + let demand = producer.producer.demand(); + producer.follow = Some(Following::start(allocator, lower, &demand, ceiling, &video)); + } + children.adopt(Arc::new(video)) } } @@ -874,6 +877,8 @@ struct Followed { /// never keeps the rendition below it publishing. lower: Option>, consumer: moq_net::bandwidth::Consumer, + /// The claim this thread follows, which retires it once the rendition moves off it or ends. + reservation: std::sync::Weak, ceiling: moq_net::bandwidth::Rate, applied: Arc, } @@ -905,11 +910,7 @@ fn spawn_follow(followed: Followed) -> Follow { } }) .expect("failed to spawn rate-control thread"); - Follow { - close: Some(close), - thread: Some(thread), - failed, - } + Follow { close, thread, failed } } /// The policy a rendition follows: its manual `ceiling`, above the `lower` rendition's @@ -943,6 +944,7 @@ async fn follow_reservation(followed: Followed) { inner, lower, consumer, + reservation, ceiling, applied, } = followed; @@ -957,7 +959,7 @@ async fn follow_reservation(followed: Followed) { let next = kio::wait(|waiter| { let below = lower_ceiling(&lower, waiter); let guard = inner.lock().unwrap(); - let Some(producer) = guard.as_ref() else { + let Some(producer) = guard.as_ref().filter(|producer| producer.follows(&reservation)) else { return std::task::Poll::Ready(None); }; follower.set_policy(video_policy(*producer.ceiling.read(), below)); @@ -969,7 +971,9 @@ async fn follow_reservation(followed: Followed) { }; let below = lower_ceiling(&lower, &noop); let mut guard = inner.lock().unwrap(); - let Some(producer) = guard.as_mut() else { + // Checked again under the lock it applies under: a move or finish may have replaced this + // claim while the target was on its way. + let Some(producer) = guard.as_mut().filter(|producer| producer.follows(&reservation)) else { return; }; let policy = video_policy(*producer.ceiling.read(), below); @@ -1518,12 +1522,8 @@ mod finish_tests { .await .unwrap(); // Stand in for a follower thread that panicked. - video - .follow - .lock() - .unwrap() - .as_ref() - .expect("published against an allocator") + video.inner.lock().unwrap().as_ref().unwrap().follow.as_ref().expect("published against an allocator") + .follower .failed .store(true, Ordering::Release); @@ -1533,6 +1533,125 @@ mod finish_tests { let result = track.recv_group().await.map(|group| group.is_some()); assert!(matches!(result, Ok(false)), "the track should end cleanly: {result:?}"); } + + /// A frame that moves the rendition to a new track moves its claim there in the same step, + /// and a finish releases that claim: no claim outlives the track it watches. + #[tokio::test] + async fn a_moved_claim_follows_its_track_and_ends_with_it() { + let estimate = moq_net::bandwidth::Producer::new(); + let bandwidth = Arc::new(MoqAllocator::new(moq_net::bandwidth::Allocator::new( + estimate.consume(), + ))); + let broadcast = crate::producer::MoqBroadcastProducer::new().unwrap(); + let input = MoqVideoEncoderInput { + format: MoqVideoPixelFormat::Rgba, + width: 320, + height: 240, + framerate: crate::video::MoqVideoFrameRate { + numerator: 30, + denominator: 1, + }, + }; + let output = MoqVideoEncoderOutput { + codec: MoqVideoCodec::H264, + bitrate: Some(1_000_000), + join: None, + kind: MoqVideoEncoderKind::Software, + }; + let video = crate::video::MoqVideoProducer::new( + &crate::media::MoqMediaCatalogProducer::new(broadcast.clone()).unwrap(), + crate::video::MoqVideoProducerOptions { + input, + output, + track: Some("camera".into()), + bandwidth: Some(bandwidth), + lower: None, + }, + ) + .unwrap(); + let mut larger = moq_video::encode::Config::new(640, 480, moq_video::FrameRate::new(30, 1).unwrap()); + larger.kind = moq_video::encode::Kind::Software; + + tokio::task::spawn_blocking(move || { + let picture = |width: usize, height: usize, micros: u64| MoqVideoFrame { + timestamp: MoqTimestamp { + value: micros, + scale: 1_000_000, + }, + data: vec![0x80; width * height * 4], + }; + video.write(picture(320, 240, 0)).unwrap(); + let before = video.reservation().expect("published against an allocator"); + // Disabled, then resumed onto a larger picture, a higher level and so a new codec string. + video + .with(|producer| { + producer.apply(moq_mux::rate::Target::Disabled)?; + producer.config = larger.clone(); + producer.size = larger.size(); + producer.apply(moq_mux::rate::Target::Enabled(moq_net::bandwidth::Rate::from_mbps(1)))?; + Ok(()) + }) + .unwrap(); + video.write(picture(640, 480, 66_666)).unwrap(); + + assert_eq!(video.track_demand().unwrap().name(), "camera/1"); + let after = video.reservation().expect("the claim moved with the track"); + assert!(!Arc::ptr_eq(&before, &after), "the claim still watches the ended track"); + video.finish().unwrap(); + assert!(video.reservation().is_none(), "a finished track still holds a claim"); + }) + .await + .unwrap(); + } + + /// A disable that fails partway leaves the catalog and the encoder agreeing, so viewers + /// never wait on a rendition the catalog calls enabled while its frames are dropped. + #[tokio::test] + async fn a_failed_disable_leaves_the_catalog_and_encoder_agreeing() { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let config = moq_mux::catalog::Config::default() + .with_catalog(moq_mux::catalog::hang::Catalog::::default()); + let catalog = moq_mux::catalog::Producer::new(&mut broadcast, config).unwrap(); + let mut encode = moq_video::encode::Config::new(320, 240, moq_video::FrameRate::new(30, 1).unwrap()); + encode.kind = moq_video::encode::Kind::Software; + let rendition = encode.probe().await.unwrap(); + let track = broadcast + .create_track("camera", catalog.track_info(hang::catalog::PRIORITY.video)) + .unwrap(); + let producer = + moq_video::encode::Producer::with_track(track.clone(), catalog.reserve(), rendition).unwrap(); + let encoder = moq_video::encode::Sink::open(&encode).await.unwrap(); + let mut video = VideoProducer { + encoder: Some(encoder), + producer, + config: encode.clone(), + format: MoqVideoPixelFormat::I420, + size: encode.size(), + ceiling: kio::Producer::new(moq_net::bandwidth::Rate::from_mbps(1)), + priority: hang::catalog::PRIORITY.video, + follow: None, + }; + let (video, disabled) = tokio::task::spawn_blocking(move || { + video + .write(MoqVideoFrame { + timestamp: MoqTimestamp { + value: 0, + scale: 1_000_000, + }, + data: vec![0x80; 320 * 240 * 3 / 2], + }) + .unwrap(); + // The track breaks, so the break marker the disable writes fails. + track.abort(moq_net::Error::Cancel).unwrap(); + let disabled = video.apply(moq_mux::rate::Target::Disabled); + (video, disabled) + }) + .await + .unwrap(); + assert!(disabled.is_err(), "the break marker should fail on a broken track"); + let enabled = catalog.snapshot().video.renditions["camera"].enabled; + assert_eq!(video.encoder.is_some(), enabled, "the catalog and the encoder disagree"); + } } #[cfg(test)] From b13a80820459fb807cb6061c59907b5ed6c23222 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 8 Nov 2026 00:04:20 -0800 Subject: [PATCH 03/11] fix(moq-c): video follow and Background hold one state each The encoder's track, reservation, and follower live in one Following value under the encoder's lock, so a finish can't leave a claim behind and a moved track re-follows from the frame that moved it. A disable sets the catalog before draining the encoder. A Background dropped without a result reads as Cancel everywhere instead of Panic. Co-Authored-By: Claude Opus 5.5 --- rs/moq-c/src/ffi.rs | 71 ++++++++-- rs/moq-c/src/video.rs | 306 ++++++++++++++++++++++++++---------------- rs/moq-ffi/src/lib.rs | 5 +- 3 files changed, 254 insertions(+), 128 deletions(-) diff --git a/rs/moq-c/src/ffi.rs b/rs/moq-c/src/ffi.rs index d2576ce6945..38a202186e4 100644 --- a/rs/moq-c/src/ffi.rs +++ b/rs/moq-c/src/ffi.rs @@ -124,26 +124,34 @@ impl Background { Self { result: Some(receiver) } } + /// Read the operation's completion, arming `waiter` while it runs. Every reader goes through + /// here, so a synchronous check and an awaited one agree on each state. + fn completion(&self, waiter: &kio::Waiter) -> Completion { + let Some(result) = &self.result else { + return Completion::Detached; + }; + let completed = result.poll(waiter, |state| match &**state { + Some(result) => std::task::Poll::Ready(result.clone()), + None => std::task::Poll::Pending, + }); + match completed { + std::task::Poll::Pending => Completion::Running, + std::task::Poll::Ready(Ok(result)) => Completion::Done(result), + std::task::Poll::Ready(Err(_)) => Completion::Dropped, + } + } + /// Return a completed background failure, if any. pub fn check(&self) -> Result<(), Error> { - self.result - .as_ref() - .map(|result| result.read().clone().unwrap_or(Ok(()))) - .unwrap_or(Ok(())) + self.completion(&kio::Waiter::noop()).result().unwrap_or(Ok(())) } /// The background operation's result, once it finishes. #[kio::wrapper] pub fn poll_finished(&self, waiter: &kio::Waiter) -> std::task::Poll> { - let Some(result) = &self.result else { - return std::task::Poll::Ready(Ok(())); - }; - match std::task::ready!(result.poll(waiter, |state| match &**state { - Some(result) => std::task::Poll::Ready(result.clone()), + match self.completion(waiter).result() { + Some(result) => std::task::Poll::Ready(result), None => std::task::Poll::Pending, - })) { - Ok(result) => std::task::Poll::Ready(result), - Err(_) => std::task::Poll::Ready(Err(Error::Panic)), } } @@ -164,6 +172,28 @@ impl Background { } } +/// Where an owned operation stands, as its handle's calls report it. +enum Completion { + /// The handle owns no operation, so nothing can fail it. + Detached, + Running, + Done(Result<(), Error>), + /// Released without a result, which only the runtime's teardown does. + Dropped, +} + +impl Completion { + /// What the handle's calls report, or `None` while the operation runs. + fn result(self) -> Option> { + match self { + Self::Detached => Some(Ok(())), + Self::Running => None, + Self::Done(result) => Some(result), + Self::Dropped => Some(Err(moq_net::Error::Cancel.into())), + } + } +} + #[cfg(test)] pub(crate) fn panicking_origin() -> (moq_net::origin::Producer, Background, kio::Producer) { struct Driver { @@ -617,6 +647,23 @@ mod tests { )); } + /// A background dropped before it reported reads as cancelled to a check and an await alike. + #[tokio::test] + async fn a_dropped_background_reads_cancelled_everywhere() { + let result = kio::Producer::new(None); + let background = Background { + result: Some(result.consume()), + }; + drop(result); + let checked = background.check(); + let finished = background.finished().await; + assert!( + matches!(checked, Err(Error::Moq(moq_net::Error::Cancel))) + && matches!(finished, Err(Error::Moq(moq_net::Error::Cancel))), + "{checked:?} {finished:?}" + ); + } + struct PanickingDriver { dropped: Option>, observed: std::sync::mpsc::Receiver<()>, diff --git a/rs/moq-c/src/video.rs b/rs/moq-c/src/video.rs index 6b44b18a3d1..fd532ed4f69 100644 --- a/rs/moq-c/src/video.rs +++ b/rs/moq-c/src/video.rs @@ -311,6 +311,7 @@ async fn follow_reservation( inner: Shared, lower: Option>, consumer: moq_net::bandwidth::Consumer, + reservation: std::sync::Weak, ceiling: moq_net::bandwidth::Rate, ) { let noop = kio::Waiter::noop(); @@ -324,7 +325,7 @@ async fn follow_reservation( let next = kio::wait(|waiter| { let below = lower_ceiling(&lower, waiter); let guard = inner.lock(); - let Some(producer) = guard.as_ref() else { + let Some(producer) = guard.as_ref().filter(|producer| producer.follows(&reservation)) else { return std::task::Poll::Ready(None); }; follower.set_policy(video_policy(*producer.ceiling.read(), below)); @@ -336,7 +337,9 @@ async fn follow_reservation( }; let below = lower_ceiling(&lower, &noop); let mut guard = inner.lock(); - let Some(producer) = guard.as_mut() else { + // Checked again under the lock it applies under: a move or finish may have replaced this + // claim while the target was on its way. + let Some(producer) = guard.as_mut().filter(|producer| producer.follows(&reservation)) else { return; }; let policy = video_policy(*producer.ceiling.read(), below); @@ -407,12 +410,10 @@ pub(crate) struct VideoEncoder { /// The encoded resolution, from the publish config. Frames carry only pixels, /// so this is what says how to read them. size: moq_video::Size, - /// The allocator [`follow`](VideoPublish::follow) reserved against, kept to reserve again - /// when a decoder identity change moves the rendition to a new track. - allocator: Option, - reservation: Option>, - follow: Option>, - followed: Option, + /// The bandwidth claim on the current track and its follower, when published against an + /// allocator. Beside the encoder, so a move to a new track, a finish, and an abort change + /// all three together. + follow: Option, /// The manual ceiling [`moq_encode_video_bitrate`] last applied, which caps every /// grant. Behind the encoder's lock, so the follower never reads an old, higher /// one and retunes over a newer cap. A channel, so a rung above this rendition @@ -421,9 +422,20 @@ pub(crate) struct VideoEncoder { children: crate::publish::Children, } +/// A rendition's bandwidth claim on its current track, and the task retuning the encoder to +/// the claim's grant. +struct Following { + /// Reserves against the track a decoder identity change moves the rendition to. + allocator: moq_net::bandwidth::Allocator, + reservation: Arc, + /// Dropped with this claim, which stops the follower. + _stop: oneshot::Sender<()>, + follower: crate::ffi::Background, +} + impl crate::publish::Abort for Shared { fn abort(self, err: moq_net::Error) { - // Dropping the follow sender stops the rate follower; the encoder closes with it. + // Dropping the claim stops the rate follower; the encoder closes with it. if let Some(encoder) = self.take() { encoder.producer.abort(err); } @@ -538,44 +550,60 @@ fn finalize( } } -/// Reserve the encoder's ceiling against its current track and follow the grant, replacing any -/// earlier claim and its follower: a decoder identity change moves the rendition to a new track, -/// and the claim has to watch that track's demand. -fn follow_grant(shared: &Shared) -> Result<(), Error> { - let (reservation, ceiling, lower) = { - let mut guard = shared.lock(); - let encoder = guard.as_mut().ok_or(Error::MediaNotFound)?; - let Some(allocator) = &encoder.allocator else { - return Ok(()); - }; - let ceiling = *encoder.ceiling.read(); - let reservation = Arc::new(allocator.reserve(&encoder.producer.demand(), ceiling)); - encoder.reservation = Some(reservation.clone()); - // Dropping the sender stops the previous follower. - encoder.follow = None; - (reservation, ceiling, encoder.lower.clone()) - }; - let (close, closed) = oneshot::channel(); - shared.lock().as_mut().ok_or(Error::MediaNotFound)?.follow = Some(close); - let follower = shared.clone(); - let background = crate::ffi::Background::new(async move { - tokio::select! { - biased; - _ = closed => {} - _ = follow_reservation(follower, lower, reservation.consume(), ceiling) => {} - } - Ok(()) - }); - shared.lock().as_mut().ok_or(Error::MediaNotFound)?.followed = Some(background); - Ok(()) -} - impl VideoEncoder { - /// Encode and publish one picture, returning whether it moved the rendition to a new track. - fn publish_frame(&mut self, timestamp: moq_net::Timestamp, data: &[u8]) -> Result { - if let Some(followed) = &self.followed { - followed.check()?; - } + /// Reserve the ceiling against the current track and follow the grant, replacing any earlier + /// claim, whose dropped stop ends its follower. `shared` is the handle holding this encoder, + /// whose lock the caller holds, so the claim and its follower change together. + fn follow_grant(&mut self, shared: &Shared, allocator: moq_net::bandwidth::Allocator) { + let ceiling = *self.ceiling.read(); + let reservation = Arc::new(allocator.reserve(&self.producer.demand(), ceiling)); + let (stop, stopped) = oneshot::channel(); + let follower = crate::ffi::Background::new({ + let shared = shared.clone(); + let lower = self.lower.clone(); + let consumer = reservation.consume(); + let identity = Arc::downgrade(&reservation); + async move { + tokio::select! { + biased; + _ = stopped => {} + _ = follow_reservation(shared, lower, consumer, identity, ceiling) => {} + } + Ok(()) + } + }); + self.follow = Some(Following { + allocator, + reservation, + _stop: stop, + follower, + }); + } + + /// Whether this rendition still follows `reservation`, rather than having moved off the + /// track it was reserved on or ended. The [`Weak`](std::sync::Weak) holds the allocation, so + /// a newer reservation never takes its address. + fn follows(&self, reservation: &std::sync::Weak) -> bool { + self.follow + .as_ref() + .is_some_and(|follow| std::ptr::eq(Arc::as_ptr(&follow.reservation), reservation.as_ptr())) + } + + /// A rate follower's panic, which ends adaptation but not the track. + fn check(&self) -> Result<(), Error> { + self.follow.as_ref().map_or(Ok(()), |follow| follow.follower.check()) + } + + /// Encode and publish one picture. A picture that moves the rendition to a new track moves + /// its claim there too; `shared` is the handle holding this encoder, whose lock the caller + /// holds. + fn publish_frame( + &mut self, + shared: &Shared, + timestamp: moq_net::Timestamp, + data: &[u8], + ) -> Result<(), Error> { + self.check()?; // A buffer that isn't one picture at the configured size is rejected here, // by the surface constructors, rather than reinterpreted. let size = self.size; @@ -588,7 +616,7 @@ impl VideoEncoder { // Disabled: nothing is encoded until the grant resumes the rendition. let Some(encoder) = self.encoder.as_mut() else { - return Ok(false); + return Ok(()); }; let frame = moq_video::Frame::new(surface, timestamp); // A backend that pipelines hands back an earlier frame's output, so this is @@ -597,14 +625,17 @@ impl VideoEncoder { let before = self.producer.demand(); self.producer.publish(&encoded)?; // A frame whose decoder identity changed, such as a resume at a ceiling past the declared - // level, moved the rendition to a newly named track. - Ok(self.producer.demand().name() != before.name()) + // level, moved the rendition to a newly named track, whose demand the claim has to watch. + if self.producer.demand().name() != before.name() + && let Some(follow) = &self.follow + { + self.follow_grant(shared, follow.allocator.clone()); + } + Ok(()) } fn publish_cut(&mut self) -> Result<(), Error> { - if let Some(followed) = &self.followed { - followed.check()?; - } + self.check()?; // A keyframe is what a cut is on the wire: the importer closes the open // group and starts a new one at it. A disabled rung resumes on a keyframe anyway. match self.encoder.as_mut() { @@ -614,9 +645,7 @@ impl VideoEncoder { } fn publish_bitrate(&mut self, bitrate: u64) -> Result<(), Error> { - if let Some(followed) = &self.followed { - followed.check()?; - } + self.check()?; let bitrate = moq_net::bandwidth::Rate::from_bps(bitrate); // A disabled rung has no encoder to retune; it reopens at the new ceiling. if let Some(encoder) = self.encoder.as_mut() { @@ -627,8 +656,8 @@ impl VideoEncoder { }; *ceiling = bitrate; drop(ceiling); - if let Some(reservation) = &self.reservation { - reservation.update(bitrate); + if let Some(follow) = &self.follow { + follow.reservation.update(bitrate); } Ok(()) } @@ -644,6 +673,10 @@ impl VideoEncoder { let bitrate = match target { moq_mux::rate::Target::Enabled(bitrate) => bitrate, moq_mux::rate::Target::Disabled => { + // The catalog first, the only step that leaves the rendition as it was when it + // fails. After it the encoder goes whatever the drain reports, so the catalog never + // advertises a rendition whose frames are dropped. + self.producer.set_enabled(false)?; if let Some(encoder) = self.encoder.take() { // The encoder is gone either way, so a lost tail only costs those frames. let drained = block_on(encoder.finish()).and_then(|encoded| self.producer.publish(&encoded)); @@ -652,7 +685,7 @@ impl VideoEncoder { } self.producer.discontinuity()?; } - return self.producer.set_enabled(false); + return Ok(()); } }; if let Some(encoder) = self.encoder.as_mut() { @@ -682,12 +715,14 @@ impl VideoEncoder { let VideoEncoder { encoder, mut producer, - followed, + follow, children, ceiling, .. } = self; - // Ended before the drain, so a rung above stops counting on this rendition now. + // The claim goes before the drain, so siblings can take the room while the last frames go + // out, and the rendition ends before it, so a rung above stops counting on it now. + let follower = follow.map(|follow| follow.follower); drop(ceiling); // Drain the codec into the track before ending it, so the last frames land // in it rather than being dropped with the encoder. @@ -698,7 +733,7 @@ impl VideoEncoder { finalize(producer, drained, &children, crate::publish::Child::Video(id))?; // A follower panic only stops rate adaptation, so the track still ends // cleanly before the panic is reported. - followed.map_or(Ok(()), |followed| followed.check()) + follower.map_or(Ok(()), |follower| follower.check()) } } @@ -760,10 +795,7 @@ impl Video { lower, format, size: config.size(), - allocator: None, - reservation: None, follow: None, - followed: None, ceiling: kio::Producer::new(ceiling), children: target.children.clone(), }); @@ -775,18 +807,19 @@ impl Video { /// Reserve the encoder's ceiling against `allocator` and follow the grant. pub(crate) fn follow(&self, id: Id, allocator: &moq_net::bandwidth::Allocator) -> Result<(), Error> { let shared = self.producer(id)?; - shared.lock().as_mut().ok_or(Error::MediaNotFound)?.allocator = Some(allocator.clone()); - follow_grant(&shared) + let mut guard = shared.lock(); + guard + .as_mut() + .ok_or(Error::MediaNotFound)? + .follow_grant(&shared, allocator.clone()); + Ok(()) } pub(crate) fn reservation(&self, id: Id) -> Result>, Error> { - Ok(self - .producer(id)? - .lock() - .as_ref() - .ok_or(Error::MediaNotFound)? - .reservation - .clone()) + let shared = self.producer(id)?; + let guard = shared.lock(); + let encoder = guard.as_ref().ok_or(Error::MediaNotFound)?; + Ok(encoder.follow.as_ref().map(|follow| follow.reservation.clone())) } /// A watch-only handle to the encoded track's subscriber demand. @@ -1112,16 +1145,11 @@ pub unsafe extern "C" fn moq_encode_video_frame(producer: u32, frame: *const moq let data = unsafe { ffi::parse_slice(frame.data, frame.data_size)? }; let producer = State::lock().video.producer(producer)?; - let moved = producer - .lock() + let mut guard = producer.lock(); + guard .as_mut() .ok_or(Error::MediaNotFound)? - .publish_frame(frame.timestamp.try_into()?, data)?; - // The follower takes the encoder's lock, so it moves with the track after the encode. - match moved { - true => crate::video::follow_grant(&producer), - false => Ok(()), - } + .publish_frame(&producer, frame.timestamp.try_into()?, data) }) } @@ -1421,25 +1449,24 @@ mod tests { lower: None, format: moq_video_pixel_format::MOQ_VIDEO_PIXEL_FORMAT_RGBA, size: probed.size(), - allocator: Some(moq_net::bandwidth::Allocator::new(estimate.consume())), - reservation: None, follow: None, - followed: None, ceiling: kio::Producer::new(moq_net::bandwidth::Rate::from_mbps(1)), children: Default::default(), }); + let allocator = moq_net::bandwidth::Allocator::new(estimate.consume()); let moved = tokio::task::spawn_blocking(move || { - follow_grant(&shared).unwrap(); - let before = shared.lock().as_ref().unwrap().reservation.clone().unwrap(); - let small = vec![0x80; 320 * 240 * 4]; - assert!( - !shared - .lock() - .as_mut() - .unwrap() - .publish_frame(moq_net::Timestamp::ZERO, &small) - .unwrap() - ); + let reservation = |shared: &Shared| { + let guard = shared.lock(); + guard.as_ref().unwrap().follow.as_ref().map(|follow| follow.reservation.clone()) + }; + let publish = |timestamp: moq_net::Timestamp, data: &[u8]| { + let mut guard = shared.lock(); + guard.as_mut().unwrap().publish_frame(&shared, timestamp, data).unwrap(); + }; + shared.lock().as_mut().unwrap().follow_grant(&shared, allocator); + let before = reservation(&shared).unwrap(); + publish(moq_net::Timestamp::ZERO, &vec![0x80; 320 * 240 * 4]); + assert!(Arc::ptr_eq(&before, &reservation(&shared).unwrap())); // Disabled, then resumed onto the larger picture. { @@ -1451,26 +1478,72 @@ mod tests { .apply(moq_mux::rate::Target::Enabled(moq_net::bandwidth::Rate::from_mbps(1))) .unwrap(); } - let large = vec![0x80; 640 * 480 * 4]; - let moved = shared - .lock() - .as_mut() - .unwrap() - .publish_frame(moq_net::Timestamp::from_micros(66_666).unwrap(), &large) - .unwrap(); - assert!(moved, "the larger picture is a new codec string"); - follow_grant(&shared).unwrap(); - let guard = shared.lock(); - let video = guard.as_ref().unwrap(); - let after = video.reservation.clone().unwrap(); + publish( + moq_net::Timestamp::from_micros(66_666).unwrap(), + &vec![0x80; 640 * 480 * 4], + ); + let after = reservation(&shared).expect("the claim moved with the track"); assert!(!Arc::ptr_eq(&before, &after), "the claim still watches the ended track"); - video.producer.demand().name().to_string() + // Finishing takes the claim with the encoder, so nothing watches the ended rendition. + let finished = shared.take().unwrap(); + let name = finished.producer.demand().name().to_string(); + finished.publish_finish(Id::try_from(1u32).unwrap()).unwrap(); + assert_eq!(Arc::strong_count(&after), 1, "only this test still holds the claim"); + name }) .await .unwrap(); assert_eq!(moved, format!("{first}/1")); } + /// A disable that fails partway leaves the catalog and the encoder agreeing, so viewers + /// never wait on a rendition the catalog calls enabled while its frames are dropped. + #[tokio::test] + async fn a_failed_disable_leaves_the_catalog_and_encoder_agreeing() { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let config = moq_mux::catalog::Config::default() + .with_catalog(moq_mux::catalog::hang::Catalog::::default()); + let catalog = moq_mux::catalog::Producer::new(&mut broadcast, config).unwrap(); + let mut encode = moq_video::encode::Config::new(320, 240, moq_video::FrameRate::new(30, 1).unwrap()); + encode.kind = moq_video::encode::Kind::Software; + let rendition = encode.probe().await.unwrap(); + let track = broadcast + .create_track("camera", catalog.track_info(hang::catalog::PRIORITY.video)) + .unwrap(); + let producer = moq_video::encode::Producer::with_track(track.clone(), catalog.reserve(), rendition).unwrap(); + let shared = Shared::new(VideoEncoder { + encoder: Some(moq_video::encode::Sink::open(&encode).await.unwrap()), + producer, + config: encode.clone(), + priority: hang::catalog::PRIORITY.video, + lower: None, + format: moq_video_pixel_format::MOQ_VIDEO_PIXEL_FORMAT_I420, + size: encode.size(), + follow: None, + ceiling: kio::Producer::new(moq_net::bandwidth::Rate::from_mbps(1)), + children: Default::default(), + }); + let (shared, disabled) = tokio::task::spawn_blocking(move || { + let disabled = { + let mut guard = shared.lock(); + let video = guard.as_mut().unwrap(); + video + .publish_frame(&shared, moq_net::Timestamp::ZERO, &vec![0x80; 320 * 240 * 3 / 2]) + .unwrap(); + // The track breaks, so the break marker the disable writes fails. + track.abort(moq_net::Error::Cancel).unwrap(); + video.apply(moq_mux::rate::Target::Disabled) + }; + (shared, disabled) + }) + .await + .unwrap(); + assert!(disabled.is_err(), "the break marker should fail on a broken track"); + let encoding = shared.lock().as_ref().unwrap().encoder.is_some(); + let enabled = catalog.snapshot().video.renditions["camera"].enabled; + assert_eq!(encoding, enabled, "the catalog and the encoder disagree"); + } + /// A clean finish reaches the subscriber as the end of the track, which is what /// makes the abort case below meaningful rather than vacuous. #[tokio::test] @@ -1518,8 +1591,11 @@ mod tests { let mut config = moq_video::encode::Config::new(320, 240, moq_video::FrameRate::new(30, 1).unwrap()); config.kind = moq_video::encode::Kind::Software; let encoder = moq_video::encode::Sink::open(&config).await.unwrap(); - let followed = crate::ffi::Background::new(async { panic!("injected rate follower panic") }); - assert!(matches!(followed.finished().await, Err(Error::Panic))); + let follower = crate::ffi::Background::new(async { panic!("injected rate follower panic") }); + assert!(matches!(follower.finished().await, Err(Error::Panic))); + let estimate = moq_net::bandwidth::Producer::new(); + let allocator = moq_net::bandwidth::Allocator::new(estimate.consume()); + let reservation = Arc::new(allocator.reserve(&producer.demand(), moq_net::bandwidth::Rate::from_mbps(1))); let video = VideoEncoder { encoder: Some(encoder), producer, @@ -1528,10 +1604,12 @@ mod tests { lower: None, format: moq_video_pixel_format::MOQ_VIDEO_PIXEL_FORMAT_I420, size: config.size(), - allocator: None, - reservation: None, - follow: None, - followed: Some(followed), + follow: Some(Following { + allocator, + reservation, + _stop: oneshot::channel().0, + follower, + }), ceiling: kio::Producer::new(moq_net::bandwidth::Rate::from_mbps(1)), children: Default::default(), }; diff --git a/rs/moq-ffi/src/lib.rs b/rs/moq-ffi/src/lib.rs index 3eca58814ef..d49fde86a56 100644 --- a/rs/moq-ffi/src/lib.rs +++ b/rs/moq-ffi/src/lib.rs @@ -35,8 +35,9 @@ uniffi::setup_scaffolding!("moq"); /// Stop the runtime workers before the host runtime goes away. /// -/// Every task is dropped, so a call parked on the runtime resolves `Cancelled` and so does any -/// later one; the handles stay safe to use and drop. This is for process exit, not a reset. +/// Every task is dropped, so a call parked on the runtime resolves `Cancelled`, and so does any +/// later call that needs the runtime or a background task it ran; the handles stay safe to use +/// and drop. This is for process exit, not a reset. /// /// It exists because the runtime workers call back into the host to wake an awaiting future, /// and CPython (before 3.14) terminates a foreign thread that touches the interpreter once From 104ffaada63a88106c2c54d90b987e4678b460f5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 8 Nov 2026 00:05:52 -0800 Subject: [PATCH 04/11] quest: rewrite ffi-state to the remaining sweep, split connection state to m2 The video follow, Background, and request handle fixes are done. What remains is the Task, MoqServer, keep, and moq-c id sweep plus the wrappers. MoqConnectionState breaks every binding, so it becomes its own m2 quest as the item recorded. Co-Authored-By: Claude Opus 5.5 --- quest/m1/explicit-state/README.md | 2 +- quest/m1/explicit-state/ffi-state.md | 80 +++++++++------------------- quest/m2/connection-state-phases.md | 36 +++++++++++++ 3 files changed, 62 insertions(+), 56 deletions(-) create mode 100644 quest/m2/connection-state-phases.md diff --git a/quest/m1/explicit-state/README.md b/quest/m1/explicit-state/README.md index 02506d23ad4..561bbf858b2 100644 --- a/quest/m1/explicit-state/README.md +++ b/quest/m1/explicit-state/README.md @@ -72,7 +72,7 @@ which collapses that state and waits on [Drop set](/quest/m1/drop-set.md). - [moq-boy pause state](/quest/m1/explicit-state/boy-pause.md) - a new viewer always wakes a paused emulator - [Publish capture phases](/quest/m1/explicit-state/publish-capture.md) - dismissing the screen-share picker turns sharing off - [js/qmux close](/quest/m1/explicit-state/qmux-close.md) - a graceful remote close ends qmux streams cleanly -- [FFI handle state](/quest/m1/explicit-state/ffi-state.md) - FFI handles hold one state each: video follow under one lock, requests answered once, drivers Cancelled after shutdown +- [FFI handle state](/quest/m1/explicit-state/ffi-state.md) - moq-ffi's tasks and server, moq-c's ids, and the language wrappers hold one state each - [Track and group end-of-life](/quest/m1/explicit-state/track-end.md) - moq-net's track and group outcomes are one enum each, in Rust and JS - [moq-net phase data](/quest/m1/explicit-state/net-phases.md) - moq-net's session and subscription state machines keep each phase's data in its variant - [moq-mux and moq-gst lifecycles](/quest/m1/explicit-state/mux-gst.md) - export sources, TS and fMP4 export tracks, and gst pads are explicit state diff --git a/quest/m1/explicit-state/ffi-state.md b/quest/m1/explicit-state/ffi-state.md index 2993048db67..64fc1caeaba 100644 --- a/quest/m1/explicit-state/ffi-state.md +++ b/quest/m1/explicit-state/ffi-state.md @@ -1,66 +1,35 @@ -# [L] FFI handle state +# [M] FFI handle state ## Goal -Each moq-ffi and moq-c handle holds its lifecycle in one value under one lock: - -- A video producer's track, its bandwidth reservation, and its follower - change together. A finish or abort can't leave a reservation behind. -- A one-shot request handle (track, broadcast, datagram, socket, and server - requests) is pending, answered, or failed. A failed answer leaves the - request pending, or reports why it failed. -- A background driver that never ran reports `Cancelled`, never `Ok`. - Synchronous calls after `moq_ffi_shutdown` fail the same way the - asynchronous ones do. - -The language wrappers stop mirroring Rust state in their own flags. +The moq-ffi and moq-c handles that still spread one lifecycle over several +fields hold it in one value under one lock, and the language wrappers stop +mirroring Rust state in their own flags. ## Plan The shared decisions are in the [epic](/quest/m1/explicit-state/README.md). -Bugs to reproduce and fix: - -- **Video follow across locks** (`rs/moq-ffi/src/video.rs`): - - `inner`, `reservation`, and `follow` are three mutexes. - - `write()` releases `inner` and only then calls `follow_track`, so a - `finish()` on another thread can run in between. - - `follow_track` then stores a new reservation for a finished track. - - `abort()` returns early once `inner` is `None` and never clears it. - - Recommended: `Mutex }, Closed }>`. - - The same bug exists in `rs/moq-c/src/video.rs` (`follow_grant` spreads - `reservation`, `follow`, and `followed` across three lock acquisitions). - It also exists in the encoder `apply` of both crates: the encoder is taken - before a fallible `discontinuity()`, so a failure leaves the catalog - saying enabled while frames are dropped. -- **`Background` after shutdown** (`rs/moq-ffi/src/ffi.rs`; `rs/moq-c/src/ffi.rs` - maps the same case to `Panic`): - - `Option>>` encodes five states. - - `check()` reads "dropped without a result" as `Ok`, while `finished()` - reads it as `Cancel`. - - Recommended: `enum Completion { Detached, Running, Done(Result), Dropped }`. - -Also sweep: - -- The `Mutex>` handles: `producer.rs`, `origin.rs`, - `datagram.rs`, `socket.rs`, `server.rs`, and `media.rs`'s `Requested` - target. Each is taken before validation or before an await, and they return - inconsistent errors (`Closed` vs `AlreadyResponded`). -- `Task` (`ffi.rs`): a cancel flag, an `Option` state, and the cleanup. -- `MoqServer` (`server.rs`): its lifecycle is spread over four places. +[moq-dev/moq#8264](https://github.com/moq-dev/moq/pull/8264) fixed the +reproduced bugs: the video producer's track, reservation, and follower +under one lock in both crates; the encoder disable that left the catalog +enabled; `Background` reading a dropped driver as `Cancelled` everywhere; +and the one-shot request handles (`OneShot` in `rs/moq-ffi/src/ffi.rs`), +which now answer once and report `AlreadyResponded` or the first answer's +error. What remains has no known bug, so it needs no test beyond what each +redesign changes. + +Sweep in moq-ffi and moq-c: + +- `Task` (`rs/moq-ffi/src/ffi.rs`): a cancel flag, an `Option` state, and + the cleanup. +- `MoqServer` (`rs/moq-ffi/src/server.rs`): its lifecycle is spread over + four places (`task`, `driver`, `listening`, and the task's state). - `MoqTrackProducer.keep: None`, which means both "finished" and "has no family". -- `MoqConnectionState` (`session.rs`) is a flat snapshot (`revision`, - `phase`, `connects`, `failure`, `version`), so `failure` and `version` are - readable in phases where they mean nothing. It carries each phase's data - with that phase, mirroring whatever shape Rust's connection state takes, - and moq-c's `moq_connection_state` follows (renaming its `protocol` to - `version`). It breaks every binding, so under the epic's milestone rule - this item splits into its own m2 quest when this quest starts (added - 2026-11-07, maintainer, /quest-plan from the API shape audit). -- moq-c's global id counter, whose wrap lets a stale route id name a newer - broadcast. Use generation-tagged ids or clear ids on close. +- moq-c's global id counter (`rs/moq-c/src/id.rs`), whose wrap lets a stale + route id name a newer broadcast. Use generation-tagged ids or clear ids on + close. Then sweep the hand-written wrappers: @@ -69,5 +38,6 @@ Then sweep the hand-written wrappers: - the OBS output's mirrored `connected`/`live`; - the Dart stream. -[moq-dev/moq#8060](https://github.com/moq-dev/moq/pull/8060) (moq-c parity) -edits `rs/moq-c/src/video.rs` and `ffi.rs`, so expect merge conflicts. +`MoqConnectionState` split into +[Connection state carries each phase's data](/quest/m2/connection-state-phases.md), +since it breaks every binding. diff --git a/quest/m2/connection-state-phases.md b/quest/m2/connection-state-phases.md new file mode 100644 index 00000000000..6adc627f7f3 --- /dev/null +++ b/quest/m2/connection-state-phases.md @@ -0,0 +1,36 @@ +# [L] Connection state carries each phase's data + +## Goal + +A connection state snapshot carries each phase's data inside that phase, so +a field that means nothing in the current phase can't be read: + +- moq-ffi's `MoqConnectionState` (`rs/moq-ffi/src/session.rs`) is a flat + record (`revision`, `phase`, `connects`, `failure`, `version`), so + `failure` and `version` are readable in phases where they mean nothing. + It becomes a phase enum whose variants carry their own data, mirroring + whatever shape Rust's connection state takes, and every hand-written + wrapper follows. +- moq-c's `moq_connection_state` follows the same shape and renames its + `protocol` to `version`. + +## Plan + +Split from [FFI handle state](/quest/m1/explicit-state/ffi-state.md) when it +started (2026-11-08), as that item recorded: it breaks every binding, so +under the [explicit-state epic](/quest/m1/explicit-state/README.md)'s +milestone rule it is its own m2 quest. Its PR marks its title `!` and says +how to migrate. + +Rust's `moq_tokio::connection::Observation` (`rs/moq-tokio/src/connection.rs`) +is flat too: `failure`, `version`, `transport`, and `last_connected` sit +beside `phase`, while the private `Lifecycle` enum it is built from already +carries them per phase. Open, for whoever starts this, with the maintainer: +reshape `Observation` the same way so the bindings mirror it (recommended, +since the bindings can then convert one enum to another), or reshape only +the binding records and keep Rust's snapshot flat. + +## Related + +- [Lifecycles are explicit state](/quest/m1/explicit-state/README.md) - the epic this item split from +- [Bindings' small surface fixes](/quest/m2/api-shape/bindings.md) - other breaking binding fixes before the cut From d3f4b24b19efa738acecb39e7535d4ec5def85a0 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 8 Nov 2026 00:18:54 -0800 Subject: [PATCH 05/11] fix(moq-ffi): the Go wrapper reads AlreadyResponded after an answer Regenerate the Dart bindings for the changed docs and format. Co-Authored-By: Claude Opus 5.5 --- dart/moq_ffi/lib/src/moq.dart | 6 ++---- go/wrapper/errors.go | 2 +- go/wrapper/moq_test.go | 4 ++-- rs/moq-c/src/video.rs | 7 ++++++- rs/moq-ffi/src/media.rs | 11 ++++++----- rs/moq-ffi/src/test.rs | 18 ++++++++++++++---- rs/moq-ffi/src/video.rs | 13 ++++++++++--- 7 files changed, 41 insertions(+), 20 deletions(-) diff --git a/dart/moq_ffi/lib/src/moq.dart b/dart/moq_ffi/lib/src/moq.dart index 290b21c71fd..cb27283aaa8 100644 --- a/dart/moq_ffi/lib/src/moq.dart +++ b/dart/moq_ffi/lib/src/moq.dart @@ -8,9 +8,7 @@ import "dart:ffi"; import "dart:io" show Platform, File, Directory; import "dart:isolate"; import "dart:typed_data"; - import "package:ffi/ffi.dart"; - import "uniffi_runtime.dart"; export "uniffi_runtime.dart"; @@ -24694,7 +24692,7 @@ void _checkApiChecksums() { 55377) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } - if (uniffi_moq_ffi_checksum_method_moqrequest_accept() != 55136) { + if (uniffi_moq_ffi_checksum_method_moqrequest_accept() != 30910) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } if (uniffi_moq_ffi_checksum_method_moqrequest_cancel() != 25859) { @@ -24710,7 +24708,7 @@ void _checkApiChecksums() { if (uniffi_moq_ffi_checksum_method_moqrequest_query() != 23842) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } - if (uniffi_moq_ffi_checksum_method_moqrequest_reject() != 2829) { + if (uniffi_moq_ffi_checksum_method_moqrequest_reject() != 53566) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } if (uniffi_moq_ffi_checksum_method_moqrequest_transport() != 57171) { diff --git a/go/wrapper/errors.go b/go/wrapper/errors.go index 73057fa29c9..79bce50789e 100644 --- a/go/wrapper/errors.go +++ b/go/wrapper/errors.go @@ -61,7 +61,7 @@ var ( ErrBind = ffi.ErrMoqErrorBind // ErrReject is returned when a session is refused during the handshake. ErrReject = ffi.ErrMoqErrorReject - // ErrAlreadyResponded is returned when a Request is accepted or rejected more than once. + // ErrAlreadyResponded is returned when a request is used after it was accepted or rejected. ErrAlreadyResponded = ffi.ErrMoqErrorAlreadyResponded // ErrCodec is returned when codec configuration or bitstream parsing fails. ErrCodec = ffi.ErrMoqErrorCodec diff --git a/go/wrapper/moq_test.go b/go/wrapper/moq_test.go index 99063943ee5..e92b2f2cbd5 100644 --- a/go/wrapper/moq_test.go +++ b/go/wrapper/moq_test.go @@ -1202,8 +1202,8 @@ func TestDynamicTrackRequestCanPublishAudio(t *testing.T) { if mediaName != "requested-audio" { t.Fatalf("media name = %q, want requested-audio", mediaName) } - if _, err := request.Name(); !errors.Is(err, moq.ErrClosed) { - t.Fatalf("request name after accept error = %v, want ErrClosed", err) + if _, err := request.Name(); !errors.Is(err, moq.ErrAlreadyResponded) { + t.Fatalf("request name after accept error = %v, want ErrAlreadyResponded", err) } var mediaConsumer *moqmedia.TrackConsumer diff --git a/rs/moq-c/src/video.rs b/rs/moq-c/src/video.rs index fd532ed4f69..3554981e585 100644 --- a/rs/moq-c/src/video.rs +++ b/rs/moq-c/src/video.rs @@ -1457,7 +1457,12 @@ mod tests { let moved = tokio::task::spawn_blocking(move || { let reservation = |shared: &Shared| { let guard = shared.lock(); - guard.as_ref().unwrap().follow.as_ref().map(|follow| follow.reservation.clone()) + guard + .as_ref() + .unwrap() + .follow + .as_ref() + .map(|follow| follow.reservation.clone()) }; let publish = |timestamp: moq_net::Timestamp, data: &[u8]| { let mut guard = shared.lock(); diff --git a/rs/moq-ffi/src/media.rs b/rs/moq-ffi/src/media.rs index 9a95920d796..8c001b675d1 100644 --- a/rs/moq-ffi/src/media.rs +++ b/rs/moq-ffi/src/media.rs @@ -1091,11 +1091,12 @@ impl MoqMediaTrackStreamProducer { broadcast.with_state(|state| { let priority = init.priority; target.import(&state.broadcast, init.format, |request| { - let import = moq_mux::import::TrackStream::video(request, state.catalog.reserve(), init).map_err( - |err| MoqError::Codec { - reason: format!("init failed: {err}"), - }, - )?; + let import = + moq_mux::import::TrackStream::video(request, state.catalog.reserve(), init).map_err(|err| { + MoqError::Codec { + reason: format!("init failed: {err}"), + } + })?; state.children.adopt(Arc::new(MoqMediaTrackStreamProducer { inner: std::sync::Mutex::new(Some(MediaStreamProducer { import: Box::new(import), diff --git a/rs/moq-ffi/src/test.rs b/rs/moq-ffi/src/test.rs index cb875cd0e24..228e12079fb 100644 --- a/rs/moq-ffi/src/test.rs +++ b/rs/moq-ffi/src/test.rs @@ -3140,15 +3140,23 @@ async fn a_track_request_reports_its_answer() { tokio::spawn(async move { consumer.subscribe_track(name.into(), None).await }) }; let _first = subscribe("first"); - let first = kio::clock::timeout(TIMEOUT, dynamic.requested_track()).await.unwrap().unwrap(); + let first = kio::clock::timeout(TIMEOUT, dynamic.requested_track()) + .await + .unwrap() + .unwrap(); first.accept(None).unwrap(); let rejected = first.reject(0); let _second = subscribe("second"); - let second = kio::clock::timeout(TIMEOUT, dynamic.requested_track()).await.unwrap().unwrap(); + let second = kio::clock::timeout(TIMEOUT, dynamic.requested_track()) + .await + .unwrap() + .unwrap(); let failed = MoqMediaTrackProducer::audio( &broadcast, - MoqMediaTarget::Requested { request: second.clone() }, + MoqMediaTarget::Requested { + request: second.clone(), + }, audio_init(MoqAudioFormat::Opus, vec![1, 2, 3]), ); let failed = failed.map(|_| ()); @@ -8063,7 +8071,9 @@ async fn shutdown_cancels_and_drops_cleanly() { // A synchronous call fails like the parked asynchronous ones, on a driver shutdown dropped // and on one created after it, which never ran. let late = MoqOriginProducer::new(MoqOriginConfig::default()); - let routed = server_origin.route("late".into(), MoqRouteOptions::default()).map(|_| ()); + let routed = server_origin + .route("late".into(), MoqRouteOptions::default()) + .map(|_| ()); let unrun = late.route("late".into(), MoqRouteOptions::default()).map(|_| ()); assert!( matches!(routed, Err(MoqError::Cancelled)) && matches!(unrun, Err(MoqError::Cancelled)), diff --git a/rs/moq-ffi/src/video.rs b/rs/moq-ffi/src/video.rs index 08ce3ee4fb0..92097b0c6cc 100644 --- a/rs/moq-ffi/src/video.rs +++ b/rs/moq-ffi/src/video.rs @@ -1522,7 +1522,15 @@ mod finish_tests { .await .unwrap(); // Stand in for a follower thread that panicked. - video.inner.lock().unwrap().as_ref().unwrap().follow.as_ref().expect("published against an allocator") + video + .inner + .lock() + .unwrap() + .as_ref() + .unwrap() + .follow + .as_ref() + .expect("published against an allocator") .follower .failed .store(true, Ordering::Release); @@ -1618,8 +1626,7 @@ mod finish_tests { let track = broadcast .create_track("camera", catalog.track_info(hang::catalog::PRIORITY.video)) .unwrap(); - let producer = - moq_video::encode::Producer::with_track(track.clone(), catalog.reserve(), rendition).unwrap(); + let producer = moq_video::encode::Producer::with_track(track.clone(), catalog.reserve(), rendition).unwrap(); let encoder = moq_video::encode::Sink::open(&encode).await.unwrap(); let mut video = VideoProducer { encoder: Some(encoder), From d63f86d83b428bda0359aaadc3d71ecb4b614bd2 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 8 Nov 2026 08:04:52 -0800 Subject: [PATCH 06/11] fix(ffi): a failed publish still moves the claim to the new track A write whose decoder identity change switched the track and then failed to publish left the reservation and follower on the ended track, in both moq-ffi and moq-c. The claim now moves before the publish error returns. Also records the maintainer's decision to reshape Observation together with MoqConnectionState, lists that quest in the m2 index, and checks the saved failure's reason in the request test. Co-Authored-By: Claude Opus 5.5 --- quest/m2/README.md | 1 + quest/m2/connection-state-phases.md | 14 ++++++++------ rs/moq-c/src/video.rs | 6 ++++-- rs/moq-ffi/src/test.rs | 7 +++---- rs/moq-ffi/src/video.rs | 17 ++++++++++------- 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/quest/m2/README.md b/quest/m2/README.md index 5955c29d242..6b2a9c5edac 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -198,6 +198,7 @@ quests that require them. - [moq-quic drops inherited surfaces with no consumer](/quest/m2/moq-quic-unused-surface.md) - unused accept, connection, and stream-id surfaces leave moq-quic's public API before the cut - [Publish reports rendition stats as hang's type](/quest/m2/rendition-stats-type.md) - `Rendition.stats` and `Report.Writer` take the rendition stats schema's input type, so encoders write plain numbers - [No sentinel values in public API](/quest/m2/no-sentinels.md) - receive windows, cache durations, reconnect timeouts, and the FFI test clock use `Option` instead of a huge value or zero +- [Connection state carries each phase's data](/quest/m2/connection-state-phases.md) - moq-tokio's `Observation`, moq-ffi's `MoqConnectionState`, and moq-c's `moq_connection_state` each hold one phase per value, so no field is readable in a phase where it means nothing - [moq-video takes devices, not environment variables](/quest/m2/video-devices.md) - VA-API and V4L2 device paths are config fields, and `moq_video::ENV` never ships - [Gateways share one ingest config and one rate cap](/quest/m2/gateway-ingest.md) - `moq_mux::Ingest` replaces 18 per-gateway setters, and the live rate cap moves to `moq_net::bandwidth` - [Native playout sync keeps tracks decoupled](/quest/m2/mux-sync.md) - `moq_mux::timeline` becomes `moq_mux::sync`, and one slow track no longer stalls or moves another diff --git a/quest/m2/connection-state-phases.md b/quest/m2/connection-state-phases.md index 6adc627f7f3..b306fc61665 100644 --- a/quest/m2/connection-state-phases.md +++ b/quest/m2/connection-state-phases.md @@ -9,8 +9,8 @@ a field that means nothing in the current phase can't be read: record (`revision`, `phase`, `connects`, `failure`, `version`), so `failure` and `version` are readable in phases where they mean nothing. It becomes a phase enum whose variants carry their own data, mirroring - whatever shape Rust's connection state takes, and every hand-written - wrapper follows. + Rust's `moq_tokio::connection::Observation`, which takes the same shape, + and every hand-written wrapper follows. - moq-c's `moq_connection_state` follows the same shape and renames its `protocol` to `version`. @@ -25,10 +25,12 @@ how to migrate. Rust's `moq_tokio::connection::Observation` (`rs/moq-tokio/src/connection.rs`) is flat too: `failure`, `version`, `transport`, and `last_connected` sit beside `phase`, while the private `Lifecycle` enum it is built from already -carries them per phase. Open, for whoever starts this, with the maintainer: -reshape `Observation` the same way so the bindings mirror it (recommended, -since the bindings can then convert one enum to another), or reshape only -the binding records and keep Rust's snapshot flat. +carries them per phase. + +- ✅ Decided 2026-11-08 (maintainer): reshape `Observation` together with + `MoqConnectionState`, one phase per value in both, so each binding converts + one enum to another (rejected: reshape only the binding records and keep + Rust's snapshot flat). ## Related diff --git a/rs/moq-c/src/video.rs b/rs/moq-c/src/video.rs index 3554981e585..fc1e0358e80 100644 --- a/rs/moq-c/src/video.rs +++ b/rs/moq-c/src/video.rs @@ -623,15 +623,17 @@ impl VideoEncoder { // zero or more access units rather than one per call. let encoded = block_on(encoder.encode(frame))?; let before = self.producer.demand(); - self.producer.publish(&encoded)?; + let published = self.producer.publish(&encoded); // A frame whose decoder identity changed, such as a resume at a ceiling past the declared // level, moved the rendition to a newly named track, whose demand the claim has to watch. + // The claim moves even when publishing on the new track failed, so it never stays on the + // ended one. if self.producer.demand().name() != before.name() && let Some(follow) = &self.follow { self.follow_grant(shared, follow.allocator.clone()); } - Ok(()) + Ok(published?) } fn publish_cut(&mut self) -> Result<(), Error> { diff --git a/rs/moq-ffi/src/test.rs b/rs/moq-ffi/src/test.rs index 7f5c3d4f3f5..4784c2ffcf8 100644 --- a/rs/moq-ffi/src/test.rs +++ b/rs/moq-ffi/src/test.rs @@ -3162,10 +3162,9 @@ async fn a_track_request_reports_its_answer() { let failed = failed.map(|_| ()); assert!(matches!(failed, Err(MoqError::Codec { .. })), "{failed:?}"); let later = second.reject(0); - assert!( - matches!(rejected, Err(MoqError::AlreadyResponded)) && matches!(later, Err(MoqError::Codec { .. })), - "{rejected:?} {later:?}" - ); + assert!(matches!(rejected, Err(MoqError::AlreadyResponded)), "{rejected:?}"); + // The saved failure is the import's own error, reason included. + assert_eq!(format!("{later:?}"), format!("{failed:?}")); } #[tokio::test] diff --git a/rs/moq-ffi/src/video.rs b/rs/moq-ffi/src/video.rs index 92097b0c6cc..2e1cec7353d 100644 --- a/rs/moq-ffi/src/video.rs +++ b/rs/moq-ffi/src/video.rs @@ -631,24 +631,27 @@ impl MoqVideoProducer { /// bandwidth, the frame is dropped without encoding. pub fn write(&self, frame: MoqVideoFrame) -> Result<(), MoqError> { let _guard = crate::ffi::runtime().enter(); - let moved = self.with(|producer| { + let (written, moved) = self.with(|producer| { let before = producer.producer.demand(); - producer.write(frame)?; + let written = producer.write(frame); // A frame whose decoder identity changed, such as a resume at a ceiling past the // declared level, moved the rendition to a newly named track, and the claim moves - // with it under the same lock. + // with it under the same lock. It moves even when publishing on the new track + // failed, so the claim never stays on the ended one. let after = producer.producer.demand(); if after.name() == before.name() { - return Ok(None); + return Ok((written, None)); } let ceiling = *producer.ceiling.read(); - Ok(producer.follow.as_mut().map(|follow| { + let moved = producer.follow.as_mut().map(|follow| { let moved = Following::start(follow.allocator.clone(), follow.lower.clone(), &after, ceiling, self); std::mem::replace(follow, moved).follower - })) + }); + Ok((written, moved)) })?; // Outside the lock, which the old follower takes on its way out. - moved.map_or(Ok(()), Follow::stop) + let stopped = moved.map_or(Ok(()), Follow::stop); + written.and(stopped) } /// Cut a new group at the next written frame. From 9d7a2ea64595392b61cbc2b95e96aa32de61138b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 8 Nov 2026 09:01:50 -0800 Subject: [PATCH 07/11] fix(ffi,c): a moved claim resumes from the encoder's target, and a disable drain moves it too Co-Authored-By: Claude Opus 5.5 --- rs/moq-c/src/video.rs | 47 ++++++++++++++++---- rs/moq-ffi/src/video.rs | 96 +++++++++++++++++++++++++---------------- rs/moq-mux/src/rate.rs | 37 ++++++++++++++++ 3 files changed, 135 insertions(+), 45 deletions(-) diff --git a/rs/moq-c/src/video.rs b/rs/moq-c/src/video.rs index fc1e0358e80..e833332c631 100644 --- a/rs/moq-c/src/video.rs +++ b/rs/moq-c/src/video.rs @@ -313,9 +313,11 @@ async fn follow_reservation( consumer: moq_net::bandwidth::Consumer, reservation: std::sync::Weak, ceiling: moq_net::bandwidth::Rate, + target: moq_mux::rate::Target, ) { let noop = kio::Waiter::noop(); let mut follower = moq_mux::rate::Follower::new(consumer, video_policy(ceiling, lower_ceiling(&lower, &noop))); + follower.assume(target); loop { // Both ceilings are read on every wake, before the follower filters a grant against // them. This one's `moq_encode_video_bitrate` moves its reservation after moving its @@ -343,7 +345,11 @@ async fn follow_reservation( return; }; let policy = video_policy(*producer.ceiling.read(), below); - if retune(&mut follower, policy, target, |target| producer.apply(target)).is_err() { + let before = producer.producer.demand(); + let retuned = retune(&mut follower, policy, target, |target| producer.apply(target)); + // A disable drains the encoder into the track, and output with a new decoder identity + // moves the rendition, so the claim moves with it and this task ends. + if producer.follow_track(before.name(), &inner) || retuned.is_err() { return; } } @@ -414,6 +420,9 @@ pub(crate) struct VideoEncoder { /// allocator. Beside the encoder, so a move to a new track, a finish, and an abort change /// all three together. follow: Option, + /// What the encoder currently produces, which a follower started on a moved track resumes + /// from rather than assuming the ceiling. + target: moq_mux::rate::Target, /// The manual ceiling [`moq_encode_video_bitrate`] last applied, which caps every /// grant. Behind the encoder's lock, so the follower never reads an old, higher /// one and retunes over a newer cap. A channel, so a rung above this rendition @@ -563,11 +572,12 @@ impl VideoEncoder { let lower = self.lower.clone(); let consumer = reservation.consume(); let identity = Arc::downgrade(&reservation); + let target = self.target; async move { tokio::select! { biased; _ = stopped => {} - _ = follow_reservation(shared, lower, consumer, identity, ceiling) => {} + _ = follow_reservation(shared, lower, consumer, identity, ceiling, target) => {} } Ok(()) } @@ -580,6 +590,19 @@ impl VideoEncoder { }); } + /// Move the claim to the track the rendition is on now, if publishing moved it off the one + /// named `before`, such as on a decoder identity change. Returns whether it moved. + fn follow_track(&mut self, before: &str, shared: &Shared) -> bool { + if self.producer.demand().name() == before { + return false; + } + let Some(follow) = &self.follow else { + return false; + }; + self.follow_grant(shared, follow.allocator.clone()); + true + } + /// Whether this rendition still follows `reservation`, rather than having moved off the /// track it was reserved on or ended. The [`Weak`](std::sync::Weak) holds the allocation, so /// a newer reservation never takes its address. @@ -628,11 +651,7 @@ impl VideoEncoder { // level, moved the rendition to a newly named track, whose demand the claim has to watch. // The claim moves even when publishing on the new track failed, so it never stays on the // ended one. - if self.producer.demand().name() != before.name() - && let Some(follow) = &self.follow - { - self.follow_grant(shared, follow.allocator.clone()); - } + self.follow_track(before.name(), shared); Ok(published?) } @@ -652,6 +671,7 @@ impl VideoEncoder { // A disabled rung has no encoder to retune; it reopens at the new ceiling. if let Some(encoder) = self.encoder.as_mut() { block_on(encoder.set_bitrate(bitrate))?; + self.target = moq_mux::rate::Target::Enabled(bitrate); } let Ok(mut ceiling) = self.ceiling.write() else { unreachable!("only dropping the encoder closes its ceiling") @@ -680,6 +700,7 @@ impl VideoEncoder { // advertises a rendition whose frames are dropped. self.producer.set_enabled(false)?; if let Some(encoder) = self.encoder.take() { + self.target = moq_mux::rate::Target::Disabled; // The encoder is gone either way, so a lost tail only costs those frames. let drained = block_on(encoder.finish()).and_then(|encoded| self.producer.publish(&encoded)); if let Err(err) = drained { @@ -691,7 +712,9 @@ impl VideoEncoder { } }; if let Some(encoder) = self.encoder.as_mut() { - return block_on(encoder.set_bitrate(bitrate)); + block_on(encoder.set_bitrate(bitrate))?; + self.target = target; + return Ok(()); } let ceiling = *self.ceiling.read(); @@ -709,6 +732,10 @@ impl VideoEncoder { } self.producer.set_enabled(true)?; self.encoder = Some(encoder); + self.target = moq_mux::rate::Target::Enabled(match retuned { + Ok(()) => bitrate.min(ceiling), + Err(_) => ceiling, + }); retuned } @@ -798,6 +825,7 @@ impl Video { format, size: config.size(), follow: None, + target: moq_mux::rate::Target::Enabled(ceiling), ceiling: kio::Producer::new(ceiling), children: target.children.clone(), }); @@ -1452,6 +1480,7 @@ mod tests { format: moq_video_pixel_format::MOQ_VIDEO_PIXEL_FORMAT_RGBA, size: probed.size(), follow: None, + target: moq_mux::rate::Target::Enabled(moq_net::bandwidth::Rate::from_mbps(1)), ceiling: kio::Producer::new(moq_net::bandwidth::Rate::from_mbps(1)), children: Default::default(), }); @@ -1527,6 +1556,7 @@ mod tests { format: moq_video_pixel_format::MOQ_VIDEO_PIXEL_FORMAT_I420, size: encode.size(), follow: None, + target: moq_mux::rate::Target::Enabled(moq_net::bandwidth::Rate::from_mbps(1)), ceiling: kio::Producer::new(moq_net::bandwidth::Rate::from_mbps(1)), children: Default::default(), }); @@ -1617,6 +1647,7 @@ mod tests { _stop: oneshot::channel().0, follower, }), + target: moq_mux::rate::Target::Enabled(moq_net::bandwidth::Rate::from_mbps(1)), ceiling: kio::Producer::new(moq_net::bandwidth::Rate::from_mbps(1)), children: Default::default(), }; diff --git a/rs/moq-ffi/src/video.rs b/rs/moq-ffi/src/video.rs index 2e1cec7353d..5d956c28e53 100644 --- a/rs/moq-ffi/src/video.rs +++ b/rs/moq-ffi/src/video.rs @@ -11,7 +11,7 @@ //! timestamp. use std::sync::Arc; -use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::sync::atomic::{AtomicBool, Ordering}; use tokio::sync::oneshot; @@ -353,6 +353,9 @@ struct VideoProducer { ceiling: kio::Producer, /// The track priority this rendition was published at; a higher rung takes one below. priority: i8, + /// What the encoder currently produces, which a follower started on a moved track resumes + /// from rather than assuming the ceiling. + target: moq_mux::rate::Target, /// The bandwidth claim on the current track and its follower, when published against /// an allocator. Beside the encoder, so a move to a new track, a finish, and an abort /// change all three together. @@ -372,22 +375,23 @@ struct Following { } impl Following { - /// Reserve `ceiling` against `demand` and follow the grant, retuning the producer in `inner`. + /// Reserve `producer`'s ceiling against its current track and follow the grant from its + /// current target, retuning the producer in `inner`. fn start( allocator: Arc, lower: Option>, - demand: &moq_net::track::Demand, - ceiling: moq_net::bandwidth::Rate, - owner: &MoqVideoProducer, + producer: &VideoProducer, + inner: &Shared, ) -> Self { - let reservation = allocator.reserve_demand(demand, ceiling.as_bps()); + let ceiling = *producer.ceiling.read(); + let reservation = allocator.reserve_demand(&producer.producer.demand(), ceiling.as_bps()); let follower = spawn_follow(Followed { - inner: owner.inner.clone(), + inner: inner.clone(), lower: lower.clone(), consumer: reservation.consume(), reservation: Arc::downgrade(&reservation), ceiling, - applied: owner.applied.clone(), + target: producer.target, }); Self { allocator, @@ -443,6 +447,18 @@ impl VideoProducer { .is_some_and(|follow| std::ptr::eq(Arc::as_ptr(&follow.reservation), reservation.as_ptr())) } + /// Move the claim to the track the rendition is on now, if publishing moved it off the one + /// named `before`, such as on a decoder identity change. Returns the replaced follower, which + /// takes the producer's lock on its way out. + fn follow_track(&mut self, before: &str, inner: &Shared) -> Option { + if self.producer.demand().name() == before { + return None; + } + let follow = self.follow.as_ref()?; + let moved = Following::start(follow.allocator.clone(), follow.lower.clone(), self, inner); + Some(std::mem::replace(self.follow.as_mut()?, moved).follower) + } + fn write(&mut self, frame: MoqVideoFrame) -> Result<(), MoqError> { // A buffer that isn't one picture at the configured size is rejected here, // by the surface constructors, rather than reinterpreted. @@ -468,6 +484,7 @@ impl VideoProducer { // A disabled rung has no encoder to retune; it reopens at the new ceiling. if let Some(encoder) = self.encoder.as_mut() { block_on(encoder.set_bitrate(bitrate))?; + self.target = moq_mux::rate::Target::Enabled(bitrate); } // Under the same lock as the retune, so the follower sees both or neither. let Ok(mut ceiling) = self.ceiling.write() else { @@ -497,6 +514,7 @@ impl VideoProducer { // advertises a rendition whose frames are dropped. self.producer.set_enabled(false)?; if let Some(encoder) = self.encoder.take() { + self.target = moq_mux::rate::Target::Disabled; // The encoder is gone either way, so a lost tail only costs those frames. let drained = block_on(encoder.finish()).and_then(|encoded| self.producer.publish(&encoded)); if let Err(err) = drained { @@ -508,7 +526,9 @@ impl VideoProducer { } }; if let Some(encoder) = self.encoder.as_mut() { - return block_on(encoder.set_bitrate(bitrate)); + block_on(encoder.set_bitrate(bitrate))?; + self.target = target; + return Ok(()); } let ceiling = *self.ceiling.read(); @@ -526,6 +546,10 @@ impl VideoProducer { } self.producer.set_enabled(true)?; self.encoder = Some(encoder); + self.target = moq_mux::rate::Target::Enabled(match retuned { + Ok(()) => bitrate.min(ceiling), + Err(_) => ceiling, + }); retuned } @@ -564,9 +588,7 @@ impl VideoProducer { #[derive(uniffi::Object)] pub struct MoqVideoProducer { /// `None` once finished or aborted. - inner: Arc>>, - /// Last bitrate applied by the follow loop or [`set_bitrate`](Self::set_bitrate). - applied: Arc, + inner: Shared, children: crate::producer::Children, } @@ -585,9 +607,13 @@ impl MoqVideoProducer { self.with(|producer| Ok(producer.producer.demand())) } + /// The bitrate the encoder produces at, or 0 while disabled. #[cfg(test)] pub(crate) fn applied_bitrate(&self) -> u64 { - self.applied.load(Ordering::SeqCst) + match self.inner.lock().unwrap().as_ref().map(|producer| producer.target) { + Some(moq_mux::rate::Target::Enabled(bitrate)) => bitrate.as_bps(), + _ => 0, + } } } @@ -638,16 +664,7 @@ impl MoqVideoProducer { // declared level, moved the rendition to a newly named track, and the claim moves // with it under the same lock. It moves even when publishing on the new track // failed, so the claim never stays on the ended one. - let after = producer.producer.demand(); - if after.name() == before.name() { - return Ok((written, None)); - } - let ceiling = *producer.ceiling.read(); - let moved = producer.follow.as_mut().map(|follow| { - let moved = Following::start(follow.allocator.clone(), follow.lower.clone(), &after, ceiling, self); - std::mem::replace(follow, moved).follower - }); - Ok((written, moved)) + Ok((written, producer.follow_track(before.name(), &self.inner))) })?; // Outside the lock, which the old follower takes on its way out. let stopped = moved.map_or(Ok(()), Follow::stop); @@ -699,9 +716,7 @@ impl MoqVideoProducer { /// stop publishing. pub fn set_bitrate(&self, bitrate: u64) -> Result<(), MoqError> { let _guard = crate::ffi::runtime().enter(); - self.with(|producer| producer.set_ceiling(moq_net::bandwidth::Rate::from_bps(bitrate)))?; - self.applied.store(bitrate, Ordering::SeqCst); - Ok(()) + self.with(|producer| producer.set_ceiling(moq_net::bandwidth::Rate::from_bps(bitrate))) } /// This encoder's bandwidth reservation, if it was published against a @@ -839,9 +854,9 @@ impl MoqVideoProducer { config, ceiling: kio::Producer::new(ceiling), priority, + target: moq_mux::rate::Target::Enabled(ceiling), follow: None, }))), - applied: Arc::new(AtomicU64::new(ceiling.as_bps())), children: children.clone(), }; if let Some(allocator) = bandwidth { @@ -849,8 +864,7 @@ impl MoqVideoProducer { let mut guard = video.inner.lock().map_err(|_| panic_error())?; let producer = guard.as_mut().expect("just constructed"); let lower = lower.map(|(lower, _, _)| lower); - let demand = producer.producer.demand(); - producer.follow = Some(Following::start(allocator, lower, &demand, ceiling, &video)); + producer.follow = Some(Following::start(allocator, lower, producer, &video.inner)); } children.adopt(Arc::new(video)) } @@ -873,9 +887,12 @@ fn video_ceiling( }) } +/// The rendition behind a [`MoqVideoProducer`], `None` once finished or aborted. +type Shared = Arc>>; + /// What the follow thread reads and retunes. struct Followed { - inner: Arc>>, + inner: Shared, /// The next lower rendition's ceiling, which sets this one's band. A consumer, so a rung /// never keeps the rendition below it publishing. lower: Option>, @@ -883,7 +900,8 @@ struct Followed { /// The claim this thread follows, which retires it once the rendition moves off it or ends. reservation: std::sync::Weak, ceiling: moq_net::bandwidth::Rate, - applied: Arc, + /// What the encoder produces when the follower starts. + target: moq_mux::rate::Target, } fn spawn_follow(followed: Followed) -> Follow { @@ -949,10 +967,11 @@ async fn follow_reservation(followed: Followed) { consumer, reservation, ceiling, - applied, + target, } = followed; let noop = kio::Waiter::noop(); let mut follower = moq_mux::rate::Follower::new(consumer, video_policy(ceiling, lower_ceiling(&lower, &noop))); + follower.assume(target); loop { // Both ceilings are read on every wake, before the follower filters a grant against // them. This one's `set_bitrate` moves its reservation after moving its ceiling, @@ -980,11 +999,13 @@ async fn follow_reservation(followed: Followed) { return; }; let policy = video_policy(*producer.ceiling.read(), below); - match retune(&mut follower, policy, target, |target| producer.apply(target)) { - Ok(Some(moq_mux::rate::Target::Enabled(bitrate))) => applied.store(bitrate.as_bps(), Ordering::SeqCst), - Ok(Some(moq_mux::rate::Target::Disabled)) => applied.store(0, Ordering::SeqCst), - Ok(None) => {} - Err(_) => return, + let before = producer.producer.demand(); + let retuned = retune(&mut follower, policy, target, |target| producer.apply(target)); + // A disable drains the encoder into the track, and output with a new decoder identity + // moves the rendition, so the claim moves with it. The replaced follower is this + // thread, which ends here, so it is detached rather than joined. + if producer.follow_track(before.name(), &inner).is_some() || retuned.is_err() { + return; } } } @@ -1637,6 +1658,7 @@ mod finish_tests { config: encode.clone(), format: MoqVideoPixelFormat::I420, size: encode.size(), + target: moq_mux::rate::Target::Enabled(moq_net::bandwidth::Rate::from_mbps(1)), ceiling: kio::Producer::new(moq_net::bandwidth::Rate::from_mbps(1)), priority: hang::catalog::PRIORITY.video, follow: None, diff --git a/rs/moq-mux/src/rate.rs b/rs/moq-mux/src/rate.rs index c61d8250dec..c5b098655d6 100644 --- a/rs/moq-mux/src/rate.rs +++ b/rs/moq-mux/src/rate.rs @@ -445,6 +445,23 @@ impl Follower { } } + /// Follow on from a sender already producing at `target`, such as one moving to a new + /// grant, rather than at [`Policy::max`]. The current grant is fed on the next poll, so a + /// sender below a grant that has room climbs back without waiting for the grant to change. + pub fn assume(&mut self, target: Target) { + match target { + Target::Enabled(rate) => { + self.control.target = rate.min(self.control.policy.max); + self.control.enabled = true; + } + Target::Disabled => self.control.enabled = false, + } + self.control.applied = None; + self.control.deadline = None; + self.control.last = None; + self.refeed = true; + } + /// The policy the target follows. pub fn policy(&self) -> &Policy { &self.control.policy @@ -943,6 +960,26 @@ mod tests { assert!(applied.windows(2).all(|pair| pair[0] < pair[1]), "{applied:?}"); } + /// A follower that assumes its sender's state corrects it against an unchanged grant: a + /// sender below a full grant climbs back, and a disabled one resumes. + #[tokio::test(start_paused = true)] + async fn an_assumed_target_climbs_to_the_grant() { + let grant = bandwidth::Producer::new(); + grant.set(Some(bps(4_000_000))).unwrap(); + + let mut follower = Follower::new(grant.consume(), Policy::new(bps(4_000_000))); + follower.assume(on(bps(1_000_000))); + let mut last = None; + while let Ok(target) = kio::clock::timeout(Duration::from_secs(60), follower.next()).await { + last = Some(target.unwrap()); + } + assert_eq!(last, Some(on(bps(4_000_000)))); + + let mut follower = Follower::new(grant.consume(), Policy::new(bps(4_000_000))); + follower.assume(Target::Disabled); + assert!(matches!(follower.next().await.unwrap(), Target::Enabled(_))); + } + /// A new policy feeds the unchanged grant through it at once, and setting the same /// policy again changes nothing. #[tokio::test(start_paused = true)] From faf4a706494b6a4154c106b40391f509e6ba9cd3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 8 Nov 2026 10:14:30 -0800 Subject: [PATCH 08/11] fix(mux): an assumed target ramps from now Co-Authored-By: Claude Opus 5.5 --- rs/moq-mux/src/rate.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/rs/moq-mux/src/rate.rs b/rs/moq-mux/src/rate.rs index c5b098655d6..f5195b9473a 100644 --- a/rs/moq-mux/src/rate.rs +++ b/rs/moq-mux/src/rate.rs @@ -453,10 +453,14 @@ impl Follower { Target::Enabled(rate) => { self.control.target = rate.min(self.control.policy.max); self.control.enabled = true; + // The sender is at this rate now, so a raise ramps from here. + self.control.applied = Some(kio::clock::now()); + } + Target::Disabled => { + self.control.enabled = false; + self.control.applied = None; } - Target::Disabled => self.control.enabled = false, } - self.control.applied = None; self.control.deadline = None; self.control.last = None; self.refeed = true; @@ -961,7 +965,7 @@ mod tests { } /// A follower that assumes its sender's state corrects it against an unchanged grant: a - /// sender below a full grant climbs back, and a disabled one resumes. + /// sender below a full grant ramps back up, and a disabled one resumes. #[tokio::test(start_paused = true)] async fn an_assumed_target_climbs_to_the_grant() { let grant = bandwidth::Producer::new(); @@ -969,11 +973,12 @@ mod tests { let mut follower = Follower::new(grant.consume(), Policy::new(bps(4_000_000))); follower.assume(on(bps(1_000_000))); - let mut last = None; + let mut applied = Vec::new(); while let Ok(target) = kio::clock::timeout(Duration::from_secs(60), follower.next()).await { - last = Some(target.unwrap()); + applied.push(target.unwrap()); } - assert_eq!(last, Some(on(bps(4_000_000)))); + assert_eq!(applied.last(), Some(&on(bps(4_000_000)))); + assert!(applied.len() > 1, "the climb ramps rather than jumping: {applied:?}"); let mut follower = Follower::new(grant.consume(), Policy::new(bps(4_000_000))); follower.assume(Target::Disabled); From 1655a945689452186c18733cba06673124b40357 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 8 Nov 2026 10:31:04 -0800 Subject: [PATCH 09/11] fix(mux): an assumed target above the ceiling comes down to it Co-Authored-By: Claude Opus 5.5 --- rs/moq-mux/src/rate.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/rs/moq-mux/src/rate.rs b/rs/moq-mux/src/rate.rs index f5195b9473a..688ea07081c 100644 --- a/rs/moq-mux/src/rate.rs +++ b/rs/moq-mux/src/rate.rs @@ -330,7 +330,9 @@ impl Control { let disable = boundary.is_some_and(|boundary| desired < boundary); let under = boundary.is_some_and(|boundary| self.target < boundary); let settling = next > self.target && next == self.policy.max; - let suppressed = next == self.target || (!settling && next.abs_diff(self.target) < band(self.target)); + // A sender above the ceiling, such as an assumed one, always comes down to it. + let over = self.target > self.policy.max; + let suppressed = next == self.target || (!settling && !over && next.abs_diff(self.target) < band(self.target)); if suppressed && !(disable && under) { self.deadline = self.raise(desired); return None; @@ -447,11 +449,12 @@ impl Follower { /// Follow on from a sender already producing at `target`, such as one moving to a new /// grant, rather than at [`Policy::max`]. The current grant is fed on the next poll, so a - /// sender below a grant that has room climbs back without waiting for the grant to change. + /// sender below a grant that has room climbs back without waiting for the grant to change, + /// and one above [`Policy::max`] is lowered to it. pub fn assume(&mut self, target: Target) { match target { Target::Enabled(rate) => { - self.control.target = rate.min(self.control.policy.max); + self.control.target = rate; self.control.enabled = true; // The sender is at this rate now, so a raise ramps from here. self.control.applied = Some(kio::clock::now()); @@ -985,6 +988,19 @@ mod tests { assert!(matches!(follower.next().await.unwrap(), Target::Enabled(_))); } + /// A follower that assumes a sender above its ceiling lowers it to the ceiling, even from + /// inside the deadband, with the grant unchanged. + #[tokio::test(start_paused = true)] + async fn an_assumed_target_above_the_ceiling_comes_down() { + let grant = bandwidth::Producer::new(); + grant.set(Some(bps(2_000_000))).unwrap(); + for above in [4_000_000, 2_050_000] { + let mut follower = Follower::new(grant.consume(), Policy::new(bps(2_000_000))); + follower.assume(on(bps(above))); + assert_eq!(follower.next().await.unwrap(), on(bps(2_000_000)), "from {above}"); + } + } + /// A new policy feeds the unchanged grant through it at once, and setting the same /// policy again changes nothing. #[tokio::test(start_paused = true)] From b1e94f673738c0ed8201f8f6d60233654600df94 Mon Sep 17 00:00:00 2001 From: quest fixer Date: Sun, 8 Nov 2026 15:12:29 -0800 Subject: [PATCH 10/11] fix(moq-c): follow video producer constructor rename in test --- rs/moq-c/src/video.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rs/moq-c/src/video.rs b/rs/moq-c/src/video.rs index 1f1328c977d..ae0b513f0c6 100644 --- a/rs/moq-c/src/video.rs +++ b/rs/moq-c/src/video.rs @@ -1548,7 +1548,7 @@ mod tests { let track = broadcast .create_track("camera", catalog.track_info(hang::catalog::PRIORITY.video)) .unwrap(); - let producer = moq_video::encode::Producer::with_track(track.clone(), catalog.reserve(), rendition).unwrap(); + let producer = moq_video::encode::Producer::new(track.clone(), catalog.reserve(), rendition).unwrap(); let shared = Shared::new(VideoEncoder { encoder: Some(moq_video::encode::Sink::open(&encode).await.unwrap()), producer, From c5a7fd0e967f179a82f3dba427bc5dc9788dc6f4 Mon Sep 17 00:00:00 2001 From: quest fixer Date: Sun, 8 Nov 2026 15:16:10 -0800 Subject: [PATCH 11/11] fix(moq-ffi): follow video producer constructor rename in test --- rs/moq-ffi/src/video.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rs/moq-ffi/src/video.rs b/rs/moq-ffi/src/video.rs index 1bc57e60f1a..3d359278e94 100644 --- a/rs/moq-ffi/src/video.rs +++ b/rs/moq-ffi/src/video.rs @@ -1649,7 +1649,7 @@ mod finish_tests { let track = broadcast .create_track("camera", catalog.track_info(hang::catalog::PRIORITY.video)) .unwrap(); - let producer = moq_video::encode::Producer::with_track(track.clone(), catalog.reserve(), rendition).unwrap(); + let producer = moq_video::encode::Producer::new(track.clone(), catalog.reserve(), rendition).unwrap(); let encoder = moq_video::encode::Sink::open(&encode).await.unwrap(); let mut video = VideoProducer { encoder: Some(encoder),