diff --git a/packages/spf/src/network/moqt/session.ts b/packages/spf/src/network/moqt/session.ts index 105b4f1d74..e849db2543 100644 --- a/packages/spf/src/network/moqt/session.ts +++ b/packages/spf/src/network/moqt/session.ts @@ -60,9 +60,9 @@ import { type BidirectionalStreamLike, openRequestStream, type RequestStream } f * Structural subset of `WebTransport` the session driver needs. A real `WebTransport` instance satisfies this; tests * provide an in-memory fake (the same seam pattern as SPF's fetch injection). * - * Publish-side fill failure signaling requires writers with WebTransport's `commit()` method, which preserves the - * written prefix across a reset. Support is detected on the control stream's writer and must be consistent across - * outgoing streams. Without it, nonempty fill requests are rejected with NOT_SUPPORTED. + * Publish-side fill failure signaling uses WebTransport's `commit()` writer method where it exists, which preserves the + * written FETCH_HEADER across the reset. Without it the subscription is still served, and the reset that fails the fill + * may discard its header. */ export interface MoqtTransport { readonly incomingUnidirectionalStreams: ReadableStream>; diff --git a/packages/spf/src/publish/session/publish-session.ts b/packages/spf/src/publish/session/publish-session.ts index 5bcf0b210d..9424d1f32f 100644 --- a/packages/spf/src/publish/session/publish-session.ts +++ b/packages/spf/src/publish/session/publish-session.ts @@ -643,8 +643,15 @@ class MoqtPublishSessionImpl implements MoqtPublishSession { * Answer a nonempty fill request (see `#fillRequested`). This origin serves no fills, so it meets the §5.1.3.1 * requirement the honest way: open a uni stream, write the FETCH_HEADER carrying the initiating Request ID so the * subscriber can correlate the failure, then reset it. A reset is the fill-failure signal — a FIN would falsely claim - * the fill range was delivered in full. Moq-relay 0.14.17 requests the current group this way; the reliable reset - * keeps that join from waiting on a fill that never arrives. + * the fill range was delivered in full. Moq-relay requests the current group this way (0.14.17 through 0.17.0) on + * every upstream SUBSCRIBE once the track has content. + * + * The header survives the reset only where the writer has WebTransport's `commit()` (RESET_STREAM_AT's reliable + * prefix). Without it (Chromium as of 147) the subscription is still served: the reset may discard the header, and + * the subscriber then sees no fill at all — which, like a failed one, leaves its join at the next group boundary. + * Refusing the SUBSCRIBE instead would fail every relay's second pull of a live track (the real subscribe after a + * cross-relay TRACK_INFO probe, or any re-subscribe after the last viewer left), so nothing would play across a relay + * mesh. */ #openAndResetFill(requestId: number, subscriber: SubscriberStream): void { const fill: FillStream = {}; @@ -660,20 +667,26 @@ class MoqtPublishSessionImpl implements MoqtPublishSession { if (!subscriber.fills.has(fill) || subscriber.finished) return; - // All WebTransport writers expose the same capabilities, probed - // on the control writer before accepting a nonempty fill. Check - // this stream too so an inconsistent injected transport never - // writes a header it cannot preserve through the reset. - if (!hasMethods(writer, ['commit'])) return; - + // PUBLISH_DONE's Stream Count includes fill fetch streams + // (§10.12). Count in the same synchronous step that commits this + // stream to its FETCH_HEADER — the track publisher's rule for + // subgroup streams — so any PUBLISH_DONE covers every fill that + // carries a header. A fill still opening when the track ended + // was stopped by the guard above: it is reset with no header, so + // the subscriber cannot attribute it to this subscription and + // leaving it uncounted cannot put the subscriber over the count. + // A counted fill whose reset discards its header (no commit()) + // or whose write rejects over-counts by one; for that §10.12 has + // the subscriber fall back to a timeout. subscriber.fillStreams++; await writer.write(encodeFetchHeader(requestId)); if (!subscriber.fills.has(fill) || subscriber.finished) return; // write() only hands bytes to the transport. commit() includes - // them in the reliable prefix of RESET_STREAM_AT. - writer.commit(); + // them in the reliable prefix of RESET_STREAM_AT; without it the + // reset below is best-effort for the header. + if (hasMethods(writer, ['commit'])) writer.commit(); } catch { // Session closing or the peer went away before the header landed. } finally { @@ -1044,18 +1057,6 @@ class MoqtPublishSessionImpl implements MoqtPublishSession { subscribe.parameters.fillParameters ); - if (fillRequested && !hasMethods(this.#controlWriter, ['commit'])) { - try { - await writer.write(encodeRequestError(REQUEST_ERROR_CODE.NOT_SUPPORTED, 'fill requires reliable stream reset')); - await writer.close(); - } catch { - // Peer cancelled. - } - - await reader.cancel(); - return; - } - track.subscribers.push(subscriber); // STOP_SENDING on the response direction cancels the subscription @@ -1166,7 +1167,6 @@ class MoqtPublishSessionImpl implements MoqtPublishSession { // here, immediately resets) a fill fetch stream keyed by the // update's own Request ID (§5.1.3.1); an empty range opens none. const fillRequested = this.#fillRequested(largestObject, subscriber, subscribe.parameters, fillParameters); - const unsupportedFill = fillRequested && !hasMethods(this.#controlWriter, ['commit']); this.#callbacks.onRequestUpdate?.({ requestId: subscribe.requestId, @@ -1175,7 +1175,7 @@ class MoqtPublishSessionImpl implements MoqtPublishSession { }); if (!subscriber.finished) { - if (Object.keys(unsupported).length > 0 || unsupportedFill) { + if (Object.keys(unsupported).length > 0) { // Acknowledging an update we did not apply would leave the // peer serving stale expectations (a filter or range it // believes is in effect). Forward State is applied above diff --git a/packages/spf/src/publish/session/tests/publish-session.test.ts b/packages/spf/src/publish/session/tests/publish-session.test.ts index c9e4fdb77d..099e38f975 100644 --- a/packages/spf/src/publish/session/tests/publish-session.test.ts +++ b/packages/spf/src/publish/session/tests/publish-session.test.ts @@ -80,9 +80,11 @@ function collectTrack( async function makeFillHarness({ reliableReset = true, openFillStream, + callbacks, }: { reliableReset?: boolean; openFillStream?: () => Promise>; + callbacks?: MoqtPublishSessionCallbacks; } = {}) { const pair = createTransportPair(); const openUniStream = pair.client.createUnidirectionalStream.bind(pair.client); @@ -102,7 +104,7 @@ async function makeFillHarness({ return stream; }; - const session = createMoqtPublishSession(pair.client); + const session = createMoqtPublishSession(pair.client, { callbacks }); const fills: { requestId: number; reset: boolean }[] = []; const streams = pair.server.incomingUnidirectionalStreams.getReader(); @@ -146,8 +148,11 @@ async function makeFillHarness({ }; } -/** A buffered send stream: reset discards everything except the explicitly committed prefix. */ -function makeBufferedFillStream() { +/** + * A buffered send stream: reset discards everything except the explicitly committed prefix. Without `reliableReset` the + * writer has no `commit()` — a browser WebTransport today — so a reset discards everything still unread. + */ +function makeBufferedFillStream({ reliableReset = true }: { reliableReset?: boolean } = {}) { const chunks: Uint8Array[] = []; let committed = 0; let readIndex = 0; @@ -188,12 +193,14 @@ function makeBufferedFillStream() { }); const getWriter = writable.getWriter.bind(writable); - writable.getWriter = () => - Object.assign(getWriter(), { - commit() { - committed = chunks.length; - }, - }); + if (reliableReset) { + writable.getWriter = () => + Object.assign(getWriter(), { + commit() { + committed = chunks.length; + }, + }); + } return { writable, readable, reset: () => reset }; } @@ -1320,39 +1327,230 @@ describe('createMoqtPublishSession', () => { await finish(); }); - it('rejects nonempty fills without reliable reset support while allowing ordinary subscriptions', async () => { - const { pair, session, fills, finish } = await makeFillHarness({ reliableReset: false }); + it.each([{ reliableReset: true }, { reliableReset: false }])( + 'leaves a fill still opening when the track ends out of the Stream Count, headerless (reliable reset: $reliableReset)', + async ({ reliableReset }) => { + let resolveOpen!: (stream: WritableStream) => void; + const openFillStream = vi.fn( + () => + new Promise>((resolve) => { + resolveOpen = resolve; + }) + ); + const { pair, session, finish } = await makeFillHarness({ reliableReset, openFillStream }); + const track = session.registerTrack({ + trackNamespace: NAMESPACE, + trackName: 'video', + getLargestObject: () => ({ group: 4, object: 2 }), + getStreamCount: () => 3, + }); + const sub = await rawSubscribe(pair.server, 'video', 245); + + await vi.waitFor(() => expect(sub.received[0]?.kind).toBe('subscribe-ok')); + await sub.send(encodeRequestUpdate(247, { fillParameters: {} })); + await vi.waitFor(() => expect(openFillStream).toHaveBeenCalledOnce()); + + // The track ends while the fill's stream is still opening. + track.end(); + await vi.waitFor(() => expect(sub.ended()).toBe(true)); + + const write = vi.fn(); + const abort = vi.fn(); + + resolveOpen(new WritableStream({ write, abort })); + await vi.waitFor(() => expect(abort).toHaveBeenCalledOnce()); + + // A fill is counted in the same step that commits it to its + // FETCH_HEADER, as the track publisher counts subgroup streams. + // This one opened after PUBLISH_DONE, so it gets no header and is + // reset bare: the peer cannot attribute it to the subscription, so + // leaving it out of the count cannot put the peer over it. + expect(write).not.toHaveBeenCalled(); + expect(sub.received.at(-1)).toMatchObject({ + kind: 'publish-done', + statusCode: PUBLISH_DONE_STATUS.TRACK_ENDED, + streamCount: 3, + }); + await finish(); + } + ); + + it.each([{ probeEnds: 'before' }, { probeEnds: 'after' }])( + "serves a relay's second pull of a live track without reliable reset support (probe ends $probeEnds the pull)", + async ({ probeEnds }) => { + const bindings: (number | undefined)[] = []; + const ended: number[] = []; + const { pair, session, fills, finish } = await makeFillHarness({ + reliableReset: false, + callbacks: { + onTrackBinding: ({ trackAlias }) => bindings.push(trackAlias), + onSubscribeEnd: ({ requestId }) => ended.push(requestId), + }, + }); + let largest: { group: number; object: number } | undefined; + + session.registerTrack({ trackNamespace: NAMESPACE, trackName: 'catalog', getLargestObject: () => largest }); + + // Moq-relay's upstream SUBSCRIBE (0.14.17 through 0.17.0): Next Object, + // plus a fill of the current group's head. + const relayPull = (requestId: number) => + openRawRequest( + pair.server, + encodeSubscribe({ + requestId, + trackNamespace: NAMESPACE, + trackName: 'catalog', + parameters: { + forward: 1, + subscriberPriority: 128, + locationFilter: { type: 'next-object' }, + fillParameters: { locationFilter: { type: 'relative-group', groupsBeforeNext: 1 } }, + groupOrder: 'descending', + includeProperties: 1, + }, + }) + ); + + // A cross-relay viewer's first pull is the relay's TRACK_INFO probe: + // nothing published yet, so there is nothing to fill. + const probe = await relayPull(235); + + await vi.waitFor(() => expect(probe.received.map((m) => m.kind)).toEqual(['subscribe-ok'])); + + // The probe's bind put the catalog on the wire, then the relay + // cancelled it and pulled again for the viewer — now with a nonempty + // fill this transport cannot fail reliably. The pull is served anyway. + largest = { group: 0, object: 0 }; + + if (probeEnds === 'before') await probe.reset(); + + const pull = await relayPull(237); + + await vi.waitFor(() => { + expect(pull.received.map((m) => m.kind)).toEqual(['subscribe-ok']); + expect(bindings.at(-1)).toBe(237); + expect(fills).toEqual([{ requestId: 237, reset: true }]); + }); + + // The probe's end may be handled after the pull is bound: it reports + // the newest live binding, not an unbind, so the pull keeps it. + if (probeEnds === 'after') await probe.reset(); + + await vi.waitFor(() => expect(ended).toContain(235)); + expect(bindings.at(-1)).toBe(237); + expect(pull.ended()).toBe(false); + + // A fill requested by REQUEST_UPDATE is acknowledged and failed the same way. + await pull.send( + encodeRequestUpdate(239, { + fillParameters: { locationFilter: { type: 'relative-group', groupsBeforeNext: 1 } }, + }) + ); + await vi.waitFor(() => { + expect(pull.received.map((m) => m.kind)).toEqual(['subscribe-ok', 'request-ok']); + expect(fills).toEqual([ + { requestId: 237, reset: true }, + { requestId: 239, reset: true }, + ]); + }); + expect(pull.ended()).toBe(false); + await finish(); + } + ); + + it('still resets the fill when the reset may discard its header (no reliable reset)', async () => { + const fill = makeBufferedFillStream({ reliableReset: false }); + const { pair, session, finish } = await makeFillHarness({ + reliableReset: false, + openFillStream: async () => fill.writable, + }); session.registerTrack({ trackNamespace: NAMESPACE, trackName: 'video', getLargestObject: () => ({ group: 4, object: 2 }), }); - const rejected = await openRawRequest( + const sub = await openRawRequest( pair.server, encodeSubscribe({ - requestId: 235, + requestId: 241, trackNamespace: NAMESPACE, trackName: 'video', parameters: { fillParameters: {} }, }) ); - await vi.waitFor(() => expect(rejected.ended()).toBe(true)); - expect(rejected.received).toMatchObject([{ kind: 'request-error', errorCode: REQUEST_ERROR_CODE.NOT_SUPPORTED }]); + await vi.waitFor(() => { + expect(sub.received.map((m) => m.kind)).toEqual(['subscribe-ok']); + expect(fill.reset()).toBeDefined(); + }); - const sub = await rawSubscribe(pair.server, 'video', 237); + // Nothing preserved the header through the reset: the peer sees only + // the reset, and the subscription is unaffected. + const reader = new StreamReader(fill.readable); - await vi.waitFor(() => expect(sub.received[0]?.kind).toBe('subscribe-ok')); - await sub.send(encodeRequestUpdate(239, { fillParameters: { locationFilter: { type: 'next-object' } } })); - await vi.waitFor(() => expect(sub.received[1]?.kind).toBe('request-ok')); - await sub.send(encodeRequestUpdate(241, { fillParameters: {} })); - await vi.waitFor(() => expect(sub.ended()).toBe(true)); - expect(sub.received[2]).toMatchObject({ kind: 'request-error', errorCode: REQUEST_ERROR_CODE.NOT_SUPPORTED }); - expect(fills).toEqual([]); + await expect(reader.readVarint()).rejects.toThrow('fill fetch streams are not served'); + expect(sub.ended()).toBe(false); await finish(); }); + it.each([{ headerSurvives: true }, { headerSurvives: false }])( + 'counts a fill reset without reliable reset in the PUBLISH_DONE Stream Count (header survives: $headerSurvives)', + async ({ headerSurvives }) => { + const buffered = makeBufferedFillStream({ reliableReset: false }); + const { pair, session, fills, finish } = await makeFillHarness({ + reliableReset: false, + openFillStream: headerSurvives ? undefined : async () => buffered.writable, + }); + const track = session.registerTrack({ + trackNamespace: NAMESPACE, + trackName: 'video', + getLargestObject: () => ({ group: 4, object: 2 }), + getStreamCount: () => 3, + }); + const sub = await openRawRequest( + pair.server, + encodeSubscribe({ + requestId: 243, + trackNamespace: NAMESPACE, + trackName: 'video', + parameters: { fillParameters: {} }, + }) + ); + + await vi.waitFor(() => { + expect(sub.received.map((m) => m.kind)).toEqual(['subscribe-ok']); + + if (headerSurvives) expect(fills).toEqual([{ requestId: 243, reset: true }]); + else expect(buffered.reset()).toBeDefined(); + }); + + if (!headerSurvives) { + // The peer saw only the reset, so it cannot tell the stream + // belonged to this subscription. + await expect(new StreamReader(buffered.readable).readVarint()).rejects.toThrow( + 'fill fetch streams are not served' + ); + } + + track.end(); + await vi.waitFor(() => { + expect(sub.ended()).toBe(true); + }); + // §10.12 counts the streams the publisher opened, fill fetch + // streams included: three from the track publisher plus the fill, + // whether or not its header outlived the reset. A subscriber waits + // out a timeout for a stream reset before its header, but one that + // receives more streams than the count may close the session. + expect(sub.received.at(-1)).toMatchObject({ + kind: 'publish-done', + statusCode: PUBLISH_DONE_STATUS.TRACK_ENDED, + streamCount: 4, + }); + await finish(); + } + ); + it('rejects a subscription update it cannot apply instead of acknowledging it', async () => { const endedRequests: number[] = []; const bindings: (number | undefined)[] = []; diff --git a/packages/spf/src/publish/tests/helpers/relay-hub.ts b/packages/spf/src/publish/tests/helpers/relay-hub.ts index 271e8c61a4..bca23d1027 100644 --- a/packages/spf/src/publish/tests/helpers/relay-hub.ts +++ b/packages/spf/src/publish/tests/helpers/relay-hub.ts @@ -178,6 +178,11 @@ export function createRelayHub( options: { /** Delay individual downstream object streams to exercise jitter and late arrivals. */ deliveryDelayMs?: (trackName: string, object: Readonly) => number; + /** + * Give the publisher's stream writers WebTransport's `commit()` (RESET_STREAM_AT). Default `true`; `false` models + * the browsers that ship WebTransport without it (Chromium 147). + */ + reliableReset?: boolean; } = {} ): RelayHub { let destroyed = false; @@ -254,7 +259,8 @@ export function createRelayHub( // These zero-buffer pipes settle writes only after the peer reads // them, so the reliable prefix is already delivered at commit time. - stream.getWriter = () => Object.assign(getWriter(), { commit() {} }); + if (options.reliableReset !== false) stream.getWriter = () => Object.assign(getWriter(), { commit() {} }); + return stream; }; diff --git a/packages/spf/src/publish/tests/relay-hub.test.ts b/packages/spf/src/publish/tests/relay-hub.test.ts index d90ee928c5..e0033621a3 100644 --- a/packages/spf/src/publish/tests/relay-hub.test.ts +++ b/packages/spf/src/publish/tests/relay-hub.test.ts @@ -10,54 +10,61 @@ import { createRelayHub } from './helpers/relay-hub'; * cross-engine suites are too coarse to pin down. */ describe('createRelayHub', () => { - it.each([false, true])('follows live after a relay 0.14.17 pull (existing content: %s)', async (hasContent) => { - const hub = createRelayHub(); - const { transport } = hub.connectPublisher({ url: 'https://relay.test/moq', namespace: ['live'] }); - const onSubscribe = vi.fn(); - const onTrackBinding = vi.fn(); - const onClosed = vi.fn(); - const session = createMoqtPublishSession(transport, { callbacks: { onSubscribe, onTrackBinding, onClosed } }); + it.each([ + { hasContent: false, reliableReset: true }, + { hasContent: true, reliableReset: true }, + { hasContent: true, reliableReset: false }, + ])( + 'follows live after a relay 0.14.17 pull (existing content: $hasContent, reliable reset: $reliableReset)', + async ({ hasContent, reliableReset }) => { + const hub = createRelayHub({ reliableReset }); + const { transport } = hub.connectPublisher({ url: 'https://relay.test/moq', namespace: ['live'] }); + const onSubscribe = vi.fn(); + const onTrackBinding = vi.fn(); + const onClosed = vi.fn(); + const session = createMoqtPublishSession(transport, { callbacks: { onSubscribe, onTrackBinding, onClosed } }); - try { - await session.ready; - session.announce(['live']); - session.registerTrack({ - trackNamespace: ['live'], - trackName: 'video', - getLargestObject: () => (hasContent ? { group: 255, object: 128 } : undefined), - }); - hub.subscribeUpstream('video'); - await vi.waitFor(() => { - expect(onSubscribe).toHaveBeenCalledWith( - expect.objectContaining({ - parameters: expect.objectContaining({ - subscriberPriority: 255, - locationFilter: { type: 'next-object' }, - fillParameters: { locationFilter: { type: 'relative-group', groupsBeforeNext: 1 } }, - }), - }) - ); - expect(onTrackBinding).toHaveBeenCalledWith({ trackName: 'video', trackAlias: expect.any(Number) }); - expect(hub.fills).toEqual(hasContent ? [{ requestId: 3, reset: true }] : []); - }); + try { + await session.ready; + session.announce(['live']); + session.registerTrack({ + trackNamespace: ['live'], + trackName: 'video', + getLargestObject: () => (hasContent ? { group: 255, object: 128 } : undefined), + }); + hub.subscribeUpstream('video'); + await vi.waitFor(() => { + expect(onSubscribe).toHaveBeenCalledWith( + expect.objectContaining({ + parameters: expect.objectContaining({ + subscriberPriority: 255, + locationFilter: { type: 'next-object' }, + fillParameters: { locationFilter: { type: 'relative-group', groupsBeforeNext: 1 } }, + }), + }) + ); + expect(onTrackBinding).toHaveBeenCalledWith({ trackName: 'video', trackAlias: expect.any(Number) }); + expect(hub.fills).toEqual(hasContent ? [{ requestId: 3, reset: true }] : []); + }); - const trackAlias = onTrackBinding.mock.calls[0]![0].trackAlias; - const stream = createSubgroupWriter(await session.openUniStream(), { - trackAlias, - groupId: 256, - endOfGroup: true, - }); + const trackAlias = onTrackBinding.mock.calls[0]![0].trackAlias; + const stream = createSubgroupWriter(await session.openUniStream(), { + trackAlias, + groupId: 256, + endOfGroup: true, + }); - await stream.writeObject({ objectId: 0, payload: Uint8Array.of(1, 2, 3) }); - await stream.fin(); - await vi.waitFor(() => expect(hub.objectCount('video')).toBe(1)); - expect(hub.fills).toEqual(hasContent ? [{ requestId: 3, reset: true }] : []); - expect(onClosed).not.toHaveBeenCalled(); - } finally { - session.destroy(); - hub.destroy(); + await stream.writeObject({ objectId: 0, payload: Uint8Array.of(1, 2, 3) }); + await stream.fin(); + await vi.waitFor(() => expect(hub.objectCount('video')).toBe(1)); + expect(hub.fills).toEqual(hasContent ? [{ requestId: 3, reset: true }] : []); + expect(onClosed).not.toHaveBeenCalled(); + } finally { + session.destroy(); + hub.destroy(); + } } - }); + ); it('re-pulls a track the publisher ended and re-registered while demand stands', async () => { const hub = createRelayHub();