From 74bec5afcc763e9535d852f77c43d3e8362690af Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 23:31:22 -0800 Subject: [PATCH 1/7] chore(quest): claim quest/m2/api-shape/native.md Quest-Claim: 5e23596576181b5d From 6d5cb4bc96a6939319460595183e7c45e55d76ff Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 8 Nov 2026 00:36:52 -0800 Subject: [PATCH 2/7] feat(moq-audio,moq-video)!: native media's small surface fixes - Both crates' `encode::publish_capture` returns a `Publication` handle and a `Driver`. Audio's `Control` and its start/stop/replace go. - moq-audio's `encode::Capture::on_demand` is `retain`. - moq-video's `encode::Producer::new` takes the track; `with_track` goes. - `encode::Applied` reports `cut`. - moq-audio's `playback::Sink::{set_volume, volume, peak}` go; use `control()`. - `render::Exporter::new` returns the settled `encode::Config`. Co-Authored-By: Claude Opus 5.5 --- doc/lib/rs/moq-audio.md | 9 +- quest/m2/api-shape/README.md | 1 - quest/m2/api-shape/native.md | 31 - quest/m3/video-publish-grant.md | 7 +- rs/moq-audio/src/capture/fanout.rs | 20 - rs/moq-audio/src/encode/capture.rs | 985 ++++++--------------- rs/moq-audio/src/encode/mod.rs | 2 +- rs/moq-audio/src/level.rs | 3 +- rs/moq-audio/src/lib.rs | 14 +- rs/moq-audio/src/playback/sink.rs | 20 +- rs/moq-audio/tests/integration/playback.rs | 10 +- rs/moq-boy/src/video.rs | 5 +- rs/moq-c/src/video.rs | 10 +- rs/moq-cli/src/publish.rs | 15 +- rs/moq-ffi/src/video.rs | 5 +- rs/moq-video/README.md | 3 +- rs/moq-video/src/capture/wgc.rs | 2 +- rs/moq-video/src/decode/consumer.rs | 5 +- rs/moq-video/src/encode/encoder.rs | 13 +- rs/moq-video/src/encode/mod.rs | 7 +- rs/moq-video/src/encode/producer.rs | 374 ++++++-- rs/moq-video/src/encode/sink.rs | 24 +- rs/moq-video/src/lib.rs | 3 +- rs/moq-video/src/render/export.rs | 31 +- rs/moq-video/src/render/export/dx12.rs | 4 +- rs/moq-video/src/render/export/metal.rs | 4 +- rs/moq-video/src/render/export/vulkan.rs | 4 +- 27 files changed, 630 insertions(+), 981 deletions(-) delete mode 100644 quest/m2/api-shape/native.md diff --git a/doc/lib/rs/moq-audio.md b/doc/lib/rs/moq-audio.md index c53cd28c2b1..747c3ee3e40 100644 --- a/doc/lib/rs/moq-audio.md +++ b/doc/lib/rs/moq-audio.md @@ -37,11 +37,10 @@ without stalling the mix. Without a subscriber the encoder keeps raw PCM up to the track's `max_age`, then encodes the part of that history within the subscribers' max delay before continuing live when someone subscribes. With an unlimited `max_age` it keeps at most 30 seconds -(about 11.5 MB for 48 kHz stereo f32). Set `encode::Capture::on_demand` to false -to encode continuously instead. Stopping an encoder leaves the shared capture running, and the -history it retained stays on the track until it is subscribed or expires; a device failure keeps -it the same way before the encoder returns the error. Its consumer can be swapped without -changing the track subscribers know. Opus packs 20 ms per packet by default, +(about 11.5 MB for 48 kHz stereo f32). Set `encode::Capture::retain` to false +to encode continuously instead. Dropping an encoder leaves the shared capture running. A device +failure keeps the retained history on the track until it is subscribed or expires, then the +encoder returns the error. Opus packs 20 ms per packet by default, or 10 ms with a shorter `frame_duration`. Groups carry at least 20 ms by default, so 10 ms packets pair up. `encode::Options::join` changes that minimum; a join shorter than a packet puts every packet in its own diff --git a/quest/m2/api-shape/README.md b/quest/m2/api-shape/README.md index 1b78418aacc..bb612304b5f 100644 --- a/quest/m2/api-shape/README.md +++ b/quest/m2/api-shape/README.md @@ -58,7 +58,6 @@ Rejected in planning: - [Transports take shared types](/quest/m2/api-shape/transport.md) - moq-uring takes a congestion factory and rustls configs, and the tokio server request stops exposing its QUIC internals - [Crates re-export no whole crate](/quest/m2/api-shape/reexports.md) - no moq crate re-exports a whole crate, moq's own included, so a dependency bump is no moq break, and moq-mux's container `Guard` gets a name - [Media crates' small surface fixes](/quest/m2/api-shape/media.md) - exporters, the container consumer, catalog color range, rate grant, moq-stats, JSON, and MSF each have one path and one name -- [Native media's small surface fixes](/quest/m2/api-shape/native.md) - audio encode internals go private, `on_demand` has one meaning, and video and render setters fold into their constructors - [Apps' small surface fixes](/quest/m2/api-shape/apps.md) - the relay, gateways, moq-hls, moq-cli, and moq-boy drop dead or unreachable surface and gain role names - [JS packages' small surface fixes](/quest/m2/api-shape/js.md) - track info, connection, qmux, watch, publish, ui, video, boy, and auth exports each have one path and one name - [Bindings' small surface fixes](/quest/m2/api-shape/bindings.md) - Go's read cancel, JSON producer construction, and the Python wheel's test module diff --git a/quest/m2/api-shape/native.md b/quest/m2/api-shape/native.md deleted file mode 100644 index dadb658788b..00000000000 --- a/quest/m2/api-shape/native.md +++ /dev/null @@ -1,31 +0,0 @@ -# [M] Native media's small surface fixes - -## Goal - -- `publish_capture` returns a handle plus a driver in both moq-audio and - moq-video, mirroring each other, and moq-audio's `encode::{Control, State, - Status}` stop being separate public names. The handle may start with only - the state accessors audio has today, so - [video-publish-grant](/quest/m3/video-publish-grant.md) adds `grant()` - without a break (added 2026-11-07, maintainer, /quest-plan from the 2026-11-07 API sweep of open PRs and remaining quests). -- `on_demand` means one thing across crates. moq-video's closes the camera - between viewers and defaults to `false`; moq-audio's keeps the device open, - defers encoding, and replays retained PCM, and defaults to `true`. Audio's - gets a name for its role (for example `retain`), and `@moq/publish`'s - audio `capture` mode is checked for the same name. -- moq-video's encode producer folds `with_track` into `new`. -- `encode::Applied` reports `cut`, so callers stop matching - `CutUnsupported` to learn it. -- moq-audio's playback `Sink::{set_volume, volume, peak}` go, since - `Control` has them, and the audio decode `Consumer::max_delay()` duplicate - goes. -- The render `Exporter::new` returns the settled `encode::Config` instead - of rewriting the caller's `&mut` config. - -## Plan - -Found by the 2026-11-07 API shape audit (native media); see the -[epic](/quest/m2/api-shape/README.md). moq-ffi, moq-c, and the wrappers -follow each rename. The device paths are in -[video-devices](/quest/m2/video-devices.md). Check downstream audio -callers of `on_demand` before picking the new name. diff --git a/quest/m3/video-publish-grant.md b/quest/m3/video-publish-grant.md index 66b900999c9..0e9f67b551c 100644 --- a/quest/m3/video-publish-grant.md +++ b/quest/m3/video-publish-grant.md @@ -12,11 +12,12 @@ Decided 2026-11-05 (maintainer, on moq-dev/studio's /quest-plan): m3, since the moq-net already exposes the grant (`bandwidth::Reservation::rate`, also in moq-ffi), but `encode::publish_capture` (`rs/moq-video/src/encode/producer.rs`) -holds the reservation privately in its capture loop and returns nothing. The -only figure a caller sees is the target reported through the catalog stats. +holds the reservation privately in its capture loop, and the `Publication` +handle it returns reports only its lifecycle state. The only figure a caller +sees is the target reported through the catalog stats. Consumer: the studio's encode-control rungs and Health panel ([moq-dev/studio#25](https://github.com/moq-dev/studio/pull/25)). Recommendation: return a handle from the publish call that carries the grant, rather than a callback. -Changed 2026-11-07 (maintainer, /quest-plan from the API sweep of open PRs and remaining quests): [api-shape/native](/quest/m2/api-shape/native.md) makes `publish_capture` return a handle in both crates before the cut, so `grant()` is an additive method on it. +Changed 2026-11-07 (maintainer, /quest-plan from the API sweep of open PRs and remaining quests): `publish_capture` returns an `encode::Publication` handle in both moq-video and moq-audio before the cut, so `grant()` is an additive method on it. diff --git a/rs/moq-audio/src/capture/fanout.rs b/rs/moq-audio/src/capture/fanout.rs index ffc32982be5..adcb2094e6f 100644 --- a/rs/moq-audio/src/capture/fanout.rs +++ b/rs/moq-audio/src/capture/fanout.rs @@ -110,26 +110,6 @@ pub struct Consumer { } impl Consumer { - pub(crate) fn poll_failed(&self, waiter: &kio::Waiter) -> Poll { - ready!(self.state.poll_closed(waiter)); - Poll::Ready( - self.state - .read() - .failure - .clone() - .unwrap_or_else(|| Error::Capture("audio capture stopped".into())), - ) - } - - /// The shared failure, if capture has already ended, without waiting. - pub(crate) fn failure(&self) -> Option { - self.state.read().failure.clone() - } - - pub(crate) fn skip_to_live(&mut self) { - self.next = self.state.read().next; - } - /// The PCM format shared by every consumer of this capture. pub fn input(&self) -> &Input { &self.input diff --git a/rs/moq-audio/src/encode/capture.rs b/rs/moq-audio/src/encode/capture.rs index 4784593ccbd..64176e649b8 100644 --- a/rs/moq-audio/src/encode/capture.rs +++ b/rs/moq-audio/src/encode/capture.rs @@ -1,4 +1,4 @@ -//! Control a capture-backed audio publication. +//! Publish a capture consumer as an encoded audio track. use std::fmt; use std::sync::Arc; @@ -8,22 +8,54 @@ use moq_net::time::Instant; use moq_mux::catalog::hang::CatalogExt; -use super::{Input, Options, Producer}; +use super::{Options, Producer}; use crate::capture; -use crate::resample::{Remix, Resampler}; use crate::{Error, Frame, Level}; +/// Capture and encode settings for [`publish_capture`]. +/// +/// Samples are stamped on the catalog's [`clock`](moq_mux::catalog::Producer::clock), the one +/// its consumers are told about, so a concurrent video capture on the same catalog stays aligned. +/// A microphone buffer is placed at the capture instant of its first sample, mapped through the +/// catalog clock's own correlation. A host that reports no usable capture time, and system audio, +/// are stamped once by the capture producer before fanout. +/// +/// `#[non_exhaustive]`: construct via [`Capture::new`] and set +/// fields, so new publication settings can be added without changing +/// [`publish_capture`]. +#[derive(Clone, Debug)] +#[non_exhaustive] +pub struct Capture { + /// An independent reader from the shared capture producer. + pub capture: capture::Consumer, + /// The track's stable codec and encode settings. + pub encode: Options, + /// Retain raw audio up to the track's `max_age` and encode only while subscribed. + /// + /// On by default. Capture stays open, and a subscriber encodes the retained past before + /// continuing live. Tracks with an unlimited `max_age` keep 30 seconds, the default media max age. + /// Set false to encode continuously instead. + pub retain: bool, +} + +impl Capture { + /// Publish samples from this independent capture consumer. + pub fn new(capture: capture::Consumer) -> Self { + Self { + capture, + encode: Options::default(), + retain: true, + } + } +} + /// The capture publication's current lifecycle state. #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[non_exhaustive] pub enum Status { - /// Encoding was explicitly stopped; the shared capture is independent. - /// - /// Retained history is still encoded on demand, or expires on its own. - Stopped, /// Raw audio is retained until a subscriber needs encoding. Waiting, - /// The publication driver has not started yet. + /// The [`Driver`] has not started yet. Starting, /// Post-processing samples are being encoded and published. Live, @@ -37,11 +69,11 @@ pub enum Status { Ended, } -/// A snapshot of a capture-backed publication's lifecycle. +/// A snapshot of a capture publication's lifecycle. /// /// Levels are deliberately not here: they change every buffer, so they would -/// drown out the transitions [`Control::changed`] exists to report. Read -/// them with [`Control::level`] instead. +/// drown out the transitions [`Publication::changed`] exists to report. Read +/// them with [`Publication::level`] instead. #[derive(Clone, Debug)] pub struct State { status: Status, @@ -74,187 +106,87 @@ impl State { } } -/// Capture and encode settings for [`Control::new`] and [`publish_capture`]. -/// -/// Samples are stamped on the catalog's [`clock`](moq_mux::catalog::Producer::clock), the one -/// its consumers are told about, so a concurrent video capture on the same catalog stays aligned. -/// A microphone buffer is placed at the capture instant of its first sample, mapped through the -/// catalog clock's own correlation. A host that reports no usable capture time, and system audio, -/// are stamped once by the capture producer before fanout. -/// -/// `#[non_exhaustive]`: construct via [`Capture::new`] and set -/// fields, so new publication settings can be added without changing -/// [`Control::new`]. #[derive(Clone, Debug)] -#[non_exhaustive] -pub struct Capture { - /// An independent reader from the shared capture producer. - pub capture: capture::Consumer, - /// The track's stable codec and encode settings. - pub encode: Options, - /// Encode only while subscribed, retaining raw audio up to the track's `max_age`. - /// - /// On by default. Capture stays open, and a subscriber encodes the retained past before - /// continuing live. Tracks with an unlimited `max_age` keep 30 seconds, the default media max age. - /// Set false to encode continuously instead. - pub on_demand: bool, -} - -impl Capture { - /// Publish samples from this independent capture consumer. - pub fn new(capture: capture::Consumer) -> Self { - Self { - capture, - encode: Options::default(), - on_demand: true, - } - } -} - -#[derive(Clone, Debug)] -struct Desired { - capture: capture::Consumer, - enabled: bool, +struct Published { + state: State, revision: u64, } -#[derive(Clone, Debug)] -struct PublishedState { - state: State, - revision: u64, +/// Capture audio and publish it as an encoded MoQ track. +/// +/// Registers the rendition before any samples are read, and returns a handle +/// that observes the publication plus the [`Driver`] that encodes it. +pub fn publish_capture( + mut broadcast: moq_net::broadcast::Producer, + catalog: moq_mux::catalog::Producer, + options: Capture, +) -> Result<(Publication, Driver), Error> { + let clock = catalog.clock(); + let input = options.capture.input().clone(); + let producer = Producer::new(&mut broadcast, catalog, input, &options.encode)?; + let track_name: Arc = producer.track_name().into(); + let reservation = options.encode.bandwidth.reserve(&producer.demand(), producer.bitrate()); + let state = kio::Producer::new(Published { + state: State { + status: Status::Starting, + source: options.capture.source().clone(), + device: options.capture.device().cloned(), + failure: None, + }, + revision: 0, + }); + let level = kio::Producer::new(Level::default()); + let publication = Publication { + state: state.consume(), + level: level.consume(), + observed: 0, + track_name, + }; + let driver = Driver { + _broadcast: broadcast, + _reservation: reservation, + producer: Some(producer), + capture: options.capture, + retain: options.retain, + clock, + state, + level, + }; + Ok((publication, driver)) } -/// A handle for controlling a capture-backed audio publication. +/// A handle that observes a capture publication. /// -/// Clones control the same MoQ track. Starting, stopping, or replacing this -/// reader leaves the shared device and other consumers alone. Dropping the -/// final clone stops encoding and releases the publication. -pub struct Control { - desired: kio::Producer, - state: kio::Consumer, +/// Clones observe the same MoQ track. Dropping it changes nothing: the +/// [`Driver`] owns the publication. +pub struct Publication { + state: kio::Consumer, level: kio::Consumer, observed: u64, track_name: Arc, - /// The track's fixed PCM input, which every replacement must convert into. - input: Input, } -impl Clone for Control { +impl Clone for Publication { fn clone(&self) -> Self { Self { - desired: self.desired.clone(), state: self.state.clone(), level: self.level.clone(), observed: self.state.read().revision, track_name: self.track_name.clone(), - input: self.input.clone(), } } } -impl fmt::Debug for Control { +impl fmt::Debug for Publication { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("Control") + f.debug_struct("Publication") .field("track_name", &self.track_name) .field("state", &self.state.read().state) .finish_non_exhaustive() } } -impl Control { - /// Register a track from an already-open capture and return its controls and driver. - pub fn new( - mut broadcast: moq_net::broadcast::Producer, - catalog: moq_mux::catalog::Producer, - options: Capture, - ) -> Result<(Self, Driver), Error> { - let clock = catalog.clock(); - let input = options.capture.input().clone(); - let producer = Producer::new(&mut broadcast, catalog, input.clone(), &options.encode)?; - let track_name: Arc = producer.track_name().into(); - let reservation = options.encode.bandwidth.reserve(&producer.demand(), producer.bitrate()); - let source = options.capture.source().clone(); - let device = options.capture.device().cloned(); - let initial = PublishedState { - state: State { - status: Status::Starting, - source: source.clone(), - device: device.clone(), - failure: None, - }, - revision: 0, - }; - let desired = kio::Producer::new(Desired { - capture: options.capture, - enabled: true, - revision: 0, - }); - let state = kio::Producer::new(initial); - let level = kio::Producer::new(Level::default()); - let control = Self { - desired: desired.clone(), - state: state.consume(), - level: level.consume(), - observed: 0, - track_name, - input: input.clone(), - }; - let driver = Driver { - _broadcast: broadcast, - _reservation: reservation, - producer: Some(producer), - input, - source, - device, - on_demand: options.on_demand, - clock, - desired: desired.consume(), - state, - level, - }; - Ok((control, driver)) - } - - /// Resume encoding the selected consumer from live capture. - pub fn start(&self) { - let Ok(mut desired) = self.desired.write() else { return }; - if desired.enabled { - return; - } - desired.enabled = true; - desired.revision = desired.revision.wrapping_add(1); - } - - /// Stop this encoder while retaining the MoQ track and shared device. - /// - /// Audio already retained stays on the track: a subscriber still encodes it on - /// demand, and it expires on its own. Nothing is encoded eagerly. - pub fn stop(&self) { - let Ok(mut desired) = self.desired.write() else { return }; - if !desired.enabled { - return; - } - desired.enabled = false; - desired.revision = desired.revision.wrapping_add(1); - } - - /// Replace the selected input without changing the MoQ track identity. - /// - /// The new reader starts at live capture, immediately if started or on the next - /// [`start`](Self::start). A consumer whose layout cannot be converted into the - /// track's fixed input is rejected, leaving the current one selected. A capture - /// failure is terminal, so neither this nor [`start`](Self::start) recovers it; - /// create a new publication instead. - pub fn replace(&self, capture: capture::Consumer) -> Result<(), Error> { - Converter::new(capture.input(), &self.input)?; - let Ok(mut desired) = self.desired.write() else { - return Err(moq_net::Error::Closed.into()); - }; - desired.capture = capture; - desired.revision = desired.revision.wrapping_add(1); - Ok(()) - } - +impl Publication { /// The stable track name registered for this publication. pub fn track_name(&self) -> &str { &self.track_name @@ -293,26 +225,31 @@ impl Control { Some(published.state) } - /// Whether the publication driver has exited. + /// Whether the driver has exited. pub fn is_finished(&self) -> bool { self.state.is_closed() } } +/// Publishing a consumer can be spawned independently of the device's platform affinity. +#[allow(dead_code, reason = "a compile-time `Send` assertion, never called")] +fn assert_driver_send(driver: Driver) { + fn is_send(_: &T) {} + is_send(&driver.run()); +} + /// The task that encodes one consumer without owning its capture device. +/// +/// Dropping it, or its [`run`](Self::run) future, cancels the track and leaves the +/// shared capture and its other consumers running. pub struct Driver { _broadcast: moq_net::broadcast::Producer, _reservation: moq_net::bandwidth::Reservation, producer: Option, - input: Input, - /// The selected consumer's source and device, so a terminal state names the - /// reader that ended rather than a replacement that has not been read yet. - source: capture::Source, - device: Option, - on_demand: bool, + capture: capture::Consumer, + retain: bool, clock: moq_mux::Clock, - desired: kio::Consumer, - state: kio::Producer, + state: kio::Producer, level: kio::Producer, } @@ -333,19 +270,18 @@ impl Drop for Driver { /// Why [`Driver::drive`] stopped without an encoding or publishing error. enum End { - /// The final control dropped or the track ended. + /// The track closed. Closed, /// The source failed, and every retained frame since was served or expired. Failed(Error), } impl Driver { - /// Encode until the final control drops, the track ends, or the source fails. + /// Encode until the track closes or the source fails, then end the track. /// - /// A source failure is terminal, yet retained history stays as reachable as while - /// stopped: subscribers still encode it on demand until it is served or expires. - /// The driver then ends the track with the failure, keeping what it published, and - /// returns it. + /// A source failure is terminal, yet retained history stays reachable: subscribers + /// still encode it on demand until it is served or expires. The driver then ends the + /// track with the failure, keeping what it published, and returns it. pub async fn run(mut self) -> Result<(), Error> { let end = self.drive().await; let mut producer = self.producer.take().unwrap(); @@ -382,159 +318,71 @@ impl Driver { Err(error) } }; - if let Ok(mut level) = self.level.write() { - *level = Level::default(); - } + self.publish_level(Level::default()); let _ = self.state.close(); result } async fn drive(&mut self) -> Result { - let track = self.producer.as_ref().unwrap().track().clone(); + let producer = self.producer.as_mut().unwrap(); + let track = producer.track().clone(); let mut retention = Retention { demand: track.demand(), - on_demand: self.on_demand, + retain: self.retain, + clock: self.clock.clone(), + state: &self.state, + level: &self.level, + failure: None, }; - 'drive: loop { - let desired = self.desired.read().clone(); - self.source = desired.capture.source().clone(); - self.device = desired.capture.device().cloned(); - if !desired.enabled { - self.update(Status::Stopped, None); - if let Ok(mut level) = self.level.write() { - *level = Level::default(); - } - let producer = self.producer.as_mut().unwrap(); - // Retained history outlives the stop: demand still encodes it, and it expires on its own. - retention.refresh(producer, &self.clock)?; - loop { - tokio::select! { - biased; - changed = desired_changed(&self.desired, desired.revision) => { - // A failure that landed first still ends the publication. - if let Some(error) = desired.capture.failure() { return self.failed(error, &mut retention).await; } - if changed.is_none() { return Ok(End::Closed); } - break; - } - _ = track.closed() => return Ok(End::Closed), - error = kio::wait(|waiter| desired.capture.poll_failed(waiter)) => { - return self.failed(error, &mut retention).await; - } - changed = retention.changed(producer, &self.clock) => { changed?; } - } + let reader = &mut self.capture; + retention.refresh(producer)?; + let failure = loop { + tokio::select! { + biased; + _ = track.closed() => return Ok(End::Closed), + samples = reader.read() => { + let samples = match samples { + Ok(samples) => samples, + Err(error) => break error, + }; + if samples.gap { producer.reanchor(); } + if samples.data.is_empty() { continue; } + let timestamp = stamp(&retention.clock, samples.captured)?; + retention.level(Level::measure(&samples.data)); + retention.refresh(producer)?; + producer.write(&frame(&samples.data, timestamp)?)?; } - continue; + changed = retention.changed(producer) => changed?, } - let capture = desired.capture; - let mut converter = Converter::new(capture.input(), &self.input)?; - let mut output = EncoderOutput { - producer: self.producer.as_mut().unwrap(), - retention: &mut retention, - clock: &self.clock, - source: &self.source, - device: self.device.clone(), - state: &self.state, - level: &self.level, - }; - // Capture is immutable metadata plus a cursor; read through an independent clone. - let mut reader = capture.clone(); - if desired.revision != 0 { - reader.skip_to_live(); - } - output.producer.reanchor(); - output.refresh()?; - let failure = loop { - tokio::select! { - biased; - changed = desired_changed(&self.desired, desired.revision) => { - // This reader's stream ends here. Its tail belongs before a stop, which - // can be followed by `finish`; a restart or replacement reanchors anyway. - if let Some((samples, timestamp)) = converter.drain()? { - output.write(&samples, timestamp)?; - } - // A failure that landed first still ends the publication. - if let Some(error) = reader.failure() { break error; } - if changed.is_none() { return Ok(End::Closed); } - continue 'drive; - } - _ = track.closed() => return Ok(End::Closed), - samples = reader.read() => { - let samples = match samples { - Ok(samples) => samples, - Err(error) => { - // The source ended here, so its converted tail belongs on the track. - if let Some((samples, timestamp)) = converter.drain()? { - output.write(&samples, timestamp)?; - } - break error; - } - }; - if samples.gap { output.producer.reanchor(); } - let timestamp = output.stamp(samples.captured)?; - if let Some((samples, timestamp)) = converter.process(samples, timestamp)? { - output.write(&samples, timestamp)?; - } - } - changed = output.changed() => changed?, - } - }; - return self.failed(failure, &mut retention).await; - } - } + }; - /// Keep serving retained history after the source fails, until none is left or - /// nothing can want it any more. Controls can no longer restart or replace the source. - async fn failed(&mut self, error: Error, retention: &mut Retention) -> Result { - self.update(Status::Failed, Some(error.clone())); - if let Ok(mut level) = self.level.write() { - *level = Level::default(); - } - let track = self.producer.as_ref().unwrap().track().clone(); - let producer = self.producer.as_mut().unwrap(); - let mut revision = self.desired.read().revision; - retention.refresh(producer, &self.clock)?; + // The source failed, yet retained history stays reachable: subscribers still encode + // it on demand until none is left or nothing can want it any more. + retention.failed(failure.clone()); + retention.refresh(producer)?; while producer.retains() { tokio::select! { biased; - changed = desired_changed(&self.desired, revision) => match changed { - Some(desired) => revision = desired.revision, - None => break, - }, _ = track.closed() => break, - changed = retention.changed(producer, &self.clock) => { changed?; } + changed = retention.changed(producer) => changed?, } } - Ok(End::Failed(error)) + Ok(End::Failed(failure)) } fn update(&self, status: Status, failure: Option) { - publish_state(&self.state, &self.source, status, self.device.clone(), failure); + publish_state(&self.state, status, failure); } -} -async fn desired_changed(desired: &kio::Consumer, revision: u64) -> Option { - desired - .wait(move |desired| { - if desired.revision != revision { - Poll::Ready((**desired).clone()) - } else { - Poll::Pending - } - }) - .await - .ok() + fn publish_level(&self, level: Level) { + publish_level(&self.level, level); + } } /// Publish a lifecycle transition, skipping one that changes nothing. /// /// Levels and repeated capture states must not wake lifecycle-only observers. -fn publish_state( - state: &kio::Producer, - source: &capture::Source, - status: Status, - device: Option, - failure: Option, -) { +fn publish_state(state: &kio::Producer, status: Status, failure: Option) { let Ok(mut published) = state.write() else { return }; // `Error` is not `PartialEq`, and a failure publish is rare enough that // rendering both to compare them costs nothing worth avoiding. @@ -543,242 +391,98 @@ fn publish_state( (Some(before), Some(now)) => before.to_string() == now.to_string(), _ => false, }; - if same_failure - && published.state.status == status - && published.state.source == *source - && published.state.device == device - { + if same_failure && published.state.status == status { return; } - published.state = State { - status, - source: source.clone(), - device, - failure, - }; + published.state.status = status; + published.state.failure = failure; published.revision = published.revision.wrapping_add(1); } -/// Capture audio and publish it as an encoded MoQ track. -/// -/// This convenience function runs a [`Control`] from -/// `options` without handing back the handle. Use [`Control::new`] directly to -/// retain controls. -pub async fn publish_capture( - broadcast: moq_net::broadcast::Producer, - catalog: moq_mux::catalog::Producer, - options: Capture, -) -> Result<(), Error> { - let (_control, driver) = Control::new(broadcast, catalog, options)?; - driver.run().await +/// Levels ride their own channel: they change every buffer, so folding them +/// into [`State`] would wake every [`Publication::changed`] waiter at the +/// capture rate and rebuild the whole snapshot to do it. +fn publish_level(level: &kio::Producer, now: Level) { + let Ok(mut published) = level.write() else { return }; + if *published != now { + *published = now; + } } -/// Publishing a consumer can be spawned independently of the device's platform affinity. -#[allow(dead_code, reason = "a compile-time `Send` assertion, never called")] -fn assert_publish_capture_send( - broadcast: moq_net::broadcast::Producer, - catalog: moq_mux::catalog::Producer, - options: Capture, -) { - fn is_send(_: &T) {} - is_send(&publish_capture(broadcast, catalog, options)); +/// The capture instant `captured` on `clock`, in microseconds. +fn stamp(clock: &moq_mux::Clock, captured: Instant) -> Result { + // Clamp to the nearest instant the clock can name: a host can stamp a + // chunk after now, or a newly created clock can begin after capture. + let timestamp = match clock.capture(captured) { + Err(moq_mux::Error::InvalidCapture) => { + let now = clock.now(); + let ago = Instant::now().saturating_duration_since(captured).as_micros(); + if ago > now.as_micros() { + moq_net::Timestamp::ZERO + } else { + now + } + } + timestamp => timestamp?, + }; + Ok(timestamp.as_micros() as u64) } /// Encodes retained history while a subscriber wants it, and expires it otherwise. /// -/// Runs whether or not this encoder is reading capture, so history stays reachable -/// after a stop or a source failure. -struct Retention { +/// Runs whether or not the source is still capturing, so history stays reachable +/// after a source failure. Publishes whether the track is live or waiting, until +/// the source fails. +struct Retention<'a> { demand: moq_net::track::Demand, - on_demand: bool, + retain: bool, + clock: moq_mux::Clock, + state: &'a kio::Producer, + level: &'a kio::Producer, + /// The source failure, after which the status stays failed. + failure: Option, } -impl Retention { +impl Retention<'_> { /// Expire retained history, then encode one retained frame if wanted. - /// - /// Returns whether a subscriber wants encoding. - fn refresh(&self, producer: &mut Producer, clock: &moq_mux::Clock) -> Result { - let active = !self.on_demand || self.demand.wanted().is_some(); - producer.set_active(active, clock.now())?; - Ok(active) + fn refresh(&self, producer: &mut Producer) -> Result<(), Error> { + let active = !self.retain || self.demand.wanted().is_some(); + producer.set_active(active, self.clock.now())?; + match &self.failure { + Some(failure) => publish_state(self.state, Status::Failed, Some(failure.clone())), + None => publish_state(self.state, if active { Status::Live } else { Status::Waiting }, None), + } + Ok(()) } /// Wait for a change in demand, the next expiry, or the next retained frame to encode, /// then [`refresh`](Self::refresh). - async fn changed(&mut self, producer: &mut Producer, clock: &moq_mux::Clock) -> Result { + async fn changed(&mut self, producer: &mut Producer) -> Result<(), Error> { if producer.backlog_ready() { // Keep the microphone queue moving between codec frames during a long catch-up. tokio::task::yield_now().await; - return self.refresh(producer, clock); + return self.refresh(producer); } let delay = producer .backlog_deadline() - .map(|at| at.saturating_sub(clock.now().into())); + .map(|at| at.saturating_sub(self.clock.now().into())); tokio::select! { changed = self.demand.wanted_changed() => { changed?; } _ = async { match delay { Some(delay) => kio::clock::sleep(delay).await, None => std::future::pending().await } } => {} } - self.refresh(producer, clock) - } -} - -struct EncoderOutput<'a> { - producer: &'a mut Producer, - retention: &'a mut Retention, - clock: &'a moq_mux::Clock, - /// The source this reader belongs to, so a published state can - /// never name an input this stream isn't actually reading. - source: &'a capture::Source, - state: &'a kio::Producer, - level: &'a kio::Producer, - device: Option, -} - -impl EncoderOutput<'_> { - fn stamp(&self, captured: Instant) -> Result { - // Clamp to the nearest instant the clock can name: a host can stamp a - // chunk after now, or a newly created clock can begin after capture. - let timestamp = match self.clock.capture(captured) { - Err(moq_mux::Error::InvalidCapture) => { - let now = self.clock.now(); - let ago = Instant::now().saturating_duration_since(captured).as_micros(); - if ago > now.as_micros() { - moq_net::Timestamp::ZERO - } else { - now - } - } - timestamp => timestamp?, - }; - Ok(timestamp.as_micros() as u64) - } - - fn write(&mut self, samples: &[f32], timestamp_us: u64) -> Result<(), Error> { - self.publish_level(Level::measure(samples)); - self.refresh()?; - self.producer.write(&frame(samples, timestamp_us)?) - } - - async fn changed(&mut self) -> Result<(), Error> { - let active = self.retention.changed(self.producer, self.clock).await?; - self.publish_status(active); - Ok(()) - } - - fn refresh(&mut self) -> Result<(), Error> { - let active = self.retention.refresh(self.producer, self.clock)?; - self.publish_status(active); - Ok(()) - } - - fn publish_status(&self, active: bool) { - publish_state( - self.state, - self.source, - if active { Status::Live } else { Status::Waiting }, - self.device.clone(), - None, - ); - } - - /// Levels ride their own channel: they change every buffer, so folding them - /// into [`State`] would wake every [`Control::changed`] waiter at the - /// capture rate and rebuild the whole snapshot to do it. - fn publish_level(&self, level: Level) { - let Ok(mut published) = self.level.write() else { return }; - if *published != level { - *published = level; - } - } -} - -/// Converted samples plus the timestamp of the first input they carry. -type Converted = (Arc<[f32]>, u64); - -/// Converts a replacement consumer into the track's fixed PCM layout. -struct Converter { - remix: Option, - resampler: Option, - /// The timestamp of the first input not yet reflected in any output. - anchor_us: Option, - /// The timestamp of the most recent input, for output with no pending anchor. - last_us: u64, -} - -impl Converter { - fn new(input: &Input, output: &Input) -> Result { - let remix = (input.layout != output.layout) - .then(|| Remix::new(input.layout, output.layout)) - .transpose()?; - - let resampler = if input.sample_rate == output.sample_rate { - None - } else { - // Ten milliseconds bounds recovery buffering while giving rubato a - // useful window independent of the device callback size. - let chunk_frames = (input.sample_rate as usize / 100).max(1); - Some(Resampler::new( - input.sample_rate, - output.sample_rate, - input.layout.channels(), - chunk_frames, - )?) - }; - - Ok(Self { - remix, - resampler, - anchor_us: None, - last_us: 0, - }) - } - - fn reset(&mut self) { - if let Some(resampler) = self.resampler.as_mut() { - resampler.reset(); - } - self.anchor_us = None; + self.refresh(producer) } - /// Return converted samples plus the timestamp of the first input buffered - /// into them. The timestamp preserves the anchor when resampling spans reads. - fn process(&mut self, samples: capture::Buffer, timestamp_us: u64) -> Result, Error> { - if samples.gap { - self.reset(); - } - if self.anchor_us.is_none() && !samples.data.is_empty() { - self.anchor_us = Some(timestamp_us); - } - self.last_us = timestamp_us; - - let mut data = samples.data; - if let Some(resampler) = self.resampler.as_mut() { - data = resampler - .process(&data, moq_net::Timestamp::from_micros(timestamp_us)?)? - .into(); - } - self.output(data) - } - - /// Return what the resampler still holds once this reader's stream ends. - fn drain(&mut self) -> Result, Error> { - let Some(resampler) = self.resampler.as_mut() else { - return Ok(None); - }; - let data = resampler.drain()?.into(); - self.output(data) + fn level(&self, level: Level) { + publish_level(self.level, level); } - fn output(&mut self, mut data: Arc<[f32]>) -> Result, Error> { - if let Some(remix) = &self.remix { - data = remix.process(&data).into(); - } - if data.is_empty() { - return Ok(None); - } - Ok(Some((data, self.anchor_us.take().unwrap_or(self.last_us)))) + /// The source failed: the level goes quiet and the status stays failed. + fn failed(&mut self, failure: Error) { + publish_level(self.level, Level::default()); + self.failure = Some(failure); } } @@ -804,18 +508,18 @@ mod tests { struct Fixture { capture: capture::Producer, samples: kio::Queue>, - control: Control, + publication: Publication, driver: Driver, broadcast: moq_net::broadcast::Consumer, catalog: moq_mux::catalog::Producer, epoch: Instant, } - fn fixture(on_demand: bool) -> Fixture { - fixture_codec(on_demand, Codec::Pcm) + fn fixture(retain: bool) -> Fixture { + fixture_codec(retain, Codec::Pcm) } - fn fixture_codec(on_demand: bool, codec: Codec) -> Fixture { + fn fixture_codec(retain: bool, codec: Codec) -> Fixture { let (capture, samples) = capture::test_source(); let mut broadcast = moq_net::broadcast::Info::new().produce(); let consumer = broadcast.consume(); @@ -831,12 +535,12 @@ mod tests { let mut options = Capture::new(capture.consume()); options.encode.track = Some("audio".into()); options.encode.config.codec = codec; - options.on_demand = on_demand; - let (control, driver) = Control::new(broadcast, catalog.clone(), options).unwrap(); + options.retain = retain; + let (publication, driver) = publish_capture(broadcast, catalog.clone(), options).unwrap(); Fixture { capture, samples, - control, + publication, driver, broadcast: consumer, catalog, @@ -860,9 +564,16 @@ mod tests { } } - async fn status(control: &mut Control, expected: Status) { - while control.state().status() != expected { - control.changed().await.expect("driver still alive"); + async fn status(publication: &mut Publication, expected: Status) { + while publication.state().status() != expected { + publication.changed().await.expect("driver still alive"); + } + } + + /// Wait until the driver has read and retained a buffer, which publishes its level. + async fn retained(publication: &Publication) { + while publication.level() == Level::default() { + tokio::task::yield_now().await; } } @@ -904,8 +615,8 @@ mod tests { // Audio retains its PCM until wanted, the capture default. let mut options = Capture::new(capture.consume()); options.encode.track = Some("audio".into()); - assert!(options.on_demand, "on demand by default"); - let (_control, driver) = Control::new(broadcast.clone(), catalog.clone(), options).unwrap(); + assert!(options.retain, "retained by default"); + let (_publication, driver) = publish_capture(broadcast.clone(), catalog.clone(), options).unwrap(); let capture_task = tokio::task::spawn_local(capture.run()); let encode_task = tokio::spawn(driver.run()); @@ -971,11 +682,11 @@ mod tests { let Fixture { capture, samples, - control, driver, broadcast, catalog: _catalog, epoch, + .. } = fixture(false); let mut observer = capture.consume(); let (bus, mut bus_driver) = Bus::new(BusConfig::default()).unwrap(); @@ -1043,8 +754,8 @@ mod tests { samples.try_push(Ok(block)).unwrap(); observer.read().await.unwrap(); assert_eq!(packet(&mut track).await.timestamp.as_micros(), 240_000); - drop(control); - encode_task.await.unwrap().unwrap(); + encode_task.abort(); + assert!(encode_task.await.unwrap_err().is_cancelled()); // The capture and independent observer remain live after the encoder goes away too. samples.try_push(Ok(Samples::plain(vec![0.75; 1920], false))).unwrap(); assert_eq!(observer.read().await.unwrap().data[0], 0.75); @@ -1062,7 +773,7 @@ mod tests { let Fixture { capture, samples, - control, + publication, driver, broadcast: _, catalog: _catalog, @@ -1082,73 +793,24 @@ mod tests { ] { assert!(matches!(result, Err(Error::Capture(message)) if message == "device lost")); } - assert_eq!(control.state().status(), Status::Failed); - assert!(matches!(control.state().failure(), Some(Error::Capture(message)) if message == "device lost")); - assert!(control.is_finished()); - }) - .await; - } - - #[tokio::test(start_paused = true)] - async fn subscriber_demand_retains_history_and_stop_only_stops_this_encoder() { - tokio::task::LocalSet::new() - .run_until(async { - let Fixture { - capture, - samples, - mut control, - driver, - broadcast, - catalog: _catalog, - epoch, - } = fixture(true); - let mut observer = capture.consume(); - let capture_task = tokio::task::spawn_local(capture.run()); - let encode_task = tokio::spawn(driver.run()); - status(&mut control, Status::Waiting).await; - let captured = epoch + Duration::from_millis(100); - let mut block = Samples::plain(vec![0.25; 1920], false); - block.captured = Some(captured); - samples.try_push(Ok(block)).unwrap(); - observer.read().await.unwrap(); - tokio::task::yield_now().await; - let mut track = subscribe(&broadcast, Duration::from_secs(30)).await; - let encoded = packet(&mut track).await; - assert_eq!(encoded.timestamp.as_micros(), 100_000); - control.stop(); - status(&mut control, Status::Stopped).await; - samples.try_push(Ok(Samples::plain(vec![0.5; 1920], false))).unwrap(); - assert_eq!(observer.read().await.unwrap().data[0], 0.5); - assert_eq!(control.level(), Level::default()); - control.start(); - status(&mut control, Status::Live).await; - tokio::time::advance(Duration::from_millis(40)).await; - samples.try_push(Ok(Samples::plain(vec![0.75; 1920], false))).unwrap(); - observer.read().await.unwrap(); - assert_eq!(packet(&mut track).await.timestamp.as_micros(), 1_040_000); - drop(control); - encode_task.await.unwrap().unwrap(); - capture_task.abort(); - assert!(capture_task.await.unwrap_err().is_cancelled()); + assert_eq!(publication.state().status(), Status::Failed); + assert!( + matches!(publication.state().failure(), Some(Error::Capture(message)) if message == "device lost") + ); + assert!(publication.is_finished()); }) .await; } - /// Wait until the driver has read and retained a buffer, which publishes its level. - async fn retained(control: &Control) { - while control.level() == Level::default() { - tokio::task::yield_now().await; - } - } #[tokio::test(start_paused = true)] - async fn history_retained_before_a_stop_is_encoded_on_demand_or_expires() { + async fn retained_history_is_encoded_on_demand_or_expires() { tokio::task::LocalSet::new() .run_until(async { for expire in [false, true] { let Fixture { capture, samples, - mut control, + mut publication, driver, broadcast, catalog: _catalog, @@ -1157,33 +819,28 @@ mod tests { let mut observer = capture.consume(); let capture_task = tokio::task::spawn_local(capture.run()); let encode_task = tokio::spawn(driver.run()); - status(&mut control, Status::Waiting).await; + status(&mut publication, Status::Waiting).await; let mut block = Samples::plain(vec![0.25; 1920], false); block.captured = Some(epoch + Duration::from_millis(100)); samples.try_push(Ok(block)).unwrap(); observer.read().await.unwrap(); - retained(&control).await; - control.stop(); - status(&mut control, Status::Stopped).await; + retained(&publication).await; if expire { // Past the track's 30 s max_age. tokio::time::advance(Duration::from_secs(31)).await; } let mut track = subscribe(&broadcast, Duration::from_secs(30)).await; if expire { - // Only fresh capture after a restart reaches the subscriber. - control.start(); - status(&mut control, Status::Live).await; - samples.try_push(Ok(Samples::plain(vec![0.5; 1920], false))).unwrap(); + // Only fresh capture, resuming after the discontinuity, reaches the subscriber. + samples.try_push(Ok(Samples::plain(vec![0.5; 1920], true))).unwrap(); observer.read().await.unwrap(); assert!(packet(&mut track).await.timestamp.as_micros() > 30_000_000); } else { - // Served while still stopped, without restarting capture. assert_eq!(packet(&mut track).await.timestamp.as_micros(), 100_000); - assert_eq!(control.state().status(), Status::Stopped); + assert_eq!(publication.state().status(), Status::Live); } - drop(control); - encode_task.await.unwrap().unwrap(); + encode_task.abort(); + assert!(encode_task.await.unwrap_err().is_cancelled()); capture_task.abort(); assert!(capture_task.await.unwrap_err().is_cancelled()); } @@ -1195,11 +852,11 @@ mod tests { async fn a_failed_source_serves_retained_history_then_ends_with_the_failure() { tokio::task::LocalSet::new() .run_until(async { - for (stop_first, subscribe_late) in [(false, true), (true, true), (false, false)] { + for subscribe_late in [true, false] { let Fixture { capture, samples, - mut control, + mut publication, driver, broadcast, catalog: _catalog, @@ -1208,24 +865,19 @@ mod tests { let mut observer = capture.consume(); let capture_task = tokio::task::spawn_local(capture.run()); let encode_task = tokio::spawn(driver.run()); - status(&mut control, Status::Waiting).await; + status(&mut publication, Status::Waiting).await; let mut block = Samples::plain(vec![0.25; 1920], false); block.captured = Some(epoch + Duration::from_millis(100)); samples.try_push(Ok(block)).unwrap(); observer.read().await.unwrap(); - retained(&control).await; - if stop_first { - control.stop(); - status(&mut control, Status::Stopped).await; - } + retained(&publication).await; samples.try_push(Err(Error::Capture("device lost".into()))).unwrap(); - status(&mut control, Status::Failed).await; + status(&mut publication, Status::Failed).await; assert!( - matches!(control.state().failure(), Some(Error::Capture(message)) if message == "device lost") + matches!(publication.state().failure(), Some(Error::Capture(message)) if message == "device lost") ); // Failure is terminal, but the publication still serves what it retained. - assert!(!control.is_finished()); - control.start(); + assert!(!publication.is_finished()); let track = if subscribe_late { let mut track = subscribe(&broadcast, Duration::from_secs(30)).await; assert_eq!(packet(&mut track).await.timestamp.as_micros(), 100_000); @@ -1236,8 +888,8 @@ mod tests { // Once served, or expired unwanted, the history is gone and the driver reports the failure. let result = encode_task.await.unwrap(); assert!(matches!(result, Err(Error::Capture(message)) if message == "device lost")); - assert!(control.is_finished()); - assert_eq!(control.state().status(), Status::Failed); + assert!(publication.is_finished()); + assert_eq!(publication.state().status(), Status::Failed); if let Some(mut track) = track { // The track keeps what it published, then ends with the failure rather than cleanly. let error = loop { @@ -1255,127 +907,6 @@ mod tests { .await; } - #[tokio::test(start_paused = true)] - async fn replace_rejects_a_consumer_the_track_cannot_convert() { - tokio::task::LocalSet::new() - .run_until(async { - let Fixture { - capture, - samples, - control, - driver, - broadcast, - catalog: _catalog, - epoch, - } = fixture(false); - let (array, _array_samples) = capture::test_source_with(48_000, 3); - assert!(control.replace(array.consume()).is_err()); - let mut observer = capture.consume(); - let capture_task = tokio::task::spawn_local(capture.run()); - let encode_task = tokio::spawn(driver.run()); - let mut track = subscribe(&broadcast, Duration::ZERO).await; - // The original consumer is still selected and encoding. - let mut block = Samples::plain(vec![0.25; 1920], false); - block.captured = Some(epoch + Duration::from_millis(100)); - samples.try_push(Ok(block)).unwrap(); - observer.read().await.unwrap(); - assert_eq!(packet(&mut track).await.timestamp.as_micros(), 100_000); - drop(control); - encode_task.await.unwrap().unwrap(); - capture_task.abort(); - assert!(capture_task.await.unwrap_err().is_cancelled()); - }) - .await; - } - - #[tokio::test(start_paused = true)] - async fn a_resampled_replacement_is_drained_when_the_track_finishes() { - tokio::task::LocalSet::new() - .run_until(async { - // Finish directly from live, and after a stop that leaves the producer anchored. - for stop_first in [false, true] { - let Fixture { - capture: _original, - mut control, - driver, - broadcast, - catalog: _catalog, - epoch, - .. - } = fixture(false); - let (replacement, samples) = capture::test_source_with(44_100, 1); - control.replace(replacement.consume()).unwrap(); - let mut observer = replacement.consume(); - let capture_task = tokio::task::spawn_local(replacement.run()); - let encode_task = tokio::spawn(driver.run()); - let mut track = subscribe(&broadcast, Duration::ZERO).await; - // The replacement starts at live capture, so let the driver select it first. - status(&mut control, Status::Live).await; - // Ten whole 10 ms resampler chunks plus a partial one. - let frames = 4_410 + 200; - let mut block = Samples::plain(vec![0.25; frames], false); - block.captured = Some(epoch + Duration::from_millis(100)); - samples.try_push(Ok(block)).unwrap(); - observer.read().await.unwrap(); - packet(&mut track).await; - if stop_first { - control.stop(); - status(&mut control, Status::Stopped).await; - } - drop(control); - encode_task.await.unwrap().unwrap(); - // A live reader may skip to the newest group, and its end can precede its last frame. - let (mut peak, mut end) = (0.0f32, None); - while let Some(read) = track.read().await.unwrap() { - match read { - Read::End { end: at, .. } => end = Some(at), - Read::Frame { frame, .. } => { - let pcm = crate::Format::F32.as_interleaved_f32(&frame.payload, 2).unwrap(); - peak = pcm.iter().fold(peak, |peak, sample| peak.max(sample.abs())); - } - _ => {} - } - } - let end = end.expect("the track finished without its end"); - // The track ends where its source did, including the partial chunk and filter tail. - let resampled = (frames * 48_000 + 22_050) / 44_100; - assert_eq!(end.as_micros(), 100_000 + (resampled * 1_000_000 / 48_000) as u128); - // The tail is the source's audio, not padding; filter ringing rules out an exact match. - assert!(peak > 0.05, "tail peak {peak}"); - capture_task.abort(); - assert!(capture_task.await.unwrap_err().is_cancelled()); - } - }) - .await; - } - - #[tokio::test(start_paused = true)] - async fn a_failure_before_a_replacement_still_ends_the_publication() { - tokio::task::LocalSet::new() - .run_until(async { - let Fixture { - capture, - mut control, - driver, - catalog: _catalog, - .. - } = fixture(false); - let (replacement, _samples) = capture::test_source(); - let encode_task = tokio::spawn(driver.run()); - status(&mut control, Status::Live).await; - // Both land before the driver runs again; the failure came first. - drop(capture); - control.replace(replacement.consume()).unwrap(); - let result = kio::clock::timeout(Duration::from_secs(1), encode_task) - .await - .expect("the failed capture ends the driver") - .unwrap(); - assert!(matches!(result, Err(Error::Capture(_)))); - assert_eq!(control.state().status(), Status::Failed); - }) - .await; - } - #[tokio::test(start_paused = true)] async fn dropping_an_unfinished_driver_cancels_its_track() { let Fixture { driver, broadcast, .. } = fixture(false); @@ -1393,11 +924,11 @@ mod tests { let Fixture { capture, samples, - control, driver, broadcast, catalog: _catalog, epoch, + .. } = fixture_codec(false, Codec::Opus); let mut observer = capture.consume(); let capture_task = tokio::task::spawn_local(capture.run()); @@ -1422,8 +953,8 @@ mod tests { assert!(observer.read().await.unwrap().gap); let next = packet(&mut track).await; assert_eq!(next.timestamp.as_micros(), 1_000_000); - drop(control); - encode_task.await.unwrap().unwrap(); + encode_task.abort(); + assert!(encode_task.await.unwrap_err().is_cancelled()); capture_task.abort(); assert!(capture_task.await.unwrap_err().is_cancelled()); } diff --git a/rs/moq-audio/src/encode/mod.rs b/rs/moq-audio/src/encode/mod.rs index 8e72d3f5058..b4342eb408d 100644 --- a/rs/moq-audio/src/encode/mod.rs +++ b/rs/moq-audio/src/encode/mod.rs @@ -31,4 +31,4 @@ pub use encoder::{Codec, Config, Encoder, Finish, Input, Kind}; pub use producer::{Options, Producer}; #[cfg(feature = "capture")] -pub use capture::{Capture, Control, Driver, State, Status, publish_capture}; +pub use capture::{Capture, Driver, Publication, State, Status, publish_capture}; diff --git a/rs/moq-audio/src/level.rs b/rs/moq-audio/src/level.rs index f9797fc4afc..b72f3bac82d 100644 --- a/rs/moq-audio/src/level.rs +++ b/rs/moq-audio/src/level.rs @@ -1,7 +1,6 @@ /// How loud a stretch of audio was, for drawing a meter. /// -/// Reported by a capture `encode::Control` and by a mixing -/// bus and its strips. Read it at whatever rate the meter draws at. +/// Reported by a capture `encode::Publication` and by a mixing bus and its strips. Read it at whatever rate the meter draws at. #[derive(Clone, Copy, Debug, Default, PartialEq)] pub struct Level { rms: f32, diff --git a/rs/moq-audio/src/lib.rs b/rs/moq-audio/src/lib.rs index a4c627d0f17..86f72851803 100644 --- a/rs/moq-audio/src/lib.rs +++ b/rs/moq-audio/src/lib.rs @@ -16,14 +16,12 @@ //! they don't exist in a default build. //! - [`encode`] encodes PCM and publishes it through `moq_mux::container`, //! registering the rendition in the `hang` catalog. Two entry points: -//! - `encode::Control::new` takes a capture consumer and returns a `Control` -//! and `Driver`. The handle starts, stops, and replaces this track's reader, -//! preserving track identity without controlling the shared device. -//! - `encode::publish_capture` is the turnkey shorthand for an already-open -//! capture consumer. Its rendition is registered before processing samples. -//! Capture keeps running while raw audio -//! waits for a subscriber, then encodes that history and continues live. -//! Set `encode::Capture::on_demand` to false to encode continuously instead. +//! - `encode::publish_capture` publishes an already-open capture consumer, +//! returning a `Publication` that reports its state and level, and the +//! `Driver` that encodes. Its rendition is registered before processing +//! samples. Capture keeps running while raw audio waits for a subscriber, +//! then encodes that history and continues live. Set +//! `encode::Capture::retain` to false to encode continuously instead. //! - [`encode::Producer`] publishes PCM you hand it. //! - [`decode`] subscribes to an encoded track and decodes it back to PCM. //! [`decode::Consumer`] is the mirror of [`encode::Producer`]. It reads AAC-LC diff --git a/rs/moq-audio/src/playback/sink.rs b/rs/moq-audio/src/playback/sink.rs index 87b8b2906bf..b46b81766c9 100644 --- a/rs/moq-audio/src/playback/sink.rs +++ b/rs/moq-audio/src/playback/sink.rs @@ -277,27 +277,11 @@ impl Sink { &self.input } - /// A handle for adjusting this sink from another thread, e.g. a volume - /// slider on a UI thread while a decode task does the writing. + /// This sink's volume and level, as a handle a UI thread can hold while a + /// decode task does the writing. pub fn control(&self) -> Control { self.control.clone() } - - /// Set the playback volume, `0.0` to `1.0`. See [`Control::set_volume`]. - pub fn set_volume(&self, volume: f32) { - self.control.set_volume(volume); - } - - /// The volume last set. Defaults to `1.0`. - pub fn volume(&self) -> f32 { - self.control.volume() - } - - /// The loudest sample this sink contributed since the last call. See - /// [`Control::peak`]. - pub fn peak(&self) -> f32 { - self.control.peak() - } } impl Drop for Sink { diff --git a/rs/moq-audio/tests/integration/playback.rs b/rs/moq-audio/tests/integration/playback.rs index 3050cb1bf52..5a30a08a405 100644 --- a/rs/moq-audio/tests/integration/playback.rs +++ b/rs/moq-audio/tests/integration/playback.rs @@ -120,7 +120,7 @@ async fn plays_a_tone_through_the_default_device() { let _ = sink.write(&tone).expect("write"); kio::clock::sleep(Duration::from_millis(100)).await; - peaked |= sink.peak() > 0.4; + peaked |= sink.control().peak() > 0.4; // The device is consuming if the queue stays near the target latency // rather than growing by a full chunk every write. drained |= sink.buffered() < Duration::from_millis(250); @@ -166,7 +166,7 @@ async fn mixes_two_sinks_and_honors_volume() { let mut loud = engine.sink(input()).expect("a sink"); let mut quiet = engine.sink(input()).expect("a second sink"); - quiet.set_volume(0.0); + quiet.control().set_volume(0.0); let tone = tone(0.1); let mut loud_peak: f32 = 0.0; @@ -176,8 +176,8 @@ async fn mixes_two_sinks_and_honors_volume() { let _ = loud.write(&tone).expect("write"); let _ = quiet.write(&tone).expect("write"); kio::clock::sleep(Duration::from_millis(100)).await; - loud_peak = loud_peak.max(loud.peak()); - quiet_peak = quiet_peak.max(quiet.peak()); + loud_peak = loud_peak.max(loud.control().peak()); + quiet_peak = quiet_peak.max(quiet.control().peak()); } assert!(loud_peak > 0.4, "the audible sink never registered: {loud_peak}"); @@ -243,7 +243,7 @@ async fn sink_outlives_its_engine() { for _ in 0..5 { let _ = sink.write(&tone).expect("write"); kio::clock::sleep(Duration::from_millis(100)).await; - peaked |= sink.peak() > 0.4; + peaked |= sink.control().peak() > 0.4; } assert!(peaked, "playback stopped when the engine was dropped"); diff --git a/rs/moq-boy/src/video.rs b/rs/moq-boy/src/video.rs index 93328f1fc16..87fa1b0efd7 100644 --- a/rs/moq-boy/src/video.rs +++ b/rs/moq-boy/src/video.rs @@ -58,8 +58,11 @@ impl VideoEncoder { // viewer can discover (and unpause) a moq-boy that has published nothing. let rendition = config.probe().await.expect("failed to probe the H.264 encoder"); let reserved = catalog.reserve().with_interval(config.effective_join()); + let track = broadcast + .unique_track(".avc3", reserved.track_info(hang::catalog::PRIORITY.video)) + .expect("failed to create the avc3 track"); let producer = - moq_video::encode::Producer::new(broadcast, reserved, rendition).expect("failed to create avc3 producer"); + moq_video::encode::Producer::new(track, reserved, rendition).expect("failed to create avc3 producer"); let demand = producer.demand(); let force_keyframe = Arc::new(AtomicBool::new(false)); diff --git a/rs/moq-c/src/video.rs b/rs/moq-c/src/video.rs index 6b44b18a3d1..8a23f9c441d 100644 --- a/rs/moq-c/src/video.rs +++ b/rs/moq-c/src/video.rs @@ -739,8 +739,7 @@ impl Video { } None => (None, hang::catalog::PRIORITY.video), }; - // The suffix `moq_video::encode::Producer::new` would derive, built here so the - // track can take this rendition's priority. + // The track's suffix by codec, the one `moq_video::encode::publish_capture` uses too. let suffix = match &rendition.codec { hang::catalog::VideoCodec::H264(_) => ".avc3", hang::catalog::VideoCodec::H265(_) => ".hev1", @@ -751,7 +750,7 @@ impl Video { } }; let track = target.producer.unique_track(suffix, reserved.track_info(priority))?; - let producer = moq_video::encode::Producer::with_track(track, reserved, rendition)?; + let producer = moq_video::encode::Producer::new(track, reserved, rendition)?; let encoder = Shared::new(VideoEncoder { encoder: Some(encoder), producer, @@ -1393,7 +1392,10 @@ mod tests { let encode = moq_video::encode::Config::new(320, 240, moq_video::FrameRate::new(30, 1).unwrap()); let rendition = encode.probe().await.unwrap(); let reserved = catalog.reserve().with_interval(encode.effective_join()); - let producer = moq_video::encode::Producer::new(broadcast, reserved, rendition).unwrap(); + let track = broadcast + .unique_track(".avc3", reserved.track_info(hang::catalog::PRIORITY.video)) + .unwrap(); + let producer = moq_video::encode::Producer::new(track, reserved, rendition).unwrap(); let name = producer.demand().name().to_string(); let track = consumer.track(&name).unwrap().subscribe(None).await.unwrap(); diff --git a/rs/moq-cli/src/publish.rs b/rs/moq-cli/src/publish.rs index 9ac4a09799d..c3cce29bf04 100644 --- a/rs/moq-cli/src/publish.rs +++ b/rs/moq-cli/src/publish.rs @@ -678,21 +678,24 @@ impl Publish { let catalog = catalog.clone(); async move { match video { - Some(options) => moq_video::encode::publish_capture(broadcast, catalog, options) - .await - .map_err(anyhow::Error::from), + Some(options) => { + let (_publication, driver) = + moq_video::encode::publish_capture(broadcast, catalog, options)?; + driver.run().await.map_err(anyhow::Error::from) + } None => Ok(()), } } }; let audio_fut = async move { match audio { - Some((config, encode, on_demand)) => { + Some((config, encode, retain)) => { let capture = moq_audio::capture::Producer::open(config).await?; let mut options = moq_audio::encode::Capture::new(capture.consume()); options.encode = encode; - options.on_demand = on_demand; - let (_control, driver) = moq_audio::encode::Control::new(broadcast, catalog, options)?; + options.retain = retain; + let (_publication, driver) = + moq_audio::encode::publish_capture(broadcast, catalog, options)?; let mut publish = std::pin::pin!(driver.run()); tokio::select! { result = capture.run() => { diff --git a/rs/moq-ffi/src/video.rs b/rs/moq-ffi/src/video.rs index 2a6945dd17a..29ab3e91e77 100644 --- a/rs/moq-ffi/src/video.rs +++ b/rs/moq-ffi/src/video.rs @@ -782,8 +782,7 @@ impl MoqVideoProducer { ), }); } - // The suffix `moq_video::encode::Producer::new` would derive, built here so the - // track can take this rendition's priority. + // The track's suffix by codec, the one `moq_video::encode::publish_capture` uses too. let suffix = match &rendition.codec { hang::catalog::VideoCodec::H264(_) => ".avc3", hang::catalog::VideoCodec::H265(_) => ".hev1", @@ -800,7 +799,7 @@ impl MoqVideoProducer { Some(name) => state.broadcast.create_track(name, info)?, None => state.broadcast.unique_track(suffix, info)?, }; - Ok(moq_video::encode::Producer::with_track( + Ok(moq_video::encode::Producer::new( track, state.catalog.reserve().with_interval(join), rendition, diff --git a/rs/moq-video/README.md b/rs/moq-video/README.md index 043c5403aed..8342493f69e 100644 --- a/rs/moq-video/README.md +++ b/rs/moq-video/README.md @@ -127,7 +127,8 @@ way, awaited. Public entry points: -- `encode::publish_capture(...)` captures a webcam, encodes it, and publishes it: +- `encode::publish_capture(...)` captures a webcam, encodes it, and publishes it, + returning a `Publication` that reports its state and the `Driver` to run: the track and catalog are advertised up front, and the camera stays open whether or not anyone is watching. With `Capture::on_demand`, the camera opens only while a subscriber is watching and is released when the last one leaves. diff --git a/rs/moq-video/src/capture/wgc.rs b/rs/moq-video/src/capture/wgc.rs index 2961f7a36f0..aeb1c92bdfa 100644 --- a/rs/moq-video/src/capture/wgc.rs +++ b/rs/moq-video/src/capture/wgc.rs @@ -319,7 +319,7 @@ mod tests { let container = moq_mux::catalog::hang::Container::try_from(&rendition).unwrap(); let mut subscriber = moq_mux::container::Consumer::new(subscriber, container); let reserved = catalog.reserve().with_interval(config.effective_join()); - let mut producer = Producer::with_track(track, reserved, rendition).unwrap(); + let mut producer = Producer::new(track, reserved, rendition).unwrap(); assert!(producer.demand().wanted().is_some()); // Only one captured picture arrives; more than three intervals of paced diff --git a/rs/moq-video/src/decode/consumer.rs b/rs/moq-video/src/decode/consumer.rs index 7b81c5566d7..5785c8b2ca4 100644 --- a/rs/moq-video/src/decode/consumer.rs +++ b/rs/moq-video/src/decode/consumer.rs @@ -488,7 +488,10 @@ mod tests { }; let rendition = config.probe().await.unwrap(); let reserved = source_catalog.reserve().with_interval(config.effective_join()); - let mut producer = EncodeProducer::new(source_broadcast, reserved, rendition).unwrap(); + let track = source_broadcast + .unique_track(".avc3", reserved.track_info(hang::catalog::PRIORITY.video)) + .unwrap(); + let mut producer = EncodeProducer::new(track, reserved, rendition).unwrap(); let mut encoder = Encoder::new(&config).unwrap(); let rgba = vec![0x80u8; 320 * 240 * 4]; for index in 0..2 { diff --git a/rs/moq-video/src/encode/encoder.rs b/rs/moq-video/src/encode/encoder.rs index c092a502b46..8ec8e8ae04e 100644 --- a/rs/moq-video/src/encode/encoder.rs +++ b/rs/moq-video/src/encode/encoder.rs @@ -87,6 +87,9 @@ pub struct Applied { /// The backend controls that took effect, for display, e.g. /// `"p1, low-latency tuning, no B-frames, CBR, 1-frame VBV"`. Not a parse target. pub controls: String, + /// Whether [`Encoder::cut`] can force a group boundary; one that cannot (a V4L2 + /// driver without the force-keyframe control) refuses every cut. + pub cut: bool, } impl Applied { @@ -115,6 +118,8 @@ impl Applied { Self { preset: Some(preset), controls: controls.into(), + // The encoder reports `cut` from the backend's `can_cut`. + cut: false, } } @@ -129,6 +134,7 @@ impl Applied { Self { preset: None, controls: controls.into(), + cut: false, } } } @@ -389,9 +395,12 @@ impl Encoder { } /// The latency and compression controls the backend applied for - /// [`Config::preset`], fixed for the session's lifetime. + /// [`Config::preset`], and whether it cuts, fixed for the session's lifetime. pub fn applied(&self) -> Applied { - self.backend.applied() + Applied { + cut: self.backend.can_cut(), + ..self.backend.applied() + } } /// The resolution this encoder emits; external Vulkan inputs may be larger. diff --git a/rs/moq-video/src/encode/mod.rs b/rs/moq-video/src/encode/mod.rs index 3fe08e2fe47..a7d9c1eb8a8 100644 --- a/rs/moq-video/src/encode/mod.rs +++ b/rs/moq-video/src/encode/mod.rs @@ -4,8 +4,9 @@ //! for which backends cover each on this platform. //! //! Entry points, high to low level: -//! - `publish_capture` captures and publishes a webcam (turnkey). Requires the -//! `capture` feature. +//! - `publish_capture` captures and publishes a webcam (turnkey), returning a +//! `Publication` that reports its state and the `Driver` that runs it. +//! Requires the `capture` feature. //! - [`Encoder`] encodes raw [`Frame`](crate::Frame)s you supply into //! [`Encoded`] access units, and [`Producer`] publishes those (bring your own //! frames). Build both for the same [`Codec`]. @@ -47,7 +48,7 @@ pub use encoded::Encoded; pub use encoder::{Applied, Codec, Config, Encoder, Kind, Preset}; pub use producer::Producer; #[cfg(feature = "capture")] -pub use producer::{Capture, publish_capture}; +pub use producer::{Capture, Driver, Publication, State, Status, publish_capture}; pub use sink::Sink; #[cfg(test)] diff --git a/rs/moq-video/src/encode/producer.rs b/rs/moq-video/src/encode/producer.rs index fb21f0dd466..a229b5aa0ce 100644 --- a/rs/moq-video/src/encode/producer.rs +++ b/rs/moq-video/src/encode/producer.rs @@ -94,8 +94,8 @@ pub struct Producer { } impl Producer { - /// Publish a track carrying `rendition` into `broadcast`, registering it under - /// `reserved`, a [`catalog::Producer::reserve`](moq_mux::catalog::Producer::reserve) + /// Publish `rendition` on `track`, registering it under `reserved`, a + /// [`catalog::Producer::reserve`](moq_mux::catalog::Producer::reserve) /// that carries any per-track hint such as the encoder's GOP duration /// ([`Reserved::with_interval`](moq_mux::catalog::Reserved::with_interval)). /// The frames fed to [`publish`](Self::publish) must be in that @@ -109,28 +109,6 @@ impl Producer { /// already says what the first keyframe says, that keyframe confirms the /// catalog instead of correcting it. pub fn new( - broadcast: moq_net::broadcast::Producer, - reserved: moq_mux::catalog::Reserved, - rendition: hang::catalog::VideoConfig, - ) -> Result { - let suffix = match &rendition.codec { - hang::catalog::VideoCodec::H264(_) => ".avc3", - hang::catalog::VideoCodec::H265(_) => ".hev1", - other => { - return Err(Error::Codec(anyhow::anyhow!( - "{other} is not a codec this producer can publish" - ))); - } - }; - let track = broadcast.unique_track(suffix, reserved.track_info(hang::catalog::PRIORITY.video))?; - Self::with_track(track, reserved, rendition) - } - - /// Publish `rendition` on an existing track, registering it under `reserved`. - /// - /// Use this when the caller owns the track name. [`new`](Self::new) derives a - /// unique name from the codec instead. - pub fn with_track( track: moq_net::track::Producer, reserved: moq_mux::catalog::Reserved, rendition: hang::catalog::VideoConfig, @@ -336,25 +314,211 @@ pub struct Capture { pub on_demand: bool, } +/// The capture publication's current lifecycle state. +#[cfg(feature = "capture")] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[non_exhaustive] +pub enum Status { + /// On demand, the source is closed until a subscriber wants the track. + Waiting, + /// The source and its encoder are opening. + Starting, + /// Frames are being captured, encoded, and published. + Live, + /// The capture, encoder, or track failed; [`Driver::run`] returned the error. + Failed, + /// The MoQ track ended, so the driver has stopped. + Ended, +} + +/// A snapshot of a capture publication's lifecycle. +#[cfg(feature = "capture")] +#[derive(Clone, Debug)] +pub struct State { + status: Status, +} + +#[cfg(feature = "capture")] +impl State { + /// The publication's current lifecycle state. + pub fn status(&self) -> Status { + self.status + } +} + +#[cfg(feature = "capture")] +#[derive(Clone, Debug)] +struct Published { + state: State, + revision: u64, +} + +/// A new publication's state, before its driver runs. +#[cfg(feature = "capture")] +fn starting() -> kio::Producer { + kio::Producer::new(Published { + state: State { + status: Status::Starting, + }, + revision: 0, + }) +} + +/// Publish a lifecycle transition, skipping one that changes nothing. +#[cfg(feature = "capture")] +fn publish_status(state: &kio::Producer, status: Status) { + let Ok(mut published) = state.write() else { return }; + if published.state.status != status { + published.state.status = status; + published.revision = published.revision.wrapping_add(1); + } +} + /// Capture a webcam and publish it as a video track. /// -/// The source is opened at startup to probe the mode it negotiates, so the -/// catalog rendition is exact before a frame is published, then captured and -/// encoded whether or not anyone is watching. Set -/// [`Capture::on_demand`] to capture only while a subscriber is watching -/// instead. Drop the future to stop it, which also abandons a camera open or -/// permission prompt still in flight. -/// -/// Returns when the broadcast is dropped (the track stops being announced) or -/// the capture fails. Frames are stamped on the catalog's clock, so a -/// concurrent audio publish into the same catalog stays aligned. +/// Registers the track before anything about its source is known, and returns a +/// handle that observes the publication plus the [`Driver`] that captures it. The +/// driver opens the source to probe the mode it negotiates, so the catalog +/// rendition is exact before a frame is published, then captures and encodes +/// whether or not anyone is watching. Set [`Capture::on_demand`] to capture only +/// while a subscriber is watching instead. #[cfg(feature = "capture")] -pub async fn publish_capture( +pub fn publish_capture( broadcast: moq_net::broadcast::Producer, catalog: moq_mux::catalog::Producer, options: Capture, -) -> Result<(), Error> { - publish_capture_from(broadcast, catalog, options, DeviceSource).await +) -> Result<(Publication, Driver), Error> { + let track = capture_track(broadcast, &catalog, &options)?; + let state = starting(); + let publication = Publication { + state: state.consume(), + observed: 0, + }; + let driver = Driver { + track: Some(track), + catalog, + options, + state, + }; + Ok((publication, driver)) +} + +/// A handle that observes a capture publication. +/// +/// Clones observe the same publication. Dropping it changes nothing: the +/// [`Driver`] owns the publication. +#[cfg(feature = "capture")] +pub struct Publication { + state: kio::Consumer, + observed: u64, +} + +#[cfg(feature = "capture")] +impl Clone for Publication { + fn clone(&self) -> Self { + Self { + state: self.state.clone(), + observed: self.state.read().revision, + } + } +} + +#[cfg(feature = "capture")] +impl std::fmt::Debug for Publication { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Publication") + .field("state", &self.state.read().state) + .finish_non_exhaustive() + } +} + +#[cfg(feature = "capture")] +impl Publication { + /// Return the latest state without waiting. + pub fn state(&self) -> State { + self.state.read().state.clone() + } + + /// Wait for a lifecycle change, or return `None` after the driver exits. + pub async fn changed(&mut self) -> Option { + let observed = self.observed; + let published = self + .state + .wait(move |published| { + if published.revision != observed { + std::task::Poll::Ready((**published).clone()) + } else { + std::task::Poll::Pending + } + }) + .await + .ok()?; + self.observed = published.revision; + Some(published.state) + } + + /// Whether the driver has exited. + pub fn is_finished(&self) -> bool { + self.state.is_closed() + } +} + +/// The task that captures, encodes, and publishes one video track. +/// +/// Dropping it, or its [`run`](Self::run) future, stops the capture, which also +/// abandons a camera open or permission prompt still in flight. +#[cfg(feature = "capture")] +pub struct Driver { + track: Option, + catalog: moq_mux::catalog::Producer, + options: Capture, + state: kio::Producer, +} + +#[cfg(feature = "capture")] +impl std::fmt::Debug for Driver { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Driver").finish_non_exhaustive() + } +} + +#[cfg(feature = "capture")] +impl Drop for Driver { + fn drop(&mut self) { + // Dropped before `run`: say so rather than `Dropped`. + if let Some(track) = self.track.take() { + let _ = track.abort(moq_net::Error::Cancel); + } + } +} + +#[cfg(feature = "capture")] +impl Driver { + /// Capture until the broadcast is dropped (the track stops being announced) or the + /// capture fails. + /// + /// Frames are stamped on the catalog's clock, so a concurrent audio publish into the + /// same catalog stays aligned. Off macOS and iOS the future is `Send` and can be + /// spawned; on macOS the capture session is `!Send`, so await it on a local task there. + pub async fn run(self) -> Result<(), Error> { + self.run_from(DeviceSource).await + } + + /// [`run`](Self::run) from `source`, so the fixtures can drive it from a synthetic or + /// stalled one. + async fn run_from(mut self, source: S) -> Result<(), Error> { + let track = self.track.take().expect("a driver runs once"); + let result = capture_from(track, &self.catalog, &self.options, source, &self.state).await; + publish_status( + &self.state, + match result { + Ok(()) => Status::Ended, + Err(_) => Status::Failed, + }, + ); + let _ = self.state.close(); + result + } } /// Register the capture's video track, before anything about its source is known. @@ -377,18 +541,15 @@ fn capture_track( Ok(broadcast.unique_track(suffix, info)?) } -/// [`publish_capture`] from `source`, so the fixtures can drive it from a synthetic or stalled one. -/// -/// Off macOS the future is `Send` and can be spawned; on macOS the capture session is `!Send`, -/// so await it on a local task there. +/// Probe `source`, publish its rendition on `track`, then run the capture loop. #[cfg(feature = "capture")] -async fn publish_capture_from( - broadcast: moq_net::broadcast::Producer, - catalog: moq_mux::catalog::Producer, - options: Capture, +async fn capture_from( + track: moq_net::track::Producer, + catalog: &moq_mux::catalog::Producer, + options: &Capture, mut source: S, + state: &kio::Producer, ) -> Result<(), Error> { - let track = capture_track(broadcast, &catalog, &options)?; let clock = catalog.clock(); // Open the camera to find out what it actually negotiated, since a requested size is only a @@ -427,7 +588,7 @@ async fn publish_capture_from( // The importer consumes the track, so keep a handle to abort it if setup fails, // then drop it so it never outlives the importer's. let fallback = track.clone(); - let mut producer = match Producer::with_track(track, catalog.reserve().with_interval(join), rendition) { + let mut producer = match Producer::new(track, catalog.reserve().with_interval(join), rendition) { Ok(producer) => producer, Err(err) => { let _ = fallback.abort(moq_net::Error::Transport(err.to_string())); @@ -442,7 +603,7 @@ async fn publish_capture_from( source, }; - let result = capture_loop(&mut producer, &mut source, &options, &clock).await; + let result = capture_loop(&mut producer, &mut source, options, &clock, state).await; // This runs only when the loop ends on its own (the track is usually already // going away by then); dropping the future cancels it before this point, since @@ -460,20 +621,16 @@ async fn publish_capture_from( result } -/// Off macOS and iOS, [`publish_capture`]'s future must stay `Send` so a server can +/// Off macOS and iOS, [`Driver::run`]'s future must stay `Send` so a server can /// `tokio::spawn` it: the encoder runs on its own thread and the capture guard /// is `Send` there. This is never called; it exists only to fail compilation if /// the future ever regains a `!Send` component. Apple platforms are exempt (the inline encoder and objc /// capture session are `!Send`). #[cfg(all(feature = "capture", not(apple)))] #[allow(dead_code, reason = "a compile-time `Send` assertion, never called")] -fn assert_publish_capture_send( - broadcast: moq_net::broadcast::Producer, - catalog: moq_mux::catalog::Producer, - options: Capture, -) { +fn assert_driver_send(driver: Driver) { fn is_send(_: &T) {} - is_send(&publish_capture(broadcast, catalog, options)); + is_send(&driver.run()); } /// Where the capture opens its camera. Kept apart from the device backends so the @@ -574,16 +731,6 @@ async fn apply_target( } } -/// Whether `sink` honors a cut. -#[cfg(feature = "capture")] -async fn honors_cuts(sink: &mut Sink) -> Result { - match sink.check_cut().await { - Ok(()) => Ok(true), - Err(Error::CutUnsupported(_)) => Ok(false), - Err(err) => Err(err), - } -} - /// An application dropping or closing its producer ends publishing normally; /// a specific abort (such as a transport reset) stays visible in diagnostics. #[cfg(feature = "capture")] @@ -680,6 +827,7 @@ async fn capture_loop( source: &mut S, options: &Capture, clock: &moq_mux::Clock, + state: &kio::Producer, ) -> Result<(), Error> { // This track's claim on the connection. Taken on the first open, because the // negotiated mode is what finally says how much this encoder can ever send, and @@ -696,10 +844,14 @@ async fn capture_loop( // On demand, idle until a viewer subscribes. The catalog rendition was published when // the track was created, so a subscriber can get here without a frame ever having been // encoded. Either way the track ending is a clean exit. + if on_demand { + publish_status(state, Status::Waiting); + } if let Err(err) = until_capturing(&demand, on_demand, true).await { log_track_ended(err); return Ok(()); } + publish_status(state, Status::Starting); // Open the camera and an encoder sized to its negotiated mode. let Some(mut camera) = wait_capture(&demand, on_demand, source.open(capture)).await? else { @@ -729,8 +881,9 @@ async fn capture_loop( continue; }; // A reopen can land on another backend than the probe did, so ask this one. - let cuts = honors_cuts(&mut encoder).await?; + let cuts = encoder.applied().cut; tracing::info!(encoder = encoder.name(), device = camera.label(), "capturing"); + publish_status(state, Status::Live); // A reopen can negotiate a different mode (a display resized while nothing was // subscribed), and the claim follows it: the old ceiling would otherwise cap a @@ -897,7 +1050,7 @@ mod tests { /// /// `kind` is explicit so the test picks a deterministic encoder rather than `Auto`, which on /// Linux CI would try the NVENC backend and panic in cudarc on a GPU-less runner. - async fn roundtrip_rendition(codec: Codec, kind: encoder::Kind) -> (String, hang::catalog::VideoConfig) { + async fn roundtrip_rendition(codec: Codec, kind: encoder::Kind) -> hang::catalog::VideoConfig { let mut broadcast = moq_net::broadcast::Info::new().produce(); let catalog = moq_mux::catalog::Producer::new(&mut broadcast, moq_mux::catalog::Config::default()).unwrap(); @@ -906,7 +1059,9 @@ mod tests { config.kind = kind; let mut producer = Producer::new( - broadcast, + broadcast + .create_track("video", catalog.track_info(hang::catalog::PRIORITY.video)) + .unwrap(), catalog.reserve().with_interval(config.effective_join()), config.probe().await.unwrap(), ) @@ -924,7 +1079,7 @@ mod tests { } producer.publish(&encoder.finish().unwrap()).unwrap(); - let (name, resolved) = rendition(&catalog).expect("the importer should have registered a video rendition"); + let (_, resolved) = rendition(&catalog).expect("the importer should have registered a video rendition"); // Jitter and delay aside, which are measured from the frames rather than declared by either. let (mut before, mut after) = (advertised, resolved.clone()); (before.jitter, before.delay) = (None, None); @@ -933,7 +1088,7 @@ mod tests { before, after, "the first keyframe should confirm the advertised rendition, not correct it" ); - (name, resolved) + resolved } /// The catalog's single video rendition, if it has one yet. @@ -978,7 +1133,7 @@ mod tests { let mut config = Config::new(320, 240, crate::FrameRate::new(30, 1).unwrap()); config.kind = encoder::Kind::Software; - let mut producer = Producer::with_track( + let mut producer = Producer::new( track, catalog.reserve().with_interval(config.effective_join()), config.probe().await.unwrap(), @@ -1009,7 +1164,9 @@ mod tests { let mut initial = Config::new(320, 240, crate::FrameRate::new(30, 1).unwrap()); initial.kind = encoder::Kind::Software; let mut producer = Producer::new( - broadcast, + broadcast + .create_track("video", catalog.track_info(hang::catalog::PRIORITY.video)) + .unwrap(), catalog.reserve().with_interval(initial.effective_join()), initial.probe().await.unwrap(), ) @@ -1032,7 +1189,7 @@ mod tests { // 640x360 takes a higher level than the probed 320x240, so it is a new track. let (name, rendition) = rendition(&catalog).expect("the resized rendition should be published"); - assert!(name.ends_with(".avc3/1"), "{name} is not a new track"); + assert_eq!(name, "video/1", "the resize is a new track"); assert_eq!(rendition.coded_width, Some(640)); assert_eq!(rendition.coded_height, Some(360)); } @@ -1056,7 +1213,10 @@ mod tests { selected.container = hang::catalog::Container::Loc; selected.preference = -1; - let _producer = Producer::new(broadcast, catalog.reserve(), selected).unwrap(); + let track = broadcast + .unique_track(".avc3", catalog.track_info(hang::catalog::PRIORITY.video)) + .unwrap(); + let _producer = Producer::new(track, catalog.reserve(), selected).unwrap(); let (_, published) = rendition(&catalog).expect("the rendition publishes before any frame"); assert_eq!(published.container, hang::catalog::Container::Loc); @@ -1082,7 +1242,9 @@ mod tests { // Software (openh264) so the test is deterministic and never touches a hardware backend. config.kind = encoder::Kind::Software; let _producer = Producer::new( - broadcast, + broadcast + .create_track("video", catalog.track_info(hang::catalog::PRIORITY.video)) + .unwrap(), catalog.reserve().with_interval(config.effective_join()), config.probe().await.unwrap(), ) @@ -1094,13 +1256,12 @@ mod tests { .unwrap(); let snapshot = stream.next().await.unwrap().expect("a catalog before any frame"); - let (name, rendition) = snapshot + let (_, rendition) = snapshot .video .renditions .iter() .next() .expect("the track must be discoverable before it has encoded anything"); - assert!(name.ends_with(".avc3")); // Read out of the encoder rather than guessed: the avc3 shape (parameter sets in band) and // the geometry it was opened at, which is what its first keyframe will carry. @@ -1127,7 +1288,7 @@ mod tests { .create_track("video", catalog.track_info(hang::catalog::PRIORITY.video)) .unwrap(); let mut subscriber = track.subscribe(None); - let mut producer = Producer::with_track( + let mut producer = Producer::new( track, catalog.reserve().with_interval(config.effective_join()), config.probe().await.unwrap(), @@ -1149,9 +1310,8 @@ mod tests { async fn h264_roundtrip_publishes_avc3() { // Software (openh264) so the test is deterministic and never touches a // hardware backend. - let (name, config) = roundtrip_rendition(Codec::H264, encoder::Kind::Software).await; + let config = roundtrip_rendition(Codec::H264, encoder::Kind::Software).await; assert_eq!(config.reorder, Some(0)); - assert!(name.ends_with(".avc3")); assert_eq!(config.coded_width, Some(320)); assert_eq!(config.coded_height, Some(240)); } @@ -1161,8 +1321,7 @@ mod tests { #[cfg(apple)] #[tokio::test] async fn h265_roundtrip_publishes_hev1() { - let (name, config) = roundtrip_rendition(Codec::H265, encoder::Kind::Hardware).await; - assert!(name.ends_with(".hev1")); + let config = roundtrip_rendition(Codec::H265, encoder::Kind::Hardware).await; assert_eq!(config.coded_width, Some(320)); assert_eq!(config.coded_height, Some(240)); } @@ -1240,7 +1399,7 @@ mod tests { let mut probe = Config::new(320, 240, crate::FrameRate::new(30, 1).unwrap()); probe.kind = encoder::Kind::Software; - let mut producer = Producer::with_track( + let mut producer = Producer::new( track, catalog.reserve().with_interval(probe.effective_join()), probe.probe().await.unwrap(), @@ -1259,8 +1418,9 @@ mod tests { on_demand: true, ..Capture::default() }; + let state = starting(); tokio::select! { - res = capture_loop(&mut producer, &mut source, &options, &capturing) => res?, + res = capture_loop(&mut producer, &mut source, &options, &capturing, &state) => res?, _ = stopped => {} } producer.finish() @@ -1673,6 +1833,23 @@ mod tests { } } + /// [`publish_capture`] from `source` rather than a device. + async fn publish_from( + broadcast: moq_net::broadcast::Producer, + catalog: moq_mux::catalog::Producer, + options: Capture, + source: impl CaptureSource, + ) -> Result<(), Error> { + let (_, driver) = publish_capture(broadcast, catalog, options)?; + driver.run_from(source).await + } + + async fn status(publication: &mut Publication, expected: Status) { + while publication.state().status() != expected { + publication.changed().await.expect("driver still alive"); + } + } + /// What the capture is fed next, one at a time. enum Step { /// A frame 10ms after the previous one, waited on until the first subscriber has it. @@ -1721,7 +1898,7 @@ mod tests { // script below stays inside. The capture keeps the probe's camera. let rate = crate::FrameRate::new(10, 1).unwrap(); let (camera, stream) = Synthetic::open(crate::Size::new(320, 240), rate); - let run = tokio::task::spawn_local(publish_capture_from( + let run = tokio::task::spawn_local(publish_from( broadcast.clone(), catalog, options, @@ -1888,7 +2065,7 @@ mod tests { let (camera, stream) = camera(); let source = Streams::new([stream]); let opens = source.opens.clone(); - let run = tokio::task::spawn_local(publish_capture_from(broadcast.clone(), catalog, options, source)); + let run = tokio::task::spawn_local(publish_from(broadcast.clone(), catalog, options, source)); let name = rendition_name(&consumer).await; let mut track = consumer.track(&name).unwrap(); @@ -1940,21 +2117,29 @@ mod tests { let (camera, stream) = camera(); let source = Streams::new([probe, stream]); let opens = source.opens.clone(); - let run = tokio::task::spawn_local(publish_capture_from(broadcast.clone(), catalog, options, source)); + let (mut publication, driver) = publish_capture(broadcast.clone(), catalog, options).unwrap(); + assert_eq!(publication.state().status(), Status::Starting); + let run = tokio::task::spawn_local(driver.run_from(source)); let name = rendition_name(&consumer).await; until(|| probed.released()).await; assert_eq!(opens.load(Ordering::SeqCst), 1, "only the probe opened the camera"); + status(&mut publication, Status::Waiting).await; let mut subscriber = consumer.track(&name).unwrap().subscribe(None).await.unwrap(); camera.push_native(surface(), us(0)); subscriber.recv_group().await.unwrap().expect("a group"); assert_eq!(opens.load(Ordering::SeqCst), 2, "the viewer opened the camera"); + assert_eq!(publication.state().status(), Status::Live); drop(subscriber); until(|| camera.released()).await; + status(&mut publication, Status::Waiting).await; assert!(!run.is_finished(), "the capture ended early: {:?}", run.await); run.abort(); + assert!(run.await.unwrap_err().is_cancelled()); + // Dropping the driver's future closes the publication. + assert!(publication.is_finished()); }) .await } @@ -2000,7 +2185,7 @@ mod tests { let watched = track.clone(); let mut rendition = Config::new(320, 240, crate::FrameRate::new(30, 1).unwrap()); rendition.kind = encoder::Kind::Software; - let mut producer = Producer::with_track( + let mut producer = Producer::new( track, catalog.reserve().with_interval(rendition.effective_join()), rendition.probe().await.unwrap(), @@ -2016,7 +2201,8 @@ mod tests { ..Capture::default() }; let clock = catalog.clock(); - let mut run = std::pin::pin!(capture_loop(&mut producer, &mut source, &options, &clock)); + let state = starting(); + let mut run = std::pin::pin!(capture_loop(&mut producer, &mut source, &options, &clock, &state)); let viewer = watched.subscribe(None); let followed = settle(run.as_mut(), |set| !set.is_empty()).await; @@ -2056,7 +2242,7 @@ mod tests { let _subscriber = track.subscribe(None); let mut rendition = Config::new(320, 240, crate::FrameRate::new(30, 1).unwrap()); rendition.kind = encoder::Kind::Software; - let mut producer = Producer::with_track( + let mut producer = Producer::new( track, catalog.reserve().with_interval(rendition.effective_join()), rendition.probe().await.unwrap(), @@ -2070,7 +2256,8 @@ mod tests { ..Capture::default() }; let clock = catalog.clock(); - let mut run = std::pin::pin!(capture_loop(&mut producer, &mut source, &options, &clock)); + let state = starting(); + let mut run = std::pin::pin!(capture_loop(&mut producer, &mut source, &options, &clock, &state)); // The probe holds its first frame back, so this one reaches the codec but not the track. camera.push_native(surface(), us(0)); @@ -2227,7 +2414,10 @@ mod finished { })); let finished: Vec<_> = (0..sources) .map(|_| { - let mut producer = Producer::new(broadcast.clone(), catalog.reserve(), rendition.clone()).unwrap(); + let track = broadcast + .unique_track(".avc3", catalog.track_info(hang::catalog::PRIORITY.video)) + .unwrap(); + let mut producer = Producer::new(track, catalog.reserve(), rendition.clone()).unwrap(); for pts in [0, 33_333] { let pts = moq_net::Timestamp::from_micros(pts).unwrap(); producer.publish(&[Encoded::new(keyframe(), pts, true)]).unwrap(); diff --git a/rs/moq-video/src/encode/sink.rs b/rs/moq-video/src/encode/sink.rs index d94f3213d5a..9d3dcdddf8a 100644 --- a/rs/moq-video/src/encode/sink.rs +++ b/rs/moq-video/src/encode/sink.rs @@ -94,12 +94,6 @@ impl Sink { self.0.cut().await } - /// What [`cut`](Self::cut) would answer, without queueing anything. - #[cfg(feature = "capture")] - pub(crate) async fn check_cut(&mut self) -> Result<(), Error> { - self.0.check_cut().await - } - /// Encode one frame, waiting for its access units. /// /// Otherwise [`Encoder::encode`](super::Encoder::encode): zero or more access @@ -176,9 +170,6 @@ mod threaded { /// refusal has to reach the caller, since the alternative is a group /// boundary that silently never happens. Cut { resp: oneshot::Sender> }, - /// Report whether a cut would be refused, queueing nothing. - #[cfg(feature = "capture")] - CheckCut { resp: oneshot::Sender> }, /// Retune to a new bitrate, reporting whether the backend took it so the /// caller can stop adapting against an encoder that can't. The round trip /// is affordable because the rate control policy only sends one of these @@ -236,10 +227,6 @@ mod threaded { Request::Cut { resp } => { let _ = resp.send(encoder.cut()); } - #[cfg(feature = "capture")] - Request::CheckCut { resp } => { - let _ = resp.send(encoder.check_cut()); - } Request::SetBitrate { bitrate, resp } => { let _ = resp.send(encoder.set_bitrate(bitrate)); } @@ -282,11 +269,6 @@ mod threaded { self.0.request(|resp| Request::Cut { resp }).await } - #[cfg(feature = "capture")] - pub async fn check_cut(&mut self) -> Result<(), Error> { - self.0.request(|resp| Request::CheckCut { resp }).await - } - pub async fn encode(&mut self, frame: Arc) -> Result, Error> { self.0.request(|resp| Request::Encode { frame, resp }).await } @@ -342,11 +324,6 @@ mod inline { self.0.cut() } - #[cfg(feature = "capture")] - pub async fn check_cut(&mut self) -> Result<(), Error> { - self.0.check_cut() - } - pub async fn encode(&mut self, frame: Arc) -> Result, Error> { self.0.encode(&frame) } @@ -430,6 +407,7 @@ mod tests { config.kind = Kind::Named(probe::NO_CUT.into()); let mut sink = pollster::block_on(Sink::open(&config)).unwrap(); assert_eq!(sink.name(), probe::NO_CUT); + assert!(!sink.applied().cut, "the report says it cannot cut"); let err = pollster::block_on(sink.cut()).expect_err("the backend cannot cut"); assert!( diff --git a/rs/moq-video/src/lib.rs b/rs/moq-video/src/lib.rs index 21a7ed4cff2..1f6f88e6693 100644 --- a/rs/moq-video/src/lib.rs +++ b/rs/moq-video/src/lib.rs @@ -25,7 +25,8 @@ //! and framing. The codec is chosen via [`encode::Codec`]: H.264 (openh264 / //! VideoToolbox / Media Foundation / NVENC / VAAPI / V4L2) or H.265 //! (VideoToolbox / Media Foundation / NVENC). Entry points: -//! - `encode::publish_capture` captures a webcam and publishes it (turnkey). +//! - `encode::publish_capture` captures a webcam and publishes it (turnkey), +//! returning a `Publication` handle and the `Driver` that runs it. //! The track and catalog are advertised up front (the camera opens at //! startup so they can be exact), and the camera stays open and encodes //! whether or not anyone is watching, unless `encode::Capture::on_demand` diff --git a/rs/moq-video/src/render/export.rs b/rs/moq-video/src/render/export.rs index 3958acb7e9a..68029eefbd4 100644 --- a/rs/moq-video/src/render/export.rs +++ b/rs/moq-video/src/render/export.rs @@ -90,7 +90,7 @@ impl Exporter { pub const DEPTH: usize = Readback::DEPTH; /// Export textures drawn on `device` to an encoder `config` allows, and - /// return the rendition it produces. + /// return the settled encoder config and the rendition it produces. /// /// Tries the encoders [`encode::Config::kind`](crate::encode::Config::kind) /// allows in priority order, probing each as @@ -102,10 +102,10 @@ impl Exporter { /// `device` is not on the NVIDIA GPU and settles on an encoder that reads /// frames back, rather than failing. /// - /// Textures are `config`'s size. This leaves `config.kind` naming the - /// encoder it settled on, and on Linux + /// Textures are `config`'s size. The returned config is `config` with its + /// `kind` naming the encoder this settled on, and on Linux /// [`encode::Config::input`](crate::encode::Config::input) naming the device - /// that encoder reads, so open the encoder with `config` as this leaves it. + /// that encoder reads, so open the encoder with it. /// /// # Errors /// @@ -119,8 +119,8 @@ impl Exporter { pub async fn new( device: &wgpu::Device, queue: &wgpu::Queue, - config: &mut crate::encode::Config, - ) -> Result<(Self, hang::catalog::VideoConfig), Error> { + config: &crate::encode::Config, + ) -> Result<(Self, crate::encode::Config, hang::catalog::VideoConfig), Error> { let candidates = crate::encode::backend::candidates(config.codec, &config.kind); if candidates.is_empty() { return Err(crate::encode::backend::missing(config.codec, &config.kind)); @@ -138,9 +138,9 @@ impl Exporter { async fn settle( device: &wgpu::Device, queue: &wgpu::Queue, - config: &mut crate::encode::Config, + config: &crate::encode::Config, candidates: impl IntoIterator, - ) -> Result<(Self, hang::catalog::VideoConfig), Error> { + ) -> Result<(Self, crate::encode::Config, hang::catalog::VideoConfig), Error> { let color = crate::Color::infer(config.size()); if let Some(configured) = config.color && configured != color @@ -171,8 +171,7 @@ impl Exporter { ); } tracing::info!(encoder = name, ?route, "exporting to the encoder"); - *config = attempt; - return Ok((exporter, rendition)); + return Ok((exporter, attempt, rendition)); } Err(e) if candidate.gpu => { tracing::debug!( @@ -548,8 +547,8 @@ pub(crate) mod tests { async fn takes_the_route_the_encoder_reads() { let (device, queue) = gpu().await.expect("a GPU adapter"); let size = Size::new(64, 64); - let mut config = crate::encode::Config::new(size.width, size.height, crate::FrameRate::new(30, 1).unwrap()); - let (mut exporter, _) = Exporter::new(&device, &queue, &mut config).await.unwrap(); + let config = crate::encode::Config::new(size.width, size.height, crate::FrameRate::new(30, 1).unwrap()); + let (mut exporter, config, _) = Exporter::new(&device, &queue, &config).await.unwrap(); assert_eq!(exporter.size(), size); let mut encoder = crate::encode::Encoder::new(&config).unwrap(); let expected = match crate::encode::backend::reads_export(encoder.name()) { @@ -590,13 +589,13 @@ pub(crate) mod tests { #[ignore = "needs a GPU and OpenH264; run by just rs render-sw"] async fn choosing_passes_over_an_encoder_that_cannot_read_this_device() { let (device, queue) = gpu().await.expect("a GPU adapter"); - let mut config = crate::encode::Config::new(64, 64, crate::FrameRate::new(30, 1).unwrap()); + let config = crate::encode::Config::new(64, 64, crate::FrameRate::new(30, 1).unwrap()); let readback = Candidate { name: "openh264", hardware: false, gpu: false, }; - let (exporter, _) = Exporter::settle(&device, &queue, &mut config, [FOREIGN, readback]) + let (exporter, config, _) = Exporter::settle(&device, &queue, &config, [FOREIGN, readback]) .await .unwrap(); assert_eq!(exporter.route(), Route::Readback); @@ -613,7 +612,7 @@ pub(crate) mod tests { let (device, queue) = gpu().await.expect("a GPU adapter"); let mut config = crate::encode::Config::new(64, 64, crate::FrameRate::new(30, 1).unwrap()); config.kind = crate::encode::Kind::Named(FOREIGN.name.into()); - let Err(err) = Exporter::settle(&device, &queue, &mut config, [FOREIGN]).await else { + let Err(err) = Exporter::settle(&device, &queue, &config, [FOREIGN]).await else { panic!("exported to an encoder that cannot read this device"); }; let err = err.to_string(); @@ -631,7 +630,7 @@ pub(crate) mod tests { let (device, queue) = gpu().await.expect("a GPU adapter"); let mut config = crate::encode::Config::new(640, 480, crate::FrameRate::new(30, 1).unwrap()); config.color = Some(crate::Color::BT709); - let Err(err) = Exporter::new(&device, &queue, &mut config).await else { + let Err(err) = Exporter::new(&device, &queue, &config).await else { panic!("exported BT.601 pixels under a BT.709 label"); }; assert!(matches!(err, Error::Unsupported(_)), "{err}"); diff --git a/rs/moq-video/src/render/export/dx12.rs b/rs/moq-video/src/render/export/dx12.rs index ad53f84c4f6..efc8507558d 100644 --- a/rs/moq-video/src/render/export/dx12.rs +++ b/rs/moq-video/src/render/export/dx12.rs @@ -349,8 +349,8 @@ mod tests { async fn media_foundation_encodes_the_export_in_place() { let (device, queue) = dx12(false).await; let size = Size::new(640, 480); - let mut config = crate::encode::Config::new(size.width, size.height, crate::FrameRate::new(30, 1).unwrap()); - let (mut exporter, _) = Exporter::new(&device, &queue, &mut config).await.unwrap(); + let config = crate::encode::Config::new(size.width, size.height, crate::FrameRate::new(30, 1).unwrap()); + let (mut exporter, config, _) = Exporter::new(&device, &queue, &config).await.unwrap(); assert_eq!(exporter.route(), Route::Gpu, "Media Foundation reads textures"); let mut encoder = crate::encode::Encoder::new(&config).unwrap(); assert_eq!(encoder.name(), "mediafoundation"); diff --git a/rs/moq-video/src/render/export/metal.rs b/rs/moq-video/src/render/export/metal.rs index c90059e06db..737885c3dfe 100644 --- a/rs/moq-video/src/render/export/metal.rs +++ b/rs/moq-video/src/render/export/metal.rs @@ -302,8 +302,8 @@ mod tests { async fn videotoolbox_reads_the_export_in_place() { let (device, queue) = gpu().await.expect("a Metal device"); let size = Size::new(64, 48); - let mut config = crate::encode::Config::new(size.width, size.height, crate::FrameRate::new(30, 1).unwrap()); - let (mut exporter, _) = Exporter::new(&device, &queue, &mut config).await.unwrap(); + let config = crate::encode::Config::new(size.width, size.height, crate::FrameRate::new(30, 1).unwrap()); + let (mut exporter, config, _) = Exporter::new(&device, &queue, &config).await.unwrap(); assert_eq!(exporter.route(), Route::Gpu, "VideoToolbox reads pixel buffers"); let mut encoder = crate::encode::Encoder::new(&config).unwrap(); assert_eq!(encoder.name(), "videotoolbox"); diff --git a/rs/moq-video/src/render/export/vulkan.rs b/rs/moq-video/src/render/export/vulkan.rs index b3aaa78a14d..ea1ee7dc816 100644 --- a/rs/moq-video/src/render/export/vulkan.rs +++ b/rs/moq-video/src/render/export/vulkan.rs @@ -662,8 +662,8 @@ mod tests { .await .unwrap(); let size = Size::new(320, 240); - let mut config = crate::encode::Config::new(size.width, size.height, crate::FrameRate::new(30, 1).unwrap()); - let (mut exporter, _) = Exporter::new(&device, &queue, &mut config).await.unwrap(); + let config = crate::encode::Config::new(size.width, size.height, crate::FrameRate::new(30, 1).unwrap()); + let (mut exporter, config, _) = Exporter::new(&device, &queue, &config).await.unwrap(); assert_eq!(exporter.route(), Route::Gpu); assert_eq!(config.input, exporter.input()); let mut encoder = crate::encode::Encoder::new(&config).unwrap(); From adadeebe3f49d2511cc2e1cc7d7d5374538db0e9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 8 Nov 2026 00:47:22 -0800 Subject: [PATCH 3/7] fix(moq-audio,moq-video): a dropped capture driver reports Cancelled Co-Authored-By: Claude Opus 5.5 --- rs/moq-audio/src/encode/capture.rs | 15 ++++++++++++++- rs/moq-video/src/encode/producer.rs | 8 +++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/rs/moq-audio/src/encode/capture.rs b/rs/moq-audio/src/encode/capture.rs index 64176e649b8..5acbaca391c 100644 --- a/rs/moq-audio/src/encode/capture.rs +++ b/rs/moq-audio/src/encode/capture.rs @@ -67,6 +67,9 @@ pub enum Status { Failed, /// The MoQ track ended, so the driver has stopped. Ended, + /// The [`Driver`] or its [`run`](Driver::run) future was dropped before it + /// finished, cancelling the track. + Cancelled, } /// A snapshot of a capture publication's lifecycle. @@ -265,6 +268,9 @@ impl Drop for Driver { if let Some(producer) = self.producer.take() { producer.abort(moq_net::Error::Cancel); } + // A no-op once `run` closed the state on its own terminal status. + publish_state(&self.state, Status::Cancelled, None); + publish_level(&self.level, Level::default()); } } @@ -909,11 +915,18 @@ mod tests { #[tokio::test(start_paused = true)] async fn dropping_an_unfinished_driver_cancels_its_track() { - let Fixture { driver, broadcast, .. } = fixture(false); + let Fixture { + driver, + broadcast, + publication, + .. + } = fixture(false); let mut track = subscribe(&broadcast, Duration::ZERO).await; drop(driver); let error = track.read().await.unwrap_err(); assert!(format!("{error:?}").contains("Cancel"), "{error:?}"); + assert_eq!(publication.state().status(), Status::Cancelled); + assert!(publication.is_finished()); } #[tokio::test(start_paused = true)] diff --git a/rs/moq-video/src/encode/producer.rs b/rs/moq-video/src/encode/producer.rs index a229b5aa0ce..524220a3b44 100644 --- a/rs/moq-video/src/encode/producer.rs +++ b/rs/moq-video/src/encode/producer.rs @@ -329,6 +329,9 @@ pub enum Status { Failed, /// The MoQ track ended, so the driver has stopped. Ended, + /// The [`Driver`] or its [`run`](Driver::run) future was dropped before it + /// finished, cancelling the capture. + Cancelled, } /// A snapshot of a capture publication's lifecycle. @@ -489,6 +492,8 @@ impl Drop for Driver { if let Some(track) = self.track.take() { let _ = track.abort(moq_net::Error::Cancel); } + // A no-op once `run` closed the state on its own terminal status. + publish_status(&self.state, Status::Cancelled); } } @@ -2138,8 +2143,9 @@ mod tests { assert!(!run.is_finished(), "the capture ended early: {:?}", run.await); run.abort(); assert!(run.await.unwrap_err().is_cancelled()); - // Dropping the driver's future closes the publication. + // Dropping the driver's future cancels the publication. assert!(publication.is_finished()); + assert_eq!(publication.state().status(), Status::Cancelled); }) .await } From 36be011d9d074df8de6bd62d3f89744c7847f4b5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 8 Nov 2026 08:03:18 -0800 Subject: [PATCH 4/7] feat(moq-audio,moq-video)!: Status::Failed carries its error A value holds one phase: Status::Failed(Arc) replaces audio's separate State::failure field, and video's State now carries the error its Driver::run returns. Video's driver holds its importer so a dropped run future cancels the track rather than leaving Dropped. Co-Authored-By: Claude Opus 5.5 --- doc/lib/rs/moq-video.md | 9 +- rs/moq-audio/MIGRATE.md | 31 +++- rs/moq-audio/src/encode/capture.rs | 111 +++++++------ rs/moq-video/MIGRATE.md | 26 ++++ rs/moq-video/src/encode/producer.rs | 234 ++++++++++++++++------------ rs/moq-video/src/render/export.rs | 19 ++- 6 files changed, 260 insertions(+), 170 deletions(-) diff --git a/doc/lib/rs/moq-video.md b/doc/lib/rs/moq-video.md index 6b33aa8d39d..01f14be226a 100644 --- a/doc/lib/rs/moq-video.md +++ b/doc/lib/rs/moq-video.md @@ -26,10 +26,11 @@ feature (vendored C++); the published bindings and binaries enable it. H.265 is hardware-only, AV1 decodes through NVDEC and MediaCodec, and VP8 and VP9 decode in software through the opt-in `vpx` feature. -`encode::publish_capture` advertises the track up front, then keeps the camera -open and encoding whether or not anyone subscribes, so a late viewer can fetch -the recent past. Set `encode::Capture::on_demand` to open the camera only while -someone subscribes, at the cost of that past. A viewer who joins +`encode::publish_capture` advertises the track up front and returns a +`Publication` that reports its status, plus the `Driver` that captures. Running +the driver keeps the camera open and encoding whether or not anyone subscribes, +so a late viewer can fetch the recent past. Set `encode::Capture::on_demand` to +open the camera only while someone subscribes, at the cost of that past. A viewer who joins once the current group's deltas outweigh twice its keyframe gets a fresh keyframe, so `encode::Config::join` is only the longest a group runs. Through a moq-lite relay, only the join that opens the relay's upstream subscription counts. A diff --git a/rs/moq-audio/MIGRATE.md b/rs/moq-audio/MIGRATE.md index 94376708a9f..b306148e9cd 100644 --- a/rs/moq-audio/MIGRATE.md +++ b/rs/moq-audio/MIGRATE.md @@ -4,6 +4,27 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Capture publishes through `publish_capture`, returning a `Publication` and a `Driver` + +`encode::Control::new(broadcast, catalog, options)` is +`let (publication, driver) = encode::publish_capture(broadcast, catalog, options)?;`, +then `driver.run().await`. `encode::Publication` keeps the state accessors: +`track_name`, `state`, `changed`, `level`, and `is_finished`. +`Control::{start, stop, replace}` have no replacement: drop the `Driver` to stop, +and publish a new consumer to switch sources. `Status::Stopped` is gone, and +`Status::Cancelled` reports a `Driver` or `run` future dropped before it finished. + +`Status::Failed` carries its error as `Failed(Arc)`, and `State::failure()` +is gone: match `Status::Failed(error)` instead. `State::status()` returns +`&Status`, and `Status` is no longer `Copy` or `PartialEq`, so compare with +`matches!`. + +`encode::Capture::on_demand` is `retain`, with the same default and meaning. + +### Playback `Sink` volume moves to its `Control` + +`Sink::{set_volume, volume, peak}` are gone. Call them on `sink.control()`. + ### Playback fills a declared silence with comfort noise `Sink::end()` declares that the audio written so far is followed by a silence @@ -68,15 +89,13 @@ A libopus allocation, internal, or state failure is the new `Error::Codec` inste Open `capture::Producer::open(config).await?` once and create a consumer with `capture.consume()` for each destination. Pass one to `bus.capture(consumer, strip)?` and another to `encode::Capture::new(consumer)`, then set its `encode` and -`on_demand` fields. Drive `capture.run()` alongside the bus and encoder futures. +`retain` fields. Drive `capture.run()` alongside the bus and encoder futures. This replaces both device-opening entry points; `encode::Capture::default()` and its `capture: Config` field are gone. The audio rendition is registered only after the capture has opened and its actual input format is known. -`encode::Control::start` and `stop` control only that encoder, and `replace` -takes another capture consumer and returns an error, keeping the current one, when -its layout cannot be converted into the track's input. They never open or release -the device. A capture +Dropping an encoder's `Driver` stops only that encoder; it never opens or +releases the device. A capture failure sets `Status::Failed` at once; the driver keeps serving retained on-demand audio until it is served or expires, then returns the failure. Recovery requires a new capture and publication. There is no automatic device reopen. Dropping the capture @@ -89,7 +108,7 @@ a microphone array does not declare surround-speaker positions. A stereo bus refuses those inputs instead of guessing a surround downmix. Request mono or stereo through `capture::Config::channels` when the device supports it, or map the known channels explicitly before writing a `Bus::channel`. The encoder refuses -them too: `encode::Control::new` fails for such a consumer, since its default +them too: `encode::publish_capture` fails for such a consumer, since its default stereo config (and Opus in any config) cannot take a discrete layout. Publish a microphone array only when it delivers mono or stereo, or with an `encode.config.layout` set to that same `Layout::Discrete` for a codec that carries it. diff --git a/rs/moq-audio/src/encode/capture.rs b/rs/moq-audio/src/encode/capture.rs index 5acbaca391c..4fc6e1b3604 100644 --- a/rs/moq-audio/src/encode/capture.rs +++ b/rs/moq-audio/src/encode/capture.rs @@ -50,7 +50,7 @@ impl Capture { } /// The capture publication's current lifecycle state. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Debug)] #[non_exhaustive] pub enum Status { /// Raw audio is retained until a subscriber needs encoding. @@ -64,7 +64,7 @@ pub enum Status { /// /// After a source failure the driver keeps serving retained history on demand /// until it is served or expires, then returns the failure. - Failed, + Failed(Arc), /// The MoQ track ended, so the driver has stopped. Ended, /// The [`Driver`] or its [`run`](Driver::run) future was dropped before it @@ -72,6 +72,16 @@ pub enum Status { Cancelled, } +impl Status { + /// Whether publishing `self` over `other` changes nothing an observer can see. + fn same(&self, other: &Self) -> bool { + match (self, other) { + (Self::Failed(a), Self::Failed(b)) => Arc::ptr_eq(a, b), + _ => std::mem::discriminant(self) == std::mem::discriminant(other), + } + } +} + /// A snapshot of a capture publication's lifecycle. /// /// Levels are deliberately not here: they change every buffer, so they would @@ -82,13 +92,12 @@ pub struct State { status: Status, source: capture::Source, device: Option, - failure: Option, } impl State { /// The publication's current lifecycle state. - pub fn status(&self) -> Status { - self.status + pub fn status(&self) -> &Status { + &self.status } /// The selected source, including an unresolved default microphone. @@ -102,11 +111,6 @@ impl State { pub fn device(&self) -> Option<&capture::Device> { self.device.as_ref() } - - /// The source failure or driver error while [`Status::Failed`]. - pub fn failure(&self) -> Option<&Error> { - self.failure.as_ref() - } } #[derive(Clone, Debug)] @@ -134,7 +138,6 @@ pub fn publish_capture( status: Status::Starting, source: options.capture.source().clone(), device: options.capture.device().cloned(), - failure: None, }, revision: 0, }); @@ -269,7 +272,7 @@ impl Drop for Driver { producer.abort(moq_net::Error::Cancel); } // A no-op once `run` closed the state on its own terminal status. - publish_state(&self.state, Status::Cancelled, None); + publish_state(&self.state, Status::Cancelled); publish_level(&self.level, Level::default()); } } @@ -279,7 +282,7 @@ enum End { /// The track closed. Closed, /// The source failed, and every retained frame since was served or expired. - Failed(Error), + Failed(Arc), } impl Driver { @@ -292,41 +295,42 @@ impl Driver { let end = self.drive().await; let mut producer = self.producer.take().unwrap(); let now = self.clock.now(); - let ended = end.and_then(|end| match end { + let ended = end.map_err(Arc::new).and_then(|end| match end { End::Closed => producer .set_active(false, now) .and_then(|()| producer.finish()) - .map(|()| None), - End::Failed(error) => { + .map(|()| None) + .map_err(Arc::new), + End::Failed(failure) => { let ended = producer .set_active(false, now) - .and_then(|()| producer.fail(moq_net::Error::Transport(error.to_string()))); + .and_then(|()| producer.fail(moq_net::Error::Transport(failure.to_string()))); // The source failure stays the cause, even when its track can no longer end // cleanly, such as one aborted while it still retained audio. match ended { - Ok(()) => Ok(Some(error)), - Err(_) => Err(error), + Ok(()) => Ok(Some(failure)), + Err(_) => Err(failure), } } }); let result = match ended { Ok(None) => { - self.update(Status::Ended, None); + self.update(Status::Ended); Ok(()) } - Ok(Some(error)) => { - self.update(Status::Failed, Some(error.clone())); - Err(error) + Ok(Some(failure)) => { + self.update(Status::Failed(failure.clone())); + Err(failure) } - Err(error) => { - self.update(Status::Failed, Some(error.clone())); - producer.abort(moq_net::Error::Transport(error.to_string())); - Err(error) + Err(failure) => { + self.update(Status::Failed(failure.clone())); + producer.abort(moq_net::Error::Transport(failure.to_string())); + Err(failure) } }; self.publish_level(Level::default()); let _ = self.state.close(); - result + result.map_err(Arc::unwrap_or_clone) } async fn drive(&mut self) -> Result { @@ -364,6 +368,8 @@ impl Driver { // The source failed, yet retained history stays reachable: subscribers still encode // it on demand until none is left or nothing can want it any more. + // One shared copy, so repeated failed refreshes publish nothing new. + let failure = Arc::new(failure); retention.failed(failure.clone()); retention.refresh(producer)?; while producer.retains() { @@ -376,8 +382,8 @@ impl Driver { Ok(End::Failed(failure)) } - fn update(&self, status: Status, failure: Option) { - publish_state(&self.state, status, failure); + fn update(&self, status: Status) { + publish_state(&self.state, status); } fn publish_level(&self, level: Level) { @@ -388,20 +394,12 @@ impl Driver { /// Publish a lifecycle transition, skipping one that changes nothing. /// /// Levels and repeated capture states must not wake lifecycle-only observers. -fn publish_state(state: &kio::Producer, status: Status, failure: Option) { +fn publish_state(state: &kio::Producer, status: Status) { let Ok(mut published) = state.write() else { return }; - // `Error` is not `PartialEq`, and a failure publish is rare enough that - // rendering both to compare them costs nothing worth avoiding. - let same_failure = match (&published.state.failure, &failure) { - (None, None) => true, - (Some(before), Some(now)) => before.to_string() == now.to_string(), - _ => false, - }; - if same_failure && published.state.status == status { + if published.state.status.same(&status) { return; } published.state.status = status; - published.state.failure = failure; published.revision = published.revision.wrapping_add(1); } @@ -446,7 +444,7 @@ struct Retention<'a> { state: &'a kio::Producer, level: &'a kio::Producer, /// The source failure, after which the status stays failed. - failure: Option, + failure: Option>, } impl Retention<'_> { @@ -455,8 +453,8 @@ impl Retention<'_> { let active = !self.retain || self.demand.wanted().is_some(); producer.set_active(active, self.clock.now())?; match &self.failure { - Some(failure) => publish_state(self.state, Status::Failed, Some(failure.clone())), - None => publish_state(self.state, if active { Status::Live } else { Status::Waiting }, None), + Some(failure) => publish_state(self.state, Status::Failed(failure.clone())), + None => publish_state(self.state, if active { Status::Live } else { Status::Waiting }), } Ok(()) } @@ -486,7 +484,7 @@ impl Retention<'_> { } /// The source failed: the level goes quiet and the status stays failed. - fn failed(&mut self, failure: Error) { + fn failed(&mut self, failure: Arc) { publish_level(self.level, Level::default()); self.failure = Some(failure); } @@ -570,8 +568,8 @@ mod tests { } } - async fn status(publication: &mut Publication, expected: Status) { - while publication.state().status() != expected { + async fn status(publication: &mut Publication, reached: impl Fn(&Status) -> bool) { + while !reached(publication.state().status()) { publication.changed().await.expect("driver still alive"); } } @@ -799,9 +797,8 @@ mod tests { ] { assert!(matches!(result, Err(Error::Capture(message)) if message == "device lost")); } - assert_eq!(publication.state().status(), Status::Failed); assert!( - matches!(publication.state().failure(), Some(Error::Capture(message)) if message == "device lost") + matches!(publication.state().status(), Status::Failed(error) if matches!(&**error, Error::Capture(message) if message == "device lost")) ); assert!(publication.is_finished()); }) @@ -825,7 +822,7 @@ mod tests { let mut observer = capture.consume(); let capture_task = tokio::task::spawn_local(capture.run()); let encode_task = tokio::spawn(driver.run()); - status(&mut publication, Status::Waiting).await; + status(&mut publication, |status| matches!(status, Status::Waiting)).await; let mut block = Samples::plain(vec![0.25; 1920], false); block.captured = Some(epoch + Duration::from_millis(100)); samples.try_push(Ok(block)).unwrap(); @@ -843,7 +840,7 @@ mod tests { assert!(packet(&mut track).await.timestamp.as_micros() > 30_000_000); } else { assert_eq!(packet(&mut track).await.timestamp.as_micros(), 100_000); - assert_eq!(publication.state().status(), Status::Live); + assert!(matches!(publication.state().status(), Status::Live)); } encode_task.abort(); assert!(encode_task.await.unwrap_err().is_cancelled()); @@ -871,16 +868,16 @@ mod tests { let mut observer = capture.consume(); let capture_task = tokio::task::spawn_local(capture.run()); let encode_task = tokio::spawn(driver.run()); - status(&mut publication, Status::Waiting).await; + status(&mut publication, |status| matches!(status, Status::Waiting)).await; let mut block = Samples::plain(vec![0.25; 1920], false); block.captured = Some(epoch + Duration::from_millis(100)); samples.try_push(Ok(block)).unwrap(); observer.read().await.unwrap(); retained(&publication).await; samples.try_push(Err(Error::Capture("device lost".into()))).unwrap(); - status(&mut publication, Status::Failed).await; + status(&mut publication, |status| matches!(status, Status::Failed(_))).await; assert!( - matches!(publication.state().failure(), Some(Error::Capture(message)) if message == "device lost") + matches!(publication.state().status(), Status::Failed(error) if matches!(&**error, Error::Capture(message) if message == "device lost")) ); // Failure is terminal, but the publication still serves what it retained. assert!(!publication.is_finished()); @@ -895,7 +892,7 @@ mod tests { let result = encode_task.await.unwrap(); assert!(matches!(result, Err(Error::Capture(message)) if message == "device lost")); assert!(publication.is_finished()); - assert_eq!(publication.state().status(), Status::Failed); + assert!(matches!(publication.state().status(), Status::Failed(_))); if let Some(mut track) = track { // The track keeps what it published, then ends with the failure rather than cleanly. let error = loop { @@ -918,14 +915,16 @@ mod tests { let Fixture { driver, broadcast, - publication, + mut publication, .. } = fixture(false); let mut track = subscribe(&broadcast, Duration::ZERO).await; drop(driver); let error = track.read().await.unwrap_err(); assert!(format!("{error:?}").contains("Cancel"), "{error:?}"); - assert_eq!(publication.state().status(), Status::Cancelled); + let state = publication.changed().await.expect("the cancellation is observed"); + assert!(matches!(state.status(), Status::Cancelled)); + assert!(publication.changed().await.is_none()); assert!(publication.is_finished()); } diff --git a/rs/moq-video/MIGRATE.md b/rs/moq-video/MIGRATE.md index 3952eddb28b..6786b7e1638 100644 --- a/rs/moq-video/MIGRATE.md +++ b/rs/moq-video/MIGRATE.md @@ -4,6 +4,32 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Capture returns a `Publication` and a `Driver` + +`encode::publish_capture(broadcast, catalog, options).await?` is +`let (publication, driver) = encode::publish_capture(broadcast, catalog, options)?;`, +then `driver.run().await`. `Publication` reports the capture's `State`, whose +`Status` is `Waiting`, `Starting`, `Live`, `Failed(Arc)`, `Ended`, or +`Cancelled`. `Driver::run` returns `Result<(), Arc>`, sharing the error +its `Status::Failed` carries. + +### `encode::Producer::new` takes a track + +`encode::Producer::new(broadcast, reserved, rendition)` is +`new(track, reserved, rendition)`, and `with_track` is gone. Create the track +with `broadcast.unique_track(".avc3", reserved.track_info(PRIORITY.video))?` +(`.hev1` for H.265). + +### `encode::Applied::cut` reports cut support + +Read `encoder.applied().cut` instead of matching `Error::CutUnsupported`. + +### `render::Exporter::new` returns the encoder config + +`render::Exporter::new(&device, &queue, &config)` takes `&encode::Config` and +returns `(Exporter, encode::Config, VideoConfig)`. Open the encoder with the +returned config. + ### Consumer decoder setup refusals are typed `decode::Consumer::new` reports `Error::UnsupportedConfig` for a codec or description this build cannot decode, where `decode::Decoder::new` reports `Error::UnsupportedCodec`. That now includes a missing or malformed avcC or hvcC description (previously `Error::Codec` or `Error::Mux`) and a codec no compiled-in decoder handles (previously `Error::NoDecoder`). A stream every decoder for its codec refuses because the device cannot decode it, such as AV1 on a GPU without AV1 decode or a size past its limits, is a refusal too. `Error::NoDecoder` now means that a decoder for the codec failed to open for another reason, such as a missing driver or no device. Backend faults, scale-hint errors, and subscription failures keep their own errors. A codec worker that cannot start or exits during initialization reports `Error::CodecGone`, like a worker lost after initialization, rather than `Error::Codec`. diff --git a/rs/moq-video/src/encode/producer.rs b/rs/moq-video/src/encode/producer.rs index 524220a3b44..ad642e3bcc4 100644 --- a/rs/moq-video/src/encode/producer.rs +++ b/rs/moq-video/src/encode/producer.rs @@ -11,6 +11,9 @@ //! is sure to honor a cut, and forces a keyframe whenever a joining subscriber asks for a group //! past the largest. The encoder's GOP stays the upper bound between keyframes. +#[cfg(feature = "capture")] +use std::sync::Arc; + use moq_mux::catalog::hang::CatalogExt; #[cfg(feature = "capture")] use moq_mux::rate; @@ -316,7 +319,7 @@ pub struct Capture { /// The capture publication's current lifecycle state. #[cfg(feature = "capture")] -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Debug)] #[non_exhaustive] pub enum Status { /// On demand, the source is closed until a subscriber wants the track. @@ -325,8 +328,8 @@ pub enum Status { Starting, /// Frames are being captured, encoded, and published. Live, - /// The capture, encoder, or track failed; [`Driver::run`] returned the error. - Failed, + /// The capture, encoder, or track failed with this error, which [`Driver::run`] returns. + Failed(Arc), /// The MoQ track ended, so the driver has stopped. Ended, /// The [`Driver`] or its [`run`](Driver::run) future was dropped before it @@ -334,6 +337,17 @@ pub enum Status { Cancelled, } +#[cfg(feature = "capture")] +impl Status { + /// Whether publishing `self` over `other` changes nothing an observer can see. + fn same(&self, other: &Self) -> bool { + match (self, other) { + (Self::Failed(a), Self::Failed(b)) => Arc::ptr_eq(a, b), + _ => std::mem::discriminant(self) == std::mem::discriminant(other), + } + } +} + /// A snapshot of a capture publication's lifecycle. #[cfg(feature = "capture")] #[derive(Clone, Debug)] @@ -344,8 +358,8 @@ pub struct State { #[cfg(feature = "capture")] impl State { /// The publication's current lifecycle state. - pub fn status(&self) -> Status { - self.status + pub fn status(&self) -> &Status { + &self.status } } @@ -371,7 +385,7 @@ fn starting() -> kio::Producer { #[cfg(feature = "capture")] fn publish_status(state: &kio::Producer, status: Status) { let Ok(mut published) = state.write() else { return }; - if published.state.status != status { + if !published.state.status.same(&status) { published.state.status = status; published.revision = published.revision.wrapping_add(1); } @@ -399,6 +413,7 @@ pub fn publish_capture( }; let driver = Driver { track: Some(track), + producer: None, catalog, options, state, @@ -472,7 +487,10 @@ impl Publication { /// abandons a camera open or permission prompt still in flight. #[cfg(feature = "capture")] pub struct Driver { + /// The registered track, until the probe knows its rendition. track: Option, + /// The track's importer once probed, held here so a dropped `run` future still cancels it. + producer: Option, catalog: moq_mux::catalog::Producer, options: Capture, state: kio::Producer, @@ -488,10 +506,13 @@ impl std::fmt::Debug for Driver { #[cfg(feature = "capture")] impl Drop for Driver { fn drop(&mut self) { - // Dropped before `run`: say so rather than `Dropped`. + // Dropped before `run` finished: say so rather than `Dropped`. if let Some(track) = self.track.take() { let _ = track.abort(moq_net::Error::Cancel); } + if let Some(producer) = self.producer.take() { + producer.abort(moq_net::Error::Cancel); + } // A no-op once `run` closed the state on its own terminal status. publish_status(&self.state, Status::Cancelled); } @@ -505,25 +526,101 @@ impl Driver { /// Frames are stamped on the catalog's clock, so a concurrent audio publish into the /// same catalog stays aligned. Off macOS and iOS the future is `Send` and can be /// spawned; on macOS the capture session is `!Send`, so await it on a local task there. - pub async fn run(self) -> Result<(), Error> { + pub async fn run(self) -> Result<(), Arc> { self.run_from(DeviceSource).await } /// [`run`](Self::run) from `source`, so the fixtures can drive it from a synthetic or /// stalled one. - async fn run_from(mut self, source: S) -> Result<(), Error> { - let track = self.track.take().expect("a driver runs once"); - let result = capture_from(track, &self.catalog, &self.options, source, &self.state).await; + async fn run_from(mut self, source: S) -> Result<(), Arc> { + let result = self.capture(source).await.map_err(Arc::new); + // This runs only when the capture ends on its own (the track is usually already + // going away by then); dropping the future leaves the track to `Drop` instead, + // since async `Drop` can't finalize it. + match &result { + // Clean end (the track was dropped): best-effort finish. + Ok(()) => { + if let Some(mut producer) = self.producer.take() + && let Err(err) = producer.finish() + { + tracing::debug!(error = %err, "video track finish after capture ended"); + } + } + // Abort with the real cause so subscribers see it. A track that already ended has + // nobody left to tell. + Err(err) => { + let cause = moq_net::Error::Transport(err.to_string()); + if let Some(track) = self.track.take() { + let _ = track.abort(cause.clone()); + } + if let Some(producer) = self.producer.take() { + producer.abort(cause); + } + } + } publish_status( &self.state, - match result { + match &result { Ok(()) => Status::Ended, - Err(_) => Status::Failed, + Err(err) => Status::Failed(err.clone()), }, ); let _ = self.state.close(); result } + + /// Probe `source`, wrap the track in an importer for its rendition, then run the capture loop. + async fn capture(&mut self, mut source: S) -> Result<(), Error> { + let clock = self.catalog.clock(); + let options = &self.options; + + // Open the camera to find out what it actually negotiated, since a requested size is only a + // hint (macOS ignores it outright) and the encoder is built from the mode, not the request. + // This buys a rendition that says exactly what the stream will carry, rather than one every + // consumer has to treat as provisional, and lets a consumer size itself against it without + // waiting for an encoder that may not run yet. + let (rendition, join, camera) = async { + let camera = source.open(&options.capture).await?; + let mut probe_config = encoder::Config::new( + camera.width(), + camera.height(), + options + .capture + .framerate + .or_else(|| camera.framerate()) + .unwrap_or(DEFAULT_FRAMERATE), + ); + probe_config.bitrate = options.bitrate; + probe_config.codec = options.codec; + probe_config.kind = options.kind.clone(); + probe_config.color = camera.color(); + let rendition = probe_config.probe().await?; + // The encoder is built from the same defaults, so the probe's join is the one it emits. + Ok::<_, Error>((rendition, probe_config.effective_join(), camera)) + } + .await?; + + // The importer consumes the track, so keep a handle to abort it if setup fails, + // then drop it so it never outlives the importer's. + let track = self.track.take().expect("a driver runs once"); + let fallback = track.clone(); + let producer = match Producer::new(track, self.catalog.reserve().with_interval(join), rendition) { + Ok(producer) => producer, + Err(err) => { + let _ = fallback.abort(moq_net::Error::Transport(err.to_string())); + return Err(err); + } + }; + drop(fallback); + let producer = self.producer.insert(producer); + // Capturing from the start, the loop keeps the probe's camera rather than reopening it, + // which could mean a second permission prompt or picker. On demand it closes until a viewer. + let mut source = Opened { + stream: (!options.on_demand).then_some(camera), + source, + }; + capture_loop(producer, &mut source, options, &clock, &self.state).await + } } /// Register the capture's video track, before anything about its source is known. @@ -546,86 +643,6 @@ fn capture_track( Ok(broadcast.unique_track(suffix, info)?) } -/// Probe `source`, publish its rendition on `track`, then run the capture loop. -#[cfg(feature = "capture")] -async fn capture_from( - track: moq_net::track::Producer, - catalog: &moq_mux::catalog::Producer, - options: &Capture, - mut source: S, - state: &kio::Producer, -) -> Result<(), Error> { - let clock = catalog.clock(); - - // Open the camera to find out what it actually negotiated, since a requested size is only a - // hint (macOS ignores it outright) and the encoder is built from the mode, not the request. - // This buys a rendition that says exactly what the stream will carry, rather than one every - // consumer has to treat as provisional, and lets a consumer size itself against it without - // waiting for an encoder that may not run yet. - let rendition = async { - let camera = source.open(&options.capture).await?; - let mut probe_config = encoder::Config::new( - camera.width(), - camera.height(), - options - .capture - .framerate - .or_else(|| camera.framerate()) - .unwrap_or(DEFAULT_FRAMERATE), - ); - probe_config.bitrate = options.bitrate; - probe_config.codec = options.codec; - probe_config.kind = options.kind.clone(); - probe_config.color = camera.color(); - let rendition = probe_config.probe().await?; - // The encoder is built from the same defaults, so the probe's join is the one it emits. - Ok::<_, Error>((rendition, probe_config.effective_join(), camera)) - }; - let (rendition, join, camera) = match rendition.await { - Ok(probed) => probed, - Err(err) => { - // A track that already ended has nobody left to tell. - let _ = track.abort(moq_net::Error::Transport(err.to_string())); - return Err(err); - } - }; - - // The importer consumes the track, so keep a handle to abort it if setup fails, - // then drop it so it never outlives the importer's. - let fallback = track.clone(); - let mut producer = match Producer::new(track, catalog.reserve().with_interval(join), rendition) { - Ok(producer) => producer, - Err(err) => { - let _ = fallback.abort(moq_net::Error::Transport(err.to_string())); - return Err(err); - } - }; - drop(fallback); - // Capturing from the start, the loop keeps the probe's camera rather than reopening it, - // which could mean a second permission prompt or picker. On demand it closes until a viewer. - let mut source = Opened { - stream: (!options.on_demand).then_some(camera), - source, - }; - - let result = capture_loop(&mut producer, &mut source, options, &clock, state).await; - - // This runs only when the loop ends on its own (the track is usually already - // going away by then); dropping the future cancels it before this point, since - // async `Drop` can't finalize the track. - match &result { - // Clean end (the track was dropped): best-effort finish. - Ok(()) => { - if let Err(err) = producer.finish() { - tracing::debug!(error = %err, "video track finish after capture ended"); - } - } - // The capture loop failed: abort with the real cause so subscribers see it. - Err(err) => producer.abort(moq_net::Error::Transport(err.to_string())), - } - result -} - /// Off macOS and iOS, [`Driver::run`]'s future must stay `Send` so a server can /// `tokio::spawn` it: the encoder runs on its own thread and the capture guard /// is `Send` there. This is never called; it exists only to fail compilation if @@ -1844,13 +1861,13 @@ mod tests { catalog: moq_mux::catalog::Producer, options: Capture, source: impl CaptureSource, - ) -> Result<(), Error> { + ) -> Result<(), Arc> { let (_, driver) = publish_capture(broadcast, catalog, options)?; driver.run_from(source).await } - async fn status(publication: &mut Publication, expected: Status) { - while publication.state().status() != expected { + async fn status(publication: &mut Publication, reached: impl Fn(&Status) -> bool) { + while !reached(publication.state().status()) { publication.changed().await.expect("driver still alive"); } } @@ -2123,29 +2140,42 @@ mod tests { let source = Streams::new([probe, stream]); let opens = source.opens.clone(); let (mut publication, driver) = publish_capture(broadcast.clone(), catalog, options).unwrap(); - assert_eq!(publication.state().status(), Status::Starting); + assert!(matches!(publication.state().status(), Status::Starting)); let run = tokio::task::spawn_local(driver.run_from(source)); let name = rendition_name(&consumer).await; until(|| probed.released()).await; assert_eq!(opens.load(Ordering::SeqCst), 1, "only the probe opened the camera"); - status(&mut publication, Status::Waiting).await; + status(&mut publication, |status| matches!(status, Status::Waiting)).await; let mut subscriber = consumer.track(&name).unwrap().subscribe(None).await.unwrap(); camera.push_native(surface(), us(0)); subscriber.recv_group().await.unwrap().expect("a group"); assert_eq!(opens.load(Ordering::SeqCst), 2, "the viewer opened the camera"); - assert_eq!(publication.state().status(), Status::Live); + assert!(matches!(publication.state().status(), Status::Live)); drop(subscriber); until(|| camera.released()).await; - status(&mut publication, Status::Waiting).await; + status(&mut publication, |status| matches!(status, Status::Waiting)).await; assert!(!run.is_finished(), "the capture ended early: {:?}", run.await); + let mut subscriber = consumer.track(&name).unwrap().subscribe(None).await.unwrap(); run.abort(); assert!(run.await.unwrap_err().is_cancelled()); - // Dropping the driver's future cancels the publication. + // Dropping the driver's future cancels the publication and its track. + let error = loop { + match subscriber.recv_group().await { + Ok(Some(_)) => {} + Ok(None) => panic!("the cancelled track ended cleanly"), + Err(error) => break error, + } + }; + assert!(format!("{error:?}").contains("Cancel"), "{error:?}"); + let mut last = None; + while let Some(state) = publication.changed().await { + last = Some(state); + } + assert!(matches!(last.as_ref().map(State::status), Some(Status::Cancelled))); assert!(publication.is_finished()); - assert_eq!(publication.state().status(), Status::Cancelled); }) .await } diff --git a/rs/moq-video/src/render/export.rs b/rs/moq-video/src/render/export.rs index 68029eefbd4..1519de2874c 100644 --- a/rs/moq-video/src/render/export.rs +++ b/rs/moq-video/src/render/export.rs @@ -547,9 +547,18 @@ pub(crate) mod tests { async fn takes_the_route_the_encoder_reads() { let (device, queue) = gpu().await.expect("a GPU adapter"); let size = Size::new(64, 64); - let config = crate::encode::Config::new(size.width, size.height, crate::FrameRate::new(30, 1).unwrap()); + let mut config = crate::encode::Config::new(size.width, size.height, crate::FrameRate::new(30, 1).unwrap()); + let bitrate = Some(moq_net::bandwidth::Rate::from_kbps(700)); + let join = std::time::Duration::from_millis(1500); + config.bitrate = bitrate; + config.join = join; let (mut exporter, config, _) = Exporter::new(&device, &queue, &config).await.unwrap(); assert_eq!(exporter.size(), size); + assert_eq!( + (config.bitrate, config.join), + (bitrate, join), + "the caller's settings survive" + ); let mut encoder = crate::encode::Encoder::new(&config).unwrap(); let expected = match crate::encode::backend::reads_export(encoder.name()) { true => Route::Gpu, @@ -589,7 +598,13 @@ pub(crate) mod tests { #[ignore = "needs a GPU and OpenH264; run by just rs render-sw"] async fn choosing_passes_over_an_encoder_that_cannot_read_this_device() { let (device, queue) = gpu().await.expect("a GPU adapter"); - let config = crate::encode::Config::new(64, 64, crate::FrameRate::new(30, 1).unwrap()); + let mut config = crate::encode::Config::new(64, 64, crate::FrameRate::new(30, 1).unwrap()); + // A caller's stale device must not survive onto the readback encoder. + config.input = Some(crate::frame::vulkan::Device { + device_uuid: [0xaa; 16], + driver_uuid: [0xbb; 16], + render_node: None, + }); let readback = Candidate { name: "openh264", hardware: false, From 4c27b8e4ff3d471c1e90d4c5ab1c6e064390a5bc Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 8 Nov 2026 08:20:02 -0800 Subject: [PATCH 5/7] refactor(moq-video): a capture driver owns its track in one phase Co-Authored-By: Claude Opus 5.5 --- rs/moq-video/src/encode/producer.rs | 102 +++++++++++++++------------- 1 file changed, 56 insertions(+), 46 deletions(-) diff --git a/rs/moq-video/src/encode/producer.rs b/rs/moq-video/src/encode/producer.rs index ad642e3bcc4..2bdd2b3a542 100644 --- a/rs/moq-video/src/encode/producer.rs +++ b/rs/moq-video/src/encode/producer.rs @@ -412,8 +412,7 @@ pub fn publish_capture( observed: 0, }; let driver = Driver { - track: Some(track), - producer: None, + owned: Owned::Track(track), catalog, options, state, @@ -487,15 +486,50 @@ impl Publication { /// abandons a camera open or permission prompt still in flight. #[cfg(feature = "capture")] pub struct Driver { - /// The registered track, until the probe knows its rendition. - track: Option, - /// The track's importer once probed, held here so a dropped `run` future still cancels it. - producer: Option, + owned: Owned, catalog: moq_mux::catalog::Producer, options: Capture, state: kio::Producer, } +/// What a [`Driver`] owns of its track, one phase at a time, held in the driver so a +/// dropped [`run`](Driver::run) future still cancels it. +#[cfg(feature = "capture")] +enum Owned { + /// Registered, until the probe knows its rendition. + Track(moq_net::track::Producer), + /// Probed, and wrapped in the importer that publishes it. + Producer(Box), + /// Ended or cancelled, so nothing is left to end. + Done, +} + +#[cfg(feature = "capture")] +impl Owned { + /// End the track cleanly, best effort: it is usually already going away. + fn finish(self) { + let ended = match self { + Self::Track(track) => track.finish().map_err(Error::from), + Self::Producer(mut producer) => producer.finish(), + Self::Done => Ok(()), + }; + if let Err(err) = ended { + tracing::debug!(error = %err, "video track finish after capture ended"); + } + } + + /// End the track with `err`. A track that already ended has nobody left to tell. + fn abort(self, err: moq_net::Error) { + match self { + Self::Track(track) => { + let _ = track.abort(err); + } + Self::Producer(producer) => producer.abort(err), + Self::Done => {} + } + } +} + #[cfg(feature = "capture")] impl std::fmt::Debug for Driver { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -507,12 +541,7 @@ impl std::fmt::Debug for Driver { impl Drop for Driver { fn drop(&mut self) { // Dropped before `run` finished: say so rather than `Dropped`. - if let Some(track) = self.track.take() { - let _ = track.abort(moq_net::Error::Cancel); - } - if let Some(producer) = self.producer.take() { - producer.abort(moq_net::Error::Cancel); - } + std::mem::replace(&mut self.owned, Owned::Done).abort(moq_net::Error::Cancel); // A no-op once `run` closed the state on its own terminal status. publish_status(&self.state, Status::Cancelled); } @@ -534,29 +563,13 @@ impl Driver { /// stalled one. async fn run_from(mut self, source: S) -> Result<(), Arc> { let result = self.capture(source).await.map_err(Arc::new); - // This runs only when the capture ends on its own (the track is usually already - // going away by then); dropping the future leaves the track to `Drop` instead, - // since async `Drop` can't finalize it. + // This runs only when the capture ends on its own; dropping the future leaves the + // track to `Drop` instead, since async `Drop` can't finalize it. + let owned = std::mem::replace(&mut self.owned, Owned::Done); match &result { - // Clean end (the track was dropped): best-effort finish. - Ok(()) => { - if let Some(mut producer) = self.producer.take() - && let Err(err) = producer.finish() - { - tracing::debug!(error = %err, "video track finish after capture ended"); - } - } - // Abort with the real cause so subscribers see it. A track that already ended has - // nobody left to tell. - Err(err) => { - let cause = moq_net::Error::Transport(err.to_string()); - if let Some(track) = self.track.take() { - let _ = track.abort(cause.clone()); - } - if let Some(producer) = self.producer.take() { - producer.abort(cause); - } - } + Ok(()) => owned.finish(), + // Abort with the real cause so subscribers see it. + Err(err) => owned.abort(moq_net::Error::Transport(err.to_string())), } publish_status( &self.state, @@ -600,19 +613,16 @@ impl Driver { } .await?; - // The importer consumes the track, so keep a handle to abort it if setup fails, - // then drop it so it never outlives the importer's. - let track = self.track.take().expect("a driver runs once"); - let fallback = track.clone(); - let producer = match Producer::new(track, self.catalog.reserve().with_interval(join), rendition) { - Ok(producer) => producer, - Err(err) => { - let _ = fallback.abort(moq_net::Error::Transport(err.to_string())); - return Err(err); - } + // The importer takes a clone, so a setup failure leaves the registered track for `run` + // to abort, and success drops ours so it never outlives the importer's. + let Owned::Track(track) = &self.owned else { + unreachable!("a driver runs once") + }; + let producer = Producer::new(track.clone(), self.catalog.reserve().with_interval(join), rendition)?; + self.owned = Owned::Producer(Box::new(producer)); + let Owned::Producer(producer) = &mut self.owned else { + unreachable!("just set") }; - drop(fallback); - let producer = self.producer.insert(producer); // Capturing from the start, the loop keeps the probe's camera rather than reopening it, // which could mean a second permission prompt or picker. On demand it closes until a viewer. let mut source = Opened { From ef38050a8164f7451461a4fcfcf84e3daebdc7fd Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 8 Nov 2026 10:33:31 -0800 Subject: [PATCH 6/7] feat(moq-video)!: Error is Clone, so Driver::run and Status::Failed hold a plain Error moq-video's Error wraps its anyhow payloads in Arc and derives Clone. Both crates' Status::Failed carries Error, published once, and Driver::run returns it. Revert per-PR MIGRATE.md entries (the release cut writes them), document the on-demand probe, and correct Driver::run's stop condition. Co-Authored-By: Claude Opus 5.5 --- doc/lib/rs/moq-video.md | 4 +- rs/moq-audio/MIGRATE.md | 37 +++---- rs/moq-audio/src/encode/capture.rs | 85 ++++++--------- rs/moq-c/src/video.rs | 2 +- rs/moq-video/MIGRATE.md | 26 ----- rs/moq-video/src/capture/avfoundation.rs | 12 +-- rs/moq-video/src/capture/mediafoundation.rs | 4 +- rs/moq-video/src/capture/mod.rs | 2 +- rs/moq-video/src/capture/pipewire.rs | 48 ++++----- rs/moq-video/src/capture/pipewire/camera.rs | 6 +- rs/moq-video/src/capture/pump.rs | 2 +- rs/moq-video/src/capture/screencapture.rs | 22 ++-- rs/moq-video/src/capture/v4l2.rs | 26 ++--- rs/moq-video/src/capture/wgc/native.rs | 8 +- rs/moq-video/src/capture/x11.rs | 14 +-- rs/moq-video/src/convert.rs | 10 +- rs/moq-video/src/decode/backend/mediacodec.rs | 18 ++-- .../src/decode/backend/mediafoundation.rs | 12 +-- rs/moq-video/src/decode/backend/mod.rs | 4 +- rs/moq-video/src/decode/backend/nvdec.rs | 2 +- rs/moq-video/src/decode/backend/openh264.rs | 10 +- rs/moq-video/src/decode/backend/v4l2.rs | 12 +-- rs/moq-video/src/decode/backend/vaapi.rs | 10 +- .../src/decode/backend/videotoolbox.rs | 32 +++--- rs/moq-video/src/decode/backend/vpx.rs | 12 +-- rs/moq-video/src/encode/backend/mediacodec.rs | 12 +-- .../src/encode/backend/mediafoundation.rs | 12 +-- rs/moq-video/src/encode/backend/mod.rs | 2 +- rs/moq-video/src/encode/backend/nvenc.rs | 36 +++---- rs/moq-video/src/encode/backend/openh264.rs | 6 +- rs/moq-video/src/encode/backend/probe.rs | 2 +- rs/moq-video/src/encode/backend/v4l2.rs | 16 +-- rs/moq-video/src/encode/backend/vaapi.rs | 6 +- .../src/encode/backend/videotoolbox.rs | 16 +-- rs/moq-video/src/encode/encoder.rs | 6 +- rs/moq-video/src/encode/producer.rs | 37 +++---- rs/moq-video/src/error.rs | 27 ++++- rs/moq-video/src/frame.rs | 102 +++++++++--------- rs/moq-video/src/frame/cuda.rs | 22 ++-- rs/moq-video/src/frame/cuda_vulkan.rs | 4 +- rs/moq-video/src/frame/pool.rs | 2 +- rs/moq-video/src/frame/vaapi.rs | 10 +- rs/moq-video/src/frame/vulkan.rs | 4 +- rs/moq-video/src/mf.rs | 4 +- rs/moq-video/src/render/device.rs | 6 +- rs/moq-video/src/render/dmabuf.rs | 6 +- rs/moq-video/src/render/export.rs | 2 +- rs/moq-video/src/render/metal.rs | 2 +- rs/moq-video/src/render/readback.rs | 2 +- rs/moq-video/src/render/renderer.rs | 2 +- rs/moq-video/src/size.rs | 8 +- rs/moq-video/src/v4l2.rs | 22 ++-- 52 files changed, 377 insertions(+), 419 deletions(-) diff --git a/doc/lib/rs/moq-video.md b/doc/lib/rs/moq-video.md index 01f14be226a..5a66d4f7072 100644 --- a/doc/lib/rs/moq-video.md +++ b/doc/lib/rs/moq-video.md @@ -30,7 +30,9 @@ decode in software through the opt-in `vpx` feature. `Publication` that reports its status, plus the `Driver` that captures. Running the driver keeps the camera open and encoding whether or not anyone subscribes, so a late viewer can fetch the recent past. Set `encode::Capture::on_demand` to -open the camera only while someone subscribes, at the cost of that past. A viewer who joins +open the camera only while someone subscribes, at the cost of that past. Either +way the driver first opens the camera once to learn its mode, which can raise a +permission prompt before anyone subscribes. A viewer who joins once the current group's deltas outweigh twice its keyframe gets a fresh keyframe, so `encode::Config::join` is only the longest a group runs. Through a moq-lite relay, only the join that opens the relay's upstream subscription counts. A diff --git a/rs/moq-audio/MIGRATE.md b/rs/moq-audio/MIGRATE.md index b306148e9cd..9d12aa62f7f 100644 --- a/rs/moq-audio/MIGRATE.md +++ b/rs/moq-audio/MIGRATE.md @@ -4,26 +4,13 @@ How to update your code for each breaking change, newest first. ## Unreleased -### Capture publishes through `publish_capture`, returning a `Publication` and a `Driver` +### `Bus::channel` is `Bus::live` -`encode::Control::new(broadcast, catalog, options)` is -`let (publication, driver) = encode::publish_capture(broadcast, catalog, options)?;`, -then `driver.run().await`. `encode::Publication` keeps the state accessors: -`track_name`, `state`, `changed`, `level`, and `is_finished`. -`Control::{start, stop, replace}` have no replacement: drop the `Driver` to stop, -and publish a new consumer to switch sources. `Status::Stopped` is gone, and -`Status::Cancelled` reports a `Driver` or `run` future dropped before it finished. - -`Status::Failed` carries its error as `Failed(Arc)`, and `State::failure()` -is gone: match `Status::Failed(error)` instead. `State::status()` returns -`&Status`, and `Status` is no longer `Copy` or `PartialEq`, so compare with -`matches!`. - -`encode::Capture::on_demand` is `retain`, with the same default and meaning. - -### Playback `Sink` volume moves to its `Control` - -`Sink::{set_volume, volume, peak}` are gone. Call them on `sink.control()`. +The bus has two kinds of source: a live one on its own clock, whose ring +absorbs jitter and drift, and a positioned one on the bus's clock, whose every +write names its bus position. `Bus::channel` and `bus::Channel` are renamed +`Bus::live` and `bus::Live` to pair with `Bus::positioned` and +`bus::Positioned`. Their behavior is unchanged: rename the calls and the type. ### Playback fills a declared silence with comfort noise @@ -89,13 +76,15 @@ A libopus allocation, internal, or state failure is the new `Error::Codec` inste Open `capture::Producer::open(config).await?` once and create a consumer with `capture.consume()` for each destination. Pass one to `bus.capture(consumer, strip)?` and another to `encode::Capture::new(consumer)`, then set its `encode` and -`retain` fields. Drive `capture.run()` alongside the bus and encoder futures. +`on_demand` fields. Drive `capture.run()` alongside the bus and encoder futures. This replaces both device-opening entry points; `encode::Capture::default()` and its `capture: Config` field are gone. The audio rendition is registered only after the capture has opened and its actual input format is known. -Dropping an encoder's `Driver` stops only that encoder; it never opens or -releases the device. A capture +`encode::Control::start` and `stop` control only that encoder, and `replace` +takes another capture consumer and returns an error, keeping the current one, when +its layout cannot be converted into the track's input. They never open or release +the device. A capture failure sets `Status::Failed` at once; the driver keeps serving retained on-demand audio until it is served or expires, then returns the failure. Recovery requires a new capture and publication. There is no automatic device reopen. Dropping the capture @@ -107,8 +96,8 @@ Device channel counts above stereo are now consistently `Layout::Discrete`: a microphone array does not declare surround-speaker positions. A stereo bus refuses those inputs instead of guessing a surround downmix. Request mono or stereo through `capture::Config::channels` when the device supports it, or map -the known channels explicitly before writing a `Bus::channel`. The encoder refuses -them too: `encode::publish_capture` fails for such a consumer, since its default +the known channels explicitly before writing a `Bus::live`. The encoder refuses +them too: `encode::Control::new` fails for such a consumer, since its default stereo config (and Opus in any config) cannot take a discrete layout. Publish a microphone array only when it delivers mono or stereo, or with an `encode.config.layout` set to that same `Layout::Discrete` for a codec that carries it. diff --git a/rs/moq-audio/src/encode/capture.rs b/rs/moq-audio/src/encode/capture.rs index 4fc6e1b3604..a8235289ad5 100644 --- a/rs/moq-audio/src/encode/capture.rs +++ b/rs/moq-audio/src/encode/capture.rs @@ -64,7 +64,7 @@ pub enum Status { /// /// After a source failure the driver keeps serving retained history on demand /// until it is served or expires, then returns the failure. - Failed(Arc), + Failed(Error), /// The MoQ track ended, so the driver has stopped. Ended, /// The [`Driver`] or its [`run`](Driver::run) future was dropped before it @@ -72,16 +72,6 @@ pub enum Status { Cancelled, } -impl Status { - /// Whether publishing `self` over `other` changes nothing an observer can see. - fn same(&self, other: &Self) -> bool { - match (self, other) { - (Self::Failed(a), Self::Failed(b)) => Arc::ptr_eq(a, b), - _ => std::mem::discriminant(self) == std::mem::discriminant(other), - } - } -} - /// A snapshot of a capture publication's lifecycle. /// /// Levels are deliberately not here: they change every buffer, so they would @@ -282,7 +272,7 @@ enum End { /// The track closed. Closed, /// The source failed, and every retained frame since was served or expired. - Failed(Arc), + Failed(Error), } impl Driver { @@ -295,42 +285,34 @@ impl Driver { let end = self.drive().await; let mut producer = self.producer.take().unwrap(); let now = self.clock.now(); - let ended = end.map_err(Arc::new).and_then(|end| match end { - End::Closed => producer - .set_active(false, now) - .and_then(|()| producer.finish()) - .map(|()| None) - .map_err(Arc::new), - End::Failed(failure) => { + let result = match end { + Ok(End::Failed(failure)) => { + // Retention already reports the failure. It stays the cause even when the track + // can no longer end cleanly, such as one aborted while it still retained audio. let ended = producer .set_active(false, now) .and_then(|()| producer.fail(moq_net::Error::Transport(failure.to_string()))); - // The source failure stays the cause, even when its track can no longer end - // cleanly, such as one aborted while it still retained audio. - match ended { - Ok(()) => Ok(Some(failure)), - Err(_) => Err(failure), + if ended.is_err() { + producer.abort(moq_net::Error::Transport(failure.to_string())); } - } - }); - let result = match ended { - Ok(None) => { - self.update(Status::Ended); - Ok(()) - } - Ok(Some(failure)) => { - self.update(Status::Failed(failure.clone())); - Err(failure) - } - Err(failure) => { - self.update(Status::Failed(failure.clone())); - producer.abort(moq_net::Error::Transport(failure.to_string())); Err(failure) } + // The track closed, or encoding or publishing failed. + end => match end.and_then(|_| producer.set_active(false, now).and_then(|()| producer.finish())) { + Ok(()) => { + self.update(Status::Ended); + Ok(()) + } + Err(err) => { + self.update(Status::Failed(err.clone())); + producer.abort(moq_net::Error::Transport(err.to_string())); + Err(err) + } + }, }; self.publish_level(Level::default()); let _ = self.state.close(); - result.map_err(Arc::unwrap_or_clone) + result } async fn drive(&mut self) -> Result { @@ -342,7 +324,7 @@ impl Driver { clock: self.clock.clone(), state: &self.state, level: &self.level, - failure: None, + failed: false, }; let reader = &mut self.capture; retention.refresh(producer)?; @@ -368,9 +350,7 @@ impl Driver { // The source failed, yet retained history stays reachable: subscribers still encode // it on demand until none is left or nothing can want it any more. - // One shared copy, so repeated failed refreshes publish nothing new. - let failure = Arc::new(failure); - retention.failed(failure.clone()); + retention.failed(&failure); retention.refresh(producer)?; while producer.retains() { tokio::select! { @@ -396,7 +376,10 @@ impl Driver { /// Levels and repeated capture states must not wake lifecycle-only observers. fn publish_state(state: &kio::Producer, status: Status) { let Ok(mut published) = state.write() else { return }; - if published.state.status.same(&status) { + // A repeated status changes nothing an observer can see; a failure always carries news. + if !matches!(status, Status::Failed(_)) + && std::mem::discriminant(&published.state.status) == std::mem::discriminant(&status) + { return; } published.state.status = status; @@ -443,8 +426,8 @@ struct Retention<'a> { clock: moq_mux::Clock, state: &'a kio::Producer, level: &'a kio::Producer, - /// The source failure, after which the status stays failed. - failure: Option>, + /// The source failed, so the status stays failed. + failed: bool, } impl Retention<'_> { @@ -452,9 +435,8 @@ impl Retention<'_> { fn refresh(&self, producer: &mut Producer) -> Result<(), Error> { let active = !self.retain || self.demand.wanted().is_some(); producer.set_active(active, self.clock.now())?; - match &self.failure { - Some(failure) => publish_state(self.state, Status::Failed(failure.clone())), - None => publish_state(self.state, if active { Status::Live } else { Status::Waiting }), + if !self.failed { + publish_state(self.state, if active { Status::Live } else { Status::Waiting }); } Ok(()) } @@ -484,9 +466,10 @@ impl Retention<'_> { } /// The source failed: the level goes quiet and the status stays failed. - fn failed(&mut self, failure: Arc) { + fn failed(&mut self, failure: &Error) { publish_level(self.level, Level::default()); - self.failure = Some(failure); + publish_state(self.state, Status::Failed(failure.clone())); + self.failed = true; } } diff --git a/rs/moq-c/src/video.rs b/rs/moq-c/src/video.rs index 8a23f9c441d..8e7e1229dcc 100644 --- a/rs/moq-c/src/video.rs +++ b/rs/moq-c/src/video.rs @@ -1494,7 +1494,7 @@ mod tests { #[tokio::test] async fn a_failed_drain_aborts_the_track() { let (producer, mut track) = track_under_test().await; - let err = moq_video::Error::Codec(anyhow::anyhow!("the codec lost the tail")); + let err = moq_video::Error::Codec(anyhow::anyhow!("the codec lost the tail").into()); finalize( producer, Err(err), diff --git a/rs/moq-video/MIGRATE.md b/rs/moq-video/MIGRATE.md index 6786b7e1638..3952eddb28b 100644 --- a/rs/moq-video/MIGRATE.md +++ b/rs/moq-video/MIGRATE.md @@ -4,32 +4,6 @@ How to update your code for each breaking change, newest first. ## Unreleased -### Capture returns a `Publication` and a `Driver` - -`encode::publish_capture(broadcast, catalog, options).await?` is -`let (publication, driver) = encode::publish_capture(broadcast, catalog, options)?;`, -then `driver.run().await`. `Publication` reports the capture's `State`, whose -`Status` is `Waiting`, `Starting`, `Live`, `Failed(Arc)`, `Ended`, or -`Cancelled`. `Driver::run` returns `Result<(), Arc>`, sharing the error -its `Status::Failed` carries. - -### `encode::Producer::new` takes a track - -`encode::Producer::new(broadcast, reserved, rendition)` is -`new(track, reserved, rendition)`, and `with_track` is gone. Create the track -with `broadcast.unique_track(".avc3", reserved.track_info(PRIORITY.video))?` -(`.hev1` for H.265). - -### `encode::Applied::cut` reports cut support - -Read `encoder.applied().cut` instead of matching `Error::CutUnsupported`. - -### `render::Exporter::new` returns the encoder config - -`render::Exporter::new(&device, &queue, &config)` takes `&encode::Config` and -returns `(Exporter, encode::Config, VideoConfig)`. Open the encoder with the -returned config. - ### Consumer decoder setup refusals are typed `decode::Consumer::new` reports `Error::UnsupportedConfig` for a codec or description this build cannot decode, where `decode::Decoder::new` reports `Error::UnsupportedCodec`. That now includes a missing or malformed avcC or hvcC description (previously `Error::Codec` or `Error::Mux`) and a codec no compiled-in decoder handles (previously `Error::NoDecoder`). A stream every decoder for its codec refuses because the device cannot decode it, such as AV1 on a GPU without AV1 decode or a size past its limits, is a refusal too. `Error::NoDecoder` now means that a decoder for the codec failed to open for another reason, such as a missing driver or no device. Backend faults, scale-hint errors, and subscription failures keep their own errors. A codec worker that cannot start or exits during initialization reports `Error::CodecGone`, like a worker lost after initialization, rather than `Error::Codec`. diff --git a/rs/moq-video/src/capture/avfoundation.rs b/rs/moq-video/src/capture/avfoundation.rs index cf7a1415250..b8ff00f4522 100644 --- a/rs/moq-video/src/capture/avfoundation.rs +++ b/rs/moq-video/src/capture/avfoundation.rs @@ -44,7 +44,7 @@ const ACCESS_TIMEOUT: Duration = Duration::from_secs(60); /// List the cameras. Unlike capture, enumeration needs no TCC grant: an /// unauthorized process still sees the devices, just not their frames. pub(super) fn cameras() -> Result, Error> { - let media = unsafe { AVMediaTypeVideo }.ok_or_else(|| Error::Codec(anyhow::anyhow!("AVMediaTypeVideo")))?; + let media = unsafe { AVMediaTypeVideo }.ok_or_else(|| Error::codec(anyhow::anyhow!("AVMediaTypeVideo")))?; #[expect( deprecated, reason = "its replacement needs device-type constants that are macOS 14+, which would drop cameras on older systems" @@ -61,7 +61,7 @@ pub(super) fn cameras() -> Result, Error> { /// Open the default (or requested) camera and stream its frames. pub(super) async fn open(config: &Config, device: Option<&str>) -> Result { - let media = unsafe { AVMediaTypeVideo }.ok_or_else(|| Error::Codec(anyhow::anyhow!("AVMediaTypeVideo")))?; + let media = unsafe { AVMediaTypeVideo }.ok_or_else(|| Error::codec(anyhow::anyhow!("AVMediaTypeVideo")))?; // Gate on camera authorization before opening the device, so an unauthorized // client gets a clear error (and a prompt on first run) instead of a silent @@ -87,7 +87,7 @@ pub(super) async fn open(config: &Config, device: Option<&str>) -> Result) -> Result) -> Result frame, Ok(Err(error)) => return Err(error), Ok(Ok(None)) | Err(_) => { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "no frames from camera {device_id} within {FIRST_FRAME_TIMEOUT:?} (permission denied?)" ))); } diff --git a/rs/moq-video/src/capture/mediafoundation.rs b/rs/moq-video/src/capture/mediafoundation.rs index d8232b986c9..128a80a0c8b 100644 --- a/rs/moq-video/src/capture/mediafoundation.rs +++ b/rs/moq-video/src/capture/mediafoundation.rs @@ -176,7 +176,7 @@ impl Camera { let (width, height) = unpack_2x32(frame_size); // I420 chroma is 2x2 subsampled, so the encoder needs even dimensions. if width % 2 != 0 || height % 2 != 0 { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "camera resolution {width}x{height} must be even for H.264 encoding" ))); } @@ -324,7 +324,7 @@ fn create_attributes(capacity: u32) -> Result { unsafe { MFCreateAttributes(&mut attrs, capacity).map_err(|e| mf_err("create attributes", e))?; } - attrs.ok_or_else(|| Error::Codec(anyhow::anyhow!("MFCreateAttributes returned null"))) + attrs.ok_or_else(|| Error::codec(anyhow::anyhow!("MFCreateAttributes returned null"))) } /// Which device to open. diff --git a/rs/moq-video/src/capture/mod.rs b/rs/moq-video/src/capture/mod.rs index b7092e5277d..1d60425ead4 100644 --- a/rs/moq-video/src/capture/mod.rs +++ b/rs/moq-video/src/capture/mod.rs @@ -710,7 +710,7 @@ where { tokio::task::spawn_blocking(f) .await - .map_err(|err| Error::Codec(anyhow::anyhow!("capture enumeration thread failed: {err}")))? + .map_err(|err| Error::codec(anyhow::anyhow!("capture enumeration thread failed: {err}")))? } #[cfg(test)] diff --git a/rs/moq-video/src/capture/pipewire.rs b/rs/moq-video/src/capture/pipewire.rs index 7ee53e22598..eddfb006340 100644 --- a/rs/moq-video/src/capture/pipewire.rs +++ b/rs/moq-video/src/capture/pipewire.rs @@ -60,7 +60,7 @@ const FORMAT_TIMEOUT: Duration = Duration::from_secs(10); const FIRST_FRAME_TIMEOUT: Duration = Duration::from_secs(5); fn err(ctx: &str, e: impl std::fmt::Display) -> Error { - Error::Codec(anyhow::anyhow!("{ctx}: {e}")) + Error::codec(anyhow::anyhow!("{ctx}: {e}")) } pub(super) mod camera; @@ -178,12 +178,12 @@ async fn start(config: &Config, capture: Capture, session: Option) let geo = match kio::clock::timeout(FORMAT_TIMEOUT, geo_rx).await { Ok(Ok(result)) => result?, Ok(Err(_)) => { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "{kind:?} capture thread exited before negotiating a format" ))); } Err(_) => { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "no video format from {} within {FORMAT_TIMEOUT:?}", kind.producer() ))); @@ -194,7 +194,7 @@ async fn start(config: &Config, capture: Capture, session: Option) Ok(Ok(Some(frame))) => frame, Ok(Err(error)) => return Err(error), Ok(Ok(None)) | Err(_) => { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "no frames from {} within {FIRST_FRAME_TIMEOUT:?}", kind.producer() ))); @@ -290,7 +290,7 @@ async fn portal_negotiate(cursor: bool, selection: &portal::Selection) -> Result let stream = response .streams() .first() - .ok_or_else(|| Error::Codec(anyhow::anyhow!("portal granted no streams")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("portal granted no streams")))?; let node_id = stream.pipe_wire_node_id(); let fd = proxy @@ -429,23 +429,23 @@ impl DmaBufFrame for PipeWireDmaBuf { // DRM_FORMAT_MOD_LINEAR is zero. A tiled allocation cannot be interpreted // as strided rows; its fallback needs a GPU/VPP download instead. if self.modifier != 0 { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "cannot download DMA-BUF modifier {:#x} as linear rows", self.modifier ))); } wait_dma_buf_readable(self.fd.as_fd()) - .map_err(|e| Error::Codec(anyhow::anyhow!("waiting for DMA-BUF producer: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("waiting for DMA-BUF producer: {e}")))?; with_dma_buf_read(&self.fd, || { let allocation_size = self .allocation_size - .ok_or_else(|| Error::Codec(anyhow::anyhow!("DMA-BUF descriptor does not report a mappable size")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("DMA-BUF descriptor does not report a mappable size")))?; let mapping = Mapping::new(&self.fd, self.map_offset, allocation_size)?; let data = mapping .as_slice() .get(self.data_offset..) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("DMA-BUF chunk starts outside its allocation")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("DMA-BUF chunk starts outside its allocation")))?; match self.format { DrmFormat::NV12 => { @@ -465,7 +465,7 @@ impl DmaBufFrame for PipeWireDmaBuf { self.layout.stride, crate::Size::new(self.layout.width, self.layout.height), ), - other => Err(Error::Codec(anyhow::anyhow!( + other => Err(Error::codec(anyhow::anyhow!( "cannot download DMA-BUF format {:#x}", other.as_raw() ))), @@ -538,7 +538,7 @@ fn dma_buf_sync(fd: &OwnedFd, flags: u64) -> Result<(), Error> { } let err = std::io::Error::last_os_error(); if err.kind() != std::io::ErrorKind::Interrupted { - return Err(Error::Codec(anyhow::anyhow!("DMA-BUF sync: {err}"))); + return Err(Error::codec(anyhow::anyhow!("DMA-BUF sync: {err}"))); } } } @@ -587,7 +587,7 @@ struct Mapping { impl Mapping { fn new(fd: &OwnedFd, offset: u32, len: usize) -> Result { if len == 0 { - return Err(Error::Codec(anyhow::anyhow!("cannot map an empty DMA-BUF"))); + return Err(Error::codec(anyhow::anyhow!("cannot map an empty DMA-BUF"))); } // SAFETY: `fd` stays open for the mapping's lifetime, `len` is non-zero, // and the kernel validates the PipeWire-provided offset and allocation. @@ -602,7 +602,7 @@ impl Mapping { ) }; if ptr == libc::MAP_FAILED { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "DMA-BUF mmap: {}", std::io::Error::last_os_error() ))); @@ -636,24 +636,24 @@ fn nv12_to_i420(data: &[u8], layout: FrameLayout) -> Result { layout.source_height as usize, ); if source_height < height { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "NV12 source is shorter than the cropped output" ))); } let y_len = stride .checked_mul(source_height) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("NV12 luma size overflow")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("NV12 luma size overflow")))?; let uv_rows = height / 2; let uv_len = uv_rows .checked_sub(1) .and_then(|rows| rows.checked_mul(stride)) .and_then(|offset| offset.checked_add(width)) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("NV12 chroma size overflow")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("NV12 chroma size overflow")))?; let frame_len = y_len .checked_add(uv_len) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("NV12 frame size overflow")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("NV12 frame size overflow")))?; if stride < width || data.len() < frame_len { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "NV12 frame is shorter than its declared rows" ))); } @@ -1265,7 +1265,7 @@ fn run_loop(args: CaptureLoop) -> Result<(), Error> { .iter() .map(|offer| { spa::pod::Pod::from_bytes(offer) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("failed to build pipewire format offer"))) + .ok_or_else(|| Error::codec(anyhow::anyhow!("failed to build pipewire format offer"))) }) .collect::, _>>()?; stream @@ -1445,7 +1445,7 @@ fn color_from_pipewire(range: u32, matrix: u32, size: Size) -> Result Range::Limited, spa::sys::SPA_VIDEO_COLOR_RANGE_0_255 => Range::Full, _ => { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "unsupported PipeWire YUV color range {range}" ))); } @@ -1455,7 +1455,7 @@ fn color_from_pipewire(range: u32, matrix: u32, size: Size) -> Result Color::BT709, spa::sys::SPA_VIDEO_COLOR_MATRIX_BT601 => Color::BT601, _ => { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "unsupported PipeWire YUV color matrix {matrix}" ))); } @@ -1494,7 +1494,7 @@ fn validate_pipewire_description(kind: Kind, color: Color, primaries: u32, trans spa::sys::SPA_VIDEO_COLOR_PRIMARIES_SMPTE170M | spa::sys::SPA_VIDEO_COLOR_PRIMARIES_BT709 )); if !primaries_match { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "PipeWire YUV primaries {primaries} do not match the negotiated matrix" ))); } @@ -1505,7 +1505,7 @@ fn validate_pipewire_description(kind: Kind, color: Color, primaries: u32, trans | SPA_VIDEO_TRANSFER_BT601 | SPA_VIDEO_TRANSFER_BT2020_10 ) { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "unsupported PipeWire YUV transfer function {transfer}" ))); } @@ -1543,7 +1543,7 @@ fn convert(format: VideoFormat, bytes: &[u8], layout: FrameLayout, color: Option } // Only a camera negotiates MJPEG, which the format records as `Encoded`. VideoFormat::Encoded => I420::from_mjpeg(bytes, crate::Size::new(layout.width, layout.height)), - other => Err(Error::Codec(anyhow::anyhow!( + other => Err(Error::codec(anyhow::anyhow!( "pipewire negotiated an unsupported video format {other:?}" ))), } diff --git a/rs/moq-video/src/capture/pipewire/camera.rs b/rs/moq-video/src/capture/pipewire/camera.rs index 5988686af7b..d6872c298e7 100644 --- a/rs/moq-video/src/capture/pipewire/camera.rs +++ b/rs/moq-video/src/capture/pipewire/camera.rs @@ -216,7 +216,7 @@ pub(super) fn choose( let node = resolve(&nodes, name)?; let formats = formats(mainloop, core, ®istry, node)?; let selected = select(&formats, want).ok_or_else(|| { - Error::Codec(anyhow::anyhow!( + Error::codec(anyhow::anyhow!( "PipeWire camera {} offers no YUY2, NV12, RGB, or MJPEG mode", node.name )) @@ -317,7 +317,7 @@ fn roundtrip(mainloop: &pw::main_loop::MainLoopRc, core: &pw::core::CoreRc) -> R mainloop.run(); let outcome = outcome.borrow_mut().take(); outcome.unwrap_or_else(|| { - Err(Error::Codec(anyhow::anyhow!( + Err(Error::codec(anyhow::anyhow!( "PipeWire did not answer within {QUERY_TIMEOUT:?}" ))) }) @@ -646,7 +646,7 @@ fn formats_each( let Query { formats, failure } = query.take(); match failure { // Formats that were read still describe the node. - Some(failure) if formats.is_empty() => Err(Error::Codec(anyhow::anyhow!( + Some(failure) if formats.is_empty() => Err(Error::codec(anyhow::anyhow!( "PipeWire camera {} formats: {failure}", node.name ))), diff --git a/rs/moq-video/src/capture/pump.rs b/rs/moq-video/src/capture/pump.rs index fa4d7e12f1e..636bd9e2c24 100644 --- a/rs/moq-video/src/capture/pump.rs +++ b/rs/moq-video/src/capture/pump.rs @@ -127,7 +127,7 @@ where match geo_rx.await { Ok(Ok(geometry)) => Ok((geometry, guard)), Ok(Err(err)) => Err(err), - Err(_) => Err(Error::Codec(anyhow::anyhow!( + Err(_) => Err(Error::codec(anyhow::anyhow!( "capture thread exited before reporting geometry" ))), } diff --git a/rs/moq-video/src/capture/screencapture.rs b/rs/moq-video/src/capture/screencapture.rs index 17c75cccf4b..c8e33acb592 100644 --- a/rs/moq-video/src/capture/screencapture.rs +++ b/rs/moq-video/src/capture/screencapture.rs @@ -130,7 +130,7 @@ async fn ensure_screen_access() -> Result<(), Error> { let granted = tokio::task::spawn_blocking(|| objc2_core_graphics::CGRequestScreenCaptureAccess()) .await - .map_err(|error| Error::Codec(anyhow::anyhow!("screen permission request failed: {error}")))?; + .map_err(|error| Error::codec(anyhow::anyhow!("screen permission request failed: {error}")))?; if granted { Ok(()) } else { @@ -176,7 +176,7 @@ async fn open(config: &Config, filter: &SCContentFilter, size: (u32, u32)) -> Re let proto = ProtocolObject::from_ref(&*delegate); stream .addStreamOutput_type_sampleHandlerQueue_error(proto, SCStreamOutputType::Screen, Some(&dispatch)) - .map_err(|e| Error::Codec(anyhow::anyhow!("add screen output: {e:?}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("add screen output: {e:?}")))?; } start_capture(&stream).await?; @@ -195,7 +195,7 @@ async fn open(config: &Config, filter: &SCContentFilter, size: (u32, u32)) -> Re Ok(Ok(Some(frame))) => frame, Ok(Err(error)) => return Err(error), Ok(Ok(None)) | Err(_) => { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "no frames from {label} within {FIRST_FRAME_TIMEOUT:?} (screen recording permission?)" ))); } @@ -295,7 +295,7 @@ fn find_display(content: &SCShareableContent, device: Option<&str>) -> Result() - .map_err(|_| Error::Codec(anyhow::anyhow!("invalid display selector {spec:?}")))?, + .map_err(|_| Error::codec(anyhow::anyhow!("invalid display selector {spec:?}")))?, }; if index >= displays.count() { return Err(Error::SourceUnavailable(format!("no display at index {index}"))); @@ -307,7 +307,7 @@ fn find_display(content: &SCShareableContent, device: Option<&str>) -> Result Result, Error> { let wanted: u32 = id .parse() - .map_err(|_| Error::Codec(anyhow::anyhow!("invalid window id {id:?}")))?; + .map_err(|_| Error::codec(anyhow::anyhow!("invalid window id {id:?}")))?; let windows = unsafe { content.windows() }; (0..windows.count()) @@ -378,9 +378,9 @@ async fn shareable_content() -> Result, Error> { match kio::clock::timeout(ASYNC_TIMEOUT, rx).await { Ok(Ok(Ok(content))) => Ok(content.0), - Ok(Ok(Err(msg))) => Err(Error::Codec(anyhow::anyhow!("shareable content: {msg}"))), - Ok(Err(_)) => Err(Error::Codec(anyhow::anyhow!("shareable content handler dropped"))), - Err(_) => Err(Error::Codec(anyhow::anyhow!( + Ok(Ok(Err(msg))) => Err(Error::codec(anyhow::anyhow!("shareable content: {msg}"))), + Ok(Err(_)) => Err(Error::codec(anyhow::anyhow!("shareable content handler dropped"))), + Err(_) => Err(Error::codec(anyhow::anyhow!( "timed out listing shareable content (screen recording permission?)" ))), } @@ -400,9 +400,9 @@ async fn start_capture(stream: &SCStream) -> Result<(), Error> { match kio::clock::timeout(ASYNC_TIMEOUT, rx).await { Ok(Ok(None)) => Ok(()), - Ok(Ok(Some(msg))) => Err(Error::Codec(anyhow::anyhow!("start capture: {msg}"))), - Ok(Err(_)) => Err(Error::Codec(anyhow::anyhow!("start-capture handler dropped"))), - Err(_) => Err(Error::Codec(anyhow::anyhow!("timed out starting screen capture"))), + Ok(Ok(Some(msg))) => Err(Error::codec(anyhow::anyhow!("start capture: {msg}"))), + Ok(Err(_)) => Err(Error::codec(anyhow::anyhow!("start-capture handler dropped"))), + Err(_) => Err(Error::codec(anyhow::anyhow!("timed out starting screen capture"))), } } diff --git a/rs/moq-video/src/capture/v4l2.rs b/rs/moq-video/src/capture/v4l2.rs index 8978c46f08f..68a7adefc53 100644 --- a/rs/moq-video/src/capture/v4l2.rs +++ b/rs/moq-video/src/capture/v4l2.rs @@ -101,7 +101,7 @@ fn convertible_with(mut format: impl FnMut(u32) -> Result, Error> None => return Ok(false), } } - Err(Error::Codec(anyhow::anyhow!("V4L2 format index overflow"))) + Err(Error::codec(anyhow::anyhow!("V4L2 format index overflow"))) } /// List the modes a V4L2 camera reports, for the formats this backend converts. @@ -207,9 +207,9 @@ fn frame_sizes(device: &Device, fourcc: FourCC) -> Result, Er if enumeration(result)?.is_none() { return Ok(sizes); } - sizes.push(FrameSizeEnum::try_from(entry).map_err(|e| Error::Codec(anyhow::anyhow!(e)))?); + sizes.push(FrameSizeEnum::try_from(entry).map_err(|e| Error::codec(anyhow::anyhow!(e)))?); } - Err(Error::Codec(anyhow::anyhow!("V4L2 frame size index overflow"))) + Err(Error::codec(anyhow::anyhow!("V4L2 frame size index overflow"))) } fn enumeration(result: std::io::Result) -> Result, Error> { @@ -241,17 +241,17 @@ fn framerates(device: &Device, fourcc: FourCC, size: Size) -> Result Result { FrameRate::new(interval.denominator, interval.numerator) - .map_err(|error| Error::Codec(anyhow::anyhow!("invalid V4L2 frame interval: {error}"))) + .map_err(|error| Error::codec(anyhow::anyhow!("invalid V4L2 frame interval: {error}"))) } /// Open a V4L2 camera and stream its frames over a pump thread. @@ -367,7 +367,7 @@ impl Camera { // The stream owns a clone of the device's `Arc`, so the fd stays // open after `device` drops here; the mmap'd buffers live with the stream. let mut stream = MmapStream::with_buffers(&device, BufType::VideoCapture, BUFFER_COUNT) - .map_err(|e| Error::Codec(anyhow::anyhow!("V4L2 stream init: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("V4L2 stream init: {e}")))?; stream.set_timeout(READ_TIMEOUT); tracing::info!(device = %name, width, height, "opened V4L2 capture"); @@ -512,13 +512,13 @@ fn negotiate_with( let Some((best, source, _)) = closest(replies, want) else { if offered.is_empty() { let Some(error) = probe_error else { - return Err(Error::Codec(anyhow::anyhow!("camera {name} has no formats to probe"))); + return Err(Error::codec(anyhow::anyhow!("camera {name} has no formats to probe"))); }; return Err(error); } let offered = offered.join(", "); let wanted = Source::ALL.map(|source| source.fourcc().to_string()).join(", "); - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "camera {name} has no encodable {wanted} mode (the driver returned {offered})" ))); }; @@ -526,7 +526,7 @@ fn negotiate_with( // A successful probe may have left the device on another candidate. let (applied, rate) = apply(Format::new(best.width, best.height, best.fourcc))?; if applied.fourcc != best.fourcc || applied.width != best.width || applied.height != best.height { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "camera {name} would not re-apply the {}x{} {} mode it just negotiated", best.width, best.height, @@ -549,7 +549,7 @@ fn closest( } fn set_format(device: &Device, format: Format) -> Result { - Capture::set_format(device, &format).map_err(|e| Error::Codec(anyhow::anyhow!("V4L2 set format: {e}"))) + Capture::set_format(device, &format).map_err(|e| Error::codec(anyhow::anyhow!("V4L2 set format: {e}"))) } #[cfg(test)] @@ -745,7 +745,7 @@ mod tests { calls += 1; match calls { 1 => Ok((Format::new(640, 480, Source::Yuyv.fourcc()), None)), - 2 => Err(Error::Codec(anyhow::anyhow!("MJPEG is unsupported"))), + 2 => Err(Error::codec(anyhow::anyhow!("MJPEG is unsupported"))), 3 => Ok((requested, None)), _ => panic!("unexpected format probe"), } @@ -763,7 +763,7 @@ mod tests { let mut calls = 0; let error = negotiate_with("camera", request(640, 480), |_| { calls += 1; - Err(Error::Codec(anyhow::anyhow!("probe {calls} failed"))) + Err(Error::codec(anyhow::anyhow!("probe {calls} failed"))) }) .expect_err("no format probe succeeded"); diff --git a/rs/moq-video/src/capture/wgc/native.rs b/rs/moq-video/src/capture/wgc/native.rs index cbd25a4158e..c54b2ead4d2 100644 --- a/rs/moq-video/src/capture/wgc/native.rs +++ b/rs/moq-video/src/capture/wgc/native.rs @@ -58,11 +58,11 @@ pub(in crate::capture) async fn open(config: &Config) -> Result { match result { Ok(Ok(())) => chan.close(), Ok(Err(error)) => chan.fail(error), - Err(_) => chan.fail(Error::Codec(anyhow::anyhow!("WGC capture thread panicked"))), + Err(_) => chan.fail(Error::codec(anyhow::anyhow!("WGC capture thread panicked"))), } } }) - .map_err(|error| Error::Codec(anyhow::anyhow!("start WGC thread: {error}")))?; + .map_err(|error| Error::codec(anyhow::anyhow!("start WGC thread: {error}")))?; let guard = Guard { signal, handle: Some(handle), @@ -356,7 +356,7 @@ impl Capture { .map_err(|e| error("read WGC timestamp", e))? .Duration; let micros = - u64::try_from(ticks).map_err(|_| Error::Codec(anyhow::anyhow!("negative WGC timestamp")))? / 10; + u64::try_from(ticks).map_err(|_| Error::codec(anyhow::anyhow!("negative WGC timestamp")))? / 10; let timestamp = moq_net::Timestamp::from_micros(micros)?; let surface = frame.0.Surface().map_err(|e| error("read WGC surface", e))?; let access: IDirect3DDxgiInterfaceAccess = surface.cast().map_err(|e| error("query WGC surface", e))?; @@ -584,7 +584,7 @@ fn error(context: &str, error: windows::core::Error) -> Error { if error.code() == E_ACCESSDENIED { Error::PermissionDenied(format!("{context}: {error}")) } else { - Error::Codec(anyhow::anyhow!("{context}: {error}")) + Error::codec(anyhow::anyhow!("{context}: {error}")) } } diff --git a/rs/moq-video/src/capture/x11.rs b/rs/moq-video/src/capture/x11.rs index 52ca675934b..2d15b334976 100644 --- a/rs/moq-video/src/capture/x11.rs +++ b/rs/moq-video/src/capture/x11.rs @@ -345,9 +345,9 @@ impl Capture { Settled::Changed => return Ok(pump::Read::Done), } - let width = u16::try_from(self.width).map_err(|_| Error::Codec(anyhow::anyhow!("X11 width is too large")))?; + let width = u16::try_from(self.width).map_err(|_| Error::codec(anyhow::anyhow!("X11 width is too large")))?; let height = - u16::try_from(self.height).map_err(|_| Error::Codec(anyhow::anyhow!("X11 height is too large")))?; + u16::try_from(self.height).map_err(|_| Error::codec(anyhow::anyhow!("X11 height is too large")))?; if let Err(error) = self.capture_image(width, height) { // A resize can land after the state check but before the image request. // The reply has queued the preceding events; only a confirmed source @@ -824,9 +824,9 @@ impl PixelFormat { .flat_map(|screen| &screen.allowed_depths) .flat_map(|depth| &depth.visuals) .find(|visual| visual.visual_id == visual_id) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("X11 root visual is missing")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("X11 root visual is missing")))?; if visual.class != VisualClass::TRUE_COLOR { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "unsupported X11 visual class: {:?}", visual.class ))); @@ -835,7 +835,7 @@ impl PixelFormat { .pixmap_formats .iter() .find(|format| format.depth == depth) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("X11 pixel format for depth {depth} is missing")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("X11 pixel format for depth {depth} is missing")))?; Ok(Self { byte_order: setup.image_byte_order, bits_per_pixel: format.bits_per_pixel, @@ -872,7 +872,7 @@ impl PixelFormat { let stride = self.stride(width)?; let required = self.required_len(width, height)?; if data.len() < required { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "short X11 frame: got {} bytes, need {required}", data.len() ))); @@ -957,7 +957,7 @@ fn wm_class(value: &str) -> String { } fn codec(error: impl std::fmt::Display) -> Error { - Error::Codec(anyhow::anyhow!("X11: {error}")) + Error::codec(anyhow::anyhow!("X11: {error}")) } fn source(error: impl std::fmt::Display) -> Error { diff --git a/rs/moq-video/src/convert.rs b/rs/moq-video/src/convert.rs index e2a774edb4b..b9be6fee1fd 100644 --- a/rs/moq-video/src/convert.rs +++ b/rs/moq-video/src/convert.rs @@ -157,22 +157,22 @@ fn packed(surface: &Surface, config: &Config, convert: Convert, name: &str) -> R .unwrap_or_else(|| Color::infer(size)); let i420 = surface.to_i420()?; let luma = usize::try_from(size.pixels()).map_err(|_| { - Error::Codec(anyhow::anyhow!( + Error::codec(anyhow::anyhow!( "{name} frame {size}: dimensions too large to represent" )) })?; let stride = size.width.checked_mul(4).ok_or_else(|| { - Error::Codec(anyhow::anyhow!( + Error::codec(anyhow::anyhow!( "{name} frame {size}: row stride is too large to represent" )) })?; let stride_usize = usize::try_from(stride).map_err(|_| { - Error::Codec(anyhow::anyhow!( + Error::codec(anyhow::anyhow!( "{name} frame {size}: row stride is too large to represent" )) })?; let len = stride_usize.checked_mul(size.height as usize).ok_or_else(|| { - Error::Codec(anyhow::anyhow!( + Error::codec(anyhow::anyhow!( "{name} frame {size}: byte length is too large to represent" )) })?; @@ -190,7 +190,7 @@ fn packed(surface: &Surface, config: &Config, convert: Convert, name: &str) -> R let mut data = vec![0; len]; let (range, matrix) = color.yuv(); convert(&planar, &mut data, stride, range, matrix) - .map_err(|e| Error::Codec(anyhow::anyhow!("{name} conversion failed for {size}: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("{name} conversion failed for {size}: {e}")))?; Ok(Packed { size, diff --git a/rs/moq-video/src/decode/backend/mediacodec.rs b/rs/moq-video/src/decode/backend/mediacodec.rs index 7aa475e53d5..aa182d25a1a 100644 --- a/rs/moq-video/src/decode/backend/mediacodec.rs +++ b/rs/moq-video/src/decode/backend/mediacodec.rs @@ -187,7 +187,7 @@ impl MediaCodec { Codec::H265 => MIME_H265, Codec::Av1 => MIME_AV1, other => { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "MediaCodec {} decode is not wired", other.label() ))); @@ -222,7 +222,7 @@ impl MediaCodec { let surface = reader.window().map_err(|e| reader_err("get the reader's window", e))?; let decoder = media_codec::MediaCodec::from_decoder_type(mime) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("no MediaCodec decoder for {mime}")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("no MediaCodec decoder for {mime}")))?; let format = decoder_format(mime, width, height); decoder .configure(&format, Some(&surface), MediaCodecDirection::Decoder) @@ -258,7 +258,7 @@ impl MediaCodec { DequeuedInputBufferResult::Buffer(mut buffer) => { let target = buffer.buffer_mut(); if target.len() < access_unit.len() { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "MediaCodec input buffer is {} bytes, needs {} for this access unit", target.len(), access_unit.len() @@ -293,7 +293,7 @@ impl MediaCodec { self.drain(OUTPUT_TIMEOUT, out)?; } - Err(Error::Codec(anyhow::anyhow!( + Err(Error::codec(anyhow::anyhow!( "MediaCodec never freed an input buffer for an access unit" ))) } @@ -405,7 +405,7 @@ impl MediaCodec { } } if !ended { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "MediaCodec did not reach end of stream within {:?}", DRAIN_TIMEOUT * DRAIN_ROUNDS ))); @@ -455,7 +455,7 @@ impl MediaCodec { self.drain(OUTPUT_TIMEOUT, out)?; } - Err(Error::Codec(anyhow::anyhow!( + Err(Error::codec(anyhow::anyhow!( "MediaCodec never freed an input buffer for the end of stream" ))) } @@ -549,7 +549,7 @@ fn image_size(image: &Image) -> Result<(u32, u32, u32, u32), Error> { // with its last row or column. let (w, h) = (w.max(0) as u32 & !1, h.max(0) as u32 & !1); if w == 0 || h == 0 { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "MediaCodec produced a {width}x{height} image, which is not a picture" ))); } @@ -558,12 +558,12 @@ fn image_size(image: &Image) -> Result<(u32, u32, u32, u32), Error> { /// Wrap an NDK media error from the codec, naming the call that produced it. fn codec_err(what: &str, error: MediaError) -> Error { - Error::Codec(anyhow::anyhow!("failed to {what} on the MediaCodec decoder: {error}")) + Error::codec(anyhow::anyhow!("failed to {what} on the MediaCodec decoder: {error}")) } /// Wrap an NDK media error from the reader, naming the call that produced it. fn reader_err(what: &str, error: MediaError) -> Error { - Error::Codec(anyhow::anyhow!( + Error::codec(anyhow::anyhow!( "failed to {what} on the decoder's ImageReader: {error}" )) } diff --git a/rs/moq-video/src/decode/backend/mediafoundation.rs b/rs/moq-video/src/decode/backend/mediafoundation.rs index a91288015a3..70c4ea0ad02 100644 --- a/rs/moq-video/src/decode/backend/mediafoundation.rs +++ b/rs/moq-video/src/decode/backend/mediafoundation.rs @@ -108,7 +108,7 @@ impl MediaFoundation { Codec::H264 => (MFVideoFormat_H264, "H.264"), Codec::H265 => (MFVideoFormat_HEVC, "H.265"), Codec::Av1 | Codec::Vp8 | Codec::Vp9 => { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "Media Foundation {} decode is not wired", codec.label() ))); @@ -192,7 +192,7 @@ impl MediaFoundation { let media = match unsafe { self.transform.GetOutputAvailableType(0, index) } { Ok(media) => media, Err(e) if e.code() == MF_E_NO_MORE_TYPES => { - return Err(Error::Codec(anyhow::anyhow!("decoder offers no NV12 output type"))); + return Err(Error::codec(anyhow::anyhow!("decoder offers no NV12 output type"))); } Err(e) => return Err(mf_err("GetOutputAvailableType", e)), }; @@ -239,7 +239,7 @@ impl MediaFoundation { // I420 chroma is 2x2 subsampled, so the download needs even dimensions. if width == 0 || height == 0 || width % 2 != 0 || height % 2 != 0 { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "decoder reported unusable frame size {width}x{height}" ))); } @@ -448,7 +448,7 @@ impl Backend for MediaFoundation { // it, and that is exactly the case where feeding the same access // unit again is the right move. if !self.drain_output(&mut out)? { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "decoder rejected the access unit with no output to drain" ))); } @@ -573,7 +573,7 @@ fn enumerate_decoder(subtype: GUID) -> Result { .map_err(|e| mf_err("MFTEnumEx", e))?; } if count == 0 { - return Err(Error::Codec(anyhow::anyhow!("no decoder MFT found"))); + return Err(Error::codec(anyhow::anyhow!("no decoder MFT found"))); } let entries = unsafe { std::slice::from_raw_parts_mut(activates, count as usize) }; @@ -590,7 +590,7 @@ fn enumerate_decoder(subtype: GUID) -> Result { windows::Win32::System::Com::CoTaskMemFree(Some(activates as *const c_void)); } - transform.ok_or_else(|| Error::Codec(anyhow::anyhow!("failed to activate decoder MFT"))) + transform.ok_or_else(|| Error::codec(anyhow::anyhow!("failed to activate decoder MFT"))) } #[cfg(test)] diff --git a/rs/moq-video/src/decode/backend/mod.rs b/rs/moq-video/src/decode/backend/mod.rs index 25fba7c66cb..446e89fd502 100644 --- a/rs/moq-video/src/decode/backend/mod.rs +++ b/rs/moq-video/src/decode/backend/mod.rs @@ -470,7 +470,7 @@ mod tests { const DRIVERLESS: Candidate = Candidate { name: "driverless", supports: |c| matches!(c, Codec::H264), - open: |_, _| Err(Error::Codec(anyhow::anyhow!("driver libraries not found"))), + open: |_, _| Err(Error::codec(anyhow::anyhow!("driver libraries not found"))), }; #[tracing_test::traced_test] @@ -501,7 +501,7 @@ mod tests { const H265_ONLY: Candidate = Candidate { name: "driverless", supports: |c| matches!(c, Codec::H265), - open: |_, _| Err(Error::Codec(anyhow::anyhow!("driver libraries not found"))), + open: |_, _| Err(Error::codec(anyhow::anyhow!("driver libraries not found"))), }; let attempts = vec![Attempt::hardware(&H265_ONLY), Attempt::software(&WORKING)]; diff --git a/rs/moq-video/src/decode/backend/nvdec.rs b/rs/moq-video/src/decode/backend/nvdec.rs index 71a4ad6727e..2d891cf0808 100644 --- a/rs/moq-video/src/decode/backend/nvdec.rs +++ b/rs/moq-video/src/decode/backend/nvdec.rs @@ -44,7 +44,7 @@ use crate::{Error, Frame}; pub(crate) const NAME: &str = "nvdec"; fn codec_err(msg: String) -> Error { - Error::Codec(anyhow::anyhow!(msg)) + Error::codec(anyhow::anyhow!(msg)) } pub(crate) struct Nvdec { diff --git a/rs/moq-video/src/decode/backend/openh264.rs b/rs/moq-video/src/decode/backend/openh264.rs index 989ddf7e6b5..2b321da6942 100644 --- a/rs/moq-video/src/decode/backend/openh264.rs +++ b/rs/moq-video/src/decode/backend/openh264.rs @@ -103,7 +103,7 @@ pub(crate) struct Openh264 { fn new_decoder() -> Result { let config = DecoderConfig::new().flush_after_decode(Flush::NoFlush); Decoder::with_api_config(OpenH264API::from_source(), config) - .map_err(|e| Error::Codec(anyhow::anyhow!("openh264 decoder init: {e}"))) + .map_err(|e| Error::codec(anyhow::anyhow!("openh264 decoder init: {e}"))) } impl Openh264 { @@ -118,7 +118,7 @@ impl Openh264 { /// drain it through a method the [`Backend`] trait does not carry. fn new(codec: Codec, _config: &Config) -> Result { if codec != Codec::H264 { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "openh264 cannot decode {}", codec.label() ))); @@ -156,7 +156,7 @@ impl Openh264 { fn picture_lost(&mut self, timestamp: Timestamp, err: &openh264::Error) -> Result, Error> { let state = i32::try_from(err.native_code()).unwrap_or(dsErrorFree); if state == dsErrorFree || state & !PICTURE_LOST != 0 { - return Err(Error::Codec(anyhow::anyhow!("openh264 decode: {err}"))); + return Err(Error::codec(anyhow::anyhow!("openh264 decode: {err}"))); } if state & DECODER_RESET != 0 { @@ -219,7 +219,7 @@ impl Openh264 { let tail = self .decoder .flush_remaining() - .map_err(|e| Error::Codec(anyhow::anyhow!("openh264 flush: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("openh264 flush: {e}")))?; // `tail` borrows the decoder, so copy the pictures before taking their // timestamps. The placeholder is replaced before any frame reaches a // caller; an unstamped picture is dropped below. @@ -255,7 +255,7 @@ impl Openh264 { fn picture(yuv: &impl YUVSource, timestamp: Timestamp) -> Result { let (width, height) = yuv.dimensions(); if width % 2 != 0 || height % 2 != 0 { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "decoded frame has odd dimensions {width}x{height}, expected 4:2:0" ))); } diff --git a/rs/moq-video/src/decode/backend/v4l2.rs b/rs/moq-video/src/decode/backend/v4l2.rs index 059d678f562..f9dd33dd485 100644 --- a/rs/moq-video/src/decode/backend/v4l2.rs +++ b/rs/moq-video/src/decode/backend/v4l2.rs @@ -163,7 +163,7 @@ impl V4l2 { }, )?; if coded.pixelformat != v4l2::H264 { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "V4L2 decoder answered an H264 request with {}", v4l2::name(coded.pixelformat) ))); @@ -197,7 +197,7 @@ impl V4l2 { break index; } if Instant::now() >= deadline { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "V4L2 decoder held every input buffer for {BUFFER_TIMEOUT:?}" ))); } @@ -209,7 +209,7 @@ impl V4l2 { // The buffer goes back on the free list: losing one to a single oversized // access unit would shrink the pool for the rest of the stream. self.coded.reclaim(index); - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "access unit of {} bytes exceeds the V4L2 decoder's {capacity} byte buffer", access_unit.len() ))); @@ -310,7 +310,7 @@ impl V4l2 { let offered = self.device.formats(Dir::Capture)?; let Some(&pixelformat) = v4l2::RAW.iter().find(|code| offered.contains(code)) else { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "V4L2 decoder defaults to {} and offers no 8-bit 4:2:0 format for this stream", v4l2::name(format.pixelformat) ))); @@ -444,7 +444,7 @@ impl V4l2 { if reclaimed > 0 || frames.len() > before { deadline = Instant::now() + DRAIN_TIMEOUT; } else if Instant::now() >= deadline { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "V4L2 decoder did not finish its drain within {DRAIN_TIMEOUT:?}, holding {} access unit(s)", self.coded.outstanding() ))); @@ -569,7 +569,7 @@ impl Backend for V4l2 { if Instant::now() >= deadline { let waited = since.elapsed(); if waited >= SOURCE_CHANGE_LIMIT { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "V4L2 decoder did not report the stream's size within {waited:?}" ))); } diff --git a/rs/moq-video/src/decode/backend/vaapi.rs b/rs/moq-video/src/decode/backend/vaapi.rs index 5aabfa78541..943edf910e7 100644 --- a/rs/moq-video/src/decode/backend/vaapi.rs +++ b/rs/moq-video/src/decode/backend/vaapi.rs @@ -117,7 +117,7 @@ impl Vaapi { moq_vaapi::Codec::H265, Some((VAProfile::VAProfileHEVCMain, "H.265 Main".to_owned())), ), - other => return Err(Error::Codec(anyhow::anyhow!("VAAPI cannot decode {}", other.label()))), + other => return Err(Error::codec(anyhow::anyhow!("VAAPI cannot decode {}", other.label()))), }; if let Some((profile, name)) = required && offered(codec, profile) == Some(false) @@ -129,7 +129,7 @@ impl Vaapi { codec, device: vaapi::device(codec).map(Path::to_path_buf), }; - let decoder = Decoder::new(vaapi).map_err(|e| Error::Codec(anyhow::anyhow!("VAAPI decoder init: {e:?}")))?; + let decoder = Decoder::new(vaapi).map_err(|e| Error::codec(anyhow::anyhow!("VAAPI decoder init: {e:?}")))?; // The renderer imports the NV12 this exports, so `Render` keeps it too // in a build that has the renderer; without one it would only download. @@ -198,7 +198,7 @@ impl Vaapi { Ok(frames) } Err(err) if self.export == Export::Exporting => { - Err(Error::Codec(err.context("VAAPI decode to a shared surface"))) + Err(Error::codec(err.context("VAAPI decode to a shared surface"))) } Err(err) => { tracing::warn!(%err, "VAAPI cannot hand out decoded surfaces; downloading them instead"); @@ -221,7 +221,7 @@ impl Backend for Vaapi { let decoded = self .decoder .decode(&access_unit, timestamp) - .map_err(|e| Error::Codec(anyhow::anyhow!("VAAPI decode: {e:?}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("VAAPI decode: {e:?}")))?; convert(decoded) } @@ -235,7 +235,7 @@ impl Backend for Vaapi { let decoded = self .decoder .flush() - .map_err(|e| Error::Codec(anyhow::anyhow!("VAAPI flush: {e:?}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("VAAPI flush: {e:?}")))?; convert(decoded) } diff --git a/rs/moq-video/src/decode/backend/videotoolbox.rs b/rs/moq-video/src/decode/backend/videotoolbox.rs index e57518627f5..d1f24a1be6d 100644 --- a/rs/moq-video/src/decode/backend/videotoolbox.rs +++ b/rs/moq-video/src/decode/backend/videotoolbox.rs @@ -166,7 +166,7 @@ impl VideoToolbox { fn new(stream: &Stream) -> Result { let codec = stream.codec; if !matches!(codec, Codec::H264 | Codec::H265) { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "VideoToolbox {} decode is not wired", codec.label() ))); @@ -232,7 +232,7 @@ impl VideoToolbox { if let Some(old) = self.session.take() { let status = old.finish(); if status != 0 { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "VTDecompressionSessionWaitForAsynchronousFrames failed: {status}" ))); } @@ -269,7 +269,7 @@ impl VideoToolbox { self.codec.label() )) } else { - Error::Codec(anyhow::anyhow!("VTDecompressionSessionCreate failed: {status}")) + Error::codec(anyhow::anyhow!("VTDecompressionSessionCreate failed: {status}")) } })?; @@ -285,7 +285,7 @@ impl VideoToolbox { fn take_timestamp(&mut self, sample_time: i64) -> Result { let found = self.pending.iter().position(|(fed, _)| *fed == sample_time); let Some(index) = found else { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "decoder output did not match fed sample time {sample_time}" ))); }; @@ -331,7 +331,7 @@ impl Backend for VideoToolbox { .decode_frame(&sample, VTDecodeFrameFlags(0), ptr::null_mut(), ptr::null_mut()) }; if status != 0 { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "VTDecompressionSessionDecodeFrame failed: {status}" ))); } @@ -339,7 +339,7 @@ impl Backend for VideoToolbox { }; if let Some(error) = self.sink.error.take() { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "VideoToolbox decode callback failed: {error}" ))); } @@ -357,11 +357,11 @@ impl Backend for VideoToolbox { let status = self.session.take().map_or(0, Session::finish); let result = if status != 0 { - Err(Error::Codec(anyhow::anyhow!( + Err(Error::codec(anyhow::anyhow!( "VTDecompressionSessionWaitForAsynchronousFrames failed: {status}" ))) } else if let Some(error) = self.sink.error.take() { - Err(Error::Codec(anyhow::anyhow!( + Err(Error::codec(anyhow::anyhow!( "VideoToolbox drain callback failed: {error}" ))) } else { @@ -433,7 +433,7 @@ fn create_format_description(codec: Codec, params: &[Bytes]) -> Result> = params .iter() .map(|p| { - NonNull::new(p.as_ptr() as *mut u8).ok_or_else(|| Error::Codec(anyhow::anyhow!("empty parameter set"))) + NonNull::new(p.as_ptr() as *mut u8).ok_or_else(|| Error::codec(anyhow::anyhow!("empty parameter set"))) }) .collect::>()?; let sizes: Vec = params.iter().map(|p| p.len()).collect(); @@ -468,7 +468,7 @@ fn create_format_description(codec: Codec, params: &[Bytes]) -> Result { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "VideoToolbox {} decode is not wired", codec.label() ))); @@ -478,7 +478,7 @@ fn create_format_description(codec: Codec, params: &[Bytes]) -> Result Result, Error> { let format = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange as i32; let number = unsafe { CFNumber::new(None, CFNumberType::SInt32Type, &format as *const i32 as *const c_void) } - .ok_or_else(|| Error::Codec(anyhow::anyhow!("failed to build CFNumber")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("failed to build CFNumber")))?; let key = (unsafe { kCVPixelBufferPixelFormatTypeKey } as *const CFString).cast::(); let value = (number.as_ref() as *const CFNumber).cast::(); @@ -571,7 +571,7 @@ fn nv12_output_attributes() -> Result, Error> { &objc2_core_foundation::kCFTypeDictionaryValueCallBacks, ) } - .ok_or_else(|| Error::Codec(anyhow::anyhow!("failed to build NV12 attributes dictionary"))) + .ok_or_else(|| Error::codec(anyhow::anyhow!("failed to build NV12 attributes dictionary"))) } /// Classify a NAL by its header so the parameter sets can be split out. H.264 diff --git a/rs/moq-video/src/decode/backend/vpx.rs b/rs/moq-video/src/decode/backend/vpx.rs index 4375c1dcacf..1d3a71347af 100644 --- a/rs/moq-video/src/decode/backend/vpx.rs +++ b/rs/moq-video/src/decode/backend/vpx.rs @@ -61,7 +61,7 @@ impl Vpx { Codec::Vp8 => vpx_codec_vp8_dx(), Codec::Vp9 => vpx_codec_vp9_dx(), other => { - return Err(Error::Codec(anyhow::anyhow!("{NAME} cannot decode {}", other.label()))); + return Err(Error::codec(anyhow::anyhow!("{NAME} cannot decode {}", other.label()))); } } }; @@ -78,7 +78,7 @@ impl Vpx { // SAFETY: `ctx` is zeroed and outlives the call; libvpx copies `cfg`. let err = unsafe { vpx_codec_dec_init_ver(&mut ctx, iface, &cfg, 0, VPX_DECODER_ABI_VERSION as i32) }; if err != vpx_codec_err_t::VPX_CODEC_OK { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "{NAME} {} decoder init: {}", codec.label(), describe(err, None) @@ -119,7 +119,7 @@ impl Vpx { ) }; if err != vpx_codec_err_t::VPX_CODEC_OK { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "{NAME} corruption check: {}", self.error(err) ))); @@ -203,7 +203,7 @@ fn picture(codec: Codec, img: &vpx_image_t, timestamp: Timestamp) -> Result Result<(), Error> { match self { - Input::Ended => Err(Error::Codec(anyhow::anyhow!( + Input::Ended => Err(Error::codec(anyhow::anyhow!( "the MediaCodec encoder has ended its stream and takes no more input" ))), Input::Open => Ok(()), @@ -185,7 +185,7 @@ impl MediaCodec { }; let codec = media_codec::MediaCodec::from_encoder_type(mime) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("no MediaCodec encoder for {mime}")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("no MediaCodec encoder for {mime}")))?; let format = encoder_format(config, mime)?; codec .configure(&format, None, MediaCodecDirection::Encoder) @@ -348,7 +348,7 @@ impl MediaCodec { } } - Err(Error::Codec(anyhow::anyhow!( + Err(Error::codec(anyhow::anyhow!( "MediaCodec did not reach end of stream within {:?}", DRAIN_TIMEOUT * DRAIN_ROUNDS ))) @@ -382,7 +382,7 @@ impl MediaCodec { self.drain(OUTPUT_TIMEOUT, out)?; } - Err(Error::Codec(anyhow::anyhow!( + Err(Error::codec(anyhow::anyhow!( "MediaCodec never freed an input buffer for the end of stream" ))) } @@ -535,7 +535,7 @@ fn fill_nv12(buffer: &mut [MaybeUninit], i420: &I420, width: usize, height: let luma_len = width * height; let needed = luma_len + luma_len / 2; if buffer.len() < needed { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "MediaCodec input buffer is {} bytes, needs {needed} for {width}x{height} NV12", buffer.len() ))); @@ -650,7 +650,7 @@ fn opens_with_parameter_set(unit: &[u8], codec: Codec) -> bool { /// Wrap an NDK media error, naming the call that produced it. fn codec_err(what: &str, error: MediaError) -> Error { - Error::Codec(anyhow::anyhow!("failed to {what} on the MediaCodec encoder: {error}")) + Error::codec(anyhow::anyhow!("failed to {what} on the MediaCodec encoder: {error}")) } fn clamp_i32(value: u64) -> i32 { diff --git a/rs/moq-video/src/encode/backend/mediafoundation.rs b/rs/moq-video/src/encode/backend/mediafoundation.rs index cf66267fbea..b1329f32099 100644 --- a/rs/moq-video/src/encode/backend/mediafoundation.rs +++ b/rs/moq-video/src/encode/backend/mediafoundation.rs @@ -228,10 +228,10 @@ impl MediaFoundation { fn stream(&mut self) -> Result<&mut Stream, Error> { match &mut self.phase { Phase::Streaming(stream) => Ok(stream), - Phase::Unstarted => Err(Error::Codec(anyhow::anyhow!( + Phase::Unstarted => Err(Error::codec(anyhow::anyhow!( "the mediafoundation encoder has not started" ))), - Phase::Ended => Err(Error::Codec(anyhow::anyhow!( + Phase::Ended => Err(Error::codec(anyhow::anyhow!( "the mediafoundation encoder has ended its stream and takes no more frames" ))), } @@ -389,7 +389,7 @@ impl MediaFoundation { reason = "the other surfaces exist only on some platforms and features" )] _ => { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "unsupported frame for mediafoundation encoder" ))); } @@ -776,7 +776,7 @@ fn enumerate_encoder(subtype: GUID) -> Result { .map_err(|e| mf_err("MFTEnumEx", e))?; } if count == 0 { - return Err(Error::Codec(anyhow::anyhow!("no hardware encoder found"))); + return Err(Error::codec(anyhow::anyhow!("no hardware encoder found"))); } let entries = unsafe { std::slice::from_raw_parts_mut(activates, count as usize) }; @@ -793,7 +793,7 @@ fn enumerate_encoder(subtype: GUID) -> Result { windows::Win32::System::Com::CoTaskMemFree(Some(activates as *const std::ffi::c_void)); } - transform.ok_or_else(|| Error::Codec(anyhow::anyhow!("failed to activate encoder MFT"))) + transform.ok_or_else(|| Error::codec(anyhow::anyhow!("failed to activate encoder MFT"))) } /// The Media Foundation output type for a codec: the format subtype enumerated @@ -830,7 +830,7 @@ fn device_manager(device: &ID3D11Device) -> Result unsafe { MFCreateDXGIDeviceManager(&mut token, &mut manager).map_err(|e| mf_err("MFCreateDXGIDeviceManager", e))?; } - let manager = manager.ok_or_else(|| Error::Codec(anyhow::anyhow!("MFCreateDXGIDeviceManager returned null")))?; + let manager = manager.ok_or_else(|| Error::codec(anyhow::anyhow!("MFCreateDXGIDeviceManager returned null")))?; unsafe { manager .ResetDevice(device, token) diff --git a/rs/moq-video/src/encode/backend/mod.rs b/rs/moq-video/src/encode/backend/mod.rs index 22eaa8c3762..bb757760d06 100644 --- a/rs/moq-video/src/encode/backend/mod.rs +++ b/rs/moq-video/src/encode/backend/mod.rs @@ -613,7 +613,7 @@ mod tests { const REFUSING: Candidate = Candidate { name: "driverless", codecs: &[Codec::H264], - open: |_| Err(Error::Codec(anyhow::anyhow!("driver libraries not found"))), + open: |_| Err(Error::codec(anyhow::anyhow!("driver libraries not found"))), cuts: true, #[cfg(target_os = "linux")] vulkan: false, diff --git a/rs/moq-video/src/encode/backend/nvenc.rs b/rs/moq-video/src/encode/backend/nvenc.rs index 15978cedd2b..618a59b1fbc 100644 --- a/rs/moq-video/src/encode/backend/nvenc.rs +++ b/rs/moq-video/src/encode/backend/nvenc.rs @@ -89,12 +89,12 @@ pub(crate) struct Nvenc { impl Nvenc { pub(crate) fn open(config: &Config) -> Result, Error> { - Encoder::load().map_err(|error| Error::Codec(anyhow::anyhow!("NVENC unavailable: {error}")))?; + Encoder::load().map_err(|error| Error::codec(anyhow::anyhow!("NVENC unavailable: {error}")))?; // cudarc still panics while loading a missing CUDA driver. Probe it before // creating the context so automatic codec selection can fall through. if !cuda_driver_present() { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "CUDA driver library not found (libcuda); NVENC unavailable" ))); } @@ -104,14 +104,14 @@ impl Nvenc { let cuda = input_context(config.input)?; let encoder = Encoder::initialize_with_cuda(cuda.clone()) - .map_err(|e| Error::Codec(anyhow::anyhow!("NVENC init: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC init: {e}")))?; // Start from the preset, then set rate control and GOP. let (preset_guid, label) = nvenc_preset(config.preset); let tuning = NV_ENC_TUNING_INFO::NV_ENC_TUNING_INFO_LOW_LATENCY; let mut preset = encoder .get_preset_config(codec_guid, preset_guid, tuning) - .map_err(|e| Error::Codec(anyhow::anyhow!("NVENC preset config: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC preset config: {e}")))?; let cfg = &mut preset.presetCfg; let interval = config.keyframe_interval(); @@ -214,7 +214,7 @@ impl Nvenc { // frame registers directly; the CPU path interleaves I420 chroma on write. let session = encoder .start_session(NV_ENC_BUFFER_FORMAT::NV_ENC_BUFFER_FORMAT_NV12, init) - .map_err(|e| Error::Codec(anyhow::anyhow!("NVENC start session: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC start session: {e}")))?; tracing::info!( encoder = NAME, @@ -279,7 +279,7 @@ impl Backend for Nvenc { let output = self .session .create_output_bitstream() - .map_err(|e| Error::Codec(anyhow::anyhow!("NVENC output bitstream: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC output bitstream: {e}")))?; let params = moq_nvenc::EncodePictureParams { input_timestamp: self.timestamp, @@ -308,12 +308,12 @@ impl Backend for Nvenc { cuda.pitch, ) } - .map_err(|e| Error::Codec(anyhow::anyhow!("NVENC register CUDA frame: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC register CUDA frame: {e}")))?; let submission = self .session .encode_picture(resource, output, params) - .map_err(|e| Error::Codec(anyhow::anyhow!("NVENC encode: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC encode: {e}")))?; drain_output(submission)? } // Everything else goes through a CPU NV12 input buffer. @@ -321,7 +321,7 @@ impl Backend for Nvenc { let mut input = self .session .create_input_buffer() - .map_err(|e| Error::Codec(anyhow::anyhow!("NVENC input buffer: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC input buffer: {e}")))?; let i420 = frame.to_i420()?; @@ -336,7 +336,7 @@ impl Backend for Nvenc { // width (interleaved U+V) at the luma pitch. let mut lock = input .lock() - .map_err(|e| Error::Codec(anyhow::anyhow!("NVENC lock input: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC lock input: {e}")))?; let pitch = lock.pitch() as usize; // SAFETY: offsets stay within the pitch*height*3/2 NV12 buffer and // each source plane is exactly row_bytes * rows. @@ -349,7 +349,7 @@ impl Backend for Nvenc { let submission = self .session .encode_picture(input, output, params) - .map_err(|e| Error::Codec(anyhow::anyhow!("NVENC encode: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC encode: {e}")))?; drain_output(submission)? } }; @@ -379,7 +379,7 @@ impl Backend for Nvenc { // control mode set at open still applies, only the target moves. self.session .reconfigure(bitrate.min(u32::MAX as u64) as u32) - .map_err(|e| Error::Codec(anyhow::anyhow!("NVENC set bitrate to {bitrate}: {e}"))) + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC set bitrate to {bitrate}: {e}"))) } fn can_cut(&self) -> bool { @@ -401,25 +401,25 @@ impl Backend for Nvenc { fn drain_output(submission: moq_nvenc::Submission) -> Result, Error> { let (data, _input, _output) = submission .finish() - .map_err(|e| Error::Codec(anyhow::anyhow!("NVENC lock output: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC lock output: {e}")))?; Ok(data) } /// Match the declared Vulkan device before allocating an NVENC session. fn input_context(input: Option) -> Result, Error> { let Some(device) = input else { - return CudaContext::new(0).map_err(|e| Error::Codec(anyhow::anyhow!("CUDA init: {e:?}"))); + return CudaContext::new(0).map_err(|e| Error::codec(anyhow::anyhow!("CUDA init: {e:?}"))); }; matching_driver(device)?; - let count = CudaContext::device_count().map_err(|e| Error::Codec(anyhow::anyhow!("CUDA enumerate: {e:?}")))?; + let count = CudaContext::device_count().map_err(|e| Error::codec(anyhow::anyhow!("CUDA enumerate: {e:?}")))?; for ordinal in 0..count { let physical = cudarc::driver::result::device::get(ordinal) - .map_err(|e| Error::Codec(anyhow::anyhow!("CUDA device {ordinal}: {e:?}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("CUDA device {ordinal}: {e:?}")))?; let uuid = cudarc::driver::result::device::get_uuid(physical) - .map_err(|e| Error::Codec(anyhow::anyhow!("CUDA UUID: {e:?}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("CUDA UUID: {e:?}")))?; if uuid.bytes.map(|byte| u8::from_ne_bytes(byte.to_ne_bytes())) == device.device_uuid { return CudaContext::new(ordinal as usize) - .map_err(|e| Error::Codec(anyhow::anyhow!("CUDA input device {ordinal}: {e:?}"))); + .map_err(|e| Error::codec(anyhow::anyhow!("CUDA input device {ordinal}: {e:?}"))); } } Err(Error::Unsupported(format!( diff --git a/rs/moq-video/src/encode/backend/openh264.rs b/rs/moq-video/src/encode/backend/openh264.rs index 83ffcaaa0db..def568eb136 100644 --- a/rs/moq-video/src/encode/backend/openh264.rs +++ b/rs/moq-video/src/encode/backend/openh264.rs @@ -86,7 +86,7 @@ impl Openh264 { .vui(vui); let encoder = Encoder::with_api_config(OpenH264API::from_source(), cfg) - .map_err(|e| Error::Codec(anyhow::anyhow!("openh264 init: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("openh264 init: {e}")))?; tracing::info!( encoder = NAME, @@ -146,7 +146,7 @@ impl Openh264 { api.set_option(ENCODER_OPTION_BITRATE, std::ptr::from_mut(&mut info).cast()) }; if status != 0 { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "openh264 set bitrate to {bitrate}: status {status}" ))); } @@ -177,7 +177,7 @@ impl Backend for Openh264 { let bitstream = self .encoder .encode(&yuv) - .map_err(|e| Error::Codec(anyhow::anyhow!("openh264 encode: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("openh264 encode: {e}")))?; // One Annex-B access unit per frame (low-delay, no B-frames). A skipped // frame yields an empty bitstream. diff --git a/rs/moq-video/src/encode/backend/probe.rs b/rs/moq-video/src/encode/backend/probe.rs index b971a99cba7..3c75cab810a 100644 --- a/rs/moq-video/src/encode/backend/probe.rs +++ b/rs/moq-video/src/encode/backend/probe.rs @@ -136,7 +136,7 @@ impl Backend for Probe { let mut refuse = REFUSE.lock().unwrap(); if *refuse == Some(bitrate) { *refuse = None; - return Err(Error::Codec(anyhow::anyhow!("probe refused {bitrate} bps"))); + return Err(Error::codec(anyhow::anyhow!("probe refused {bitrate} bps"))); } BITRATES.lock().unwrap().push(bitrate); Ok(()) diff --git a/rs/moq-video/src/encode/backend/v4l2.rs b/rs/moq-video/src/encode/backend/v4l2.rs index 1d66650530f..aa383eac08c 100644 --- a/rs/moq-video/src/encode/backend/v4l2.rs +++ b/rs/moq-video/src/encode/backend/v4l2.rs @@ -146,7 +146,7 @@ enum Phase { /// The error every call reports once the encoder has failed. fn failed() -> Error { - Error::Codec(anyhow::anyhow!( + Error::codec(anyhow::anyhow!( "the V4L2 encoder could not start or stop its queues and takes no more frames" )) } @@ -232,7 +232,7 @@ impl V4l2 { }, )?; if coded.pixelformat != v4l2::H264 { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "V4L2 encoder answered an H264 request with {}", v4l2::name(coded.pixelformat) ))); @@ -343,7 +343,7 @@ impl V4l2 { return Ok(index); } if Instant::now() >= deadline { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "V4L2 encoder held every input buffer for {BUFFER_TIMEOUT:?}" ))); } @@ -482,7 +482,7 @@ impl V4l2 { if out.len() > before { deadline = Instant::now() + FLUSH_TIMEOUT; } else if Instant::now() >= deadline { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "V4L2 encoder did not finish its drain within {FLUSH_TIMEOUT:?}, holding {} frame(s)", self.pending.len() ))); @@ -525,7 +525,7 @@ impl V4l2 { if out.len() > before { deadline = Instant::now() + FLUSH_TIMEOUT; } else if Instant::now() >= deadline { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "V4L2 encoder held {} frame(s) for {FLUSH_TIMEOUT:?} without encoding them", self.pending.len() ))); @@ -541,7 +541,7 @@ impl Backend for V4l2 { return Err(failed()); } if frame.size() != self.size { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "V4L2 encoder opened for {} was given a {} frame", self.size, frame.size() @@ -558,7 +558,7 @@ impl Backend for V4l2 { self.device .set_control(V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME, 0) .map_err(|err| { - Error::Codec(anyhow::anyhow!( + Error::codec(anyhow::anyhow!( "driver refused the keyframe request it advertised: {err}" )) })?; @@ -938,7 +938,7 @@ fn h264_level(config: &Config) -> Result { }) .map(|level| level.code as i32) .ok_or_else(|| { - Error::Codec(anyhow::anyhow!( + Error::codec(anyhow::anyhow!( "{size} at {}fps and {kbps} kbit/s exceeds H.264 level 6.2", config.framerate )) diff --git a/rs/moq-video/src/encode/backend/vaapi.rs b/rs/moq-video/src/encode/backend/vaapi.rs index ac262519bdc..67e97962eff 100644 --- a/rs/moq-video/src/encode/backend/vaapi.rs +++ b/rs/moq-video/src/encode/backend/vaapi.rs @@ -104,7 +104,7 @@ impl Vaapi { interval, ) }; - let encoder = Encoder::new(vaapi).map_err(|e| Error::Codec(anyhow::anyhow!("VAAPI encoder init: {e:?}")))?; + let encoder = Encoder::new(vaapi).map_err(|e| Error::codec(anyhow::anyhow!("VAAPI encoder init: {e:?}")))?; tracing::info!( encoder = NAME, @@ -168,7 +168,7 @@ impl Vaapi { i420_to_nv12(&i420, &mut self.nv12); self.encoder .encode_nv12(&self.nv12, cut) - .map_err(|e| Error::Codec(e.context("VAAPI encode"))) + .map_err(|e| Error::codec(e.context("VAAPI encode"))) } } @@ -210,7 +210,7 @@ impl Backend for Vaapi { // applies from the next one without an IDR. self.encoder .set_bitrate(bitrate.min(u32::MAX as u64) as u32) - .map_err(|e| Error::Codec(e.context("VAAPI bitrate change"))) + .map_err(|e| Error::codec(e.context("VAAPI bitrate change"))) } fn can_cut(&self) -> bool { diff --git a/rs/moq-video/src/encode/backend/videotoolbox.rs b/rs/moq-video/src/encode/backend/videotoolbox.rs index 333bef6cc14..aea66d82f0e 100644 --- a/rs/moq-video/src/encode/backend/videotoolbox.rs +++ b/rs/moq-video/src/encode/backend/videotoolbox.rs @@ -111,7 +111,7 @@ impl VideoToolbox { let session = NonNull::new(session_ptr) .filter(|_| status == 0) .map(|p| unsafe { CFRetained::from_raw(p) }) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("VTCompressionSessionCreate failed: {status}")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("VTCompressionSessionCreate failed: {status}")))?; // Every preset forbids reordering. A failure here cannot be fixed by downgrading. set_bool( @@ -198,7 +198,7 @@ impl VideoToolbox { "opened video encoder" ); let framerate_numerator = i32::try_from(config.framerate.numerator()) - .map_err(|_| Error::Codec(anyhow::anyhow!("VideoToolbox frame-rate numerator exceeds i32")))?; + .map_err(|_| Error::codec(anyhow::anyhow!("VideoToolbox frame-rate numerator exceeds i32")))?; Ok(Self { session, sink, @@ -247,7 +247,7 @@ impl Backend for VideoToolbox { ) }; if status != 0 { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "VTCompressionSessionEncodeFrame failed: {status}" ))); } @@ -255,13 +255,13 @@ impl Backend for VideoToolbox { // Force this frame out; with no reordering there's nothing else pending. let status = unsafe { self.session.complete_frames(kCMTimeInvalid) }; if status != 0 { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "VTCompressionSessionCompleteFrames failed: {status}" ))); } if let Some(status) = self.sink.error.take() { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "VideoToolbox encode callback failed: {status}" ))); } @@ -493,13 +493,13 @@ fn force_keyframe_dict() -> Result, Error> { &objc2_core_foundation::kCFTypeDictionaryValueCallBacks, ) } - .ok_or_else(|| Error::Codec(anyhow::anyhow!("failed to build force-keyframe dictionary"))) + .ok_or_else(|| Error::codec(anyhow::anyhow!("failed to build force-keyframe dictionary"))) } fn set_property(session: &VTCompressionSession, key: &CFString, value: &CFType) -> Result<(), Error> { let status = unsafe { VTSessionSetProperty(session, key, Some(value)) }; if status != 0 { - return Err(Error::Codec(anyhow::anyhow!("VTSessionSetProperty failed: {status}"))); + return Err(Error::codec(anyhow::anyhow!("VTSessionSetProperty failed: {status}"))); } Ok(()) } @@ -511,7 +511,7 @@ fn set_bool(session: &VTCompressionSession, key: &CFString, value: bool) -> Resu fn set_number(session: &VTCompressionSession, key: &CFString, value: i32) -> Result<(), Error> { let number = unsafe { CFNumber::new(None, CFNumberType::SInt32Type, &value as *const i32 as *const c_void) } - .ok_or_else(|| Error::Codec(anyhow::anyhow!("failed to build CFNumber")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("failed to build CFNumber")))?; set_property(session, key, number.as_ref()) } diff --git a/rs/moq-video/src/encode/encoder.rs b/rs/moq-video/src/encode/encoder.rs index 8ec8e8ae04e..ab7c570daab 100644 --- a/rs/moq-video/src/encode/encoder.rs +++ b/rs/moq-video/src/encode/encoder.rs @@ -293,7 +293,7 @@ impl Config { Codec::H265 => moq_mux::codec::h265::config(&annexb), }; let mut rendition = parsed.map_err(|err| { - Error::Codec(anyhow::anyhow!( + Error::codec(anyhow::anyhow!( "{} emitted no usable parameter sets: {err}", sink.name() )) @@ -527,7 +527,7 @@ impl Encoder { }; let size = frame.size(); if !external && size != self.size { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "frame {size} does not match encoder {}", self.size ))); @@ -1393,7 +1393,7 @@ mod tests { impl Backend for Failing { fn encode(&mut self, _frame: &Frame, _cut: bool) -> Result, Error> { - Err(Error::Codec(anyhow::anyhow!("no"))) + Err(Error::codec(anyhow::anyhow!("no"))) } fn flush(&mut self) -> Result, Error> { diff --git a/rs/moq-video/src/encode/producer.rs b/rs/moq-video/src/encode/producer.rs index 2bdd2b3a542..e9b9b30607a 100644 --- a/rs/moq-video/src/encode/producer.rs +++ b/rs/moq-video/src/encode/producer.rs @@ -11,9 +11,6 @@ //! is sure to honor a cut, and forces a keyframe whenever a joining subscriber asks for a group //! past the largest. The encoder's GOP stays the upper bound between keyframes. -#[cfg(feature = "capture")] -use std::sync::Arc; - use moq_mux::catalog::hang::CatalogExt; #[cfg(feature = "capture")] use moq_mux::rate; @@ -127,7 +124,7 @@ impl Producer { }, // Unreachable via `Config::probe`, which only encodes what `Codec` covers. other => { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "{other} is not a codec this producer can publish" ))); } @@ -329,7 +326,7 @@ pub enum Status { /// Frames are being captured, encoded, and published. Live, /// The capture, encoder, or track failed with this error, which [`Driver::run`] returns. - Failed(Arc), + Failed(Error), /// The MoQ track ended, so the driver has stopped. Ended, /// The [`Driver`] or its [`run`](Driver::run) future was dropped before it @@ -337,17 +334,6 @@ pub enum Status { Cancelled, } -#[cfg(feature = "capture")] -impl Status { - /// Whether publishing `self` over `other` changes nothing an observer can see. - fn same(&self, other: &Self) -> bool { - match (self, other) { - (Self::Failed(a), Self::Failed(b)) => Arc::ptr_eq(a, b), - _ => std::mem::discriminant(self) == std::mem::discriminant(other), - } - } -} - /// A snapshot of a capture publication's lifecycle. #[cfg(feature = "capture")] #[derive(Clone, Debug)] @@ -385,7 +371,10 @@ fn starting() -> kio::Producer { #[cfg(feature = "capture")] fn publish_status(state: &kio::Producer, status: Status) { let Ok(mut published) = state.write() else { return }; - if !published.state.status.same(&status) { + // A repeated status changes nothing an observer can see; a failure always carries news. + if matches!(status, Status::Failed(_)) + || std::mem::discriminant(&published.state.status) != std::mem::discriminant(&status) + { published.state.status = status; published.revision = published.revision.wrapping_add(1); } @@ -549,20 +538,20 @@ impl Drop for Driver { #[cfg(feature = "capture")] impl Driver { - /// Capture until the broadcast is dropped (the track stops being announced) or the - /// capture fails. + /// Capture until the capture fails or its track ends. Closing or dropping the broadcast + /// leaves the track running; drop the [`Driver`] or this future to stop it. /// /// Frames are stamped on the catalog's clock, so a concurrent audio publish into the /// same catalog stays aligned. Off macOS and iOS the future is `Send` and can be /// spawned; on macOS the capture session is `!Send`, so await it on a local task there. - pub async fn run(self) -> Result<(), Arc> { + pub async fn run(self) -> Result<(), Error> { self.run_from(DeviceSource).await } /// [`run`](Self::run) from `source`, so the fixtures can drive it from a synthetic or /// stalled one. - async fn run_from(mut self, source: S) -> Result<(), Arc> { - let result = self.capture(source).await.map_err(Arc::new); + async fn run_from(mut self, source: S) -> Result<(), Error> { + let result = self.capture(source).await; // This runs only when the capture ends on its own; dropping the future leaves the // track to `Drop` instead, since async `Drop` can't finalize it. let owned = std::mem::replace(&mut self.owned, Owned::Done); @@ -635,7 +624,7 @@ impl Driver { /// Register the capture's video track, before anything about its source is known. /// -/// Consumes `broadcast`, so the publish ends once the caller drops theirs. +/// Takes `broadcast` only to mint the track, which outlives it. #[cfg(feature = "capture")] fn capture_track( broadcast: moq_net::broadcast::Producer, @@ -1871,7 +1860,7 @@ mod tests { catalog: moq_mux::catalog::Producer, options: Capture, source: impl CaptureSource, - ) -> Result<(), Arc> { + ) -> Result<(), Error> { let (_, driver) = publish_capture(broadcast, catalog, options)?; driver.run_from(source).await } diff --git a/rs/moq-video/src/error.rs b/rs/moq-video/src/error.rs index 1023a0a404f..e7204c33bd9 100644 --- a/rs/moq-video/src/error.rs +++ b/rs/moq-video/src/error.rs @@ -1,5 +1,7 @@ +use std::sync::Arc; + /// Errors returned by `moq-video`. -#[derive(Debug, thiserror::Error)] +#[derive(Clone, Debug, thiserror::Error)] #[non_exhaustive] pub enum Error { /// A [`decode::Consumer`](crate::decode::Consumer) refused the rendition before @@ -108,11 +110,11 @@ pub enum Error { /// GPU rendering failure: building the pipeline, importing a frame's surface /// as a texture, or the device itself. #[error("render: {0}")] - Render(#[source] anyhow::Error), + Render(#[source] Arc), /// Capture / encode / codec failure (the message carries the detail). #[error(transparent)] - Codec(#[from] anyhow::Error), + Codec(Arc), /// moq-mux muxer/catalog error. #[error(transparent)] @@ -126,3 +128,22 @@ pub enum Error { #[error(transparent)] TimeOverflow(#[from] moq_net::TimeOverflow), } + +impl Error { + /// A [`Codec`](Self::Codec) failure. + pub(crate) fn codec(err: anyhow::Error) -> Self { + Self::Codec(Arc::new(err)) + } + + /// A [`Render`](Self::Render) failure. + #[cfg(feature = "render")] + pub(crate) fn render(err: anyhow::Error) -> Self { + Self::Render(Arc::new(err)) + } +} + +impl From for Error { + fn from(err: anyhow::Error) -> Self { + Self::codec(err) + } +} diff --git a/rs/moq-video/src/frame.rs b/rs/moq-video/src/frame.rs index 08f8eadd838..2fbcd97a700 100644 --- a/rs/moq-video/src/frame.rs +++ b/rs/moq-video/src/frame.rs @@ -522,7 +522,7 @@ impl Surface { size.validate("RGBA frame")?; let expected = size.byte_len(4, "RGBA frame")?; if rgba.len() != expected { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "RGBA buffer is {} bytes, expected {expected} for {size}", rgba.len() ))); @@ -530,7 +530,7 @@ impl Surface { Ok(Surface::I420(I420::from_rgba( rgba, size.width.checked_mul(4).ok_or_else(|| { - Error::Codec(anyhow::anyhow!( + Error::codec(anyhow::anyhow!( "RGBA frame {size}: row stride is too large to represent" )) })?, @@ -800,7 +800,7 @@ impl I420 { size.validate("I420")?; let expected = Self::len(size)?; if data.len() != expected { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "I420 {size} needs {expected} bytes, got {}", data.len() ))); @@ -861,7 +861,7 @@ impl I420 { size.validate("I420")?; let luma = size.byte_len(1, "I420")?; luma.checked_add(luma / 2) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("I420 {size}: byte length is too large to represent"))) + .ok_or_else(|| Error::codec(anyhow::anyhow!("I420 {size}: byte length is too large to represent"))) } /// Convert RGBA (`stride` bytes per row, >= `width * 4`) to I420 in @@ -875,7 +875,7 @@ impl I420 { let (range, matrix) = color.yuv(); let mut planar = YuvPlanarImageMut::alloc(width, height, YuvChromaSubsampling::Yuv420); rgba_to_yuv420(&mut planar, rgba, stride, range, matrix, YuvConversionMode::Balanced) - .map_err(|e| Error::Codec(anyhow::anyhow!("rgba_to_yuv420 failed for {width}x{height}: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("rgba_to_yuv420 failed for {width}x{height}: {e}")))?; Self::pack(&planar, size, Some(color)) } @@ -893,7 +893,7 @@ impl I420 { let (range, matrix) = color.yuv(); let mut planar = YuvPlanarImageMut::alloc(width, height, YuvChromaSubsampling::Yuv420); bgra_to_yuv420(&mut planar, bgra, stride, range, matrix, YuvConversionMode::Balanced) - .map_err(|e| Error::Codec(anyhow::anyhow!("bgra_to_yuv420 failed for {width}x{height}: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("bgra_to_yuv420 failed for {width}x{height}: {e}")))?; Self::pack(&planar, size, Some(color)) } @@ -919,18 +919,18 @@ impl I420 { let (w, h) = (width as usize, height as usize); let (cw, ch) = (w / 2, h / 2); if y_stride < w || uv_stride < cw { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "I420 {size}: strides {y_stride}/{uv_stride} are smaller than plane widths {w}/{cw}" ))); } let y_need = y_stride .checked_mul(h) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("I420 {size}: luma plane length overflows")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("I420 {size}: luma plane length overflows")))?; let uv_need = uv_stride .checked_mul(ch) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("I420 {size}: chroma plane length overflows")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("I420 {size}: chroma plane length overflows")))?; if y.len() < y_need || u.len() < uv_need || v.len() < uv_need { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "I420 {size}: source planes are shorter than their declared strides" ))); } @@ -964,7 +964,7 @@ impl I420 { size.validate("RGB frame")?; let Size { width, height } = size; let stride = width.checked_mul(3).ok_or_else(|| { - Error::Codec(anyhow::anyhow!( + Error::codec(anyhow::anyhow!( "RGB frame {size}: row stride is too large to represent" )) })?; @@ -972,7 +972,7 @@ impl I420 { let (range, matrix) = color.yuv(); let mut planar = YuvPlanarImageMut::alloc(width, height, YuvChromaSubsampling::Yuv420); rgb_to_yuv420(&mut planar, rgb, stride, range, matrix, YuvConversionMode::Balanced) - .map_err(|e| Error::Codec(anyhow::anyhow!("rgb_to_yuv420 failed for {width}x{height}: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("rgb_to_yuv420 failed for {width}x{height}: {e}")))?; Self::pack(&planar, size, Some(color)) } @@ -990,12 +990,12 @@ impl I420 { let mut decoder = zune_jpeg::JpegDecoder::new(ZCursor::new(jpeg)); let rgb = decoder .decode() - .map_err(|e| Error::Codec(anyhow::anyhow!("MJPEG decode: {e:?}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("MJPEG decode: {e:?}")))?; let (w, h) = decoder .dimensions() - .ok_or_else(|| Error::Codec(anyhow::anyhow!("MJPEG frame had no dimensions")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("MJPEG frame had no dimensions")))?; if w as u32 != size.width || h as u32 != size.height { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "MJPEG frame is {w}x{h}, not the negotiated {size}" ))); } @@ -1019,7 +1019,7 @@ impl I420 { height, }; yuyv422_to_yuv420(&mut planar, &packed) - .map_err(|e| Error::Codec(anyhow::anyhow!("yuyv422_to_yuv420 failed for {width}x{height}: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("yuyv422_to_yuv420 failed for {width}x{height}: {e}")))?; // A chroma resample, not a color conversion: these samples are in // whatever space the camera produced, which the caller names if it can. Self::pack(&planar, size, None) @@ -1040,7 +1040,7 @@ impl I420 { let chroma = luma / 4; let need = Self::len(size)?; if nv12.len() < need { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "NV12 buffer too small: {} < {need} for {width}x{height}", nv12.len() ))); @@ -1087,12 +1087,12 @@ impl I420 { dh: u32| -> Result<(), Error> { let src = ImageRef::new(sw, sh, src, PixelType::U8) - .map_err(|e| Error::Codec(anyhow::anyhow!("resize source: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("resize source: {e}")))?; let mut dst = Image::from_slice_u8(dw, dh, dst, PixelType::U8) - .map_err(|e| Error::Codec(anyhow::anyhow!("resize destination: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("resize destination: {e}")))?; resizer .resize(&src, &mut dst, &options) - .map_err(|e| Error::Codec(anyhow::anyhow!("resize: {e}"))) + .map_err(|e| Error::codec(anyhow::anyhow!("resize: {e}"))) }; let Size { width, height } = size; @@ -1444,7 +1444,7 @@ pub mod android { let buffer = self .image .hardware_buffer() - .map_err(|e| Error::Codec(anyhow::anyhow!("AImage_getHardwareBuffer failed: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("AImage_getHardwareBuffer failed: {e}")))?; // The getter borrows: it hands back the image's own handle without // touching the refcount, so the caller needs a reference of its own. Ok(buffer.acquire()) @@ -1482,7 +1482,7 @@ pub mod android { /// One plane's bytes and the two strides needed to walk them. fn plane(&self, index: i32) -> Result, Error> { let fail = |what: &str, e: ndk::media_error::MediaError| { - Error::Codec(anyhow::anyhow!("AImage_getPlane{what} failed for plane {index}: {e}")) + Error::codec(anyhow::anyhow!("AImage_getPlane{what} failed for plane {index}: {e}")) }; Ok(Plane { data: self.image.plane_data(index).map_err(|e| fail("Data", e))?, @@ -1523,9 +1523,9 @@ pub mod android { .and_then(|col| offset.checked_add(col)) }) .and_then(|offset| offset.checked_add(1)) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("image plane strides overflow the address space")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("image plane strides overflow the address space")))?; if self.data.len() < needed { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "image plane is {} bytes, needs {needed} for {width}x{height} at ({left}, {top}), row stride {}, and pixel stride {}", self.data.len(), self.row, @@ -1659,13 +1659,13 @@ pub mod apple { let scaler = { let mut scalers = SCALERS .lock() - .map_err(|_| Error::Codec(anyhow::anyhow!("pixel-transfer scaler cache lock poisoned")))?; + .map_err(|_| Error::codec(anyhow::anyhow!("pixel-transfer scaler cache lock poisoned")))?; scalers.get_or_insert_with((width, height), || Scaler::new(width, height))? }; let result = scaler .lock() - .map_err(|_| Error::Codec(anyhow::anyhow!("pixel-transfer scaler lock poisoned")))? + .map_err(|_| Error::codec(anyhow::anyhow!("pixel-transfer scaler lock poisoned")))? .resize(self); drop(scaler); if let Ok(mut scalers) = SCALERS.lock() { @@ -1731,7 +1731,7 @@ pub mod apple { if format != kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange && format != kCVPixelFormatType_420YpCbCr8BiPlanarFullRange { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "cannot download pixel format {format:#x}; expected NV12" ))); } @@ -1743,7 +1743,7 @@ pub mod apple { let status = unsafe { CVPixelBufferLockBaseAddress(&self.buffer, LOCK_READ_ONLY) }; if status != 0 { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "CVPixelBufferLockBaseAddress failed: {status}" ))); } @@ -1806,7 +1806,7 @@ pub mod apple { let session = NonNull::new(session_ptr) .filter(|_| status == 0) .map(|ptr| unsafe { CFRetained::from_raw(ptr) }) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("VTPixelTransferSessionCreate failed: {status}")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("VTPixelTransferSessionCreate failed: {status}")))?; let attributes = pool_attributes(width, height)?; let mut pool_ptr: *mut CVPixelBufferPool = std::ptr::null_mut(); @@ -1821,7 +1821,7 @@ pub mod apple { let pool = NonNull::new(pool_ptr) .filter(|_| status == 0) .map(|ptr| unsafe { CFRetained::from_raw(ptr) }) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("CVPixelBufferPoolCreate failed: {status}")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("CVPixelBufferPoolCreate failed: {status}")))?; Ok(Self { session, @@ -1843,11 +1843,11 @@ pub mod apple { let output = NonNull::new(output_ptr) .filter(|_| status == 0) .map(|ptr| unsafe { CFRetained::from_raw(ptr) }) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("CVPixelBufferPoolCreatePixelBuffer failed: {status}")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("CVPixelBufferPoolCreatePixelBuffer failed: {status}")))?; let status = unsafe { self.session.transfer_image(&source.buffer, &output) }; if status != 0 { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "VTPixelTransferSessionTransferImage failed: {status}" ))); } @@ -1859,9 +1859,9 @@ pub mod apple { /// Build a reusable NV12 IOSurface pool for one output size. fn pool_attributes(width: u32, height: u32) -> Result, Error> { let width = - i32::try_from(width).map_err(|_| Error::Codec(anyhow::anyhow!("pixel-buffer width is too large")))?; + i32::try_from(width).map_err(|_| Error::codec(anyhow::anyhow!("pixel-buffer width is too large")))?; let height = - i32::try_from(height).map_err(|_| Error::Codec(anyhow::anyhow!("pixel-buffer height is too large")))?; + i32::try_from(height).map_err(|_| Error::codec(anyhow::anyhow!("pixel-buffer height is too large")))?; let format = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange as i32; let width = cf_number(width)?; @@ -1877,7 +1877,7 @@ pub mod apple { &objc2_core_foundation::kCFTypeDictionaryValueCallBacks, ) } - .ok_or_else(|| Error::Codec(anyhow::anyhow!("failed to build IOSurface attributes dictionary")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("failed to build IOSurface attributes dictionary")))?; let mut keys = [ (unsafe { kCVPixelBufferPixelFormatTypeKey } as *const CFString).cast::(), @@ -1902,7 +1902,7 @@ pub mod apple { ) } .ok_or_else(|| { - Error::Codec(anyhow::anyhow!( + Error::codec(anyhow::anyhow!( "failed to build pixel-buffer pool attributes dictionary" )) }) @@ -1910,7 +1910,7 @@ pub mod apple { fn cf_number(value: i32) -> Result, Error> { unsafe { CFNumber::new(None, CFNumberType::SInt32Type, (&value as *const i32).cast::()) } - .ok_or_else(|| Error::Codec(anyhow::anyhow!("failed to build CFNumber"))) + .ok_or_else(|| Error::codec(anyhow::anyhow!("failed to build CFNumber"))) } struct UnlockGuard<'a>(&'a CVPixelBuffer); @@ -2001,12 +2001,12 @@ pub mod apple { let buffer = NonNull::new(ptr) .filter(|_| status == 0) .map(|p| unsafe { CFRetained::from_raw(p) }) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("CVPixelBufferCreate failed: {status}")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("CVPixelBufferCreate failed: {status}")))?; let flags = CVPixelBufferLockFlags(0); let status = unsafe { CVPixelBufferLockBaseAddress(&buffer, flags) }; if status != 0 { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "CVPixelBufferLockBaseAddress failed: {status}" ))); } @@ -2092,7 +2092,7 @@ pub mod d3d11 { use crate::{Color, Error, Size}; fn err(ctx: &str, e: windows::core::Error) -> Error { - Error::Codec(anyhow::anyhow!("{ctx}: {e}")) + Error::codec(anyhow::anyhow!("{ctx}: {e}")) } /// Create a hardware Direct3D11 device on `adapter`, or the default one, @@ -2121,7 +2121,7 @@ pub mod d3d11 { ) .map_err(|e| err("D3D11CreateDevice", e))?; } - let device = device.ok_or_else(|| Error::Codec(anyhow::anyhow!("D3D11CreateDevice returned null")))?; + let device = device.ok_or_else(|| Error::codec(anyhow::anyhow!("D3D11CreateDevice returned null")))?; let multithread = device .cast::() @@ -2213,7 +2213,7 @@ pub mod d3d11 { let mut desc = D3D11_TEXTURE2D_DESC::default(); unsafe { source.GetDesc(&mut desc) }; if desc.Format != DXGI_FORMAT_B8G8R8A8_UNORM || desc.Width < size.width || desc.Height < size.height { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "invalid WGC BGRA surface geometry or format" ))); } @@ -2276,7 +2276,7 @@ pub mod d3d11 { .CreateTexture2D(&desc, None, Some(&mut staging)) .map_err(|e| err("CreateTexture2D (staging)", e))?; } - let staging = staging.ok_or_else(|| Error::Codec(anyhow::anyhow!("CreateTexture2D returned null")))?; + let staging = staging.ok_or_else(|| Error::codec(anyhow::anyhow!("CreateTexture2D returned null")))?; unsafe { context.CopySubresourceRegion(&staging, 0, 0, 0, 0, &self.texture, 0, None); @@ -2365,7 +2365,7 @@ pub mod d3d11 { let scaler = { let mut scalers = SCALERS .lock() - .map_err(|_| Error::Codec(anyhow::anyhow!("video-processor cache lock poisoned")))?; + .map_err(|_| Error::codec(anyhow::anyhow!("video-processor cache lock poisoned")))?; scalers .get_or_insert_with(key, || { Ok::<_, std::convert::Infallible>(ScalerState::discover(device, plan)) @@ -2374,11 +2374,11 @@ pub mod d3d11 { }; let mut state = scaler .lock() - .map_err(|_| Error::Codec(anyhow::anyhow!("video processor lock poisoned")))?; + .map_err(|_| Error::codec(anyhow::anyhow!("video processor lock poisoned")))?; let result = match &*state { ScalerState::Ready(scaler) => scaler.scale(source), ScalerState::Unsupported { reason, .. } => { - return Err(Error::Codec(anyhow::anyhow!("GPU processing is unsupported: {reason}"))); + return Err(Error::codec(anyhow::anyhow!("GPU processing is unsupported: {reason}"))); } }; let texture = match result { @@ -2506,7 +2506,7 @@ pub mod d3d11 { let support = unsafe { enumerator.CheckVideoProcessorFormat(format) } .map_err(|e| err("CheckVideoProcessorFormat", e))?; if support & required.0 as u32 == 0 { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "video processor does not support {format:?} as {required:?}" ))); } @@ -2560,7 +2560,7 @@ pub mod d3d11 { let mut desc = D3D11_TEXTURE2D_DESC::default(); unsafe { source.GetDesc(&mut desc) }; if desc.Format != self.input_format { - return Err(ScaleError::Transient(Error::Codec(anyhow::anyhow!( + return Err(ScaleError::Transient(Error::codec(anyhow::anyhow!( "video processor input format changed" )))); } @@ -2584,7 +2584,7 @@ pub mod d3d11 { .map_err(|e| ScaleError::Unsupported(err("CreateVideoProcessorInputView", e)))?; } let input = - input.ok_or_else(|| ScaleError::Unsupported(Error::Codec(anyhow::anyhow!("input view is null"))))?; + input.ok_or_else(|| ScaleError::Unsupported(Error::codec(anyhow::anyhow!("input view is null"))))?; let output_desc = D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC { ViewDimension: D3D11_VPOV_DIMENSION_TEXTURE2D, @@ -2599,7 +2599,7 @@ pub mod d3d11 { .map_err(|e| ScaleError::Unsupported(err("CreateVideoProcessorOutputView", e)))?; } let view = - view.ok_or_else(|| ScaleError::Unsupported(Error::Codec(anyhow::anyhow!("output view is null"))))?; + view.ok_or_else(|| ScaleError::Unsupported(Error::codec(anyhow::anyhow!("output view is null"))))?; let streams = [D3D11_VIDEO_PROCESSOR_STREAM { Enable: true.into(), @@ -2649,7 +2649,7 @@ pub mod d3d11 { .CreateTexture2D(&desc, None, Some(&mut texture)) .map_err(|e| err("CreateTexture2D", e))?; } - texture.ok_or_else(|| Error::Codec(anyhow::anyhow!("CreateTexture2D returned null"))) + texture.ok_or_else(|| Error::codec(anyhow::anyhow!("CreateTexture2D returned null"))) } /// Upload packed I420 as an NV12 texture on `device`, the inverse of diff --git a/rs/moq-video/src/frame/cuda.rs b/rs/moq-video/src/frame/cuda.rs index bd441a13258..9e8e77020cc 100644 --- a/rs/moq-video/src/frame/cuda.rs +++ b/rs/moq-video/src/frame/cuda.rs @@ -76,7 +76,7 @@ fn kernels(ctx: &Arc) -> Result, Error> { })) }) .clone() - .map_err(|e| Error::Codec(anyhow::anyhow!("CUDA resize unavailable: {e}"))) + .map_err(|e| Error::codec(anyhow::anyhow!("CUDA resize unavailable: {e}"))) } /// An owned device allocation. Plain `cuMemAlloc` on purpose: NVENC's @@ -92,10 +92,10 @@ struct Raw { impl Raw { fn alloc(ctx: &Arc, len: usize) -> Result { ctx.bind_to_thread() - .map_err(|e| Error::Codec(anyhow::anyhow!("CUDA bind: {e:?}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("CUDA bind: {e:?}")))?; // SAFETY: a plain device allocation, freed exactly once by Drop. let ptr = unsafe { result::malloc_sync(len) } - .map_err(|e| Error::Codec(anyhow::anyhow!("CUDA alloc of {len} bytes: {e:?}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("CUDA alloc of {len} bytes: {e:?}")))?; Ok(Self { ctx: ctx.clone(), ptr, @@ -183,7 +183,7 @@ fn nv12_len(height: u32, pitch: u32) -> Result { .and_then(|luma| luma.checked_mul(3)) .map(|bytes| bytes / 2) .ok_or_else(|| { - Error::Codec(anyhow::anyhow!( + Error::codec(anyhow::anyhow!( "NV12 frame of {height} rows at pitch {pitch} is too large" )) }) @@ -196,7 +196,7 @@ fn nv12_len(height: u32, pitch: u32) -> Result { fn aligned_pitch(width: u32) -> Result { width .checked_next_multiple_of(256) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("frame width {width} is too wide for a CUDA NV12 pitch"))) + .ok_or_else(|| Error::codec(anyhow::anyhow!("frame width {width} is too wide for a CUDA NV12 pitch"))) } impl Frame { @@ -249,12 +249,12 @@ impl Frame { self.buf .ctx .bind_to_thread() - .map_err(|e| Error::Codec(anyhow::anyhow!("CUDA bind: {e:?}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("CUDA bind: {e:?}")))?; let mut host = vec![0u8; self.buf.len]; // SAFETY: the buffer is `len` bytes of device memory and stays alive // for the synchronous copy. unsafe { result::memcpy_dtoh_sync(&mut host, self.buf.ptr) } - .map_err(|e| Error::Codec(anyhow::anyhow!("CUDA download: {e:?}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("CUDA download: {e:?}")))?; let (w, h) = (self.width as usize, self.height as usize); let (cw, ch) = (w / 2, h / 2); @@ -322,7 +322,7 @@ impl Frame { let stream = ctx.default_stream(); let block = (16u32, 16, 1); let grid = |w: u32, h: u32| (w.div_ceil(16), h.div_ceil(16), 1); - let launch_err = |plane: &str, e| Error::Codec(anyhow::anyhow!("CUDA resize {plane}: {e:?}")); + let launch_err = |plane: &str, e| Error::codec(anyhow::anyhow!("CUDA resize {plane}: {e:?}")); // Luma plane: one thread per destination pixel. // @@ -378,7 +378,7 @@ impl Frame { // our stream), so wait for the kernels rather than queueing. stream .synchronize() - .map_err(|e| Error::Codec(anyhow::anyhow!("CUDA resize sync: {e:?}"))) + .map_err(|e| Error::codec(anyhow::anyhow!("CUDA resize sync: {e:?}"))) } } @@ -539,13 +539,13 @@ impl Slot { shared_mem_bytes: 0, }) } - .map_err(|e| Error::Codec(anyhow::anyhow!("CUDA color conversion: {e:?}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("CUDA color conversion: {e:?}")))?; // The frame may head straight to NVENC, which does not order against // our stream, and the caller may drop the image next; wait here. stream .synchronize() - .map_err(|e| Error::Codec(anyhow::anyhow!("CUDA color conversion sync: {e:?}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("CUDA color conversion sync: {e:?}")))?; Ok(dst) } diff --git a/rs/moq-video/src/frame/cuda_vulkan.rs b/rs/moq-video/src/frame/cuda_vulkan.rs index 46561a23b42..c6215df1431 100644 --- a/rs/moq-video/src/frame/cuda_vulkan.rs +++ b/rs/moq-video/src/frame/cuda_vulkan.rs @@ -40,7 +40,7 @@ impl Importer { completion.complete(reusable); } }) - .map_err(|e| Error::Codec(anyhow::anyhow!("start Vulkan/CUDA completion worker: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("start Vulkan/CUDA completion worker: {e}")))?; let thread_id = thread.thread().id(); Ok(Self { backend: Arc::new(Backend { @@ -356,5 +356,5 @@ fn timeline_signal(value: u64) -> sys::CUDA_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS { } fn cuda(action: &'static str) -> impl FnOnce(cudarc::driver::result::DriverError) -> Error { - move |error| Error::Codec(anyhow::anyhow!("{action}: {error:?}")) + move |error| Error::codec(anyhow::anyhow!("{action}: {error:?}")) } diff --git a/rs/moq-video/src/frame/pool.rs b/rs/moq-video/src/frame/pool.rs index ee6b9a00ec5..3939f34b0d9 100644 --- a/rs/moq-video/src/frame/pool.rs +++ b/rs/moq-video/src/frame/pool.rs @@ -246,7 +246,7 @@ mod tests { fn alloc(&self, _len: usize) -> Result<(), Error> { if self.0.swap(false, Ordering::Relaxed) { - return Err(Error::Codec(anyhow::anyhow!("out of device memory"))); + return Err(Error::codec(anyhow::anyhow!("out of device memory"))); } Ok(()) } diff --git a/rs/moq-video/src/frame/vaapi.rs b/rs/moq-video/src/frame/vaapi.rs index 38b93949111..38a2388d557 100644 --- a/rs/moq-video/src/frame/vaapi.rs +++ b/rs/moq-video/src/frame/vaapi.rs @@ -111,11 +111,11 @@ pub(crate) fn device(codec: Codec) -> Option<&'static Path> { pub(crate) fn import(buffer: &DmaBuf) -> Result<(VaapiDmaBuf, DmaBufExport), Error> { let export = buffer .export() - .map_err(|e| Error::Codec(anyhow::anyhow!("export a DMA-BUF for VA-API: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("export a DMA-BUF for VA-API: {e}")))?; let fd: OwnedFd = export .as_fd() .try_clone_to_owned() - .map_err(|e| Error::Codec(anyhow::anyhow!("duplicate a DMA-BUF descriptor: {e}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("duplicate a DMA-BUF descriptor: {e}")))?; let descriptor = VaapiDmaBuf { drm_format: buffer.format().as_raw(), modifier: buffer.modifier(), @@ -229,10 +229,10 @@ pub(crate) fn resize(buffer: &DmaBuf, size: Size) -> Result { drop(lease); resized.map_err(|e| { REFUSED.with(|refused| refused.borrow_mut().insert(key)); - Error::Codec(e.context("scale a DMA-BUF with the VA-API video processor")) + Error::codec(e.context("scale a DMA-BUF with the VA-API video processor")) }) })?; - adopt(exported, label).map_err(Error::Codec) + adopt(exported, label).map_err(Error::codec) } /// Describes an exported picture as a [`DmaBuf`]: the driver's format modifier, @@ -338,7 +338,7 @@ impl DmaBufFrame for Exported { let frame = self.frame.lock().expect("poisoned"); let nv12 = frame .download() - .map_err(|e| Error::Codec(anyhow::anyhow!("read a VA-API surface back: {e:?}")))?; + .map_err(|e| Error::codec(anyhow::anyhow!("read a VA-API surface back: {e:?}")))?; let i420 = I420::from_nv12(&nv12.data, crate::Size::new(nv12.width, nv12.height))?; Ok(match self.color { Some(color) => i420.with_color(color), diff --git a/rs/moq-video/src/frame/vulkan.rs b/rs/moq-video/src/frame/vulkan.rs index 94cacf7c242..3e7c379b1e9 100644 --- a/rs/moq-video/src/frame/vulkan.rs +++ b/rs/moq-video/src/frame/vulkan.rs @@ -71,8 +71,8 @@ impl Handles { )] fn try_clone(&self) -> Result { Ok(Self { - memory: self.memory.try_clone().map_err(|e| Error::Codec(e.into()))?, - timeline: self.timeline.try_clone().map_err(|e| Error::Codec(e.into()))?, + memory: self.memory.try_clone().map_err(|e| Error::codec(e.into()))?, + timeline: self.timeline.try_clone().map_err(|e| Error::codec(e.into()))?, }) } } diff --git a/rs/moq-video/src/mf.rs b/rs/moq-video/src/mf.rs index 7f4787f835c..b54fa8a758e 100644 --- a/rs/moq-video/src/mf.rs +++ b/rs/moq-video/src/mf.rs @@ -12,7 +12,7 @@ use crate::frame::d3d11; /// Wrap a `windows` error with context as a codec error. pub(crate) fn mf_err(ctx: &str, e: windows::core::Error) -> Error { - Error::Codec(anyhow::anyhow!("{ctx}: {e}")) + Error::codec(anyhow::anyhow!("{ctx}: {e}")) } /// Pack two 32-bit values into the high/low halves of a u64, the layout Media @@ -42,7 +42,7 @@ pub(crate) fn create_d3d_device() -> Result<(ID3D11Device, IMFDXGIDeviceManager) unsafe { MFCreateDXGIDeviceManager(&mut token, &mut manager).map_err(|e| mf_err("MFCreateDXGIDeviceManager", e))?; } - let manager = manager.ok_or_else(|| Error::Codec(anyhow::anyhow!("MFCreateDXGIDeviceManager returned null")))?; + let manager = manager.ok_or_else(|| Error::codec(anyhow::anyhow!("MFCreateDXGIDeviceManager returned null")))?; unsafe { manager .ResetDevice(&device, token) diff --git a/rs/moq-video/src/render/device.rs b/rs/moq-video/src/render/device.rs index a96e87d5329..03dd513bb82 100644 --- a/rs/moq-video/src/render/device.rs +++ b/rs/moq-video/src/render/device.rs @@ -25,7 +25,7 @@ pub async fn request_device( adapter .request_device(&descriptor) .await - .map_err(|e| Error::Render(anyhow::Error::new(e).context("open the GPU"))) + .map_err(|e| Error::render(anyhow::Error::new(e).context("open the GPU"))) } /// Open a Vulkan device with `VK_KHR_external_semaphore_fd` added, or `None` @@ -64,11 +64,11 @@ fn vulkan( })), ) } - .map_err(|e| Error::Render(anyhow::Error::new(e).context("open the Vulkan device")))?; + .map_err(|e| Error::render(anyhow::Error::new(e).context("open the Vulkan device")))?; drop(hal); // SAFETY: `open` came from this adapter with exactly the descriptor's // features. let opened = unsafe { adapter.create_device_from_hal(open, descriptor) } - .map_err(|e| Error::Render(anyhow::Error::new(e).context("open the GPU")))?; + .map_err(|e| Error::render(anyhow::Error::new(e).context("open the GPU")))?; Ok(Some(opened)) } diff --git a/rs/moq-video/src/render/dmabuf.rs b/rs/moq-video/src/render/dmabuf.rs index 2475bc763f9..7a484a693e3 100644 --- a/rs/moq-video/src/render/dmabuf.rs +++ b/rs/moq-video/src/render/dmabuf.rs @@ -28,7 +28,7 @@ use super::source::{Layout, Source}; use crate::{Color, DmaBuf, DmaBufPlane, DrmFormat, Error, Size}; fn err(message: impl std::fmt::Display) -> Error { - Error::Render(anyhow::anyhow!("{message}")) + Error::render(anyhow::anyhow!("{message}")) } /// Alias one DMA-BUF allocation as the sampled Vulkan textures of a frame. @@ -53,7 +53,7 @@ pub(super) fn import(device: &wgpu::Device, buffer: &DmaBuf) -> Result Error { - Error::Render(anyhow::anyhow!("{message}")) + Error::render(anyhow::anyhow!("{message}")) } /// An encoder [`Exporter::new`] may settle on. diff --git a/rs/moq-video/src/render/metal.rs b/rs/moq-video/src/render/metal.rs index 060a8bf7bc0..e060cef4436 100644 --- a/rs/moq-video/src/render/metal.rs +++ b/rs/moq-video/src/render/metal.rs @@ -28,7 +28,7 @@ use crate::frame::apple::PixelBuffer; use crate::{Color, Error, Size}; fn err(message: impl std::fmt::Display) -> Error { - Error::Render(anyhow::anyhow!("{message}")) + Error::render(anyhow::anyhow!("{message}")) } /// Holds the CoreVideo wrapper for as long as wgpu keeps the texture made from diff --git a/rs/moq-video/src/render/readback.rs b/rs/moq-video/src/render/readback.rs index 440d5d24a95..0a6ca95f747 100644 --- a/rs/moq-video/src/render/readback.rs +++ b/rs/moq-video/src/render/readback.rs @@ -15,7 +15,7 @@ use crate::convert::Rgba; use crate::{Error, Size}; fn err(message: impl std::fmt::Display) -> Error { - Error::Render(anyhow::anyhow!("{message}")) + Error::render(anyhow::anyhow!("{message}")) } /// Copies textures back to the CPU, through a small pool of staging buffers. diff --git a/rs/moq-video/src/render/renderer.rs b/rs/moq-video/src/render/renderer.rs index 061ba9557a9..1ed5a169538 100644 --- a/rs/moq-video/src/render/renderer.rs +++ b/rs/moq-video/src/render/renderer.rs @@ -138,7 +138,7 @@ impl Completion { drop(keepalive); } }) - .map_err(|err| Error::Render(anyhow::anyhow!("start GPU completion worker: {err}")))?; + .map_err(|err| Error::render(anyhow::anyhow!("start GPU completion worker: {err}")))?; Ok(Self { device: device.clone(), diff --git a/rs/moq-video/src/size.rs b/rs/moq-video/src/size.rs index bdcfedd9261..0d4e8940862 100644 --- a/rs/moq-video/src/size.rs +++ b/rs/moq-video/src/size.rs @@ -28,7 +28,7 @@ impl Size { usize::try_from(self.pixels()) .ok() .and_then(|pixels| pixels.checked_mul(bytes_per_pixel)) - .ok_or_else(|| Error::Codec(anyhow::anyhow!("{what} {self}: byte length is too large to represent"))) + .ok_or_else(|| Error::codec(anyhow::anyhow!("{what} {self}: byte length is too large to represent"))) } /// Reject anything the I420 pipeline can't represent. @@ -39,7 +39,7 @@ impl Size { pub(crate) fn validate(&self, what: &str) -> Result<(), Error> { self.validate_nonzero(what)?; if !self.width.is_multiple_of(2) || !self.height.is_multiple_of(2) { - return Err(Error::Codec(anyhow::anyhow!("{what} {self}: dimensions must be even"))); + return Err(Error::codec(anyhow::anyhow!("{what} {self}: dimensions must be even"))); } Ok(()) } @@ -63,7 +63,7 @@ impl Size { && usize::try_from(pixels).is_ok_and(|pixels| pixels.checked_mul(4).is_some()); if !representable { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "{what} {self} at {framerate}fps: dimensions too large to represent" ))); } @@ -75,7 +75,7 @@ impl Size { /// window). pub(crate) fn validate_nonzero(&self, what: &str) -> Result<(), Error> { if self.width == 0 || self.height == 0 { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "{what} {self}: dimensions must be non-zero" ))); } diff --git a/rs/moq-video/src/v4l2.rs b/rs/moq-video/src/v4l2.rs index 42de5b21b3c..4315f7e3cc3 100644 --- a/rs/moq-video/src/v4l2.rs +++ b/rs/moq-video/src/v4l2.rs @@ -206,7 +206,7 @@ pub(crate) fn open(role: &Role) -> Result { } } - Err(Error::Codec(anyhow::anyhow!( + Err(Error::codec(anyhow::anyhow!( "no V4L2 M2M node converts {} to {} (set {} to name one; tried {})", join_fourcc(role.input), join_fourcc(role.output), @@ -245,7 +245,7 @@ impl Device { .write(true) .custom_flags(libc::O_NONBLOCK) .open(path) - .map_err(|err| Error::Codec(anyhow::anyhow!("{}: open: {err}", path.display())))?; + .map_err(|err| Error::codec(anyhow::anyhow!("{}: open: {err}", path.display())))?; let device = Self { file, @@ -260,12 +260,12 @@ impl Device { 0 => "not an M2M device", _ => "single-planar M2M only", }; - return Err(Error::Codec(anyhow::anyhow!("{}: {what}", path.display()))); + return Err(Error::codec(anyhow::anyhow!("{}: {what}", path.display()))); } // Everything below streams MMAP buffers, so a node without streaming I/O is // the wrong node however its formats enumerate. if caps & V4L2_CAP_STREAMING == 0 { - return Err(Error::Codec(anyhow::anyhow!("{}: no streaming I/O", path.display()))); + return Err(Error::codec(anyhow::anyhow!("{}: no streaming I/O", path.display()))); } Ok(device) } @@ -275,7 +275,7 @@ impl Device { for (dir, wanted) in [(Dir::Output, role.input), (Dir::Capture, role.output)] { let offered = self.formats(dir)?; if !wanted.iter().any(|code| offered.contains(code)) { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "offers {} where {} is needed", join_fourcc(&offered), join_fourcc(wanted) @@ -303,7 +303,7 @@ impl Device { /// Wrap an ioctl failure with the node and the operation that failed. fn err(&self, what: impl std::fmt::Display, err: std::io::Error) -> Error { - Error::Codec(anyhow::anyhow!("{}: {what}: {err}", self.path.display())) + Error::codec(anyhow::anyhow!("{}: {what}: {err}", self.path.display())) } /// The capabilities of this node specifically, rather than of the driver as a @@ -1070,7 +1070,7 @@ impl Planes { let size = rect.size; size.validate("V4L2 4:2:0 frame")?; if rect.left + size.width > format.size.width || rect.top + size.height > format.size.height { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "V4L2 negotiated {} for a {size} picture at {},{}", format.size, rect.left, @@ -1086,7 +1086,7 @@ impl Planes { NV12 | NV12M => true, YUV420 | YUV420M => false, other => { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "V4L2 chose the unsupported raw format {}", name(other) ))); @@ -1096,7 +1096,7 @@ impl Planes { let luma = *format .planes .first() - .ok_or_else(|| Error::Codec(anyhow::anyhow!("V4L2 reported a format with no planes")))?; + .ok_or_else(|| Error::codec(anyhow::anyhow!("V4L2 reported a format with no planes")))?; let stride = luma.stride.max(format.size.width) as usize; let y = Component { plane: 0, @@ -1149,7 +1149,7 @@ impl Planes { ) } (false, true) => { - return Err(Error::Codec(anyhow::anyhow!( + return Err(Error::codec(anyhow::anyhow!( "V4L2 chose planar {} with {} planes", name(format.pixelformat), format.planes.len() @@ -1318,7 +1318,7 @@ fn deinterleave(u: &mut [u8], v: &mut [u8], src: &[u8], at: Component, width: us } fn short(len: usize, needed: usize) -> Error { - Error::Codec(anyhow::anyhow!( + Error::codec(anyhow::anyhow!( "V4L2 buffer of {len} bytes is too small for the {needed} its format implies" )) } From 20a954861f57a299bd0be892123f503fda023f84 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 8 Nov 2026 10:40:59 -0800 Subject: [PATCH 7/7] test(moq-audio): match the failed status's plain Error Co-Authored-By: Claude Opus 5.5 --- rs/moq-audio/src/encode/capture.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rs/moq-audio/src/encode/capture.rs b/rs/moq-audio/src/encode/capture.rs index a8235289ad5..af802e637fe 100644 --- a/rs/moq-audio/src/encode/capture.rs +++ b/rs/moq-audio/src/encode/capture.rs @@ -781,7 +781,7 @@ mod tests { assert!(matches!(result, Err(Error::Capture(message)) if message == "device lost")); } assert!( - matches!(publication.state().status(), Status::Failed(error) if matches!(&**error, Error::Capture(message) if message == "device lost")) + matches!(publication.state().status(), Status::Failed(error) if matches!(error, Error::Capture(message) if message == "device lost")) ); assert!(publication.is_finished()); }) @@ -860,7 +860,7 @@ mod tests { samples.try_push(Err(Error::Capture("device lost".into()))).unwrap(); status(&mut publication, |status| matches!(status, Status::Failed(_))).await; assert!( - matches!(publication.state().status(), Status::Failed(error) if matches!(&**error, Error::Capture(message) if message == "device lost")) + matches!(publication.state().status(), Status::Failed(error) if matches!(error, Error::Capture(message) if message == "device lost")) ); // Failure is terminal, but the publication still serves what it retained. assert!(!publication.is_finished());