Skip to main content

Crate moq_net

Crate moq_net 

Source
Expand description

§moq-net: Media over QUIC networking layer

moq-net is the networking layer for Media over QUIC: real-time pub/sub with built-in caching, fan-out, and prioritization, on top of QUIC. Sub-second latency at massive scale. At session setup it negotiates one of two wire protocols: the simplified moq-lite protocol (the default) or the full IETF moq-transport protocol.

§Features

The async feature, off by default, enables the generated async wrappers for the poll_* methods, the announcement Stream adapter, time::run, and the Lite/IETF sessions (Client, Server, Session, and the session Driver). Without it, use the model’s poll_* methods and origin::Driver with explicit time. Origin serving stays available without a native runtime; no executor or clock is chosen by the model.

§API

The API is built around Producer/Consumer pairs, with the hierarchy:

Each level lives in its own module (broadcast, track, group, frame, origin, announce) that owns the short Producer / Consumer / Info names.

meta reads a track’s groups and each frame’s header without the payload, as a subscription of its own beside payload subscriptions (broadcast::Consumer::meta).

Traffic counters for the levels above live in stats: build a stats::Registry and hand each session a stats::Session via Client::with_stats / Server::with_stats. Publishing the counters as MoQ broadcasts lives in the moq-stats crate.

§Wire invariants

  • Stream identities: a subscription stays on its starting identity while that identity has a route, even after a newer route wins the path. When none remains, it ends with an error (never a clean end, never stitched onto another identity). A clean end means only a deliberate publisher finish; apps restart on announcements, never request errors.
  • Live position: largest group / live edge comes only from an open upstream subscription (SUBSCRIBE_OK); a copy with none open waits, and a partial group hit still asks upstream for missing frames.
  • Group expiry: groups never overlap in time (frames within a group may reorder for B-frames). Expire a group by timestamp (from GROUP_END once whole, else lowest timestamp known in any higher group) and by wall clock once its successor arrived longer ago than the budget; the latest group never expires.
  • Route switch: for the same identity, split at one boundary (largest group any copy holds, past its contiguous frames): the new subscription starts there while the old caps there and drains below within budget, delivering each frame once from whichever arrives first.

§Compatibility

The API exposes the intersection of features supported by both protocols, intentionally keeping it small rather than polluting it with half-baked features.

The library is forwards-compatible with the full IETF specification and supports moq-transport drafts 18+ via version negotiation. Everything will work perfectly, so long as your application uses the API as defined above.

For example, there’s no concept of “sub-group”. When connecting to a moq-transport implementation, we use sub-group=0 for all frames and silently drop any received frames not in sub-group=0. If your application genuinely needs multiple sub-groups, tell me why and we can figure something out.

§Producers and Consumers

Each level of the hierarchy is split into a Producer / Consumer pair:

  • The Producer is the writer: it appends new state (publishes a broadcast, starts a group, writes frames, closes a track).
  • The Consumer is a reader: each consumer holds its own independent view of the producer’s state, with its own cursor through the stream.

Both halves are cheaply clonable so you can hand out multiple handles. Cloning a consumer creates another reader (each at its own cursor); cloning a producer gives another writer that contributes to the same shared state. Closing the last producer signals consumers that no more updates are coming.

§Driving and time

Session and origin drivers never spawn tasks or read the clock. Minting an Identity uses secure randomness. Client::connect and Server::accept take an initial time::Instant and return (Session, Driver). Poll the Driver with the current instant and a [kio::Waiter], then wake on external activity or at the deadline it returns; time::run does exactly that on tokio or the browser. The last Session drop requests closure; dropping the driver cancels it.

origin::Producer::new also returns a producer and driver. Its driver runs route changes, serving, linger, teardown, and the origin’s cache expiration. Standalone caches expose cache::Pool::poll. Frame read/write methods clear their expiration timestamp for the next cleanup pass. Datagram tracks keep a short buffer and no cache; model read/write APIs take no time argument, though a frame write stamps its arrival from the ambient clock (frame::Frame::created).

Both drivers implement time::Driver. moq-uring drives thread-local transports on its own timer heap. Tests advance time by supplying a later instant.

Re-exports§

pub use path::InvalidPrefix;
pub use path::Path;
pub use path::Prefix;
pub use path::Prefixes;
pub use reconnect::Reconnect;
pub use server::Server;
pub use session::Session;

Modules§

announce
Listing the paths announced on an origin.
auth
In-band authorization: present tokens to the peer and learn what they grant.
bandwidth
Rate estimation, split into a Producer and Consumer handle.
broadcast
A broadcast is a named collection of tracks, split into a Producer and Consumer handle.
cache
A shared byte budget for cached groups, repaid by write-time eviction.
datagram
Datagram tracks: live, unreliable payloads addressed by their own sequence.
frame
Frames are the leaf of the model: a sized, optionally timestamped payload within a group.
goaway
GOAWAY, the graceful drain signal.
group
A group is a stream of frames, split into a Producer and Consumer handle.
meta
Metadata subscriptions: a track’s group and frame progress without its payload.
origin
Routing broadcasts into an origin, and consuming them through it.
path
Broadcast paths and the prefixes that grant them.
reconnect
Client connect policy: which of a peer’s addresses to dial, how long each dial may take, when to back off and when to give up, and how to follow a GOAWAY.
server
Accepting a MoQ session, including the paused handshake that inspects the peer’s SETUP before granting origins.
session
A MoQ session handle and a snapshot of its connection statistics.
setup
The SETUP exchange and the credential a peer may present in it.
stats
Traffic counter collection for moq-net sessions.
time
Caller-supplied time for protocol and model drivers.
track
A track is a collection of semi-reliable and semi-ordered streams, split into a Producer and Subscriber handle.
transport
The poll transport interface owned by moq-net.
varint
Variable-length integers: QUIC’s two-bit length tag, and moq-transport’s leading ones.

Structs§

BoundsExceeded
The number does not fit the target: a varint wire form or a narrower integer.
Client
A MoQ client session builder.
Driver
Drives a session with caller-supplied time.
Identity
Names one continuous stream at a path.
InvalidIdentity
The identity is empty or longer than 256 UTF-8 bytes.
TimeOverflow
Returned when a Timestamp operation would exceed its range (2^61 - 1), a Timescale would exceed the QUIC varint range (2^62 - 1), scale conversion or arithmetic would overflow, arithmetic mixes timestamps with mismatched scales, or a conversion needs the scale an unscaled Timestamp lacks.
Timescale
Units per second used by a track for frame timestamps.
Timestamp
A timestamp in a track’s timescale (units per second), or a scale-less track’s group key.
Version
A MoQ protocol version, such as moq-lite-06.
Versions
A set of supported MoQ versions.

Enums§

DecodeError
A decode error.
EncodeError
An error that occurs during encoding.
Error
Failures in this crate, both local conditions and codes received off the wire.
Retry
What a caller does after a request fails, as Error::retry answers it.
SessionError
A code sent when terminating the session.
StreamError
A code sent when resetting a stream, or refusing to receive one.

Constants§

ALPNS
ALPN strings for supported versions, most-preferred first. ALPNS[0] is the newest moq-lite ALPN that both sides converge on.
IMPLEMENTATION
This crate’s product token, moq-net-rs/<version>, which SETUP advertises by default.

Traits§

Consume
Derive a read view from a handle.
IntoBytes
Converts borrowed or owned byte buffers into Bytes.

Type Aliases§

Result
A Result with this crate’s Error.