diff --git a/dart/moq_ffi/lib/src/moq.dart b/dart/moq_ffi/lib/src/moq.dart index d52e3abe28e..b1407ed2e86 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 6bbe426013c..f5384eea6ee 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/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/quest/m1/explicit-state/README.md b/quest/m1/explicit-state/README.md index 29889688da4..85017a16f54 100644 --- a/quest/m1/explicit-state/README.md +++ b/quest/m1/explicit-state/README.md @@ -70,7 +70,7 @@ which collapses that state and waits on [Drop set](/quest/m1/drop-set.md). ## Required - [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 - [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 - [TS and fMP4 export lifecycles](/quest/m1/explicit-state/mux-gst.md) - the TS export program and tracks and the fMP4 export track 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/README.md b/quest/m2/README.md index 894e0c4e386..70ef38a0880 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -197,6 +197,7 @@ quests that require them. - [Unbounded groups](/quest/m2/unbounded-groups/README.md) - groups and frames lose their size caps; a group keeps a 32 MB tail, and readers behind it replay through paced flights or refresh - [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 - [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` - [JS time values are types, not numbers](/quest/m2/js-time-types.md) - every public JS duration and instant is a `Clock.Duration` or `Clock.Instant` object, so a unit mistake fails to compile diff --git a/quest/m2/connection-state-phases.md b/quest/m2/connection-state-phases.md new file mode 100644 index 00000000000..b306fc61665 --- /dev/null +++ b/quest/m2/connection-state-phases.md @@ -0,0 +1,38 @@ +# [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 + 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`. + +## 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. + +- ✅ 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 + +- [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 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 8e7e1229dcc..ae0b513f0c6 100644 --- a/rs/moq-c/src/video.rs +++ b/rs/moq-c/src/video.rs @@ -311,10 +311,13 @@ async fn follow_reservation( inner: Shared, lower: Option>, 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 @@ -324,7 +327,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,11 +339,17 @@ 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); - 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; } } @@ -407,12 +416,13 @@ 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, + /// 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 @@ -421,9 +431,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 +559,74 @@ 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); + let target = self.target; + async move { + tokio::select! { + biased; + _ = stopped => {} + _ = follow_reservation(shared, lower, consumer, identity, ceiling, target) => {} + } + Ok(()) + } + }); + self.follow = Some(Following { + allocator, + reservation, + _stop: stop, + follower, + }); + } + + /// 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. + 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,23 +639,24 @@ 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 // 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. - Ok(self.producer.demand().name() != before.name()) + // 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. + self.follow_track(before.name(), shared); + Ok(published?) } 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,21 +666,20 @@ 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() { 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") }; *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,7 +695,12 @@ 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() { + 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 { @@ -652,11 +708,13 @@ impl VideoEncoder { } self.producer.discontinuity()?; } - return self.producer.set_enabled(false); + return Ok(()); } }; 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(); @@ -674,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 } @@ -682,12 +744,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 +762,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()) } } @@ -759,10 +823,8 @@ impl Video { lower, format, size: config.size(), - allocator: None, - reservation: None, follow: None, - followed: None, + target: moq_mux::rate::Target::Enabled(ceiling), ceiling: kio::Producer::new(ceiling), children: target.children.clone(), }); @@ -774,18 +836,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. @@ -1111,16 +1174,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) }) } @@ -1423,25 +1481,30 @@ 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, + 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(), }); + 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() + let reservation = |shared: &Shared| { + let guard = shared.lock(); + guard + .as_ref() .unwrap() - .publish_frame(moq_net::Timestamp::ZERO, &small) - .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. { @@ -1453,26 +1516,73 @@ 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::new(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, + 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(), + }); + 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] @@ -1520,8 +1630,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, @@ -1530,10 +1643,13 @@ 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, + }), + 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/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/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 diff --git a/rs/moq-ffi/src/media.rs b/rs/moq-ffi/src/media.rs index 0b1a19e294d..2b5c876db80 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,21 @@ 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 { - 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(), - })) + 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(), + })) + }) }) } } diff --git a/rs/moq-ffi/src/origin.rs b/rs/moq-ffi/src/origin.rs index d1b252fc084..1f4f3f4c116 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, } @@ -555,23 +555,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. @@ -588,10 +582,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. @@ -603,17 +598,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(()) + }) } } @@ -692,7 +689,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 d2473f8919b..4784c2ffcf8 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,45 @@ 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)), "{rejected:?}"); + // The saved failure is the import's own error, reason included. + assert_eq!(format!("{later:?}"), format!("{failed:?}")); +} + #[tokio::test] async fn dynamic_track_request_can_publish_media() { let broadcast = MoqBroadcastProducer::new().unwrap(); @@ -3164,7 +3203,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 +5085,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 @@ -5627,7 +5666,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 @@ -8032,6 +8071,18 @@ 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 29ab3e91e77..3d359278e94 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,15 +353,62 @@ 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. + follow: Option, +} + +/// 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, } -/// Stops the follow thread when the producer is finished or dropped. +impl Following { + /// 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>, + producer: &VideoProducer, + inner: &Shared, + ) -> Self { + let ceiling = *producer.ceiling.read(); + let reservation = allocator.reserve_demand(&producer.producer.demand(), ceiling.as_bps()); + let follower = spawn_follow(Followed { + inner: inner.clone(), + lower: lower.clone(), + consumer: reservation.consume(), + reservation: Arc::downgrade(&reservation), + ceiling, + target: producer.target, + }); + 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 +420,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 +437,28 @@ 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())) + } + + /// 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) } -} -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 +479,25 @@ 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))?; + 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 { + 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,7 +509,12 @@ 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() { + 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 { @@ -432,11 +522,13 @@ impl VideoProducer { } self.producer.discontinuity()?; } - return self.producer.set_enabled(false); + return Ok(()); } }; 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(); @@ -454,17 +546,26 @@ 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 } - 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 +573,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 +587,33 @@ impl VideoProducer { /// `Unroutable`, which a moq-transport reader sees as `Internal`: its wire has no such code. #[derive(uniffi::Object)] pub struct MoqVideoProducer { - 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>, + /// `None` once finished or aborted. + inner: Shared, 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())) + } + + /// The bitrate the encoder produces at, or 0 while disabled. + #[cfg(test)] + pub(crate) fn applied_bitrate(&self) -> u64 { + match self.inner.lock().unwrap().as_ref().map(|producer| producer.target) { + Some(moq_mux::rate::Target::Enabled(bitrate)) => bitrate.as_bps(), + _ => 0, + } } } @@ -532,19 +621,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 +642,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 +656,19 @@ 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 (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. - 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(()), - } + // 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. + 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); + written.and(stopped) } /// Cut a new group at the next written frame. @@ -624,15 +688,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,27 +715,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.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 @@ -681,7 +725,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 +734,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 } } @@ -806,46 +844,28 @@ 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, + target: moq_mux::rate::Target::Enabled(ceiling), + follow: None, + }))), 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); + producer.follow = Some(Following::start(allocator, lower, producer, &video.inner)); + } + children.adopt(Arc::new(video)) } } @@ -866,15 +886,21 @@ 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>, 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, + /// What the encoder produces when the follower starts. + target: moq_mux::rate::Target, } fn spawn_follow(followed: Followed) -> Follow { @@ -904,11 +930,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 @@ -942,11 +964,13 @@ async fn follow_reservation(followed: Followed) { inner, lower, 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, @@ -956,7 +980,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)); @@ -968,15 +992,19 @@ 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); - 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; } } } @@ -1518,11 +1546,15 @@ mod finish_tests { .unwrap(); // Stand in for a follower thread that panicked. video - .follow + .inner .lock() .unwrap() .as_ref() + .unwrap() + .follow + .as_ref() .expect("published against an allocator") + .follower .failed .store(true, Ordering::Release); @@ -1532,6 +1564,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::new(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(), + 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, + }; + 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)] diff --git a/rs/moq-mux/src/rate.rs b/rs/moq-mux/src/rate.rs index c61d8250dec..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; @@ -445,6 +447,28 @@ 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, + /// 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; + 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; + } + } + 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 +967,40 @@ 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 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(); + 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 applied = Vec::new(); + while let Ok(target) = kio::clock::timeout(Duration::from_secs(60), follower.next()).await { + applied.push(target.unwrap()); + } + 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); + 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)]