Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/interop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ on:
pull_request:
paths:
- "test/lib/**"
- "test/p2p/**"
- "js/p2p/**"
- "test/interop/**"
- "test/max-age/**"
- "test/portrait/**"
Expand Down Expand Up @@ -102,6 +104,11 @@ jobs:
run: nix develop --command just test bare-fin
shell: bash -leo pipefail {0}

- name: P2P data-channel transport
if: ${{ !cancelled() && steps.setup.outcome == 'success' }}
run: nix develop --command just test p2p
shell: bash -leo pipefail {0}

- name: Interop (full matrix)
if: ${{ !cancelled() && steps.setup.outcome == 'success' }}
run: nix develop --command just test interop --all
Expand Down
31 changes: 31 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion demo/web/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,9 @@ ui.run((effect) => {

// Report the transport negotiated by the live connection.
const transport = effect.get(connection.transport);
$("network-transport").textContent = transport ? (transport === "websocket" ? "WebSocket" : "WebTransport") : "";
$("network-transport").textContent = transport
? { websocket: "WebSocket", webtransport: "WebTransport", webrtc: "WebRTC" }[transport]
: "";

const video = effect.get(watch.video.out.stats);
const audio = effect.get(watch.audio.out.stats);
Expand Down
4 changes: 3 additions & 1 deletion demo/web/src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,9 @@ ui.run((effect) => {
// Report the transport negotiated by the live connection.
ui.run((effect) => {
const transport = effect.get(publish.connection.transport);
$("network-transport").textContent = transport ? (transport === "websocket" ? "WebSocket" : "WebTransport") : "";
$("network-transport").textContent = transport
? { websocket: "WebSocket", webtransport: "WebTransport", webrtc: "WebRTC" }[transport]
: "";
});

// Audio: the resolved audio config (codec / sample rate / channels / bitrate).
Expand Down
11 changes: 10 additions & 1 deletion doc/concept/transport.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Transport
description: QUIC, WebTransport, the WebSocket fallback, raw QUIC, stream listeners, and iroh
description: QUIC, WebTransport, the WebSocket fallback, raw QUIC, stream listeners, WebRTC data channels, and iroh
---

# Transport
Expand Down Expand Up @@ -62,6 +62,15 @@ authenticate through the same JWT and public-access rules as QUIC, and the Unix
socket can additionally allowlist the connecting process by uid, gid, or pid.
See [stream listeners](/bin/relay/auth#stream-listeners).

## WebRTC data channels (experimental)

Applications that already negotiate a browser `RTCPeerConnection` can pass an
[`@moq/p2p` transport](https://github.com/moq-dev/moq/tree/main/js/p2p) to
`@moq/net`'s connect or accept. One reliable ordered data channel carries qmux
records, with no datagrams. The application supplies signaling and ICE and
keeps ownership of the peer connection; closing the transport closes only its
channel. SCTP ordering means one lost packet can hold back every stream.

## iroh (peer-to-peer, experimental)

[iroh](https://www.iroh.computer/) swaps "dial a hostname" for "dial a public
Expand Down
20 changes: 15 additions & 5 deletions drafts/draft-lcurley-moq-lite.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ normative:
date: false
RFC3986:
RFC6455:
RFC8831:
RFC9000:
RFC9002:
RFC9562:
Expand Down Expand Up @@ -93,7 +94,6 @@ The moq-lite layer provides fanout, prioritization, and caching even for latency

## Session
A Session consists of a connection between a client and a server.
There is currently no P2P support within QUIC so it's out of scope for moq-lite.

The moq-lite version identifier is `moq-lite-xx` where `xx` is the two-digit draft version.
The identifier for this draft is `moq-lite-07-wip` while it is a work in progress, and becomes `moq-lite-07` once finalized.
Expand Down Expand Up @@ -218,8 +218,8 @@ See [Transports](#transports) for the bindings.
How the underlying connection is authenticated is out-of-scope for this draft.

## Transports {#transports}
moq-lite defines four transport bindings.
All four carry the same control and data streams defined elsewhere in this document; they differ only in how QUIC streams are multiplexed onto the underlying connection.
moq-lite defines five transport bindings.
All five carry the same control and data streams defined elsewhere in this document; they differ only in how QUIC streams are multiplexed onto the underlying connection.

|----|---------------------|------------------|----------------------|
| | Transport | ALPN / Identifier | Record framing |
Expand All @@ -228,15 +228,23 @@ All four carry the same control and data streams defined elsewhere in this docum
| 2 | WebTransport / H3 | `moq-lite-xx` (CONNECT header) | Native WebTransport streams |
| 3 | Qmux over TCP/TLS | `moq-lite-xx` (ALPN over TLS) | Qmux Record [qmux] |
| 4 | Qmux over WebSocket | `moq-lite-xx` (Sec-WebSocket-Protocol) | WebSocket message [qmuxws] |
| 5 | Qmux over RTCDataChannel | `moq-lite-xx` (signaling and DataChannel protocol) | DataChannel message |

For bindings 1 and 2, moq-lite uses the underlying QUIC/WebTransport stream APIs directly.
QUIC datagrams (see [Datagrams](#datagrams)) are supported by bindings 1 and 2 only; a publisher MUST NOT emit datagrams on bindings 3 and 4.
QUIC datagrams (see [Datagrams](#datagrams)) are supported by bindings 1 and 2 only; a publisher MUST NOT emit datagrams on bindings 3, 4, and 5.

For binding 3, a client opens a TCP connection, performs a TLS handshake, and negotiates the moq-lite version as the ALPN token.
Each direction of the TLS byte stream then carries Qmux Records as defined in [qmux].

For binding 4, a client opens a WebSocket connection [RFC6455] offering the moq-lite version as the subprotocol, per [qmuxws]: each WebSocket binary message carries one Qmux Record's `Frames` payload, with the message boundary replacing the Record `Size` field.

For binding 5, WebRTC Data Channels [RFC8831] carry Qmux. Signaling agrees the moq-lite version before establishing the peer connection.
The client creates one reliable, ordered RTCDataChannel with that version as its protocol; the server MUST refuse a channel whose protocol differs or whose delivery is unordered or partially reliable.
The binding uses Qmux draft-01: each binary DataChannel message carries one Record's `Frames` payload, with the message boundary replacing the Record `Size` field.
The record limit defaults to 16 KiB and MUST NOT exceed the negotiated SCTP `maxMessageSize`; an endpoint MUST refuse a message limit below Qmux's minimum record size.
A sender splits stream payloads at frame boundaries to fit this limit, never within a frame.
Signaling, peer admission, and ICE server selection are application concerns.

All other moq-lite semantics (stream types, message encoding, flow control, etc.) are identical across bindings.

## Termination
Expand Down Expand Up @@ -1561,6 +1569,8 @@ The `Message Length` describes the payload size on the wire.

## moq-lite-07

- Added the reliable ordered RTCDataChannel binding, with one Qmux Record per binary message and no datagrams.

- Added `Group Order` to SUBSCRIBE and SUBSCRIBE_UPDATE: descending (0x0, the previous fixed order) or ascending (0x1). A relay requests descending upstream unless every subscription it serves is ascending.
- Listed the shared INVALID_REQUEST_ID (0x4) session code used by moq-transport drafts 14 through 16.

Expand Down Expand Up @@ -1762,7 +1772,7 @@ Some of these fields occur in multiple messages.


# Security Considerations
moq-lite inherits the transport security of the underlying connection: QUIC and WebTransport provide confidentiality and integrity via TLS 1.3, and the Qmux bindings run over TLS (TCP) or a `wss://` WebSocket. How that connection is authenticated is out of scope (see [Connection](#connection)). The considerations below are specific to moq-lite.
moq-lite inherits the transport security of the underlying connection: QUIC and WebTransport provide confidentiality and integrity via TLS 1.3, the Qmux TCP and WebSocket bindings run over TLS, and the RTCDataChannel binding inherits DTLS security. How that connection is authenticated is out of scope (see [Connection](#connection)). The considerations below are specific to moq-lite.

## Bandwidth Probing
The `Increase` Probe level (see [Probe Parameter](#probe-parameter)) lets a subscriber ask the publisher to pad the connection up to a target bitrate. A publisher MUST NOT treat the target as authorization to send beyond what congestion control allows: padding is bounded by the congestion window, so probing cannot be used to amplify traffic toward the subscriber or a spoofed address. A publisher that only advertised `Report` MUST NOT pad above its current sending rate. Because all data flows on an established, congestion-controlled session to the connecting peer, moq-lite offers no off-path amplification vector.
Expand Down
5 changes: 5 additions & 0 deletions js/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,8 @@ clock-bench:
audio-warmup-bench:
bun install --frozen-lockfile
bun watch/bench/warmup.ts

# Sweep data-channel qmux stream count and payload size.
p2p-bench:
bun install --frozen-lockfile
bun p2p/bench/transport.ts
5 changes: 3 additions & 2 deletions js/net/src/connection/goaway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
import { RefusedRedirect } from "../error.ts";
import * as Time from "../time.ts";
import type { Transport } from "./transport.ts";

/** A peer's GOAWAY as a session received it. */
export interface Drain {
Expand Down Expand Up @@ -60,7 +61,7 @@ export function handover(cap: Time.Milli, timeout?: Time.Milli): Time.Milli {
*
* @internal
*/
export function dialed(primary: URL, transport: "webtransport" | "websocket", websocket?: URL): URL {
export function dialed(primary: URL, transport: Transport, websocket?: URL): URL {
return transport === "websocket" && websocket ? websocket : primary;
}

Expand All @@ -70,7 +71,7 @@ export function dialed(primary: URL, transport: "webtransport" | "websocket", we
*
* @internal
*/
export function pinnedTransport(transport: "webtransport" | "websocket", configured: boolean): boolean {
export function pinnedTransport(transport: Transport, configured: boolean): boolean {
return configured && transport === "webtransport";
}

Expand Down
7 changes: 4 additions & 3 deletions js/net/src/connection/transport.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import Session from "@moq/qmux";

/** The wire transport a session runs over. */
export type Transport = "webtransport" | "websocket";
export type Transport = "webtransport" | "websocket" | "webrtc";

/**
* The transport a live session is actually running over. qmux implements the `WebTransport`
* interface on top of a WebSocket, so the two are indistinguishable by shape.
* The transport a live session is actually running over. WebSocket and WebRTC adapters
* share the WebTransport shape; WebRTC carries its label to avoid a dependency cycle.
*
* @internal Not re-exported from the package entrypoint.
*/
export function transportOf(quic: WebTransport): Transport {
if ("transport" in quic && quic.transport === "webrtc") return "webrtc";
return quic instanceof Session ? "websocket" : "webtransport";
}
2 changes: 1 addition & 1 deletion js/net/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* MoQ networking layer for browsers: connect to a relay, then publish and consume
* broadcasts, tracks, groups, and frames over WebTransport (or a WebSocket fallback).
* broadcasts, tracks, groups, and frames over WebTransport, its WebSocket fallback, or a supplied transport.
*
* @module
*/
Expand Down
29 changes: 29 additions & 0 deletions js/p2p/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# @moq/p2p

A WebTransport-shaped MoQ transport over one reliable ordered WebRTC data channel.
Applications own signaling, ICE configuration, admission, and the `RTCPeerConnection`.
The transport owns its data channel. Closing it leaves the peer connection open.

```ts
import { Transport } from "@moq/p2p";
import { Connection } from "@moq/net";

// The application agrees this ALPN with its peer through signaling.
const protocol = "moq-lite-07-wip";
const transport = Transport.connect({ peer, protocol });
const connection = await Connection.connect({ url, transport });
```

The receiver adopts the incoming channel with
`Transport.accept({ peer, channel, protocol })` and passes that transport to
`Connection.accept({ url, transport })`. The channel's protocol must match the
agreed ALPN. Unordered or partially reliable channels are refused.

Each binary data-channel message carries one qmux-01 record without its size
field. Records are capped at 16 KiB and the negotiated SCTP message limit;
a limit below qmux-01's 16,382-byte minimum is refused. There are no datagrams.
SCTP preserves ordering, so packet loss can hold back every stream.

`just test p2p` runs SETUP and a media transfer between two real peer connections
in Chromium. `just js p2p-bench` sweeps stream counts and payload sizes through
an in-memory data-channel pair.
57 changes: 57 additions & 0 deletions js/p2p/bench/transport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/** Sweep concurrent streams and bytes per stream through the real qmux adapter. @module */
import { peers } from "../src/mock.ts";
import { Transport } from "../src/transport.ts";

async function receive(stream: ReadableStream<Uint8Array>) {
const reader = stream.getReader();
let bytes = 0;
try {
for (;;) {
const { value, done } = await reader.read();
if (done) return bytes;
bytes += value.byteLength;
}
} finally {
reader.releaseLock();
}
}

async function measure(streams: number, bytes: number) {
const { left, right } = peers();
const protocol = "moq-lite-07-wip";
const client = Transport.connect({ peer: left.asPeer(), protocol });
const server = Transport.accept({ peer: right.asPeer(), channel: right.incoming.asChannel(), protocol });
left.channel.open();
right.incoming.open();
await Promise.all([client.ready, server.ready]);
const reader = server.incomingUnidirectionalStreams.getReader();
const receiving = (async () => {
const pending: Promise<number>[] = [];
for (let i = 0; i < streams; i++) {
const { value, done } = await reader.read();
if (done) throw new Error("missing benchmark stream");
pending.push(receive(value));
}
return (await Promise.all(pending)).reduce((total, value) => total + value, 0);
})();
const payload = new Uint8Array(bytes);
const start = performance.now();
await Promise.all(
Array.from({ length: streams }, async () => {
const writer = (await client.createUnidirectionalStream()).getWriter();
await writer.write(payload);
await writer.close();
}),
);
const delivered = await receiving;
const elapsed = performance.now() - start;
if (delivered !== streams * bytes || left.channel.records.some((record) => record.byteLength > 16384))
throw new Error("benchmark delivery changed");
console.log(`${streams},${bytes},${left.channel.records.length},${elapsed.toFixed(3)}`);
reader.releaseLock();
client.close();
await server.closed;
}

console.log("streams,bytes_per_stream,records,elapsed_ms");
for (const streams of [1, 8, 64]) for (const bytes of [1024, 16384, 262144]) await measure(streams, bytes);
1 change: 1 addition & 0 deletions js/p2p/bench/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "../tsconfig.json", "compilerOptions": { "rootDir": "..", "noEmit": true }, "include": ["transport.ts"] }
31 changes: 31 additions & 0 deletions js/p2p/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@moq/p2p",
"type": "module",
"version": "0.1.0",
"description": "Media over QUIC transports over reliable WebRTC data channels.",
"license": "(MIT OR Apache-2.0)",
"repository": {
"type": "git",
"url": "git+https://github.com/moq-dev/moq.git",
"directory": "js/p2p"
},
"sideEffects": false,
"exports": {
".": "./src/index.ts"
},
"scripts": {
"build": "rimraf dist && tsc -b tsconfig.build.json && bun ../common/package.ts",
"check": "tsc --noEmit && tsc --noEmit --project bench/tsconfig.json",
"test": "bun test --only-failures",
"release": "bun ../common/release.ts"
},
"dependencies": {
"@moq/qmux": "^0.3.3"
},
"devDependencies": {
"@types/bun": "^1.4.2",
"@typescript/lib-dom": "npm:@types/web@^0.0.350",
"rimraf": "^6.1.3",
"typescript": "7.0.2"
}
}
Loading
Loading