diff --git a/bench/README.md b/bench/README.md index 8f8f738e2ab..a1e4e0dc385 100644 --- a/bench/README.md +++ b/bench/README.md @@ -224,10 +224,9 @@ come from Linux interfaces. The default worker count is the number of logical CPUs the run may use. `moq-uring`'s `worker_turns` target also measures QUIC request/reply on one -worker, sweeping 1 or 16 connections against 64 B or 256 KiB replies with -1 or 4 task passes. It prints rounds and counter deltas for turns, passes, -enters and received datagrams, so counts per round can be compared alongside -timing. The existing nightly `rust-shared` group runs these cases. +worker, sweeping 1 or 16 connections against 64 B or 256 KiB replies. It +prints rounds and counter deltas for turns, passes, enters and received +datagrams, so counts per round can be compared alongside timing. The existing nightly `rust-shared` group runs these cases. ```bash nix develop --command cargo bench --locked -p moq-uring --bench worker_turns -- worker_request_reply diff --git a/dart/moq/lib/src/aliases.dart b/dart/moq/lib/src/aliases.dart index e456a825512..bdd2dfb6c2e 100644 --- a/dart/moq/lib/src/aliases.dart +++ b/dart/moq/lib/src/aliases.dart @@ -93,7 +93,7 @@ typedef BroadcastRequests = MoqBroadcastRequests; /// The write side of a raw track: append groups of frames. typedef TrackProducer = MoqTrackProducer; -/// A subscriber-requested track not yet accepted: accept it for a [TrackProducer] or abort it. +/// A subscriber-requested track not yet accepted: accept it for a [TrackProducer] or reject it. typedef TrackRequest = MoqTrackRequest; /// A stream of the fills readers of one track need: runs of groups the cache lacks. diff --git a/dart/moq_ffi/lib/src/moq.dart b/dart/moq_ffi/lib/src/moq.dart index 572cdaf62f8..cfe6302841e 100644 --- a/dart/moq_ffi/lib/src/moq.dart +++ b/dart/moq_ffi/lib/src/moq.dart @@ -11068,10 +11068,10 @@ class FfiConverterMoqDatagramProducer { } abstract class MoqDatagramRequestInterface { - void abort({required int errorCode}); MoqDatagramProducer accept({required MoqDatagramInfo? info}); String name(); int? priority(); + void reject({required int errorCode}); } final _MoqDatagramRequestFinalizer = Finalizer>((ptr) { @@ -11111,16 +11111,6 @@ class MoqDatagramRequest implements MoqDatagramRequestInterface { } } - void abort({required int errorCode}) { - return rustCall((status) { - uniffi_moq_ffi_fn_method_moqdatagramrequest_abort( - uniffiClonePointer(), - FfiConverterUInt16.lower(errorCode), - status, - ); - }, moqExceptionErrorHandler); - } - MoqDatagramProducer accept({required MoqDatagramInfo? info}) { return rustCallWithLifter( (status) => uniffi_moq_ffi_fn_method_moqdatagramrequest_accept( @@ -11154,6 +11144,16 @@ class MoqDatagramRequest implements MoqDatagramRequestInterface { moqExceptionErrorHandler, ); } + + void reject({required int errorCode}) { + return rustCall((status) { + uniffi_moq_ffi_fn_method_moqdatagramrequest_reject( + uniffiClonePointer(), + FfiConverterUInt16.lower(errorCode), + status, + ); + }, moqExceptionErrorHandler); + } } class FfiConverterMoqDatagramRequest { @@ -14022,13 +14022,12 @@ class FfiConverterMoqBroadcastRequests { abstract class MoqFillInterface { MoqGroupProducer accept({required int sequence}); void dropGroups({required int start, required int end}); - int endGroup(); + int end(); bool isWanted({required int sequence}); Duration? maxDelay(); MoqOrder order(); int priority(); - int startFrame(); - int startGroup(); + MoqPosition start(); UniffiRustFuture unwanted({required int sequence}); int? wanted(); UniffiRustFuture wantedChanged(); @@ -14092,12 +14091,10 @@ class MoqFill implements MoqFillInterface { }, null); } - int endGroup() { + int end() { return rustCallWithLifter( - (status) => uniffi_moq_ffi_fn_method_moqfill_end_group( - uniffiClonePointer(), - status, - ), + (status) => + uniffi_moq_ffi_fn_method_moqfill_end(uniffiClonePointer(), status), FfiConverterUInt64.lift, null, ); @@ -14146,24 +14143,11 @@ class MoqFill implements MoqFillInterface { ); } - int startFrame() { - return rustCallWithLifter( - (status) => uniffi_moq_ffi_fn_method_moqfill_start_frame( - uniffiClonePointer(), - status, - ), - FfiConverterUInt64.lift, - null, - ); - } - - int startGroup() { + MoqPosition start() { return rustCallWithLifter( - (status) => uniffi_moq_ffi_fn_method_moqfill_start_group( - uniffiClonePointer(), - status, - ), - FfiConverterUInt64.lift, + (status) => + uniffi_moq_ffi_fn_method_moqfill_start(uniffiClonePointer(), status), + FfiConverterMoqPosition.lift, null, ); } @@ -14568,10 +14552,10 @@ class FfiConverterMoqTrackProducer { } abstract class MoqTrackRequestInterface { - void abort({required int errorCode}); MoqTrackProducer accept({required MoqTrackInfo? info}); MoqTrackDemand demand(); String name(); + void reject({required int errorCode}); MoqTrackRequests requests(); } @@ -14612,16 +14596,6 @@ class MoqTrackRequest implements MoqTrackRequestInterface { } } - void abort({required int errorCode}) { - return rustCall((status) { - uniffi_moq_ffi_fn_method_moqtrackrequest_abort( - uniffiClonePointer(), - FfiConverterUInt16.lower(errorCode), - status, - ); - }, moqExceptionErrorHandler); - } - MoqTrackProducer accept({required MoqTrackInfo? info}) { return rustCallWithLifter( (status) => uniffi_moq_ffi_fn_method_moqtrackrequest_accept( @@ -14656,6 +14630,16 @@ class MoqTrackRequest implements MoqTrackRequestInterface { ); } + void reject({required int errorCode}) { + return rustCall((status) { + uniffi_moq_ffi_fn_method_moqtrackrequest_reject( + uniffiClonePointer(), + FfiConverterUInt16.lower(errorCode), + status, + ); + }, moqExceptionErrorHandler); + } + MoqTrackRequests requests() { return rustCallWithLifter( (status) => uniffi_moq_ffi_fn_method_moqtrackrequest_requests( @@ -20420,15 +20404,6 @@ external void uniffi_moq_ffi_fn_free_moqdatagramrequest( Pointer uniffiStatus, ); -@Native, Uint16, Pointer)>( - assetId: _uniffiAssetId, -) -external void uniffi_moq_ffi_fn_method_moqdatagramrequest_abort( - Pointer ptr, - int error_code, - Pointer uniffiStatus, -); - @Native< Pointer Function(Pointer, RustBuffer, Pointer) >(assetId: _uniffiAssetId) @@ -20454,6 +20429,15 @@ external RustBuffer uniffi_moq_ffi_fn_method_moqdatagramrequest_priority( Pointer uniffiStatus, ); +@Native, Uint16, Pointer)>( + assetId: _uniffiAssetId, +) +external void uniffi_moq_ffi_fn_method_moqdatagramrequest_reject( + Pointer ptr, + int error_code, + Pointer uniffiStatus, +); + @Native Function(Pointer, Pointer)>( assetId: _uniffiAssetId, ) @@ -21723,7 +21707,7 @@ external void uniffi_moq_ffi_fn_method_moqfill_drop_groups( @Native, Pointer)>( assetId: _uniffiAssetId, ) -external int uniffi_moq_ffi_fn_method_moqfill_end_group( +external int uniffi_moq_ffi_fn_method_moqfill_end( Pointer ptr, Pointer uniffiStatus, ); @@ -21761,18 +21745,10 @@ external int uniffi_moq_ffi_fn_method_moqfill_priority( Pointer uniffiStatus, ); -@Native, Pointer)>( - assetId: _uniffiAssetId, -) -external int uniffi_moq_ffi_fn_method_moqfill_start_frame( - Pointer ptr, - Pointer uniffiStatus, -); - -@Native, Pointer)>( +@Native, Pointer)>( assetId: _uniffiAssetId, ) -external int uniffi_moq_ffi_fn_method_moqfill_start_group( +external RustBuffer uniffi_moq_ffi_fn_method_moqfill_start( Pointer ptr, Pointer uniffiStatus, ); @@ -21987,15 +21963,6 @@ external void uniffi_moq_ffi_fn_free_moqtrackrequest( Pointer uniffiStatus, ); -@Native, Uint16, Pointer)>( - assetId: _uniffiAssetId, -) -external void uniffi_moq_ffi_fn_method_moqtrackrequest_abort( - Pointer ptr, - int error_code, - Pointer uniffiStatus, -); - @Native< Pointer Function(Pointer, RustBuffer, Pointer) >(assetId: _uniffiAssetId) @@ -22021,6 +21988,15 @@ external RustBuffer uniffi_moq_ffi_fn_method_moqtrackrequest_name( Pointer uniffiStatus, ); +@Native, Uint16, Pointer)>( + assetId: _uniffiAssetId, +) +external void uniffi_moq_ffi_fn_method_moqtrackrequest_reject( + Pointer ptr, + int error_code, + Pointer uniffiStatus, +); + @Native Function(Pointer, Pointer)>( assetId: _uniffiAssetId, ) @@ -23430,9 +23406,6 @@ external int uniffi_moq_ffi_checksum_method_moqdatagramproducer_priority(); external int uniffi_moq_ffi_checksum_method_moqdatagramproducer_priority_changed(); -@Native(assetId: _uniffiAssetId) -external int uniffi_moq_ffi_checksum_method_moqdatagramrequest_abort(); - @Native(assetId: _uniffiAssetId) external int uniffi_moq_ffi_checksum_method_moqdatagramrequest_accept(); @@ -23442,6 +23415,9 @@ external int uniffi_moq_ffi_checksum_method_moqdatagramrequest_name(); @Native(assetId: _uniffiAssetId) external int uniffi_moq_ffi_checksum_method_moqdatagramrequest_priority(); +@Native(assetId: _uniffiAssetId) +external int uniffi_moq_ffi_checksum_method_moqdatagramrequest_reject(); + @Native(assetId: _uniffiAssetId) external int uniffi_moq_ffi_checksum_method_moqdatagramrequests_cancel(); @@ -23696,7 +23672,7 @@ external int uniffi_moq_ffi_checksum_method_moqfill_accept(); external int uniffi_moq_ffi_checksum_method_moqfill_drop_groups(); @Native(assetId: _uniffiAssetId) -external int uniffi_moq_ffi_checksum_method_moqfill_end_group(); +external int uniffi_moq_ffi_checksum_method_moqfill_end(); @Native(assetId: _uniffiAssetId) external int uniffi_moq_ffi_checksum_method_moqfill_is_wanted(); @@ -23711,10 +23687,7 @@ external int uniffi_moq_ffi_checksum_method_moqfill_order(); external int uniffi_moq_ffi_checksum_method_moqfill_priority(); @Native(assetId: _uniffiAssetId) -external int uniffi_moq_ffi_checksum_method_moqfill_start_frame(); - -@Native(assetId: _uniffiAssetId) -external int uniffi_moq_ffi_checksum_method_moqfill_start_group(); +external int uniffi_moq_ffi_checksum_method_moqfill_start(); @Native(assetId: _uniffiAssetId) external int uniffi_moq_ffi_checksum_method_moqfill_unwanted(); @@ -23776,9 +23749,6 @@ external int uniffi_moq_ffi_checksum_method_moqtrackproducer_skip_groups(); @Native(assetId: _uniffiAssetId) external int uniffi_moq_ffi_checksum_method_moqtrackproducer_write_frame(); -@Native(assetId: _uniffiAssetId) -external int uniffi_moq_ffi_checksum_method_moqtrackrequest_abort(); - @Native(assetId: _uniffiAssetId) external int uniffi_moq_ffi_checksum_method_moqtrackrequest_accept(); @@ -23788,6 +23758,9 @@ external int uniffi_moq_ffi_checksum_method_moqtrackrequest_demand(); @Native(assetId: _uniffiAssetId) external int uniffi_moq_ffi_checksum_method_moqtrackrequest_name(); +@Native(assetId: _uniffiAssetId) +external int uniffi_moq_ffi_checksum_method_moqtrackrequest_reject(); + @Native(assetId: _uniffiAssetId) external int uniffi_moq_ffi_checksum_method_moqtrackrequest_requests(); @@ -24307,9 +24280,6 @@ void _checkApiChecksums() { 17584) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } - if (uniffi_moq_ffi_checksum_method_moqdatagramrequest_abort() != 45486) { - throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); - } if (uniffi_moq_ffi_checksum_method_moqdatagramrequest_accept() != 58466) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } @@ -24319,6 +24289,9 @@ void _checkApiChecksums() { if (uniffi_moq_ffi_checksum_method_moqdatagramrequest_priority() != 9109) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } + if (uniffi_moq_ffi_checksum_method_moqdatagramrequest_reject() != 22744) { + throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); + } if (uniffi_moq_ffi_checksum_method_moqdatagramrequests_cancel() != 15138) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } @@ -24576,16 +24549,16 @@ void _checkApiChecksums() { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } if (uniffi_moq_ffi_checksum_method_moqbroadcastrequests_requested_track() != - 49236) { + 42416) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } - if (uniffi_moq_ffi_checksum_method_moqfill_accept() != 63990) { + if (uniffi_moq_ffi_checksum_method_moqfill_accept() != 42158) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } if (uniffi_moq_ffi_checksum_method_moqfill_drop_groups() != 7289) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } - if (uniffi_moq_ffi_checksum_method_moqfill_end_group() != 42504) { + if (uniffi_moq_ffi_checksum_method_moqfill_end() != 60940) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } if (uniffi_moq_ffi_checksum_method_moqfill_is_wanted() != 47156) { @@ -24600,10 +24573,7 @@ void _checkApiChecksums() { if (uniffi_moq_ffi_checksum_method_moqfill_priority() != 23036) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } - if (uniffi_moq_ffi_checksum_method_moqfill_start_frame() != 29713) { - throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); - } - if (uniffi_moq_ffi_checksum_method_moqfill_start_group() != 8622) { + if (uniffi_moq_ffi_checksum_method_moqfill_start() != 28457) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } if (uniffi_moq_ffi_checksum_method_moqfill_unwanted() != 13637) { @@ -24666,9 +24636,6 @@ void _checkApiChecksums() { if (uniffi_moq_ffi_checksum_method_moqtrackproducer_write_frame() != 46891) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } - if (uniffi_moq_ffi_checksum_method_moqtrackrequest_abort() != 62713) { - throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); - } if (uniffi_moq_ffi_checksum_method_moqtrackrequest_accept() != 26498) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } @@ -24678,6 +24645,9 @@ void _checkApiChecksums() { if (uniffi_moq_ffi_checksum_method_moqtrackrequest_name() != 56715) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } + if (uniffi_moq_ffi_checksum_method_moqtrackrequest_reject() != 30225) { + throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); + } if (uniffi_moq_ffi_checksum_method_moqtrackrequest_requests() != 23724) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } diff --git a/demo/carla/web/src/truck.test.ts b/demo/carla/web/src/truck.test.ts index 84070b8dfce..3648f41c17d 100644 --- a/demo/carla/web/src/truck.test.ts +++ b/demo/carla/web/src/truck.test.ts @@ -432,7 +432,7 @@ describe("Truck sessions", () => { driving, focused, }); - const endpoint = new Moq.socket.Endpoint(origin, SOCKET, "truck1"); + const endpoint = new Moq.Socket.Endpoint(origin, SOCKET, "truck1"); const close = () => { truck.close(); endpoint.close(); @@ -442,7 +442,7 @@ describe("Truck sessions", () => { return { truck, driving, endpoint, close }; } - async function read(stream: Moq.socket.TrackStream, reader = stream.consumer.ordered()) { + async function read(stream: Moq.Socket.TrackStream, reader = stream.consumer.ordered()) { const frame = await reader.readFrame(); return frame && JSON.parse(decoder.decode(frame.payload)); } @@ -563,7 +563,7 @@ describe("Truck safety sessions", () => { driving, focused, }); - const endpoint = new Moq.socket.Endpoint(origin, SOCKET, "truck1"); + const endpoint = new Moq.Socket.Endpoint(origin, SOCKET, "truck1"); try { const incoming = endpoint.acceptTrack(); truck.emergencyStop(); diff --git a/demo/carla/web/src/truck.ts b/demo/carla/web/src/truck.ts index 0a7ebefcae3..f88f9c26ad4 100644 --- a/demo/carla/web/src/truck.ts +++ b/demo/carla/web/src/truck.ts @@ -42,7 +42,7 @@ interface Command { } /** A driver session: pending until the truck accepts, `null` once the dial or the session failed. */ -type Session = Moq.Signals.Signal; +type Session = Moq.Signals.Signal; const CONTROL_HEARTBEAT = Milli(250); const MAX_PENDING_ACKS = 64; @@ -50,7 +50,7 @@ const encoder = new TextEncoder(); const decoder = new TextDecoder(); /** Send one command as its own message, so a late one never holds up the next. */ -function write(stream: Moq.socket.TrackStream, command: Command) { +function write(stream: Moq.Socket.TrackStream, command: Command) { // A session that already ended has nothing to send to; its reply reader reports the end. if (stream.producer.closed.peek() !== undefined) return; const message = { ...command.cmd, sequence: command.sequence, timestamps: command.timestamps }; @@ -473,8 +473,8 @@ export class Truck { /** Dial the truck from a new endpoint `id`, both closed with `effect`. */ #dial(effect: Moq.Signals.Effect, id: string): Session { - const session: Session = new Moq.Signals.Signal(undefined); - const endpoint = new Moq.socket.Endpoint(this.#origin, SOCKET, id); + const session: Session = new Moq.Signals.Signal(undefined); + const endpoint = new Moq.Socket.Endpoint(this.#origin, SOCKET, id); effect.cleanup(() => { // Finishing first ends the session cleanly rather than resetting it. session.peek()?.producer.finish(); @@ -493,7 +493,7 @@ export class Truck { } /** Read the truck's acknowledgements, returning true once the session ends unless `effect` closed it. */ - async #readReplies(effect: Moq.Signals.Effect, stream: Moq.socket.TrackStream): Promise { + async #readReplies(effect: Moq.Signals.Effect, stream: Moq.Socket.TrackStream): Promise { // An acknowledgement a heartbeat late is superseded by the next one. stream.consumer.update({ order: "ascending", delay: { max: CONTROL_HEARTBEAT } }); const replies = stream.consumer.ordered(); diff --git a/doc/bin/relay/auth.md b/doc/bin/relay/auth.md index f49142e01ef..75836017340 100644 --- a/doc/bin/relay/auth.md +++ b/doc/bin/relay/auth.md @@ -287,24 +287,6 @@ the same way, as a token with an empty root. Libraries: [`moq-auth`](/lib/rs/moq-auth) (Rust) and [`@moq/auth`](/lib/js/auth) (TypeScript) sign and verify the same tokens. -### Peer grants - -A peer grant is the relay's short-lived statement of one session's publish and -subscribe patterns, bound to a public key the client holds (`cnf`). Another -client verifies it with the relay's public keys and serves the presenter only -those paths on a direct connection. It is not a token: presented as `?jwt=` -it is refused, and a token is never a peer grant. - -Peer grants need an asymmetric key, ES256 or EdDSA. A relay configured with -only HMAC keys (HS256 and friends) issues none, since giving a client an HMAC -key would let it forge grants, and its sessions serve nothing on a direct -connection. The set of public keys that verify peer grants is not a secret: it -holds only ES256 and EdDSA public keys, never HMAC material. - -[`moq-auth`](/lib/rs/moq-auth) signs and verifies peer grants -(`KeySet::sign_peer`, `KeySet::verify_peer`, `KeySet::to_peer_set`). The relay -does not issue them on the AUTH stream yet. - ## Anonymous access ```toml diff --git a/doc/bin/relay/config.md b/doc/bin/relay/config.md index 25afa723fda..965cba60175 100644 --- a/doc/bin/relay/config.md +++ b/doc/bin/relay/config.md @@ -19,7 +19,6 @@ Precedence is CLI, then env, then file, then defaults. bind = "[::]:443" # QUIC (UDP), as --listen. Omit for a stream-only relay. version = ["moq-lite-05"] # Restrict accepted versions. Omit for all but moq-lite-07-wip. timeout = "10s" # Handshake deadline. "0" waits forever. -implementation = "edge/1" # SETUP name for the relay. Omit for moq-relay/ moq-net-rs/, "" to send none. [listen.reset] secret = "/etc/moq/reset.key" # File holding 64 hex chars. Omit for a random key. @@ -58,10 +57,11 @@ admission, so a peer that connects and never speaks, or whose admission never gets an answer, is closed instead of held open by keep-alives. An HTTP read's admission is bounded by the same value from the request's arrival. -`implementation` names the relay in its SETUP, on accepted sessions and cluster dials -alike. A client that advertises its own name (moq-lite-05+ or any moq-transport draft) -reaches the [auth server](/bin/relay/auth#the-contract) with it as `implementation`, -which is where to count client versions; older clients arrive without one. +The relay names itself `moq-relay/ moq-net-rs/` in its SETUP, on accepted +sessions and cluster dials alike. A client that advertises its own name (moq-lite-05+ +or any moq-transport draft) reaches the [auth server](/bin/relay/auth#the-contract) +with it as `implementation`, which is where to count client versions; older clients +arrive without one. `bind` is the relay's one UDP port. Beside QUIC it carries WebRTC media for an [embedder](/bin/relay/#embed) that takes it; otherwise that traffic is dropped, diff --git a/doc/lib/go/index.md b/doc/lib/go/index.md index 563210798c9..daaaeb292e1 100644 --- a/doc/lib/go/index.md +++ b/doc/lib/go/index.md @@ -133,9 +133,9 @@ shows which module holds each. - **Decoded frames hold decoder buffers.** `Close` each `VideoDecodedFrame` promptly, or the decoder stalls. `Resize` is best effort, so read each frame's `Width()` and `Height()`. - **Closing.** `client.Close()` drains for up to one second and returns an error on failure. `session.Shutdown()` requests that drain immediately; `session.Closed(ctx)` waits for its result, and `session.Cancel(code)` closes at once. Finish or abort live tracks first. moq-lite-05 and newer wait for the peer to receive finished tails; older Lite versions and IETF wait only for transport ACKs. - **Ending cleanly.** When the input ends, finish every track, then call `Finish()` on its `media.CatalogProducer` to finish the catalog, which ends the content. The broadcast stays announced and keeps serving the finished tracks, so a subscriber still catching up can drain them, until `broadcast.Close()` takes it offline. How long to keep it up is yours to decide. A track still open at the close, or released without a finish, ends with `Unroutable` (`Internal` over moq-transport, which has no such code). Readers follow a publisher's runs by announcement, not by that error: see [identity and recovery](/concept/identity). -- **Stats.** `Session().Stats()` reports `RTT`, `EstimatedSendRateBps`, and the byte and packet counters (`BytesSent`, `BytesReceived`, `BytesLost`, `PacketsSent`, `PacketsReceived`, `PacketsLost`), and `DatagramsUnrouted`, the datagrams received for a subscription with no datagram track, reported only on moq-lite 05 and later, which carry datagrams. A field is nil when the transport does not report it, which is not the same as zero. `Version` names the negotiated protocol. `Dial` returns a connecting session; read `Session().State()` and wait with `StateChanged(ctx, state.Revision)` to observe first-dial failures and reconnects. Hold the owned `BandwidthConsumer` returned by `Session().RecvBandwidth()` to receive bandwidth estimates. `Rate()` reads the current estimate in bits per second and `RateChanged(ctx)` waits for a new one. Calling `Close()` on the last consumer stops PROBE; the session and stats snapshots do not request it. Client consumers survive reconnects and report no estimate while disconnected. +- **Stats.** `Session().Stats()` reports `RTT`, `EstimatedSendRateBps`, and the byte and packet counters (`BytesSent`, `BytesReceived`, `BytesLost`, `PacketsSent`, `PacketsReceived`, `PacketsLost`), and `DatagramsUnrouted`, the datagrams received for a subscription with no datagram track, reported only on moq-lite 05 and later, which carry datagrams. A field is nil when the transport does not report it, which is not the same as zero. `Version` names the negotiated protocol. `Dial` returns a connecting session; read `Session().State()` and wait with `StateChanged(ctx, state.Revision)` to observe first-dial failures and reconnects. Hold the owned `BandwidthConsumer` returned by `Session().RecvBandwidth()` to receive bandwidth estimates. `Rate()` reads the current estimate in bits per second and `RateChanged(ctx)` waits for a new one. Calling `Cancel()` on the last consumer stops PROBE; the session and stats snapshots do not request it. Client consumers survive reconnects and report no estimate while disconnected. - **Viewers.** `Session().Viewers()` divides the connection's receive estimate among the broadcasts you watch, max-min fair by each one's ceiling. `Watch(maxBps)` returns a `Viewer` claiming up to what the largest rendition the player would switch to needs; `Rate()` reads its slice in bits per second, nil while there is no estimate, so hold the current selection. `Update(maxBps)` changes the ceiling, such as when the catalog enables or disables a rendition. Holding a viewer requests PROBE, and the session sends the viewers' summed ceilings as the PROBE target; `Close()` it once the broadcast stops being watched, from the goroutine that last used it. `SetTarget(&bps)` sends another target, zero sends none, and nil follows the viewers again. `Watch`, `Update`, and `SetTarget` return `moq.ErrOutOfRange` when the ceilings or the target would pass 2^53 - 1 bps, the most a PROBE target carries. Client viewers survive reconnects and read nil while disconnected. -- **Authorization.** On `moq-lite-07-wip` (opt in with `ClientConfig.Versions`) and on moq-transport draft-18+ with the [MoQ Auth extension](/draft/moq-auth), `Session().Grants()` returns a `GrantConsumer`: `Grant()` is the `Grant` the relay sent back, nil until it answers and while a client session reconnects, and `Changed(ctx)` waits for the next one. `Close()` it when done. +- **Authorization.** On `moq-lite-07-wip` (opt in with `ClientConfig.Versions`) and on moq-transport draft-18+ with the [MoQ Auth extension](/draft/moq-auth), `Session().Grants()` returns a `GrantConsumer`: `Grant()` is the `Grant` the relay sent back, nil until it answers and while a client session reconnects, and `Changed(ctx)` waits for the next one. `Cancel()` it when done. - **Tokens.** Set `ClientConfig.Tokens` instead of putting the credential in the URL. The first is the connection credential, sent in SETUP and, while an offered version can't carry it there (lite before 07), as the URL's `?jwt=`; each other token widens the grant over AUTH. `Session().Tokens().Set(...)` replaces a client session's tokens before they expire: the next dial presents them, a live AUTH session at once, and after an authorization refusal the session redials only if they changed. `Answered(ctx)` returns nil once the relay accepts them, or its refusal with the relay's code. A `?jwt=` already in the URL stays the connection credential, which is how to reach a relay older than this release. - **Errors.** Read diagnostic payloads from `Reason` and structured protocol failures from `Details`. - **Disabled renditions.** Audio and video convenience decoders reject a catalog configuration with `enabled: false`. Select an enabled rendition from the latest catalog before opening a decoder. Among the video renditions you can decode, prefer the highest `Preference`; `0` is the default and `-1` marks a compatibility transcode. Audio and video subscribe setup returns `moq.ErrUnsupportedConfig` when the decoder refuses the codec, layout, or description; a player can then try its next ranked rendition. Decoder faults and later peer or decode failures do not report that setup refusal. diff --git a/doc/lib/js/net.md b/doc/lib/js/net.md index 8293a785e60..6bcd7032472 100644 --- a/doc/lib/js/net.md +++ b/doc/lib/js/net.md @@ -72,8 +72,9 @@ Readers that fall behind the shed front receive `Evicted`; tracks with a `refresh` ratio also request a fresh group. A join never starts at an arbitrary frame in the retained tail. -Frames have no size cap either. `group.createFrame({ size })` writes one in -pieces, and `group.nextFrame()` reads it as it arrives with `readChunk()`, so a +Frames have no size cap either. `group.createFrame({ size })` returns a +`Moq.Frame.Producer` that writes one in pieces, and `group.nextFrame()` returns a +`Moq.Frame.Consumer` that reads it as it arrives with `readChunk()`, so a file-sized frame passes through holding only the tail. The tail sheds a large frame's oldest 1 MiB segments while it is written, so a chunk reader more than the tail behind gets `Evicted`. `readFrame()` still works for any size, but it @@ -92,7 +93,7 @@ for (let chunk = await reader?.readChunk(); chunk; chunk = await reader?.readChu ## Sockets -Use `Moq.socket.Endpoint` for [point-to-point sessions](/concept/socket), with the same +Use `Moq.Socket.Endpoint` for [point-to-point sessions](/concept/socket), with the same origin publishing and consuming through the relay. An endpoint's first dial announces its hidden client broadcast and its first accept its hidden server broadcast, so grants decide which roles it may take; accepting verifies that the named peer actually dialed this session. @@ -107,7 +108,7 @@ const connection = await Moq.Connection.connect({ url, versions: ["moq-lite-07-wip"], publish: origin.consume(), consume: origin, }); -const endpoint = new Moq.socket.Endpoint(origin, Moq.Path.from("game"), "viewer"); +const endpoint = new Moq.Socket.Endpoint(origin, Moq.Path.from("game"), "viewer"); const session = await endpoint.dialTcp("server", { signal: abort.signal }); session.producer.writeFrame({ payload: new TextEncoder().encode("hello") }); session.producer.finish(); // FIN in this direction; the peer may still reply. diff --git a/doc/lib/py/index.md b/doc/lib/py/index.md index 64d40be8805..fb1b7356e18 100644 --- a/doc/lib/py/index.md +++ b/doc/lib/py/index.md @@ -26,7 +26,7 @@ Media timestamps are exact `moq.Timestamp(value, scale)` records, so a 90 kHz ti `Timestamp.from_timedelta` is exact; `.to_timedelta()` rounds down to a whole microsecond. A raw frame without a timestamp stays untimed, which only an untimed track accepts; JSON writes always take one. The library never substitutes zero or now. JSON consumers yield `moq.json.Timed(value, timestamp)`; a snapshot consumer yields each state in order, -and `await consumer.latest()` skips to the newest. A window consumer's value is a `WindowPush`, `WindowPop`, or `WindowSkip`. +and `await consumer.latest()` skips to the newest. A window consumer's value is a `WindowEventPush`, `WindowEventPop`, or `WindowEventSkip`. Raw tracks with a `None` timescale are untimed on both publish and receive. `TrackInfo(compress=True)` sends each group DEFLATE-compressed to opt-in `moq-lite-07-wip` peers; other peers, and every reader here, get the original frames. Set it on JSON tracks. The JSON producers' `update`, `append`, `push`, and `pop` require a `timestamp` on the broadcast clock, the epoch every track in the broadcast shares, so their track needs a timescale. diff --git a/doc/lib/rs/moq-mux.md b/doc/lib/rs/moq-mux.md index eef5d2f641d..84762aedeb5 100644 --- a/doc/lib/rs/moq-mux.md +++ b/doc/lib/rs/moq-mux.md @@ -17,7 +17,7 @@ can select the newest due picture without waiting and repeat it during a stall. | Format | Import | Export | Notes | | --- | --- | --- | --- | -| fMP4 / CMAF | yes | yes | Audio/video unwrapped to `legacy` by default, or passed through as `cmaf` with `with_passthrough`; `wvtt` subtitles import as timed WebVTT text and `stpp` TTML (IMSC timing profile) as `ttml` documents on the broadcast clock, preserving source language and label when present; other subtitle codecs are skipped. Import streams media samples as they arrive; each track's samples must be in forward byte order, and a `moov` or `moof` over 2 MiB, a text sample over 32 MiB, a `moof` of more than 131,072 samples, or a `moov` of more than 256 tracks is refused. | +| fMP4 / CMAF | yes | yes | Audio/video unwrapped to `legacy` by default, or published whole as `cmaf` with `with_cmaf`; `wvtt` subtitles import as timed WebVTT text and `stpp` TTML (IMSC timing profile) as `ttml` documents on the broadcast clock, preserving source language and label when present; other subtitle codecs are skipped. Import streams media samples as they arrive; each track's samples must be in forward byte order, and a `moov` or `moof` over 2 MiB, a text sample over 32 MiB, a `moof` of more than 131,072 samples, or a `moov` of more than 256 tracks is refused. | | MPEG-TS | yes | yes | H.264/H.265; AAC, MP2, AC-3, E-AC-3, Opus up to 7.1; SCTE-35 sections and every other undecoded PES stream (KLV, subtitles, DVB AC-3) as tracks; service tables round-trip. | | FLV / RTMP | yes | yes | Legacy H.264 + AAC + MP3, plus enhanced-RTMP HEVC, AV1, VP9, Opus, AC-3, E-AC-3, and multitrack. | | Matroska / WebM | yes | yes | Import streams each block into its frame in one pass, retaining no declared size. It refuses subtitle tracks, CMAF output, video `BlockGroup`s with fields after their `Block`, a `CodecPrivate` before its `CodecID`, and decoder configs carrying extra metadata (any FLAC metadata block after STREAMINFO, OpusHead extension fields, av1C OBUs beyond the sequence header and HDR metadata, AAC AudioSpecificConfig bytes past its last field or with a non-AAC object type's config): remux video as `SimpleBlock`s, extract subtitles separately, and remux FLAC so its `CodecPrivate` holds only STREAMINFO. Stripping tags is not enough, since seek tables and padding are refused too. ffmpeg's `-c:a copy` keeps only STREAMINFO, except for channel layouts FLAC does not define (such as 5.1 with back surrounds, ffmpeg's `-ac 6` default, or 4.0): there ffmpeg adds a Vorbis comment holding the channel mask, so those are refused. An hvcC keeps only its parameter sets, dropping SEI such as x265's encoder settings. | diff --git a/doc/lib/rs/moq-net.md b/doc/lib/rs/moq-net.md index ab340a85df7..ed1976326d3 100644 --- a/doc/lib/rs/moq-net.md +++ b/doc/lib/rs/moq-net.md @@ -31,7 +31,7 @@ What you use it for, beyond what the concept page already describes: - **You drive the session, or `moq-tokio` does.** `connect` and `accept` return a session plus a driver that never reads the clock itself. `moq_net::time::run` polls it on tokio. `moq-tokio` hides that. A custom transport implements `moq_net::transport::poll`, and `moq_net::transport::memory::pair` links two sessions in one process with no sockets, for tests or in-process links. `Server` bounds each accept with one deadline (10 s by default, `with_timeout`) across SETUP, your admission decision (`Handshake::expired`), and moq-lite 01/02's SETUP reply, and opens a receive window the transport held back once you admit (`with_receive_window`). - **Reconnect policy comes with it.** With the `async` feature, `moq_net::Reconnect` decides which of a peer's addresses to dial, how long each dial and handshake may take, when to back off and give up, and how to follow a GOAWAY, without doing I/O: you dial each `Attempt` it asks for and report back. `moq-tokio`'s `Connection` runs it, so a client on another runtime gets the same policy rather than a copy. - **A session bounds what its peer can hold.** Each request holds a stream, so the transport's stream credit bounds subscriptions and FETCHes. Once that credit runs out, a waiting open resets the lowest-priority group stream below it, which the subscriber sees dropped, so the credit it frees goes to the higher-priority group. `session::Limits` (via `Client::with_limits` and `Server::with_limits`) bounds the announcements per session. The default suits a relay mesh, so lower it for untrusted peers. Past it, the session closes with `TOO_MANY_REQUESTS`. Peer-declared lengths are capped before they are buffered. -- **Authorization is in band.** On moq-lite 07 (`moq-lite-07-wip`, opt-in) and on moq-transport draft-18+ with the [MoQ Auth extension](/draft/moq-auth), `session.auth()` presents tokens without reconnecting and watches the grant the peer sent back. `add(token)` resolves with the token and how the peer took it: `Answer::Granted` on its AUTH stream, or `Answer::Requests` on moq-transport without AUTH, where only a request that names it carries it. A handle passed to `Client::with_auth` outlives each session, so its tokens, and the routes naming them, carry across your own reconnects. A server verifies tokens itself by taking `handshake.requests()` before `ok()`. `authorize(&grant)` narrows or widens a live session on any version: what falls outside resets with `Unauthorized` and the session stays up. A client that publishes outside its grant closes the session, naming the path. +- **Authorization is in band.** On moq-lite 07 (`moq-lite-07-wip`, opt-in) and on moq-transport draft-18+ with the [MoQ Auth extension](/draft/moq-auth), `session.auth()` presents tokens without reconnecting and watches the grant the peer sent back. `add(token)` resolves with the token once the peer takes it: on its AUTH stream, or on moq-transport without AUTH, where only a request that names it carries it. A handle passed to `Client::with_auth` outlives each session, so its tokens, and the routes naming them, carry across your own reconnects. A server verifies tokens itself by taking `handshake.requests()` before `ok()`. `authorize(&grant)` narrows or widens a live session on any version: what falls outside resets with `Unauthorized` and the session stays up. A client that publishes outside its grant closes the session, naming the path. - **A graceful close waits.** `session.close().await` withdraws announcements and drains for up to one second. `moq-lite-05` and newer wait for the subscriber to receive finished tails; older Lite versions and IETF wait only for transport ACKs. `abort` ends immediately. ```bash diff --git a/go/wrapper/auth_test.go b/go/wrapper/auth_test.go index 08adc9f7933..0dbefc105c6 100644 --- a/go/wrapper/auth_test.go +++ b/go/wrapper/auth_test.go @@ -16,7 +16,7 @@ func awaitGrant(t *testing.T, ctx context.Context, session *moq.Session, want mo t.Helper() grants := session.Grants() - defer grants.Close() + defer grants.Cancel() got := grants.Grant() for got == nil || !reflect.DeepEqual(*got, want) { var err error @@ -97,7 +97,7 @@ func TestSessionAuthReadsTheGrant(t *testing.T) { t.Fatalf("answered = %v, want ErrUnsupported", err) } grants := client.Session().Grants() - defer grants.Close() + defer grants.Cancel() if got := grants.Grant(); got == nil || !reflect.DeepEqual(*got, everything) { t.Fatalf("grant after a refusal = %+v", got) } diff --git a/go/wrapper/datagram.go b/go/wrapper/datagram.go index 83e110773ea..8b00e5c8580 100644 --- a/go/wrapper/datagram.go +++ b/go/wrapper/datagram.go @@ -143,5 +143,5 @@ func (r *DatagramRequest) Accept(info *DatagramInfo) (*DatagramProducer, error) return &DatagramProducer{inner: inner}, nil } -// Abort rejects this request with an application error code. -func (r *DatagramRequest) Abort(code uint16) error { return r.inner.Abort(code) } +// Reject rejects this request with an application error code. +func (r *DatagramRequest) Reject(code uint16) error { return r.inner.Reject(code) } diff --git a/go/wrapper/json/json.go b/go/wrapper/json/json.go index 1af07d5f31f..e2715aaca21 100644 --- a/go/wrapper/json/json.go +++ b/go/wrapper/json/json.go @@ -45,7 +45,7 @@ func (o SnapshotOptions) deltas() bool { type WindowOptions struct { // CheckpointRecords bounds the records retained and restated when a group rolls, // for a window that is never popped: a reader that keeps up still sees every - // record, while a later joiner gets a WindowSkip for the older ones. Nil restates + // record, while a later joiner gets a WindowEventSkip for the older ones. Nil restates // the whole window; zero is refused. CheckpointRecords *uint64 } @@ -286,43 +286,43 @@ func (p *WindowProducer) Finish() error { return p.inner.Finish() } -// WindowEvent is one change to a window: a WindowPush, WindowPop, or WindowSkip. +// WindowEvent is one change to a window: a WindowEventPush, WindowEventPop, or WindowEventSkip. type WindowEvent interface { windowEvent() } -// WindowPush is a record joining the window at its absolute index. -type WindowPush struct { +// WindowEventPush is a record joining the window at its absolute index. +type WindowEventPush struct { // Index is the record's absolute index. Index uint64 // Value is the JSON document. Value stdjson.RawMessage } -// WindowPop is the records in [Start, End) leaving the window. -type WindowPop struct { +// WindowEventPop is the records in [Start, End) leaving the window. +type WindowEventPop struct { // Start is the first index popped. Start uint64 // End is one past the last index popped. End uint64 } -// WindowSkip is the records in [Start, End) that will never be delivered: they +// WindowEventSkip is the records in [Start, End) that will never be delivered: they // were pushed and dropped while this reader was behind, or fell outside the // producer's checkpoint when it joined. -type WindowSkip struct { +type WindowEventSkip struct { // Start is the first index skipped. Start uint64 // End is one past the last index skipped. End uint64 } -func (WindowPush) windowEvent() {} -func (WindowPop) windowEvent() {} -func (WindowSkip) windowEvent() {} +func (WindowEventPush) windowEvent() {} +func (WindowEventPop) windowEvent() {} +func (WindowEventSkip) windowEvent() {} -// TimedWindowEvent is a window change and the timestamp of the frame that carried it. -type TimedWindowEvent struct { +// WindowTimed is a window change and the timestamp of the frame that carried it. +type WindowTimed struct { // Event is the change to the window. Event WindowEvent // Timestamp is on the broadcast clock, or nil on an untimed track. A record @@ -331,18 +331,18 @@ type TimedWindowEvent struct { Timestamp *moq.Timestamp } -func timedWindowEvent(value *ffi.MoqJsonWindowTimed) (*TimedWindowEvent, error) { +func windowTimed(value *ffi.MoqJsonWindowTimed) (*WindowTimed, error) { if value == nil { return nil, nil } - out := &TimedWindowEvent{Timestamp: value.Timestamp} + out := &WindowTimed{Timestamp: value.Timestamp} switch event := value.Event.(type) { case ffi.MoqJsonWindowEventPush: - out.Event = WindowPush{Index: event.Index, Value: stdjson.RawMessage(event.Value)} + out.Event = WindowEventPush{Index: event.Index, Value: stdjson.RawMessage(event.Value)} case ffi.MoqJsonWindowEventPop: - out.Event = WindowPop{Start: event.Start, End: event.End} + out.Event = WindowEventPop{Start: event.Start, End: event.End} case ffi.MoqJsonWindowEventSkip: - out.Event = WindowSkip{Start: event.Start, End: event.End} + out.Event = WindowEventSkip{Start: event.Start, End: event.End} default: return nil, fmt.Errorf("unknown window event: %T", event) } @@ -366,16 +366,16 @@ func NewWindowConsumer(track *moq.TrackConsumer) (*WindowConsumer, error) { // Next returns the next change to the window, or nil when the track ends. Group // rolls never surface. -func (c *WindowConsumer) Next(ctx context.Context) (*TimedWindowEvent, error) { +func (c *WindowConsumer) Next(ctx context.Context) (*WindowTimed, error) { value, err := bridge.Call(ctx, c.inner.Cancel, c.inner.Next) if err != nil { return nil, err } - return timedWindowEvent(value) + return windowTimed(value) } // All ranges over every change to the window until the track ends. -func (c *WindowConsumer) All(ctx context.Context) iter.Seq2[*TimedWindowEvent, error] { +func (c *WindowConsumer) All(ctx context.Context) iter.Seq2[*WindowTimed, error] { return bridge.Seq(ctx, c.Next) } diff --git a/go/wrapper/json/json_test.go b/go/wrapper/json/json_test.go index d05c34f82ea..42c077f0d40 100644 --- a/go/wrapper/json/json_test.go +++ b/go/wrapper/json/json_test.go @@ -163,11 +163,11 @@ func TestWindow(t *testing.T) { t.Fatal(err) } - want := []moqjson.TimedWindowEvent{ - {Event: moqjson.WindowPush{Index: 0, Value: json.RawMessage(`{"n":0}`)}, Timestamp: ms(0)}, - {Event: moqjson.WindowPush{Index: 1, Value: json.RawMessage(`{"n":1}`)}, Timestamp: ms(1)}, - {Event: moqjson.WindowPush{Index: 2, Value: json.RawMessage(`{"n":2}`)}, Timestamp: ms(2)}, - {Event: moqjson.WindowPop{Start: 0, End: 2}, Timestamp: ms(3)}, + want := []moqjson.WindowTimed{ + {Event: moqjson.WindowEventPush{Index: 0, Value: json.RawMessage(`{"n":0}`)}, Timestamp: ms(0)}, + {Event: moqjson.WindowEventPush{Index: 1, Value: json.RawMessage(`{"n":1}`)}, Timestamp: ms(1)}, + {Event: moqjson.WindowEventPush{Index: 2, Value: json.RawMessage(`{"n":2}`)}, Timestamp: ms(2)}, + {Event: moqjson.WindowEventPop{Start: 0, End: 2}, Timestamp: ms(3)}, } for _, expected := range want { event, err := windowConsumer.Next(ctx) @@ -189,15 +189,15 @@ func TestWindow(t *testing.T) { if err := window.Finish(); err != nil { t.Fatal(err) } - var got []moqjson.TimedWindowEvent + var got []moqjson.WindowTimed for event, err := range windowConsumer.All(ctx) { if err != nil { t.Fatal(err) } got = append(got, *event) } - tail := []moqjson.TimedWindowEvent{ - {Event: moqjson.WindowPush{Index: 3, Value: json.RawMessage(`{"n":3}`)}, Timestamp: ms(4)}, + tail := []moqjson.WindowTimed{ + {Event: moqjson.WindowEventPush{Index: 3, Value: json.RawMessage(`{"n":3}`)}, Timestamp: ms(4)}, } if !reflect.DeepEqual(got, tail) { t.Fatalf("events after the cut = %+v, want %+v", got, tail) diff --git a/go/wrapper/moq_test.go b/go/wrapper/moq_test.go index aaee1411cfb..99063943ee5 100644 --- a/go/wrapper/moq_test.go +++ b/go/wrapper/moq_test.go @@ -738,8 +738,8 @@ func TestFetchGroupAndServeDynamicMiss(t *testing.T) { if err != nil { t.Fatal(err) } - if fill.StartGroup() != 7 || fill.EndGroup() != 8 || fill.Priority() != 11 { - t.Fatalf("unexpected fill: start=%d end=%d priority=%d", fill.StartGroup(), fill.EndGroup(), fill.Priority()) + if fill.Start() != (moq.Position{Group: 7, Frame: 0}) || fill.End() != 8 || fill.Priority() != 11 { + t.Fatalf("unexpected fill: start=%+v end=%d priority=%d", fill.Start(), fill.End(), fill.Priority()) } if wanted := fill.Wanted(); wanted == nil || *wanted != 7 { t.Fatalf("unexpected fill: wanted=%v", wanted) @@ -1429,7 +1429,7 @@ func TestSubscribeTrackCancelKeepsTheBroadcast(t *testing.T) { case <-waitCtx.Done(): t.Fatal("SubscribeTrack did not return after its context was cancelled") } - _ = pending.Abort(0) + _ = pending.Reject(0) resolved := make(chan error, 1) go func() { @@ -1568,7 +1568,7 @@ func TestCancelDoesNotLeakGoroutines(t *testing.T) { runtime.KeepAlive(pending) for _, request := range pending { - _ = request.Abort(0) + _ = request.Reject(0) } } diff --git a/go/wrapper/publish.go b/go/wrapper/publish.go index b506335d357..f01e4cee205 100644 --- a/go/wrapper/publish.go +++ b/go/wrapper/publish.go @@ -134,9 +134,9 @@ func (r *TrackRequest) Accept(info *TrackInfo) (*TrackProducer, error) { return &TrackProducer{inner: inner}, nil } -// Abort rejects the request with an application error code. -func (r *TrackRequest) Abort(errorCode uint16) error { - return r.inner.Abort(errorCode) +// Reject rejects the request with an application error code. +func (r *TrackRequest) Reject(errorCode uint16) error { + return r.inner.Reject(errorCode) } // TrackDemand watches the group a published track's subscribers want next. @@ -334,19 +334,14 @@ type Fill struct { inner *ffi.MoqFill } -// StartGroup is the first group to fill. -func (f *Fill) StartGroup() uint64 { - return f.inner.StartGroup() +// Start is the first group to fill, and the frame within it to start from. +func (f *Fill) Start() Position { + return f.inner.Start() } -// StartFrame is the frame within the first group to start from. -func (f *Fill) StartFrame() uint64 { - return f.inner.StartFrame() -} - -// EndGroup is the exclusive end group. -func (f *Fill) EndGroup() uint64 { - return f.inner.EndGroup() +// End is the exclusive end group. +func (f *Fill) End() uint64 { + return f.inner.End() } // Priority is the most urgent delivery priority among the readers waiting on it. @@ -381,7 +376,7 @@ func (f *Fill) WantedChanged(ctx context.Context) (*uint64, error) { } // Accept inserts group sequence and returns a producer for it. The first group -// starts at StartFrame, so its frames keep their indices. +// starts at Start's frame, so its frames keep their indices. func (f *Fill) Accept(sequence uint64) (*GroupProducer, error) { inner, err := f.inner.Accept(sequence) if err != nil { diff --git a/go/wrapper/session.go b/go/wrapper/session.go index a266e194b01..c705c3643ce 100644 --- a/go/wrapper/session.go +++ b/go/wrapper/session.go @@ -94,14 +94,14 @@ func (g *GrantConsumer) Grant() *Grant { // Changed blocks until the grant differs from the one this last returned, or // held when it was made, and returns it. It errors once a client's connection -// or a server's session ends, or after Close; a reconnect is not an end. +// or a server's session ends, or after Cancel; a reconnect is not an end. func (g *GrantConsumer) Changed(ctx context.Context) (*Grant, error) { grant, err := bridge.Call(ctx, nil, g.inner.Changed) return grantFromFFI(grant), err } -// Close ends every current and future Changed call. -func (g *GrantConsumer) Close() { g.inner.Cancel() } +// Cancel ends every current and future Changed call. +func (g *GrantConsumer) Cancel() { g.inner.Cancel() } // Tokens returns the credentials a native client session presents, to replace // while it runs, or nil for a server-accepted session, which presents none. @@ -307,7 +307,7 @@ func (s *Session) Cancel(code uint32) { s.inner.Cancel(code) } -// RecvBandwidth holds receive-estimate demand until its consumer is closed. +// RecvBandwidth holds receive-estimate demand until its consumer is cancelled. func (s *Session) RecvBandwidth() *BandwidthConsumer { inner := s.inner.RecvBandwidth() if inner == nil { @@ -327,5 +327,5 @@ func (b *BandwidthConsumer) RateChanged(ctx context.Context) (*uint64, error) { return bridge.Call(ctx, b.inner.Cancel, b.inner.RateChanged) } -// Close releases this consumer's demand, including a pending wait. -func (b *BandwidthConsumer) Close() { b.inner.Cancel() } +// Cancel releases this consumer's demand, including a pending wait. +func (b *BandwidthConsumer) Cancel() { b.inner.Cancel() } diff --git a/js/auth/src/path.ts b/js/auth/src/path.ts index 897c36c9261..4f725060450 100644 --- a/js/auth/src/path.ts +++ b/js/auth/src/path.ts @@ -15,7 +15,7 @@ * @module */ -/** The most UTF-8 bytes a path may hold, matching `@moq/net`'s `Path.MAX_BYTES`. */ +/** The most UTF-8 bytes a path may hold, matching `@moq/net`'s path limit. */ const MAX_BYTES = 4096; const utf8 = new TextEncoder(); diff --git a/js/hang/api/catalog.api.md b/js/hang/api/catalog.api.md index fe423896544..d0c8e83767a 100644 --- a/js/hang/api/catalog.api.md +++ b/js/hang/api/catalog.api.md @@ -26,86 +26,82 @@ import { ZodMiniType } from "zod/mini"; import { ZodMiniUnion } from "zod/mini"; import { infer } from "zod/mini"; -export namespace Audio { - export type Audio = infer; - export type AudioConfig = infer; - export const AudioConfigSchema: ZodMiniObject<{ - bitrate: ZodMiniOptional<$ZodBranded, "u53", "out">>; - broadcast: ZodMiniOptional>>; - codec: ZodMiniString; - container: ZodMiniDefault; - }, $strip>, ZodMiniObject<{ - init: ZodMiniBase64; - kind: ZodMiniLiteral<"cmaf">; - }, $strip>, ZodMiniObject<{ - kind: ZodMiniLiteral<"loc">; - }, $strip>], "kind">, ZodMiniPipe; - }, $loose>, ZodMiniTransform<{ +export const Audio: { + ranked: (renditions: Record) => [string, AudioConfig][]; +}; + +export type Audio = infer; + +export type AudioConfig = infer; + +export const AudioConfigSchema: ZodMiniObject<{ + bitrate: ZodMiniOptional<$ZodBranded, "u53", "out">>; + broadcast: ZodMiniOptional>>; + codec: ZodMiniString; + container: ZodMiniDefault; + }, $strip>, ZodMiniObject<{ + init: ZodMiniBase64; + kind: ZodMiniLiteral<"cmaf">; + }, $strip>, ZodMiniObject<{ + kind: ZodMiniLiteral<"loc">; + }, $strip>], "kind">, ZodMiniPipe; + }, $loose>, ZodMiniTransform<{ + kind: "unknown"; + raw: { + [x: string]: unknown; + kind: string; + }; + }, { + [x: string]: unknown; + kind: string; + }>>]>>; + delay: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; + description: ZodMiniOptional>; + enabled: ZodMiniOptional>; + instance: ZodMiniOptional>>; + jitter: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; + label: ZodMiniOptional>; + layout: ZodMiniOptional>; + numberOfChannels: $ZodBranded, "u53", "out">; + sampleRate: $ZodBranded, "u53", "out">; + track: ZodMiniOptional>; + warmup: ZodMiniOptional<$ZodBranded, "u53", "out">>; +}, $strip>; + +export const AudioSchema: ZodMiniObject<{ + renditions: ZodMiniPipe, Record>, ZodMiniTransform | undefined; + broadcast?: RelativeBroadcast | undefined; + codec: string; + container: { + kind: "legacy"; + } | { + init: string; + kind: "cmaf"; + } | { + kind: "loc"; + } | { kind: "unknown"; raw: { [x: string]: unknown; kind: string; }; - }, { - [x: string]: unknown; - kind: string; - }>>]>>; - delay: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; - description: ZodMiniOptional>; - enabled: ZodMiniOptional>; - instance: ZodMiniOptional>>; - jitter: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; - label: ZodMiniOptional>; - layout: ZodMiniOptional>; - numberOfChannels: $ZodBranded, "u53", "out">; - sampleRate: $ZodBranded, "u53", "out">; - track: ZodMiniOptional>; - warmup: ZodMiniOptional<$ZodBranded, "u53", "out">>; - }, $strip>; - export const AudioSchema: ZodMiniObject<{ - renditions: ZodMiniPipe, Record>, ZodMiniTransform | undefined; - broadcast?: RelativeBroadcast | undefined; - codec: string; - container: { - kind: "legacy"; - } | { - init: string; - kind: "cmaf"; - } | { - kind: "loc"; - } | { - kind: "unknown"; - raw: { - [x: string]: unknown; - kind: string; - }; - }; - delay?: number & $brand<"u53"> | undefined; - description?: string | undefined; - enabled?: boolean | undefined; - instance?: ReferenceInstance | undefined; - jitter?: number & $brand<"u53"> | undefined; - label?: string | undefined; - layout?: string | undefined; - numberOfChannels: number & $brand<"u53">; - sampleRate: number & $brand<"u53">; - track?: string | undefined; - warmup?: number & $brand<"u53"> | undefined; - }>, Record>>; - }, $strip>; - export function ranked(renditions: Record): [string, Audio.AudioConfig][]; -} - -export type Audio = Audio.Audio; - -export import AudioConfig = Audio.AudioConfig; - -export import AudioConfigSchema = Audio.AudioConfigSchema; - -export import AudioSchema = Audio.AudioSchema; + }; + delay?: number & $brand<"u53"> | undefined; + description?: string | undefined; + enabled?: boolean | undefined; + instance?: ReferenceInstance | undefined; + jitter?: number & $brand<"u53"> | undefined; + label?: string | undefined; + layout?: string | undefined; + numberOfChannels: number & $brand<"u53">; + sampleRate: number & $brand<"u53">; + track?: string | undefined; + warmup?: number & $brand<"u53"> | undefined; + }>, Record>>; +}, $strip>; export type Binary = infer; @@ -147,9 +143,41 @@ export type CmafContainer = Extract; -export import Color = Video.Color; +export type Color = infer; -export import ColorSchema = Video.ColorSchema; +export const ColorSchema: ZodMiniObject<{ + contentLight: ZodMiniOptional; + maxFall: ZodMiniNumber; + }, $strip>>; + mastering: ZodMiniOptional; + y: ZodMiniNumber; + }, $strip>; + green: ZodMiniObject<{ + x: ZodMiniNumber; + y: ZodMiniNumber; + }, $strip>; + maxLuminance: ZodMiniNumber; + minLuminance: ZodMiniNumber; + red: ZodMiniObject<{ + x: ZodMiniNumber; + y: ZodMiniNumber; + }, $strip>; + white: ZodMiniObject<{ + x: ZodMiniNumber; + y: ZodMiniNumber; + }, $strip>; + }, $strip>>; + matrix: ZodMiniOptional<$ZodBranded, "u8", "out">>; + primaries: ZodMiniOptional<$ZodBranded, "u8", "out">>; + range: ZodMiniOptional>; + transfer: ZodMiniOptional<$ZodBranded, "u8", "out">>; +}, $strip>; export type Container = infer; @@ -268,9 +296,25 @@ export const PRIORITY: { readonly video: 60; }; -export import Projection = Video.Projection; +export type Projection = infer; -export import ProjectionSchema = Video.ProjectionSchema; +export const ProjectionSchema: ZodMiniUnion; +}, $strip>, ZodMiniObject<{ + fov: ZodMiniOptional>; + kind: ZodMiniLiteral<"equirectangular">; +}, $strip>], "kind">, ZodMiniPipe; +}, $loose>, ZodMiniTransform<{ + kind: "unknown"; + raw: { + [x: string]: unknown; + kind: string; + }; +}, { + [x: string]: unknown; + kind: string; +}>>]>; export type ReferenceInstance = string & { readonly _brand: "instance"; @@ -586,9 +630,21 @@ export class UnsupportedVersion extends Error { export const VERSION: 4; -export namespace Video { - export type Color = infer; - export const ColorSchema: ZodMiniObject<{ +export const Video: { + ranked: (renditions: Record) => [string, VideoConfig][]; +}; + +export type Video = infer; + +export type VideoConfig = infer; + +export const VideoConfigSchema: ZodMiniObject<{ + bitrate: ZodMiniOptional<$ZodBranded, "u53", "out">>; + broadcast: ZodMiniOptional>>; + codec: ZodMiniString; + codedHeight: ZodMiniOptional<$ZodBranded, "u53", "out">>; + codedWidth: ZodMiniOptional<$ZodBranded, "u53", "out">>; + color: ZodMiniOptional; maxFall: ZodMiniNumber; @@ -620,9 +676,39 @@ export namespace Video { limited: "limited"; }>>; transfer: ZodMiniOptional<$ZodBranded, "u8", "out">>; - }, $strip>; - export type Projection = infer; - export const ProjectionSchema: ZodMiniUnion>; + container: ZodMiniDefault; + }, $strip>, ZodMiniObject<{ + init: ZodMiniBase64; + kind: ZodMiniLiteral<"cmaf">; + }, $strip>, ZodMiniObject<{ + kind: ZodMiniLiteral<"loc">; + }, $strip>], "kind">, ZodMiniPipe; + }, $loose>, ZodMiniTransform<{ + kind: "unknown"; + raw: { + [x: string]: unknown; + kind: string; + }; + }, { + [x: string]: unknown; + kind: string; + }>>]>>; + delay: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; + description: ZodMiniOptional>; + displayAspectHeight: ZodMiniOptional<$ZodBranded, "u53", "out">>; + displayAspectWidth: ZodMiniOptional<$ZodBranded, "u53", "out">>; + enabled: ZodMiniOptional>; + framerate: ZodMiniOptional>; + instance: ZodMiniOptional>>; + interlaced: ZodMiniOptional>; + jitter: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; + label: ZodMiniOptional>; + optimizeForLatency: ZodMiniOptional>; + preference: ZodMiniOptional; + projection: ZodMiniOptional; }, $strip>, ZodMiniObject<{ fov: ZodMiniOptional>; @@ -638,198 +724,100 @@ export namespace Video { }, { [x: string]: unknown; kind: string; - }>>]>; - export type Video = infer; - export type VideoConfig = infer; - export const VideoConfigSchema: ZodMiniObject<{ - bitrate: ZodMiniOptional<$ZodBranded, "u53", "out">>; - broadcast: ZodMiniOptional>>; - codec: ZodMiniString; - codedHeight: ZodMiniOptional<$ZodBranded, "u53", "out">>; - codedWidth: ZodMiniOptional<$ZodBranded, "u53", "out">>; - color: ZodMiniOptional; - maxFall: ZodMiniNumber; - }, $strip>>; - mastering: ZodMiniOptional; - y: ZodMiniNumber; - }, $strip>; - green: ZodMiniObject<{ - x: ZodMiniNumber; - y: ZodMiniNumber; - }, $strip>; - maxLuminance: ZodMiniNumber; - minLuminance: ZodMiniNumber; - red: ZodMiniObject<{ - x: ZodMiniNumber; - y: ZodMiniNumber; - }, $strip>; - white: ZodMiniObject<{ - x: ZodMiniNumber; - y: ZodMiniNumber; - }, $strip>; - }, $strip>>; - matrix: ZodMiniOptional<$ZodBranded, "u8", "out">>; - primaries: ZodMiniOptional<$ZodBranded, "u8", "out">>; - range: ZodMiniOptional>; - transfer: ZodMiniOptional<$ZodBranded, "u8", "out">>; - }, $strip>>; - container: ZodMiniDefault; - }, $strip>, ZodMiniObject<{ - init: ZodMiniBase64; - kind: ZodMiniLiteral<"cmaf">; - }, $strip>, ZodMiniObject<{ - kind: ZodMiniLiteral<"loc">; - }, $strip>], "kind">, ZodMiniPipe; - }, $loose>, ZodMiniTransform<{ + }>>]>>; + reorder: ZodMiniOptional<$ZodBranded, "u8", "out">>; + stereo: ZodMiniOptional>; + track: ZodMiniOptional>; + warmup: ZodMiniOptional<$ZodBranded, "u53", "out">>; +}, $strip>; + +export const VideoSchema: ZodMiniObject<{ + display: ZodMiniOptional, "u53", "out">; + width: $ZodBranded, "u53", "out">; + }, $strip>>; + flip: ZodMiniOptional>; + renditions: ZodMiniPipe, Record>, ZodMiniTransform | undefined; + broadcast?: RelativeBroadcast | undefined; + codec: string; + codedHeight?: number & $brand<"u53"> | undefined; + codedWidth?: number & $brand<"u53"> | undefined; + color?: { + contentLight?: { + maxCll: number; + maxFall: number; + } | undefined; + mastering?: { + blue: { + x: number; + y: number; + }; + green: { + x: number; + y: number; + }; + maxLuminance: number; + minLuminance: number; + red: { + x: number; + y: number; + }; + white: { + x: number; + y: number; + }; + } | undefined; + matrix?: number & $brand<"u8"> | undefined; + primaries?: number & $brand<"u8"> | undefined; + range?: "full" | "limited" | undefined; + transfer?: number & $brand<"u8"> | undefined; + } | undefined; + container: { + kind: "legacy"; + } | { + init: string; + kind: "cmaf"; + } | { + kind: "loc"; + } | { kind: "unknown"; raw: { [x: string]: unknown; kind: string; }; - }, { - [x: string]: unknown; - kind: string; - }>>]>>; - delay: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; - description: ZodMiniOptional>; - displayAspectHeight: ZodMiniOptional<$ZodBranded, "u53", "out">>; - displayAspectWidth: ZodMiniOptional<$ZodBranded, "u53", "out">>; - enabled: ZodMiniOptional>; - framerate: ZodMiniOptional>; - instance: ZodMiniOptional>>; - interlaced: ZodMiniOptional>; - jitter: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; - label: ZodMiniOptional>; - optimizeForLatency: ZodMiniOptional>; - preference: ZodMiniOptional; - projection: ZodMiniOptional; - }, $strip>, ZodMiniObject<{ - fov: ZodMiniOptional>; - kind: ZodMiniLiteral<"equirectangular">; - }, $strip>], "kind">, ZodMiniPipe; - }, $loose>, ZodMiniTransform<{ + }; + delay?: number & $brand<"u53"> | undefined; + description?: string | undefined; + displayAspectHeight?: number & $brand<"u53"> | undefined; + displayAspectWidth?: number & $brand<"u53"> | undefined; + enabled?: boolean | undefined; + framerate?: number | undefined; + instance?: ReferenceInstance | undefined; + interlaced?: boolean | undefined; + jitter?: number & $brand<"u53"> | undefined; + label?: string | undefined; + optimizeForLatency?: boolean | undefined; + preference?: number | undefined; + projection?: { + kind: "rectilinear"; + } | { + fov?: number | undefined; + kind: "equirectangular"; + } | { kind: "unknown"; raw: { [x: string]: unknown; kind: string; }; - }, { - [x: string]: unknown; - kind: string; - }>>]>>; - reorder: ZodMiniOptional<$ZodBranded, "u8", "out">>; - stereo: ZodMiniOptional>; - track: ZodMiniOptional>; - warmup: ZodMiniOptional<$ZodBranded, "u53", "out">>; - }, $strip>; - export const VideoSchema: ZodMiniObject<{ - display: ZodMiniOptional, "u53", "out">; - width: $ZodBranded, "u53", "out">; - }, $strip>>; - flip: ZodMiniOptional>; - renditions: ZodMiniPipe, Record>, ZodMiniTransform | undefined; - broadcast?: RelativeBroadcast | undefined; - codec: string; - codedHeight?: number & $brand<"u53"> | undefined; - codedWidth?: number & $brand<"u53"> | undefined; - color?: { - contentLight?: { - maxCll: number; - maxFall: number; - } | undefined; - mastering?: { - blue: { - x: number; - y: number; - }; - green: { - x: number; - y: number; - }; - maxLuminance: number; - minLuminance: number; - red: { - x: number; - y: number; - }; - white: { - x: number; - y: number; - }; - } | undefined; - matrix?: number & $brand<"u8"> | undefined; - primaries?: number & $brand<"u8"> | undefined; - range?: "full" | "limited" | undefined; - transfer?: number & $brand<"u8"> | undefined; - } | undefined; - container: { - kind: "legacy"; - } | { - init: string; - kind: "cmaf"; - } | { - kind: "loc"; - } | { - kind: "unknown"; - raw: { - [x: string]: unknown; - kind: string; - }; - }; - delay?: number & $brand<"u53"> | undefined; - description?: string | undefined; - displayAspectHeight?: number & $brand<"u53"> | undefined; - displayAspectWidth?: number & $brand<"u53"> | undefined; - enabled?: boolean | undefined; - framerate?: number | undefined; - instance?: ReferenceInstance | undefined; - interlaced?: boolean | undefined; - jitter?: number & $brand<"u53"> | undefined; - label?: string | undefined; - optimizeForLatency?: boolean | undefined; - preference?: number | undefined; - projection?: { - kind: "rectilinear"; - } | { - fov?: number | undefined; - kind: "equirectangular"; - } | { - kind: "unknown"; - raw: { - [x: string]: unknown; - kind: string; - }; - } | undefined; - reorder?: number & $brand<"u8"> | undefined; - stereo?: string | undefined; - track?: string | undefined; - warmup?: number & $brand<"u53"> | undefined; - }>, Record>>; - rotation: ZodMiniOptional>; - }, $strip>; - export function ranked(renditions: Record): [string, Video.VideoConfig][]; -} - -export type Video = Video.Video; - -export import VideoConfig = Video.VideoConfig; - -export import VideoConfigSchema = Video.VideoConfigSchema; - -export import VideoSchema = Video.VideoSchema; + } | undefined; + reorder?: number & $brand<"u8"> | undefined; + stereo?: string | undefined; + track?: string | undefined; + warmup?: number & $brand<"u53"> | undefined; + }>, Record>>; + rotation: ZodMiniOptional>; +}, $strip>; export function checkRenditions(root: Root): Root; diff --git a/js/hang/api/container.api.md b/js/hang/api/container.api.md index 8db9aa8394b..f2e81f7f2cb 100644 --- a/js/hang/api/container.api.md +++ b/js/hang/api/container.api.md @@ -1,13 +1,13 @@ # `@moq/hang/container` ```ts -import { Audio } from "@moq/hang/catalog"; +import { AudioConfig } from "@moq/hang/catalog"; import { Format as Format_1 } from "@moq/hang/container"; import { Micro } from "@moq/clock"; import { Milli } from "@moq/clock"; import { Net } from "@moq/hang"; import { Signals } from "@moq/hang"; -import { Video } from "@moq/hang/catalog"; +import { VideoConfig } from "@moq/hang/catalog"; export interface BufferedRange { end: Milli; @@ -27,13 +27,10 @@ export class Consumer { export interface ConsumerProps { delay?: Signals.GetterInit; format: Format; - maxBuffer?: number; predecessor?: Consumer; warmup?: Milli; } -export const DEFAULT_MAX_BUFFER: number; - export interface Format { decode(payload: Uint8Array, timestamp: Net.Time.Timestamp | undefined): Frame[]; end?(frame: Frame): Micro | undefined; @@ -51,7 +48,7 @@ export namespace Legacy { export { BufferedRange }; export { BufferedRanges }; export class Format implements Format_1 { - constructor(config: Audio.AudioConfig | Video.VideoConfig | "audio" | "video" | "data"); + constructor(config: AudioConfig | VideoConfig | "audio" | "video" | "data"); decode(frame: Uint8Array): Frame[]; end(frame: Frame): Micro | undefined; finishGroup(group: Net.Group.Producer, end?: Micro): void; diff --git a/js/hang/api/index.api.md b/js/hang/api/index.api.md index 6bb8472111f..c9bb0404ea3 100644 --- a/js/hang/api/index.api.md +++ b/js/hang/api/index.api.md @@ -154,13 +154,11 @@ export * as Net from "@moq/net"; export namespace Report { export const INTERVAL: 1000; - export const PRIORITY: -128; export class Writer { constructor(broadcast: Net.Broadcast.Producer, name: string, schema?: ZodMiniType); finish(): void; readonly track: Net.Track.Producer; update(value: T): void; - get wanted(): boolean; } } diff --git a/js/hang/src/catalog/index.ts b/js/hang/src/catalog/index.ts index 6ca97df89eb..d58df4bcf95 100644 --- a/js/hang/src/catalog/index.ts +++ b/js/hang/src/catalog/index.ts @@ -8,17 +8,18 @@ * @module */ +import type * as z from "zod/mini"; import * as audio from "./audio"; import * as video from "./video"; -/** Audio schemas and quality ordering. */ -export { audio as Audio }; +/** Audio quality ordering. */ +export const Audio = { ranked: audio.ranked }; /** The catalog audio section. */ -export type Audio = audio.Audio; -/** Video schemas and quality ordering. */ -export { video as Video }; +export type Audio = z.infer; +/** Video quality ordering. */ +export const Video = { ranked: video.ranked }; /** The catalog video section. */ -export type Video = video.Video; +export type Video = z.infer; export type { AudioConfig } from "./audio"; export { AudioConfigSchema, AudioSchema } from "./audio"; diff --git a/js/hang/src/container/consumer.ts b/js/hang/src/container/consumer.ts index ff97e6cb370..59eaffd3c56 100644 --- a/js/hang/src/container/consumer.ts +++ b/js/hang/src/container/consumer.ts @@ -28,14 +28,16 @@ export interface ConsumerProps { delay?: GetterInit; /** * Cap the bytes of decoded frames held ahead of the reader, counting the whole wire payload they - * share and each frame's and group's own overhead (default: {@link DEFAULT_MAX_BUFFER}). Mirrors - * Rust's `Consumer::set_max_buffer`. + * share and each frame's and group's own overhead (default: one group at the transport's size + * cap). Mirrors Rust's `Consumer::set_max_buffer`. * * Once it is full the consumer stops decoding ahead and stops pulling new groups past one successor * to the group being read, so the undecoded rest stays in the transport's cache, which bounds it. The group being read still decodes a wire * frame whenever it holds nothing, so reading never waits on its own lookahead. If it stalls while the * cap is full, the consumer moves on to the next group like a skip past a slow group, read or not: no * more of what it waits behind can be held. + * + * @internal */ maxBuffer?: number; /** @@ -87,8 +89,8 @@ export type Read = */ | { kind: "end"; group: bigint; end: Micro }; -/** The default {@link ConsumerProps.maxBuffer}: one group at the transport's size cap. Mirrors Rust's. */ -export const DEFAULT_MAX_BUFFER = 32 * 1024 * 1024; +// The default max buffer: one group at the transport's size cap. Mirrors Rust's. +const DEFAULT_MAX_BUFFER = 32 * 1024 * 1024; // Rough sizes of a buffered frame's and a group's own objects, charged to the max buffer alongside // payload bytes. JS cannot measure them; these only need to be the right order of magnitude. diff --git a/js/hang/src/container/index.ts b/js/hang/src/container/index.ts index aed42709dd3..7ae22670d60 100644 --- a/js/hang/src/container/index.ts +++ b/js/hang/src/container/index.ts @@ -7,7 +7,7 @@ /** Re-export of {@link https://www.npmjs.com/package/@moq/loc | @moq/loc}, the LOC container. */ export * as Loc from "@moq/loc"; -export { Consumer, type ConsumerProps, DEFAULT_MAX_BUFFER, type Read } from "./consumer"; +export { Consumer, type ConsumerProps, type Read } from "./consumer"; export type { Format } from "./format"; /** The legacy varint container. */ export * as Legacy from "./legacy"; diff --git a/js/hang/src/report.ts b/js/hang/src/report.ts index adb23f300b9..d44cd11b199 100644 --- a/js/hang/src/report.ts +++ b/js/hang/src/report.ts @@ -10,8 +10,8 @@ import type * as z from "zod/mini"; /** How often a reporter writes, in milliseconds: about once a second, as the hang draft asks. */ export const INTERVAL = 1000; -/** The report track's priority: below every media track, so a report never delays the media it describes. */ -export const PRIORITY = -128; +// Below every media track, so a report never delays the media it describes. +const PRIORITY = -128; // Deltas compress against the snapshot in their group, and a joiner asks for a fresh group once // they outweigh eight snapshots. Matches the Rust CLI's report tracks. @@ -35,17 +35,13 @@ export class Writer { this.#producer = new Json.Snapshot.Producer({ track: this.track, schema }); } - /** Whether anyone subscribes now, which is when {@link update} writes. Observe `track.demand().wanted` to follow it. */ - get wanted(): boolean { - return this.track.demand().wanted.peek() !== undefined; - } - /** - * Publish `value` when someone subscribes; an unchanged value writes nothing. A track that refuses - * a write would refuse every later one, so it is aborted with the cause, which its subscribers read. + * Publish `value` when someone subscribes (observe `track.demand().wanted` to follow that); an + * unchanged value writes nothing. A track that refuses a write would refuse every later one, so it + * is aborted with the cause, which its subscribers read. */ update(value: T): void { - if (this.#closed || !this.wanted) return; + if (this.#closed || this.track.demand().wanted.peek() === undefined) return; try { this.#producer.update({ value }); } catch (err) { diff --git a/js/moq-boy/api/index.api.md b/js/moq-boy/api/index.api.md index 2199aa3b957..922e6207ed0 100644 --- a/js/moq-boy/api/index.api.md +++ b/js/moq-boy/api/index.api.md @@ -22,10 +22,7 @@ export class Game { readonly expanded: Moq.Signals.Signal; readonly heldButtons: Set; readonly hovered: Moq.Signals.Signal; - readonly latency: Moq.Signals.Signal<{ - label: string; - ms: number; - }[] | undefined>; + readonly latency: Moq.Signals.Signal; sendButtons(): void; sendCommand(cmd: Record): void; readonly sessionId: string; @@ -50,7 +47,7 @@ export class Game { export interface GameConfig { connection: Moq.Connection; - endpoint: Moq.socket.Endpoint; + endpoint: Moq.Socket.Endpoint; expanded: Moq.Signals.Signal; gamePrefix: string; origin: Moq.Origin.Producer; @@ -82,12 +79,10 @@ export const GameStatusSchema: ZodMiniObject<{ export const KEY_MAP: Record; -export type LatencyEntry = infer; - -export const LatencyEntrySchema: ZodMiniObject<{ - label: ZodMiniString; - ms: ZodMiniNumber; -}, $strip>; +export interface LatencyEntry { + label: string; + ms: number; +} export * as Moq from "@moq/net"; diff --git a/js/moq-boy/src/element.tsx b/js/moq-boy/src/element.tsx index 5025aa48f1a..b0ae90dc1a2 100644 --- a/js/moq-boy/src/element.tsx +++ b/js/moq-boy/src/element.tsx @@ -168,7 +168,7 @@ export default class MoqBoy extends HTMLElement { const gamePrefix = effect.get(this.#gamePrefixOverride) ?? Moq.Path.from(base, "game"); // The endpoint every game under this prefix is dialed from. - const endpoint = new Moq.socket.Endpoint(this.origin, gamePrefix, viewerId()); + const endpoint = new Moq.Socket.Endpoint(this.origin, gamePrefix, viewerId()); effect.cleanup(() => endpoint.close()); // Games under the old prefix dial from the old endpoint, so they go with it. diff --git a/js/moq-boy/src/game.test.ts b/js/moq-boy/src/game.test.ts index a7af181c24c..2157d32d651 100644 --- a/js/moq-boy/src/game.test.ts +++ b/js/moq-boy/src/game.test.ts @@ -18,7 +18,7 @@ test("audio downloads only while the tile is active and audible", async () => { const connection = new Moq.Connection({ enabled: false }); const origin = new Moq.Origin.Producer(); - const endpoint = new Moq.socket.Endpoint(origin, Moq.Path.from("game"), "viewer-test"); + const endpoint = new Moq.Socket.Endpoint(origin, Moq.Path.from("game"), "viewer-test"); const game = new Game({ sessionId: "test", connection, @@ -70,7 +70,7 @@ test("status skips values that fail the schema and ends when the track errors", group.finish(); }; - const endpoint = new Moq.socket.Endpoint(origin, Moq.Path.from("game"), "viewer-test"); + const endpoint = new Moq.Socket.Endpoint(origin, Moq.Path.from("game"), "viewer-test"); const game = new Game({ sessionId: "test", connection, @@ -117,8 +117,8 @@ test("input opens a session that carries commands up and latency down until deac // The session waits for a live connection; the endpoints share one origin instead of a relay. Object.defineProperty(connection, "status", { value: new Moq.Signals.Signal("connected") }); const origin = new Moq.Origin.Producer(); - const emulator = new Moq.socket.Endpoint(origin, Moq.Path.from("game"), "test"); - const endpoint = new Moq.socket.Endpoint(origin, Moq.Path.from("game"), "viewer-test"); + const emulator = new Moq.Socket.Endpoint(origin, Moq.Path.from("game"), "test"); + const endpoint = new Moq.Socket.Endpoint(origin, Moq.Path.from("game"), "viewer-test"); const game = new Game({ sessionId: "test", connection, diff --git a/js/moq-boy/src/game.ts b/js/moq-boy/src/game.ts index a8e6b2ca8b8..57bb4c5ae5b 100644 --- a/js/moq-boy/src/game.ts +++ b/js/moq-boy/src/game.ts @@ -23,7 +23,7 @@ export interface GameConfig { /** The origin the game broadcast arrives in; the connection feeds it. */ origin: Moq.Origin.Producer; /** This viewer's socket endpoint, under {@link gamePrefix}, which dials the game's. */ - endpoint: Moq.socket.Endpoint; + endpoint: Moq.Socket.Endpoint; /** Shared signal tracking which game is currently expanded. */ expanded: Moq.Signals.Signal; /** MoQ path prefix for game broadcasts (e.g. "anon/boy/game"). */ @@ -72,7 +72,7 @@ export class Game { /** The session currently expanded, shared by every game on the page. */ readonly expanded: Moq.Signals.Signal; - readonly #endpoint: Moq.socket.Endpoint; + readonly #endpoint: Moq.Socket.Endpoint; /** Whether the pointer is over this game's tile. */ readonly hovered = new Moq.Signals.Signal(false); @@ -320,7 +320,7 @@ export class Game { // One session per activation: a reload or an idle timeout ends it, and the next input dials a new one. const abort = new AbortController(); - const stream = new Moq.Signals.Signal(undefined); + const stream = new Moq.Signals.Signal(undefined); effect.cleanup(() => { abort.abort(); const open = stream.peek(); @@ -330,7 +330,7 @@ export class Game { }); effect.spawn(async () => { - let open: Moq.socket.TrackStream; + let open: Moq.Socket.TrackStream; try { open = await this.#endpoint.dialTrack(this.sessionId, undefined, { signal: abort.signal }); } catch (err) { @@ -354,7 +354,7 @@ export class Game { effect.run(this.#runCommands.bind(this, stream)); } - #runCommands(stream: Moq.Signals.Signal, effect: Moq.Signals.Effect) { + #runCommands(stream: Moq.Signals.Signal, effect: Moq.Signals.Effect) { const open = effect.get(stream); if (!open) return; diff --git a/js/moq-boy/src/index.ts b/js/moq-boy/src/index.ts index 15e81945722..4e6d5760eef 100644 --- a/js/moq-boy/src/index.ts +++ b/js/moq-boy/src/index.ts @@ -10,5 +10,5 @@ export type { default as MoqBoy } from "./element.tsx"; export type { GameConfig } from "./game.ts"; export { Game, KEY_MAP } from "./game.ts"; export type { GameStats, GameStatus, LatencyEntry } from "./schemas.ts"; -export { GameStatsSchema, GameStatusSchema, LatencyEntrySchema } from "./schemas.ts"; +export { GameStatsSchema, GameStatusSchema } from "./schemas.ts"; export { Moq, Watch }; diff --git a/js/moq-boy/src/schemas.ts b/js/moq-boy/src/schemas.ts index 0d470725210..d76c445efee 100644 --- a/js/moq-boy/src/schemas.ts +++ b/js/moq-boy/src/schemas.ts @@ -8,8 +8,15 @@ export const GameStatsSchema = z.object({ wall_secs: z.number(), }); -/** A single latency component measurement. */ -export const LatencyEntrySchema = z.object({ +/** One component of a viewer's latency, in milliseconds. */ +export interface LatencyEntry { + /** The component's name. */ + label: string; + /** Its duration in milliseconds. */ + ms: number; +} + +const LatencyEntrySchema: z.ZodMiniType = z.object({ label: z.string(), ms: z.number().check(z.int(), z.nonnegative(), z.lte(4294967295)), }); @@ -29,7 +36,5 @@ export const ReplySchema = z.object({ /** Server-reported encoding and emulation performance stats. */ export type GameStats = z.infer; -/** One component of a viewer's latency, in milliseconds. */ -export type LatencyEntry = z.infer; /** Per-frame status published by the emulator. */ export type GameStatus = z.infer; diff --git a/js/net/api/index.api.md b/js/net/api/index.api.md index b9bb6b83b4e..40d9626fd6a 100644 --- a/js/net/api/index.api.md +++ b/js/net/api/index.api.md @@ -296,13 +296,6 @@ export namespace Datagram { readonly name: string; get priority(): Signals.Getter; } - export class Request { - accept(properties?: Partial): Datagram.Producer; - get closed(): Signals.GetPromise; - get name(): string; - get priority(): Signals.Getter; - reject(error: Error): void; - } export function rescale(info: { timescale?: Time.Timescale | null; }, timestamp: Time.Timestamp | undefined): Time.Timestamp | undefined; @@ -370,6 +363,29 @@ export namespace Error { } } +export namespace Frame { + export class Consumer { + get arrived(): Instant | undefined; + readAll(): Promise; + readChunk(): Promise; + readonly sequence: number; + readonly size: number; + readonly timestamp: Time.Timestamp | undefined; + } + export interface Info { + size: number; + timestamp?: Time.Timestamp; + } + export class Producer { + abort(error: Error): void; + finish(): void; + get remaining(): number; + readonly size: number; + readonly timestamp: Time.Timestamp | undefined; + write(chunk: Uint8Array): void; + } +} + export namespace Group { export class Consumer { get arrived(): Instant; @@ -378,7 +394,7 @@ export namespace Group { get done(): boolean; get frameCount(): number; get isClosed(): boolean; - nextFrame(): Promise; + nextFrame(): Promise; readBool(): Promise; readFrame(): Promise; readFrameSequence(options?: Group.ReadOptions): Promise<{ @@ -398,26 +414,6 @@ export namespace Group { payload: Uint8Array; timestamp?: Time.Timestamp; } - export class FrameConsumer { - get arrived(): Instant | undefined; - readAll(): Promise; - readChunk(): Promise; - readonly sequence: number; - readonly size: number; - readonly timestamp: Time.Timestamp | undefined; - } - export interface FrameInfo { - size: number; - timestamp?: Time.Timestamp; - } - export class FrameProducer { - abort(error: Error): void; - finish(): void; - get remaining(): number; - readonly size: number; - readonly timestamp: Time.Timestamp | undefined; - write(chunk: Uint8Array): void; - } export interface Info { sequence: bigint; } @@ -426,7 +422,7 @@ export namespace Group { abort(error: Error): void; get closed(): Signals.GetPromise; consume(): Group.Consumer; - createFrame(info: Group.FrameInfo): Group.FrameProducer; + createFrame(info: Frame.Info): Frame.Producer; finish(): void; get isClosed(): boolean; readonly sequence: bigint; @@ -437,8 +433,6 @@ export namespace Group { } } -export const IMPLEMENTATION: string; - export namespace Instance { export type Valid = string & { readonly _brand: "instance"; @@ -534,7 +528,6 @@ export namespace Origin { export namespace Path { export { InvalidPattern } from "@moq/pattern"; - export const MAX_BYTES: 4096; export const MAX_PARTS: 32; export { Pattern } from "@moq/pattern"; export { Patterns } from "@moq/pattern"; @@ -588,6 +581,41 @@ export type SessionCode = number & { export * as Signals from "@moq/signals"; +export namespace Socket { + export class Endpoint { + constructor(origin: Origin.Producer, prefix: Path.Valid, id: string); + acceptTcp(options?: Socket.Options): Promise>; + acceptTrack(options?: Socket.Options): Promise>; + close(): void; + get closed(): Signals.GetPromise; + dialTcp(peer: string, options?: Socket.Options): Promise; + dialTrack(peer: string, info?: Track.InfoInit, options?: Socket.Options): Promise; + readonly id: string; + readonly prefix: Path.Valid; + } + export class Incoming { + accept(...args: S extends Socket.TcpStream ? [options?: Socket.Options] : [info?: Track.InfoInit, options?: Socket.Options]): Promise; + readonly peer: string; + reject(): void; + readonly session: string; + } + export interface Options { + signal?: AbortSignal; + } + export class TcpStream { + readonly consumer: Group.Consumer; + readonly peer: string; + readonly producer: Group.Producer; + readonly session: string; + } + export class TrackStream { + readonly consumer: Track.Subscriber; + readonly peer: string; + readonly producer: Track.Producer; + readonly session: string; + } +} + export const StreamCode: ((code: number) => StreamCode) & { readonly Cancel: StreamCode; readonly ControlTimeout: StreamCode; @@ -822,39 +850,4 @@ export namespace Varint { export function encodeTo(dst: ArrayBuffer, v: number | bigint): Uint8Array; export function size(v: number): number; } - -export namespace socket { - export class Endpoint { - constructor(origin: Origin.Producer, prefix: Path.Valid, id: string); - acceptTcp(options?: socket.Options): Promise>; - acceptTrack(options?: socket.Options): Promise>; - close(): void; - get closed(): Signals.GetPromise; - dialTcp(peer: string, options?: socket.Options): Promise; - dialTrack(peer: string, info?: Track.InfoInit, options?: socket.Options): Promise; - readonly id: string; - readonly prefix: Path.Valid; - } - export class Incoming { - accept(...args: S extends socket.TcpStream ? [options?: socket.Options] : [info?: Track.InfoInit, options?: socket.Options]): Promise; - readonly peer: string; - reject(): void; - readonly session: string; - } - export interface Options { - signal?: AbortSignal; - } - export class TcpStream { - readonly consumer: Group.Consumer; - readonly peer: string; - readonly producer: Group.Producer; - readonly session: string; - } - export class TrackStream { - readonly consumer: Track.Subscriber; - readonly peer: string; - readonly producer: Track.Producer; - readonly session: string; - } -} ``` diff --git a/js/net/bench/socket.ts b/js/net/bench/socket.ts index fef5e678251..caa634a8a3e 100644 --- a/js/net/bench/socket.ts +++ b/js/net/bench/socket.ts @@ -2,7 +2,7 @@ import { yieldNow } from "@moq/clock"; import { accept } from "../src/connection/accept.ts"; import { connect } from "../src/connection/connect.ts"; -import { Origin, Path, socket } from "../src/index.ts"; +import { Origin, Path, Socket } from "../src/index.ts"; import * as Lite from "../src/lite/index.ts"; import { createMockTransportPair } from "../src/mock.ts"; @@ -33,10 +33,10 @@ for (const mode of ["local", "lite07"] as const) }), ]) : []; - const dialer = new socket.Endpoint(origin, Path.from("bench"), "dialer"); + const dialer = new Socket.Endpoint(origin, Path.from("bench"), "dialer"); const peers = Array.from( { length: endpoints }, - (_, i) => new socket.Endpoint(remote, Path.from("bench"), `peer-${i}`), + (_, i) => new Socket.Endpoint(remote, Path.from("bench"), `peer-${i}`), ); const start = performance.now(); const pairs = await Promise.all( @@ -54,7 +54,7 @@ for (const mode of ["local", "lite07"] as const) pairs.map(async ([local, remote]) => { local.producer.writeFrame({ payload: new Uint8Array(1024) }); const frame = - remote instanceof socket.TcpStream + remote instanceof Socket.TcpStream ? await remote.consumer.readFrame() : await remote.consumer.ordered().readFrame(); if (frame?.payload.length !== 1024) throw new Error("missing socket payload"); diff --git a/js/net/src/connection/connect.ts b/js/net/src/connection/connect.ts index 3e32c75efc3..5f1734fb39a 100644 --- a/js/net/src/connection/connect.ts +++ b/js/net/src/connection/connect.ts @@ -278,8 +278,8 @@ export interface ConnectProps { auth?: Auth.Handle; /** - * The implementation this side advertises in SETUP. Defaults to {@link IMPLEMENTATION}; - * a wrapper prepends its own token, like `my-app/1.0 moq-net-js/0.4.2`. Empty sends nothing. + * The implementation this side advertises in SETUP, a User-Agent style product token such as + * `my-app/1.0`. Defaults to this library's, like `moq-net-js/0.4.2`. Empty sends nothing. */ implementation?: string; diff --git a/js/net/src/datagram.ts b/js/net/src/datagram.ts index 0f7cdde831f..dc607ebf9dc 100644 --- a/js/net/src/datagram.ts +++ b/js/net/src/datagram.ts @@ -187,7 +187,10 @@ export class Producer { } } -/** A datagram track requested on demand, waiting for its publisher's answer. */ +/** + * A datagram track requested on demand, waiting for its publisher's answer. + * @internal Wire layers only. + */ export class Request { #producer: Producer; #pending: Set; diff --git a/js/net/src/frame.ts b/js/net/src/frame.ts new file mode 100644 index 00000000000..f663579dcf5 --- /dev/null +++ b/js/net/src/frame.ts @@ -0,0 +1,325 @@ +/** + * Frame role handles: one frame's payload written or read in pieces as it arrives. + * + * @module + */ +import type { Instant } from "@moq/clock"; +import { type Getter, Once, race, Signal } from "@moq/signals"; +import { StreamCode, StreamError } from "./error.ts"; +import { stored } from "./flate/group.ts"; +import type { Frame } from "./group.ts"; +import { hooks } from "./internal.ts"; +import { Queue } from "./queue.ts"; +import type { Timestamp } from "./time.ts"; +import { SEGMENT_BYTES } from "./util/limits.ts"; + +/** + * A frame's header, declared before its payload, as Rust's `frame::Info`. + * + * @public + */ +export interface Info { + /** The payload size in bytes, which the writes must add up to exactly. */ + size: number; + /** Presentation timestamp, present exactly when the track has a timescale (see `Group.Frame.timestamp`). */ + timestamp?: Timestamp; +} + +// A frame written in pieces, shared by its group and every mirror. Written whole, it stays the +// writer's one buffer; otherwise it is one buffer within a segment, or a chain of +// `SEGMENT_BYTES` segments, each allocated by its first byte. Sealed segments shed from the +// front under the group's tail budget, so a reader that falls behind inside it is `Evicted`. +/** @internal */ +export class Chunked implements Frame { + readonly size: number; + readonly timestamp: Timestamp | undefined; + arrived?: Instant; + // Bytes written so far, which a streaming reader waits on. + readonly written = new Signal(0); + // Settled once every byte is written (`null`), or with the failure that cut the frame short. + readonly done = new Once(); + // The frame offset of the first retained segment. + base = 0; + // Allocated bytes across the retained segments. + retained = 0; + #segments = new Queue(); + // Whether one write carried the whole frame, which is then a single buffer of any size. + #whole = false; + + constructor(info: Info) { + this.size = info.size; + this.timestamp = info.timestamp; + } + + // Append `chunk`, returning the bytes newly allocated for it. + write(chunk: Uint8Array): number { + let at = this.written.peek(); + if (at === 0 && chunk.byteLength === this.size) { + this.#whole = true; + this.#segments.push(chunk); + this.retained += chunk.byteLength; + this.written.set(this.size); + return chunk.byteLength; + } + let allocated = 0; + let offset = 0; + while (offset < chunk.byteLength) { + // The open segment ends at the next segment boundary, or the frame's end. + if (at % SEGMENT_BYTES === 0) { + const segment = new Uint8Array(Math.min(SEGMENT_BYTES, this.size - at)); + this.#segments.push(segment); + this.retained += segment.byteLength; + allocated += segment.byteLength; + } + const segment = this.#segments.last as Uint8Array; + const start = at % SEGMENT_BYTES; + const n = Math.min(chunk.byteLength - offset, segment.byteLength - start); + segment.set(chunk.subarray(offset, offset + n), start); + offset += n; + at += n; + } + this.written.set(at); + return allocated; + } + + // The written bytes from frame offset `at` to the end of its segment, as a view. + chunk(at: number): Uint8Array { + const written = this.written.peek(); + if (this.#whole) { + const whole = this.#segments.first as Uint8Array; + return at === 0 ? whole : whole.subarray(at, written); + } + const index = Math.floor(at / SEGMENT_BYTES); + const segment = this.#segments.at(index - this.base / SEGMENT_BYTES) as Uint8Array; + const start = index * SEGMENT_BYTES; + return segment.subarray(at - start, Math.min(segment.byteLength, written - start)); + } + + // Drop the oldest sealed segment, returning its bytes, or 0 when only the newest is left. + shed(): number { + if (this.#segments.length <= 1) return 0; + const segment = this.#segments.shift() as Uint8Array; + this.base += segment.byteLength; + this.retained -= segment.byteLength; + return segment.byteLength; + } + + // Release every segment once the group lets go of the frame. + release(): void { + this.#segments = new Queue(); + this.base = this.size; + this.retained = 0; + } + + get payload(): Uint8Array { + const done = this.done.peek(); + if (done instanceof Error) throw done; + if (done === undefined) throw new Error("frame is still being written"); + if (this.base > 0) throw evicted(); + if (this.#segments.length === 1) return this.chunk(0); + const out = new Uint8Array(this.size); + let offset = 0; + for (const segment of this.#segments) { + out.set(segment, offset); + offset += segment.byteLength; + } + return out; + } +} + +// The payload size, without joining a chunked frame's segments. +/** @internal */ +export function payloadBytes(frame: Frame): number { + if (frame instanceof Chunked) return frame.size; + return stored(frame)?.payload.length ?? frame.payload.byteLength; +} + +/** @internal */ +export const EMPTY = new Uint8Array(0); + +/** @internal */ +export function evicted(): Error { + return new StreamError(StreamCode.Evicted, { message: "group front was evicted" }); +} + +// What a frame producer drives in its group. +/** @internal */ +export interface Ops { + written(): number; + write(chunk: Uint8Array): void; + finish(): void; + abort(error: Error): void; +} + +/** @internal */ +export let makeProducer: (info: Info, ops: Ops) => Producer; + +/** + * Writes one frame's payload in pieces, as Rust's `frame::Producer`. Obtain one from + * `Group.Producer.createFrame`. + * + * The writes must add up to the declared size exactly, and {@link finish} commits the frame. + * Its group takes no other frame until then; ending the group first fails the frame. + * + * @public + */ +export class Producer { + /** The declared payload size in bytes. */ + readonly size: number; + /** The frame's presentation timestamp, if its track is timed. */ + readonly timestamp: Timestamp | undefined; + #ops: Ops; + #done = false; + + private constructor(info: Info, ops: Ops) { + this.size = info.size; + this.timestamp = info.timestamp; + this.#ops = ops; + } + + static { + makeProducer = (info, ops) => new Producer(info, ops); + } + + /** Bytes still needed to complete the frame. */ + get remaining(): number { + return this.size - this.#ops.written(); + } + + /** Append `chunk`, waking readers streaming the frame. The bytes are copied unless one write carries the whole frame. */ + write(chunk: Uint8Array): void { + if (this.#done) throw new Error("frame is closed"); + if (chunk.byteLength > this.remaining) throw new RangeError("write exceeds the frame's declared size"); + if (chunk.byteLength > 0) this.#ops.write(chunk); + } + + /** Commit the frame once every declared byte is written. */ + finish(): void { + if (this.#done) throw new Error("frame is closed"); + if (this.remaining !== 0) throw new RangeError(`frame is ${this.remaining} bytes short of its declared size`); + this.#done = true; + this.#ops.finish(); + } + + /** Fail the frame and its group with `error`. A no-op once the frame finished or aborted. */ + abort(error: Error): void { + if (this.#done) return; + this.#done = true; + this.#ops.abort(error); + } +} + +/** @internal */ +export let makeConsumer: (sequence: number, frame: Frame) => Consumer; +// The next chunk without waiting: `undefined` at the end, or `WAIT` until the next write. +/** @internal */ +export let pollChunk: (consumer: Consumer) => Uint8Array | undefined | typeof WAIT; +// What a waiting chunk read waits on. +/** @internal */ +export let chunkChanged: (consumer: Consumer) => Getter[]; +/** @internal */ +export const WAIT: unique symbol = Symbol("wait"); + +// Parts read in order, joined into one buffer, copied only when there is more than one. +/** @internal */ +export function joined(parts: Uint8Array[], size: number): Uint8Array { + if (parts.length === 1) return parts[0]; + const out = new Uint8Array(size); + let at = 0; + for (const part of parts) { + out.set(part, at); + at += part.byteLength; + } + return out; +} + +/** + * Reads one frame's payload as it arrives, as Rust's `frame::Consumer`. Obtain one from + * `Group.Consumer.nextFrame`. + * + * Chunks are read-only views shared with other readers. A frame larger than its group's + * retained tail sheds its oldest segments while it is written, so a reader that falls that + * far behind fails with a `StreamError` of code `Evicted`. + * + * @public + */ +export class Consumer { + /** The frame's sequence number within its group. */ + readonly sequence: number; + /** The payload size in bytes. */ + readonly size: number; + /** The frame's presentation timestamp, if its track is timed. */ + readonly timestamp: Timestamp | undefined; + #frame: Frame; + #offset = 0; + + private constructor(sequence: number, frame: Frame) { + this.sequence = sequence; + this.#frame = frame; + this.size = payloadBytes(frame); + this.timestamp = frame.timestamp; + } + + static { + makeConsumer = (sequence, frame) => new Consumer(sequence, frame); + hooks.readFrameChunk = (consumer, closed) => consumer.#readChunk(closed); + pollChunk = (consumer) => consumer.#poll(); + chunkChanged = (consumer) => { + const frame = consumer.#frame; + return frame instanceof Chunked ? [frame.written, frame.done] : []; + }; + } + + /** When the frame's last byte landed in this node's group; `undefined` until then. */ + get arrived(): Instant | undefined { + return this.#frame.arrived; + } + + #poll(): Uint8Array | undefined | typeof WAIT { + const frame = this.#frame; + if (!(frame instanceof Chunked)) { + if (this.#offset >= this.size) return undefined; + this.#offset = this.size; + return frame.payload; + } + const done = frame.done.peek(); + if (done instanceof Error) throw done; + if (this.#offset < frame.base) throw evicted(); + if (this.#offset < frame.written.peek()) { + const chunk = frame.chunk(this.#offset); + this.#offset += chunk.byteLength; + return chunk; + } + return done === null ? undefined : WAIT; + } + + /** The next chunk of the payload, waiting for it to be written; `undefined` once every byte is read. */ + readChunk(): Promise { + return this.#readChunk(); + } + + async #readChunk(closed?: Promise): Promise { + for (;;) { + const chunk = this.#poll(); + if (chunk !== WAIT) return chunk; + const changed = Signal.race(...chunkChanged(this)); + if (closed === undefined) await changed; + // A frame changes with a byte count or a verdict; only the stream resolves void. + else if ((await race([changed, closed])) === undefined) return undefined; + } + } + + /** + * The rest of the payload in one buffer, copied only when it spans chunks. It grows as + * bytes arrive, so a declared size is never allocated ahead of them. + */ + async readAll(): Promise { + const parts: Uint8Array[] = []; + let size = 0; + for (let chunk = await this.readChunk(); chunk; chunk = await this.readChunk()) { + parts.push(chunk); + size += chunk.byteLength; + } + return parts.length === 0 ? EMPTY : joined(parts, size); + } +} diff --git a/js/net/src/group.ts b/js/net/src/group.ts index 4785a3df392..f97a40751ac 100644 --- a/js/net/src/group.ts +++ b/js/net/src/group.ts @@ -7,12 +7,27 @@ import { type Instant, now } from "@moq/clock"; import { type Dispose, type GetPromise, type Getter, Once, race, Signal } from "@moq/signals"; import { FrameTooLarge, ProtocolViolation, StreamCode, StreamError, TimestampMismatch, TooFarBehind } from "./error.ts"; import { Compression, encodable, stored } from "./flate/group.ts"; +import { + Chunked, + chunkChanged, + EMPTY, + evicted, + type Consumer as FrameConsumer, + type Info as FrameInfo, + type Producer as FrameProducer, + joined, + makeConsumer as makeFrameConsumer, + makeProducer as makeFrameProducer, + payloadBytes, + pollChunk, + WAIT, +} from "./frame.ts"; import { type Expiry, type GroupRecover, hooks, type ReadGroupFrame, type Replacement } from "./internal.ts"; import { Queue } from "./queue.ts"; import { rescale } from "./rescale.ts"; import type { Timestamp } from "./time.ts"; import type { Info as TrackInfo } from "./track.ts"; -import { MAX_FRAME_BYTES, SEGMENT_BYTES } from "./util/limits.ts"; +import { MAX_FRAME_BYTES } from "./util/limits.ts"; import { checkSequence } from "./util/sequence.ts"; // A live tail budget, including a conservative charge for each frame's object and queue slot. @@ -25,18 +40,6 @@ function frameBytes(frame: Frame): number { return FRAME_BYTES + (slice ? slice.payload.length + slice.inflated : frame.payload.byteLength); } -// The payload size, without joining a chunked frame's segments. -function payloadBytes(frame: Frame): number { - if (frame instanceof Chunked) return frame.size; - return stored(frame)?.payload.length ?? frame.payload.byteLength; -} - -const EMPTY = new Uint8Array(0); - -function evicted(): Error { - return new StreamError(StreamCode.Evicted, { message: "group front was evicted" }); -} - /** * A frame buffered in a group: its presentation {@link Timestamp}, if any, and payload bytes. * @@ -62,119 +65,6 @@ export interface Frame { arrived?: Instant; } -/** - * A frame's header, declared before its payload, as Rust's `frame::Info`. - * - * @public - */ -export interface FrameInfo { - /** The payload size in bytes, which the writes must add up to exactly. */ - size: number; - /** Presentation timestamp, present exactly when the track has a timescale (see {@link Frame.timestamp}). */ - timestamp?: Timestamp; -} - -// A frame written in pieces, shared by its group and every mirror. Written whole, it stays the -// writer's one buffer; otherwise it is one buffer within a segment, or a chain of -// `SEGMENT_BYTES` segments, each allocated by its first byte. Sealed segments shed from the -// front under the group's tail budget, so a reader that falls behind inside it is `Evicted`. -class Chunked implements Frame { - readonly size: number; - readonly timestamp: Timestamp | undefined; - arrived?: Instant; - // Bytes written so far, which a streaming reader waits on. - readonly written = new Signal(0); - // Settled once every byte is written (`null`), or with the failure that cut the frame short. - readonly done = new Once(); - // The frame offset of the first retained segment. - base = 0; - // Allocated bytes across the retained segments. - retained = 0; - #segments = new Queue(); - // Whether one write carried the whole frame, which is then a single buffer of any size. - #whole = false; - - constructor(info: FrameInfo) { - this.size = info.size; - this.timestamp = info.timestamp; - } - - // Append `chunk`, returning the bytes newly allocated for it. - write(chunk: Uint8Array): number { - let at = this.written.peek(); - if (at === 0 && chunk.byteLength === this.size) { - this.#whole = true; - this.#segments.push(chunk); - this.retained += chunk.byteLength; - this.written.set(this.size); - return chunk.byteLength; - } - let allocated = 0; - let offset = 0; - while (offset < chunk.byteLength) { - // The open segment ends at the next segment boundary, or the frame's end. - if (at % SEGMENT_BYTES === 0) { - const segment = new Uint8Array(Math.min(SEGMENT_BYTES, this.size - at)); - this.#segments.push(segment); - this.retained += segment.byteLength; - allocated += segment.byteLength; - } - const segment = this.#segments.last as Uint8Array; - const start = at % SEGMENT_BYTES; - const n = Math.min(chunk.byteLength - offset, segment.byteLength - start); - segment.set(chunk.subarray(offset, offset + n), start); - offset += n; - at += n; - } - this.written.set(at); - return allocated; - } - - // The written bytes from frame offset `at` to the end of its segment, as a view. - chunk(at: number): Uint8Array { - const written = this.written.peek(); - if (this.#whole) { - const whole = this.#segments.first as Uint8Array; - return at === 0 ? whole : whole.subarray(at, written); - } - const index = Math.floor(at / SEGMENT_BYTES); - const segment = this.#segments.at(index - this.base / SEGMENT_BYTES) as Uint8Array; - const start = index * SEGMENT_BYTES; - return segment.subarray(at - start, Math.min(segment.byteLength, written - start)); - } - - // Drop the oldest sealed segment, returning its bytes, or 0 when only the newest is left. - shed(): number { - if (this.#segments.length <= 1) return 0; - const segment = this.#segments.shift() as Uint8Array; - this.base += segment.byteLength; - this.retained -= segment.byteLength; - return segment.byteLength; - } - - // Release every segment once the group lets go of the frame. - release(): void { - this.#segments = new Queue(); - this.base = this.size; - this.retained = 0; - } - - get payload(): Uint8Array { - const done = this.done.peek(); - if (done instanceof Error) throw done; - if (done === undefined) throw new Error("frame is still being written"); - if (this.base > 0) throw evicted(); - if (this.#segments.length === 1) return this.chunk(0); - const out = new Uint8Array(this.size); - let offset = 0; - for (const segment of this.#segments) { - out.set(segment, offset); - offset += segment.byteLength; - } - return out; - } -} - // Whether a frame's head was shed, so it can no longer be read from its start. function headShed(frame: Frame | undefined): boolean { return frame instanceof Chunked && frame.base > 0; @@ -739,181 +629,6 @@ export class Producer { } } -// What a frame producer drives in its group. -interface FrameOps { - written(): number; - write(chunk: Uint8Array): void; - finish(): void; - abort(error: Error): void; -} - -let makeFrameProducer: (info: FrameInfo, ops: FrameOps) => FrameProducer; - -/** - * Writes one frame's payload in pieces, as Rust's `frame::Producer`. Obtain one from - * {@link Producer.createFrame}. - * - * The writes must add up to the declared size exactly, and {@link finish} commits the frame. - * Its group takes no other frame until then; ending the group first fails the frame. - * - * @public - */ -export class FrameProducer { - /** The declared payload size in bytes. */ - readonly size: number; - /** The frame's presentation timestamp, if its track is timed. */ - readonly timestamp: Timestamp | undefined; - #ops: FrameOps; - #done = false; - - private constructor(info: FrameInfo, ops: FrameOps) { - this.size = info.size; - this.timestamp = info.timestamp; - this.#ops = ops; - } - - static { - makeFrameProducer = (info, ops) => new FrameProducer(info, ops); - } - - /** Bytes still needed to complete the frame. */ - get remaining(): number { - return this.size - this.#ops.written(); - } - - /** Append `chunk`, waking readers streaming the frame. The bytes are copied unless one write carries the whole frame. */ - write(chunk: Uint8Array): void { - if (this.#done) throw new Error("frame is closed"); - if (chunk.byteLength > this.remaining) throw new RangeError("write exceeds the frame's declared size"); - if (chunk.byteLength > 0) this.#ops.write(chunk); - } - - /** Commit the frame once every declared byte is written. */ - finish(): void { - if (this.#done) throw new Error("frame is closed"); - if (this.remaining !== 0) throw new RangeError(`frame is ${this.remaining} bytes short of its declared size`); - this.#done = true; - this.#ops.finish(); - } - - /** Fail the frame and its group with `error`. A no-op once the frame finished or aborted. */ - abort(error: Error): void { - if (this.#done) return; - this.#done = true; - this.#ops.abort(error); - } -} - -let makeFrameConsumer: (sequence: number, frame: Frame) => FrameConsumer; -// The next chunk without waiting: `undefined` at the end, or `WAIT` until the next write. -let pollChunk: (consumer: FrameConsumer) => Uint8Array | undefined | typeof WAIT; -// What a waiting chunk read waits on. -let chunkChanged: (consumer: FrameConsumer) => Getter[]; -const WAIT: unique symbol = Symbol("wait"); - -// Parts read in order, joined into one buffer, copied only when there is more than one. -function joined(parts: Uint8Array[], size: number): Uint8Array { - if (parts.length === 1) return parts[0]; - const out = new Uint8Array(size); - let at = 0; - for (const part of parts) { - out.set(part, at); - at += part.byteLength; - } - return out; -} - -/** - * Reads one frame's payload as it arrives, as Rust's `frame::Consumer`. Obtain one from - * {@link Consumer.nextFrame}. - * - * Chunks are read-only views shared with other readers. A frame larger than its group's - * retained tail sheds its oldest segments while it is written, so a reader that falls that - * far behind fails with a `StreamError` of code `Evicted`. - * - * @public - */ -export class FrameConsumer { - /** The frame's sequence number within its group. */ - readonly sequence: number; - /** The payload size in bytes. */ - readonly size: number; - /** The frame's presentation timestamp, if its track is timed. */ - readonly timestamp: Timestamp | undefined; - #frame: Frame; - #offset = 0; - - private constructor(sequence: number, frame: Frame) { - this.sequence = sequence; - this.#frame = frame; - this.size = payloadBytes(frame); - this.timestamp = frame.timestamp; - } - - static { - makeFrameConsumer = (sequence, frame) => new FrameConsumer(sequence, frame); - hooks.readFrameChunk = (consumer, closed) => consumer.#readChunk(closed); - pollChunk = (consumer) => consumer.#poll(); - chunkChanged = (consumer) => { - const frame = consumer.#frame; - return frame instanceof Chunked ? [frame.written, frame.done] : []; - }; - } - - /** When the frame's last byte landed in this node's group; `undefined` until then. */ - get arrived(): Instant | undefined { - return this.#frame.arrived; - } - - #poll(): Uint8Array | undefined | typeof WAIT { - const frame = this.#frame; - if (!(frame instanceof Chunked)) { - if (this.#offset >= this.size) return undefined; - this.#offset = this.size; - return frame.payload; - } - const done = frame.done.peek(); - if (done instanceof Error) throw done; - if (this.#offset < frame.base) throw evicted(); - if (this.#offset < frame.written.peek()) { - const chunk = frame.chunk(this.#offset); - this.#offset += chunk.byteLength; - return chunk; - } - return done === null ? undefined : WAIT; - } - - /** The next chunk of the payload, waiting for it to be written; `undefined` once every byte is read. */ - readChunk(): Promise { - return this.#readChunk(); - } - - async #readChunk(closed?: Promise): Promise { - for (;;) { - const chunk = this.#poll(); - if (chunk !== WAIT) return chunk; - const changed = Signal.race(...chunkChanged(this)); - if (closed === undefined) await changed; - // A frame changes with a byte count or a verdict; only the stream resolves void. - else if ((await race([changed, closed])) === undefined) return undefined; - } - } - - /** - * The rest of the payload in one buffer, copied only when it spans chunks. It grows as - * bytes arrive, so a declared size is never allocated ahead of them. - */ - async readAll(): Promise { - const parts: Uint8Array[] = []; - let size = 0; - for (let chunk = await this.readChunk(); chunk; chunk = await this.readChunk()) { - parts.push(chunk); - size += chunk.byteLength; - } - return parts.length === 0 ? EMPTY : joined(parts, size); - } -} - let makeConsumer: (state: GroupState) => Consumer; // A replacement copy asked for on behalf of one consumer, settled or still out. diff --git a/js/net/src/implementation.ts b/js/net/src/implementation.ts index 7eb608f1405..65dbf649957 100644 --- a/js/net/src/implementation.ts +++ b/js/net/src/implementation.ts @@ -1,9 +1,5 @@ import { VERSION } from "./version.ts"; -/** - * This library's name and version, the default each side advertises in SETUP. - * - * A User-Agent style product token. A wrapper prepends its own, outermost first: - * `my-app/1.0 moq-net-js/0.4.2`. Run from source rather than a published build, it reads `moq-net-js/dev`. - */ +// This library's name and version as a User-Agent style product token, the default each side +// advertises in SETUP. Run from source rather than a published build, it reads `moq-net-js/dev`. export const IMPLEMENTATION = `moq-net-js/${VERSION}`; diff --git a/js/net/src/index.ts b/js/net/src/index.ts index 6023214dc27..ddc44c05f06 100644 --- a/js/net/src/index.ts +++ b/js/net/src/index.ts @@ -23,9 +23,10 @@ export * as Datagram from "./datagram.ts"; export { SessionCode, StreamCode } from "./error.ts"; /** Session, stream, and local request errors. */ export * as Error from "./errors.ts"; -/** Group role handles and frame helpers. */ +/** Frame role handles: one frame's payload written or read in pieces. */ +export * as Frame from "./frame.ts"; +/** Group role handles. */ export * as Group from "./group.ts"; -export { IMPLEMENTATION } from "./implementation.ts"; /** Stream instances: one continuous stream at a path, carried on every route. */ export * as Instance from "./instance.ts"; /** Broadcast routing tables, independent of any connection. */ @@ -33,7 +34,7 @@ export * as Origin from "./origin.ts"; /** Broadcast path utilities with delimiter-aware prefix matching. Path patterns are re-exported from `@moq/pattern`. */ export * as Path from "./path.ts"; /** Point-to-point TCP and track sessions through an origin. */ -export * as socket from "./socket.ts"; +export * as Socket from "./socket.ts"; /** Presentation timestamps and their timescales; duration units live in `@moq/clock`. */ export * as Time from "./time.ts"; /** A value paired with the timestamp it was captured at, absent when untimed. */ diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 8495da60ff0..5f4fb076f93 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -11,7 +11,8 @@ import type { Consumer as BroadcastConsumer, Producer as BroadcastProducer } fro import type * as datagram from "./datagram.ts"; import type { Delivered } from "./delivered.ts"; import { StreamCode, StreamError, toStreamCode } from "./error.ts"; -import type { Frame, FrameConsumer, Consumer as GroupConsumer, Producer as GroupProducer } from "./group.ts"; +import type { Consumer as FrameConsumer } from "./frame.ts"; +import type { Frame, Consumer as GroupConsumer, Producer as GroupProducer } from "./group.ts"; import { isAnonymous, type WireRoute } from "./hop.ts"; import type * as Instance from "./instance.ts"; import type { Consumer as OriginConsumer, Producer as OriginProducer } from "./origin.ts"; diff --git a/js/net/src/lite/connection.ts b/js/net/src/lite/connection.ts index cf60eaa6199..5ea2d4a8068 100644 --- a/js/net/src/lite/connection.ts +++ b/js/net/src/lite/connection.ts @@ -76,7 +76,7 @@ export interface ConnectionProps { client?: boolean; /** The connection credential the client's SETUP presents, on versions that carry one (lite-07+). */ token?: Uint8Array; - /** The implementation our SETUP advertises (lite-05+). Defaults to {@link IMPLEMENTATION}; empty sends nothing. */ + /** The implementation our SETUP advertises (lite-05+). Defaults to this library's; empty sends nothing. */ implementation?: string; /** The PROBE target to send a peer that advertised `Increase`, in bits per second; zero sends none. */ probeTarget?: Getter; diff --git a/js/net/src/path.ts b/js/net/src/path.ts index 82f5dde7eff..e0495608f3e 100644 --- a/js/net/src/path.ts +++ b/js/net/src/path.ts @@ -61,6 +61,8 @@ export const MAX_PARTS = 32; * * Matches the IETF moq-transport limit of 4096 bytes for a namespace. Its tuple fields * drop the path's separators and escapes, so every path fits both wires. + * + * @internal */ export const MAX_BYTES = 4096; @@ -98,7 +100,7 @@ export function parts(path: Valid): string[] { } /** - * Validate an untrusted wire string as a path, enforcing {@link MAX_PARTS} and {@link MAX_BYTES}. + * Validate an untrusted wire string as a path, enforcing {@link MAX_PARTS} and a 4096 byte limit. * * Throws when the path has an empty segment or is too deep or too long; use at wire decode sites. */ @@ -107,7 +109,7 @@ export function decode(raw: string): Valid { } /** - * Validate a path before writing it to the wire, enforcing {@link MAX_PARTS} and {@link MAX_BYTES}. + * Validate a path before writing it to the wire, enforcing {@link MAX_PARTS} and a 4096 byte limit. * * Throws when the path is too deep or too long; use at wire encode sites so we never * emit a path the remote side is required to reject. diff --git a/js/net/src/payload.ts b/js/net/src/payload.ts index be73a0ad7b7..b9d92753861 100644 --- a/js/net/src/payload.ts +++ b/js/net/src/payload.ts @@ -5,6 +5,7 @@ * @module */ import { race } from "@moq/signals"; +import type * as frame from "./frame.ts"; import type * as group from "./group.ts"; import { hooks, type ReadGroupFrame } from "./internal.ts"; import type { Cursor, Reader } from "./stream.ts"; @@ -61,7 +62,7 @@ export function readSize(read: ReadGroupFrame): number { /** Write a streamed payload with `write` as it arrives. Throws if `closed` settles first, since the stream can take no more. */ export async function sendPayload( - chunks: group.FrameConsumer, + chunks: frame.Consumer, write: (chunk: Uint8Array) => Promise, closed: Promise, ): Promise { diff --git a/js/net/src/socket.test.ts b/js/net/src/socket.test.ts index dd5f9670cb1..12c4cc4c421 100644 --- a/js/net/src/socket.test.ts +++ b/js/net/src/socket.test.ts @@ -5,7 +5,7 @@ import { accept as acceptConnection } from "./connection/accept.ts"; import { connect } from "./connection/connect.ts"; import { NotFound, ProtocolViolation, StreamCode, StreamError } from "./error.ts"; import * as Ietf from "./ietf/index.ts"; -import { Origin, Path, socket } from "./index.ts"; +import { Origin, Path, Socket } from "./index.ts"; import { hooks } from "./internal.ts"; import * as Lite from "./lite/index.ts"; import { createMockTransportPair } from "./mock.ts"; @@ -29,8 +29,8 @@ const bytes = (value: number) => ({ payload: new Uint8Array([value]) }); function setup() { const origin = new Origin.Producer(); origins.add(origin); - const a = new socket.Endpoint(origin, Path.from("room"), "alice"); - const b = new socket.Endpoint(origin, Path.from("room"), "bob"); + const a = new Socket.Endpoint(origin, Path.from("room"), "alice"); + const b = new Socket.Endpoint(origin, Path.from("room"), "bob"); return { origin, a, b }; } async function tcp() { @@ -60,18 +60,18 @@ async function raw(origin: Origin.Producer, role: Role, id: string, name: string return (await broadcast(origin, role, id)).track(name).subscribe({ order: "ascending", delay: { max: null } }); } -test("socket namespace exposes endpoint and both stream roles", () => { - expect(socket.Endpoint).toBeFunction(); - expect(socket.TcpStream).toBeFunction(); - expect(socket.TrackStream).toBeFunction(); +test("Socket namespace exposes endpoint and both stream roles", () => { + expect(Socket.Endpoint).toBeFunction(); + expect(Socket.TcpStream).toBeFunction(); + expect(Socket.TrackStream).toBeFunction(); }); test("an endpoint closes with its origin before it dials or accepts", async () => { const origin = new Origin.Producer(); - const endpoint = new socket.Endpoint(origin, Path.from("room"), "alice"); + const endpoint = new Socket.Endpoint(origin, Path.from("room"), "alice"); origin.close(); expect(await endpoint.closed).toBeNull(); - expect(new socket.Endpoint(origin, Path.from("room"), "bob").closed.peek()).toBeNull(); + expect(new Socket.Endpoint(origin, Path.from("room"), "bob").closed.peek()).toBeNull(); }); test("TCP carries ordered frames in both directions, with independent FIN", async () => { @@ -232,7 +232,7 @@ for (const protocol of ["tcp", "track"] as const) { const next = protocol === "tcp" ? b.acceptTcp() : b.acceptTrack(); void next.catch(() => {}); const reset = new StreamError(StreamCode(70)); - const oldReader = local instanceof socket.TcpStream ? local.consumer : local.consumer.ordered(); + const oldReader = local instanceof Socket.TcpStream ? local.consumer : local.consumer.ordered(); remote.producer.abort(reset); await expect(oldReader.readFrame()).rejects.toBeInstanceOf(Error); const source = await broadcast(origin, ".server", "bob"); @@ -265,7 +265,7 @@ for (const protocol of ["tcp", "track"] as const) { const localFresh = await dialing; localFresh.producer.writeFrame(bytes(9)); const frame = - remoteFresh instanceof socket.TcpStream + remoteFresh instanceof Socket.TcpStream ? await remoteFresh.consumer.readFrame() : await remoteFresh.consumer.ordered().readFrame(); expect(frame?.payload).toEqual(bytes(9).payload); @@ -381,7 +381,7 @@ test("a dial waits for the peer to announce, without an arbitrary timeout", asyn const { origin, a } = setup(); const dialing = a.dialTcp("later"); await clock.advance(Nano.fromSecond(Second(3600))); - const later = new socket.Endpoint(origin, Path.from("room"), "later"); + const later = new Socket.Endpoint(origin, Path.from("room"), "later"); const incoming = await later.acceptTcp(); const remote = await incoming.accept(); const local = await dialing; @@ -548,7 +548,7 @@ test("unpaced TCP eviction is an error, never a skipped frame or clean FIN", asy test("endpoint ids refuse separators, and close is idempotent", async () => { const { origin, a } = setup(); for (const id of ["", "a/b", "a\\b"]) { - expect(() => new socket.Endpoint(origin, Path.from("room"), id)).toThrow(); + expect(() => new Socket.Endpoint(origin, Path.from("room"), id)).toThrow(); await expect(a.dialTcp(id)).rejects.toBeInstanceOf(Error); } a.close(); @@ -565,7 +565,7 @@ for (const protocol of ["tcp", "track"] as const) { const bo = new Origin.Producer(); origins.add(ao); origins.add(bo); - const a = new socket.Endpoint(ao, Path.from("room"), "alice"); + const a = new Socket.Endpoint(ao, Path.from("room"), "alice"); const route = bo.route(Path.from("room/.server/bob"), { hidden: true }); const requested = wireOf(route.broadcast).requested(); route.announce(); @@ -609,7 +609,7 @@ for (const protocol of ["tcp", "track"] as const) { staged.commit(); await clock.advance(Nano.zero); const local = await dialing; - const reader = local instanceof socket.TcpStream ? local.consumer : local.consumer.ordered(); + const reader = local instanceof Socket.TcpStream ? local.consumer : local.consumer.ordered(); const received: number[] = []; let completed = false; let failure: unknown; @@ -649,8 +649,8 @@ for (const alpn of [Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_18, Ietf.ALPN.DRAFT_20, Ie const bo = new Origin.Producer(); origins.add(ao); origins.add(bo); - const a = new socket.Endpoint(ao, Path.from("room"), "alice"); - const b = new socket.Endpoint(bo, Path.from("room"), "bob"); + const a = new Socket.Endpoint(ao, Path.from("room"), "alice"); + const b = new Socket.Endpoint(bo, Path.from("room"), "bob"); const pair = createMockTransportPair(alpn); const url = new URL("https://localhost/socket"); const [client, server] = await Promise.all([ @@ -691,8 +691,8 @@ for (const alpn of [Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_18, Ietf.ALPN.DRAFT_20, Ie const bo = new Origin.Producer(); origins.add(ao); origins.add(bo); - const a = new socket.Endpoint(ao, Path.from("room"), "alice"); - const b = new socket.Endpoint(bo, Path.from("room"), "bob"); + const a = new Socket.Endpoint(ao, Path.from("room"), "alice"); + const b = new Socket.Endpoint(bo, Path.from("room"), "bob"); const pair = createMockTransportPair(alpn); const url = new URL("https://localhost/socket"); const [client, server] = await Promise.all([ @@ -718,7 +718,7 @@ for (const alpn of [Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_18, Ietf.ALPN.DRAFT_20, Ie const local = await dialing; local.producer.writeFrame(bytes(7)); const frame = - remote instanceof socket.TcpStream + remote instanceof Socket.TcpStream ? await remote.consumer.readFrame() : await remote.consumer.ordered().readFrame(); expect(frame?.payload).toEqual(bytes(7).payload); @@ -741,8 +741,8 @@ test("a socket endpoint behind a lite-06 client fails its dials and accepts, not const bo = new Origin.Producer(); origins.add(ao); origins.add(bo); - const a = new socket.Endpoint(ao, Path.from("room"), "alice"); - const b = new socket.Endpoint(bo, Path.from("room"), "bob"); + const a = new Socket.Endpoint(ao, Path.from("room"), "alice"); + const b = new Socket.Endpoint(bo, Path.from("room"), "bob"); // Already waiting before the session attaches, as when an endpoint's client reconnects. const waiting = a.dialTcp("bob").then( () => undefined, diff --git a/js/publish/api/element.api.md b/js/publish/api/element.api.md index 8096ec435e0..e624c896f52 100644 --- a/js/publish/api/element.api.md +++ b/js/publish/api/element.api.md @@ -20,14 +20,14 @@ export default class extends HTMLElement { get announce(): AnnounceMode | undefined; set announce(value: AnnounceMode); attributeChangedCallback(name: "source" | "url" | "stats" | "announce" | "name" | "capture" | "backend" | "versions" | "muted" | "invisible" | "preview" | "echo-path", oldValue: string | null, newValue: string | null): void; - audio: Audio.Encoder; + readonly audio: Audio.Encoder; get backend(): Preview.Backend; set backend(value: Preview.Backend); - broadcast: Broadcast; + readonly broadcast: Broadcast; get capture(): CaptureMode; set capture(value: CaptureMode); connectedCallback(): void; - connection: Net.Connection; + readonly connection: Net.Connection; readonly controls: { announce: Signals.Signal; backend: Signals.Signal; @@ -65,6 +65,6 @@ export default class extends HTMLElement { set url(value: string | URL | undefined); get versions(): readonly string[] | undefined; set versions(value: readonly string[] | undefined); - video: Video.Encoder; + readonly video: Video.Encoder; } ``` diff --git a/js/publish/api/index.api.md b/js/publish/api/index.api.md index 39b742a0458..7278bc47db2 100644 --- a/js/publish/api/index.api.md +++ b/js/publish/api/index.api.md @@ -55,7 +55,7 @@ export namespace Audio { }; export type EncoderOutput = { active: Signals.Signal; - catalog: Signals.Signal; + catalog: Signals.Signal; error: Signals.Signal; root: Signals.Signal; stats: Signals.Signal; @@ -80,7 +80,7 @@ export namespace Audio { packetlossperc?: number; useinbandfec?: boolean; }; - export type Rendition = import("@moq/publish").Rendition; + export type Rendition = import("@moq/publish").Rendition; export interface SampleSource { channelCount: number; kind: Audio.Kind; @@ -120,15 +120,14 @@ export namespace Audio { export class Broadcast { constructor(props?: Signals.Inputs); static readonly CATALOG_TRACK: "catalog.json"; - static readonly STATS_TRACK: "stats.json"; - audio(name: string): Rendition; + audio(name: string): Rendition; readonly catalog: CatalogProducer; close(): void; readonly in: Signals.Readonlys; readonly join: Signals.Signal; readonly net: Signals.Getter; text(name: string): Rendition; - video(name: string): Rendition; + video(name: string): Rendition; } export type BroadcastInput = { @@ -157,18 +156,10 @@ export class CatalogProducer { export * as Clock from "@moq/clock"; -export class Fanout { - constructor(source: ReadableStream, props?: FanoutProps); - close(): void; +export interface Fanout { subscribe(effect: Signals.Effect, queue?: number): ReadableStream; } -export interface FanoutProps { - clone?: (value: T) => T; - queue?: number; - release?: (value: T) => void; -} - export * as Hang from "@moq/hang"; export type Kind = "video" | "audio" | "text"; @@ -393,7 +384,7 @@ export namespace Video { }; export type EncoderOutput = { active: Signals.Signal; - catalog: Signals.Signal; + catalog: Signals.Signal; error: Signals.Signal; resolved: Signals.Signal; stats: Signals.Signal; @@ -405,7 +396,7 @@ export namespace Video { frameRate?: number; frames: ReadableStream; } - export type Rendition = import("@moq/publish").Rendition; + export type Rendition = import("@moq/publish").Rendition; export type Source = Video.StreamTrack | Video.SourceConfig | Video.FrameSource; export interface SourceConfig { scale?: number; diff --git a/js/publish/src/broadcast.test.ts b/js/publish/src/broadcast.test.ts index fef92ad70ea..e043c2276e9 100644 --- a/js/publish/src/broadcast.test.ts +++ b/js/publish/src/broadcast.test.ts @@ -118,7 +118,7 @@ test("shared Rust stats names survive the publisher's report builder", async () const net = broadcast.net.peek(); if (!net) throw new Error("expected a network producer"); const consumer = new Json.Snapshot.Consumer({ - track: net.track(Broadcast.STATS_TRACK), + track: net.track("stats.json"), schema: Stats.SnapshotSchema, }); try { @@ -936,12 +936,12 @@ test("stats names a track in the catalog and reports each listed rendition while stats.set(true); await settle(); - expect((await readCatalog(broadcast))?.stats).toEqual({ track: Broadcast.STATS_TRACK }); + expect((await readCatalog(broadcast))?.stats).toEqual({ track: "stats.json" }); const net = broadcast.net.peek(); if (!net) throw new Error("expected a network producer once connected"); const consumer = new Json.Snapshot.Consumer({ - track: net.track(Broadcast.STATS_TRACK), + track: net.track("stats.json"), schema: Stats.SnapshotSchema, }); await settle(); @@ -980,7 +980,7 @@ test("an unsubscribed stats track writes nothing", async () => { const net = broadcast.net.peek(); if (!net) throw new Error("expected a network producer once connected"); - const subscriber = net.track(Broadcast.STATS_TRACK).subscribe(); + const subscriber = net.track("stats.json").subscribe(); await settle(); await clock.advance(Nano.fromMilli(Milli(1000))); // Nothing was written while nobody subscribed, so the first report opens the first group. diff --git a/js/publish/src/broadcast.ts b/js/publish/src/broadcast.ts index 2a31e199364..0be9a147598 100644 --- a/js/publish/src/broadcast.ts +++ b/js/publish/src/broadcast.ts @@ -15,6 +15,9 @@ import { makeRendition } from "./rendition-internal"; // outweigh twice its keyframe. Matches moq-video's capture `REFRESH`; see it for the measurements. const VIDEO_REFRESH = 2; +// The stats track, named in the catalog's `stats` section while stats are on. Matches moq-cli's. +const STATS_TRACK = "stats.json"; + /** * Signals the broadcast reads. Whoever owns the backing Signal (the element, or another component * whose output is wired in, e.g. a Video.Capture's `display`) does the writing. @@ -117,9 +120,6 @@ export class Broadcast { /** The catalog track name served to subscribers. */ static readonly CATALOG_TRACK = Catalog.TRACK; - /** The stats track name, named in the catalog's `stats` section while {@link BroadcastInput.stats} is set. */ - static readonly STATS_TRACK = "stats.json"; - /** The wired inputs, read-only. */ readonly in: Readonlys; @@ -382,7 +382,7 @@ export class Broadcast { const writer = report(); this.catalog.mutate((catalog) => { - catalog.stats = { track: Broadcast.STATS_TRACK }; + catalog.stats = { track: STATS_TRACK }; }); effect.cleanup(() => this.catalog.mutate((catalog) => { @@ -474,7 +474,7 @@ export class Broadcast { // Created on first use and kept with the broadcast, like a rendition's track. let stats: Report.Writer | undefined; const report = () => { - stats ??= new Report.Writer(broadcast, Broadcast.STATS_TRACK, Stats.SnapshotSchema); + stats ??= new Report.Writer(broadcast, STATS_TRACK, Stats.SnapshotSchema); return stats; }; effect.cleanup(() => stats?.finish()); diff --git a/js/publish/src/element.ts b/js/publish/src/element.ts index 997339c5ec6..d266a01807e 100644 --- a/js/publish/src/element.ts +++ b/js/publish/src/element.ts @@ -157,18 +157,18 @@ export default class MoqPublish extends HTMLElement { * same URL. The broadcast publishes into its `origin`, so a `` on the same * page and URL resolves it locally with no round trip. */ - connection: Moq.Connection; + readonly connection: Moq.Connection; #capture: Video.Capture; /** The broadcast being published. */ - broadcast: Broadcast; + readonly broadcast: Broadcast; /** * The single video encoder, tuned via `video.config`. For multiple renditions (e.g. simulcast), drop * the element and register your own encoders on a Broadcast via the JS API. */ - video: Video.Encoder; + readonly video: Video.Encoder; /** The single audio encoder, tuned via its knobs (`codec`, `volume`, ...). */ - audio: Audio.Encoder; + readonly audio: Audio.Encoder; // The selected input sources: the Camera/Screen, Microphone/Screen, and File holders driving capture. // Read by the UI (device pickers) and written by #runSource. diff --git a/js/publish/src/fanout.ts b/js/publish/src/fanout.ts index cf0de53d1ae..59c7469625b 100644 --- a/js/publish/src/fanout.ts +++ b/js/publish/src/fanout.ts @@ -114,7 +114,12 @@ export class Fanout { ); } - /** Stop distributing, cancel the source, and release anything still queued. */ + /** + * Stop distributing, cancel the source, and release anything still queued. Only the capture that + * owns the fanout closes it; a reader cancels its own stream instead. + * + * @internal + */ close(): void { if (this.#closed) return; this.#closed = true; diff --git a/js/publish/src/index.ts b/js/publish/src/index.ts index 0581edb03b7..d781aa62c24 100644 --- a/js/publish/src/index.ts +++ b/js/publish/src/index.ts @@ -20,7 +20,7 @@ export { assets } from "./assets"; export * as Audio from "./audio"; export * from "./broadcast"; export * from "./catalog"; -export { Fanout, type FanoutProps } from "./fanout"; +export type { Fanout } from "./fanout"; /** The local canvas preview. */ export * as Preview from "./preview"; export * from "./rendition"; diff --git a/js/qmux/api/index.api.md b/js/qmux/api/index.api.md index 17870d4a713..d206a8c5826 100644 --- a/js/qmux/api/index.api.md +++ b/js/qmux/api/index.api.md @@ -56,11 +56,6 @@ export interface SessionOptions extends WebTransportOptions { versions?: Record; } -export interface StreamCredit { - available: bigint; - blocked: boolean; -} - export class StreamError extends Error implements Pick { constructor(code: number, message?: string); readonly source: "stream"; @@ -137,7 +132,6 @@ export default class implements WebTransport { readonly ready: Promise; get reliability(): "reliable-only"; setSendBufferSize(bytes: number): void; - streamCredit(dir: "uni" | "bi"): StreamCredit; } export function install(options?: InstallOptions): boolean; diff --git a/js/qmux/src/credit.ts b/js/qmux/src/credit.ts index fff86a724bc..abbc07f43b1 100644 --- a/js/qmux/src/credit.ts +++ b/js/qmux/src/credit.ts @@ -25,7 +25,6 @@ export class Credit { #used: bigint; #max: bigint; #released = 0n; - #blocked = false; #closed = false; #closeReason?: Error; // A Set, so an aborted claim unlinks itself in O(1). @@ -41,10 +40,7 @@ export class Credit { tryClaim(limit: bigint): bigint { if (limit === 0n || this.#closed) return 0n; const available = this.#max - this.#used; - if (available <= 0n) { - this.#blocked = true; - return 0n; - } + if (available <= 0n) return 0n; const claimed = limit < available ? limit : available; this.#used += claimed; return claimed; @@ -140,11 +136,6 @@ export class Credit { return avail > 0n ? avail : 0n; } - /** Whether a claim found no credit since credit last returned, as QUIC's *_BLOCKED says. */ - get blocked(): boolean { - return this.#blocked; - } - /** Get the current max value. */ get max(): bigint { return this.#max; @@ -156,7 +147,6 @@ export class Credit { } #wake(): void { - this.#blocked = false; const waiters = this.#waiters; this.#waiters = new Set(); for (const { resolve } of waiters) resolve(); diff --git a/js/qmux/src/index.ts b/js/qmux/src/index.ts index 55ffaedeefe..dddadb09e37 100644 --- a/js/qmux/src/index.ts +++ b/js/qmux/src/index.ts @@ -11,7 +11,6 @@ export type { SendStreamOptions, SendStreamStats, SessionOptions, - StreamCredit, Version, } from "./session.ts"; export { selectSubprotocol } from "./session.ts"; diff --git a/js/qmux/src/session.test.ts b/js/qmux/src/session.test.ts index e256121871c..32e9a329cee 100644 --- a/js/qmux/src/session.test.ts +++ b/js/qmux/src/session.test.ts @@ -2100,25 +2100,6 @@ describe("Session integration (scripted peer)", () => { // Chrome rejects an over-limit create rather than queueing it, and @moq/net // passes waitUntilAvailable: false where it relies on that. - test("streamCredit counts down per direction and reports a waiting create as blocked", async () => { - const { session, peer } = connect(); - peer.send({ type: "transport_parameters", params: peerParams({ initialMaxStreamsUni: 1n }) }); - await session.ready; - - await session.createUnidirectionalStream(); - expect(session.streamCredit("uni")).toEqual({ available: 0n, blocked: false }); - const waiting = session.createUnidirectionalStream(); - await flushMicrotasks(); - expect(session.streamCredit("uni")).toEqual({ available: 0n, blocked: true }); - expect(session.streamCredit("bi")).toEqual({ available: 100n, blocked: false }); - - // The peer's MAX_STREAMS unblocks, and the waiting create takes the slot it grants. - peer.send({ type: "max_streams_uni", max: 2n }); - await waiting; - expect(session.streamCredit("uni")).toEqual({ available: 0n, blocked: false }); - session.close(); - }); - describe("waitUntilAvailable: false refuses instead of waiting", () => { const refused = (err: unknown) => { expect(err).toBeInstanceOf(DOMException); diff --git a/js/qmux/src/session.ts b/js/qmux/src/session.ts index a732017de73..949c4dfdbf6 100644 --- a/js/qmux/src/session.ts +++ b/js/qmux/src/session.ts @@ -358,15 +358,6 @@ class IncomingStreamQueue { } } -/** The streams a peer still lets this side open in one direction. */ -export interface StreamCredit { - /** The streams this side may still open before the peer grants more. */ - available: bigint; - /** Whether an open was refused for lack of credit since the peer last granted more, so - * this side waits for the peer, as QUIC's STREAMS_BLOCKED says. */ - blocked: boolean; -} - /** What a send stream has sent so far, the shape of W3C WebTransport's `WebTransportSendStreamStats`. */ export interface SendStreamStats { /** The bytes written to the stream. */ @@ -1894,15 +1885,6 @@ export default class Session implements WebTransport { }); } - /** The streams the peer still lets this side open in `dir`, as last heard. - * - * A WebTransport extension: a browser's WebTransport reports no stream credit, so callers - * holding either check for this method. */ - streamCredit(dir: "uni" | "bi"): StreamCredit { - const credit = dir === "bi" ? this.#bidiStreamCredit : this.#uniStreamCredit; - return { available: credit.available, blocked: credit.blocked }; - } - /** Open a bidirectional stream, waiting for the peer to grant stream credit * unless `waitUntilAvailable` is false. */ async createBidirectionalStream(options?: SendStreamOptions): Promise { diff --git a/js/ui/README.md b/js/ui/README.md index 3dc1ad74ebd..6fff1eddaea 100644 --- a/js/ui/README.md +++ b/js/ui/README.md @@ -10,8 +10,7 @@ Presentation components shared by the [@moq/watch](../watch) and [@moq/publish](../publish) overlays, built on [@moq/signals](../signals). You only need it directly to build a custom overlay in the same style. -- `controlButton`, `settingsButton`, `fullscreenButton`, `icon`: overlay control buttons and SVG glyphs. -- `createFullscreen`: cross-browser fullscreen with an iPhone pseudo-fullscreen fallback, its state a signal. +- `controlButton`, `settingsButton`, `fullscreenButton`, `icon`: overlay control buttons and SVG glyphs. `fullscreenButton` toggles cross-browser fullscreen, falling back to an iPhone pseudo-fullscreen. - `statCard`, `statLine`, `graph`: stats panel cards, key/value rows, and a rolling sparkline. - `formatBitrate`, `formatMillis`, `formatHz`, `formatFps`: human-readable stats values. - `Support`: the base of `` and ``, a banner and details table for a browser support report. diff --git a/js/ui/api/index.api.md b/js/ui/api/index.api.md index 45fe0925c41..99b096a59bf 100644 --- a/js/ui/api/index.api.md +++ b/js/ui/api/index.api.md @@ -2,14 +2,8 @@ ```ts import { Effect } from "@moq/signals"; -import { Getter } from "@moq/signals"; import { Signal } from "@moq/signals"; -export interface Fullscreen { - active: Getter; - toggle(): void; -} - export interface Graph { el: HTMLElement; push(value: number | undefined): void; @@ -18,8 +12,6 @@ export interface Graph { export interface GraphOptions { color?: string; format?: (v: number) => string; - max?: number; - samples?: number; } export type Level = "full" | "partial" | "none"; @@ -59,8 +51,6 @@ export abstract class Support extends HTMLElement { export function controlButton(svg: string, label: string): HTMLButtonElement; -export function createFullscreen(parent: Effect, player: HTMLElement): Fullscreen; - export function formatBitrate(bps: number): string; export function formatFps(fps: number): string; diff --git a/js/ui/src/button.ts b/js/ui/src/button.ts index 5f46415bd97..f7dc8c8c77b 100644 --- a/js/ui/src/button.ts +++ b/js/ui/src/button.ts @@ -44,7 +44,7 @@ export function settingsButton(parent: Effect, panel: Signal): HTMLButt return button; } -/** Fullscreen toggle button for `player`, wired to {@link createFullscreen}. */ +/** Fullscreen toggle button for `player`, falling back to a pseudo-fullscreen class on iPhone. */ export function fullscreenButton(parent: Effect, player: HTMLElement): HTMLButtonElement { const button = controlButton(fullscreenEnter, "Fullscreen"); const fullscreen = createFullscreen(parent, player); diff --git a/js/ui/src/graph.ts b/js/ui/src/graph.ts index 00759c8bee1..4799749a690 100644 --- a/js/ui/src/graph.ts +++ b/js/ui/src/graph.ts @@ -2,10 +2,6 @@ import type { Effect } from "@moq/signals"; /** Options for {@link graph}. */ export interface GraphOptions { - /** Fixed y-axis maximum. If omitted, the graph autoscales to its rolling peak. */ - max?: number; - /** How many samples of history to retain (older samples scroll off the left). */ - samples?: number; /** Stroke/fill color for the line (any CSS color). */ color?: string; /** Formats the latest value for the readout in the corner. */ @@ -20,7 +16,8 @@ export interface Graph { push(value: number | undefined): void; } -const DEFAULT_SAMPLES = 120; +// Samples of history retained; older ones scroll off the left. +const CAPACITY = 120; /** Normalize any CSS color and apply an alpha, so the gradient works for named/rgb/hsl inputs too. */ function withAlpha(color: string, alpha: number): string { @@ -47,10 +44,6 @@ export function graph(parent: Effect, title: string, opts?: GraphOptions): Graph // Precompute the gradient stops once (the color is fixed) instead of per frame. const fillTop = withAlpha(color, 0.33); const fillBottom = withAlpha(color, 0); - // Clamp to a sane positive integer so a bad `samples` can't wedge the trim loop. - const capacity = Number.isFinite(opts?.samples) - ? Math.max(1, Math.floor(opts?.samples as number)) - : DEFAULT_SAMPLES; const el = document.createElement("div"); el.className = "graph"; @@ -72,7 +65,7 @@ export function graph(parent: Effect, title: string, opts?: GraphOptions): Graph const samples: number[] = []; // Smoothed autoscale ceiling so the baseline doesn't jump every frame. - let scale = opts?.max ?? 1; + let scale = 1; const draw = () => { const ctx = canvas.getContext("2d"); @@ -92,19 +85,19 @@ export function graph(parent: Effect, title: string, opts?: GraphOptions): Graph ctx.clearRect(0, 0, w, h); if (w > 0 && h > 0) { - // Pick a target ceiling: fixed if given, else the rolling peak with headroom. + // Target the rolling peak with headroom. const peak = samples.length ? Math.max(...samples) : 0; - const target = opts?.max ?? Math.max(1, peak * 1.2); + const target = Math.max(1, peak * 1.2); // Ease toward the target to avoid jitter. scale += (target - scale) * 0.1; const pad = 1; const usable = h - pad * 2; - const x = (i: number) => (capacity <= 1 ? w : (i / (capacity - 1)) * w); + const x = (i: number) => (i / (CAPACITY - 1)) * w; const y = (v: number) => pad + usable - (Math.min(v, scale) / scale) * usable; // Offset so the newest sample sits at the right edge. - const offset = capacity - samples.length; + const offset = CAPACITY - samples.length; if (samples.length > 1) { ctx.beginPath(); @@ -140,7 +133,7 @@ export function graph(parent: Effect, title: string, opts?: GraphOptions): Graph // sparkline) and when the canvas is resized. No idle animation loop. const push = (v: number | undefined) => { samples.push(v !== undefined && Number.isFinite(v) ? Math.max(0, v) : 0); - while (samples.length > capacity) samples.shift(); + while (samples.length > CAPACITY) samples.shift(); value.textContent = v !== undefined && Number.isFinite(v) ? (opts?.format?.(v) ?? v.toFixed(0)) : "-"; draw(); }; diff --git a/js/ui/src/index.ts b/js/ui/src/index.ts index 95aad480bfd..8f94bba6108 100644 --- a/js/ui/src/index.ts +++ b/js/ui/src/index.ts @@ -1,7 +1,7 @@ /** * Presentation components shared by the `@moq/watch` and `@moq/publish` overlays: control - * buttons, a fullscreen controller, stats cards and formatters, a sparkline graph, and the base - * of their browser support banners. + * buttons, stats cards and formatters, a sparkline graph, and the base of their browser support + * banners. * * Browser-only: {@link Support} extends `HTMLElement` when this module loads. * @@ -10,7 +10,6 @@ export { controlButton, fullscreenButton, icon, settingsButton } from "./button"; export { formatBitrate, formatFps, formatHz, formatMillis } from "./format"; -export { createFullscreen, type Fullscreen } from "./fullscreen"; export { type Graph, type GraphOptions, graph } from "./graph"; export { type StatCard, type StatKind, statCard, statLine } from "./stats"; export { type Level, type Report, type Show, type Status, Support } from "./support"; diff --git a/js/watch/api/element.api.md b/js/watch/api/element.api.md index 5e6963169e7..6aa2b0f5559 100644 --- a/js/watch/api/element.api.md +++ b/js/watch/api/element.api.md @@ -28,7 +28,7 @@ export default class extends HTMLElement { get catalogFormat(): CatalogFormat | undefined; set catalogFormat(value: CatalogFormat | undefined); connectedCallback(): void; - connection: Net.Connection; + readonly connection: Net.Connection; readonly controls: { audio: { muted: Signals.Signal; @@ -55,7 +55,7 @@ export default class extends HTMLElement { disconnectedCallback(): void; get echo(): string | undefined; set echo(value: string | undefined); - emitter: Audio.Emitter; + readonly emitter: Audio.Emitter; get follow(): boolean; set follow(value: boolean); get lookahead(): Clock.Milli; @@ -72,8 +72,8 @@ export default class extends HTMLElement { reset(): void; readonly signals: Signals.Effect; readonly sync: Sync; - text: Text.Source; - textRenderer: Text.Renderer; + readonly text: Text.Source; + readonly textRenderer: Text.Renderer; get url(): URL | undefined; set url(value: string | URL | undefined); get versions(): readonly string[] | undefined; diff --git a/js/watch/api/index.api.md b/js/watch/api/index.api.md index 1e999c4b925..4fc9e5c9faf 100644 --- a/js/watch/api/index.api.md +++ b/js/watch/api/index.api.md @@ -27,7 +27,7 @@ export namespace Audio { readonly out: Signals.Readonlys; reset(): void; readonly source: Audio.Source; - static supported: (config: Hang.Catalog.Audio.AudioConfig) => Promise; + static supported: (config: Hang.Catalog.AudioConfig) => Promise; readonly sync: Sync; } export type DecoderInput = { @@ -77,9 +77,9 @@ export namespace Audio { target: Signals.Getter; }; export type SourceOutput = { - available: Signals.Signal>; + available: Signals.Signal>; catalog: Signals.Signal; - config: Signals.Signal; + config: Signals.Signal; error: Signals.Signal; jitter: Signals.Signal; track: Signals.Signal; @@ -96,7 +96,7 @@ export namespace Audio { stalls: number; underruns: number; } - export type Supported = (config: Hang.Catalog.Audio.AudioConfig) => Promise; + export type Supported = (config: Hang.Catalog.AudioConfig) => Promise; export type Target = { name?: string; }; @@ -258,7 +258,7 @@ export namespace Video { readonly in: Signals.Readonlys; readonly out: Signals.Readonlys; readonly source: Video.Source; - static supported: (config: Hang.Catalog.Video.VideoConfig) => Promise; + static supported: (config: Hang.Catalog.VideoConfig) => Promise; readonly sync: Sync; } export type DecoderInput = { @@ -315,10 +315,10 @@ export namespace Video { target: Signals.Getter; }; export type SourceOutput = { - available: Signals.Signal>; + available: Signals.Signal>; catalog: Signals.Signal; ceiling: Signals.Signal; - config: Signals.Signal; + config: Signals.Signal; error: Signals.Signal; track: Signals.Signal; }; @@ -334,7 +334,7 @@ export namespace Video { stalled: Clock.Milli; stalls: number; } - export type Supported = (config: Hang.Catalog.Video.VideoConfig) => Promise; + export type Supported = (config: Hang.Catalog.VideoConfig) => Promise; export type Target = { bitrate?: number; height?: number; diff --git a/js/watch/src/element.ts b/js/watch/src/element.ts index 2726cf207bf..08caabb94e9 100644 --- a/js/watch/src/element.ts +++ b/js/watch/src/element.ts @@ -145,7 +145,7 @@ export default class MoqWatch extends HTMLElement { * The relay connection, shared with every other element on the page pointing at the * same URL. Its `origin` is where the broadcasts live. */ - connection: Moq.Connection; + readonly connection: Moq.Connection; /** Headless playback pipeline behind this element. */ readonly player: Player; @@ -162,13 +162,13 @@ export default class MoqWatch extends HTMLElement { readonly renderer: Video.Renderer; /** Plays decoded samples through the speakers. */ - emitter: Audio.Emitter; + readonly emitter: Audio.Emitter; /** Selects the caption track. `text.out.available` lists the renditions for a picker. */ - text: Text.Source; + readonly text: Text.Source; /** Renders the selected text cues into an overlay above the canvas. */ - textRenderer: Text.Renderer; + readonly textRenderer: Text.Renderer; /** Keeps audio and video playing at the configured delay. */ readonly sync: Sync; diff --git a/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/Aliases.kt b/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/Aliases.kt index 356134c8b47..c94b1be8cbb 100644 --- a/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/Aliases.kt +++ b/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/Aliases.kt @@ -116,7 +116,7 @@ typealias BroadcastRequests = uniffi.moq.MoqBroadcastRequests /** The write side of a raw track: append groups of frames. */ typealias TrackProducer = uniffi.moq.MoqTrackProducer -/** A subscriber-requested track not yet accepted: accept it for a [TrackProducer] or abort it. */ +/** A subscriber-requested track not yet accepted: accept it for a [TrackProducer] or reject it. */ typealias TrackRequest = uniffi.moq.MoqTrackRequest /** A stream of the fills readers of one track need: runs of groups the cache lacks. */ diff --git a/py/moq-rs/README.md b/py/moq-rs/README.md index d7bad95e470..14c62fbff2d 100644 --- a/py/moq-rs/README.md +++ b/py/moq-rs/README.md @@ -174,7 +174,7 @@ client = moq.Client( - `moq.audio.Producer(media.CatalogProducer(broadcast), input, output, *, track=None, bandwidth=None)`. Encode raw PCM frames. A zero frame duration uses the codec's own frame. - `.close()` ends the broadcast for good; a second call is a no-op. - **`BroadcastRequests`**. Async source of tracks requested by subscribers. - - `await .requested_track() → TrackRequest`. Call `.accept()` on it for a `TrackProducer`, or `.abort(code)` to reject. + - `await .requested_track() → TrackRequest`. Call `.accept()` on it for a `TrackProducer`, or `.reject(code)` to fail the subscriber. - Async iterator yielding `TrackRequest` - **`TrackProducer` / `GroupProducer`**. Write raw payloads with no codec parsing. - `.write_frame(payload, timestamp=None)` writes a payload with optional capture time. diff --git a/py/moq-rs/docs/index.md b/py/moq-rs/docs/index.md index e3263960e4b..d51a05244f5 100644 --- a/py/moq-rs/docs/index.md +++ b/py/moq-rs/docs/index.md @@ -154,6 +154,7 @@ Every importer takes a broadcast; single-track imports choose a `Named` or `Requ TrackStreamProducer ContainerProducer ContainerInit + VideoInit Timebase TrackConsumer GroupConsumer diff --git a/py/moq-rs/moq/datagram.py b/py/moq-rs/moq/datagram.py index d1d66675019..884ce4364ee 100644 --- a/py/moq-rs/moq/datagram.py +++ b/py/moq-rs/moq/datagram.py @@ -108,9 +108,9 @@ def accept(self, info: DatagramInfo | None = None) -> DatagramProducer: """Accept the request with its fixed publisher properties.""" return DatagramProducer(self._inner.accept(info)) - def abort(self, error_code: int) -> None: + def reject(self, error_code: int) -> None: """Reject this request with an application error code.""" - self._inner.abort(error_code) + self._inner.reject(error_code) class DatagramRequests(_Stream[DatagramRequest]): diff --git a/py/moq-rs/moq/json.py b/py/moq-rs/moq/json.py index eab20bfbca4..62a242db139 100644 --- a/py/moq-rs/moq/json.py +++ b/py/moq-rs/moq/json.py @@ -48,10 +48,10 @@ "Timed", "WindowConsumer", "WindowEvent", - "WindowPop", + "WindowEventPop", + "WindowEventPush", + "WindowEventSkip", "WindowProducer", - "WindowPush", - "WindowSkip", ] @@ -223,7 +223,7 @@ def finish(self) -> None: @dataclass(frozen=True) -class WindowPush: +class WindowEventPush: """A record joined the window at absolute ``index``.""" index: int @@ -231,14 +231,14 @@ class WindowPush: @dataclass(frozen=True) -class WindowPop: +class WindowEventPop: """The records at these absolute indices left the window.""" range: range @dataclass(frozen=True) -class WindowSkip: +class WindowEventSkip: """The records at these absolute indices will never be delivered. They were pushed and dropped while this reader was behind, or fell outside the producer's @@ -248,7 +248,7 @@ class WindowSkip: range: range -WindowEvent = WindowPush | WindowPop | WindowSkip +WindowEvent = WindowEventPush | WindowEventPop | WindowEventSkip """One change to a window, as a :class:`WindowConsumer` sees it.""" @@ -258,11 +258,11 @@ def _window_event(timed: MoqJsonWindowTimed | None) -> Timed | None: event = timed.event value: WindowEvent if isinstance(event, MoqJsonWindowEvent.PUSH): - value = WindowPush(index=event.index, value=json.loads(event.value)) + value = WindowEventPush(index=event.index, value=json.loads(event.value)) elif isinstance(event, MoqJsonWindowEvent.POP): - value = WindowPop(range(event.start, event.end)) + value = WindowEventPop(range(event.start, event.end)) elif isinstance(event, MoqJsonWindowEvent.SKIP): - value = WindowSkip(range(event.start, event.end)) + value = WindowEventSkip(range(event.start, event.end)) else: raise TypeError(f"unknown window event: {event!r}") return Timed(value=value, timestamp=_opt_from_ts(timed.timestamp)) @@ -271,11 +271,11 @@ def _window_event(timed: MoqJsonWindowTimed | None) -> Timed | None: class WindowConsumer(_Stream[Timed]): """Async iterator over a JSON window track, yielding each change in order. - Each item is a :class:`Timed` whose ``value`` is a :class:`WindowPush`, :class:`WindowPop`, or - :class:`WindowSkip`. Group rolls never surface: a restated record this reader already has - yields nothing. A record first learned of from a restatement (after a join, or while behind) - carries that frame's timestamp, not its original push time. Usable as an async context manager - that cancels on exit. + Each item is a :class:`Timed` whose ``value`` is a :class:`WindowEventPush`, + :class:`WindowEventPop`, or :class:`WindowEventSkip`. Group rolls never surface: a restated + record this reader already has yields nothing. A record first learned of from a restatement + (after a join, or while behind) carries that frame's timestamp, not its original push time. + Usable as an async context manager that cancels on exit. """ def __init__(self, track: subscribe.TrackConsumer) -> None: diff --git a/py/moq-rs/moq/media.py b/py/moq-rs/moq/media.py index cd81c49955f..2986670165a 100644 --- a/py/moq-rs/moq/media.py +++ b/py/moq-rs/moq/media.py @@ -2,7 +2,6 @@ from __future__ import annotations -import copy import json from dataclasses import dataclass, field from typing import Any, cast @@ -39,15 +38,13 @@ MoqMediaTrackProducer, MoqMediaTrackStreamProducer, MoqRendition, + MoqVideoInit, ) from moq_ffi import MoqDecodeStart as DecodeStart from moq_ffi import MoqDimensions as Dimensions from moq_ffi import MoqVideo as Video from moq_ffi import MoqVideoFormat as VideoFormat from moq_ffi import MoqVideoHint as VideoHint -from moq_ffi import ( - MoqVideoInit as VideoInit, -) from moq_ffi import ( MoqVideoProperties as VideoProperties, ) @@ -82,6 +79,30 @@ def _ffi(self) -> MoqContainerInit: ) +@dataclass(frozen=True) +class VideoInit: + """A video codec, its initialization bytes, a label, and hints. + + ``data`` may be empty for a format that resolves in band. ``lower`` is the next lower + rendition of the same source, of either producer kind: this track then sits one priority + below it, so a bandwidth allocator funds that one first. + """ + + format: VideoFormat + data: bytes + label: str | None = None + hint: VideoHint | None = None + lower: TrackProducer | TrackStreamProducer | None = None + + def _ffi(self) -> MoqVideoInit: + lower: MoqMediaProducer | None = None + if isinstance(self.lower, TrackProducer): + lower = cast(MoqMediaProducer, MoqMediaProducer.TRACK(producer=self.lower._inner)) + elif isinstance(self.lower, TrackStreamProducer): + lower = cast(MoqMediaProducer, MoqMediaProducer.STREAM(producer=self.lower._inner)) + return MoqVideoInit(format=self.format, data=self.data, label=self.label, hint=self.hint, lower=lower) + + @dataclass(frozen=True) class Named: """Publish a new track with a chosen name, or a format-derived unique name.""" @@ -168,21 +189,9 @@ def audio(cls, broadcast: BroadcastProducer, init: AudioInit, *, target: Target return cls(MoqMediaTrackProducer.audio(broadcast._inner, target._ffi(), init)) @classmethod - def video( - cls, - broadcast: BroadcastProducer, - init: VideoInit, - *, - target: Target = Named(), - lower: TrackProducer | TrackStreamProducer | None = None, - ) -> TrackProducer: - """Import complete encoded video frames on a named or requested track. - - Pass ``lower``, the next lower rendition of the same source and of either - producer kind, to publish this track one priority below it, so a bandwidth - allocator funds that one first. - """ - return cls(MoqMediaTrackProducer.video(broadcast._inner, target._ffi(), _lower(init, lower))) + def video(cls, broadcast: BroadcastProducer, init: VideoInit, *, target: Target = Named()) -> TrackProducer: + """Import complete encoded video frames on a named or requested track.""" + return cls(MoqMediaTrackProducer.video(broadcast._inner, target._ffi(), init._ffi())) def __init__(self, inner: MoqMediaTrackProducer) -> None: self._inner = inner @@ -275,19 +284,9 @@ class TrackStreamProducer: """ @classmethod - def video( - cls, - broadcast: BroadcastProducer, - init: VideoInit, - *, - target: Target = Named(), - lower: TrackProducer | TrackStreamProducer | None = None, - ) -> TrackStreamProducer: - """Import a video byte stream, inferring frame boundaries. - - ``lower`` is as for :meth:`TrackProducer.video`. - """ - return cls(MoqMediaTrackStreamProducer.video(broadcast._inner, target._ffi(), _lower(init, lower))) + def video(cls, broadcast: BroadcastProducer, init: VideoInit, *, target: Target = Named()) -> TrackStreamProducer: + """Import a video byte stream, inferring frame boundaries.""" + return cls(MoqMediaTrackStreamProducer.video(broadcast._inner, target._ffi(), init._ffi())) def __init__(self, inner: MoqMediaTrackStreamProducer) -> None: self._inner = inner @@ -315,17 +314,6 @@ def finish(self) -> None: self._inner.finish() -def _lower(init: VideoInit, lower: TrackProducer | TrackStreamProducer | None) -> VideoInit: - if lower is None: - return init - init = copy.copy(init) - if isinstance(lower, TrackProducer): - init.lower = cast(MoqMediaProducer, MoqMediaProducer.TRACK(producer=lower._inner)) - else: - init.lower = cast(MoqMediaProducer, MoqMediaProducer.STREAM(producer=lower._inner)) - return init - - def _rendition(id: str, rendition: Video | Audio) -> MoqRendition: if isinstance(rendition, Video): return cast(MoqRendition, MoqRendition.VIDEO(id=id, config=rendition)) diff --git a/py/moq-rs/moq/publish.py b/py/moq-rs/moq/publish.py index 09c3c1b3dda..e184ff2e2f8 100644 --- a/py/moq-rs/moq/publish.py +++ b/py/moq-rs/moq/publish.py @@ -181,7 +181,7 @@ class TrackRequest: Accept it for raw writes, hand it to :class:`~moq.media.Requested` for :meth:`~moq.media.TrackProducer.audio` or :meth:`~moq.media.TrackProducer.video` to publish media (the importer - accepts it), or abort it to reject the subscriber. + accepts it), or reject it to fail the subscriber. """ def __init__(self, inner: MoqTrackRequest) -> None: @@ -207,9 +207,9 @@ def requests(self) -> TrackRequests: """Create a fetch handler before accepting this requested track.""" return TrackRequests(self._inner.requests()) - def abort(self, error_code: int) -> None: + def reject(self, error_code: int) -> None: """Reject the request with an application error code.""" - self._inner.abort(error_code) + self._inner.reject(error_code) class Fill: @@ -228,19 +228,14 @@ def __init__(self, inner: MoqFill) -> None: self._inner = inner @property - def start_group(self) -> int: - """The first group to fill.""" - return self._inner.start_group() - - @property - def start_frame(self) -> int: - """The frame within the first group to start from.""" - return self._inner.start_frame() + def start(self) -> Position: + """The first group to fill, and the frame within it to start from.""" + return self._inner.start() @property - def end_group(self) -> int: + def end(self) -> int: """The exclusive end group.""" - return self._inner.end_group() + return self._inner.end() @property def priority(self) -> int: @@ -277,7 +272,7 @@ async def wanted_changed(self) -> int | None: def accept(self, sequence: int) -> GroupProducer: """Insert group ``sequence`` and return a producer for it. - The first group starts at :attr:`start_frame`, so its frames keep their indices. + The first group starts at :attr:`start`'s frame, so its frames keep their indices. """ return GroupProducer(self._inner.accept(sequence)) diff --git a/py/moq-rs/tests/test_local.py b/py/moq-rs/tests/test_local.py index 7448464549f..fbe1b655e77 100644 --- a/py/moq-rs/tests/test_local.py +++ b/py/moq-rs/tests/test_local.py @@ -573,7 +573,7 @@ async def test_fetch_group_and_serve_dynamic_miss(): dynamic = track.requests() pending = asyncio.create_task(consumer.fetch_group("events", 7, moq.FetchGroupOptions(priority=11))) fill = await asyncio.wait_for(dynamic.requested_fill(), timeout=5.0) - assert (fill.start_group, fill.start_frame, fill.end_group) == (7, 0, 8) + assert (fill.start, fill.end) == (moq.Position(group=7, frame=0), 8) assert fill.priority == 11 assert fill.wanted() == 7 assert fill.is_wanted(7) @@ -635,9 +635,9 @@ async def test_json_window_roundtrip(): for n in range(3): event = await asyncio.wait_for(anext(consumer), timeout=5.0) - assert event == moq.json.Timed(moq.json.WindowPush(n, {"n": n}), moq.Timestamp(n * 1_000, 1_000_000)) + assert event == moq.json.Timed(moq.json.WindowEventPush(n, {"n": n}), moq.Timestamp(n * 1_000, 1_000_000)) event = await asyncio.wait_for(anext(consumer), timeout=5.0) - assert event == moq.json.Timed(moq.json.WindowPop(range(0, 2)), moq.Timestamp(3_000, 1_000_000)) + assert event == moq.json.Timed(moq.json.WindowEventPop(range(0, 2)), moq.Timestamp(3_000, 1_000_000)) producer.finish() with pytest.raises(moq.Error.Closed): @@ -656,10 +656,10 @@ async def test_json_window_cut_restates_the_window(): producer.pop(1, moq.Timestamp(3, 1_000)) events = [(await asyncio.wait_for(anext(consumer), timeout=5.0)).value for _ in range(4)] assert events == [ - moq.json.WindowPush(0, {"n": 0}), - moq.json.WindowPush(1, {"n": 1}), - moq.json.WindowPush(2, {"n": 2}), - moq.json.WindowPop(range(0, 1)), + moq.json.WindowEventPush(0, {"n": 0}), + moq.json.WindowEventPush(1, {"n": 1}), + moq.json.WindowEventPush(2, {"n": 2}), + moq.json.WindowEventPop(range(0, 1)), ] producer.cut() @@ -675,7 +675,7 @@ async def test_json_window_cut_restates_the_window(): # The restated records were already delivered, so the reader sees only the new one. event = await asyncio.wait_for(anext(consumer), timeout=5.0) - assert event.value == moq.json.WindowPush(3, {"n": 3}) + assert event.value == moq.json.WindowEventPush(3, {"n": 3}) producer.finish() @@ -697,9 +697,9 @@ async def test_json_window_skips_records_outside_the_checkpoint(): consumer = moq.json.WindowConsumer(await broadcast.consume().subscribe_track("log")) events = [(await asyncio.wait_for(anext(consumer), timeout=5.0)).value for _ in range(3)] assert events == [ - moq.json.WindowSkip(range(0, 3)), - moq.json.WindowPush(3, {"n": 3}), - moq.json.WindowPush(4, {"n": 4}), + moq.json.WindowEventSkip(range(0, 3)), + moq.json.WindowEventPush(3, {"n": 3}), + moq.json.WindowEventPush(4, {"n": 4}), ] producer.finish() diff --git a/quest/m1/README.md b/quest/m1/README.md index 85795295ea9..faf4809719a 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -25,7 +25,7 @@ because a downstream iGPU transcode trial needs it now. Held here by open claims: a quest's branch is its path, so a claimed quest cannot move until its PR merges, and an epic moves with its children. These breaking epics and quests stay in m1 for now and are listed in m2's -`Required`, so the cut still waits on them: api-audit, drop-set, rind, and +`Required`, so the cut still waits on them: drop-set, rind, and stream-credit. The [re-cut follow-up](/quest/m2/recut-held.md) moves what remains of them once their claims merge. @@ -39,7 +39,6 @@ moves what remains of them once their claims merge. - [A refused lite SUBSCRIBE_UPDATE ends the subscription](/quest/m1/lite-update-refused.md) - the publisher no longer serves a removed range or reports the wrong start after the model refuses an update - [Cluster peers have one identity each and fresh inputs](/quest/m1/cluster-peer-identity.md) - a duplicate cluster id, a self-dial, or a rotated cluster token fails loud instead of partitioning or looping the mesh - [Media fuzz findings](/quest/m1/fuzz-findings/README.md) - the nightly media fuzz targets run clean: mp4-atom, MKV block, TS step, SPS overflow, and catalog encoder bugs they found are fixed -- [A smaller, safer API than release](/quest/m1/api-audit/README.md) - every surface added since release has a consumer or is gone, overlapping operations are collapsed, names mirror across Rust, JS, and the bindings, and every break says how to migrate - [Routing guide](/quest/m1/routing-guide.md) - concepts and practical outbound federation setup - [A reader on a failed copy continues on the held re-offer](/quest/m1/copy-recover.md) - a live reader keeps a group whose copy fails by continuing on the same track's re-offer at the same frame - [Group errors are cached by kind](/quest/m1/group-error-cache.md) - permanent group errors bind every reader, retryable ones are kept only for a bounded time, and a live SUBSCRIBE_DROP or FETCH answered `Old` no longer caches it for good diff --git a/quest/m1/api-audit/README.md b/quest/m1/api-audit/README.md deleted file mode 100644 index 1faf0a9fb8e..00000000000 --- a/quest/m1/api-audit/README.md +++ /dev/null @@ -1,77 +0,0 @@ -# [M] A smaller, safer API than release - -## Goal - -When `main` is cut into `release`, its public API is net smaller and harder -to misuse than `release`'s. Every surface added since release has a consumer -or is removed or made private. Overlapping operations are collapsed into one. -Names mirror across Rust, JS, and the bindings. Every break says how -to migrate. - -This epic's own work, now that its children have landed: diff `main`'s public -API against `release`'s once more, and make private, remove, or record here -each surface added since the audit below that breaks these rules. - -Non-goals: redesigning independently scoped surfaces -([subscribe ranges](/quest/m2/subscribe-ranges/README.md), -[archives](/doc/concept/archives.md), -[compressed tracks](/doc/concept/compression.md), -and `Sync`'s per-track clock handles), and inherited debt that a later quest -already reshapes. - -## Plan - -Planned 2026-10-22 from an audit of `release` (36f392f492) against `main` -(d44813cb70), 3,725 files changed. Seven read-only audits covered Rust -moq-net, `@moq/net`, the media crates, the bindings, `@moq/watch` and -`@moq/publish`, the native, relay, and transport crates, and the gateway and -misc crates. Consumers were checked in this repository, moq.pro (excluding its -vendored checkout), and moq.dev, the npm consumer of `@moq/watch` and -`@moq/publish`. A prior external audit was folded in; its corrections are -recorded where they matter. The maintainer approved every recommendation -wholesale ("quest-plan anything that you recommend"), so the choices in each -child's Plan are settled. - -Rules every child follows: - -- Removal beats addition. A surface with no consumer in the repository, - moq.pro, or moq.dev goes private or away. A consumer planned in a quest - counts only when that quest is in m1 or m2. -- Inherited debt is fixed here only when it is cheap or sits on a surface - this release breaks anyway. Otherwise it joins the later quest that - reshapes it (the ordered track reader joins rust2's - [exporter](https://github.com/moq-dev/rust2/blob/main/quest/m1/ffi-export.md), - which absorbed its foreign poll quest on 2026-10-25). -- Each child lists its public API and wire impact in its PR, and its Impact - bullet says how to migrate. - -The binary-to-flate fold was followed by flate's removal in the -[compression migration](https://github.com/moq-dev/moq/pull/7046), so moq-flate, `@moq/flate`, and -`moq-mux::binary` have no API left to audit. hang's -`Archive::replay_epoch` went with the root `archive` section in catalog HLS. - -Kept public though only moq-transport uses them (decided 2026-10-23), so an -audit skips them: - -- moq-transport-only error variants (`Error::{InvalidJoiningRequestId, - InvalidRange, Remote, UnsupportedExtension}`, the `DecodeError` auth-token - variants) and the session error codes the lite draft registers with - moq-transport's meaning. Errors are fine; only dead ones go. -- `setup::Extensions::{auth, solicit}` and the relay's - `[listen|connect.extensions]` TOML, as interop knobs documented as - affecting moq-transport only. `epoch` went with moq-transport's epochs in - [moq-dev/moq#7239](https://github.com/moq-dev/moq/pull/7239). - -Rejected in planning: - -- Restoring `track::Demand::{used, unused}`: refresh - decided one `wanted()` signal. -- Removing `Audio.Decoder.arrivals()`: moq.dev has a planned consumer - waiting on this release. Only the video handle went. -- Pulling hang's `Projection`, `Stereo`, and `AudioLayout`: the maintainer - decided 2026-10-20 to land the fields first. -- One terminal verb across bindings: Kotlin's `end` exists only because - `close` collides with `AutoCloseable`, and no single verb fits every - language's reserved names. -- A follow policy enum folding `linger` and `stitch`: one name per meaning - (the export wait is `--follow`) fixes the confusion more cheaply. diff --git a/quest/m2/README.md b/quest/m2/README.md index 8c9d864ec75..0d881aa0e77 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -175,7 +175,6 @@ quests that require them. - [Drop set](/quest/m1/drop-set.md) - the drop set's track and cursor runs are billed to the cache pool, so fragmenting drops costs what the pool holds; eviction folds final runs or ends the cursor with `TooFarBehind` (held in m1 by an open claim) - [Subscribe ranges](/quest/m2/subscribe-ranges/README.md) - a lite-07 SUBSCRIBE asks for past and live ranges with absolute or relative ends, in either order, and replaces FETCH for multi-group reads (single-group FETCH stays); merged ranges never starve a subscriber; relays fill misses by range, including over moq-transport - [Address groups by timestamp](/quest/m2/time-addressing/README.md) - group start timestamps replace sequence numbers in the moq-net API and lite-07 wire, while old subscribers, old publishers, and published JS clients keep working -- [A smaller, safer API than release](/quest/m1/api-audit/README.md) - every surface added since release has a consumer or is gone, overlapping operations are collapsed, names mirror across Rust, JS, and the bindings, and every break says how to migrate (held in m1 by an open claim) - [Cluster routing](/quest/m2/cluster-routing/README.md) - prove shared routing contracts, implement per-origin routes, and connect outbound local clusters - [A job's exact route continues its claim's readers](/quest/m2/announce-takeover.md) - a job's route inherits its claim's instance, replacing the ANNOUNCE_TAKEOVER predecessor design - [A group a subscriber gives up on is always reported](/quest/m2/group-drops.md) - the wire names every abandoned group and the drop signal is public, so readers count losses instead of stalling diff --git a/quest/m2/group-drops.md b/quest/m2/group-drops.md index 17e6c1fa04f..5b159451806 100644 --- a/quest/m2/group-drops.md +++ b/quest/m2/group-drops.md @@ -53,7 +53,7 @@ every run; the 256-stream cell sometimes does. ordered reader and resume keep using it internally. Its first consumer is the receive-windows benchmark. Mirror it in `@moq/net` and the bindings only for a named consumer (for example a player or container reader that skips - gaps), per the [API audit](/quest/m1/api-audit/README.md) rule that new + gaps), per the API audit rule that new surface needs one; name it in the PR or leave the mirror out. - **Order with the [drop set](/quest/m1/drop-set.md).** The drop set replaces `poll_drop`'s `Delivered` argument and the skipped record, and diff --git a/quest/m2/recut-held.md b/quest/m2/recut-held.md index 0ebd072421b..10d76d7f746 100644 --- a/quest/m2/recut-held.md +++ b/quest/m2/recut-held.md @@ -3,8 +3,8 @@ ## Goal The epics that open claims held in place during the 2026-11-05 milestone -re-cut sit where the re-cut's rules put them. What remains of api-audit, -drop-set, rind, and stream-credit moves from m1 to m2; and the +re-cut sit where the re-cut's rules put them. What remains of drop-set, +rind, and stream-credit moves from m1 to m2; and the probe epic moves from m2 to m3. The "held" notes in the m1, m2, and cut gate READMEs are removed, along with each held entry's extra link from m2's or the gate's `Required`. diff --git a/quest/m3/release/npm-first-publish.md b/quest/m3/release/npm-first-publish.md index d82dc4de7bd..71995f5c60c 100644 --- a/quest/m3/release/npm-first-publish.md +++ b/quest/m3/release/npm-first-publish.md @@ -2,12 +2,12 @@ ## Goal -`@moq/clock`, `@moq/libav`, and `@moq/video` exist on npm, each with trusted +`@moq/clock`, `@moq/libav`, `@moq/ui`, and `@moq/video` exist on npm, each with trusted publishing attached to `moq-dev/moq` and the `npm` Environment. The cut's release workflow can then publish their real versions. A maintainer does this by hand. Delete this quest once the checks pass: -- `npm view @moq/` resolves for all three, and the package settings show +- `npm view @moq/` resolves for all four, and the package settings show the `moq-dev/moq` trusted publisher with Environment `npm`. ## Plan @@ -18,7 +18,9 @@ publish, auth, signals, and more) now depends on `@moq/clock`. npm trusted publishing attaches only to an existing package, so without this the cut's publish fails on these names and still publishes dependents that can't install. [moq-dev/moq#6243](https://github.com/moq-dev/moq/pull/6243) raised -it for `@moq/libav` alone. +it for `@moq/libav` alone. `@moq/ui`, which `@moq/watch` and `@moq/publish` +keep external, joined the list on 2026-11-06, when the final API audit found +it still returned 404. Decided 2026-10-22 (maintainer): publish a minimal `0.0.0` placeholder (README only) per name to claim it, then attach the trusted publisher. The diff --git a/quest/m4/README.md b/quest/m4/README.md index bf915752153..db5162e6656 100644 --- a/quest/m4/README.md +++ b/quest/m4/README.md @@ -22,7 +22,6 @@ deleted. - [One Ruff pin and Swift formatting on aarch64-linux](/quest/m4/format-gate-followups.md) - only the dev shell pins Ruff, and aarch64-linux checks Swift formatting once nixpkgs builds `swiftc` there - [Weekly mutation testing on critical modules](/quest/m4/mutants.md) - cargo-mutants runs weekly on moq-auth and moq-net's permission, resume, and expiry code - [P2P](/quest/m4/p2p/README.md) - application-selected peer sessions use shared priorities, routing, and same-epoch handover -- [moq-auth drops the unused peer grant API](/quest/m4/auth-drop-peer-grant.md) - P2P trusts its peers, so `PeerGrant` and its `KeySet` methods go - [Challenge proof for certificate credentials](/quest/m4/cert-challenge.md) - a relay can require a signed nonce, so a certificate credential replayed within its time window admits nothing - [Benchmark sources](/quest/m4/bench-sources.md) - every measuring nightly benchmark feeds the regression table, and none is a smoke run - [HLS and DASH browser playback](/quest/m4/hls-player-test.md) - hls.js and dash.js play, reload, switch, and survive reconfiguration nightly diff --git a/quest/m4/auth-drop-peer-grant.md b/quest/m4/auth-drop-peer-grant.md deleted file mode 100644 index f0b002d7752..00000000000 --- a/quest/m4/auth-drop-peer-grant.md +++ /dev/null @@ -1,20 +0,0 @@ -# [XS] moq-auth drops the unused peer grant API - -## Goal - -`moq-auth` no longer carries the P2P peer grant: `PeerGrant` -(`rs/moq-auth/src/peer.rs`), `KeySet::sign_peer`/`verify_peer`, -`KeySet::to_peer_set`, `PeerGrant::binds`, and their wire type are deleted, -along with the "Peer grants" section of `doc/bin/relay/auth.md` and the crate -docs that mention them. No other crate uses them. - -## Plan - -Planned 2026-11-02 (maintainer, /quest-unblock): P2P trusts its peers, so -relay-issued, key-bound peer grants are dropped until a real use case asks for -per-peer scopes ([Signaling and policy](/quest/m4/p2p/signal.md) records why). -The landed API has no consumer left. git history keeps it if a use case -returns. - -Public API: removes `moq_auth::PeerGrant` and the `KeySet` peer methods -(breaking for moq-auth); note it in its MIGRATE. Wire: none. diff --git a/rs/hang/src/catalog/root.rs b/rs/hang/src/catalog/root.rs index cbac094b9ca..a3328a79a6a 100644 --- a/rs/hang/src/catalog/root.rs +++ b/rs/hang/src/catalog/root.rs @@ -321,7 +321,7 @@ impl Catalog { } /// Copy the media sections into a catalog with no application extension. - pub fn media(&self) -> Catalog<()> { + pub fn without_ext(&self) -> Catalog<()> { Catalog { video: self.video.clone(), audio: self.audio.clone(), diff --git a/rs/moq-auth/src/error.rs b/rs/moq-auth/src/error.rs index ae4c8986228..e9018409647 100644 --- a/rs/moq-auth/src/error.rs +++ b/rs/moq-auth/src/error.rs @@ -140,14 +140,6 @@ pub enum Error { #[error("grant asks to be revalidated at no interval")] ZeroRevalidate, - /// No ES256 or EdDSA key is available to sign or verify a peer grant. - #[error("peer grants need an ES256 or EdDSA key")] - NoPeerKey, - - /// A peer grant's presenter key carries private material. - #[error("a peer grant's presenter key must be public")] - PrivatePresenter, - /// The grant's expiry has passed. #[error("grant has already expired")] GrantExpired, diff --git a/rs/moq-auth/src/key.rs b/rs/moq-auth/src/key.rs index cc6521c8d7f..1c559e049c9 100644 --- a/rs/moq-auth/src/key.rs +++ b/rs/moq-auth/src/key.rs @@ -480,7 +480,7 @@ impl Key { } } - pub(crate) fn to_decoding_key(&self) -> crate::Result<&DecodingKey> { + fn to_decoding_key(&self) -> crate::Result<&DecodingKey> { if let Some(key) = self.decode.get() { return Ok(key); } @@ -544,7 +544,7 @@ impl Key { Ok(self.decode.get_or_init(|| decoding_key)) } - pub(crate) fn to_encoding_key(&self) -> crate::Result<&EncodingKey> { + fn to_encoding_key(&self) -> crate::Result<&EncodingKey> { if let Some(key) = self.encode.get() { return Ok(key); } @@ -625,7 +625,7 @@ impl Key { } /// Refuse a key that cannot verify, or a token whose `kid` header names another key. - pub(crate) fn check_kid(&self, token: &str) -> crate::Result<()> { + fn check_kid(&self, token: &str) -> crate::Result<()> { if !self.operations.contains(&KeyOperation::Verify) { return Err(KeyError::VerifyUnsupported.into()); } @@ -682,7 +682,7 @@ impl Key { self } - pub(crate) fn validate_scope(&self, claims: &Claims) -> crate::Result<()> { + fn validate_scope(&self, claims: &Claims) -> crate::Result<()> { if let Some(scope) = &self.scope { scope.validate()?; if !scope.allows(claims) { diff --git a/rs/moq-auth/src/lib.rs b/rs/moq-auth/src/lib.rs index 8a8d4ba3676..dfe1e23f9ed 100644 --- a/rs/moq-auth/src/lib.rs +++ b/rs/moq-auth/src/lib.rs @@ -12,8 +12,6 @@ //! policy a relay used to: keys, public rules, an mTLS grant, tiers, and limits. //! - [`Claims`], [`Key`], and [`KeySet`]: the JWT a client presents in its query, with //! the keys that sign and verify it. `moq auth generate|sign|verify` is the CLI. -//! - [`PeerGrant`]: the relay's ES256 or EdDSA statement of a session's scope, bound to -//! its presenter key, that another client verifies with [`KeySet::to_peer_set`]. //! //! Grants and claims name paths with [`Pattern`]s from `moq-pattern`, re-exported here: //! `foo` is one broadcast, `foo/**` is a subtree, `**` is everything. Each role may also @@ -35,7 +33,6 @@ mod grant; mod key; mod key_id; mod path; -mod peer; mod rate; mod request; mod set; @@ -61,7 +58,6 @@ pub use grant::*; pub use key::*; pub use key_id::*; pub use moq_pattern::{InvalidPattern, Pattern, Patterns, Segment, Specificity}; -pub use peer::*; pub use rate::*; pub use request::*; pub use set::*; diff --git a/rs/moq-auth/src/peer.rs b/rs/moq-auth/src/peer.rs deleted file mode 100644 index 6b88fdbf8f1..00000000000 --- a/rs/moq-auth/src/peer.rs +++ /dev/null @@ -1,456 +0,0 @@ -//! Peer grants: the relay's short-lived statement of one session's scope, bound to the -//! session's presenter key, that another client verifies with the relay's public keys. -//! -//! A peer grant is not a [`Claims`] token. Each refuses the other's JSON: [`Claims`] -//! rejects the `cnf` claim, and a grant requires it, so presenting a grant as `?jwt=` -//! cannot widen a relay session. Only ES256 and EdDSA keys sign or verify one, so a -//! relay holding only HMAC keys issues none and never has to hand out a secret. - -use crate::error::KeyError; -use crate::{Algorithm, Claims, Jwk, Key, KeyMaterial, KeyOperation, KeySet}; -use jsonwebtoken::Header; -use moq_pattern::Patterns; -use serde::{Deserialize, Serialize}; -use serde_with::serde_as; -use std::sync::Arc; - -/// The algorithms that sign and verify a peer grant. -const PEER_ALGORITHMS: [Algorithm; 2] = [Algorithm::ES256, Algorithm::EdDSA]; - -/// A session's publish and subscribe patterns, bound to its presenter key and signed by the relay. -/// -/// The presenter key is the public half of a key the client minted and announced to the relay. -/// A grant names no routing identity: it authorizes paths and binds a key, nothing more. A -/// receiver checks the relay's signature with [`KeySet::verify_peer`], then that the grant -/// [`binds`](Self::binds) the key the peer's roster entry names and its session proved. -#[derive(Debug, Clone)] -#[non_exhaustive] -pub struct PeerGrant { - /// The root for the publish/subscribe patterns below. - pub root: String, - - /// Patterns the presenter may publish, relative to the root. - pub publish: Patterns, - - /// Patterns the presenter may subscribe to, relative to the root. - pub subscribe: Patterns, - - /// The presenter's public key (`cnf`), which a direct session must prove it holds. - pub presenter: Key, - - /// When the relay issued the grant (`iat`). - pub issued: kio::clock::Wall, - - /// When the grant lapses (`exp`). - pub expires: kio::clock::Wall, -} - -impl PeerGrant { - /// A grant bound to `presenter`, valid from `issued` until `expires`, granting nothing yet. - /// - /// Fails unless `presenter` is a public asymmetric key. - pub fn new(presenter: Key, issued: kio::clock::Wall, expires: kio::clock::Wall) -> crate::Result { - validate_presenter(&presenter)?; - Ok(Self { - root: String::new(), - publish: Patterns::default(), - subscribe: Patterns::default(), - presenter, - issued, - expires, - }) - } - - /// Set the root that the publish/subscribe patterns are relative to. - pub fn with_root(mut self, root: impl Into) -> Self { - self.root = root.into(); - self - } - - /// Set the patterns the presenter may publish. - pub fn with_publish(mut self, patterns: impl IntoIterator) -> Self { - self.publish = patterns.into_iter().collect(); - self - } - - /// Set the patterns the presenter may subscribe to. - pub fn with_subscribe(mut self, patterns: impl IntoIterator) -> Self { - self.subscribe = patterns.into_iter().collect(); - self - } - - /// Whether the grant is bound to `key`: the same algorithm and public key material. - /// - /// `kid`, `key_ops`, and private material are ignored, so a roster entry's public JWK and - /// the presenter's own key pair both match. - pub fn binds(&self, key: &Jwk) -> bool { - self.presenter.algorithm == key.algorithm && same_public(&self.presenter.material, &key.material) - } - - /// The grant as [`Claims`], so a key's scope caps a peer grant as it caps a token. - fn as_claims(&self) -> Claims { - let mut claims = Claims::default().with_root(self.root.clone()); - claims.publish = self.publish.clone(); - claims.subscribe = self.subscribe.clone(); - claims.with_issued(self.issued).with_expires(self.expires) - } - - fn validate(&self) -> crate::Result<()> { - validate_presenter(&self.presenter)?; - self.as_claims().validate() - } -} - -/// Whether two keys share their public half. -fn same_public(a: &KeyMaterial, b: &KeyMaterial) -> bool { - match (a, b) { - ( - KeyMaterial::EC { - curve: ca, - x: xa, - y: ya, - .. - }, - KeyMaterial::EC { - curve: cb, - x: xb, - y: yb, - .. - }, - ) => ca == cb && xa == xb && ya == yb, - (KeyMaterial::OKP { curve: ca, x: xa, .. }, KeyMaterial::OKP { curve: cb, x: xb, .. }) => ca == cb && xa == xb, - (KeyMaterial::RSA { public: a, .. }, KeyMaterial::RSA { public: b, .. }) => a.n == b.n && a.e == b.e, - _ => false, - } -} - -fn validate_presenter(key: &Key) -> crate::Result<()> { - if matches!(key.material, KeyMaterial::OCT { .. }) { - return Err(KeyError::NoPublicKey.into()); - } - if key.is_private() { - return Err(crate::Error::PrivatePresenter); - } - Ok(()) -} - -fn is_peer_key(key: &Key) -> bool { - PEER_ALGORITHMS.contains(&key.algorithm) && !matches!(key.material, KeyMaterial::OCT { .. }) -} - -impl Key { - /// Sign a peer grant with this key, which must be ES256 or EdDSA. - pub fn sign_peer(&self, grant: &PeerGrant) -> crate::Result { - if !is_peer_key(self) { - return Err(crate::Error::NoPeerKey); - } - if !self.operations.contains(&KeyOperation::Sign) { - return Err(KeyError::SignUnsupported.into()); - } - - crate::path::validate(&grant.root)?; - grant.validate()?; - self.validate_scope(&grant.as_claims())?; - - let mut header = Header::new(self.algorithm.into()); - header.kid = self.kid.as_ref().map(|k| k.to_string()); - Ok(jsonwebtoken::encode( - &header, - &wire::PeerGrant::from(grant), - self.to_encoding_key()?, - )?) - } - - /// Verify a peer grant's signature, claims, expiry, and this key's scope. - /// - /// Refused unless this key is ES256 or EdDSA, so an HMAC-signed grant is never one. - /// Binding the presenter key to a session is the caller's: see [`PeerGrant::binds`]. - pub fn verify_peer(&self, token: &str) -> crate::Result { - if !is_peer_key(self) { - return Err(crate::Error::NoPeerKey); - } - self.check_kid(token)?; - - let mut validation = jsonwebtoken::Validation::new(self.algorithm.into()); - validation.required_spec_claims = Default::default(); - validation.validate_exp = false; - validation.validate_nbf = false; - validation.validate_aud = false; - let wire: wire::PeerGrant = jsonwebtoken::decode(token, self.to_decoding_key()?, &validation)?.claims; - let grant = PeerGrant::try_from(wire)?; - grant.validate()?; - self.validate_scope(&grant.as_claims())?; - - if grant.expires <= kio::clock::wall() { - return Err(crate::Error::TokenExpired); - } - Ok(grant) - } -} - -impl KeySet { - /// The public keys a client verifies peer grants with: ES256 and EdDSA keys only. - /// - /// HMAC keys are skipped, never exported, so the result is safe to hand to any client. - pub fn to_peer_set(&self) -> crate::Result { - Ok(KeySet { - keys: self - .keys - .iter() - .filter(|key| is_peer_key(key) && key.operations.contains(&KeyOperation::Verify)) - .map(|key| key.to_public().map(Arc::new)) - .collect::>()?, - }) - } - - /// Sign a peer grant with the first ES256 or EdDSA key that can sign. - /// - /// Fails with [`Error::NoPeerKey`](crate::Error::NoPeerKey) when the set holds none, such as - /// a set of HMAC keys: such a relay issues no peer grants. - pub fn sign_peer(&self, grant: &PeerGrant) -> crate::Result { - let key = self - .keys - .iter() - .find(|key| is_peer_key(key) && key.operations.contains(&KeyOperation::Sign) && key.is_private()) - .ok_or(crate::Error::NoPeerKey)?; - key.sign_peer(grant) - } - - /// Verify a peer grant with the key matching its `kid` header. - pub fn verify_peer(&self, token: &str) -> crate::Result { - self.key(token)?.verify_peer(token) - } -} - -impl TryFrom for PeerGrant { - type Error = crate::Error; - - fn try_from(wire: wire::PeerGrant) -> crate::Result { - Ok(Self { - root: wire.root, - publish: wire.publish, - subscribe: wire.subscribe, - presenter: wire.cnf.jwk.import()?, - issued: wire.iat, - expires: wire.exp, - }) - } -} - -mod wire { - use super::*; - - /// The JWT payload of a [`PeerGrant`](super::PeerGrant). - /// - /// Every field outside this list is refused, as [`Claims`](crate::Claims) does, and `cnf`, - /// `iat`, and `exp` are required, so no relay token parses as a peer grant. - #[serde_as] - #[derive(Serialize, Deserialize)] - #[serde(deny_unknown_fields)] - pub(super) struct PeerGrant { - #[serde(default, skip_serializing_if = "String::is_empty")] - pub root: String, - #[serde(default, skip_serializing_if = "Patterns::is_empty")] - pub publish: Patterns, - #[serde(default, skip_serializing_if = "Patterns::is_empty")] - pub subscribe: Patterns, - /// RFC 7800 confirmation: the presenter's public key. - pub cnf: Confirmation, - #[serde_as(as = "kio::clock::serde::UnixSeconds")] - pub iat: kio::clock::Wall, - #[serde_as(as = "kio::clock::serde::UnixSeconds")] - pub exp: kio::clock::Wall, - } - - #[derive(Serialize, Deserialize)] - #[serde(deny_unknown_fields)] - pub(super) struct Confirmation { - pub jwk: Jwk, - } - - impl From<&super::PeerGrant> for PeerGrant { - fn from(grant: &super::PeerGrant) -> Self { - Self { - root: grant.root.clone(), - publish: grant.publish.clone(), - subscribe: grant.subscribe.clone(), - cnf: Confirmation { - jwk: grant.presenter.export(), - }, - iat: grant.issued, - exp: grant.expires, - } - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use std::time::Duration; - - fn presenter() -> Key { - Key::generate(Algorithm::EdDSA, None).unwrap() - } - - fn grant(presenter: &Key) -> PeerGrant { - let now = kio::clock::wall(); - PeerGrant::new(presenter.to_public().unwrap(), now, now + Duration::from_secs(60)) - .unwrap() - .with_root("room") - .with_publish(["alice/**".parse::().unwrap()]) - .with_subscribe(["**".parse::().unwrap()]) - } - - fn relay(algorithm: Algorithm) -> KeySet { - let kid = crate::KeyId::decode("relay").unwrap(); - KeySet { - keys: vec![Arc::new(Key::generate(algorithm, Some(kid)).unwrap())], - } - } - - #[test] - fn round_trip() { - for algorithm in PEER_ALGORITHMS { - let relay = relay(algorithm); - let presenter = presenter(); - let token = relay.sign_peer(&grant(&presenter)).unwrap(); - - let peers = relay.to_peer_set().unwrap(); - let verified = peers.verify_peer(&token).unwrap(); - assert_eq!(verified.root, "room"); - assert_eq!(verified.publish, grant(&presenter).publish); - assert_eq!(verified.subscribe, grant(&presenter).subscribe); - assert!(verified.binds(&presenter.export())); - assert!(verified.binds(&presenter.to_public().unwrap().export())); - } - } - - #[test] - fn hmac_relay_issues_nothing() { - let relay = relay(Algorithm::HS256); - assert!(matches!( - relay.sign_peer(&grant(&presenter())), - Err(crate::Error::NoPeerKey) - )); - assert!(relay.to_peer_set().unwrap().keys.is_empty()); - - let key = &relay.keys[0]; - assert!(matches!( - key.sign_peer(&grant(&presenter())), - Err(crate::Error::NoPeerKey) - )); - } - - #[test] - fn peer_set_drops_hmac_and_private_material() { - let mut relay = relay(Algorithm::ES256); - relay - .keys - .push(Arc::new(Key::generate(Algorithm::HS256, None).unwrap())); - relay - .keys - .push(Arc::new(Key::generate(Algorithm::RS256, None).unwrap())); - - let peers = relay.to_peer_set().unwrap(); - assert_eq!(peers.keys.len(), 1); - assert_eq!(peers.keys[0].algorithm, Algorithm::ES256); - assert!(!peers.keys[0].is_private()); - assert!(!peers.to_str().unwrap().contains("\"k\"")); - } - - #[test] - fn hmac_signed_grant_is_refused() { - // Forge a grant with an HMAC key that shares the asymmetric key's kid. - let relay = relay(Algorithm::ES256); - let wire = wire::PeerGrant::from(&grant(&presenter())); - let mut header = Header::new(jsonwebtoken::Algorithm::HS256); - header.kid = Some("relay".into()); - let secret = [7u8; 32]; - let token = jsonwebtoken::encode(&header, &wire, &jsonwebtoken::EncodingKey::from_secret(&secret)).unwrap(); - - assert!(relay.to_peer_set().unwrap().verify_peer(&token).is_err()); - - // Even a receiver holding that HMAC key refuses it. - let hmac = Jwk::new( - Algorithm::HS256, - KeyMaterial::OCT { - secret: secret.to_vec(), - }, - ); - let mut hmac = hmac; - hmac.kid = Some(crate::KeyId::decode("relay").unwrap()); - assert!(matches!( - hmac.import().unwrap().verify_peer(&token), - Err(crate::Error::NoPeerKey) - )); - } - - #[test] - fn grant_is_not_claims() { - let relay = relay(Algorithm::ES256); - let token = relay.sign_peer(&grant(&presenter())).unwrap(); - assert!(relay.verify(&token).is_err()); - assert!(relay.to_public_set().unwrap().verify(&token).is_err()); - - // Nor does a relay token pass as a peer grant. - let claims = Claims::default().with_subscribe(["**".parse().unwrap()]); - let token = relay.sign(&claims).unwrap(); - assert!(relay.verify_peer(&token).is_err()); - } - - #[test] - fn expired_grant_is_refused() { - let relay = relay(Algorithm::EdDSA); - let presenter = presenter(); - let now = kio::clock::wall(); - let mut expired = grant(&presenter); - expired.issued = now.checked_sub(Duration::from_secs(120)).unwrap(); - expired.expires = now.checked_sub(Duration::from_secs(60)).unwrap(); - let token = relay.sign_peer(&expired).unwrap(); - assert!(matches!(relay.verify_peer(&token), Err(crate::Error::TokenExpired))); - } - - #[test] - fn other_key_is_not_bound() { - let relay = relay(Algorithm::ES256); - let token = relay.sign_peer(&grant(&presenter())).unwrap(); - let verified = relay.verify_peer(&token).unwrap(); - - assert!(!verified.binds(&presenter().export())); - assert!(!verified.binds(&Key::generate(Algorithm::ES256, None).unwrap().export())); - } - - #[test] - fn presenter_must_be_public_and_asymmetric() { - let now = kio::clock::wall(); - let later = now + Duration::from_secs(60); - assert!(PeerGrant::new(presenter(), now, later).is_err()); - assert!(PeerGrant::new(Key::generate(Algorithm::HS256, None).unwrap(), now, later).is_err()); - } - - #[test] - fn scope_caps_a_grant() { - let scope = crate::Scope { - root: "room".into(), - publish: Patterns::default(), - subscribe: ["**".parse::().unwrap()].into_iter().collect(), - }; - let key = Key::generate(Algorithm::ES256, None) - .unwrap() - .with_scope(scope) - .unwrap(); - assert!(matches!( - key.sign_peer(&grant(&presenter())), - Err(crate::Error::ScopeExceeded) - )); - } - - #[test] - fn useless_grant_is_refused() { - let relay = relay(Algorithm::ES256); - let now = kio::clock::wall(); - let empty = PeerGrant::new(presenter().to_public().unwrap(), now, now + Duration::from_secs(60)).unwrap(); - assert!(relay.sign_peer(&empty).is_err()); - } -} diff --git a/rs/moq-c/src/consume.rs b/rs/moq-c/src/consume.rs index 48b320473cc..dd462013b59 100644 --- a/rs/moq-c/src/consume.rs +++ b/rs/moq-c/src/consume.rs @@ -256,7 +256,7 @@ pub struct Consume { datagram_task: NonZeroSlab, /// Buffered datagrams ready for consumption. - datagram: NonZeroSlab>, + datagram: NonZeroSlab>, /// JSON consumer tasks (snapshot, stream, and window share this slab). json_task: NonZeroSlab, diff --git a/rs/moq-c/src/datagram.rs b/rs/moq-c/src/datagram.rs index 4f45baf7163..953610641b5 100644 --- a/rs/moq-c/src/datagram.rs +++ b/rs/moq-c/src/datagram.rs @@ -64,7 +64,7 @@ pub unsafe extern "C" fn moq_publish_datagrams_insert(producer: u32, datagram: * let timestamp = unsafe { ffi::parse_timestamp(datagram.timestamp)? }; State::lock().publish.datagrams_insert( producer, - moq_net::Datagram { + moq_net::datagram::Datagram { sequence: datagram.sequence, timestamp, payload: bytes::Bytes::copy_from_slice(payload), diff --git a/rs/moq-c/src/publish.rs b/rs/moq-c/src/publish.rs index 6d5b77b3f25..1f5b3d7131a 100644 --- a/rs/moq-c/src/publish.rs +++ b/rs/moq-c/src/publish.rs @@ -1729,7 +1729,7 @@ impl Publish { .append(timestamp, bytes::Bytes::copy_from_slice(payload))?) } - pub fn datagrams_insert(&mut self, track: Id, datagram: moq_net::Datagram) -> Result<(), Error> { + pub fn datagrams_insert(&mut self, track: Id, datagram: moq_net::datagram::Datagram) -> Result<(), Error> { Ok(self .datagrams .get_mut(track) diff --git a/rs/moq-cli/src/args.rs b/rs/moq-cli/src/args.rs index b366ff3357c..3f79060000d 100644 --- a/rs/moq-cli/src/args.rs +++ b/rs/moq-cli/src/args.rs @@ -891,9 +891,7 @@ impl ImportSource { pub fn stdin_format(&self) -> Option { Some(match self { Self::Avc3 => PublishFormat::Avc3, - Self::Fmp4(args) => PublishFormat::Fmp4 { - passthrough: args.passthrough, - }, + Self::Fmp4(args) => PublishFormat::Fmp4 { cmaf: args.cmaf }, Self::Ts(args) if args.passthrough => PublishFormat::TsPassthrough { pcr_pid: args.pcr_pid.map(|pid| pid.0), }, @@ -951,8 +949,8 @@ impl ImportSource { pub struct Fmp4Import { /// Publish each fragment whole as CMAF instead of splitting it into one frame per sample. /// Keeps the source's exact sample timing, but every player has to parse ISO BMFF. - #[usage(long = "cmaf")] - pub passthrough: bool, + #[usage(long)] + pub cmaf: bool, } /// The MPEG-TS stdin container: which programs of a multiplex to publish. @@ -1591,8 +1589,8 @@ mod tests { panic!("expected import") }; match &import.source { - ImportSource::Fmp4(args) => assert!(args.passthrough), - ImportSource::Hls(args) => assert!(args.passthrough), + ImportSource::Fmp4(args) => assert!(args.cmaf), + ImportSource::Hls(args) => assert!(args.cmaf), _ => panic!("expected fragment input"), } } diff --git a/rs/moq-cli/src/auth.rs b/rs/moq-cli/src/auth.rs index 04d20ee223b..7ce136a8097 100644 --- a/rs/moq-cli/src/auth.rs +++ b/rs/moq-cli/src/auth.rs @@ -409,13 +409,6 @@ pub struct Serve { #[serde(default)] mtls: MtlsRules, - #[usage(long = "mtls-peer", hide = true, default_missing = "true", num_args = 0..=1, require_equals = true)] - #[serde(skip)] - mtls_peer: Option, - #[usage(long = "mtls-upstream", hide = true, default_missing = "true", num_args = 0..=1, require_equals = true)] - #[serde(skip)] - mtls_upstream: Option, - /// The tier label stamped on every grant, handed to the relay's stats: `/`-joined /// segments of ASCII letters, digits, `.`, `_`, and `-`. #[usage(long, setting = "tier")] @@ -660,9 +653,6 @@ impl Serve { /// A mesh setting without a mesh URL is refused here rather than by the parser, so /// the URL can come from the file and the rest from the command line. fn merged(&self, cli: &usage_rs::config::CliLayer, env: &usage_rs::config::EnvLayer) -> anyhow::Result { - if self.mtls_peer.is_some() || self.mtls_upstream.is_some() { - anyhow::bail!("--mtls-peer and --mtls-upstream are removed; use --mtls-link peer or --mtls-link upstream"); - } let value = self .file .as_ref() @@ -714,9 +704,6 @@ impl Serve { "--limit-token and --limit-remote need --revalidate, which ages out the slots of dead relays" ); } - if self.mtls_peer.is_some() || self.mtls_upstream.is_some() { - anyhow::bail!("--mtls-peer and --mtls-upstream are removed; use --mtls-link peer or --mtls-link upstream"); - } let mtls = &self.mtls; if mtls.link.is_some() && mtls.publish.is_empty() && mtls.subscribe.is_empty() && mtls.certificates.is_empty() { anyhow::bail!("--mtls-link needs --mtls-publish or --mtls-subscribe, or every certificate is refused"); @@ -1392,9 +1379,6 @@ mod tests { (&["--limit-remote", "3", "--expires", "1h"], "need --revalidate"), // A peer marking with nothing granted to certificates marks nobody. (&["--mtls-link", "peer"], "--mtls-link needs"), - (&["--mtls-subscribe", "**", "--mtls-upstream"], "use --mtls-link"), - (&["--mtls-peer=false"], "use --mtls-link"), - (&["--mtls-upstream=false"], "use --mtls-link"), ] { let argv: Vec<&str> = ["moq", "auth", "serve"].iter().chain(args).copied().collect(); let err = serve(&argv).policy().unwrap_err().to_string(); diff --git a/rs/moq-cli/src/hls.rs b/rs/moq-cli/src/hls.rs index 540fd56ff11..fee57d0b649 100644 --- a/rs/moq-cli/src/hls.rs +++ b/rs/moq-cli/src/hls.rs @@ -27,8 +27,8 @@ pub struct ImportArgs { /// Publish each fMP4 fragment whole as CMAF instead of splitting it into one frame per /// sample. Keeps the source's exact sample timing, but every player has to parse ISO BMFF. - #[usage(long = "cmaf")] - pub passthrough: bool, + #[usage(long)] + pub cmaf: bool, } /// HLS export (serve over HTTP, record to a directory) args. @@ -188,7 +188,7 @@ pub async fn import(target: ImportTarget, args: ImportArgs, instance: moq_net::I let mut listed = catalog.consume()?; let mut config = moq_hls::import::Config::new(playlist_url(&args.playlist)?); - config.passthrough = args.passthrough; + config.cmaf = args.cmaf; let mut importer = moq_hls::import::Import::new(route.broadcast().clone(), catalog, config)?; tracing::info!(%name, "importing HLS"); @@ -525,7 +525,7 @@ mod test { }; let args = ImportArgs { playlist: playlist.to_str().unwrap().to_string(), - passthrough: false, + cmaf: false, }; let importing = tokio::spawn(import(target, args, moq_net::Instance::random())); diff --git a/rs/moq-cli/src/main.rs b/rs/moq-cli/src/main.rs index 305c0586ba4..0a8a4d2590b 100644 --- a/rs/moq-cli/src/main.rs +++ b/rs/moq-cli/src/main.rs @@ -903,12 +903,12 @@ fn spawn_export( tasks.spawn(hls::export(origin.consume(), args, name)); } ExportSink::Rtmp(rtmp) => { - let max_delay = rtmp.delay.into(); + let delay = rtmp.delay.into(); if let Some(addr) = rtmp.endpoint.listen { let name = require_broadcast(name, "export rtmp --listen")?; - tasks.spawn(rtmp::listen_export(origin.consume(), addr, name, max_delay)); + tasks.spawn(rtmp::listen_export(origin.consume(), addr, name, delay)); } else if let Some(url) = rtmp.endpoint.connect { - tasks.spawn(rtmp::connect_export(origin.consume(), url, name, max_delay)); + tasks.spawn(rtmp::connect_export(origin.consume(), url, name, delay)); } } ExportSink::Srt(srt) => { diff --git a/rs/moq-cli/src/publish.rs b/rs/moq-cli/src/publish.rs index e80c6fd704b..9ac4a09799d 100644 --- a/rs/moq-cli/src/publish.rs +++ b/rs/moq-cli/src/publish.rs @@ -7,8 +7,8 @@ use moq_mux::container::{flv, fmp4, ts}; pub enum PublishFormat { /// Raw AVC (H.264) Annex B elementary stream. Avc3, - /// Fragmented MP4 (CMAF), republished whole as CMAF when `passthrough`. - Fmp4 { passthrough: bool }, + /// Fragmented MP4 (CMAF), republished whole as CMAF when `cmaf`. + Fmp4 { cmaf: bool }, /// MPEG-TS (transport stream), limited to one `program` of a multiplex when given. Ts { program: Option }, /// MPEG-TS published whole, paced on `pcr_pid` when given rather than the PMT's clock. @@ -427,9 +427,9 @@ impl Publish { import: Box::new(import), } } - PublishFormat::Fmp4 { passthrough } => { + PublishFormat::Fmp4 { cmaf } => { let timebase = catalog.timebase(); - let fmp4 = fmp4::Import::new(broadcast.clone(), timebase.reserve()).with_passthrough(*passthrough); + let fmp4 = fmp4::Import::new(broadcast.clone(), timebase.reserve()).with_cmaf(*cmaf); PublishDecoder::Fmp4(Box::new(fmp4)) } PublishFormat::Ts { .. } => unreachable!("TS is handled above with the mpegts catalog extension"), @@ -2125,8 +2125,8 @@ pub(crate) mod tests { for (format, input) in [ (PublishFormat::Ts { program: None }, TS), (PublishFormat::TsPassthrough { pcr_pid: None }, TS), - (PublishFormat::Fmp4 { passthrough: false }, FMP4), - (PublishFormat::Fmp4 { passthrough: true }, FMP4), + (PublishFormat::Fmp4 { cmaf: false }, FMP4), + (PublishFormat::Fmp4 { cmaf: true }, FMP4), ] { let origin = moq_tokio::origin::spawn(); let consumer = origin.consume(); diff --git a/rs/moq-cli/src/rtmp.rs b/rs/moq-cli/src/rtmp.rs index f7975d7c0d7..38f7fdac36a 100644 --- a/rs/moq-cli/src/rtmp.rs +++ b/rs/moq-cli/src/rtmp.rs @@ -113,7 +113,7 @@ pub async fn listen_export( origin: moq_net::origin::Consumer, addr: SocketAddr, name: String, - max_delay: Duration, + delay: Duration, ) -> anyhow::Result<()> { let name = moq_net::Path::new(&name)?; let mut server = Server::bind(addr).await?; @@ -126,7 +126,7 @@ pub async fn listen_export( let origin = origin.clone(); let name = name.clone(); tokio::spawn(async move { - if let Err(err) = play.with_max_delay(max_delay).accept(&origin, &name).await { + if let Err(err) = play.with_delay(delay).accept(&origin, &name).await { tracing::warn!(%name, %err, "RTMP play ended with error"); } }); @@ -166,7 +166,7 @@ pub async fn connect_export( origin: moq_net::origin::Consumer, url: Url, name: String, - max_delay: Duration, + delay: Duration, ) -> anyhow::Result<()> { let name = moq_net::Path::new(&name)?; let (addr, app, key) = parse_url(&url).await?; @@ -184,7 +184,7 @@ pub async fn connect_export( tracing::info!(%addr, %app, %name, "RTMP client pushing"); notify_ready(); - let client = Client::connect(addr, &app).await?.with_max_delay(max_delay); + let client = Client::connect(addr, &app).await?.with_delay(delay); Ok(client.publish(&key, source).await?) } diff --git a/rs/moq-cli/src/srt.rs b/rs/moq-cli/src/srt.rs index 451a9bb3147..d2217f26e8f 100644 --- a/rs/moq-cli/src/srt.rs +++ b/rs/moq-cli/src/srt.rs @@ -189,7 +189,7 @@ pub async fn connect_import( notify_ready(); let client = moq_srt::Client::new(addr, resource) - .with_max_delay(latency) + .with_latency(latency) .with_max_age(target.max_age) .with_bandwidth(target.bandwidth) .with_program(program) @@ -210,7 +210,7 @@ pub async fn connect_export( notify_ready(); let client = moq_srt::Client::new(addr, resource) - .with_max_delay(args.endpoint.latency.into()) + .with_latency(args.endpoint.latency.into()) .with_follow(args.follow.into()) .with_stitch(args.stitch); Ok(client.publish(&origin, &name).await?) diff --git a/rs/moq-congestion/README.md b/rs/moq-congestion/README.md index 6711872dc3f..66e50371e1f 100644 --- a/rs/moq-congestion/README.md +++ b/rs/moq-congestion/README.md @@ -6,7 +6,7 @@ BBR3's optional delay bound (`bbr3::Config::delay_bound`) reads the peer's recei The controllers take explicit `kio::clock::Instant` values and never drive a runtime. The `Controller` trait names no transport types: `on_sent` returns an opaque per-packet state that the transport stores and hands back with each acknowledgement or loss, and the controller paces its own sends. -Construct controllers with `Factory::build` on an `Arc`, `Arc`, `Arc`, `Arc`, or `Arc`. `Bbr3`, `Cubic`, `Gcc`, `Scream`, `Unlimited`, and `Metrics` live at the crate root. +Construct controllers with `Factory::build` on an `Arc`, `Arc`, `Arc`, `Arc`, or `Arc`. `Unlimited` and `Metrics` live at the crate root, and `gcc::Gcc` and `scream::Scream` in their modules. For callers of the unpublished API, replace `Bbr3Config` with `bbr3::Config`, `CubicConfig` with `cubic::Config`, `ControllerFactory` with `Factory`, and `ControllerMetrics` with `Metrics`. Remove `Controller::into_any` from custom implementations; inspect the controller's behavior or metrics instead of downcasting. `clone_box` still copies controller state for path migration. The test-only `Bbr3::probing_rtt` and `min_rtt` accessors are removed; model internals are tested in this crate. diff --git a/rs/moq-congestion/src/bbr3/bench.rs b/rs/moq-congestion/src/bbr3/bench.rs index a0574740831..8632d8d9e0b 100644 --- a/rs/moq-congestion/src/bbr3/bench.rs +++ b/rs/moq-congestion/src/bbr3/bench.rs @@ -22,7 +22,6 @@ fn measure(flight: usize, batch: usize, steady: bool) -> (Duration, usize) { now: base, mtu: PACKET, rtt: Duration::from_millis(10), - max_pacing_rate: None, seed: 0, }; let mut bbr = Bbr3::new(Arc::new(config), init); diff --git a/rs/moq-congestion/src/bbr3/mod.rs b/rs/moq-congestion/src/bbr3/mod.rs index 66cd018b8c1..374183f8594 100644 --- a/rs/moq-congestion/src/bbr3/mod.rs +++ b/rs/moq-congestion/src/bbr3/mod.rs @@ -206,7 +206,7 @@ enum AckPhase { /// /// #[derive(Debug, Clone)] -pub struct Bbr3 { +pub(crate) struct Bbr3 { /// equivalent to C.SMSS The Sender Maximum Send Size in bytes. /// smss: u64, @@ -586,13 +586,7 @@ impl Bbr3 { cycle_count_stamp: None, cwnd: initial_cwnd, pacing_rate, - pacer: Pacer::new( - init.rtt, - initial_cwnd, - init.mtu, - init.max_pacing_rate, - init.now, - ), + pacer: Pacer::new(init.rtt, initial_cwnd, init.mtu, init.now), // Keep the first flight conservative until an ACK measures the path's RTT. send_quantum: smss, pacing_gain: startup_pacing_gain, @@ -2115,7 +2109,7 @@ impl Controller for Bbr3 { } } -/// Configuration for the `Bbr3` congestion controller +/// Configuration for the BBRv3 congestion controller /// Different pacing_gains can be set to modify the multiplier used to /// increase the sending rates. /// Different cwnd_gains can be set to modify the multiplier used to increase @@ -2219,7 +2213,6 @@ mod test { now: Instant::now(), mtu, rtt: Duration::from_millis(333), - max_pacing_rate: None, seed: 0, }; Self { @@ -2587,7 +2580,6 @@ mod test { now: Instant::now(), mtu: 2500, rtt: Duration::from_millis(333), - max_pacing_rate: None, seed: 17, }; let mut bbr3 = Bbr3::new(Arc::new(Config::default()), init); diff --git a/rs/moq-congestion/src/cubic.rs b/rs/moq-congestion/src/cubic.rs index d478e801354..2adb8b0d9ae 100644 --- a/rs/moq-congestion/src/cubic.rs +++ b/rs/moq-congestion/src/cubic.rs @@ -72,7 +72,7 @@ impl State { /// The RFC8312 congestion controller, as widely used for TCP #[derive(Debug, Clone)] -pub struct Cubic { +pub(crate) struct Cubic { current_mtu: u64, state: State, /// Copy of the controller state to restore when a spurious congestion event is detected. @@ -97,7 +97,7 @@ impl Cubic { current_mtu, pre_congestion_state: None, rtt: init.rtt, - pacer: Pacer::new(init.rtt, window, init.mtu, init.max_pacing_rate, init.now), + pacer: Pacer::new(init.rtt, window, init.mtu, init.now), } } @@ -300,7 +300,7 @@ impl Controller for Cubic { } } -/// Configuration for the `Cubic` congestion controller +/// Configuration for the CUBIC congestion controller #[derive(Debug, Clone)] pub struct Config { initial_window: u64, @@ -341,7 +341,6 @@ mod tests { now, mtu: BASE_DATAGRAM_SIZE as u16, rtt: Duration::from_millis(100), - max_pacing_rate: None, seed: 0, }, ) diff --git a/rs/moq-congestion/src/gcc/bench.rs b/rs/moq-congestion/src/gcc/bench.rs index 00d4d165010..db9a2300893 100644 --- a/rs/moq-congestion/src/gcc/bench.rs +++ b/rs/moq-congestion/src/gcc/bench.rs @@ -15,7 +15,6 @@ fn measure(flight: usize, batch: usize, steady: bool) -> Duration { now: base, mtu: PACKET, rtt: Duration::from_millis(10), - max_pacing_rate: None, seed: 0, }); let bytes = u64::from(PACKET); diff --git a/rs/moq-congestion/src/gcc/mod.rs b/rs/moq-congestion/src/gcc/mod.rs index 56cf463feeb..61e2ea049c1 100644 --- a/rs/moq-congestion/src/gcc/mod.rs +++ b/rs/moq-congestion/src/gcc/mod.rs @@ -114,7 +114,7 @@ const CLUSTER_HISTORY: TimeDelta = TimeDelta::from_seconds(2); /// back at 8% a second. Only the 5 Mbit/s ceiling `ProbeController` keeps without a maximum rate /// hid this: the probe it capped happened to restore the estimate after the cut. const INITIAL_RATE: DataRate = DataRate::from_kilobits_per_sec(1000); -/// The most GoogCC estimates or probes when the transport sets no pacing limit, the 1 Gbit/s +/// The most GoogCC estimates or probes, the 1 Gbit/s /// `SendSideBandwidthEstimation` already defaults to. Without one, `ProbeController` caps every /// probe at 5 Mbit/s, so a probe target above that could never be reached. const MAX_RATE: DataRate = DataRate::from_kilobits_per_sec(1_000_000); @@ -183,9 +183,7 @@ impl Gcc { constraints: TargetRateConstraints { at_time: now, min_data_rate: None, - max_data_rate: Some(init.max_pacing_rate.map_or(MAX_RATE, |rate| { - DataRate::from_bytes_per_sec(rate.min(i64::MAX as u64) as i64) - })), + max_data_rate: Some(MAX_RATE), starting_rate: Some(starting_rate), }, stream_based_config: StreamsConfig { @@ -204,13 +202,7 @@ impl Gcc { let mut gcc = Self { cc, prober: BitrateProber::default(), - pacer: Pacer::new( - init.rtt, - initial_window, - init.mtu, - init.max_pacing_rate, - init.now, - ), + pacer: Pacer::new(init.rtt, initial_window, init.mtu, init.now), mtu: init.mtu, pacing_rate: starting_rate.bytes_per_sec() as u64, target_rate: starting_rate.bytes_per_sec() as u64, diff --git a/rs/moq-congestion/src/gcc/tests.rs b/rs/moq-congestion/src/gcc/tests.rs index f9656acf2b3..29b763f9df9 100644 --- a/rs/moq-congestion/src/gcc/tests.rs +++ b/rs/moq-congestion/src/gcc/tests.rs @@ -64,7 +64,6 @@ impl Sim { now: epoch, mtu: MTU, rtt: Duration::from_millis(333), - max_pacing_rate: None, seed: 0, }); Self { @@ -377,7 +376,6 @@ fn probe_inflight_rounds_up_to_a_packet() { now, mtu: MTU, rtt: Duration::from_millis(100), - max_pacing_rate: None, seed: 0, }); // 3000 bytes, two and a half packets @@ -449,7 +447,6 @@ fn survives_hostile_receive_times() { now, mtu: MTU, rtt: Duration::from_millis(100), - max_pacing_rate: None, seed: 0, }); let times = [ @@ -488,17 +485,16 @@ fn survives_hostile_receive_times() { } } -/// A rate cap that shrinks GoogCC's window below two datagrams of a large MTU leaves the window -/// at two datagrams, which `kMinCwnd`'s 1500-byte packets fall short of. +/// A GoogCC window below two datagrams of the largest MTU leaves the window at two datagrams, +/// which `kMinCwnd`'s 1500-byte packets fall short of. #[test] fn window_floor_tracks_mtu() { - const LARGE: u16 = 16384; + const LARGE: u16 = 65527; let mut now = Instant::ZERO + Duration::from_secs(1000); let mut gcc = Gcc::new(Init { now, mtu: LARGE, rtt: Duration::from_millis(10), - max_pacing_rate: Some(10_000), seed: 0, }); for _ in 0..200 { diff --git a/rs/moq-congestion/src/lib.rs b/rs/moq-congestion/src/lib.rs index d61dabc97d3..2eb84820a90 100644 --- a/rs/moq-congestion/src/lib.rs +++ b/rs/moq-congestion/src/lib.rs @@ -20,11 +20,7 @@ mod rate; pub mod scream; mod unlimited; -pub use bbr3::Bbr3; -pub use cubic::Cubic; -pub use gcc::Gcc; pub use rate::Packet; -pub use scream::Scream; pub use unlimited::Unlimited; /// Common interface for different congestion controllers @@ -266,8 +262,6 @@ pub struct Init { pub mtu: u16, /// The transport's RTT estimate before any sample. pub rtt: Duration, - /// The most bytes per second to pace at, whatever the controller computes. - pub max_pacing_rate: Option, /// Seeds whatever the controller randomizes, such as BBR3's probe phases, so a seeded /// connection replays it. pub seed: u64, @@ -303,7 +297,6 @@ mod tests { now: Instant::ZERO, mtu, rtt: Duration::from_millis(100), - max_pacing_rate: None, seed: 0, } } diff --git a/rs/moq-congestion/src/pacer.rs b/rs/moq-congestion/src/pacer.rs index 771b637cbc9..a8dbb8d0900 100644 --- a/rs/moq-congestion/src/pacer.rs +++ b/rs/moq-congestion/src/pacer.rs @@ -21,14 +21,13 @@ pub(crate) struct Pacer { /// The transport checks a datagram while the previous packet is still being built, /// so one packet can leave beyond the burst budget. debt: u64, - max_bytes_per_second: Option, prev: Instant, } /// Inputs a window-derived [`Pacer::capacity`] was calculated from #[derive(Debug, Copy, Clone, Eq, PartialEq)] struct WindowInputs { - /// Congestion window in bytes, after [`rate_limited_window`] has clamped it + /// Congestion window in bytes window: u64, /// MTU of the path in bytes mtu: u16, @@ -36,21 +35,13 @@ struct WindowInputs { impl Pacer { /// Obtains a new [`Pacer`]. - pub(crate) fn new( - smoothed_rtt: Duration, - window: u64, - mtu: u16, - max_bytes_per_second: Option, - now: Instant, - ) -> Self { - let window = rate_limited_window(smoothed_rtt, window, max_bytes_per_second); + pub(crate) fn new(smoothed_rtt: Duration, window: u64, mtu: u16, now: Instant) -> Self { let capacity = optimal_capacity(smoothed_rtt, window, mtu); Self { capacity, last_window_inputs: Some(WindowInputs { window, mtu }), tokens: capacity, debt: 0, - max_bytes_per_second, prev: now, } } @@ -103,7 +94,6 @@ impl Pacer { return self.delay_at_rate(pacing_rate, bytes_to_send, mtu, now); } - let window = rate_limited_window(smoothed_rtt, window, self.max_bytes_per_second); let inputs = WindowInputs { window, mtu }; if self.last_window_inputs != Some(inputs) { self.capacity = optimal_capacity(smoothed_rtt, window, mtu); @@ -170,14 +160,8 @@ impl Pacer { mtu: u16, now: Instant, ) -> Option { - // An explicit rate is clamped directly. The 1.25 correction in - // `rate_limited_window` exists only to cancel out the legacy refill speedup, which - // this path does not apply. A rate of zero would divide by zero below. - let rate = match self.max_bytes_per_second { - Some(max_bytes_per_second) => Ord::min(pacing_rate, max_bytes_per_second), - None => pacing_rate, - } - .max(1); + // A rate of zero would divide by zero below. + let rate = pacing_rate.max(1); let capacity = rate_capacity(rate, mtu); if capacity != self.capacity { @@ -258,27 +242,6 @@ fn optimal_capacity(smoothed_rtt: Duration, window: u64, mtu: u16) -> u64 { rate_capacity(rate, mtu) } -/// Clamps the window to limit the sending rate to `max_bytes_per_second`. -/// -/// If `max_bytes_per_second` is `None`, the original window is returned. -fn rate_limited_window( - smoothed_rtt: Duration, - window: u64, - max_bytes_per_second: Option, -) -> u64 { - let Some(max_bytes_per_second) = max_bytes_per_second else { - return window; - }; - - let rate_window = max_bytes_per_second as f64 * smoothed_rtt.as_secs_f64(); - - // the pacer refills tokens at x1.25 speed, so we shrink the window to cancel out the speedup - // (otherwise the actual sending rate could be higher than `max_bytes_per_second`) - let adjusted_rate_window = (rate_window / 1.25).round(); - - Ord::min(window, Ord::max(adjusted_rate_window as u64, 1)) -} - /// Period of traffic to batch together on a reasonably fast connection const TARGET_BURST_INTERVAL: Duration = Duration::from_millis(2); @@ -389,7 +352,7 @@ mod tests { let window = 2_000_000; let now = Instant::now(); let metrics = paced_metrics(window, TEST_PACING_RATE); - let mut pacer = Pacer::new(rtt, window, mtu, None, now); + let mut pacer = Pacer::new(rtt, window, mtu, now); // Honouring a finite rate is only possible by delaying, so a sender polling at a // single instant must eventually be told to wait. @@ -407,7 +370,7 @@ mod tests { let window = 2_000_000; let now = Instant::now(); let metrics = paced_metrics(window, TEST_PACING_RATE); - let mut pacer = Pacer::new(rtt, window, mtu, None, now); + let mut pacer = Pacer::new(rtt, window, mtu, now); let (resume, _) = burst_until_blocked(&mut pacer, rtt, mtu, now, metrics) .expect("pacer must block once the burst budget is spent"); @@ -430,7 +393,7 @@ mod tests { // 33 GB/s, as BBR3's Startup derives from a microsecond RTT: three bytes take under a // nanosecond to earn. let metrics = paced_metrics(window, 33_000_000_000); - let mut pacer = Pacer::new(rtt, window, mtu, None, now); + let mut pacer = Pacer::new(rtt, window, mtu, now); pacer.delay(rtt, u64::from(mtu), mtu, now, metrics); pacer.tokens = u64::from(mtu) - 3; @@ -451,7 +414,7 @@ mod tests { let start = Instant::now(); for rate in [1_250, 12_500, 62_500, 125_000] { let metrics = paced_metrics(1_000_000, rate); - let mut pacer = Pacer::new(rtt, 1_000_000, mtu, None, start); + let mut pacer = Pacer::new(rtt, 1_000_000, mtu, start); assert_eq!( pacer.delay(rtt, 2 * u64::from(mtu), mtu, start, metrics), None @@ -482,7 +445,7 @@ mod tests { let start = Instant::now(); for rate in [1_250, 12_500, 62_500, 125_000] { for explicit_rate in [false, true] { - let mut pacer = Pacer::new(rtt, 2_000_000, mtu, None, start); + let mut pacer = Pacer::new(rtt, 2_000_000, mtu, start); burst_until_blocked( &mut pacer, rtt, @@ -534,7 +497,7 @@ mod tests { ] { for batch in [1, 2] { let metrics = paced_metrics(window, rate); - let mut pacer = Pacer::new(rtt, window, mtu, None, start); + let mut pacer = Pacer::new(rtt, window, mtu, start); let sent = bytes_sent_over( &mut pacer, rtt, @@ -567,7 +530,7 @@ mod tests { let rtt = Duration::from_micros(400); assert!( - Pacer::new(rtt, 30000, 1500, None, new_instant) + Pacer::new(rtt, 30000, 1500, new_instant) .delay( Duration::from_micros(0), 0, @@ -578,7 +541,7 @@ mod tests { .is_none() ); assert!( - Pacer::new(rtt, 30000, 1500, None, new_instant) + Pacer::new(rtt, 30000, 1500, new_instant) .delay( Duration::from_micros(0), 1600, @@ -589,7 +552,7 @@ mod tests { .is_none() ); assert!( - Pacer::new(rtt, 30000, 1500, None, new_instant) + Pacer::new(rtt, 30000, 1500, new_instant) .delay( Duration::from_micros(0), 1500, @@ -608,18 +571,18 @@ mod tests { let rtt = Duration::from_millis(50); let now = Instant::now(); - let pacer = Pacer::new(rtt, window, mtu, None, now); + let pacer = Pacer::new(rtt, window, mtu, now); assert_eq!( pacer.capacity, (window as u128 * TARGET_BURST_INTERVAL.as_nanos() / rtt.as_nanos()) as u64 ); assert_eq!(pacer.tokens, pacer.capacity); - let pacer = Pacer::new(Duration::from_millis(0), window, mtu, None, now); + let pacer = Pacer::new(Duration::from_millis(0), window, mtu, now); assert_eq!(pacer.capacity, MAX_BURST_SIZE * mtu as u64); assert_eq!(pacer.tokens, pacer.capacity); - let pacer = Pacer::new(rtt, 1, mtu, None, now); + let pacer = Pacer::new(rtt, 1, mtu, now); assert_eq!(pacer.capacity, mtu as u64); assert_eq!(pacer.tokens, pacer.capacity); } @@ -631,7 +594,7 @@ mod tests { let rtt = Duration::from_millis(50); let now = Instant::now(); - let mut pacer = Pacer::new(rtt, window, mtu, None, now); + let mut pacer = Pacer::new(rtt, window, mtu, now); assert_eq!( pacer.capacity, (window as u128 * TARGET_BURST_INTERVAL.as_nanos() / rtt.as_nanos()) as u64 @@ -670,7 +633,7 @@ mod tests { let rtt = Duration::from_millis(50); let old_instant = Instant::now(); - let mut pacer = Pacer::new(rtt, window, mtu, None, old_instant); + let mut pacer = Pacer::new(rtt, window, mtu, old_instant); let packet_capacity = pacer.capacity / mtu as u64; for _ in 0..packet_capacity { @@ -734,43 +697,6 @@ mod tests { assert_eq!(pacer.tokens, pacer.capacity); } - #[test] - fn computes_pause_correctly_for_rate_limited() { - let window = 2_000_000u64; - let mtu = 1000; - let rtt = Duration::from_millis(50); - let old_instant = Instant::now(); - - let mut pacer = Pacer::new(rtt, window, mtu, Some(2_000), old_instant); - assert_eq!( - pacer.delay(rtt, 1_000, mtu, old_instant, unpaced_metrics(window)), - None, - "When capacity is available packets should be sent immediately" - ); - pacer.on_transmit(mtu); - - let actual_delay = pacer - .delay(rtt, 1_000, mtu, old_instant, unpaced_metrics(window)) - .expect("Send must be delayed") - .duration_since(old_instant); - - let expected_delay = Duration::from_millis(500); - let diff = actual_delay.abs_diff(expected_delay); - - // Allow up to 2ns difference due to rounding - assert!( - diff < Duration::from_nanos(2), - "expected ≈ {expected_delay:?}, got {actual_delay:?} (diff {diff:?})" - ); - - // Should be able to send after a while - let now = old_instant + expected_delay / 2; - assert_eq!( - pacer.delay(rtt, 500, mtu, now, unpaced_metrics(window)), - None - ); - } - #[test] fn derives_burst_budget_from_controller_pacing_rate() { let window = 2_000_000; @@ -780,7 +706,7 @@ mod tests { // A window twice the one the pacer was built with must not disturb a budget the // controller's rate determines. let metrics = paced_metrics(window * 2, TEST_PACING_RATE); - let mut pacer = Pacer::new(rtt, window, mtu, None, now); + let mut pacer = Pacer::new(rtt, window, mtu, now); pacer.delay(rtt, u64::from(mtu), mtu, now, metrics); @@ -799,7 +725,7 @@ mod tests { let rtt = Duration::from_millis(50); let now = Instant::now(); let metrics = paced_metrics(window, RATE); - let mut pacer = Pacer::new(rtt, window, mtu, None, now); + let mut pacer = Pacer::new(rtt, window, mtu, now); let (resume, _) = burst_until_blocked(&mut pacer, rtt, mtu, now, metrics) .expect("pacer must block once the burst budget is spent"); @@ -820,7 +746,7 @@ mod tests { let rtt = Duration::from_millis(50); let now = Instant::now(); let metrics = paced_metrics(window, TEST_PACING_RATE); - let mut pacer = Pacer::new(rtt, window, mtu, None, now); + let mut pacer = Pacer::new(rtt, window, mtu, now); let (_, burst) = burst_until_blocked(&mut pacer, rtt, mtu, now, metrics) .expect("pacer must block once the burst budget is spent"); @@ -847,7 +773,7 @@ mod tests { let now = Instant::now(); let fast = paced_metrics(window, TEST_PACING_RATE); let slow = paced_metrics(window, TEST_PACING_RATE / 10); - let mut pacer = Pacer::new(rtt, window, mtu, None, now); + let mut pacer = Pacer::new(rtt, window, mtu, now); // Earn credit at the high rate, as during a ProbeBW_UP phase... assert_eq!(pacer.delay(rtt, u64::from(mtu), mtu, now, fast), None); @@ -872,7 +798,7 @@ mod tests { let mtu = 1500; let rtt = Duration::from_millis(50); let now = Instant::now(); - let mut pacer = Pacer::new(rtt, window, mtu, None, now); + let mut pacer = Pacer::new(rtt, window, mtu, now); // A controller free to report a rate on some calls and not on others is within what // `Rate` allows. The rate path leaves its own budget behind... @@ -895,40 +821,4 @@ mod tests { "the window path kept a burst budget the rate path derived" ); } - - #[test] - fn max_bytes_per_second_overrides_a_higher_controller_rate() { - const SECONDS: u64 = 1; - /// 1 Mbit/s in bytes/sec, two orders of magnitude under [`TEST_PACING_RATE`]. - const LIMIT: u64 = 125_000; - - let window = 2_000_000; - let mtu = 1500; - let rtt = Duration::from_millis(50); - let start = Instant::now(); - let metrics = paced_metrics(window, TEST_PACING_RATE); - let mut pacer = Pacer::new(rtt, window, mtu, Some(LIMIT), start); - - let sent = bytes_sent_over( - &mut pacer, - rtt, - mtu, - start, - Duration::from_secs(SECONDS), - metrics, - 1, - ); - - // The configured ceiling binds even though the controller asks for far more. - let expected = LIMIT * SECONDS; - let slack = expected / 20; - assert!( - sent <= expected + slack, - "emitted {sent} bytes in {SECONDS}s, over the {expected} byte ceiling" - ); - assert!( - sent + slack >= expected, - "emitted {sent} bytes in {SECONDS}s, underrunning the {expected} byte ceiling" - ); - } } diff --git a/rs/moq-congestion/src/scream.rs b/rs/moq-congestion/src/scream.rs index b42424908d5..41d6ddcd339 100644 --- a/rs/moq-congestion/src/scream.rs +++ b/rs/moq-congestion/src/scream.rs @@ -226,7 +226,6 @@ impl Scream { init.rtt, (MIN_REF_WND as u64).max(2 * u64::from(init.mtu)), init.mtu, - init.max_pacing_rate, init.now, ), config, @@ -592,7 +591,6 @@ mod tests { now, mtu: MTU, rtt: Duration::from_millis(333), - max_pacing_rate: None, seed: 0, }, ) diff --git a/rs/moq-crypto/src/e2ee/datagram.rs b/rs/moq-crypto/src/e2ee/datagram.rs index e62d6d69ad5..2707149443a 100644 --- a/rs/moq-crypto/src/e2ee/datagram.rs +++ b/rs/moq-crypto/src/e2ee/datagram.rs @@ -115,7 +115,7 @@ impl Producer { .expect("track key") .protect(sequence, 0, plaintext, MAX_DATAGRAM_PAYLOAD) })?; - self.inner.insert(moq_net::Datagram { + self.inner.insert(moq_net::datagram::Datagram { sequence, timestamp, payload, diff --git a/rs/moq-crypto/src/e2ee/tests.rs b/rs/moq-crypto/src/e2ee/tests.rs index e93da945948..7632ebecb65 100644 --- a/rs/moq-crypto/src/e2ee/tests.rs +++ b/rs/moq-crypto/src/e2ee/tests.rs @@ -354,6 +354,14 @@ fn net_track(name: &Name) -> moq_net::track::Producer { .unwrap() } +/// A millisecond datagram track named `name`, in a broadcast of its own. +fn datagram_track(name: &str) -> moq_net::datagram::Producer { + moq_net::broadcast::Info::new() + .produce() + .create_datagrams(name, timed_datagrams()) + .unwrap() +} + struct Pair { semantic: String, generation: Consumer, @@ -387,7 +395,7 @@ struct DatagramPair { fn datagram_pair(semantic: &str) -> DatagramPair { let generation = test_generation(); - let net = timed_datagrams().produce(generation.name(semantic).unwrap().as_str()); + let net = datagram_track(generation.name(semantic).unwrap().as_str()); let producer = generation.produce_datagrams(net.clone(), semantic).unwrap(); let consuming = consume(&generation); let consumer = consuming.consume_datagrams(net.consume(), semantic).unwrap(); @@ -404,7 +412,7 @@ impl DatagramPair { /// Send `payload` raw, the way a relay could. fn inject(&mut self, sequence: u64, timestamp: moq_net::Timestamp, payload: impl Into) { self.net - .insert(moq_net::Datagram { + .insert(moq_net::datagram::Datagram { sequence, timestamp: Some(timestamp), payload: payload.into(), @@ -603,10 +611,10 @@ fn one_name_keys_its_stream_and_datagram_tracks_apart() { Poll::Ready("authentication".to_string()) ); - let mut forged = timed_datagrams().produce(name.as_str()); + let mut forged = datagram_track(name.as_str()); let mut reader = consuming.consume_datagrams(forged.consume(), "audio").unwrap(); forged - .insert(moq_net::Datagram { + .insert(moq_net::datagram::Datagram { sequence: 0, timestamp: Some(ms(1)), payload: frame, @@ -642,7 +650,7 @@ fn reproduced_track_resumes_above_its_marks() { // A datagram track of the name has sequences of its own, and resumes above them too. let datagrams = || { generation - .produce_datagrams(timed_datagrams().produce(name.as_str()), "video") + .produce_datagrams(datagram_track(name.as_str()), "video") .unwrap() }; let mut first = datagrams(); diff --git a/rs/moq-ffi/src/datagram.rs b/rs/moq-ffi/src/datagram.rs index 1ef1cbde55d..e4be80f3139 100644 --- a/rs/moq-ffi/src/datagram.rs +++ b/rs/moq-ffi/src/datagram.rs @@ -132,7 +132,7 @@ impl MoqDatagramProducer { /// Insert a datagram at its explicit sequence; later appends continue past the highest one. pub fn insert(&self, datagram: MoqDatagram) -> Result<(), MoqError> { let _guard = crate::ffi::enter(); - let datagram = moq_net::Datagram { + let datagram = moq_net::datagram::Datagram { sequence: datagram.sequence, timestamp: datagram.timestamp.map(TryInto::try_into).transpose()?, payload: datagram.payload.into(), @@ -310,7 +310,7 @@ impl MoqDatagramRequest { } /// Reject this request with an application error code. - pub fn abort(&self, error_code: u16) -> Result<(), MoqError> { + pub fn reject(&self, error_code: u16) -> Result<(), MoqError> { let _guard = crate::ffi::enter(); let request = self.inner.lock().unwrap().take().ok_or(MoqError::Closed)?; request.reject(moq_net::Error::App(error_code)); diff --git a/rs/moq-ffi/src/producer.rs b/rs/moq-ffi/src/producer.rs index cc8d7597f8a..6aecedbf9d5 100644 --- a/rs/moq-ffi/src/producer.rs +++ b/rs/moq-ffi/src/producer.rs @@ -413,7 +413,7 @@ impl MoqBroadcastRequests { /// Returns a [`MoqTrackRequest`]: accept it for raw writes with /// [`MoqTrackRequest::accept`], publish media onto it with /// [`crate::media::MoqMediaTrackProducer::audio`], or reject it with - /// [`MoqTrackRequest::abort`]. The requesting subscriber stays pending until then. + /// [`MoqTrackRequest::reject`]. The requesting subscriber stays pending until then. /// /// Returns an error once the broadcast is closed or aborted. pub async fn requested_track(&self) -> Result, MoqError> { @@ -500,18 +500,13 @@ impl MoqFill { #[uniffi::export] impl MoqFill { - /// The first group to fill. - pub fn start_group(&self) -> u64 { - self.inner.start().group - } - - /// The frame within the first group to start from. - pub fn start_frame(&self) -> u64 { - self.inner.start().frame + /// The first group to fill, and the frame within it to start from. + pub fn start(&self) -> MoqPosition { + self.inner.start().into() } /// The exclusive end group. - pub fn end_group(&self) -> u64 { + pub fn end(&self) -> u64 { self.inner.end() } @@ -550,7 +545,7 @@ impl MoqFill { } /// Insert group `sequence` and return a producer for filling it. The first group starts - /// at [`start_frame`](Self::start_frame), so its frames keep their indices. + /// at [`start`](Self::start)'s frame, so its frames keep their indices. pub fn accept(&self, sequence: u64) -> Result, MoqError> { let _guard = crate::ffi::enter(); let group = self.inner.accept(sequence, None)?; @@ -585,7 +580,7 @@ impl MoqFill { /// /// Mirrors [`moq_net::track::Request`]: [`accept`](Self::accept) it to start producing raw /// frames, hand it to [`crate::media::MoqMediaTrackProducer::audio`] to publish media, -/// or [`abort`](Self::abort) it to reject the waiting subscriber. +/// or [`reject`](Self::reject) it to fail the waiting subscriber. #[derive(uniffi::Object)] pub struct MoqTrackRequest { inner: std::sync::Mutex>, @@ -654,7 +649,7 @@ impl MoqTrackRequest { } /// Reject the request with an application error code, failing the waiting subscriber. - pub fn abort(&self, error_code: u16) -> Result<(), MoqError> { + pub fn reject(&self, error_code: u16) -> Result<(), MoqError> { let _guard = crate::ffi::enter(); let request = self.take()?; request.reject(moq_net::Error::App(error_code)); diff --git a/rs/moq-ffi/src/test.rs b/rs/moq-ffi/src/test.rs index 7b01a557120..269284b3613 100644 --- a/rs/moq-ffi/src/test.rs +++ b/rs/moq-ffi/src/test.rs @@ -2000,7 +2000,7 @@ async fn track_request_demand_ends_on_rejection() { let subscribe = tokio::spawn(async move { consumer.subscribe_track("events".into(), None).await }); let request = dynamic.requested_track().await.unwrap(); let demand = request.demand().unwrap(); - request.abort(404).unwrap(); + request.reject(404).unwrap(); assert!(subscribe.await.unwrap().is_err()); assert!(demand.until_wanted().await.is_err()); assert!(matches!(request.demand(), Err(MoqError::Closed))); @@ -2304,7 +2304,7 @@ async fn dynamic_track_request_can_abort() { .expect("timed out waiting for requested track") .unwrap(); - track.abort(404).unwrap(); + track.reject(404).unwrap(); assert!(matches!(track.name(), Err(MoqError::Closed))); let result = kio::clock::timeout(TIMEOUT, subscribe) @@ -2670,10 +2670,8 @@ async fn dynamic_track_serves_fetch_miss_and_priority() { .await .expect("timed out waiting for fill") .unwrap(); - assert_eq!( - (request.start_group(), request.start_frame(), request.end_group()), - (5, 0, 6) - ); + assert_eq!(request.start(), MoqPosition { group: 5, frame: 0 }); + assert_eq!(request.end(), 6); assert_eq!(request.priority(), 11); assert_eq!(request.wanted(), Some(5)); @@ -2822,7 +2820,7 @@ async fn requested_track_dynamic_survives_accept() { .await .expect("timed out waiting for fill") .unwrap(); - assert_eq!(fill.start_group(), 9); + assert_eq!(fill.start().group, 9); let group = fill.accept(9).unwrap(); group .write_frame(MoqFrame { diff --git a/rs/moq-hls/src/export/mod.rs b/rs/moq-hls/src/export/mod.rs index d882cc09702..143f1d1028a 100644 --- a/rs/moq-hls/src/export/mod.rs +++ b/rs/moq-hls/src/export/mod.rs @@ -194,7 +194,8 @@ impl Broadcaster { } /// Return true if this is a clone of the same broadcaster. - pub fn is_clone(&self, other: &Self) -> bool { + #[cfg(feature = "server")] + pub(crate) fn is_clone(&self, other: &Self) -> bool { Arc::ptr_eq(&self._guard, &other._guard) } diff --git a/rs/moq-hls/src/export/rendition.rs b/rs/moq-hls/src/export/rendition.rs index 37c5429b6ec..78766cb3a37 100644 --- a/rs/moq-hls/src/export/rendition.rs +++ b/rs/moq-hls/src/export/rendition.rs @@ -593,13 +593,14 @@ impl Rendition { } /// The catalog's `media.join.interval`, which fixes the [segment length](Self::segment_length). - pub fn interval(&self) -> Duration { + pub(super) fn interval(&self) -> Duration { self.interval } /// How long a segment is: the smallest whole multiple of the interval that is at least a /// second, the grid every rendition numbers its segments on. - pub fn segment_length(&self) -> Duration { + #[cfg(test)] + pub(super) fn segment_length(&self) -> Duration { self.segment } diff --git a/rs/moq-hls/src/import.rs b/rs/moq-hls/src/import.rs index 1d444f158db..88678186379 100644 --- a/rs/moq-hls/src/import.rs +++ b/rs/moq-hls/src/import.rs @@ -62,17 +62,14 @@ pub struct Config { pub playlist: Url, /// Republish each fMP4 fragment whole as CMAF instead of splitting it into one Legacy frame per - /// sample. See [`fmp4::Import::with_passthrough`](moq_mux::container::fmp4::Import::with_passthrough). - pub passthrough: bool, + /// sample. See [`fmp4::Import::with_cmaf`](moq_mux::container::fmp4::Import::with_cmaf). + pub cmaf: bool, } impl Config { /// Create an import configuration for `playlist`. pub fn new(playlist: Url) -> Self { - Self { - playlist, - passthrough: false, - } + Self { playlist, cmaf: false } } } @@ -367,17 +364,17 @@ struct Sink { /// fragment's arrival. timebase: moq_mux::catalog::Timebase, /// Republish fragments whole rather than unwrapping their samples. - passthrough: bool, + cmaf: bool, } impl Sink { - fn new(broadcast: moq_net::broadcast::Producer, catalog: CatalogProducer, passthrough: bool) -> Self { + fn new(broadcast: moq_net::broadcast::Producer, catalog: CatalogProducer, cmaf: bool) -> Self { let timebase = catalog.timebase(); Self { broadcast, catalog, timebase, - passthrough, + cmaf, } } @@ -388,7 +385,7 @@ impl Sink { // only covers the tracks this init segment declares. Fmp4::new(self.broadcast.clone(), self.timebase.reserve()) .with_select(select.clone()) - .with_passthrough(self.passthrough) + .with_cmaf(self.cmaf) } } @@ -718,7 +715,7 @@ impl Import { pub fn new(broadcast: moq_net::broadcast::Producer, catalog: CatalogProducer, cfg: Config) -> Result { let fetcher = Fetcher::new(&cfg)?; Ok(Self { - sink: Sink::new(broadcast, catalog, cfg.passthrough), + sink: Sink::new(broadcast, catalog, cfg.cmaf), fetcher, playlist: cfg.playlist, video: Vec::new(), @@ -2108,7 +2105,7 @@ mod tests { /// retries the step within its backoff budget. #[tokio::test] async fn a_refused_segment_recovers_through_a_new_importer() { - for passthrough in [false, true] { + for cmaf in [false, true] { let dir = temp_dir(); write_synth(&dir, &synth(true), "video", &[5_000_000, 7_000_000]); let playlist = @@ -2117,7 +2114,7 @@ mod tests { let mut broadcast = moq_net::broadcast::Info::new().produce(); let catalog = CatalogProducer::new(&mut broadcast, moq_mux::catalog::Config::default()).unwrap(); let mut cfg = Config::new(Url::from_file_path(dir.join("media.m3u8")).unwrap()); - cfg.passthrough = passthrough; + cfg.cmaf = cmaf; let mut import = Import::new(broadcast, catalog.clone(), cfg).unwrap(); import.init().await.unwrap(); let previous = catalog.snapshot().video.renditions.keys().next().unwrap().clone(); @@ -2303,7 +2300,7 @@ mod tests { /// Exercise the playlist/map path with real fMP4 produced by the exporter's muxer. /// /// The second segment, stamped `after_us`, follows a 5 s one behind the playlist `tags`. - async fn discontinuity(video: bool, passthrough: bool, tags: &str, after_us: u64) -> Discontinuity { + async fn discontinuity(video: bool, cmaf: bool, tags: &str, after_us: u64) -> Discontinuity { let dir = temp_dir(); let muxer = synth(video); write_synth(&dir, &muxer, "", &[5_000_000, after_us]); @@ -2312,7 +2309,7 @@ mod tests { let mut broadcast = moq_net::broadcast::Info::new().produce(); let consumer = broadcast.consume(); let catalog = CatalogProducer::new(&mut broadcast, moq_mux::catalog::Config::default()).unwrap(); - let sink = Sink::new(broadcast, catalog.clone(), passthrough); + let sink = Sink::new(broadcast, catalog.clone(), cmaf); let mut track = TrackState::new( if video { "video" } else { "audio" }.to_string(), Url::from_file_path(dir.join("media.m3u8")).unwrap(), @@ -2382,8 +2379,8 @@ mod tests { #[tokio::test] async fn forward_discontinuity_preserves_source_time_and_identity() { for video in [false, true] { - for passthrough in [false, true] { - let case = discontinuity(video, passthrough, DISCONTINUITY, 7_000_000).await; + for cmaf in [false, true] { + let case = discontinuity(video, cmaf, DISCONTINUITY, 7_000_000).await; case.result.unwrap(); assert!(case.terminal.is_none(), "{terminal:?}", terminal = case.terminal); assert_eq!(case.before, case.after); @@ -2398,16 +2395,16 @@ mod tests { const DISCONTINUITY: &str = "#EXT-X-DISCONTINUITY\n"; /// A timestamp reset under the same init segment is refused as a rewind, unwrapped or - /// passthrough, with or without the discontinuity the source should have signalled, and + /// CMAF, with or without the discontinuity the source should have signalled, and /// without publishing anything for it: the track keeps its media and its name, and no group /// (not even an aborted one) opens for the refused segment. #[tokio::test] async fn timestamp_reset_is_refused() { for video in [false, true] { - for passthrough in [false, true] { + for cmaf in [false, true] { for tags in [DISCONTINUITY, ""] { - let case = discontinuity(video, passthrough, tags, 0).await; - let label = format!("video={video} passthrough={passthrough} tags={tags:?}"); + let case = discontinuity(video, cmaf, tags, 0).await; + let label = format!("video={video} cmaf={cmaf} tags={tags:?}"); assert!( matches!( case.result, @@ -2480,9 +2477,9 @@ mod tests { #[tokio::test] async fn changed_map_discontinuity_publishes_a_fresh_identity() { for video in [false, true] { - for passthrough in [false, true] { + for cmaf in [false, true] { for after_us in [0, 7_000_000] { - let case = discontinuity(video, passthrough, CHANGED_MAP, after_us).await; + let case = discontinuity(video, cmaf, CHANGED_MAP, after_us).await; case.result.unwrap(); assert!(case.terminal.is_none(), "{terminal:?}", terminal = case.terminal); assert_ne!(case.before, case.after); diff --git a/rs/moq-hls/tests/integration/restart.rs b/rs/moq-hls/tests/integration/restart.rs index 2e8edfd4234..5a9e2200946 100644 --- a/rs/moq-hls/tests/integration/restart.rs +++ b/rs/moq-hls/tests/integration/restart.rs @@ -77,7 +77,9 @@ async fn takeover(version: &str, case: Case) { let deadline = kio::clock::now() + TIMEOUT; let current = loop { let current = server.broadcaster("live").await.expect("announced"); - if !current.is_clone(&replaced) { + // Each broadcaster builds its own renditions, so only the replaced one still holds this. + let same = current.rendition(rendition.kind, &rendition.name); + if !same.is_some_and(|same| std::sync::Arc::ptr_eq(&same, &rendition)) { break current; } assert!(kio::clock::now() < deadline, "the new instance never took over"); diff --git a/rs/moq-mux/benches/containers.rs b/rs/moq-mux/benches/containers.rs index 867a52b2399..49e3a60da21 100644 --- a/rs/moq-mux/benches/containers.rs +++ b/rs/moq-mux/benches/containers.rs @@ -128,7 +128,7 @@ async fn export(format: &str, source: moq_mux::Source, stream: impl Stream, coll fn cmaf_import(passthrough: bool) -> (moq_mux::catalog::Producer, moq_mux::container::fmp4::Import) { let mut broadcast = moq_net::broadcast::Info::new().produce(); let catalog = moq_mux::catalog::Producer::new(&mut broadcast, Default::default()).unwrap(); - let import = moq_mux::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let import = moq_mux::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); (catalog, import) } diff --git a/rs/moq-mux/src/catalog/producer.rs b/rs/moq-mux/src/catalog/producer.rs index fbbe4245c4a..37987e66cc5 100644 --- a/rs/moq-mux/src/catalog/producer.rs +++ b/rs/moq-mux/src/catalog/producer.rs @@ -1295,7 +1295,7 @@ fn packaging_of(container: &hang::catalog::Container) -> moq_msf::Packaging { /// Convert a catalog to its MSF equivalent: the media sections become MSF tracks and the /// extension rides the MSF root under the same member names it uses in the hang catalog. pub(crate) fn to_msf(catalog: &Catalog, base: &moq_net::Path) -> moq_msf::Catalog { - let mut msf = to_msf_media(&catalog.media(), base); + let mut msf = to_msf_media(&catalog.without_ext(), base); msf.ext = catalog.ext.clone(); msf } diff --git a/rs/moq-mux/src/codec/annexb/export.rs b/rs/moq-mux/src/codec/annexb/export.rs index 32aea9f3248..cba8d472020 100644 --- a/rs/moq-mux/src/codec/annexb/export.rs +++ b/rs/moq-mux/src/codec/annexb/export.rs @@ -69,7 +69,7 @@ impl Export { pub fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll>> { while let Some(catalog) = self.catalog.as_mut() { match catalog.poll_next(waiter)? { - Poll::Ready(Some(snapshot)) => self.update_catalog(&snapshot.media())?, + Poll::Ready(Some(snapshot)) => self.update_catalog(&snapshot.without_ext())?, Poll::Ready(None) => { self.catalog = None; break; diff --git a/rs/moq-mux/src/container/consumer.rs b/rs/moq-mux/src/container/consumer.rs index 71879c83fc4..fbfd63b514c 100644 --- a/rs/moq-mux/src/container/consumer.rs +++ b/rs/moq-mux/src/container/consumer.rs @@ -118,7 +118,7 @@ pub(crate) enum Item { /// /// ## Memory /// -/// Decoded frames waiting to be read are charged to a byte cap, [`set_max_buffer`](Self::set_max_buffer), +/// Decoded frames waiting to be read are charged to a byte cap, one group at the transport's size cap, /// counting the whole wire payload they share, along with each buffered frame's and group's own /// overhead. Once it is full the consumer stops /// decoding ahead and stops pulling new groups past one successor to the cursor's group, so the @@ -218,8 +218,8 @@ pub struct Consumer { /// about one group duration. const CONTIGUITY_TOLERANCE: std::time::Duration = std::time::Duration::from_millis(1); -/// The default [`Consumer::set_max_buffer`]: one group at the transport's size cap. -pub const DEFAULT_MAX_BUFFER: usize = moq_net::group::MAX_CACHE_BYTES as usize; +/// The default decoded-byte cap: one group at the transport's size cap. +pub(crate) const DEFAULT_MAX_BUFFER: usize = moq_net::group::MAX_CACHE_BYTES as usize; /// The decoded bytes a consumer's groups hold, shared with each group so one dropped by any path /// refunds what it held. @@ -247,6 +247,7 @@ impl Memory { self.used() >= self.0.max.load(Ordering::Relaxed) } + #[cfg(test)] fn set_max(&self, max: usize) { self.0.max.store(max, Ordering::Relaxed); } @@ -1184,7 +1185,8 @@ impl Consumer { /// Cap the bytes of decoded frames held ahead of the reader, counting the wire payload they share /// and each frame's and group's own overhead (default [`DEFAULT_MAX_BUFFER`]). Past it the consumer stops decoding ahead /// and pulling new groups; see the type docs. - pub fn set_max_buffer(&mut self, bytes: usize) { + #[cfg(test)] + pub(crate) fn set_max_buffer(&mut self, bytes: usize) { self.memory.set_max(bytes); } diff --git a/rs/moq-mux/src/container/flv/export.rs b/rs/moq-mux/src/container/flv/export.rs index 4f1e9dd1662..14b92d576d5 100644 --- a/rs/moq-mux/src/container/flv/export.rs +++ b/rs/moq-mux/src/container/flv/export.rs @@ -386,7 +386,7 @@ impl Export { // 1. Drain catalog updates to discover the track layout. while let Some(catalog) = self.catalog.as_mut() { match catalog.poll_next(waiter)? { - Poll::Ready(Some(snapshot)) => self.update_catalog(snapshot.media())?, + Poll::Ready(Some(snapshot)) => self.update_catalog(snapshot.without_ext())?, Poll::Ready(None) => { self.catalog = None; break; diff --git a/rs/moq-mux/src/container/fmp4/export.rs b/rs/moq-mux/src/container/fmp4/export.rs index d95921a2e3b..c0f69acc8b2 100644 --- a/rs/moq-mux/src/container/fmp4/export.rs +++ b/rs/moq-mux/src/container/fmp4/export.rs @@ -631,7 +631,7 @@ impl Export { // 1. Drain catalog updates and (un)subscribe tracks accordingly. while let Some(catalog) = self.catalog.as_mut() { match catalog.poll_next(waiter)? { - Poll::Ready(Some(snapshot)) => self.update_catalog(&snapshot.media())?, + Poll::Ready(Some(snapshot)) => self.update_catalog(&snapshot.without_ext())?, Poll::Ready(None) => { self.catalog = None; break; diff --git a/rs/moq-mux/src/container/fmp4/import.rs b/rs/moq-mux/src/container/fmp4/import.rs index d11125c7c14..36cacd44aa0 100644 --- a/rs/moq-mux/src/container/fmp4/import.rs +++ b/rs/moq-mux/src/container/fmp4/import.rs @@ -14,7 +14,7 @@ use crate::container::producer::Streamed; /// By default each fragment is unwrapped: every sample becomes one /// [`Legacy`](Container::Legacy) frame carrying the codec payload, timed at its presentation time, /// so a consumer needs no ISO BMFF parser. Either way, each track's edit list is applied to the -/// frame timestamps and left out of the catalog. [`with_passthrough`](Self::with_passthrough) instead +/// frame timestamps and left out of the catalog. [`with_cmaf`](Self::with_cmaf) instead /// republishes each fragment whole as one [`Cmaf`](Container::Cmaf) frame, for a source whose exact /// `trun` timing must survive. /// @@ -55,7 +55,7 @@ pub struct Import { select: Option, // Republish each fragment whole as CMAF instead of unwrapping its samples. - passthrough: bool, + cmaf: bool, // A lookup to tracks in the broadcast tracks: HashMap>, @@ -271,7 +271,7 @@ impl Import { timebase: reserved.timebase(), initial_reservation: Some(reserved), select: None, - passthrough: false, + cmaf: false, tracks: HashMap::default(), text: HashMap::default(), skipped: HashSet::default(), @@ -331,8 +331,8 @@ impl Import { /// /// Keeps the source's exact `trun` timing (sample durations, composition offsets) at the cost /// of every consumer parsing ISO BMFF. Text tracks are reconstructed as timed text either way. - pub fn with_passthrough(mut self, passthrough: bool) -> Self { - self.passthrough = passthrough; + pub fn with_cmaf(mut self, cmaf: bool) -> Self { + self.cmaf = cmaf; self } @@ -352,12 +352,11 @@ impl Import { /// `default_base_moof`. Forward gaps and interleaved tracks are supported. /// /// Media samples stream into their frames as bytes arrive. What must be held whole is - /// bounded: a `moov` or `moof` body larger than - /// [`MAX_METADATA_BYTES`](super::MAX_METADATA_BYTES), or a text sample larger than - /// [`MAX_HELD_BYTES`](super::MAX_HELD_BYTES), is refused before it is held. A `moof` declaring - /// more than [`MAX_FRAGMENT_SAMPLES`](super::MAX_FRAGMENT_SAMPLES) samples, or more than its - /// size accepts, is refused before they are planned, and a `moov` declaring more than - /// [`MAX_TRACKS`](super::MAX_TRACKS) tracks before any is created. + /// bounded: a `moov` or `moof` body larger than 2 MiB, or a text sample larger than the + /// transport's per-group bound ([`MAX_CACHE_BYTES`](moq_net::group::MAX_CACHE_BYTES)), is + /// refused before it is held. A `moof` declaring more than 131,072 samples, or more than its + /// size accepts, is refused before they are planned, and a `moov` declaring more than 256 + /// tracks before any is created. /// /// A decode error is terminal: later decodes, cuts, seeks and discontinuities return the same /// error without reading input or changing the tracks. @@ -553,7 +552,7 @@ impl Import { let timescale = moq_net::Timescale::new(trak.mdia.mdhd.timescale as u64)?; let edit = Edit::new(trak, moov.mvhd.timescale)?; - let output = match self.passthrough { + let output = match self.cmaf { true => Output::Passthrough(Box::new( self.init_passthrough(trak, &moov, &reserved, kind, timescale)?, )), @@ -705,7 +704,7 @@ impl Import { /// The container a rendition advertises: unwrapped samples are Legacy frames, while a /// passthrough track carries its own single-track init segment. fn container(&self, trak: &Trak, moov: &Moov) -> Result { - if !self.passthrough { + if !self.cmaf { return Ok(Container::Legacy); } @@ -1895,7 +1894,7 @@ mod tests { let pool = info.pool.clone(); let mut broadcast = info.produce(); let catalog = crate::catalog::Producer::new(&mut broadcast, Default::default()).unwrap(); - let mut import = Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut import = Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); let mut data = include_bytes!("test_data/bbb.mp4").as_slice(); mp4_atom::Ftyp::decode(&mut data).unwrap(); let mut moov = Moov::decode(&mut data).unwrap(); diff --git a/rs/moq-mux/src/container/fmp4/import_test.rs b/rs/moq-mux/src/container/fmp4/import_test.rs index da326f51d4e..9f442025800 100644 --- a/rs/moq-mux/src/container/fmp4/import_test.rs +++ b/rs/moq-mux/src/container/fmp4/import_test.rs @@ -39,7 +39,7 @@ fn run_fmp4_with(data: &[u8], passthrough: bool) -> crate::catalog::hang::Catalo let mut broadcast = moq_net::broadcast::Info::new().produce(); let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); - let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); let buf = bytes::BytesMut::from(data); // Ignore errors from incomplete/malformed trailing fragments in test files. @@ -638,7 +638,7 @@ fn repeated_reinit_keeps_flat_state() { for generation in 0..50 { let label = format!("passthrough={passthrough} generation={generation}"); let mut fmp4 = - crate::container::fmp4::Import::new(broadcast.clone(), catalog.reserve()).with_passthrough(passthrough); + crate::container::fmp4::Import::new(broadcast.clone(), catalog.reserve()).with_cmaf(passthrough); fmp4.decode(&bytes::BytesMut::from(data)).unwrap(); let snapshot = catalog.snapshot(); assert_eq!(snapshot.video.renditions.len(), 1, "{label}"); @@ -692,7 +692,7 @@ async fn bbb_samples(passthrough: bool) -> [Vec<(u128, bool, bytes::Bytes)>; 2] let mut broadcast = moq_net::broadcast::Info::new().produce(); let consumer = broadcast.consume(); let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); - let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); // The file ends on a malformed fragment, after every whole one has published. let _ = fmp4.decode(include_bytes!("test_data/bbb.mp4")); fmp4.finish().unwrap(); @@ -850,7 +850,7 @@ async fn separated_runs_keep_their_samples() { let mut broadcast = moq_net::broadcast::Info::new().produce(); let consumer = broadcast.consume(); let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); - let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); fmp4.decode(&init).unwrap(); for chunk in fragment.chunks(7) { fmp4.decode(chunk).unwrap(); @@ -899,7 +899,7 @@ fn empty_samples_are_refused() { for passthrough in [false, true] { let mut broadcast = moq_net::broadcast::Info::new().produce(); let catalog = crate::catalog::Producer::new(&mut broadcast, Default::default()).unwrap(); - let mut import = super::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut import = super::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); import.decode(&init).unwrap(); assert!( matches!( @@ -946,7 +946,7 @@ fn absolute_sample_bases_are_refused() { for passthrough in [false, true] { let mut broadcast = moq_net::broadcast::Info::new().produce(); let catalog = crate::catalog::Producer::new(&mut broadcast, Default::default()).unwrap(); - let mut import = super::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut import = super::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); import.decode(&init).unwrap(); assert!(matches!( import.decode(&fragment), @@ -1013,8 +1013,7 @@ async fn shared_sample_bytes_are_refused() { for passthrough in [false, true] { let mut broadcast = moq_net::broadcast::Info::new().produce(); let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); - let mut fmp4 = - crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); fmp4.decode(&init).unwrap(); let err = fmp4.decode(&fragment).unwrap_err(); assert!( @@ -1152,8 +1151,7 @@ fn fieldless_runs_do_not_stall_the_segment_scan() { for passthrough in [false, true] { let mut broadcast = moq_net::broadcast::Info::new().produce(); let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); - let mut fmp4 = - crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); fmp4.decode(&init).unwrap(); fmp4.decode(&video_fragment(video_id, 0, tick, &[(0, 0, 0xA)])).unwrap(); assert!( @@ -1222,8 +1220,7 @@ async fn audio_presents_from_its_edit() { let mut broadcast = moq_net::broadcast::Info::new().produce(); let consumer = broadcast.consume(); let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); - let mut fmp4 = - crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); fmp4.decode(&init).unwrap(); // A fragment wholly before the edit publishes nothing. fmp4.decode(&audio_fragment(0, 512, 4)).unwrap(); @@ -1335,8 +1332,7 @@ async fn video_drops_the_samples_before_its_edit() { let mut broadcast = moq_net::broadcast::Info::new().produce(); let consumer = broadcast.consume(); let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); - let mut fmp4 = - crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); fmp4.decode(&init).unwrap(); for fragment in &fragments { fmp4.decode(fragment).unwrap(); @@ -1385,7 +1381,7 @@ async fn video_joined_mid_gop_starts_at_a_sync_sample() { let mut broadcast = moq_net::broadcast::Info::new().produce(); let consumer = broadcast.consume(); let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); - let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); fmp4.decode(&init).unwrap(); for fragment in &fragments { fmp4.decode(fragment).unwrap(); @@ -1554,7 +1550,7 @@ async fn test_seek_sets_initial_sequence_in(passthrough: bool) { let mut broadcast = moq_net::broadcast::Info::new().produce(); let broadcast_consumer = broadcast.consume(); let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); - let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); let data = include_bytes!("test_data/bbb.mp4"); @@ -1620,7 +1616,7 @@ async fn test_msf_catalog_roundtrip() { // MSF catalog track has been created by `catalog::Producer::new`. let consumer = broadcast.consume(); let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); - let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(true); + let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(true); let data = include_bytes!("test_data/bbb.mp4"); let buf = bytes::BytesMut::from(&data[..]); @@ -2024,7 +2020,7 @@ async fn segmented_source_groups_per_segment_in(passthrough: bool) { let mut broadcast = moq_net::broadcast::Info::new().produce(); let consumer = broadcast.consume(); let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); - let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); fmp4.decode(&init).unwrap(); let snapshot = catalog.snapshot(); @@ -2081,7 +2077,7 @@ async fn segmented_source_writes_one_group_per_segment_in(passthrough: bool) { let mut broadcast = moq_net::broadcast::Info::new().produce(); let consumer = broadcast.consume(); let mut catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); - let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); fmp4.decode(&init).unwrap(); let snapshot = catalog.snapshot(); @@ -2140,7 +2136,7 @@ async fn groups_with_skew(video_offset_us: i64, passthrough: bool) -> (Vec, let mut broadcast = moq_net::broadcast::Info::new().produce(); let consumer = broadcast.consume(); let mut catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); - let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); fmp4.decode(&init).unwrap(); let snapshot = catalog.snapshot(); @@ -2225,7 +2221,7 @@ async fn a_single_leading_styp_still_segments_on_keyframes_in(passthrough: bool) let mut broadcast = moq_net::broadcast::Info::new().produce(); let consumer = broadcast.consume(); let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); - let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); fmp4.decode(&init).unwrap(); let snapshot = catalog.snapshot(); @@ -2285,7 +2281,7 @@ async fn a_mid_gop_join_keeps_tracks_on_the_same_groups_in(passthrough: bool) { let mut broadcast = moq_net::broadcast::Info::new().produce(); let consumer = broadcast.consume(); let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); - let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); fmp4.decode(&init).unwrap(); let snapshot = catalog.snapshot(); @@ -2456,7 +2452,7 @@ fn fragment_jitter_never_shrinks_in(passthrough: bool) { let mut broadcast = moq_net::broadcast::Info::new().produce(); let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); - let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); fmp4.decode(&init).unwrap(); let audio_jitter = |catalog: &crate::catalog::Producer| { @@ -2803,7 +2799,7 @@ async fn seek_keeps_the_decode_time_until_a_discontinuity_in(passthrough: bool) for discontinuity in [false, true] { let mut broadcast = moq_net::broadcast::Info::new().produce(); let catalog = crate::catalog::Producer::new(&mut broadcast, Default::default()).unwrap(); - let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); fmp4.decode(&init).unwrap(); fmp4.decode(&live_session(5_000_000, 5)).unwrap(); if discontinuity { @@ -2949,7 +2945,7 @@ async fn imports_the_sample_defaults_fixtures_in(passthrough: bool) { let mut broadcast = moq_net::broadcast::Info::new().produce(); let consumer = broadcast.consume(); let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); - let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut fmp4 = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); fmp4.decode(&fixture.init).unwrap(); let snapshot = catalog.snapshot(); @@ -3818,7 +3814,7 @@ async fn declared_boundaries_wait_for_sync_samples() { let mut broadcast = moq_net::broadcast::Info::new().produce(); let consumer = broadcast.consume(); let catalog = crate::catalog::Producer::new(&mut broadcast, Default::default()).unwrap(); - let mut import = super::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut import = super::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); import.decode(&init).unwrap(); let snapshot = catalog.snapshot(); let name = snapshot.video.renditions.keys().next().unwrap(); @@ -3856,7 +3852,7 @@ async fn declared_boundaries_do_not_split_passthrough_fragments() { let mut broadcast = moq_net::broadcast::Info::new().produce(); let consumer = broadcast.consume(); let catalog = crate::catalog::Producer::new(&mut broadcast, Default::default()).unwrap(); - let mut import = super::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut import = super::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); import.decode(&init).unwrap(); let snapshot = catalog.snapshot(); let name = snapshot.video.renditions.keys().next().unwrap(); diff --git a/rs/moq-mux/src/container/fmp4/mod.rs b/rs/moq-mux/src/container/fmp4/mod.rs index 9fb800207fa..e2e99d7cf2c 100644 --- a/rs/moq-mux/src/container/fmp4/mod.rs +++ b/rs/moq-mux/src/container/fmp4/mod.rs @@ -31,7 +31,7 @@ pub use mp4_atom; /// any of it is staged. It matches the transport's per-group bound, /// [`MAX_CACHE_BYTES`](moq_net::group::MAX_CACHE_BYTES), which the rewritten sample must fit. /// Media samples stream into their frames and have no such bound. -pub const MAX_HELD_BYTES: usize = moq_net::group::MAX_CACHE_BYTES as usize; +const MAX_HELD_BYTES: usize = moq_net::group::MAX_CACHE_BYTES as usize; /// The largest metadata box body parsed on import or from received CMAF. /// @@ -40,7 +40,7 @@ pub const MAX_HELD_BYTES: usize = moq_net::group::MAX_CACHE_BYTES as usize; /// [`TrunEntry`](mp4_atom::TrunEntry)), so this keeps a parse within [`MAX_HELD_BYTES`]. /// Counts and retained metadata-tree entries are checked separately, including entries that /// consume no bytes. Media payloads in `mdat` keep their transport-owned bound. -pub const MAX_METADATA_BYTES: usize = MAX_HELD_BYTES / 16; +const MAX_METADATA_BYTES: usize = MAX_HELD_BYTES / 16; /// The most samples parsed or planned for one CMAF fragment. /// @@ -48,7 +48,7 @@ pub const MAX_METADATA_BYTES: usize = MAX_HELD_BYTES / 16; /// a sample, so a fragment at this bound plans within [`MAX_HELD_BYTES`]. A `moof` at /// [`MAX_METADATA_BYTES`] holds this many 16-byte `trun` entries. A shorter fragment is also held /// to one sample per metadata byte past the first 4,096. -pub const MAX_FRAGMENT_SAMPLES: u64 = (MAX_METADATA_BYTES / 16) as u64; +const MAX_FRAGMENT_SAMPLES: u64 = (MAX_METADATA_BYTES / 16) as u64; /// Samples a fragment may declare beyond one per byte of its metadata. /// @@ -62,7 +62,7 @@ pub(crate) const FRAGMENT_SAMPLE_BASE: u64 = 4096; /// /// Each track costs about 16 KiB to parse and set up, and keeps its catalog entry and output /// state for as long as the import runs. Real files declare a handful. -pub const MAX_TRACKS: usize = 256; +const MAX_TRACKS: usize = 256; #[cfg(test)] mod export_test; @@ -235,7 +235,7 @@ pub enum Error { #[error("backwards per-track data offset is unsupported for streaming import")] BackwardsDataOffset, - /// A metadata body exceeds [`MAX_METADATA_BYTES`] and is refused before parsing. + /// A metadata body over 2 MiB is refused before parsing. #[error("{kind} declares {size} bytes, more than the {MAX_METADATA_BYTES} a metadata parser accepts")] MetadataTooLarge { /// The box type. @@ -244,18 +244,19 @@ pub enum Error { size: usize, }, - /// A text sample is rewritten whole, so one larger than [`MAX_HELD_BYTES`] is refused - /// before any of its bytes are staged. + /// A text sample is rewritten whole, so one larger than the transport's per-group bound, + /// [`MAX_CACHE_BYTES`](moq_net::group::MAX_CACHE_BYTES), is refused before any of its bytes + /// are staged. #[error("text sample of {0} bytes is more than the {MAX_HELD_BYTES} an importer holds")] TextSampleTooLarge(usize), - /// A fragment declaring more than [`MAX_FRAGMENT_SAMPLES`], or more than one sample per byte + /// A fragment declaring more than 131,072 samples, or more than one sample per byte /// of its metadata past a small base, is refused before parsing. A `trun` without per-sample /// fields declares its count in four bytes, so the byte bound alone does not limit it. #[error("moof declares {0} samples, more than a fragment of its size accepts")] TooManySamples(u64), - /// A `moov` declaring more than [`MAX_TRACKS`] tracks is refused before any is created. + /// A `moov` declaring more than 256 tracks is refused before any is created. #[error("moov declares {0} tracks, more than the {MAX_TRACKS} an importer accepts")] TooManyTracks(usize), diff --git a/rs/moq-mux/src/container/mkv/export.rs b/rs/moq-mux/src/container/mkv/export.rs index b3bc30b0972..808afac2e89 100644 --- a/rs/moq-mux/src/container/mkv/export.rs +++ b/rs/moq-mux/src/container/mkv/export.rs @@ -380,7 +380,7 @@ impl Export { // 1. Drain catalog updates. while let Some(catalog) = self.catalog.as_mut() { match catalog.poll_next(waiter)? { - Poll::Ready(Some(snapshot)) => self.update_catalog(snapshot.media())?, + Poll::Ready(Some(snapshot)) => self.update_catalog(snapshot.without_ext())?, Poll::Ready(None) => { self.catalog = None; break; diff --git a/rs/moq-mux/src/container/mod.rs b/rs/moq-mux/src/container/mod.rs index a1110a298da..4656c67c6f0 100644 --- a/rs/moq-mux/src/container/mod.rs +++ b/rs/moq-mux/src/container/mod.rs @@ -31,7 +31,7 @@ pub mod mkv; pub mod mp4; pub mod ts; -pub use consumer::{Consumer, DEFAULT_MAX_BUFFER, Playout}; +pub use consumer::{Consumer, Playout}; pub use group::GroupConsumer; pub use producer::Producer; pub(crate) use source::{ExportSource, Resume}; diff --git a/rs/moq-mux/src/fuzz.rs b/rs/moq-mux/src/fuzz.rs index 00d6e8c3bee..ad62ec7842c 100644 --- a/rs/moq-mux/src/fuzz.rs +++ b/rs/moq-mux/src/fuzz.rs @@ -329,7 +329,7 @@ pub fn seeds() -> Vec { /// The catalog an import of bbb.mp4 publishes, with CMAF passthrough or without. fn imported(passthrough: bool) -> crate::catalog::hang::Catalog<()> { let (broadcast, catalog) = broadcast(); - let mut import = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(passthrough); + let mut import = crate::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(passthrough); import .decode(&include_bytes!("container/fmp4/test_data/bbb.mp4")[..2035]) .expect("the fixture imports"); diff --git a/rs/moq-mux/src/import/format.rs b/rs/moq-mux/src/import/format.rs index 90d12529fcf..d94fa39d091 100644 --- a/rs/moq-mux/src/import/format.rs +++ b/rs/moq-mux/src/import/format.rs @@ -51,7 +51,7 @@ pub enum VideoFormat { /// /// Names only the input the importer parses. Every container is demuxed into its tracks' frames; /// republishing the input whole is a choice made on the concrete importer -/// ([`fmp4::Import::with_passthrough`](crate::container::fmp4::Import::with_passthrough), +/// ([`fmp4::Import::with_cmaf`](crate::container::fmp4::Import::with_cmaf), /// [`ts::Passthrough`](crate::container::ts::Passthrough)), never by the format name. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[non_exhaustive] diff --git a/rs/moq-mux/src/import/mod.rs b/rs/moq-mux/src/import/mod.rs index 4a0d49e6a7c..866629628e7 100644 --- a/rs/moq-mux/src/import/mod.rs +++ b/rs/moq-mux/src/import/mod.rs @@ -165,8 +165,7 @@ mod tests { assert_released("fmp4", &container(ContainerFormat::Fmp4, &data, CHUNK)); assert_released("fmp4 passthrough", &|broadcast, catalog| { - let mut import = - crate::container::fmp4::Import::new(broadcast.clone(), catalog.reserve()).with_passthrough(true); + let mut import = crate::container::fmp4::Import::new(broadcast.clone(), catalog.reserve()).with_cmaf(true); for chunk in data.chunks(CHUNK) { import.decode(chunk).unwrap(); } diff --git a/rs/moq-net/benches/auth.rs b/rs/moq-net/benches/auth.rs index f90c8b82fe3..0fd147bc1c2 100644 --- a/rs/moq-net/benches/auth.rs +++ b/rs/moq-net/benches/auth.rs @@ -187,8 +187,8 @@ fn renewal(c: &mut Criterion) { let mut auths = Vec::new(); for route in 0..routes { auths.push([ - handle.add(format!("a/{route}")).await.unwrap().0, - handle.add(format!("b/{route}")).await.unwrap().0, + handle.add(format!("a/{route}")).await.unwrap(), + handle.add(format!("b/{route}")).await.unwrap(), ]); } auths @@ -356,7 +356,7 @@ fn shared_requests(c: &mut Criterion) { let auths = runtime.block_on(async { let mut auths = Vec::new(); for token in 0..tokens { - auths.push(client.auth().add(format!("token/{token}")).await.unwrap().0); + auths.push(client.auth().add(format!("token/{token}")).await.unwrap()); } auths }); diff --git a/rs/moq-net/src/auth.rs b/rs/moq-net/src/auth.rs index 8623a8acc55..d49a72a1d56 100644 --- a/rs/moq-net/src/auth.rs +++ b/rs/moq-net/src/auth.rs @@ -48,7 +48,8 @@ use crate::{Error, Patterns, Result, SessionError, time::Instant}; mod tokens; -pub use tokens::{Dial, EmptyToken, Token, Tokens}; +pub(crate) use tokens::Dial; +pub use tokens::{EmptyToken, Token, Tokens}; /// Classify malformed AUTH messages separately from transport and local failures. pub(crate) fn protocol_error(err: Error) -> Error { @@ -74,7 +75,7 @@ macro_rules! ready_or { /// How a peer accepted a token [`Handle::add`] presented. #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum Answer { +pub(crate) enum Answer { /// Granted on its AUTH stream: its grant joins the union. Granted, /// Taken only for the requests that name it, on moq-transport without AUTH. @@ -633,16 +634,14 @@ impl Handle { Watch::new(self.state.clone(), Selector::Union) } - /// Present another token on every session of this handle, resolving once a peer accepts it - /// with how it did. + /// Present another token on every session of this handle, resolving once a peer accepts it. /// /// The grant joins the union while any clone of the returned [`Credential`] lives. With no /// session live it waits for the next one. A refusal that may pass, such as an /// unreachable auth server, is presented again after a backoff, and so is a token whose /// stream the peer drops without a verdict. On moq-transport - /// without AUTH there is no answer to wait for: it resolves at once with - /// [`Answer::Requests`] and a grant that stays unknown, and only a request that names it - /// carries the token. + /// without AUTH there is no answer to wait for: it resolves at once with a grant that + /// stays unknown, and only a request that names it carries the token. /// /// # Errors /// @@ -650,10 +649,10 @@ impl Handle { /// (moq-lite without AUTH, or a peer that takes no tokens in band), the peer's refusal /// as [`Error::Session`], or the handle's end. #[cfg(feature = "async")] - pub async fn add(&self, token: impl Into) -> Result<(Credential, Answer)> { + pub async fn add(&self, token: impl Into) -> Result { let auth = self.present(token.into())?; - let answer = kio::wait(|waiter| auth.poll_answered(waiter)).await?; - Ok((auth, answer)) + kio::wait(|waiter| auth.poll_answered(waiter)).await?; + Ok(auth) } /// Register a token and queue it on every open session. @@ -957,15 +956,7 @@ impl Link { } /// Verify a request token once per session while any naming request remains open. - /// - /// The acceptor sees the `kind` and `path` of the request that names it first. - pub(crate) fn verify_request( - &self, - token: crate::setup::Token, - hold: Option, - kind: RequestKind, - path: &crate::Path, - ) -> RequestVerdict { + pub(crate) fn verify_request(&self, token: crate::setup::Token, hold: Option) -> RequestVerdict { let unsupported = || RequestVerdict { issue: None, _lifetime: None, @@ -1004,7 +995,7 @@ impl Link { }; drop(stale); if fresh { - queue.push(Request::named(token, lifetime.issue.clone(), kind, path.clone())); + queue.push(Request::new(token, lifetime.issue.clone())); } RequestVerdict { issue: Some(lifetime.issue.clone()), @@ -1838,8 +1829,6 @@ impl Drop for Serving { pub struct Request { token: Bytes, issue: Option>, - /// The moq-transport request the token rode on, and its path; `None` for an AUTH token. - named: Option<(RequestKind, crate::Path)>, } impl Request { @@ -1847,16 +1836,6 @@ impl Request { Self { token, issue: Some(issue), - named: None, - } - } - - /// A token that rode on a moq-transport request of `kind` at `path`. - fn named(token: Bytes, issue: kio::Shared, kind: RequestKind, path: crate::Path) -> Self { - Self { - token, - issue: Some(issue), - named: Some((kind, path)), } } @@ -1870,23 +1849,6 @@ impl Request { &self.token } - /// The kind of moq-transport request the token rode on, or `None` for a token presented - /// on the AUTH stream, whose grant joins the session's scope. - /// - /// Requests that repeat a token share its verification, so this is the request that - /// named it first; each later one is still checked against the grant it earns. - pub fn kind(&self) -> Option { - self.named.as_ref().map(|(kind, _)| *kind) - } - - /// The path the request named, in the terms the grant's patterns match: the track's - /// namespace for a [`RequestKind::Subscribe`] or [`RequestKind::Publish`], the announced - /// namespace for a [`RequestKind::PublishNamespace`], and the prefix for a - /// [`RequestKind::Discover`]. `None` exactly when [`Self::kind`] is. - pub fn path(&self) -> Option<&crate::Path> { - self.named.as_ref().map(|(_, path)| path) - } - /// Wait for the peer to withdraw this unanswered token, end the request it rode on, or /// close the session. #[cfg(feature = "async")] @@ -1933,11 +1895,10 @@ fn refuse(issue: &kio::Shared, code: SessionError, reason: String) { } } -/// Which kind of moq-transport request a token rode on, as [`Request::kind`] reports it, and -/// so which part of the [`Grant`] must cover it. +/// Which kind of moq-transport request a token rode on, and so which part of the [`Grant`] +/// must cover it. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -#[non_exhaustive] -pub enum RequestKind { +pub(crate) enum RequestKind { /// The peer reads a track: a SUBSCRIBE, a FETCH, or a TRACK_STATUS. Covered by the /// grant's `subscribe` patterns. Subscribe, @@ -2227,7 +2188,7 @@ impl RequestGrant { hold: Option, ) -> RequestVerdict { let mut held = self.held(); - let verdict = handle.verify_request(token, hold, held.kind, &held.path); + let verdict = handle.verify_request(token, hold); held.renewing = verdict.issue.clone(); verdict } @@ -2535,7 +2496,7 @@ impl Gate { pub(crate) fn verify(&self, token: crate::setup::Token, hold: Option) -> RequestVerdict { match &self.token { Some(grant) => grant.verify_renewal_holding(&self.handle, token, hold), - None => self.handle.verify_request(token, hold, self.kind(), &self.path), + None => self.handle.verify_request(token, hold), } } @@ -2880,12 +2841,7 @@ mod tests { kind: crate::setup::Token::OUT_OF_BAND, value: b"jwt".to_vec(), }; - let _verdict = handle.verify_request( - token, - Some(slot.clone()), - RequestKind::Subscribe, - &crate::Path::from_static("room"), - ); + let _verdict = handle.verify_request(token, Some(slot.clone())); assert_eq!( std::sync::Arc::strong_count(&slot), 2, @@ -3395,7 +3351,7 @@ mod request_token_tests { async fn a_request_token_reaches_the_acceptor() { let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"jwt", 0), None); let request = requests.next().await.expect("a request"); assert_eq!(request.token().as_ref(), b"jwt"); @@ -3409,8 +3365,8 @@ mod request_token_tests { async fn naming_requests_share_verification_and_withdraw_together() { let handle = Link::new(true); let mut requests = handle.handle().requests(); - let first = handle.verify_request(token(b"shared", 0), None, RequestKind::Subscribe, &subscribe_path()); - let second = handle.verify_request(token(b"shared", 0), None, RequestKind::Subscribe, &subscribe_path()); + let first = handle.verify_request(token(b"shared", 0), None); + let second = handle.verify_request(token(b"shared", 0), None); let issued = requests.next().await.unwrap().accept(Grant::all()); assert!(requests.poll_next(&kio::Waiter::noop()).is_pending()); first.grant().await.unwrap(); @@ -3419,7 +3375,7 @@ mod request_token_tests { assert!(issued.closed().now_or_never().is_none()); drop(second); assert!(matches!(issued.closed().await, Error::Cancel)); - let later = handle.verify_request(token(b"shared", 0), None, RequestKind::Subscribe, &subscribe_path()); + let later = handle.verify_request(token(b"shared", 0), None); let _issued = requests.next().await.unwrap().accept(Grant::default()); assert_eq!(later.grant().await.unwrap(), Grant::default()); } @@ -3430,15 +3386,15 @@ mod request_token_tests { async fn a_token_the_acceptor_dropped_verifies_again() { let handle = Link::new(true); let mut requests = handle.handle().requests(); - let first = handle.verify_request(token(b"shared", 0), None, RequestKind::Subscribe, &subscribe_path()); + let first = handle.verify_request(token(b"shared", 0), None); let issued = requests.next().await.unwrap().accept(Grant::all()); first.grant().await.unwrap(); drop(issued); - let later = handle.verify_request(token(b"shared", 0), None, RequestKind::Subscribe, &subscribe_path()); + let later = handle.verify_request(token(b"shared", 0), None); let _issued = requests.next().await.unwrap().accept(Grant::default()); assert_eq!(later.grant().await.unwrap(), Grant::default()); drop(first); - let again = handle.verify_request(token(b"shared", 0), None, RequestKind::Subscribe, &subscribe_path()); + let again = handle.verify_request(token(b"shared", 0), None); assert!( requests.poll_next(&kio::Waiter::noop()).is_pending(), "the live verdict is shared" @@ -3452,8 +3408,8 @@ mod request_token_tests { let runtime = crate::time::sim(); let handle = Link::new(true); let mut requests = handle.handle().requests(); - let first = handle.verify_request(token(b"shared", 0), None, RequestKind::Subscribe, &subscribe_path()); - let second = handle.verify_request(token(b"shared", 0), None, RequestKind::Subscribe, &subscribe_path()); + let first = handle.verify_request(token(b"shared", 0), None); + let second = handle.verify_request(token(b"shared", 0), None); let issued = requests.next().await.unwrap().accept(grant_in(&runtime, 60)); let mut grants = Vec::new(); for verdict in [first, second] { @@ -3546,49 +3502,6 @@ mod request_token_tests { assert!(gate.poll_permit(&mut epoch, &kio::Waiter::noop()).is_pending()); } - /// The acceptor sees the kind and path of the request that first named a token, which - /// later requests repeating it share, and neither for a token from the AUTH stream. - #[kio::test] - async fn a_request_reports_the_kind_and_path_that_named_it() { - let handle = Link::new(true); - let mut requests = handle.handle().requests(); - let room = crate::Path::from_static("room"); - let _first = handle.verify_request(token(b"jwt", 0), None, RequestKind::PublishNamespace, &room); - let _repeat = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); - let request = requests.next().await.unwrap(); - assert_eq!(request.kind(), Some(RequestKind::PublishNamespace)); - assert_eq!(request.path(), Some(&room)); - assert!( - requests.next().now_or_never().is_none(), - "a repeated token verified again" - ); - - let auth = Request::new(Bytes::from_static(b"auth"), Default::default()); - assert_eq!(auth.kind(), None); - assert_eq!(auth.path(), None); - - // Each gate names the request it admits. - for (gate, kind) in [ - ( - Gate::new(handle.clone(), room.clone(), Direction::Publish), - RequestKind::Subscribe, - ), - ( - Gate::new(handle.clone(), room.clone(), Direction::Publish).discovery(), - RequestKind::Discover, - ), - ( - Gate::new(handle.clone(), room.clone(), Direction::Subscribe), - RequestKind::PublishNamespace, - ), - ] { - let _verdict = gate.verify(token(format!("{kind:?}").as_bytes(), 0), None); - let request = requests.next().await.unwrap(); - assert_eq!(request.kind(), Some(kind)); - assert_eq!(request.path(), Some(&room)); - } - } - /// A gate its token bound verifies another token as that grant's renewal: one the acceptor /// granted takes over, rather than the old grant lapsing before the gate admits it. #[kio::test] @@ -3628,7 +3541,7 @@ mod request_token_tests { } let handle = Link::new(true); let mut requests = handle.handle().requests(); - let first = handle.verify_request(token(b"warm", 0), None, RequestKind::Subscribe, &subscribe_path()); + let first = handle.verify_request(token(b"warm", 0), None); let _first = requests.next().await.unwrap().accept(Grant::all()); first.grant().await.unwrap(); @@ -3636,8 +3549,8 @@ mod request_token_tests { let waiter = kio::Waiter::new(std::task::Waker::from(woken.clone())); let mut epoch = 0; while handle.poll_permit(Direction::Publish, &mut epoch, &waiter).is_ready() {} - let second = handle.verify_request(token(b"shared", 0), None, RequestKind::Subscribe, &subscribe_path()); - let third = handle.verify_request(token(b"shared", 0), None, RequestKind::Subscribe, &subscribe_path()); + let second = handle.verify_request(token(b"shared", 0), None); + let third = handle.verify_request(token(b"shared", 0), None); drop((second, third)); assert!( !woken.0.load(std::sync::atomic::Ordering::SeqCst), @@ -3650,12 +3563,12 @@ mod request_token_tests { let handle = Handle::new(); let first = Link::attach(&handle, false, true).unwrap(); let mut requests = handle.requests(); - let verdict = first.verify_request(token(b"token", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = first.verify_request(token(b"token", 0), None); let issued = requests.next().await.unwrap().accept(Grant::all()); verdict.grant().await.unwrap(); first.close(Error::Cancel); let second = Link::attach(&handle, false, true).unwrap(); - let next = second.verify_request(token(b"token", 0), None, RequestKind::Subscribe, &subscribe_path()); + let next = second.verify_request(token(b"token", 0), None); let _next = requests.next().await.unwrap().accept(Grant::default()); assert_eq!(next.grant().await.unwrap(), Grant::default()); assert!(matches!(issued.closed().await, Error::Cancel)); @@ -3667,7 +3580,7 @@ mod request_token_tests { async fn a_typed_request_token_is_unsupported() { let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"cat", 1), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"cat", 1), None); let err = verdict.grant().await.expect_err("a typed token"); assert!(matches!(err.error, Error::Unsupported), "{err:?}"); assert!( @@ -3682,12 +3595,7 @@ mod request_token_tests { fn an_empty_request_token_is_unsupported() { let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request( - token(b"", crate::setup::Token::OUT_OF_BAND), - None, - RequestKind::Subscribe, - &subscribe_path(), - ); + let verdict = handle.verify_request(token(b"", crate::setup::Token::OUT_OF_BAND), None); assert!(matches!( verdict.poll_grant(&kio::Waiter::noop()), Poll::Ready(Err(Refusal { @@ -3710,7 +3618,7 @@ mod request_token_tests { let handle = Link::new(false); let mut requests = handle.handle().requests(); handle.unsupported(); - let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"jwt", 0), None); let request = requests.next().await.expect("a request reaches the acceptor"); let _issued = request.accept(Grant::all()); assert!( @@ -3724,7 +3632,7 @@ mod request_token_tests { #[kio::test] async fn no_consumer_refuses_a_request_token_as_unsupported() { let handle = Link::new(true); - let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"jwt", 0), None); let err = verdict.grant().await.expect_err("no verifier"); assert!(matches!(err.error, Error::Unsupported), "{err:?}"); } @@ -3735,7 +3643,7 @@ mod request_token_tests { async fn a_refused_request_token_is_unauthorized() { let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"jwt", 0), None); requests.next().await.unwrap().reject(SessionError::Unauthorized, "no"); let err = verdict.grant().await.expect_err("refused"); assert!( @@ -3751,14 +3659,14 @@ mod request_token_tests { let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"jwt", 0), None); let issued = requests.next().await.unwrap().accept(Grant::all()); issued.revoke(SessionError::Unauthorized, "revoked"); let err = verdict.grant().await.expect_err("revoked before admission"); assert_eq!(err.reason, "revoked"); drop(verdict); - let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"jwt", 0), None); drop(requests.next().await.unwrap().accept(Grant::all())); let err = verdict.grant().await.expect_err("dropped before admission"); assert!( @@ -3767,7 +3675,7 @@ mod request_token_tests { ); drop(verdict); - let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"jwt", 0), None); let issued = requests.next().await.unwrap().accept(Grant::all()); issued.update(Grant::default()); let grant = verdict.grant().await.expect("still granted"); @@ -3783,7 +3691,7 @@ mod request_token_tests { async fn dropping_the_verdict_ends_the_grant() { let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"jwt", 0), None); let issued = requests.next().await.unwrap().accept(Grant::all()); verdict.grant().await.expect("granted"); drop(verdict); @@ -3797,7 +3705,7 @@ mod request_token_tests { async fn a_request_token_never_joins_the_union() { let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"jwt", 0), None); let _issued = requests.next().await.unwrap().accept(Grant::all()); verdict.grant().await.expect("granted"); // The union only reflects tokens presented on this side (none here), never a @@ -3828,7 +3736,7 @@ mod request_token_tests { assert!(!handle.allows(Direction::Publish, "room/bob")); // A token on a request admits that request, and nothing else. - let verdict = handle.verify_request(token(b"request", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"request", 0), None); let _request = requests.next().await.unwrap().accept(subscribe_only("room/bob")); verdict.grant().await.expect("granted"); assert!( @@ -3916,7 +3824,7 @@ mod request_token_tests { let runtime = crate::time::sim(); let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"jwt", 0), None); let grant = grant_in(&runtime, 60); let expires = grant.expires; let _issued = requests.next().await.unwrap().accept(grant); @@ -3946,7 +3854,7 @@ mod request_token_tests { ..grant_in(&runtime, 0) }; - let verdict = handle.verify_request(token(b"old", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"old", 0), None); let _issued = requests.next().await.unwrap().accept(expired()); let answered = verdict.grant().await; let refused = RequestGrant::new( @@ -3962,7 +3870,7 @@ mod request_token_tests { "an expired grant admits nothing" ); - let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"jwt", 0), None); let _issued = requests.next().await.unwrap().accept(grant_in(&runtime, 60)); let answered = verdict.grant().await; let request_grant = RequestGrant::new( @@ -3974,7 +3882,7 @@ mod request_token_tests { None, ) .unwrap(); - let renewal = handle.verify_request(token(b"old2", 0), None, RequestKind::Subscribe, &subscribe_path()); + let renewal = handle.verify_request(token(b"old2", 0), None); let _issued = requests.next().await.unwrap().accept(expired()); let renewed = renewal.grant().await; assert!( @@ -3990,7 +3898,7 @@ mod request_token_tests { let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"jwt", 0), None); let first = grant_in(&runtime, 60); let first_expires = first.expires; let _issued = requests.next().await.unwrap().accept(first); @@ -4006,7 +3914,7 @@ mod request_token_tests { .unwrap(); // A REQUEST_UPDATE carrying a fresh token the acceptor accepts with a later expiry. - let renewal = handle.verify_request(token(b"jwt2", 0), None, RequestKind::Subscribe, &subscribe_path()); + let renewal = handle.verify_request(token(b"jwt2", 0), None); let second = grant_in(&runtime, 600); let second_expires = second.expires; let _issued2 = requests.next().await.unwrap().accept(second); @@ -4027,7 +3935,7 @@ mod request_token_tests { let runtime = crate::time::sim(); let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"jwt", 0), None); // Held for the request's life, so only the deadline (not a drop) ends it. let _issued = requests.next().await.unwrap().accept(grant_in(&runtime, 60)); let answered = verdict.grant().await.unwrap(); @@ -4055,7 +3963,7 @@ mod request_token_tests { let runtime = crate::time::sim(); let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"jwt", 0), None); let issued = requests.next().await.unwrap().accept(grant_in(&runtime, 60)); let answered = verdict.grant().await.unwrap(); let request_grant = RequestGrant::new( @@ -4091,7 +3999,7 @@ mod request_token_tests { let runtime = crate::time::sim(); let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"jwt", 0), None); let _issued = requests.next().await.unwrap().accept(grant_in(&runtime, 60)); let answered = verdict.grant().await.unwrap(); let request_grant = RequestGrant::new( @@ -4129,7 +4037,7 @@ mod request_token_tests { let runtime = crate::time::sim(); let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"jwt", 0), None); let first = grant_in(&runtime, 60); let first_expires = first.expires; let _issued = requests.next().await.unwrap().accept(first); @@ -4145,7 +4053,7 @@ mod request_token_tests { .unwrap(); // The renewal token is refused, so it renews nothing and the old grant is untouched. - let renewal = handle.verify_request(token(b"bad", 0), None, RequestKind::Subscribe, &subscribe_path()); + let renewal = handle.verify_request(token(b"bad", 0), None); requests .next() .await @@ -4175,7 +4083,7 @@ mod request_token_tests { let runtime = crate::time::sim(); let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"jwt", 0), None); let issued = requests.next().await.unwrap().accept(Grant::all()); let answered = verdict.grant().await.unwrap(); let request_grant = RequestGrant::new( @@ -4258,7 +4166,7 @@ mod request_token_tests { let runtime = crate::time::sim(); let handle = Link::new(true); let mut requests = handle.handle().requests(); - let verdict = handle.verify_request(token(b"jwt", 0), None, RequestKind::Subscribe, &subscribe_path()); + let verdict = handle.verify_request(token(b"jwt", 0), None); // A grant that never expires, so only the revoke can end the request. let issued = requests.next().await.unwrap().accept(Grant::all()); let answered = verdict.grant().await.unwrap(); diff --git a/rs/moq-net/src/auth/tokens.rs b/rs/moq-net/src/auth/tokens.rs index 4bf94415663..a62344de031 100644 --- a/rs/moq-net/src/auth/tokens.rs +++ b/rs/moq-net/src/auth/tokens.rs @@ -67,9 +67,8 @@ pub struct EmptyToken; /// ones on the live session at once, withdrawing the ones they replace only after every new /// one has its answer, so the grant never narrows in between. /// -/// A reconnect loop builds each session on [`handle`](Self::handle), calls -/// [`dial`](Self::dial) before each dial and [`connected`](Self::connected) once it -/// succeeds, and aborts the handle when it gives up. +/// [`Reconnect::with_tokens`](crate::Reconnect::with_tokens) presents them on every dial, +/// whose sessions are built on [`handle`](Self::handle); aborting the handle ends the policy. /// /// Cheap to clone; every clone shares one set. #[derive(Clone)] @@ -78,13 +77,13 @@ pub struct Tokens { } /// What one dial presents, from [`Tokens::dial`]. -pub struct Dial { +pub(crate) struct Dial { /// The URL to dial, carrying the connection credential in its query when an offered /// version has no SETUP token. - pub url: Url, + pub(crate) url: Url, /// The connection credential for [`Client::with_token`](crate::Client::with_token), unless /// the URL brought its own. - pub token: Option, + pub(crate) token: Option, generation: u64, } @@ -161,7 +160,7 @@ impl Tokens { /// Decide what a dial to `url` offering `versions` presents, and hold every other token in /// band on the session it builds. Call it before each dial. - pub fn dial(&self, url: &Url, versions: &crate::Versions) -> Dial { + pub(crate) fn dial(&self, url: &Url, versions: &crate::Versions) -> Dial { let mut state = self.handle.state.lock(); state.record(); let configured = state.configured.as_mut().expect("built by Tokens::new"); @@ -182,7 +181,7 @@ impl Tokens { /// connected, whose own answer to the connection credential then counts. A session built /// on another handle answers nothing here. #[cfg(feature = "async")] - pub fn connected(&self, session: &crate::Session) { + pub(crate) fn connected(&self, session: &crate::Session) { if session.auth().state.same_channel(&self.handle.state) { self.connect(session.credential()); } @@ -209,7 +208,7 @@ impl Tokens { /// Whether the tokens changed since `dial` read them, so a refusal of its credential earns /// another dial. - pub fn changed(&self, dial: &Dial) -> bool { + pub(crate) fn changed(&self, dial: &Dial) -> bool { let state = self.handle.state.read(); state.configured.as_ref().expect("built by Tokens::new").generation != dial.generation } diff --git a/rs/moq-net/src/error.rs b/rs/moq-net/src/error.rs index e2edb909549..661fdd9b550 100644 --- a/rs/moq-net/src/error.rs +++ b/rs/moq-net/src/error.rs @@ -371,13 +371,9 @@ pub enum Error { #[error("broadcast path contains a navigation segment")] NavigationSegment, - /// A path was spelled with a leading, trailing, or doubled `/`. - #[error("path contains an empty segment")] - EmptySegment, - - /// A path was spelled with a `\` before anything but `\` or `/`. - #[error(r"path contains a `\` that escapes neither `\` nor `/`")] - InvalidEscape, + /// A string is not a valid path, such as one with an empty segment. + #[error(transparent)] + Path(crate::path::Error), /// A duplicate ID was used // The broadcast/track is a duplicate @@ -689,8 +685,7 @@ impl From<&Error> for SessionError { | Error::BoundsExceeded(_) | Error::InvalidPath(_) | Error::NavigationSegment - | Error::EmptySegment - | Error::InvalidEscape => Self::ProtocolViolation, + | Error::Path(_) => Self::ProtocolViolation, Error::App(app) => Self::App(*app), // A code we did not recognize, so we cannot say which space it came from. // Forwarding it into this one risks landing on a value that IS registered here @@ -753,8 +748,7 @@ impl From<&Error> for StreamError { | Error::BoundsExceeded(_) | Error::InvalidPath(_) | Error::NavigationSegment - | Error::EmptySegment - | Error::InvalidEscape + | Error::Path(_) | Error::MalformedTrack => Self::MalformedTrack, Error::App(app) => Self::App(*app), // See the SessionError impl: an unregistered code carries no registry, so diff --git a/rs/moq-net/src/ietf/datagram.rs b/rs/moq-net/src/ietf/datagram.rs index 92cca7711ba..28ad161a0dc 100644 --- a/rs/moq-net/src/ietf/datagram.rs +++ b/rs/moq-net/src/ietf/datagram.rs @@ -1,6 +1,6 @@ //! OBJECT_DATAGRAM: one Object carried in a QUIC datagram (draft-22 section 11.2.1). //! -//! Public [`crate::Datagram`] writers emit Object ID 0; private relay metadata preserves +//! Public [`crate::datagram::Datagram`] writers emit Object ID 0; private relay metadata preserves //! other IDs, properties, and status. The Type is a set of flags. use bytes::Bytes; diff --git a/rs/moq-net/src/ietf/session.rs b/rs/moq-net/src/ietf/session.rs index d06824d729b..68551d383cb 100644 --- a/rs/moq-net/src/ietf/session.rs +++ b/rs/moq-net/src/ietf/session.rs @@ -2803,11 +2803,10 @@ mod tests { let session = auth_session(false); let (handle, log) = (&session.handle, &session.log); - let (auth, answer) = kio::clock::timeout(std::time::Duration::from_secs(1), handle.handle().add("token")) + let auth = kio::clock::timeout(std::time::Duration::from_secs(1), handle.handle().add("token")) .await .expect("resolved promptly") .expect("kept for requests"); - assert_eq!(answer, crate::auth::Answer::Requests); assert_eq!(auth.grant().peek(), None, "no grant without the extension"); assert_eq!(handle.handle().grant().peek(), None, "no grant without the extension"); diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index 3b8eb9ccf88..32988e220bf 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -11825,12 +11825,7 @@ mod tests { kind: crate::setup::Token::OUT_OF_BAND, value: vec![0x00, 0xff], }; - let verdict = auth.verify_request( - token, - Some(hold.clone()), - crate::auth::RequestKind::PublishNamespace, - &path, - ); + let verdict = auth.verify_request(token, Some(hold.clone())); let _issued = requests.next().await.unwrap().accept(crate::auth::Grant::all()); let answered = verdict.grant().await; let grant = crate::auth::RequestGrant::new( diff --git a/rs/moq-net/src/ietf/subscriber/tracks.rs b/rs/moq-net/src/ietf/subscriber/tracks.rs index 3bb5bf62d75..fc10bd33974 100644 --- a/rs/moq-net/src/ietf/subscriber/tracks.rs +++ b/rs/moq-net/src/ietf/subscriber/tracks.rs @@ -1586,12 +1586,7 @@ impl ServePublish { Some(token) => { let credit = kio::Producer::<()>::default(); let hold: crate::auth::Hold = std::sync::Arc::new(credit.consume()); - let verdict = this.auth.verify_request( - token.0, - Some(hold), - crate::auth::RequestKind::Publish, - &msg.track_namespace, - ); + let verdict = this.auth.verify_request(token.0, Some(hold)); ServePublishState::Verifying { stream, msg, diff --git a/rs/moq-net/src/lite/datagram.rs b/rs/moq-net/src/lite/datagram.rs index 349f6c3d870..f4ace452d76 100644 --- a/rs/moq-net/src/lite/datagram.rs +++ b/rs/moq-net/src/lite/datagram.rs @@ -9,7 +9,7 @@ //! On lite-05/06 a body is a single-frame group on a SUBSCRIBE subscription, its sequence //! the group's. On lite-07 it is a datagram of a datagram track, subscribed through a //! Datagrams Stream ([`Datagrams`]), its sequence the datagram track's own. The model -//! counterpart is [`crate::Datagram`]. +//! counterpart is [`crate::datagram::Datagram`]. use std::borrow::Cow; diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index b4f12a640dc..9f2b3722501 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -2946,7 +2946,7 @@ impl DatagramSend { /// Send one datagram over a QUIC datagram, newest first within the subscription. It is /// dropped, as a datagram may be anywhere, if its body doesn't fit the transport's /// datagram size or the transport has no room for it now. - fn send(&self, datagram: crate::Datagram) { + fn send(&self, datagram: crate::datagram::Datagram) { let Ok(timestamp) = self .stamp .map(|stamp| stamp.wire(datagram.timestamp, &self.runtime)) @@ -7375,7 +7375,7 @@ mod serve_group_test { let log = session.log.clone(); // Lite-07's type adds one byte to the three one-byte fields. let header = if version.has_padding() { 4 } else { 3 }; - datagram_send(version, session).send(crate::Datagram { + datagram_send(version, session).send(crate::datagram::Datagram { sequence: 1, timestamp: Some(Timestamp::ZERO), payload: bytes::Bytes::from(vec![0; size - header]), @@ -7397,7 +7397,7 @@ mod serve_group_test { let log = session.log.clone(); let send = datagram_send(Version::Lite07, session); for sequence in [4, 9] { - send.send(crate::Datagram { + send.send(crate::datagram::Datagram { sequence, timestamp: Some(Timestamp::ZERO), payload: bytes::Bytes::from_static(b"d"), @@ -7411,7 +7411,7 @@ mod serve_group_test { // Without send groups, the rank goes alone. let session = SinkSession::new(Log::default()).with_datagrams(1200); let log = session.log.clone(); - datagram_send(Version::Lite07, session).send(crate::Datagram { + datagram_send(Version::Lite07, session).send(crate::datagram::Datagram { sequence: 4, timestamp: Some(Timestamp::ZERO), payload: bytes::Bytes::from_static(b"d"), diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 58f24509804..cf1778ce298 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -926,7 +926,7 @@ impl Subscriber { .map(|(value, scale)| Timestamp::new(value, scale)) .transpose() .map_err(|_| Error::BoundsExceeded(crate::coding::BoundsExceeded))?; - producer.insert(crate::Datagram { + producer.insert(crate::datagram::Datagram { sequence: dg.sequence, timestamp, payload: dg.payload, diff --git a/rs/moq-net/src/model/broadcast.rs b/rs/moq-net/src/model/broadcast.rs index 2c7eff33f71..36a45e63326 100644 --- a/rs/moq-net/src/model/broadcast.rs +++ b/rs/moq-net/src/model/broadcast.rs @@ -2492,7 +2492,7 @@ mod test { producer.close(); } - fn recv_datagram(consumer: &mut datagram::Consumer) -> Poll, Error>> { + fn recv_datagram(consumer: &mut datagram::Consumer) -> Poll, Error>> { consumer.poll_recv(&kio::Waiter::noop()) } diff --git a/rs/moq-net/src/model/datagram.rs b/rs/moq-net/src/model/datagram.rs index 08055a35759..2f7525a8551 100644 --- a/rs/moq-net/src/model/datagram.rs +++ b/rs/moq-net/src/model/datagram.rs @@ -119,7 +119,8 @@ impl Info { } /// Produce a standalone datagram track with this info, outside any broadcast. - pub fn produce(self, name: impl Into>) -> Producer { + #[cfg(test)] + pub(crate) fn produce(self, name: impl Into>) -> Producer { Producer::new(name.into(), Some(self)) } } @@ -378,7 +379,8 @@ impl Producer { } /// Whether this is the same track as `other`. - pub fn is_clone(&self, other: &Self) -> bool { + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn is_clone(&self, other: &Self) -> bool { self.state.same_channel(&other.state) } } @@ -600,7 +602,8 @@ impl Consumer { } /// Whether this reads the same track as `other`. - pub fn is_clone(&self, other: &Self) -> bool { + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn is_clone(&self, other: &Self) -> bool { self.state.same_channel(&other.state) } } diff --git a/rs/moq-net/src/model/mod.rs b/rs/moq-net/src/model/mod.rs index afb192b3e76..cf3d12ce01b 100644 --- a/rs/moq-net/src/model/mod.rs +++ b/rs/moq-net/src/model/mod.rs @@ -47,8 +47,6 @@ use request_queue::RequestQueue; use weak_cache::{WeakCache, WeakEntry}; pub use bytes::*; -// The payload type stays flat at the crate root beside `Timestamp`. -pub use datagram::Datagram; pub use time::*; pub use timed::Timed; diff --git a/rs/moq-net/src/model/socket.rs b/rs/moq-net/src/model/socket.rs index 06e7e53a912..2c3aa434e5e 100644 --- a/rs/moq-net/src/model/socket.rs +++ b/rs/moq-net/src/model/socket.rs @@ -1050,11 +1050,6 @@ pub struct Dialing { } impl Dialing { - /// The session's id. - pub fn session(&self) -> &str { - &self.session - } - /// Poll for the peer to accept; see [`Endpoint::dial_tcp`]. pub fn poll_ok(&mut self, waiter: &kio::Waiter) -> Poll> { let result = self.poll_inner(waiter); diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 0ee302943ad..ed8c67724b9 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -4181,7 +4181,8 @@ impl Producer { reason = "return the owned producer without allocating on an idle check" ) )] - pub fn abort_unwanted(self, err: Error) -> std::result::Result<(), Self> { + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn abort_unwanted(self, err: Error) -> std::result::Result<(), Self> { let wanted = match self.state.write_unwanted() { kio::Unwanted::Idle(guard) => { commit_abort(guard, err).abort(); diff --git a/rs/moq-net/src/path/mod.rs b/rs/moq-net/src/path/mod.rs index ec65dd5b53a..12ababc0e8d 100644 --- a/rs/moq-net/src/path/mod.rs +++ b/rs/moq-net/src/path/mod.rs @@ -651,8 +651,7 @@ impl From for crate::Error { fn from(err: Error) -> Self { match err { Error::TooLong => crate::Error::BoundsExceeded(crate::BoundsExceeded), - Error::EmptySegment => crate::Error::EmptySegment, - Error::InvalidEscape => crate::Error::InvalidEscape, + err => crate::Error::Path(err), } } } diff --git a/rs/moq-net/src/server.rs b/rs/moq-net/src/server.rs index ef461cae36e..040d16c0c2c 100644 --- a/rs/moq-net/src/server.rs +++ b/rs/moq-net/src/server.rs @@ -1812,8 +1812,6 @@ mod tests { value: b"jwt".to_vec(), }, None, - crate::auth::RequestKind::Subscribe, - &crate::Path::from_static("room"), ); let received = requests.next().await.expect("the pre-ok consumer receives the request"); assert_eq!(received.token().as_ref(), b"jwt", "{version:?}"); diff --git a/rs/moq-net/src/session.rs b/rs/moq-net/src/session.rs index 339cee8cf6e..de31bf97706 100644 --- a/rs/moq-net/src/session.rs +++ b/rs/moq-net/src/session.rs @@ -414,13 +414,12 @@ impl Session { self.goaway.consumer() } - /// Resolves once the session carries no subscription or fetch, in either direction. + /// Ready once the session carries no subscription or fetch, in either direction. /// /// Ready at once while it carries none. A track request still being set up, waiting /// on authorization or the track's info, counts too. Announcements and the other /// control streams don't, so a session can be idle while it still serves discovery. - #[cfg_attr(feature = "async", kio::wrapper)] - pub fn poll_idle(&self, waiter: &kio::Waiter) -> Poll<()> { + pub(crate) fn poll_idle(&self, waiter: &kio::Waiter) -> Poll<()> { self.work.poll_idle(waiter) } @@ -1147,3 +1146,6 @@ impl Drop for Slot { self.0.fetch_sub(1, std::sync::atomic::Ordering::AcqRel); } } + +#[cfg(all(test, feature = "async"))] +mod tests; diff --git a/rs/moq-net/src/session/tests.rs b/rs/moq-net/src/session/tests.rs new file mode 100644 index 00000000000..ad3c551c322 --- /dev/null +++ b/rs/moq-net/src/session/tests.rs @@ -0,0 +1,265 @@ +//! [`Session::poll_idle`] reports a session that carries no subscription or fetch in either +//! direction, which is when a migrated session can close without cutting anything off. +//! +//! Deterministic: simulated time over the in-memory transport. + +use std::time::Duration; + +use crate::model::ProduceTest; +use crate::{Session, Timestamp, Version}; + +/// A millisecond track: the frames written here carry timestamps. +fn timed() -> crate::track::Info { + crate::track::Info::default().with_timescale(crate::Timescale::MILLI) +} + +const TIMEOUT: Duration = Duration::from_secs(10); + +fn origin(hop: u64) -> crate::origin::Producer { + crate::origin::Config::new(std::num::NonZeroU64::new(hop).unwrap()).produce() +} + +fn idle(session: &Session) -> bool { + session.poll_idle(&kio::Waiter::noop()).is_ready() +} + +/// Wait for `session` to go idle, naming `what` if it never does. +async fn settles(session: &Session, what: &str) { + kio::clock::timeout(TIMEOUT, kio::wait(|waiter| session.poll_idle(waiter))) + .await + .unwrap_or_else(|_| panic!("{what}")); +} + +/// The client and server sessions of one in-memory connection. +struct Pair { + client: Session, + server: Session, + /// The client's end of the transport, for sending raw datagrams. + client_transport: crate::transport::memory::Session, +} + +/// A client publishing one announced track to a server that subscribes to whatever it hears. +struct Setup { + pair: Pair, + track: crate::track::Producer, + remote: crate::broadcast::Consumer, + broadcast: crate::origin::Route, +} + +async fn setup(version: &str) -> Setup { + let version: Version = version.parse().unwrap(); + let publisher = origin(1); + let broadcast = publisher.route("bcast", crate::origin::RouteOptions::hidden()).unwrap(); + let track = broadcast.broadcast().create_track("video", timed()).unwrap(); + broadcast.announce().unwrap(); + + let subscriber = origin(2); + let (client, server) = crate::transport::memory::pair(crate::transport::memory::Config { + protocol: Some(version.alpn().to_string()), + ..Default::default() + }); + let client_transport = client.clone(); + let dial = crate::Client::new() + .with_versions(version.into()) + .with_publisher(publisher.consume()); + let serve = crate::Server::new() + .with_versions(version.into()) + .with_subscriber(subscriber.clone()); + let (client, server) = futures::join!( + async { + let (session, driver) = dial.connect(kio::clock::now(), client).await.unwrap(); + drop(kio::clock::sim::spawn(crate::time::run(driver))); + session + }, + async { + let (session, driver) = serve.accept(kio::clock::now(), server).await.unwrap(); + drop(kio::clock::sim::spawn(crate::time::run(driver))); + session + }, + ); + + let remote = kio::clock::timeout(TIMEOUT, subscriber.consume().follow("bcast", None).unwrap().available()) + .await + .expect("resolve timeout") + .expect("broadcast resolves") + .unwrap(); + + Setup { + pair: Pair { + client, + server, + client_transport, + }, + track, + remote, + broadcast, + } +} + +/// An announcement alone leaves both ends idle. A subscription keeps the end serving it +/// and the end holding it busy until the reader leaves. +#[kio::test] +async fn a_subscription_keeps_both_ends_busy() { + for version in [ + "moq-lite-04", + "moq-lite-06", + "moq-lite-07-wip", + "moq-transport-18", + "moq-transport-22", + ] { + let Setup { + pair, + track, + remote, + broadcast: _broadcast, + } = setup(version).await; + assert!(idle(&pair.client), "{version}: an announcement kept the publisher busy"); + assert!( + idle(&pair.server), + "{version}: an announcement kept the subscriber busy" + ); + + let mut group = track.append_group().unwrap(); + group.write_frame(Timestamp::ZERO, b"live".as_ref()).unwrap(); + group.finish().unwrap(); + let mut sub = kio::clock::timeout(TIMEOUT, remote.track("video").unwrap().subscribe(None)) + .await + .expect("subscribe timeout") + .expect("subscribe"); + let received = kio::clock::timeout(TIMEOUT, sub.recv_group()) + .await + .expect("recv timeout") + .expect("recv") + .expect("the track ended"); + assert_eq!(received.sequence, 0, "{version}"); + assert!(!idle(&pair.client), "{version}: idle while serving a subscription"); + assert!(!idle(&pair.server), "{version}: idle while holding a subscription"); + + drop((received, sub)); + settles( + &pair.server, + &format!("{version}: the subscriber stayed busy after the reader left"), + ) + .await; + settles( + &pair.client, + &format!("{version}: the publisher stayed busy after the reader left"), + ) + .await; + } +} + +/// A fetch keeps both ends busy until it ends: here, until the group it reads finishes. +/// moq-transport refuses a FETCH of the whole live group rather than waiting on it. +#[kio::test] +async fn a_fetch_keeps_both_ends_busy() { + for version in ["moq-lite-06", "moq-lite-07-wip"] { + let Setup { + pair, + track, + remote, + broadcast: _broadcast, + } = setup(version).await; + + let mut writing = track.append_group().unwrap(); + writing.write_frame(Timestamp::ZERO, b"first".as_ref()).unwrap(); + let fetching = remote.track("video").unwrap(); + let fetch = kio::clock::sim::spawn(async move { + let mut fetched = fetching.fetch_group(0, None).await?; + let mut frames = Vec::new(); + while let Some(frame) = fetched.read_frame().await? { + frames.push(frame.payload); + } + Ok::<_, crate::Error>(frames) + }); + // Simulated: every task runs until the fetch waits on the unfinished group. + kio::clock::sleep(Duration::from_secs(1)).await; + assert!(!idle(&pair.client), "{version}: idle while serving a fetch"); + assert!(!idle(&pair.server), "{version}: idle while a fetch is in flight"); + + writing.write_frame(Timestamp::ZERO, b"last".as_ref()).unwrap(); + writing.finish().unwrap(); + let frames = kio::clock::timeout(TIMEOUT, fetch) + .await + .expect("fetch timeout") + .expect("fetch task") + .expect("fetch"); + assert_eq!(frames, [&b"first"[..], &b"last"[..]], "{version}"); + settles( + &pair.server, + &format!("{version}: the fetching side stayed busy after the fetch"), + ) + .await; + settles( + &pair.client, + &format!("{version}: the serving side stayed busy after the fetch"), + ) + .await; + } +} + +/// A track's end waits for the subscriber to read the datagrams queued with it, so a flood +/// that keeps its receive queue full delays the end. Meanwhile the track keeps its stream, and +/// the session stays busy, so the peer's stream credit bounds the ends a flood can leave queued. +#[kio::test] +async fn a_track_ended_under_a_datagram_flood_keeps_its_stream() { + use crate::transport::poll::Session as _; + + for version in ["moq-lite-05", "moq-lite-06", "moq-lite-07-wip"] { + let Setup { + pair, + remote, + broadcast, + .. + } = setup(version).await; + let info = crate::datagram::Info::default().with_timescale(crate::Timescale::MILLI); + let producer = broadcast.broadcast().create_datagrams("audio", info).unwrap(); + let mut consumer = remote.datagrams(producer.name()).unwrap(); + // The consumer takes a priority first, so the publisher seeing it can't be anything but + // this consumer's request. + consumer.set_priority(1); + kio::clock::timeout(TIMEOUT, async { + let mut priority = producer.priority(); + while priority != Some(1) { + priority = kio::wait(|waiter| producer.poll_priority(waiter, priority)).await; + } + }) + .await + .expect("the request never reached the publisher"); + kio::clock::timeout(TIMEOUT, consumer.info()) + .await + .expect("info timeout") + .expect("the publisher answered"); + + // Each names no subscription, so the subscriber reads and drops it. The queue holds 1024. + let flood = || { + for _ in 0..512 { + let _ = pair.client_transport.send_datagram(b"x"[..].into(), Default::default()); + } + }; + flood(); + producer.finish(); + for _ in 0..32 { + flood(); + // One turn for the session's tasks, which read at most a budget each. + let mut turned = false; + kio::wait(|waiter| { + if std::mem::replace(&mut turned, true) { + return std::task::Poll::Ready(()); + } + waiter.waker().wake_by_ref(); + std::task::Poll::Pending + }) + .await; + assert!( + !idle(&pair.server), + "{version}: the track released its stream before its end applied" + ); + } + let end = kio::clock::timeout(TIMEOUT, consumer.recv()) + .await + .expect("recv timeout"); + assert!(end.unwrap().is_none(), "{version}"); + settles(&pair.server, &format!("{version}: the track never released its stream")).await; + } +} diff --git a/rs/moq-net/src/stats.rs b/rs/moq-net/src/stats.rs index 5f11194f7b8..9d958334e07 100644 --- a/rs/moq-net/src/stats.rs +++ b/rs/moq-net/src/stats.rs @@ -824,7 +824,7 @@ impl Presence { } /// Connected sessions right now, split by whether media payload moved through them -/// within the last [`Activity::WINDOW`], in either direction. +/// within the last 10 seconds, in either direction. /// /// A gauge, not a counter. A session that only holds announces, or subscriptions /// with nothing flowing, is idle: it costs keepalives and control traffic, not @@ -841,7 +841,7 @@ pub struct Activity { impl Activity { /// How recently payload must have moved for a session to count as active. - pub const WINDOW: std::time::Duration = std::time::Duration::from_secs(10); + const WINDOW: std::time::Duration = std::time::Duration::from_secs(10); fn add(&mut self, other: Self) { self.idle += other.idle; diff --git a/rs/moq-net/src/version.rs b/rs/moq-net/src/version.rs index 9df3014994d..a4c9d96b6a8 100644 --- a/rs/moq-net/src/version.rs +++ b/rs/moq-net/src/version.rs @@ -83,7 +83,7 @@ impl Version { /// Whether the client's SETUP can carry its connection credential (see /// `Client::with_token`, behind the `async` feature). Every moq-transport draft can; /// moq-lite gained it in lite-07, so older versions need the credential in the URL. - pub fn has_setup_token(self) -> bool { + pub(crate) fn has_setup_token(self) -> bool { match self { Self::Lite(version) => version.has_auth(), Self::Ietf(_) => true, diff --git a/rs/moq-net/tests/integration/auth.rs b/rs/moq-net/tests/integration/auth.rs index 2aed7b3c6eb..f20b098213d 100644 --- a/rs/moq-net/tests/integration/auth.rs +++ b/rs/moq-net/tests/integration/auth.rs @@ -141,8 +141,7 @@ async fn moqt_22_renewing_a_cancelled_announce_keeps_the_session() { within(async { let (mut pair, client_origin, _server_origin) = connect_route_auth(MOQT_22).await; let mut tokens = serve(pair.requests.take().unwrap(), |_| Some(Grant::all())); - let (first, answer) = pair.client.auth().add("first").await.expect("kept for requests"); - assert_eq!(answer, auth::Answer::Requests); + let first = pair.client.auth().add("first").await.expect("kept for requests"); let broadcast = client_origin .route("room/x", origin::RouteOptions::default().with_auth(first)) .unwrap(); @@ -155,7 +154,7 @@ async fn moqt_22_renewing_a_cancelled_announce_keeps_the_session() { let (token, _again) = tokens.next().await.expect("offered again after the cancel"); assert_eq!(token, b"first"); kio::clock::sleep(Duration::from_secs(1)).await; - let (second, _) = pair.client.auth().add("second").await.expect("kept for requests"); + let second = pair.client.auth().add("second").await.expect("kept for requests"); broadcast.set_auth(Some(second)).unwrap(); let (token, _second) = tokens.next().await.expect("announced again"); assert_eq!(token, b"second"); @@ -174,13 +173,13 @@ async fn moqt_22_a_cancelled_renewal_keeps_the_session() { let mut tokens = serve(pair.requests.take().unwrap(), |token| { (token == b"first").then(Grant::all) }); - let (first, _) = pair.client.auth().add("first").await.expect("kept for requests"); + let first = pair.client.auth().add("first").await.expect("kept for requests"); let broadcast = client_origin .route("room/x", origin::RouteOptions::default().with_auth(first)) .unwrap(); let (_, first) = tokens.next().await.expect("the announce's token"); - let (second, _) = pair.client.auth().add("second").await.expect("kept for requests"); + let second = pair.client.auth().add("second").await.expect("kept for requests"); broadcast.set_auth(Some(second)).unwrap(); kio::clock::sleep(Duration::from_secs(1)).await; drop(first); @@ -199,7 +198,7 @@ async fn moqt_22_dropping_the_old_grant_with_its_renewal_keeps_the_announce() { within(async { let (mut pair, client_origin, server_origin) = connect_route_auth(MOQT_22).await; let mut requests = pair.requests.take().unwrap(); - let (first, _) = pair.client.auth().add("first").await.expect("kept for requests"); + let first = pair.client.auth().add("first").await.expect("kept for requests"); let broadcast = client_origin .route("room/x", origin::RouteOptions::default().with_auth(first)) .unwrap(); @@ -210,7 +209,7 @@ async fn moqt_22_dropping_the_old_grant_with_its_renewal_keeps_the_announce() { .accept(Grant::all()); wait_announced(&server_origin.consume(), "room/x", true).await; - let (second, _) = pair.client.auth().add("second").await.expect("kept for requests"); + let second = pair.client.auth().add("second").await.expect("kept for requests"); broadcast.set_auth(Some(second)).unwrap(); let renewal = requests.next().await.expect("the renewal's token"); assert_eq!(renewal.token().as_ref(), b"second"); @@ -253,7 +252,7 @@ async fn moqt_22_a_revoked_route_auth_remakes_its_announce() { let (token, _setup) = tokens.next().await.expect("the setup token"); assert!(token.is_empty()); - let (first, _) = pair.client.auth().add("first").await.expect("granted"); + let first = pair.client.auth().add("first").await.expect("granted"); let (token, presented) = tokens.next().await.expect("the AUTH stream's token"); assert_eq!(token, b"first"); let _route = client_origin @@ -316,7 +315,7 @@ async fn a_route_auth_survives_a_reconnect(version: &'static str) { let server_origin = produce_origin(1); let mut first = connect_route_auth_on(version, &client_origin, &server_origin, Some(handle.clone())).await; let mut tokens = serve(first.requests.take().unwrap(), |_| Some(Grant::all())); - let (token, _) = handle.add("first").await.expect("kept for requests"); + let token = handle.add("first").await.expect("kept for requests"); let _route = client_origin .route("room/x", origin::RouteOptions::default().with_auth(token)) .unwrap(); @@ -343,7 +342,7 @@ async fn a_route_auth_renews_its_announce(version: &'static str) { let (mut pair, client_origin, server_origin) = connect_route_auth(version).await; let mut tokens = serve(pair.requests.take().unwrap(), |_| Some(Grant::all())); - let (first, _) = pair.client.auth().add("first").await.expect("kept for requests"); + let first = pair.client.auth().add("first").await.expect("kept for requests"); let route = client_origin .route("room/x", origin::RouteOptions::default().with_auth(first)) .unwrap(); @@ -351,7 +350,7 @@ async fn a_route_auth_renews_its_announce(version: &'static str) { assert_eq!(token, b"first"); wait_announced(&server_origin.consume(), "room/x", true).await; - let (second, _) = pair.client.auth().add("second").await.expect("kept for requests"); + let second = pair.client.auth().add("second").await.expect("kept for requests"); route.set_auth(Some(second)).unwrap(); let (token, _second) = tokens.next().await.expect("the renewal's token"); assert_eq!(token, b"second"); @@ -779,7 +778,7 @@ async fn tokens_union_and_withdrawing_one_shrinks_it(version: &'static str) { }); let (_, _setup) = issued.next().await.unwrap(); - let (t1, _) = pair.client.auth().add("t1").await.expect("t1 granted"); + let t1 = pair.client.auth().add("t1").await.expect("t1 granted"); let (_, t1_issued) = issued.next().await.unwrap(); assert_eq!(t1.grant().peek(), Some(grant(&["b"], &[]))); assert_eq!(granted(&pair.client).await, grant(&["a", "b"], &[])); @@ -821,7 +820,7 @@ async fn an_update_replaces_one_tokens_grant(version: &'static str) { _ => None, }); let (_, _setup) = issued.next().await.unwrap(); - let (t1, _) = pair.client.auth().add("t1").await.unwrap(); + let t1 = pair.client.auth().add("t1").await.unwrap(); let (_, t1_issued) = issued.next().await.unwrap(); t1_issued.update(grant(&["c"], &["c"])); @@ -864,7 +863,7 @@ async fn a_revoked_grant_withdraws_and_can_be_restored(version: &'static str) { crate::support::announced(&publisher.consume(), "a/x").await; assert_eq!(pair.client_transport.close_reason(), None); - let (_again, _) = pair.client.auth().add("again").await.expect("re-authorized"); + let _again = pair.client.auth().add("again").await.expect("re-authorized"); wait_announced(&served, "a/x", true).await; }) .await @@ -1074,7 +1073,7 @@ async fn older_versions_have_no_grant(version: &'static str) { match version.starts_with("moq-lite") { true => assert!(matches!(pair.client.auth().add("token").await, Err(Error::Unsupported))), false => { - let (auth, _) = pair.client.auth().add("token").await.expect("kept for requests"); + let auth = pair.client.auth().add("token").await.expect("kept for requests"); assert_eq!(auth.grant().peek(), None); assert_eq!(pair.client.auth().grant().peek(), None); } @@ -1457,7 +1456,7 @@ async fn a_grant_update_too_large_for_one_message_is_unsupported(version: &'stat Some(grant(if token.is_empty() { &["a"] } else { &["b"] }, &[])) }); let (_, _setup) = issued.next().await.unwrap(); - let (token, _) = pair.client.auth().add("update").await.unwrap(); + let token = pair.client.auth().add("update").await.unwrap(); let (_, updated) = issued.next().await.unwrap(); assert_eq!(granted(&pair.client).await, grant(&["a", "b"], &[])); let huge: Patterns = (0..20) @@ -1515,7 +1514,7 @@ async fn pattern_grants_arrive_exactly(version: &'static str) { let mut held = Vec::new(); for token in ["exact", "wildcard", "mixed", "root"] { - let (added, _) = pair.client.auth().add(token).await.expect(token); + let added = pair.client.auth().add(token).await.expect(token); assert_eq!(added.grant().peek(), table(token.as_bytes()), "{token}"); held.push(added); } @@ -2100,8 +2099,7 @@ async fn a_shared_handle_presents_its_token_on_every_session(version: &'static s b"" => Some(Grant::default()), _ => Some(grant(&["first"], &[])), }); - let (token, answer) = added.await.expect("joined").expect("granted on the first session"); - assert_eq!(answer, auth::Answer::Granted); + let token = added.await.expect("joined").expect("granted on the first session"); let _first = presented(&mut tokens).await; wait_for(token.grant(), |g| g == &Some(grant(&["first"], &[]))).await; @@ -2205,7 +2203,7 @@ async fn a_handle_backs_one_active_session(version: &'static str) { "the draining session dropped its late grant" ); - let (later, _) = handle.add("later").await.expect("granted on the second session"); + let later = handle.add("later").await.expect("granted on the second session"); let (token, _later) = second_tokens.next().await.expect("the active session presents it"); assert_eq!(token, b"later"); assert!( @@ -2255,8 +2253,7 @@ async fn a_token_queued_before_our_goaway_rides_only_the_successor(version: &'st ]; tokens.sort(); assert_eq!(tokens, [b"".to_vec(), b"late".to_vec()]); - let (_late, answer) = adding.await.expect("granted on the successor"); - assert_eq!(answer, auth::Answer::Granted); + let _late = adding.await.expect("granted on the successor"); }) .await .expect("timed out"); @@ -2286,8 +2283,7 @@ async fn a_transient_refusal_is_presented_again(version: &'static str) { } } }); - let (token, answer) = pair.client.auth().add("token").await.expect("granted on the retry"); - assert_eq!(answer, auth::Answer::Granted); + let token = pair.client.auth().add("token").await.expect("granted on the retry"); assert_eq!(token.grant().peek(), Some(Grant::default())); let _setup = issued.next().await; let _token = issued.next().await; diff --git a/rs/moq-net/tests/integration/datagram.rs b/rs/moq-net/tests/integration/datagram.rs index 8b19368c420..a52c9b8edfa 100644 --- a/rs/moq-net/tests/integration/datagram.rs +++ b/rs/moq-net/tests/integration/datagram.rs @@ -97,7 +97,7 @@ impl Chain { } } -async fn recv(consumer: &mut datagram::Consumer) -> Result, Error> { +async fn recv(consumer: &mut datagram::Consumer) -> Result, Error> { kio::clock::timeout(TIMEOUT, consumer.recv()) .await .expect("recv timeout") @@ -748,51 +748,6 @@ async fn a_stream_tracks_datagram_sent_between_its_answer_and_end_arrives() { } } -/// A track's end waits for the subscriber to read the datagrams queued with it, so a flood -/// that keeps its receive queue full delays the end. Meanwhile the track keeps its stream, and -/// the session stays busy, so the peer's stream credit bounds the ends a flood can leave queued. -#[kio::test] -async fn a_track_ended_under_a_datagram_flood_keeps_its_stream() { - use moq_net::transport::poll::Session as _; - - for version in ["moq-lite-05", "moq-lite-06", "moq-lite-07-wip"] { - let chain = Chain::new(&[version]).await; - let producer = chain.publication.broadcast().create_datagrams(TRACK, timed()).unwrap(); - let mut consumer = chain.consume(&producer, 1).await; - // Each names no subscription, so the subscriber reads and drops it. The queue holds 1024. - let flood = || { - for _ in 0..512 { - let _ = chain.pairs[0] - .client_transport - .send_datagram(b"x"[..].into(), Default::default()); - } - }; - flood(); - producer.finish(); - for _ in 0..32 { - flood(); - // One turn for the session's tasks, which read at most a budget each. - let mut turned = false; - kio::wait(|waiter| { - if std::mem::replace(&mut turned, true) { - return std::task::Poll::Ready(()); - } - waiter.waker().wake_by_ref(); - std::task::Poll::Pending - }) - .await; - assert!( - chain.pairs[0].server.poll_idle(&kio::Waiter::noop()).is_pending(), - "{version}: the track released its stream before its end applied" - ); - } - assert!(recv(&mut consumer).await.unwrap().is_none(), "{version}"); - kio::clock::timeout(TIMEOUT, chain.pairs[0].server.idle()) - .await - .unwrap_or_else(|_| panic!("{version}: the track never released its stream")); - } -} - /// Ending a track must preserve its queued datagrams across the receiver's budget. #[kio::test] async fn an_ietf_datagram_burst_sent_with_its_end_arrives() { diff --git a/rs/moq-net/tests/integration/extensions.rs b/rs/moq-net/tests/integration/extensions.rs index 937143e1102..b798752d102 100644 --- a/rs/moq-net/tests/integration/extensions.rs +++ b/rs/moq-net/tests/integration/extensions.rs @@ -132,11 +132,10 @@ async fn a_declined_auth_extension_is_not_negotiated() { assert_eq!(session.auth().grant().peek(), None, "{context}"); // Only a request that names it can carry the token, so it resolves at once with // a grant that stays unknown. - let (added, answer) = kio::clock::timeout(SETTLE, session.auth().add("token")) + let added = kio::clock::timeout(SETTLE, session.auth().add("token")) .await .unwrap_or_else(|_| panic!("{context}: add never resolved")) .unwrap_or_else(|err| panic!("{context}: {err:?}")); - assert_eq!(answer, moq_net::auth::Answer::Requests, "{context}"); assert_eq!(added.grant().peek(), None, "{context}"); assert_eq!(session.auth().grant().peek(), None, "{context}"); } diff --git a/rs/moq-net/tests/integration/main.rs b/rs/moq-net/tests/integration/main.rs index 50416d0c00c..a251de5907e 100644 --- a/rs/moq-net/tests/integration/main.rs +++ b/rs/moq-net/tests/integration/main.rs @@ -60,7 +60,6 @@ mod route_health; mod routing_contracts; mod segmented_frame; mod session_close; -mod session_idle; mod session_panic; mod settled_end_survives_abort; mod setup_negotiation; diff --git a/rs/moq-net/tests/integration/session_idle.rs b/rs/moq-net/tests/integration/session_idle.rs deleted file mode 100644 index 0888e3fc115..00000000000 --- a/rs/moq-net/tests/integration/session_idle.rs +++ /dev/null @@ -1,161 +0,0 @@ -#![cfg(feature = "async")] - -//! `Session::idle` reports a session that carries no subscription or fetch in either -//! direction, which is when a migrated session can close without cutting anything off. -//! -//! Deterministic: simulated time over the mock transport. - -use crate::support; - -use std::time::Duration; - -use moq_net::{Timestamp, Version, kio}; -use support::harness::{MockConnectOptions, MockPair, connect_mock}; - -/// A millisecond track: the frames written here carry timestamps. -fn timed() -> moq_net::track::Info { - moq_net::track::Info::default().with_timescale(moq_net::Timescale::MILLI) -} - -const TIMEOUT: Duration = Duration::from_secs(10); - -fn produce_origin(hop: u64) -> moq_net::origin::Producer { - let (producer, driver) = - moq_net::origin::Producer::new(moq_net::origin::Config::new(std::num::NonZeroU64::new(hop).unwrap())); - support::harness::spawn(driver); - producer -} - -fn idle(session: &moq_net::Session) -> bool { - session.poll_idle(&kio::Waiter::noop()).is_ready() -} - -/// A client publishing one announced track to a server that subscribes to whatever it hears. -struct Setup { - pair: MockPair, - track: moq_net::track::Producer, - remote: moq_net::broadcast::Consumer, - _broadcast: moq_net::origin::Route, -} - -async fn setup(version: &str) -> Setup { - let publisher = produce_origin(1); - let broadcast = publisher - .route("bcast", moq_net::origin::RouteOptions::hidden()) - .unwrap(); - let track = broadcast.broadcast().create_track("video", timed()).unwrap(); - broadcast.announce().unwrap(); - - let subscriber = produce_origin(2); - let mut options = MockConnectOptions::new(version.parse::().unwrap()); - options.client_publish = Some(publisher.consume()); - options.server_subscribe = Some(subscriber.clone()); - let pair = connect_mock(options).await; - - let remote = kio::clock::timeout(TIMEOUT, subscriber.consume().follow("bcast", None).unwrap().available()) - .await - .expect("resolve timeout") - .expect("broadcast resolves") - .unwrap(); - - Setup { - pair, - track, - remote, - _broadcast: broadcast, - } -} - -/// An announcement alone leaves both ends idle. A subscription keeps the end serving it -/// and the end holding it busy until the reader leaves. -#[kio::test] -async fn a_subscription_keeps_both_ends_busy() { - for version in [ - "moq-lite-04", - "moq-lite-06", - "moq-lite-07-wip", - "moq-transport-18", - "moq-transport-22", - ] { - let Setup { - pair, - track, - remote, - _broadcast, - } = setup(version).await; - assert!(idle(&pair.client), "{version}: an announcement kept the publisher busy"); - assert!( - idle(&pair.server), - "{version}: an announcement kept the subscriber busy" - ); - - let mut group = track.append_group().unwrap(); - group.write_frame(Timestamp::ZERO, b"live".as_ref()).unwrap(); - group.finish().unwrap(); - let mut sub = kio::clock::timeout(TIMEOUT, remote.track("video").unwrap().subscribe(None)) - .await - .expect("subscribe timeout") - .expect("subscribe"); - let received = kio::clock::timeout(TIMEOUT, sub.recv_group()) - .await - .expect("recv timeout") - .expect("recv") - .expect("the track ended"); - assert_eq!(received.sequence, 0, "{version}"); - assert!(!idle(&pair.client), "{version}: idle while serving a subscription"); - assert!(!idle(&pair.server), "{version}: idle while holding a subscription"); - - drop((received, sub)); - kio::clock::timeout(TIMEOUT, pair.server.idle()) - .await - .unwrap_or_else(|_| panic!("{version}: the subscriber stayed busy after the reader left")); - kio::clock::timeout(TIMEOUT, pair.client.idle()) - .await - .unwrap_or_else(|_| panic!("{version}: the publisher stayed busy after the reader left")); - } -} - -/// A fetch keeps both ends busy until it ends: here, until the group it reads finishes. -/// moq-transport refuses a FETCH of the whole live group rather than waiting on it. -#[kio::test] -async fn a_fetch_keeps_both_ends_busy() { - for version in ["moq-lite-06", "moq-lite-07-wip"] { - let Setup { - pair, - track, - remote, - _broadcast, - } = setup(version).await; - - let mut writing = track.append_group().unwrap(); - writing.write_frame(Timestamp::ZERO, b"first".as_ref()).unwrap(); - let fetching = remote.track("video").unwrap(); - let fetch = kio::clock::sim::spawn(async move { - let mut fetched = fetching.fetch_group(0, None).await?; - let mut frames = Vec::new(); - while let Some(frame) = fetched.read_frame().await? { - frames.push(frame.payload); - } - Ok::<_, moq_net::Error>(frames) - }); - // Simulated: every task runs until the fetch waits on the unfinished group. - kio::clock::sleep(Duration::from_secs(1)).await; - assert!(!idle(&pair.client), "{version}: idle while serving a fetch"); - assert!(!idle(&pair.server), "{version}: idle while a fetch is in flight"); - - writing.write_frame(Timestamp::ZERO, b"last".as_ref()).unwrap(); - writing.finish().unwrap(); - let frames = kio::clock::timeout(TIMEOUT, fetch) - .await - .expect("fetch timeout") - .expect("fetch task") - .expect("fetch"); - assert_eq!(frames, [&b"first"[..], &b"last"[..]], "{version}"); - kio::clock::timeout(TIMEOUT, pair.server.idle()) - .await - .unwrap_or_else(|_| panic!("{version}: the fetching side stayed busy after the fetch")); - kio::clock::timeout(TIMEOUT, pair.client.idle()) - .await - .unwrap_or_else(|_| panic!("{version}: the serving side stayed busy after the fetch")); - } -} diff --git a/rs/moq-quic/README.md b/rs/moq-quic/README.md index 00b3598f815..b02e532b2be 100644 --- a/rs/moq-quic/README.md +++ b/rs/moq-quic/README.md @@ -103,7 +103,7 @@ Changes on top of the upstream commit, besides the renames: - **No receive-state recycling** (`StreamsState::stream_recv_freed`): a completed stream's `Recv` is dropped, where quinn keeps it in `free_recv` with its chunk list at peak capacity for the next stream. Recycled state stayed allocated per peak concurrent stream for the connection's lifetime: about 550 B per stream, and about 113 KiB for one that once buffered a 2,048-packet backlog. Reusing it saved only 0.1 to 0.3% of a burst's instructions. `benches/receive_bursts.rs` measures both. Stream ID hash tables and waiter tables contract geometrically as streams go away and release every slot when empty; drained control queues and obsolete credit updates are dropped when nothing remains to send. A surviving stream cannot pin a burst's hash-table capacity. A cherry-pick touching `StreamRecv`, `free_recv`, or `reinit` has nothing to apply to. Not offered upstream yet. - **Bandwidth probing** (`Connection::set_probe_target`, `connection/bandwidth_probe.rs`): an application-limited sender below its target fills the headroom BBR3 opens, in Startup and in one ProbeBW\_UP per probe wait, with duplicates of STREAM data still in flight, paced to the target, so the controller's samples measure the path rather than the application. That inflight bounds an attempt like a window once it sends its first duplicate; media alone past it, as beside another flow's standing queue, leaves the sender application-limited. `Controller::probes` and `Metrics::probe_inflight` tell the sender when and how far it may fill; CUBIC never does. A duplicate is never retransmitted, a reset or stopped stream's data is never resent, and a loss retransmits only the ranges no copy delivered (`SendBuffer::retransmit`). An attempt that raises nothing for two round trips, or ends short of the target, sits out 3 s and then 4 s before retrying the same target; reaching the target or a new one retries at once. `PathStats::sent_bandwidth_probe_bytes` counts the redundant bytes. A cherry-pick touching `poll_transmit`'s space loop or `populate_packet` has to keep the probe after the ordinary frames. Not offered upstream. - **Exact datagram overhead** (`Datagram::max_payload`): `Datagrams::max_size` subtracts the frame type and the length's actual varint size from the path budget and the peer's `max_datagram_frame_size`, instead of `Datagram::SIZE_BOUND`'s worst-case 8-byte length. RFC 9221 section 3 counts the frame type and length in the limit, so a peer advertising 1200 bytes (quicly does) accepts a 1197-byte payload, where reserving 8 bytes allows only 1191. quinn has the same behaviour; not offered upstream yet. A limit too small for even an empty frame, including RFC 9221's 0 for no support, makes `max_size` return `None` instead of accepting an empty payload. -- **Waits** (`connection/waits.rs`): every wait is a `poll_*` taking the caller's `&kio::Waiter`, so a driver keeps no waiter tables of its own: `recv::Chunks::poll_next`, `recv::Stream::poll_lease` and `poll_received_reset`, `send::Stream::poll_write`, `poll_end`, and `poll_acked`, `Streams::poll_accept` and `poll_open`, `Datagrams::poll_recv`, and `Connection::poll_handshake_data`, `poll_connected`, `poll_handshake_confirmed`, and `poll_closed`, over QUIC and QMux alike, with `Connection::error` for why it was lost. quinn's `StreamEvent` and the `Event` variants a handle waited on are gone (`Event` keeps `PathUpdated`, and QMux has no events): the stream engine wakes each list where quinn queued the matching event, and wakes connection-blocked writers as soon as the window has room (`StreamsState::unblock`), so a port touching one of quinn's event sites has to wake the matching list instead. A lost connection wakes every waiter, and each wait drains what arrived (accepted streams, readable bytes, datagrams) before it fails, a stream wait with `ConnectionLost`. Not offered upstream: quinn keeps its wakers in the async layer. +- **Waits** (`connection/waits.rs`): every wait is a `poll_*` taking the caller's `&kio::Waiter`, so a driver keeps no waiter tables of its own: `recv::Chunks::poll_next`, `recv::Stream::poll_lease` and `poll_received_reset`, `send::Stream::poll_write` and `poll_end`, `Streams::poll_accept` and `poll_open`, `Datagrams::poll_recv`, and `Connection::poll_handshake_data`, `poll_connected`, and `poll_closed`, over QUIC and QMux alike, with `Connection::error` for why it was lost. quinn's `StreamEvent` and `Event` are gone: the stream engine wakes each list where quinn queued the matching event, and wakes connection-blocked writers as soon as the window has room (`StreamsState::unblock`), so a port touching one of quinn's event sites has to wake the matching list instead. A lost connection wakes every waiter, and each wait drains what arrived (accepted streams, readable bytes, datagrams) before it fails, a stream wait with `ConnectionLost`. Not offered upstream: quinn keeps its wakers in the async layer. Recheck the BBR3 changes when porting quinn#2481 updates, or if it merges. diff --git a/rs/moq-quic/src/config/transport/datagram.rs b/rs/moq-quic/src/config/transport/datagram.rs index 36bbd449a56..68776610cdc 100644 --- a/rs/moq-quic/src/config/transport/datagram.rs +++ b/rs/moq-quic/src/config/transport/datagram.rs @@ -21,7 +21,7 @@ pub struct Config { /// than the link, or even the underlying hardware, can transmit them. This limits the amount of /// memory that may be consumed in that case. When the send buffer is full and a new datagram is /// sent, the oldest datagram of the lowest-priority send group is dropped until the new - /// one fits; see [`Datagrams::send`](crate::Datagrams::send). + /// one fits; see [`Datagrams::send`](crate::connection::Datagrams::send). /// /// Counts each datagram's frame bytes, so the payload buffered is a little smaller than this. /// Defaults to 1 MiB. diff --git a/rs/moq-quic/src/config/transport/mod.rs b/rs/moq-quic/src/config/transport/mod.rs index ba1c2d36807..e0b3c06a250 100644 --- a/rs/moq-quic/src/config/transport/mod.rs +++ b/rs/moq-quic/src/config/transport/mod.rs @@ -161,13 +161,6 @@ pub struct Config { /// How to construct each connection's congestion controller, BBRv3 by default pub congestion: Arc, - /// An outbound rate limit for each connection, in bytes per second, or `None` (the default) - /// for none - /// - /// The congestion controller's pacer enforces it, so a controller that does not pace, such as - /// `Unlimited`, ignores it. - pub max_outgoing_bytes_per_second: Option, - /// How unreliable datagrams are buffered pub datagram: datagram::Config, @@ -309,7 +302,6 @@ impl Default for Config { receive_timestamps: None, congestion: Arc::new(congestion::bbr3::Config::default()), - max_outgoing_bytes_per_second: None, datagram: datagram::Config { receive_buffer: Some(STREAM_RWND as usize), @@ -347,7 +339,6 @@ impl fmt::Debug for Config { ack_frequency, receive_timestamps, congestion: _, - max_outgoing_bytes_per_second, datagram, crypto_buffer_size, allow_spin, @@ -375,7 +366,6 @@ impl fmt::Debug for Config { .field("ack_frequency", ack_frequency) .field("receive_timestamps", receive_timestamps) // congestion not debug - .field("max_outgoing_bytes_per_second", max_outgoing_bytes_per_second) .field("datagram", datagram) .field("crypto_buffer_size", crypto_buffer_size) .field("allow_spin", allow_spin) diff --git a/rs/moq-quic/src/connection/mod.rs b/rs/moq-quic/src/connection/mod.rs index 8feeb704302..c02ba252daa 100644 --- a/rs/moq-quic/src/connection/mod.rs +++ b/rs/moq-quic/src/connection/mod.rs @@ -1,4 +1,4 @@ -//! A single QUIC connection: its state machine, the events it reports, and why it ends. +//! A single QUIC connection: its state machine and why it ends. use std::{ cmp, @@ -104,9 +104,9 @@ impl ConnectionTag { } pub use stats::{CreditStats, FrameStats, PathStats, Stats, UdpStats}; -mod streams; -pub(crate) use streams::StreamsState; -pub use streams::{Streams, recv, send}; +pub(crate) mod streams; +pub use streams::Streams; +pub(crate) use streams::{StreamsState, recv, send}; mod timer; @@ -116,8 +116,8 @@ use timer::{Timer, TimerTable}; /// Protocol state and logic for a single QUIC connection /// -/// Objects of this type receive [`ConnectionEvent`]s and emit [`EndpointEvent`]s and application -/// [`Event`]s to make progress. To handle timeouts, a `Connection` returns timer updates and +/// Objects of this type receive [`ConnectionEvent`]s and emit [`EndpointEvent`]s to make +/// progress. To handle timeouts, a `Connection` returns timer updates and /// expects timeouts through various methods. A number of simple getter methods are exposed /// to allow callers to inspect some of the connection state. /// @@ -141,7 +141,6 @@ use timer::{Timer, TimerTable}; /// 1. [`poll_transmit`](Self::poll_transmit) /// 2. [`poll_timeout`](Self::poll_timeout) /// 3. [`poll_endpoint_events`](Self::poll_endpoint_events) -/// 4. [`poll`](Self::poll) /// /// Currently the only actual dependency is from (2) to (1), however additional /// dependencies may be added in future, so the above order is recommended. @@ -190,7 +189,6 @@ pub struct Connection { /// The value that the server included in the Source Connection ID field of a Retry packet, if /// one was received retry_src_cid: Option, - events: VecDeque, endpoint_events: VecDeque, /// Whether `EndpointEventInner::Draining` was emitted, which happens once. draining_reported: bool, @@ -352,7 +350,6 @@ impl Connection { orig_rem_cid: rem_cid, initial_dst_cid: init_cid, retry_src_cid: None, - events: VecDeque::new(), endpoint_events: VecDeque::new(), draining_reported: false, spin_enabled: config.allow_spin && rng.random_ratio(7, 8), @@ -466,7 +463,8 @@ impl Connection { } /// Wait for the handshake to be confirmed, as [`poll_connected`](Self::poll_connected) does - pub fn poll_handshake_confirmed(&mut self, waiter: &kio::Waiter) -> Poll> { + #[cfg(test)] + pub(crate) fn poll_handshake_confirmed(&mut self, waiter: &kio::Waiter) -> Poll> { self.streams.waits.poll_confirmed(waiter) } @@ -482,16 +480,6 @@ impl Connection { self.streams.waits.lost() } - /// Returns application-facing notifications that no `poll_*` wait covers - /// - /// Connections should be polled for events after: - /// - a call was made to `handle_event` - /// - a call was made to `handle_timeout` - #[must_use] - pub fn poll(&mut self) -> Option { - self.events.pop_front() - } - /// Return endpoint-facing events #[must_use] pub fn poll_endpoint_events(&mut self) -> Option { @@ -1306,8 +1294,8 @@ impl Connection { /// Process `ConnectionEvent`s generated by the associated `Endpoint` /// /// Will execute protocol logic upon receipt of a connection event, in turn preparing signals - /// (including application `Event`s, `EndpointEvent`s and outgoing datagrams) that should be - /// extracted through the relevant methods. + /// (including `EndpointEvent`s and outgoing datagrams) that should be extracted through the + /// relevant methods. pub fn handle_event(&mut self, event: ConnectionEvent) { use ConnectionEventInner::*; match event.0 { @@ -1373,9 +1361,8 @@ impl Connection { /// Process timer expirations /// - /// Executes protocol logic, potentially preparing signals (including application `Event`s, - /// `EndpointEvent`s and outgoing datagrams) that should be extracted through the relevant - /// methods. + /// Executes protocol logic, potentially preparing signals (including `EndpointEvent`s and + /// outgoing datagrams) that should be extracted through the relevant methods. /// /// It is most efficient to call this immediately after the system clock reaches the latest /// `Instant` that was output by `poll_timeout`; however spurious extra calls will simply @@ -1426,7 +1413,6 @@ impl Connection { if let Some((_, prev)) = self.prev_path.take() { self.path = prev; self.set_loss_detection_timer(now); - self.events.push_back(Event::PathUpdated); } self.path.challenge = None; self.path.challenge_pending = false; @@ -3431,7 +3417,6 @@ impl Connection { let mut prev = mem::replace(&mut self.path, new_path); self.datagrams().drop_oversized(); - self.events.push_back(Event::PathUpdated); // Don't clobber the original path if the previous one hasn't been validated yet if prev.challenge.is_none() { @@ -4427,17 +4412,6 @@ mod state { } } -/// Notifications of interest to the application that no `poll_*` wait covers -/// -/// Everything an application waits for (a connection, a stream, data, credit, an end, a loss) -/// is a `poll_*` taking its [`kio::Waiter`] instead. -#[derive(Debug)] -#[non_exhaustive] -pub enum Event { - /// The currently active path was updated - PathUpdated, -} - fn get_max_ack_delay(params: &TransportParameters) -> Duration { Duration::from_micros(params.max_ack_delay.0 * 1000) } diff --git a/rs/moq-quic/src/connection/paths.rs b/rs/moq-quic/src/connection/paths.rs index c7c73ab2c92..e97715cfa48 100644 --- a/rs/moq-quic/src/connection/paths.rs +++ b/rs/moq-quic/src/connection/paths.rs @@ -59,7 +59,6 @@ fn build_congestion(now: Instant, mtu: u16, seed: u64, config: &transport::Confi now, mtu, rtt: config.initial_rtt, - max_pacing_rate: config.max_outgoing_bytes_per_second, seed, }) } diff --git a/rs/moq-quic/src/connection/streams/lease.rs b/rs/moq-quic/src/connection/streams/lease.rs index 95ee5d78016..2fbfafdd3ed 100644 --- a/rs/moq-quic/src/connection/streams/lease.rs +++ b/rs/moq-quic/src/connection/streams/lease.rs @@ -277,7 +277,7 @@ impl Stream<'_> { /// Nothing is evicted while the peer has connection credit left, so while the leased and /// unread bytes of every stream together stay below the connection receive window. A receiver /// whose window exceeds the most its incomplete units can hold never sees an eviction; - /// [`CreditStats::evicted`](crate::CreditStats::evicted) counts any that happen. + /// [`CreditStats::evicted`](crate::connection::CreditStats::evicted) counts any that happen. /// /// Eviction also waits for every stream with data its reader has not leased yet, since that /// reader may be about to free the window, for every reader waiting behind a gap, which a diff --git a/rs/moq-quic/src/connection/streams/send.rs b/rs/moq-quic/src/connection/streams/send.rs index 595e30d4eae..5c061c1230d 100644 --- a/rs/moq-quic/src/connection/streams/send.rs +++ b/rs/moq-quic/src/connection/streams/send.rs @@ -519,9 +519,8 @@ impl<'a> Stream<'a> { /// Whether the peer acknowledged every byte before `offset` /// - /// A `false` notes to wake the stream's [`Self::poll_acked`] waiters once the answer changes. - /// The stream holds only its lowest waiting offset, so every waiter on it polls again then. Like [`End::Finished`], an acknowledgment only means the peer's QUIC stack - /// holds the data, not that its application read it. + /// Like [`End::Finished`], an acknowledgment only means the peer's QUIC stack holds the data, + /// not that its application read it. /// /// Fails with [`Error::Closed`] once the peer will never acknowledge it, or no longer reads /// it: a reset dropped the data before `offset`, the peer sent STOP_SENDING, or the @@ -551,12 +550,14 @@ impl<'a> Stream<'a> { /// Wait for the peer to acknowledge every byte before `offset`, as [`Self::acked`] answers it /// - /// Pending registers `waiter` until the answer changes. Fails with [`Error::ConnectionLost`] + /// Pending registers `waiter` until the answer changes. The stream holds only its lowest + /// waiting offset, so every waiter on it polls again then. Fails with [`Error::ConnectionLost`] /// instead of [`Error::Closed`] once the connection is lost. /// /// # Panics /// - when `offset` came from another stream - pub fn poll_acked(&mut self, waiter: &kio::Waiter, offset: Offset) -> Poll> { + #[cfg(test)] + pub(crate) fn poll_acked(&mut self, waiter: &kio::Waiter, offset: Offset) -> Poll> { match self.acked(offset) { Ok(true) => Poll::Ready(Ok(())), Ok(false) => { @@ -720,7 +721,7 @@ pub enum End { /// /// A higher priority group preempts every lower one, and groups of equal priority get an /// equal share of bytes no matter how many streams each has queued. Streams outside any group -/// go before all of it. A group's datagrams, from [`Datagrams::send`](crate::Datagrams::send), +/// go before all of it. A group's datagrams, from [`Datagrams::send`](crate::connection::Datagrams::send), /// share its bytes in the same order. pub struct Groups<'a> { pub(crate) state: &'a mut StreamsState, diff --git a/rs/moq-quic/src/connection/waits.rs b/rs/moq-quic/src/connection/waits.rs index 98fdddde846..349d85a2e88 100644 --- a/rs/moq-quic/src/connection/waits.rs +++ b/rs/moq-quic/src/connection/waits.rs @@ -188,6 +188,7 @@ impl Waits { } /// Ready once the handshake was confirmed, or with the error once the connection was lost first + #[cfg(test)] pub(crate) fn poll_confirmed(&mut self, waiter: &Waiter) -> Poll> { Self::poll_latch(&mut self.confirmed, &self.lost, waiter) } diff --git a/rs/moq-quic/src/fuzz.rs b/rs/moq-quic/src/fuzz.rs index 0cd4cc46b05..974ac7294a3 100644 --- a/rs/moq-quic/src/fuzz.rs +++ b/rs/moq-quic/src/fuzz.rs @@ -23,9 +23,9 @@ use bytes::{Bytes, BytesMut}; use rand::{Rng, SeedableRng, rngs::StdRng}; use crate::{ - ConnectionId, Dir, MAX_CID_SIZE, RESET_TOKEN_SIZE, Side, StreamId, Streams, VarInt, + ConnectionId, Dir, MAX_CID_SIZE, RESET_TOKEN_SIZE, Side, StreamId, VarInt, coding::BufMutExt, - connection::{Retransmits, State, StreamsState}, + connection::{Retransmits, State, Streams, StreamsState}, frame::{self, Frame}, packet::{FixedLengthConnectionIdParser, PartialDecode}, transport_parameters::{PreferredAddress, TransportParameters}, diff --git a/rs/moq-quic/src/lib.rs b/rs/moq-quic/src/lib.rs index a62ddf3ea40..dbc61a6e7d2 100644 --- a/rs/moq-quic/src/lib.rs +++ b/rs/moq-quic/src/lib.rs @@ -44,7 +44,7 @@ mod bloom_token_log; use bloom_token_log::BloomTokenLog; pub mod connection; -pub use crate::connection::{CreditStats, Datagrams, Event, FrameStats, PathStats, Streams, UdpStats, recv, send}; +pub use crate::connection::streams::{recv, send}; pub mod stream; #[cfg(feature = "mux")] diff --git a/rs/moq-quic/src/tests/characterize/paths.rs b/rs/moq-quic/src/tests/characterize/paths.rs index 1dea63d8b59..e796aa1b5f1 100644 --- a/rs/moq-quic/src/tests/characterize/paths.rs +++ b/rs/moq-quic/src/tests/characterize/paths.rs @@ -296,19 +296,14 @@ fn path_validation_failure_reverts() { assert_eq!(pair.server_conn_mut(server_ch).remote_address(), new); // Nothing the server sends arrives, and the client stays silent - let mut updates = Vec::new(); while pair.server_conn_mut(server_ch).remote_address() == new { pair.server.outbound.clear(); pair.time = pair.server.next_wakeup().expect("validation times out"); let now = pair.time; pair.server.drive(now, old); - while let Some(event) = pair.server_conn_mut(server_ch).poll() { - updates.push(event); - } } pair.server.outbound.clear(); assert_eq!(pair.server_conn_mut(server_ch).remote_address(), old); - assert_matches!(updates.as_slice(), [.., Event::PathUpdated]); // The connection still works on the old path let s = pair diff --git a/rs/moq-quic/src/tests/mod.rs b/rs/moq-quic/src/tests/mod.rs index c80bf419d16..f759c576cb5 100644 --- a/rs/moq-quic/src/tests/mod.rs +++ b/rs/moq-quic/src/tests/mod.rs @@ -2334,9 +2334,7 @@ fn migration() { assert_ne!(pair.server_conn_mut(server_ch).total_recvd(), 0); pair.drive(); - // The client's own path change is no news to it, and the connection survives it. - let client = pair.client_conn_mut(client_ch); - assert!(!iter::from_fn(|| client.poll()).any(|event| matches!(event, Event::PathUpdated))); + // The connection survives the client's own path change. assert_matches!(pair.client_conn_mut(client_ch).error(), None); assert_eq!(pair.server_conn_mut(server_ch).remote_address(), pair.client.addr); @@ -7264,7 +7262,6 @@ fn default_controller_supports_bandwidth_probing() { now: Instant::now(), mtu: INITIAL_MTU, rtt: Duration::from_millis(333), - max_pacing_rate: None, seed: 0, }); assert!(controller.probes()); diff --git a/rs/moq-quic/src/tests/scream.rs b/rs/moq-quic/src/tests/scream.rs index 9aadf54eeaa..a5bdf0c8823 100644 --- a/rs/moq-quic/src/tests/scream.rs +++ b/rs/moq-quic/src/tests/scream.rs @@ -1,4 +1,4 @@ -//! SCReAMv2 ([`congestion::Scream`]) on the seeded sim: an encoder that follows the controller's +//! SCReAMv2 ([`congestion::scream::Scream`]) on the seeded sim: an encoder that follows the controller's //! estimate sends up through a bottleneck shaped like each cellular moq-shaper preset, and the run //! reports the queue it built, what it carried, and how fast it answered each step. //! diff --git a/rs/moq-quic/src/tests/util.rs b/rs/moq-quic/src/tests/util.rs index 0b0b9c4bee5..199cb83a7cf 100644 --- a/rs/moq-quic/src/tests/util.rs +++ b/rs/moq-quic/src/tests/util.rs @@ -22,7 +22,11 @@ use tracing::{debug_span, trace}; use super::crypto::rustls::QuicServerConfig; use super::*; pub(super) use crate::sim::{Bottleneck, Link, QueueStats}; -use crate::{Duration, Instant, entropy}; +use crate::{ + Duration, Instant, + connection::{Datagrams, Streams}, + entropy, +}; pub(super) const DEFAULT_MTU: usize = 1452; diff --git a/rs/moq-relay/benches/auth.rs b/rs/moq-relay/benches/auth.rs index 2925cb48d69..38ba51af198 100644 --- a/rs/moq-relay/benches/auth.rs +++ b/rs/moq-relay/benches/auth.rs @@ -83,7 +83,7 @@ impl Fixture { producers.push(receive.recv().await.unwrap().1); let auth = connection.auth(); let added = futures::future::join_all((0..tokens).map(|index| auth.add(format!("token{index}")))).await; - let added: Vec<_> = added.into_iter().map(|added| added.unwrap().0).collect(); + let added: Vec<_> = added.into_iter().map(|added| added.unwrap()).collect(); if session == 0 { // `add` resolves once the peer grants the token, so its grant is already current. let observed = added.last().unwrap().grant(); diff --git a/rs/moq-relay/src/auth/refresh.rs b/rs/moq-relay/src/auth/refresh.rs index d64ffbb288e..0876f37163f 100644 --- a/rs/moq-relay/src/auth/refresh.rs +++ b/rs/moq-relay/src/auth/refresh.rs @@ -1084,7 +1084,7 @@ mod tests { assert_eq!(admission.request.query.as_deref(), Some("jwt=replacement")); admission.grant(lease::Consumer::fixed(grant("a/**", 20))); }); - let (token, _) = token.unwrap(); + let token = token.unwrap(); clock.advance(Duration::from_secs(4)); assert!( settles_pending(&mut supervising).await, @@ -1125,7 +1125,7 @@ mod tests { async { tokio::select! { reason = lease.ended() => panic!("the lease ended while renewing: {reason}"), - token = auth.add("replacement") => token.unwrap().0, + token = auth.add("replacement") => token.unwrap(), } }, async { admissions.next().await.unwrap().grant(replacement) }, @@ -1229,7 +1229,7 @@ mod tests { let (token, ()) = tokio::join!(auth.add("replacement"), async { admissions.next().await.unwrap().grant(consumer); }); - let (token, _) = token.unwrap(); + let token = token.unwrap(); assert!( token.grant().peek().unwrap().subscribe.is_empty(), "publisher SETUP role widened" @@ -1278,7 +1278,7 @@ mod tests { assert!( matches!(result, Err(moq_net::Error::Session(SessionError::Unauthorized))), "an empty token reissued a credential the relay removed: {:?}", - result.map(|(token, _)| token.grant().peek()) + result.map(|token| token.grant().peek()) ); assert!(!supervising.is_finished()); client.abort(moq_net::Error::Cancel); diff --git a/rs/moq-relay/src/lib.rs b/rs/moq-relay/src/lib.rs index ecddea3b5ce..141ee27cd3b 100644 --- a/rs/moq-relay/src/lib.rs +++ b/rs/moq-relay/src/lib.rs @@ -38,7 +38,7 @@ pub mod stun; #[cfg(feature = "test-support")] mod test_support; #[cfg(all(target_os = "linux", feature = "_uring"))] -pub mod uring; +pub(crate) mod uring; /// The HTTP/HTTPS server: health, certificate fingerprint, announcements, fetch, and WebSocket sessions. pub mod web; #[cfg(feature = "websocket")] diff --git a/rs/moq-relay/src/relay.rs b/rs/moq-relay/src/relay.rs index cb0f7b94df4..fbdcb4a9c7a 100644 --- a/rs/moq-relay/src/relay.rs +++ b/rs/moq-relay/src/relay.rs @@ -147,7 +147,7 @@ impl Relay { // another relay. Only negotiated when both sides offer it, so other peers never see it. config.listen.extensions.ietf = true; config.connect.extensions.ietf = true; - // Name the relay ahead of its core unless the operator overrides it (empty omits it), + // Name the relay ahead of its core unless the embedder set a name (empty omits it), // and dial cluster peers under the same name. let implementation = config .listen diff --git a/rs/moq-relay/src/uring.rs b/rs/moq-relay/src/uring.rs index eddcce74638..9a888aa7ef9 100644 --- a/rs/moq-relay/src/uring.rs +++ b/rs/moq-relay/src/uring.rs @@ -93,12 +93,6 @@ struct Serve { /// the listen address and a port conflict is an error here rather than a /// worker that quietly died. [`spawn`](Self::spawn) starts every thread, and /// nothing is served until [`Spawned::serve`]. -/// -/// ```compile_fail -/// fn serve_before_spawn(mut bound: moq_relay::uring::Workers) { -/// bound.serve(); -/// } -/// ``` pub struct Workers { members: Vec, addr: SocketAddr, diff --git a/rs/moq-relay/tests/integration/auth_lifetime.rs b/rs/moq-relay/tests/integration/auth_lifetime.rs index 1329fea6db8..ec565144d90 100644 --- a/rs/moq-relay/tests/integration/auth_lifetime.rs +++ b/rs/moq-relay/tests/integration/auth_lifetime.rs @@ -2038,7 +2038,7 @@ async fn request_token_lease_ends_before_the_connection_and_reopens_later() { .established() .await .unwrap(); - let (token, _) = publisher.auth().add("shared").await.unwrap(); + let token = publisher.auth().add("shared").await.unwrap(); let first = origin .route( "first", diff --git a/rs/moq-relay/tests/integration/runtime_uring.rs b/rs/moq-relay/tests/integration/runtime_uring.rs index 6aa98392bac..6861cd286ff 100644 --- a/rs/moq-relay/tests/integration/runtime_uring.rs +++ b/rs/moq-relay/tests/integration/runtime_uring.rs @@ -1126,18 +1126,16 @@ async fn uring_workers_offer_the_configured_extensions() { assert!(admission.attached(), "an AUTH token arrived as a new session"); admission.grant(grant()); }); - let (_, answer) = token + let _ = token .expect("the token was never answered") .expect("the token was refused"); - assert_eq!(answer, moq_net::auth::Answer::Granted); } false => { // Without MoQ Auth no AUTH stream carries it: only a request naming it does. - let (_, answer) = presented + let _ = presented .await .expect("the token was never answered") .expect("a moq-transport request can carry the token"); - assert_eq!(answer, moq_net::auth::Answer::Requests, "a token without MoQ Auth"); } } diff --git a/rs/moq-rtmp/src/dial.rs b/rs/moq-rtmp/src/dial.rs index d7d2b63e277..395f215bfa1 100644 --- a/rs/moq-rtmp/src/dial.rs +++ b/rs/moq-rtmp/src/dial.rs @@ -69,7 +69,7 @@ pub struct Client { app: String, /// How long [`publish`](Self::publish)'s FLV muxer waits for a stalled group /// before skipping. - max_delay: Duration, + delay: Duration, /// Max age declared on the media tracks [`pull`](Self::pull) publishes, or `None` for /// no publisher limit. max_age: Option, @@ -175,7 +175,7 @@ impl Client { session, work, app: app.to_string(), - max_delay: crate::DEFAULT_MAX_DELAY, + delay: crate::DEFAULT_DELAY, max_age: Some(hang::container::MAX_AGE), bandwidth: moq_net::bandwidth::Allocator::unlimited(), linger: moq_mux::Linger::DEFAULT, @@ -188,8 +188,8 @@ impl Client { /// /// The publish (egress) direction only; [`pull`](Self::pull) takes /// [`with_max_age`](Self::with_max_age) instead. - pub fn with_max_delay(mut self, max_delay: Duration) -> Self { - self.max_delay = max_delay; + pub fn with_delay(mut self, delay: Duration) -> Self { + self.delay = delay; self } @@ -203,7 +203,7 @@ impl Client { /// advertise segments that are still fetchable. Lower it when nothing reads history. /// /// The pull (ingest) direction only; [`publish`](Self::publish) reads a broadcast - /// someone else declared, and takes [`with_max_delay`](Self::with_max_delay) instead. + /// someone else declared, and takes [`with_delay`](Self::with_delay) instead. pub fn with_max_age(mut self, max_age: impl Into>) -> Self { self.max_age = max_age.into(); self @@ -261,7 +261,7 @@ impl Client { .catalog::<()>(moq_mux::catalog::CatalogFormat::default()) .await .map_err(|e| anyhow::anyhow!("init FLV export: {e}"))?; - let mut export = FlvExport::new(source, catalog).with_delay(self.max_delay); + let mut export = FlvExport::new(source, catalog).with_delay(self.delay); let mut tags = flv::TagReader::new(); let mut buffer = [0u8; READ_BUFFER]; @@ -666,7 +666,7 @@ mod tests { session, work: VecDeque::new(), app: "live".into(), - max_delay: crate::DEFAULT_MAX_DELAY, + delay: crate::DEFAULT_DELAY, max_age: None, bandwidth: Default::default(), linger: Duration::ZERO, @@ -712,7 +712,7 @@ mod tests { session, work, app: "live".into(), - max_delay: crate::DEFAULT_MAX_DELAY, + delay: crate::DEFAULT_DELAY, max_age: None, bandwidth: Default::default(), linger: Duration::ZERO, diff --git a/rs/moq-rtmp/src/lib.rs b/rs/moq-rtmp/src/lib.rs index bfcc19084fa..c8d0d1684b2 100644 --- a/rs/moq-rtmp/src/lib.rs +++ b/rs/moq-rtmp/src/lib.rs @@ -74,7 +74,7 @@ mod rml; mod server; /// Default FLV release delay and stalled-group budget. -const DEFAULT_MAX_DELAY: Duration = Duration::from_millis(500); +const DEFAULT_DELAY: Duration = Duration::from_millis(500); pub use dial::Client; pub use error::{Error, Result}; diff --git a/rs/moq-rtmp/src/server.rs b/rs/moq-rtmp/src/server.rs index 210c4ec44e5..ffc4990937d 100644 --- a/rs/moq-rtmp/src/server.rs +++ b/rs/moq-rtmp/src/server.rs @@ -767,8 +767,8 @@ pub struct Play { tc_url: Option, peer: SocketAddr, /// How long the FLV muxer waits for a stalled group before skipping to a newer - /// one; see [`with_max_delay`](Self::with_max_delay). - max_delay: Duration, + /// one; see [`with_delay`](Self::with_delay). + delay: Duration, /// Enhanced-RTMP capabilities advertised by the player in its connect object. capabilities: ClientCapabilities, } @@ -800,8 +800,8 @@ impl Play { /// Set the FLV release delay and stalled-group budget. /// Defaults to 500 ms; zero releases available frames immediately without deadline drops. - pub fn with_max_delay(mut self, max_delay: Duration) -> Self { - self.max_delay = max_delay; + pub fn with_delay(mut self, delay: Duration) -> Self { + self.delay = delay; self } @@ -890,7 +890,7 @@ impl Play { .map_err(|e| anyhow::anyhow!("init FLV export: {e}"))? .select(select); let mut export = FlvExport::new(source, stream) - .with_delay(self.max_delay) + .with_delay(self.delay) .with_multitrack(self.capabilities.multitrack); // Resolve the catalog and codec headers before Play.Start, too. Otherwise a @@ -1222,7 +1222,7 @@ async fn accept_until_request(mut stream: S, peer: SocketAddr) -> any stream_key, tc_url, peer, - max_delay: crate::DEFAULT_MAX_DELAY, + delay: crate::DEFAULT_DELAY, capabilities: client_capabilities.clone(), }))); } diff --git a/rs/moq-sock/src/demux.rs b/rs/moq-sock/src/demux.rs index b830be36215..e81db655b25 100644 --- a/rs/moq-sock/src/demux.rs +++ b/rs/moq-sock/src/demux.rs @@ -1,6 +1,6 @@ //! Splitting one UDP socket between QUIC and the WebRTC media path. //! -//! [`classify`] reads a datagram's first byte by RFC 7983's partition, which QUIC +//! Datagrams are classified by their first byte, per RFC 7983's partition, which QUIC //! fills as long as its fixed bit is set: every endpoint served through this must //! turn QUIC-bit greasing (RFC 9287) off. [`Flows`] pins a remote address to a //! stack regardless of what it sends, and [`Queue`] buffers one stack's datagrams @@ -20,8 +20,7 @@ use arc_swap::ArcSwap; /// What a datagram carries, judged by its first byte and, for STUN, its attributes. #[derive(Clone, Copy, Debug, PartialEq, Eq)] -#[non_exhaustive] -pub enum Class { +enum Class { /// A QUIC packet: the fixed bit is set, in a long (192 to 255) or short (64 to 127) header. Quic, /// ICE: a STUN Binding request carrying USERNAME (a connectivity check), or a @@ -46,7 +45,7 @@ const STUN_BINDING_ERROR: [u8; 2] = [0x01, 0x11]; const STUN_USERNAME: u16 = 0x0006; /// Classify a datagram by RFC 7983's first-byte ranges. -pub fn classify(datagram: &[u8]) -> Class { +fn classify(datagram: &[u8]) -> Class { match datagram.first() { Some(0..=3) if is_ice(datagram) => Class::Ice, Some(0..=3) => Class::Stun, @@ -173,7 +172,7 @@ impl FlowsState { } impl Flows { - /// Route `datagram` from `remote`: a pin wins, otherwise its [`Class`] decides. + /// Route `datagram` from `remote`: a pin wins, otherwise its first byte decides. pub fn route(&self, remote: SocketAddr, datagram: &[u8]) -> Route { if self.is_pinned(remote) { return Route::WebRtc; @@ -232,7 +231,7 @@ impl Drop for Pin { /// A FIFO of one stack's datagrams, bounded by cost with a per-source share. /// -/// A source may hold at most [`Queue::SHARE`] of the capacity, so one flooding +/// A source may hold at most an eighth of the capacity, so one flooding /// sender is refused while the rest still get in; filling the queue takes that /// many sources at once. #[derive(Debug)] @@ -246,7 +245,7 @@ pub struct Queue { impl Queue { /// The fraction of the capacity, as a divisor, one source may hold. - pub const SHARE: usize = 8; + const SHARE: usize = 8; /// An empty queue holding up to `capacity` in total cost. pub fn new(capacity: usize) -> Self { diff --git a/rs/moq-srt/src/dial.rs b/rs/moq-srt/src/dial.rs index 4e3389754f3..a86b04062c9 100644 --- a/rs/moq-srt/src/dial.rs +++ b/rs/moq-srt/src/dial.rs @@ -41,7 +41,7 @@ pub struct Client { /// SRT receive latency, negotiated at handshake time: the buffer that trades delay /// for loss recovery. It doubles as [`publish`]'s egress skip threshold. - max_delay: Duration, + latency: Duration, /// How long a superseded group of an ingested media track stays fresh, or `None` for /// no publisher limit. Defaults to [`MAX_AGE`](hang::container::MAX_AGE). @@ -79,7 +79,7 @@ impl Client { Self { addr, resource: resource.into(), - max_delay: DEFAULT_LATENCY, + latency: DEFAULT_LATENCY, max_age: Some(hang::container::MAX_AGE), bandwidth: moq_net::bandwidth::Allocator::unlimited(), program: None, @@ -94,8 +94,8 @@ impl Client { /// /// Both directions; [`publish`](Self::publish) also uses it as the MPEG-TS release /// delay, but no less than a second. - pub fn with_max_delay(mut self, max_delay: Duration) -> Self { - self.max_delay = max_delay; + pub fn with_latency(mut self, latency: Duration) -> Self { + self.latency = latency; self } @@ -154,7 +154,7 @@ impl Client { let path: moq_net::Path = path.into(); let socket = self.call(Mode::Publish).await?; let options = crate::ts::Options { - latency: self.max_delay, + latency: self.latency, follow: self.follow, stitch: self.stitch, }; @@ -184,7 +184,7 @@ impl Client { } let stream_id = format!("#!::r={},m={}", self.resource, mode.as_str()); let socket = SrtSocket::builder() - .latency(self.max_delay) + .latency(self.latency) .set(configure_buffers) .call(self.addr, Some(&stream_id)) .await?; diff --git a/rs/moq-stats/src/consume.rs b/rs/moq-stats/src/consume.rs index e1a13551c01..3d1b052c505 100644 --- a/rs/moq-stats/src/consume.rs +++ b/rs/moq-stats/src/consume.rs @@ -5,10 +5,7 @@ use std::task::{Poll, ready}; use moq_net::{Path, broadcast}; use serde::de::DeserializeOwned; -use crate::{ - GroupFrame, PrefixFrame, PresenceFrame, Result, TotalsFrame, group_track, prefix_track, presence_track, - totals_track, -}; +use crate::{PrefixFrame, PresenceFrame, Result, TotalsFrame, prefix_track, presence_track, totals_track}; /// Configuration for a [`Consumer`]. Empty for now; construct with /// [`Config::new`]. The struct is `#[non_exhaustive]`, so new options stay @@ -28,11 +25,8 @@ impl Config { /// yielding typed frames per track. /// /// Subscribe to the tracks you care about with [`Self::totals`], -/// [`Self::group`], [`Self::presence`], and [`Self::prefix`]. A label's -/// presence track and a prefix's track are produced only while subscribed. -/// At depth 0 the one group is the empty one; otherwise discover live groups -/// from the broadcast's [track listing](moq_net::broadcast::Consumer::tracks) -/// with [`crate::parse_group_track`]. +/// [`Self::presence`], and [`Self::prefix`]. A label's presence track and a +/// prefix's track are produced only while subscribed. pub struct Consumer { broadcast: broadcast::Consumer, } @@ -70,9 +64,10 @@ impl Consumer { /// Subscribe to group `group`'s track; see [`Self::totals`]. The first read /// fails with [`NotFound`](moq_net::Error::NotFound) unless the group is /// listed. - pub fn group(&self, group: impl Into) -> Result { + #[cfg(test)] + pub(crate) fn group(&self, group: impl Into) -> Result { Ok(GroupConsumer { - inner: self.subscribe(&group_track(group))?, + inner: self.subscribe(&crate::group_track(group))?, }) } @@ -140,17 +135,19 @@ impl PrefixConsumer { } /// A typed reader over one group's track; see [`TotalsConsumer`]. -pub struct GroupConsumer { - inner: Reader, +#[cfg(test)] +pub(crate) struct GroupConsumer { + inner: Reader, } +#[cfg(test)] impl GroupConsumer { /// The next frame, or `None` once the track ends (the producer went away /// or the group lingered out of the listing). Fails with /// [`FrameTooLarge`](moq_net::Error::FrameTooLarge) once the frame outgrows /// the cache. #[cfg_attr(feature = "async", kio::wrapper)] - pub fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll>> { + pub(crate) fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll>> { self.inner.poll_latest(waiter) } } diff --git a/rs/moq-tokio/src/connection.rs b/rs/moq-tokio/src/connection.rs index 19c4e22d4c7..0db7638d9a1 100644 --- a/rs/moq-tokio/src/connection.rs +++ b/rs/moq-tokio/src/connection.rs @@ -93,14 +93,9 @@ pub struct Backoff { /// /// A healthy session's end resets the delay and the timeout. One that ends sooner is a /// failed attempt, so a peer that accepts and then drops every session still escalates - /// the delay and exhausts the timeout. - #[usage( - name = "connect-backoff-healthy", - long, - env = "MOQ_CONNECT_BACKOFF_HEALTHY", - setting = "connect.backoff.healthy" - )] - #[serde(default, skip_serializing_if = "Option::is_none")] + /// the delay and exhausts the timeout. Set by the embedding binary, not configured. + #[usage(skip)] + #[serde(skip)] pub healthy: Option, /// Maximum time to spend retrying before giving up. Defaults to 10s; zero retries forever. @@ -1383,14 +1378,12 @@ mod tests { parsed.backoff.initial = Some((Duration::from_secs(7)).into()); parsed.backoff.multiplier = 5; parsed.backoff.max = Some((Duration::from_secs(11)).into()); - parsed.backoff.healthy = Some((Duration::from_secs(2)).into()); parsed.backoff.timeout = Some((Duration::ZERO).into()); parsed.update_from(&[]); assert_eq!(parsed.backoff.initial, Some((Duration::from_secs(7)).into())); assert_eq!(parsed.backoff.multiplier, 5); assert_eq!(parsed.backoff.max, Some((Duration::from_secs(11)).into())); - assert_eq!(parsed.backoff.healthy, Some((Duration::from_secs(2)).into())); assert_eq!(parsed.backoff.timeout(), Duration::ZERO, "0 means retry forever"); // With no flags, the parser defaults resolve to the policy's defaults. @@ -2071,7 +2064,7 @@ mod tests { break; } if let Some(added) = added.take() { - token = Some(added.await.unwrap().expect("granted").0); + token = Some(added.await.unwrap().expect("granted")); } let mut grant = token.as_ref().unwrap().grant(); while grant.peek() != Some(moq_net::auth::Grant::all()) { @@ -2642,14 +2635,6 @@ mod duration_tests { |c: &Backoff| c.max(), |d| d ); - crate::cli::duration::test_config_duration!( - healthy_set_in_code_beats_parser_default, - Backoff, - healthy, - "--connect-backoff-healthy", - |c: &Backoff| c.resolve().healthy, - |d| d - ); crate::cli::duration::test_config_duration!( timeout_set_in_code_beats_parser_default, Backoff, diff --git a/rs/moq-tokio/src/error.rs b/rs/moq-tokio/src/error.rs index d368f26a22a..414df4572cd 100644 --- a/rs/moq-tokio/src/error.rs +++ b/rs/moq-tokio/src/error.rs @@ -105,8 +105,8 @@ pub enum Error { #[error("quic.keylog needs a nonempty path; boolean keylog settings require SSLKEYLOGFILE")] KeylogUnset, - /// A zero `connect.backoff.healthy`, which would count every session as healthy. - #[error("connect.backoff.healthy must be non-zero")] + /// A zero `Backoff::healthy`, which would count every session as healthy. + #[error("the healthy backoff bar must be non-zero")] BackoffHealthy, /// The config was parsed from released spellings that no longer work. The diff --git a/rs/moq-tokio/src/listen.rs b/rs/moq-tokio/src/listen.rs index 3073dfa74db..99513bc7df8 100644 --- a/rs/moq-tokio/src/listen.rs +++ b/rs/moq-tokio/src/listen.rs @@ -146,14 +146,10 @@ pub struct Config { pub extensions: moq_net::setup::Extensions, /// The implementation to advertise in SETUP, as space-separated `name/version` - /// product tokens. Defaults to the binary's own; empty advertises nothing. - #[usage( - name = "listen-implementation", - long = "listen-implementation", - env = "MOQ_LISTEN_IMPLEMENTATION", - setting = "listen.implementation" - )] - #[serde(default, skip_serializing_if = "Option::is_none")] + /// product tokens. `None` advertises [`moq_net::IMPLEMENTATION`]; empty advertises + /// nothing. Set by the embedding binary, not configured. + #[usage(skip)] + #[serde(skip)] pub implementation: Option, /// Maximum time for one accepted connection to finish its handshake: the @@ -1040,7 +1036,7 @@ pub struct Forward { impl Forward { /// Whether any forwarding setting is present. - pub fn configured(&self) -> bool { + pub(crate) fn configured(&self) -> bool { self.bind.is_some() || self.key.is_some() || self.peers.is_some() } } diff --git a/rs/moq-tokio/src/quic/backend.rs b/rs/moq-tokio/src/quic/backend.rs index 4807eb3f253..71030004f4b 100644 --- a/rs/moq-tokio/src/quic/backend.rs +++ b/rs/moq-tokio/src/quic/backend.rs @@ -1182,7 +1182,6 @@ mod tests { now: kio::clock::Instant::now(), mtu: 1200, rtt: std::time::Duration::from_millis(333), - max_pacing_rate: None, seed: 0, }; diff --git a/rs/moq-tokio/src/quic/demux.rs b/rs/moq-tokio/src/quic/demux.rs index 5cc4c6b4518..04f956bfb9c 100644 --- a/rs/moq-tokio/src/quic/demux.rs +++ b/rs/moq-tokio/src/quic/demux.rs @@ -454,9 +454,12 @@ mod tests { let feed = demux.webrtc(); let divert = demux.divert(); let big = [0x80; 1024]; - let share = CAPACITY / Queue::::SHARE / big.len(); - for _ in 0..=share { + // Flood from one source until it is refused, which must happen before the queue fills. + for _ in 0..CAPACITY / big.len() { split(&divert, &[(remote(1), &[&big])]); + if demux.counters().get(Discard::Overflow) > 0 { + break; + } } let overflow = demux.counters().get(Discard::Overflow); assert!(overflow > 0); diff --git a/rs/moq-tokio/src/quic/driver/connection.rs b/rs/moq-tokio/src/quic/driver/connection.rs index 6cec2479b92..3138fa4b741 100644 --- a/rs/moq-tokio/src/quic/driver/connection.rs +++ b/rs/moq-tokio/src/quic/driver/connection.rs @@ -195,7 +195,6 @@ impl Future for ConnectionDriver { // might need to reset a timer. Hence, we must loop until neither happens. keep_going |= conn.drive_timer(cx); conn.forward_endpoint_events(); - conn.drain_events(); // Every datagram sent or received passes through this poll. #[cfg(feature = "net")] @@ -410,7 +409,7 @@ impl Connection { /// /// The datagram is scheduled like one of its group's streams at its priority, or among the /// ungrouped streams without a group. Sending never blocks: a full buffer drops the oldest - /// datagram of the lowest-priority group, see [`moq_quic::Datagrams::send`]. A header chained + /// datagram of the lowest-priority group, see [`moq_quic::connection::Datagrams::send`]. A header chained /// in front of a [`Bytes`] payload costs no copy of the payload. /// /// # Panics @@ -779,11 +778,6 @@ impl State { } } - /// Drain moq-quic's events, which no handle waits on: moq-quic wakes its waiters itself. - fn drain_events(&mut self) { - while self.inner.poll().is_some() {} - } - fn drive_timer(&mut self, cx: &mut Context<'_>) -> bool { let Some(deadline) = self.inner.poll_timeout() else { self.timer_deadline = None; diff --git a/rs/moq-tokio/src/quic/driver/mod.rs b/rs/moq-tokio/src/quic/driver/mod.rs index 33a656afdaa..48455f90b26 100644 --- a/rs/moq-tokio/src/quic/driver/mod.rs +++ b/rs/moq-tokio/src/quic/driver/mod.rs @@ -66,9 +66,9 @@ pub(crate) use kio::clock::Instant; pub(crate) use std::time::Duration; pub use moq_quic::{ - ApplicationClose, ConfigError, ConnectionClose, ConnectionId, ConnectionIdGenerator, CreditStats, EcnCodepoint, - FrameStats, FrameType, InvalidCid, Limits, LimitsError, PathStats, Rate, Transmit, TransportErrorCode, UdpStats, - VarInt, VarIntBoundsExceeded, Version, client, crypto, server, stream, transport, + ApplicationClose, ConfigError, ConnectionClose, ConnectionId, ConnectionIdGenerator, EcnCodepoint, FrameType, + InvalidCid, Limits, LimitsError, Rate, Transmit, TransportErrorCode, VarInt, VarIntBoundsExceeded, Version, client, + crypto, server, stream, transport, }; #[cfg(feature = "quic")] diff --git a/rs/moq-tokio/src/quic/driver/tests.rs b/rs/moq-tokio/src/quic/driver/tests.rs index bf107022d8b..3c86beb7f0f 100755 --- a/rs/moq-tokio/src/quic/driver/tests.rs +++ b/rs/moq-tokio/src/quic/driver/tests.rs @@ -1645,6 +1645,7 @@ async fn multiple_conns_with_zero_length_cids() { } #[tokio::test] +#[ignore = "flaky: its 100ms wall-clock budget runs out under a loaded CI host"] async fn stream_stopped() { let _guard = subscribe(); let factory = EndpointFactory::new(); diff --git a/rs/moq-tokio/src/settings.rs b/rs/moq-tokio/src/settings.rs index 286e57e29eb..c5d72a9e304 100644 --- a/rs/moq-tokio/src/settings.rs +++ b/rs/moq-tokio/src/settings.rs @@ -23,9 +23,6 @@ pub struct Listen { #[usage(env = "MOQ_LISTEN_TIMEOUT", cli("--listen-timeout"))] timeout: Option, - #[usage(env = "MOQ_LISTEN_IMPLEMENTATION", cli("--listen-implementation"))] - implementation: Option, - #[usage(env = "MOQ_LISTEN_PREFERRED_V4", cli("--listen-preferred-v4"))] preferred_v4: Option, @@ -213,9 +210,6 @@ struct ConnectBackoff { #[usage(env = "MOQ_CONNECT_BACKOFF_MAX", cli("--connect-backoff-max"))] max: Option, - #[usage(env = "MOQ_CONNECT_BACKOFF_HEALTHY", cli("--connect-backoff-healthy"))] - healthy: Option, - #[usage(env = "MOQ_CONNECT_BACKOFF_TIMEOUT", cli("--connect-backoff-timeout"))] timeout: Option, } diff --git a/rs/moq-tokio/src/tcp.rs b/rs/moq-tokio/src/tcp.rs index 6b717392d15..d5f634c14c1 100644 --- a/rs/moq-tokio/src/tcp.rs +++ b/rs/moq-tokio/src/tcp.rs @@ -410,13 +410,9 @@ impl Listener { /// A failed `accept(2)` is handled here rather than yielded: it is classified, /// counted, logged, and paced by [`accept_health`](Self::accept_health), then /// retried, because the caller has no better answer than to ask again. A - /// per-connection *handshake* failure is still yielded as `Some(Err(..))`. - /// - /// The `Option` no longer has a `None` case to report: nothing ends the accept - /// loop, so this always yields. It stays because dropping it is a breaking change - /// to a published signature. - pub async fn accept(&self) -> Option> { - crate::runtime::scope(async move { Some(self.accept_pending().await.await.map(|(session, _)| session)) }).await + /// per-connection *handshake* failure is still returned as an error. + pub async fn accept(&self) -> Result { + crate::runtime::scope(async move { self.accept_pending().await.await.map(|(session, _)| session) }).await } /// Accept a socket and return its independently driven qmux handshake. @@ -674,7 +670,7 @@ mod tests { .with_protocols(["moq-test"]); let addr = listener.local_addr().expect("local addr"); - let accept = tokio::spawn(async move { listener.accept().await.expect("listener gone").expect("accept") }); + let accept = tokio::spawn(async move { listener.accept().await.expect("accept") }); let blackhole: net::SocketAddr = "192.0.2.1:9".parse().unwrap(); let candidates = crate::resolve::Candidates::fixed([blackhole, addr]); @@ -706,7 +702,7 @@ mod tests { .expect("bind listener") .with_protocols(["moq-test"]); let addr = listener.local_addr().expect("local addr"); - let accept = tokio::spawn(async move { listener.accept().await.expect("listener gone").expect("accept") }); + let accept = tokio::spawn(async move { listener.accept().await.expect("accept") }); let candidates = crate::resolve::Candidates::fixed([addr]); let client = connect_addrs(candidates, &["moq-test"], &crate::qmux_default(), Duration::ZERO) .await diff --git a/rs/moq-tokio/src/websocket.rs b/rs/moq-tokio/src/websocket.rs index 5e7be93f6b9..1670aba1fb0 100644 --- a/rs/moq-tokio/src/websocket.rs +++ b/rs/moq-tokio/src/websocket.rs @@ -674,22 +674,15 @@ impl Listener { /// A failed `accept(2)` is handled here rather than yielded: it is classified, /// counted, logged, and paced by [`accept_health`](Self::accept_health), then /// retried, because the caller has no better answer than to ask again. A - /// per-connection upgrade failure is still yielded as `Some(Err(..))`. - /// - /// As in [`crate::tcp`], the `Option` has no `None` case left to report. - pub async fn accept(&self) -> Option> { - crate::runtime::scope(async move { - self.accept_with_url() - .await - .map(|result| result.map(|(session, _, _)| session)) - }) - .await + /// per-connection upgrade failure is still returned as an error. + pub async fn accept(&self) -> Result { + crate::runtime::scope(async move { self.accept_with_url().await.map(|(session, _, _)| session) }).await } /// Accept the next connection and retain the WebSocket request URL, the chosen /// sub-protocol, and the peer's address. - pub(crate) async fn accept_with_url(&self) -> Option> { - Some(self.accept_pending().await.await) + pub(crate) async fn accept_with_url(&self) -> Result<(qmux::Session, Url, Accepted)> { + self.accept_pending().await.await } /// Accept a socket and return its independently driven WebSocket upgrade, so a @@ -857,7 +850,7 @@ mod tests { async fn listener_refuses_a_client_without_subprotocol() { let listener = Listener::bind("127.0.0.1:0".parse().unwrap()).await.unwrap(); let addr = listener.local_addr().unwrap(); - let accepted = tokio::spawn(async move { listener.accept_with_url().await.unwrap() }); + let accepted = tokio::spawn(async move { listener.accept_with_url().await }); let stream = tokio::net::TcpStream::connect(addr).await.unwrap(); let err = tokio_tungstenite::client_async(format!("ws://{addr}/room"), stream) diff --git a/rs/moq-tokio/tests/integration/scream.rs b/rs/moq-tokio/tests/integration/scream.rs index f234c0dfa19..5878bfecead 100644 --- a/rs/moq-tokio/tests/integration/scream.rs +++ b/rs/moq-tokio/tests/integration/scream.rs @@ -1,4 +1,4 @@ -//! SCReAMv2 ([`congestion::Scream`]) over real sockets through the cellular moq-shaper presets. +//! SCReAMv2 ([`congestion::scream::Scream`]) over real sockets through the cellular moq-shaper presets. //! //! A media sender encodes 30 frames a second at the controller's estimate, a keyframe every two //! seconds, each group on its own stream, up through the shaper's uplink. Each frame carries when diff --git a/rs/moq-uring/README.md b/rs/moq-uring/README.md index 01652f19a30..6aa7c96550f 100644 --- a/rs/moq-uring/README.md +++ b/rs/moq-uring/README.md @@ -21,7 +21,7 @@ local (`!Send`) task set, and the UDP sockets bound through it. clock, and `Handle::run` drives MoQ with it and a single timer. - **Turns**: each turn reaps and dispatches completions first, then polls the root future and runs passes over the woken tasks until a pass wakes nothing - or `Config::passes` have run, then parks or submits. So a received + or four have run, then parks or submits. So a received datagram's routing, its connection's processing, and the reply that stages are carried by the one enter that follows. A connection stages a bounded number of GSO trains per turn, then yields its backlog to the next turn. @@ -121,7 +121,7 @@ first spike (recv batching x GRO x GSO, epoll vs io\_uring); see git history for their methodology. `echo_quic` is the ablation matrix over the real worker: the same QUIC echo with receive batching, GRO, and GSO toggled one at a time. `worker_turns` sweeps sockets against datagrams per burst for UDP, -and connections against reply bytes and task passes for QUIC request/reply. +and connections against reply bytes for QUIC request/reply. The QUIC client and server run as worker tasks; the root only waits for a measurement to finish. Counter deltas stay in the run's log beside the timing series in the nightly `rust-shared` group. diff --git a/rs/moq-uring/benches/worker_turns.rs b/rs/moq-uring/benches/worker_turns.rs index 6e015ea8403..c34772da99d 100644 --- a/rs/moq-uring/benches/worker_turns.rs +++ b/rs/moq-uring/benches/worker_turns.rs @@ -15,7 +15,7 @@ mod support; reason = "benchmarks measure themselves on the host clock" )] fn request_reply(c: &mut criterion::Criterion) { - use std::{net::UdpSocket, num::NonZeroUsize, time::Instant}; + use std::{net::UdpSocket, time::Instant}; use criterion::Throughput; use moq_uring::{Config, Worker, quic, udp}; @@ -30,92 +30,88 @@ fn request_reply(c: &mut criterion::Criterion) { let mut group = c.benchmark_group("worker_request_reply"); for connections in [1, 16] { for bytes in [64, 256 * 1024] { - for passes in [1, 4] { - let mut config = Config::default(); - config.passes = NonZeroUsize::new(passes).unwrap(); - let mut worker = Worker::new(config).expect("worker"); - let handle = worker.handle(); - let socket = || { - handle - .udp(UdpSocket::bind("127.0.0.1:0").expect("bind"), udp::Config::default()) - .expect("socket") - }; - let server = quic::Endpoint::new( - quic::endpoint::Config::default().with_server(quic::server::Config::new(tls.clone())), - socket(), - ) - .expect("server"); - let client = quic::Endpoint::new(quic::endpoint::Config::default(), socket()).expect("client"); - let mut dial = quic::client::Config::default(); - dial.alpn = vec![ALPN.to_owned()]; - dial.verify = false; - for _ in 0..connections { - let server = server.clone(); + let mut worker = Worker::new(Config::default()).expect("worker"); + let handle = worker.handle(); + let socket = || { + handle + .udp(UdpSocket::bind("127.0.0.1:0").expect("bind"), udp::Config::default()) + .expect("socket") + }; + let server = quic::Endpoint::new( + quic::endpoint::Config::default().with_server(quic::server::Config::new(tls.clone())), + socket(), + ) + .expect("server"); + let client = quic::Endpoint::new(quic::endpoint::Config::default(), socket()).expect("client"); + let mut dial = quic::client::Config::default(); + dial.alpn = vec![ALPN.to_owned()]; + dial.verify = false; + for _ in 0..connections { + let server = server.clone(); + handle.spawn(async move { + let conn = server.accept().await.expect("accept"); + let (mut send, mut recv) = conn.accept_bi().await.expect("stream"); + let reply = bytes::Bytes::from(vec![0x5a; bytes]); + let mut request = [0; 64]; + while recv.read_exact(&mut request).await.is_ok() { + send.write_all(reply.clone()).await.expect("reply"); + } + }); + } + let (mut streams, _held) = worker + .block_on(async { + let mut streams = Vec::new(); + let mut held = Vec::new(); + for _ in 0..connections { + let conn = client + .connect(&dial, server.local_addr(), "localhost") + .await + .expect("dial"); + streams.push(conn.open_bi(Default::default()).await.expect("stream")); + held.push(conn); + } + (streams, held) + }) + .expect("worker"); + let id = format!("connections-{connections}/bytes-{bytes}"); + group.throughput(Throughput::Bytes((connections * (64 + bytes)) as u64)); + let mut reply = vec![0; bytes]; + let completed = kio::Queue::bounded(1); + group.bench_function(id.clone(), |b| { + b.iter_custom(|rounds| { + let mut running = std::mem::take(&mut streams); + let mut answer = std::mem::take(&mut reply); + let done = completed.clone(); handle.spawn(async move { - let conn = server.accept().await.expect("accept"); - let (mut send, mut recv) = conn.accept_bi().await.expect("stream"); - let reply = bytes::Bytes::from(vec![0x5a; bytes]); - let mut request = [0; 64]; - while recv.read_exact(&mut request).await.is_ok() { - send.write_all(reply.clone()).await.expect("reply"); - } - }); - } - let (mut streams, _held) = worker - .block_on(async { - let mut streams = Vec::new(); - let mut held = Vec::new(); - for _ in 0..connections { - let conn = client - .connect(&dial, server.local_addr(), "localhost") - .await - .expect("dial"); - streams.push(conn.open_bi(Default::default()).await.expect("stream")); - held.push(conn); - } - (streams, held) - }) - .expect("worker"); - let id = format!("connections-{connections}/bytes-{bytes}/passes-{passes}"); - group.throughput(Throughput::Bytes((connections * (64 + bytes)) as u64)); - let mut reply = vec![0; bytes]; - let completed = kio::Queue::bounded(1); - group.bench_function(id.clone(), |b| { - b.iter_custom(|rounds| { - let mut running = std::mem::take(&mut streams); - let mut answer = std::mem::take(&mut reply); - let done = completed.clone(); - handle.spawn(async move { - for _ in 0..rounds { - for (send, _) in &mut running { - send.write_all(&[0xa5; 64][..]).await.expect("request"); - } - for (_, recv) in &mut running { - recv.read_exact(&mut answer).await.expect("reply"); - } + for _ in 0..rounds { + for (send, _) in &mut running { + send.write_all(&[0xa5; 64][..]).await.expect("request"); + } + for (_, recv) in &mut running { + recv.read_exact(&mut answer).await.expect("reply"); } - done.try_push((running, answer)).expect("complete"); - }); - let before = handle.metrics().snapshot(); - let start = Instant::now(); - (streams, reply) = worker - .block_on(kio::wait(|waiter| completed.poll_pop(waiter))) - .expect("worker") - .expect("measurement"); - let elapsed = start.elapsed(); - let after = handle.metrics().snapshot(); - assert!(reply.iter().all(|byte| *byte == 0x5a)); - eprintln!( - "{id} rounds={rounds} turns={} passes={} enters={} rx_datagrams={}", - after.turns - before.turns, - after.passes - before.passes, - after.enters - before.enters, - after.rx_datagrams - before.rx_datagrams, - ); - elapsed + } + done.try_push((running, answer)).expect("complete"); }); + let before = handle.metrics().snapshot(); + let start = Instant::now(); + (streams, reply) = worker + .block_on(kio::wait(|waiter| completed.poll_pop(waiter))) + .expect("worker") + .expect("measurement"); + let elapsed = start.elapsed(); + let after = handle.metrics().snapshot(); + assert!(reply.iter().all(|byte| *byte == 0x5a)); + eprintln!( + "{id} rounds={rounds} turns={} passes={} enters={} rx_datagrams={}", + after.turns - before.turns, + after.passes - before.passes, + after.enters - before.enters, + after.rx_datagrams - before.rx_datagrams, + ); + elapsed }); - } + }); } } group.finish(); diff --git a/rs/moq-uring/src/metrics.rs b/rs/moq-uring/src/metrics.rs index ebd89d4c25d..181d64cfb22 100644 --- a/rs/moq-uring/src/metrics.rs +++ b/rs/moq-uring/src/metrics.rs @@ -243,8 +243,8 @@ pub struct Snapshot { /// Drive-loop turns, including the final poll that completes the root future. pub turns: u64, /// Passes over the woken tasks. A turn runs passes until one wakes nothing - /// or [`crate::Config::passes`] have run, so this over `turns` is the - /// passes a turn takes, and how close the budget is to binding. + /// or four have run, so this over `turns` is the passes a turn takes, and + /// how close that budget is to binding. pub passes: u64, /// Enters forced by a full submission queue. pub sq_full_enters: u64, diff --git a/rs/moq-uring/src/quic/driver/connection.rs b/rs/moq-uring/src/quic/driver/connection.rs index 11fc1800a66..163570a8a28 100644 --- a/rs/moq-uring/src/quic/driver/connection.rs +++ b/rs/moq-uring/src/quic/driver/connection.rs @@ -229,7 +229,7 @@ impl Connection { /// or among the ungrouped streams without a group. /// /// Never blocks: a full buffer drops the oldest datagram of the lowest-priority group, - /// see [`moq_quic::Datagrams::send`]. A [`Bytes`] payload, or a chain of them, is queued + /// see [`moq_quic::connection::Datagrams::send`]. A [`Bytes`] payload, or a chain of them, is queued /// without copying. /// /// # Panics @@ -708,9 +708,6 @@ impl Driver { loop { self.endpoint_events(); - // Handles wait on moq-quic itself, so its events go unread; drain them - // so the queue stays empty. - while self.shared.conn.borrow_mut().poll().is_some() {} self.check_lost(waiter); if self.shared.state.borrow().dead { diff --git a/rs/moq-uring/src/worker.rs b/rs/moq-uring/src/worker.rs index bcb8dbdec40..1c388a2ebaa 100644 --- a/rs/moq-uring/src/worker.rs +++ b/rs/moq-uring/src/worker.rs @@ -55,9 +55,10 @@ const TEARDOWN_EINTR_RETRIES: usize = 8; /// Maximum time spent staging cancellations and draining completions. const TEARDOWN_TIMEOUT: Duration = Duration::from_millis(3200); -/// The default [`Config::passes`]. A received datagram's chain is the demux, -/// its connection, the application task it wakes, and the connection again -/// for the reply; more found nothing left to run. +/// The most passes over its woken tasks one turn runs before it submits. A +/// received datagram's chain is the demux, its connection, the application +/// task it wakes, and the connection again for the reply. A sweep of 1, 2, 4, +/// and 8 passes found nothing left to run past four. const PASSES: NonZeroUsize = NonZeroUsize::new(4).unwrap(); /// Worker construction knobs. @@ -76,14 +77,15 @@ pub struct Config { /// so give each worker its own. Cloning this config starts a fresh set for /// the cloned worker. pub metrics: Metrics, - /// The most passes over its woken tasks one turn runs before it submits. + /// The most passes over its woken tasks one turn runs before it submits: + /// [`PASSES`], which only tests change. /// /// A turn runs its tasks until a pass wakes nothing, so work a completion /// sets off (a datagram routed to its connection, the connection staging /// its reply) is staged by the enter that follows it. The budget bounds a /// wake chain, or a task waking itself, so the ring is still reaped and /// submitted to between steps of it. One is a pass per turn. - pub passes: NonZeroUsize, + pub(crate) passes: NonZeroUsize, } impl Default for Config { diff --git a/rs/moq-video/src/decode/consumer.rs b/rs/moq-video/src/decode/consumer.rs index e9d8cbef7fa..7b81c5566d7 100644 --- a/rs/moq-video/src/decode/consumer.rs +++ b/rs/moq-video/src/decode/consumer.rs @@ -515,7 +515,7 @@ mod tests { let mut broadcast = moq_net::broadcast::Info::new().produce(); let subscriber = broadcast.consume(); let catalog = moq_mux::catalog::Producer::new(&mut broadcast, moq_mux::catalog::Config::default()).unwrap(); - let mut import = moq_mux::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(true); + let mut import = moq_mux::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(true); import.decode(&init).unwrap(); import.decode(&fragment).unwrap(); diff --git a/rs/moq-video/src/decode/decoder.rs b/rs/moq-video/src/decode/decoder.rs index c7bfe4bc073..31ee9a58df5 100644 --- a/rs/moq-video/src/decode/decoder.rs +++ b/rs/moq-video/src/decode/decoder.rs @@ -714,7 +714,7 @@ mod tests { let mut broadcast = moq_net::broadcast::Info::new().produce(); let subscriber = broadcast.consume(); let catalog = moq_mux::catalog::Producer::new(&mut broadcast, moq_mux::catalog::Config::default()).unwrap(); - let mut import = moq_mux::container::fmp4::Import::new(broadcast, catalog.reserve()).with_passthrough(true); + let mut import = moq_mux::container::fmp4::Import::new(broadcast, catalog.reserve()).with_cmaf(true); import.decode(&init).unwrap(); import.decode(&fragment).unwrap(); diff --git a/rs/moq-webrtc/src/error.rs b/rs/moq-webrtc/src/error.rs index 254e6e33a1a..13045d56140 100644 --- a/rs/moq-webrtc/src/error.rs +++ b/rs/moq-webrtc/src/error.rs @@ -43,6 +43,10 @@ pub enum Error { #[error("io error: {0}")] Io(#[from] std::io::Error), + /// The request named a broadcast path that is not a valid [`moq_net::Path`]. + #[error("invalid path: {0}")] + InvalidPath(#[from] moq_net::path::Error), + /// Error from the underlying moq-net transport. #[error("moq error: {0}")] Moq(#[from] moq_net::Error), diff --git a/rs/moq-webrtc/src/server/whep.rs b/rs/moq-webrtc/src/server/whep.rs index b7ff6837624..d908b712b22 100644 --- a/rs/moq-webrtc/src/server/whep.rs +++ b/rs/moq-webrtc/src/server/whep.rs @@ -97,13 +97,7 @@ async fn accept_offer( return Err(Error::InvalidSdp("expected Content-Type: application/sdp".into())); } let offer = std::str::from_utf8(&body).map_err(|err| Error::InvalidSdp(err.to_string()))?; - accept( - server, - subscriber, - moq_net::Path::new(path).map_err(moq_net::Error::from)?, - offer, - ) - .await + accept(server, subscriber, moq_net::Path::new(path)?, offer).await } #[cfg(feature = "server")] @@ -223,9 +217,7 @@ async fn resolve(subscriber: &moq_net::origin::Consumer, path: moq_net::Path) -> #[cfg(feature = "server")] fn status_for(err: &Error) -> StatusCode { match err { - Error::InvalidSdp(_) | Error::Moq(moq_net::Error::EmptySegment | moq_net::Error::InvalidEscape) => { - StatusCode::BAD_REQUEST - } + Error::InvalidSdp(_) | Error::InvalidPath(_) => StatusCode::BAD_REQUEST, Error::UnsupportedCodec(_) => StatusCode::UNSUPPORTED_MEDIA_TYPE, Error::SessionNotFound => StatusCode::NOT_FOUND, Error::Moq(moq_net::Error::Unauthorized) => StatusCode::UNAUTHORIZED, diff --git a/rs/moq-webrtc/src/server/whip.rs b/rs/moq-webrtc/src/server/whip.rs index 0ef8551253f..9eee79396a6 100644 --- a/rs/moq-webrtc/src/server/whip.rs +++ b/rs/moq-webrtc/src/server/whip.rs @@ -82,7 +82,7 @@ async fn accept_offer( return Err(Error::InvalidSdp("expected Content-Type: application/sdp".into())); } let offer = std::str::from_utf8(&body).map_err(|err| Error::InvalidSdp(err.to_string()))?; - let broadcast = moq_net::Path::new(path).map_err(moq_net::Error::from)?; + let broadcast = moq_net::Path::new(path)?; accept(server, Request::new(publisher, broadcast, offer)).await } @@ -229,9 +229,7 @@ fn is_sdp(headers: &HeaderMap) -> bool { #[cfg(feature = "server")] fn status_for(err: &Error) -> StatusCode { match err { - Error::InvalidSdp(_) | Error::Moq(moq_net::Error::EmptySegment | moq_net::Error::InvalidEscape) => { - StatusCode::BAD_REQUEST - } + Error::InvalidSdp(_) | Error::InvalidPath(_) => StatusCode::BAD_REQUEST, Error::UnsupportedCodec(_) => StatusCode::UNSUPPORTED_MEDIA_TYPE, Error::SessionNotFound => StatusCode::NOT_FOUND, Error::Moq(moq_net::Error::Unauthorized) => StatusCode::UNAUTHORIZED, diff --git a/sh/gh/test-inventory.allow b/sh/gh/test-inventory.allow index 6d7002c7e5a..77b7e301cc9 100644 --- a/sh/gh/test-inventory.allow +++ b/sh/gh/test-inventory.allow @@ -37,3 +37,6 @@ ignore moq-video::render::export::dx12::tests::media_foundation_encodes_the_expo # Need another implementation to speak the route wire; the quest ports it. ignore moq-net::test_interop::lite_tracks_interop /quest/m2/cluster-routing/routes/js.md + +# Quarantined flakes, until the quest that owns the flake fixes it at its cause. +ignore moq-tokio::quic::driver::tests::stream_stopped /quest/m0/test-flakes-2/README.md diff --git a/swift/Sources/Moq/Datagram.swift b/swift/Sources/Moq/Datagram.swift index c400501c60d..8b74a4d1592 100644 --- a/swift/Sources/Moq/Datagram.swift +++ b/swift/Sources/Moq/Datagram.swift @@ -132,5 +132,5 @@ public final class DatagramRequest: Sendable { } /// Reject this request with an application error code. - public func abort(errorCode: UInt16) throws { try ffi.abort(errorCode: errorCode) } + public func reject(errorCode: UInt16) throws { try ffi.reject(errorCode: errorCode) } } diff --git a/swift/Sources/Moq/Media.swift b/swift/Sources/Moq/Media.swift index 12d807b4402..643f2096bf8 100644 --- a/swift/Sources/Moq/Media.swift +++ b/swift/Sources/Moq/Media.swift @@ -50,7 +50,7 @@ public enum Media { } } /// A single-track producer of either kind, such as a video rendition's `lower`. - public enum Producer { + public enum Producer: Sendable { /// Imports complete encoded frames. case track(TrackProducer) /// Imports a byte stream. @@ -64,8 +64,33 @@ public enum Media { } /// An audio codec, its initialization bytes, and label. public typealias AudioInit = MoqAudioInit - /// A video codec, its initialization bytes, label, and hints. - public typealias VideoInit = MoqVideoInit + /// A video codec, its initialization bytes, label, hints, and lower rendition. + public struct VideoInit: Sendable { + /// The video codec. + public var format: VideoFormat + /// Codec init bytes (an avcC, an hvcC, ...). May be empty for a format that resolves in band. + public var data: Data + /// Human-readable rendition name for a track picker. + public var label: String? + /// Catalog fields the stream cannot reveal itself. + public var hint: VideoHint? + /// The next lower rendition of the same source, of either producer kind: this track then + /// sits one priority below it, so a bandwidth allocator funds that one first. + public var lower: Producer? + /// Describe a video publish. + public init( + format: VideoFormat, data: Data, label: String? = nil, hint: VideoHint? = nil, lower: Producer? = nil + ) { + self.format = format + self.data = data + self.label = label + self.hint = hint + self.lower = lower + } + var ffi: MoqVideoInit { + MoqVideoInit(format: format, data: data, label: label, hint: hint, lower: lower?.ffi) + } + } /// Shares one source's timestamp offset without keeping its broadcast open. public final class Timebase: Sendable { let ffi: MoqMediaTimebase @@ -246,18 +271,11 @@ public enum Media { TrackProducer(try MoqMediaTrackProducer.audio(broadcast: broadcast.ffi, target: target.ffi, init: initData)) } /// Import complete video frames on a named or requested track. - /// - /// Pass `lower`, the next lower rendition of the same source and of either producer - /// kind, to publish this track one priority below it, so a bandwidth allocator funds - /// that one first. - public static func video( - broadcast: BroadcastProducer, initData: VideoInit, target: Target = .named(name: nil), - lower: Producer? = nil - ) throws -> TrackProducer { - var initData = initData - if let lower { initData.lower = lower.ffi } - return TrackProducer( - try MoqMediaTrackProducer.video(broadcast: broadcast.ffi, target: target.ffi, init: initData)) + public static func video(broadcast: BroadcastProducer, initData: VideoInit, target: Target = .named(name: nil)) + throws -> TrackProducer + { + TrackProducer( + try MoqMediaTrackProducer.video(broadcast: broadcast.ffi, target: target.ffi, init: initData.ffi)) } init(_ ffi: MoqMediaTrackProducer) { self.ffi = ffi @@ -357,16 +375,11 @@ public enum Media { let ffi: MoqMediaTrackStreamProducer /// Import a video byte stream, inferring frame boundaries. - /// - /// `lower` is as for ``TrackProducer/video(broadcast:initData:target:lower:)``. - public static func video( - broadcast: BroadcastProducer, initData: VideoInit, target: Target = .named(name: nil), - lower: Producer? = nil - ) throws -> TrackStreamProducer { - var initData = initData - if let lower { initData.lower = lower.ffi } - return TrackStreamProducer( - try MoqMediaTrackStreamProducer.video(broadcast: broadcast.ffi, target: target.ffi, init: initData)) + public static func video(broadcast: BroadcastProducer, initData: VideoInit, target: Target = .named(name: nil)) + throws -> TrackStreamProducer + { + TrackStreamProducer( + try MoqMediaTrackStreamProducer.video(broadcast: broadcast.ffi, target: target.ffi, init: initData.ffi)) } init(_ ffi: MoqMediaTrackStreamProducer) { self.ffi = ffi diff --git a/swift/Sources/Moq/Requests.swift b/swift/Sources/Moq/Requests.swift index 6c4bc066d67..0bb545dd6e5 100644 --- a/swift/Sources/Moq/Requests.swift +++ b/swift/Sources/Moq/Requests.swift @@ -2,7 +2,7 @@ import Foundation import MoqFFI /// A subscriber-requested track that has not been accepted yet. Accept it to get -/// a `TrackProducer` for raw writes, or abort it to reject the subscriber. +/// a `TrackProducer` for raw writes, or reject it to fail the subscriber. public final class TrackRequest: Sendable { let ffi: MoqTrackRequest @@ -32,8 +32,8 @@ public final class TrackRequest: Sendable { } /// Reject the request with an application error code, failing the subscriber. - public func abort(errorCode: UInt16) throws { - try ffi.abort(errorCode: errorCode) + public func reject(errorCode: UInt16) throws { + try ffi.reject(errorCode: errorCode) } } @@ -52,19 +52,14 @@ public final class Fill: Sendable { self.ffi = ffi } - /// The first group to fill. - public var startGroup: UInt64 { - ffi.startGroup() - } - - /// The frame within the first group to start from. - public var startFrame: UInt64 { - ffi.startFrame() + /// The first group to fill, and the frame within it to start from. + public var start: Position { + ffi.start() } /// The exclusive end group. - public var endGroup: UInt64 { - ffi.endGroup() + public var end: UInt64 { + ffi.end() } /// The most urgent delivery priority among the readers waiting on it. @@ -97,7 +92,7 @@ public final class Fill: Sendable { } /// Insert group `sequence` and return a producer for it. The first group starts at - /// `startFrame`, so its frames keep their indices. + /// `start`'s frame, so its frames keep their indices. public func accept(sequence: UInt64) throws -> GroupProducer { GroupProducer(try ffi.accept(sequence: sequence)) } diff --git a/test/interop/clients/go/main.go b/test/interop/clients/go/main.go index f559a96f2c2..c41b5ce5cc8 100644 --- a/test/interop/clients/go/main.go +++ b/test/interop/clients/go/main.go @@ -115,7 +115,7 @@ func dial(ctx context.Context, url string, opts dialOptions, timeout time.Durati ctx, cancel := context.WithTimeout(ctx, timeout) defer cancel() grants := client.Session().Grants() - defer grants.Close() + defer grants.Cancel() grant := grants.Grant() for grant == nil { if grant, err = grants.Changed(ctx); err != nil { diff --git a/test/interop/clients/js-native/socket.ts b/test/interop/clients/js-native/socket.ts index e0a3ae01cfe..8266dc48f4e 100644 --- a/test/interop/clients/js-native/socket.ts +++ b/test/interop/clients/js-native/socket.ts @@ -16,7 +16,7 @@ assert( version, ); const origin = new Moq.Origin.Producer(); -const endpoint = new Moq.socket.Endpoint(origin, Moq.Path.from(prefix), role === "dial" ? "dialer" : "acceptor"); +const endpoint = new Moq.Socket.Endpoint(origin, Moq.Path.from(prefix), role === "dial" ? "dialer" : "acceptor"); const next = role === "accept" ? (protocol === "tcp" ? endpoint.acceptTcp() : endpoint.acceptTrack()) : undefined; const connection = await Moq.Connection.connect({ url: new URL(address), @@ -29,9 +29,9 @@ const lines = createInterface({ input: process.stdin }); const commands = lines[Symbol.asyncIterator](); // Every Incoming is a claim: a pipelined request can outlive its refused metadata. // Verify each new incoming once; a failed caller's dial still fails the harness. -async function accepted(): Promise { +async function accepted(): Promise { for (;;) { - const incoming: Moq.socket.Incoming | Moq.socket.Incoming = + const incoming: Moq.Socket.Incoming | Moq.Socket.Incoming = await (protocol === "tcp" ? endpoint.acceptTcp() : endpoint.acceptTrack()); try { return await incoming.accept(); @@ -59,7 +59,7 @@ try { assert.match(stream.session, /^[0-9a-f]{32}$/); const own = role === "dial" ? 16 : 32; const peer = role === "dial" ? 32 : 16; - if (stream instanceof Moq.socket.TcpStream) { + if (stream instanceof Moq.Socket.TcpStream) { // Exchange a request and reply before FIN, then drain both independent halves. for (let i = 0; i < 3; i++) stream.producer.writeFrame({ payload: new Uint8Array([own + i]) }); stream.producer.finish(); @@ -84,7 +84,7 @@ try { : protocol === "tcp" ? await endpoint.dialTcp("acceptor") : await endpoint.dialTrack("acceptor"); - const resetReader = second instanceof Moq.socket.TcpStream ? second.consumer : second.consumer.ordered(); + const resetReader = second instanceof Moq.Socket.TcpStream ? second.consumer : second.consumer.ordered(); if (role === "dial") { second.producer.writeFrame({ payload: new Uint8Array([48]) }); await assert.rejects(resetReader.readFrame());