Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
509516d
chore(quest): claim quest/m2/video-producer.md
kixelated Nov 16, 2026
aaad5db
refactor(moq-video)!: the encoded-frame publisher is encode::Publisher
kixelated Nov 16, 2026
219f410
feat(moq-video)!: encode::Producer takes raw frames and follows the g…
kixelated Nov 16, 2026
7c2963e
refactor: moq-ffi, moq-c, and moq-boy publish video through encode::P…
kixelated Nov 16, 2026
f0f7710
fix(moq-video): a manual retune hands the encoder back to the grant
kixelated Nov 16, 2026
193363a
fix(moq-video): a rung whose resume retune is refused waits for its f…
kixelated Nov 16, 2026
5d2dde6
fix(moq-video): a deferred rung resume keeps the grant in step
kixelated Nov 17, 2026
33ce388
Merge remote-tracking branch 'origin/main' into HEAD
kixelated Nov 17, 2026
1f83ae5
docs(moq-video): an unwatched rendition encodes at its ceiling, outsi…
kixelated Nov 17, 2026
7e98d8f
Merge main into quest/m2/video-producer
kixelated Nov 17, 2026
7300e8e
quest: a session's handle is gone at its terminal callback, by contract
kixelated Nov 17, 2026
d03ca0f
quest: moq-c's grants parity test closes its client after the server too
kixelated Nov 17, 2026
5fb9646
Merge main into quest/m2/video-producer
kixelated Nov 17, 2026
fccc6ea
fix(moq-video): a failed resume at the ceiling waits a retry step
kixelated Nov 17, 2026
4703f69
fix(moq-ffi): a video producer's reservation handle no longer holds i…
kixelated Nov 17, 2026
9165844
fix(moq-ffi): the audio producer's reservation handle no longer holds…
kixelated Nov 17, 2026
7f37062
Merge remote-tracking branch 'origin/main' into HEAD
kixelated Nov 17, 2026
af43ec3
fix(moq-c): an encoder's reservation handle views its claim, released…
kixelated Nov 17, 2026
79f622b
Merge branch 'main' of https://github.com/moq-dev/moq into HEAD
kixelated Nov 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion doc/lib/rs/moq-video.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ build with the `v4l2` feature keeps every `Auto` or `Hardware` selection to its
GOP, whichever encoder it lands on, since a V4L2 driver may refuse the request.
Backends that can
change bitrate live follow the connection's send estimate while someone
watches, and return to the configured bitrate while nobody does. Where the platform
watches, and return to the configured bitrate while nobody does.

To publish frames from your own source, `encode::Producer` takes raw frames,
owns the encoder, and follows the same send estimate between frames, like
`moq_audio::encode::Producer`, once `Options::bandwidth` carries the
connection's allocator; the default holds the configured bitrate. Chain renditions through `Options::lower` for
simulcast: a higher rung stops encoding and advertises itself disabled while the
connection cannot fund it, then resumes on a keyframe. `encode::Publisher` is
the layer below, for frames you encoded yourself. Where the platform
allows it, frames stay on the GPU between capture, decode, encode, and render.

Opt-in features: `capture`, `pipewire` (Linux portal capture and PipeWire
Expand Down
32 changes: 32 additions & 0 deletions quest/m0/test-flakes-3/c-server-accepts-session.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# [S] libmoq's session tests close their client after the server did

## Goal

`rs/libmoq/src/test.rs::server_accepts_a_session` and `rs/libmoq/src/test/parity.rs::session_tokens_and_grants` pass every run.

## Plan

Found 2026-11-16 by `just check` on
[moq-dev/moq#8964](https://github.com/moq-dev/moq/pull/8964), whose change
does not touch libmoq's session or server code. Running it alone with
`cargo nextest run -p libmoq server_accepts_a_session` fails 4 of 12 runs at
main 73d65edbf7: `moq_session_close(client)`, after the accepted session
closed and its tail drained, returns -21 where the test expects 0.

The client's session likely already ended on its own once the server side
closed, so its id is gone by the time the test closes it. That release is the
contract: `rs/libmoq/src/session.rs` removes a session's entry before its
terminal `CLOSED` callback, so `moq_session_close` on a handle whose terminal
callback has fired (or is about to) returns a negative code. Find why the client
ends: if a dialed client is meant to keep reconnecting after its server closes
the accepted session, its early end is a library bug, fixed there with a
regression test. Otherwise the test changes: it waits on the client's terminal
callback and drops the `moq_session_close(client) == 0` assertion, or asserts
the close is negative after that callback, as `moq_server_close` already does.

`session_tokens_and_grants` has the same shape: it closes the accepted session,
waits for its terminal callback, then asserts `moq_session_close(client) == 0`
(`parity.rs:530`). It fails 3 of 8 runs alone at main a79bbae919, found while
merging main into #8964. Fix both the same way.

Public API: none. Wire: none.
3 changes: 1 addition & 2 deletions quest/m2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ removal, a rename, or a wire, catalog, or format change that is free until
(the `sync` section, a version per entry, no video `warmup`, `m2ts` under
`mpegts`, one strict rule);
[declared ends](/quest/m2/time-addressing/consumers.md), back from m3.
- **Native media and gateways.** [The video producer](/quest/m2/video-producer.md) and
[upstream str0m](https://github.com/moq-dev/moq/pull/8963), which reverses the
- **Native media and gateways.** [Upstream str0m](https://github.com/moq-dev/moq/pull/8963), which reverses the
2026-10-30 vendoring.
- **Bindings.** The C API as `libmoq` (done), reversing 2026-10-25.
The bindings break twice by decision: at 0.22, and again at
Expand Down
72 changes: 0 additions & 72 deletions quest/m2/video-producer.md

This file was deleted.

36 changes: 36 additions & 0 deletions quest/m3/video-unwatched-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# [S] An unwatched video rendition rests

## Goal

A `moq_video::encode::Producer` rendition with no `lower` stops spending on
frames nobody watches: while its track has no subscriber it does not encode at
its full ceiling, and the next viewer still joins on a keyframe without a burst
of frames encoded at a rate the grant would not have funded.

## Plan

Accepted 2026-11-16 (maintainer, on
[moq-dev/moq#8964](https://github.com/moq-dev/moq/pull/8964#issuecomment-6931517209)):
#8964 lifted the capture driver's rule into `encode::Producer`, so moq-ffi's
`MoqVideoProducer`, libmoq's `moq_encode_video*` and moq-boy now also encode an
unwatched rendition at its ceiling (`Producer::rest` in
`rs/moq-video/src/encode/producer.rs`), where the bindings used to follow the
grant regardless. Its groups wait in the track cache, so the cost is encoder
work and a join that starts on a group encoded at the ceiling. m3, as a fix to
behaviour #8964 shipped.

Decided 2026-11-16 (maintainer, on
[moq-dev/moq#8964](https://github.com/moq-dev/moq/pull/8964#issuecomment-6932840632)):
✅ rest by closing the encoder until a subscriber arrives (rejected: encoding
at the policy floor, which still encodes every frame; encoding at the last
granted rate, the bindings' behaviour before #8964).

While unwatched, close the encoder like a disabled rung but keep
the rendition advertised enabled; the first write after a subscriber arrives
reopens it, which opens on a keyframe on every backend, at the rate the grant
then funds. A simulcast rung keeps following the grant as it does now, since it
needs the grant's slice to resume. `publish_capture` keeps reporting its state.

Tests: an unwatched producer encodes nothing and holds no encoder; a viewer
arriving gets a group starting on a keyframe at the granted rate; a viewer
leaving and returning does the same.
6 changes: 3 additions & 3 deletions rs/libmoq/src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,15 +433,15 @@ pub unsafe extern "C" fn moq_encode_audio(

/// This encoder's bandwidth reservation, or 0 if it was published without one.
///
/// Closing the returned handle does not release the encoder's claim; that lasts
/// until [moq_encode_audio_finish].
/// The handle views the encoder's claim: closing it does not release the claim, and
/// keeping it does not hold it past [moq_encode_audio_finish], after which it reads no rate.
#[unsafe(no_mangle)]
pub extern "C" fn moq_encode_audio_reservation(producer: u32) -> i32 {
ffi::enter(move || {
let producer = ffi::parse_id(producer)?;
let mut state = State::lock();
match state.audio.reservation(producer)? {
Some(reservation) => Ok(i32::from(state.bandwidth.hold(reservation)?)),
Some(reservation) => Ok(i32::from(state.bandwidth.borrow(&reservation)?)),
None => Ok(0),
}
})
Expand Down
55 changes: 40 additions & 15 deletions rs/libmoq/src/bandwidth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{Error, Id, NonZeroSlab, State, ffi};
#[derive(Default)]
pub(crate) struct Bandwidth {
allocators: NonZeroSlab<moq_net::bandwidth::Allocator>,
reservations: NonZeroSlab<Arc<moq_net::bandwidth::Reservation>>,
reservations: NonZeroSlab<Claim>,
consumers: NonZeroSlab<Arc<Consumer>>,
viewers: NonZeroSlab<moq_net::bandwidth::Viewers>,
controls: NonZeroSlab<RateControl>,
Expand All @@ -33,6 +33,15 @@ impl RateControl {
}
}

/// Who keeps a reservation handle's claim alive.
enum Claim {
/// From [`moq_allocator_reserve`]: the handle is the claim.
Owned(Arc<moq_net::bandwidth::Reservation>),
/// A producer's own claim. Weak, so a handle the caller keeps cannot hold the room
/// after the producer finishes or moves to another track.
Borrowed(std::sync::Weak<moq_net::bandwidth::Reservation>),
}

impl Bandwidth {
pub fn insert(&mut self, allocator: moq_net::bandwidth::Allocator) -> Result<Id, Error> {
self.allocators.insert(allocator)
Expand All @@ -50,15 +59,20 @@ impl Bandwidth {
pub fn reserve(&mut self, allocator: Id, demand: &moq_net::track::Demand, max_bps: u64) -> Result<Id, Error> {
let allocator = self.allocator(allocator)?;
let reservation = allocator.reserve(demand, moq_net::bandwidth::Rate::from_bps(max_bps));
self.reservations.insert(Arc::new(reservation))
self.reservations.insert(Claim::Owned(Arc::new(reservation)))
}

pub fn hold(&mut self, reservation: Arc<moq_net::bandwidth::Reservation>) -> Result<Id, Error> {
self.reservations.insert(reservation)
/// A handle viewing a claim `owner` holds, released when the owner drops it.
pub fn borrow(&mut self, owner: &Arc<moq_net::bandwidth::Reservation>) -> Result<Id, Error> {
self.reservations.insert(Claim::Borrowed(Arc::downgrade(owner)))
}

pub fn reservation(&self, id: Id) -> Result<Arc<moq_net::bandwidth::Reservation>, Error> {
self.reservations.get(id).cloned().ok_or(Error::NotFound)
/// The handle's claim, or `None` once the producer that owned it released it.
pub fn reservation(&self, id: Id) -> Result<Option<Arc<moq_net::bandwidth::Reservation>>, Error> {
Ok(match self.reservations.get(id).ok_or(Error::NotFound)? {
Claim::Owned(inner) => Some(inner.clone()),
Claim::Borrowed(inner) => inner.upgrade(),
})
}

pub fn reservation_close(&mut self, id: Id) -> Result<(), Error> {
Expand Down Expand Up @@ -115,6 +129,7 @@ pub extern "C" fn moq_allocator_reserve(allocator: u32, track: u32, max_bps: u64
/// `present` is false when there is no estimate: hold the current rate. `present`
/// true and `bps` 0 is a real zero rate. While nothing subscribes to the track,
/// `bps` is the slice it would get back, without claiming it from the others.
/// `present` is false too once the producer that owns this claim releases it.
///
/// # Safety
/// - `bps` and `present` must be valid pointers.
Expand All @@ -124,7 +139,11 @@ pub unsafe extern "C" fn moq_reservation_rate(reservation: u32, bps: *mut u64, p
let reservation = ffi::parse_id(reservation)?;
let bps = unsafe { bps.as_mut() }.ok_or(Error::InvalidPointer)?;
let present = unsafe { present.as_mut() }.ok_or(Error::InvalidPointer)?;
match State::lock().bandwidth.reservation(reservation)?.rate() {
match State::lock()
.bandwidth
.reservation(reservation)?
.and_then(|inner| inner.rate())
{
Some(rate) => {
*bps = rate.as_bps();
*present = true;
Expand All @@ -138,23 +157,24 @@ pub unsafe extern "C" fn moq_reservation_rate(reservation: u32, bps: *mut u64, p
})
}

/// Change the ceiling, keeping the same claim.
/// Change the ceiling, keeping the same claim. Does nothing once the producer that
/// owns this claim releases it.
#[unsafe(no_mangle)]
pub extern "C" fn moq_reservation_update(reservation: u32, max_bps: u64) -> i32 {
ffi::enter(move || {
let reservation = ffi::parse_id(reservation)?;
State::lock()
.bandwidth
.reservation(reservation)?
.update(moq_net::bandwidth::Rate::from_bps(max_bps));
if let Some(inner) = State::lock().bandwidth.reservation(reservation)? {
inner.update(moq_net::bandwidth::Rate::from_bps(max_bps));
}
Ok(())
})
}

/// Release a reservation, handing its share back to siblings.
///
/// Closing an accessor returned by a video or audio producer does not release
/// the encoder's claim; that lasts until the producer is finished.
/// the encoder's claim, and keeping it open does not hold it: the claim lasts
/// until the producer is finished.
#[unsafe(no_mangle)]
pub extern "C" fn moq_reservation_close(reservation: u32) -> i32 {
ffi::enter(move || {
Expand Down Expand Up @@ -472,7 +492,8 @@ impl From<moq_mux::rate::Target> for moq_rate_target {

/// Create a rate control following this reservation, starting at the policy's maximum bitrate.
///
/// A zero `max_bps` is rejected with `MOQ_ERROR_INVALID_CONFIG`.
/// A zero `max_bps` is rejected with `MOQ_ERROR_INVALID_CONFIG`, and a claim its producer
/// already released with `MOQ_ERROR_NOT_FOUND`.
#[unsafe(no_mangle)]
pub extern "C" fn moq_rate_control_create(reservation: u32, policy: moq_rate_policy) -> i32 {
ffi::enter(move || {
Expand All @@ -484,7 +505,11 @@ pub extern "C" fn moq_rate_control_create(reservation: u32, policy: moq_rate_pol
.has_lower
.then(|| moq_net::bandwidth::Rate::from_bps(policy.lower_bps));
let mut state = State::lock();
let grant = state.bandwidth.reservation(ffi::parse_id(reservation)?)?.consume();
let grant = state
.bandwidth
.reservation(ffi::parse_id(reservation)?)?
.ok_or(Error::NotFound)?
.consume();
state.bandwidth.controls.insert(RateControl {
inner: moq_mux::rate::Control::new(native),
grant,
Expand Down
Loading