From 35391138b39b1387150393ca0f63381580a3627e Mon Sep 17 00:00:00 2001 From: Matt McClure Date: Sat, 1 Aug 2026 18:05:42 +0000 Subject: [PATCH 01/11] fix(spf): harden moqt session and wire handling from review - decodeControlMessage normalizes RangeError from truncated frame bodies to MoqtProtocolError, so malformed frames terminate the session on every path instead of being swallowed by the accept loops (the control-stream case previously left a zombie session). - Request timeouts now cancel the request stream before failing the record, so a late SUBSCRIBE_OK can no longer fire onOk after onError and resurrect the alias route. - Pending TRACK_STATUS requests are tracked by the session and failed on transport teardown with the same semantics as subscriptions and fetches (failure callback fired, response timer cleared). - MAX_OBJECT_PROPERTIES_LENGTH raised to 1 MiB: a single legal maximum-size KVP (0xffff value bytes) encodes to 65,539 bytes and could not fit under the old 64 KiB aggregate cap. - De-flaked the REQUEST_OK fetch-settle test (10ms -> 25ms deadline, still inside the 30ms observation window). --- .../spf/src/network/moqt/control-messages.ts | 14 ++++- .../spf/src/network/moqt/object-stream.ts | 8 ++- packages/spf/src/network/moqt/session.ts | 39 +++++++++--- .../moqt/tests/control-messages.test.ts | 19 ++++++ .../network/moqt/tests/object-stream.test.ts | 23 +++++++ .../src/network/moqt/tests/session.test.ts | 63 ++++++++++++++++++- 6 files changed, 151 insertions(+), 15 deletions(-) diff --git a/packages/spf/src/network/moqt/control-messages.ts b/packages/spf/src/network/moqt/control-messages.ts index cae41ca32f..9609bff803 100644 --- a/packages/spf/src/network/moqt/control-messages.ts +++ b/packages/spf/src/network/moqt/control-messages.ts @@ -901,7 +901,19 @@ function readRedirect(reader: ByteReader): Redirect { export function decodeControlMessage(frame: ControlMessageFrame): ControlMessage { const reader = new ByteReader(frame.body); const end = frame.body.length; - const message = decodeControlMessageBody(frame.type, reader, end); + let message: ControlMessage; + try { + message = decodeControlMessageBody(frame.type, reader, end); + } catch (error) { + // Field reads past the declared frame length surface as `RangeError` + // from `ByteReader`. Normalize them: the session's stream loops treat + // only `MoqtProtocolError` as fatal, and a truncated body must + // terminate the session (§10) rather than vanish as a read error. + if (error instanceof RangeError) { + throw new MoqtProtocolError(`control message 0x${frame.type.toString(16)} body is truncated`); + } + throw error; + } if (reader.offset !== end) { throw new MoqtProtocolError(`control message 0x${frame.type.toString(16)} body length mismatch`); } diff --git a/packages/spf/src/network/moqt/object-stream.ts b/packages/spf/src/network/moqt/object-stream.ts index df3f14abd7..c2cc3afb97 100644 --- a/packages/spf/src/network/moqt/object-stream.ts +++ b/packages/spf/src/network/moqt/object-stream.ts @@ -51,11 +51,13 @@ export function isSubgroupHeaderType(type: number): boolean { * hostile relay declaring 2^53 would exhaust memory before the read ever * completed, so the declaration is rejected before any allocation. * - * 16 MiB clears any real LOC frame (a 4K keyframe is low single-digit MB); - * Properties carry frame metadata, so 64 KiB is already generous. + * 16 MiB clears any real LOC frame (a 4K keyframe is low single-digit MB). + * The Properties block is Key-Value-Pairs whose individual byte values may + * legally reach 2^16-1 (`MAX_KVP_VALUE_LENGTH`), so its bound leaves room + * for several maximum-size values, not just typical frame metadata. */ export const MAX_OBJECT_PAYLOAD_LENGTH = 16 * 1024 * 1024; -export const MAX_OBJECT_PROPERTIES_LENGTH = 64 * 1024; +export const MAX_OBJECT_PROPERTIES_LENGTH = 1024 * 1024; function checkPayloadLength(length: number): number { if (length > MAX_OBJECT_PAYLOAD_LENGTH) { diff --git a/packages/spf/src/network/moqt/session.ts b/packages/spf/src/network/moqt/session.ts index cedcab8d02..46de990cfb 100644 --- a/packages/spf/src/network/moqt/session.ts +++ b/packages/spf/src/network/moqt/session.ts @@ -284,6 +284,11 @@ interface FetchRecord extends RequestRecordBase { groupOrder: 'ascending' | 'descending'; } +interface TrackStatusRecord extends RequestRecordBase { + requestId: number; + handlers: TrackStatusHandlers; +} + interface AliasWaiter { resolve(record: SubscriptionRecord | undefined): void; timer: ReturnType; @@ -303,6 +308,7 @@ class MoqtSessionImpl implements MoqtSession { #nextRequestId = 0; // client request IDs: even, starting at 0 (§10.1) #subscriptions = new Map(); #fetches = new Map(); + #trackStatuses = new Map(); #aliasRoutes = new Map(); #aliasWaiters = new Map(); @@ -389,15 +395,15 @@ class MoqtSessionImpl implements MoqtSession { // hear about — otherwise a dropped transport leaves subscribers pending // forever. A deliberate `close()` is ordinary teardown, and cancelled // requests already have their answer. - const pending = expected - ? [] - : [...this.#subscriptions.values(), ...this.#fetches.values()].filter((r) => !r.settled && !r.cancelled); - for (const record of [...this.#subscriptions.values(), ...this.#fetches.values()]) { + const records = [...this.#subscriptions.values(), ...this.#fetches.values(), ...this.#trackStatuses.values()]; + const pending = expected ? [] : records.filter((r) => !r.settled && !r.cancelled); + for (const record of records) { this.#settleRequest(record); record.stream?.cancel(error); } this.#subscriptions.clear(); this.#fetches.clear(); + this.#trackStatuses.clear(); this.#aliasRoutes.clear(); this.#controlWriter = undefined; @@ -511,7 +517,11 @@ class MoqtSessionImpl implements MoqtSession { trackStatus(options: MoqtSubscribeOptions, handlers: TrackStatusHandlers = {}): void { const requestId = this.#allocateRequestId(); - const record: RequestRecordBase = { cancelled: false, settled: false }; + const record: TrackStatusRecord = { requestId, handlers, cancelled: false, settled: false }; + // Tracked so session teardown fails a still-pending TRACK_STATUS the + // same way it fails pending subscriptions and fetches. Answered + // requests leave the map — nothing else arrives on their stream. + this.#trackStatuses.set(requestId, record); const message = encodeTrackStatus({ requestId, @@ -526,15 +536,20 @@ class MoqtSessionImpl implements MoqtSession { (msg) => { if (msg.kind === 'request-ok') { this.#settleRequest(record); + this.#trackStatuses.delete(requestId); handlers.onOk?.({ parameters: msg.parameters, trackProperties: msg.trackProperties }); record.stream?.finWrite().catch(() => {}); } else if (msg.kind === 'request-error') { this.#settleRequest(record); + this.#trackStatuses.delete(requestId); handlers.onError?.(msg); record.stream?.finWrite().catch(() => {}); } }, - (error) => handlers.onError?.(error) + (error) => { + this.#trackStatuses.delete(requestId); + handlers.onError?.(error); + } ); } @@ -574,10 +589,14 @@ class MoqtSessionImpl implements MoqtSession { const timeout = this.#config.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS; if (timeout > 0 && !record.settled && !record.cancelled) { - record.responseTimer = setTimeout( - () => fail(streamFailure('no response before the request timeout', REQUEST_ERROR_CODE.TIMEOUT)), - timeout - ); + record.responseTimer = setTimeout(() => { + const failure = streamFailure('no response before the request timeout', REQUEST_ERROR_CODE.TIMEOUT); + // The stream dies with the request: left open, a late SUBSCRIBE_OK + // would fire onOk after onError and re-register the alias route of + // a subscription already reported dead. + record.stream?.cancel(failure); + fail(failure); + }, timeout); } record.stream = openRequestStream(stream, message, { diff --git a/packages/spf/src/network/moqt/tests/control-messages.test.ts b/packages/spf/src/network/moqt/tests/control-messages.test.ts index ef60005771..de235ec767 100644 --- a/packages/spf/src/network/moqt/tests/control-messages.test.ts +++ b/packages/spf/src/network/moqt/tests/control-messages.test.ts @@ -209,6 +209,25 @@ describe('decodeControlMessage', () => { MoqtProtocolError ); }); + + it('throws MoqtProtocolError (not RangeError) for a truncated body', () => { + // PUBLISH_NAMESPACE body cut short: request id, then no namespace — + // the RangeError from reading past the frame must be normalized so + // malformed frames terminate the session. + expect(() => decodeControlMessage({ type: MESSAGE_TYPE.PUBLISH_NAMESPACE, body: new Uint8Array([0x01]) })).toThrow( + MoqtProtocolError + ); + + // A namespace field whose declared length overruns the frame. + const body = new ByteWriter(); + body.writeVarint(1); // request id + body.writeVarint(1); // field count + body.writeVarint(10); // field length, followed by only 2 bytes + body.writeBytes(utf8Encode('ab')); + expect(() => decodeControlMessage({ type: MESSAGE_TYPE.PUBLISH_NAMESPACE, body: body.toBytes() })).toThrow( + MoqtProtocolError + ); + }); }); describe('encodeMessageParameters', () => { diff --git a/packages/spf/src/network/moqt/tests/object-stream.test.ts b/packages/spf/src/network/moqt/tests/object-stream.test.ts index 2b314d0d32..15afdebf83 100644 --- a/packages/spf/src/network/moqt/tests/object-stream.test.ts +++ b/packages/spf/src/network/moqt/tests/object-stream.test.ts @@ -238,6 +238,29 @@ describe('readSubgroupObjects', () => { expect(objects[0]!.payload.length).toBe(MAX_OBJECT_PAYLOAD_LENGTH); }); + it('accepts a properties block holding a maximum-size KVP value', async () => { + // One odd-type KVP whose byte value is the largest the KVP codec + // permits (2^16-1): the aggregate block bound must not reject what the + // per-value rule allows. + const props = new ByteWriter(); + props.writeVarint(0x07); + props.writeVarint(0xffff); + props.writeBytes(new Uint8Array(0xffff).fill(1)); + const bytes = encodeSubgroupStream({ + type: 0x39, // 0x38 | PROPERTIES + trackAlias: 1, + groupId: 7, + objects: [{ objectIdDelta: 0, payload: utf8Encode('a'), properties: props.toBytes() }], + }); + const reader = new StreamReader(streamOf(bytes)); + await reader.readVarint(); + const header = await readSubgroupHeader(reader, 0x39); + const objects = await collect(readSubgroupObjects(reader, header)); + expect(objects[0]!.properties).toHaveLength(1); + expect(objects[0]!.properties[0]).toMatchObject({ type: 0x07 }); + expect((objects[0]!.properties[0]!.value as Uint8Array).length).toBe(0xffff); + }); + it('parses per-object properties when the header PROPERTIES bit is set', async () => { // Properties KVP: type 0x06 (Timestamp), varint value 90000. const props = new ByteWriter(); diff --git a/packages/spf/src/network/moqt/tests/session.test.ts b/packages/spf/src/network/moqt/tests/session.test.ts index 01f1fc7a5b..432206cb86 100644 --- a/packages/spf/src/network/moqt/tests/session.test.ts +++ b/packages/spf/src/network/moqt/tests/session.test.ts @@ -384,6 +384,47 @@ describe('createMoqtSession', () => { session.destroy(); }); + it('ignores a response that arrives after the request timeout', async () => { + const fake = createFakeTransport(); + const session = createMoqtSession(fake.transport, { requestTimeoutMs: 10, unknownAliasTimeoutMs: 50 }); + fake.sendServerSetup(); + + const onOk = vi.fn(); + const onError = vi.fn(); + const objects: MoqtObject[] = []; + session.subscribe( + { trackNamespace: ['live'], trackName: 'video' }, + { onOk, onError, onObject: (object) => objects.push(object) } + ); + const request = await fake.nextRequestStream(); + await request.firstMessage; + await vi.waitFor(() => expect(onError).toHaveBeenCalled()); + + // A SUBSCRIBE_OK after the timeout must not fire onOk after onError, + // and must not resurrect the alias route of the dead subscription. + await request.send(encodeSubscribeOk(11)).catch(() => {}); + fake.openDataStream(encodeSubgroup(11, 41, utf8Encode('late'))); + await new Promise((resolve) => setTimeout(resolve, 80)); + expect(onOk).not.toHaveBeenCalled(); + expect(objects).toHaveLength(0); + session.destroy(); + }); + + it('fails a pending trackStatus when the transport drops', async () => { + const harness = createSessionHarness(); + harness.sendServerSetup(); + + const onError = vi.fn(); + harness.session.trackStatus({ trackNamespace: ['live'], trackName: 'video' }, { onError }); + await (await harness.nextRequestStream()).firstMessage; + harness.dropTransport(); + + await vi.waitFor(() => expect(onError).toHaveBeenCalled()); + expect(onError).toHaveBeenCalledWith( + expect.objectContaining({ reason: 'session closed before the request was answered' }) + ); + }); + it('fails pending requests when the transport drops, but not on a local close()', async () => { const dropped = createSessionHarness(); dropped.sendServerSetup(); @@ -525,7 +566,9 @@ describe('createMoqtSession', () => { // instead of delivering it — the replay vanishes with no protocol error. it('settles a fetch answered with REQUEST_OK so the request timeout cannot fail it', async () => { const fake = createFakeTransport(); - const session = createMoqtSession(fake.transport, { requestTimeoutMs: 10, unknownAliasTimeoutMs: 200 }); + // 25ms: below the 30ms wait (an un-disarmed timer would still fire in + // time to be observed) with headroom for the REQUEST_OK on slow CI. + const session = createMoqtSession(fake.transport, { requestTimeoutMs: 25, unknownAliasTimeoutMs: 200 }); fake.sendServerSetup(); const subscription = session.subscribe({ trackNamespace: ['live'], trackName: 'catalog' }); @@ -723,6 +766,24 @@ describe('createMoqtSession', () => { harness.session.destroy(); }); + it('closes the session when an incoming request frame has a truncated body', async () => { + const onClosed = vi.fn(); + const harness = createSessionHarness({ onClosed }); + harness.sendServerSetup(); + + // A PUBLISH_NAMESPACE frame whose declared body length cuts the + // namespace off — decoding must terminate the session, not vanish as + // an unhandled RangeError in the accept loop. + const frame = new ByteWriter(); + frame.writeVarint(0x06); // PUBLISH_NAMESPACE + frame.writeUint16(1); + frame.writeVarint(1); // request id, then no namespace at all + harness.openIncomingBidi(frame.toBytes()); + + await vi.waitFor(() => expect(onClosed).toHaveBeenCalled()); + expect(harness.getCloseInfo()).toMatchObject({ closeCode: 0x3 }); + }); + it('closes the session with PROTOCOL_VIOLATION on an unknown data stream type', async () => { const onClosed = vi.fn(); const harness = createSessionHarness({ onClosed }); From f5a124a93edb302c2d42f1f74f52b27c3d8cc548 Mon Sep 17 00:00:00 2001 From: Matt McClure Date: Sat, 1 Aug 2026 18:05:53 +0000 Subject: [PATCH 02/11] fix(spf): validate msf channel config and audio sample rates - parseChannelConfig no longer defaults unrecognized layouts to stereo: it returns undefined and requires a positive safe integer total <= 255, while still summing dotted layouts like 5.1/7.1.4. The presentation projection substitutes 2 for the required channels field (same pattern as sampleRate). - toAudioDecoderConfig derives numberOfChannels from the raw channelConfig and rejects a declared-but-unresolvable layout instead of silently configuring the decoder for stereo; sample rates must be positive integers in the u32 range before a config is returned (callers already disable the track on null). --- packages/spf/src/media/moq/codec-mapping.ts | 33 ++++++++++++-- packages/spf/src/media/moq/parse-catalog.ts | 28 ++++++++---- .../src/media/moq/tests/codec-mapping.test.ts | 37 ++++++++++++++++ .../src/media/moq/tests/parse-catalog.test.ts | 44 ++++++++++++++++++- 4 files changed, 127 insertions(+), 15 deletions(-) diff --git a/packages/spf/src/media/moq/codec-mapping.ts b/packages/spf/src/media/moq/codec-mapping.ts index ddb6e844c7..21dc73479b 100644 --- a/packages/spf/src/media/moq/codec-mapping.ts +++ b/packages/spf/src/media/moq/codec-mapping.ts @@ -5,7 +5,7 @@ * * The WebCodecs types come from the WebWorker lib — this stays DOM-free. */ -import type { MoqAudioTrack, MoqVideoTrack } from './parse-catalog'; +import { type MoqAudioTrack, type MoqVideoTrack, parseChannelConfig } from './parse-catalog'; /** * Decoder config for a catalog video track, or `null` when the catalog @@ -37,17 +37,34 @@ export function toVideoDecoderConfig(track: MoqVideoTrack): VideoDecoderConfig | * there because the shared type requires a number, and configuring an * AAC decoder at 48 kHz for a 44.1 kHz stream corrupts every frame rather * than failing loudly. Opus is the one exception: RFC 6716 decoders always - * operate at 48 kHz, so its rate needs no declaration. + * operate at 48 kHz, so its rate needs no declaration. A declared rate is + * still validated — a malformed or hostile catalog could otherwise produce + * a non-integer, negative, or out-of-range `AudioDecoderConfig.sampleRate` + * (WebIDL `unsigned long`) — and rejected rather than handed to the decoder. + * + * Channel count follows the catalog's `channelConfig` (§5.2.21) the same + * way, via `parseChannelConfig`, rather than the projection's + * `AudioTrack.channels` substitute: an omitted `channelConfig` defaults to + * stereo (the common convention for simple streams), but a *declared* + * value this parser can't resolve — an unrecognized surround/object-audio + * layout string, or a malformed one — rejects the track instead of + * silently decoding it as stereo. */ export function toAudioDecoderConfig(track: MoqAudioTrack): AudioDecoderConfig | null { const codec = track.codecs[0]; if (!codec) return null; + const sampleRate = track.moq.samplerate ?? (isOpus(codec) ? OPUS_SAMPLE_RATE : undefined); - if (sampleRate === undefined) return null; + if (sampleRate === undefined || !isValidSampleRate(sampleRate)) return null; + + const numberOfChannels = + track.moq.channelConfig === undefined ? DEFAULT_CHANNELS : parseChannelConfig(track.moq.channelConfig); + if (numberOfChannels === undefined) return null; + const config: AudioDecoderConfig = { codec, sampleRate, - numberOfChannels: track.channels, + numberOfChannels, }; if (track.moq.initData !== undefined) { config.description = track.moq.initData.slice().buffer; @@ -56,6 +73,14 @@ export function toAudioDecoderConfig(track: MoqAudioTrack): AudioDecoderConfig | } const OPUS_SAMPLE_RATE = 48_000; +const DEFAULT_CHANNELS = 2; + +/** `AudioDecoderConfig.sampleRate` is a WebIDL `unsigned long`: a positive integer up to 2^32 - 1. */ +const MAX_SAMPLE_RATE = 0xff_ff_ff_ff; + +function isValidSampleRate(value: number): boolean { + return Number.isSafeInteger(value) && value > 0 && value <= MAX_SAMPLE_RATE; +} function isOpus(codec: string): boolean { return codec.toLowerCase().startsWith('opus'); diff --git a/packages/spf/src/media/moq/parse-catalog.ts b/packages/spf/src/media/moq/parse-catalog.ts index 44b3660f99..3e452db2ed 100644 --- a/packages/spf/src/media/moq/parse-catalog.ts +++ b/packages/spf/src/media/moq/parse-catalog.ts @@ -431,18 +431,28 @@ function trackUrl(sessionUri: string, track: MoqCatalogTrack): string { return `${sessionUri}#msf:${encodeNamespaceName(track.namespace, track.name)}`; } +/** Sane upper bound on a decoded channel count (WebCodecs practical ceiling). */ +const MAX_CHANNELS = 255; + /** - * Channel count from a catalog `channelConfig` (§5.2.21). Accepts a plain - * count (`'2'`) and the dotted surround form (`'5.1'` → 6, `'7.1.4'` → 12); - * `parseInt` alone would read `'5.1'` as 5 and silently drop the LFE. - * Unrecognized values fall back to stereo. + * Channel count from a catalog `channelConfig` (§5.2.21), or `undefined` if + * it doesn't resolve to one. Accepts a plain count (`'2'`) and the dotted + * surround form (`'5.1'` → 6, `'7.1.4'` → 12); `parseInt` alone would read + * `'5.1'` as 5 and silently drop the LFE channel. + * + * `channelConfig` is intentionally flexible (codec-specific layout strings + * are allowed alongside numeric ones), so anything this parser doesn't + * recognize — and any numeric total that isn't a sane positive channel + * count, guarding against overflowed or hostile input producing `Infinity` + * — returns `undefined` rather than guessing stereo. Callers decide how to + * substitute (see `moqCatalogToPresentation` and `toAudioDecoderConfig`). */ -export function parseChannelConfig(channelConfig: string | undefined): number { - if (!channelConfig) return 2; +export function parseChannelConfig(channelConfig: string | undefined): number | undefined { + if (!channelConfig) return undefined; const trimmed = channelConfig.trim(); - if (!/^\d+(\.\d+)*$/.test(trimmed)) return 2; + if (!/^\d+(\.\d+)*$/.test(trimmed)) return undefined; const total = trimmed.split('.').reduce((sum, part) => sum + Number(part), 0); - return total > 0 ? total : 2; + return Number.isSafeInteger(total) && total > 0 && total <= MAX_CHANNELS ? total : undefined; } /** @@ -497,7 +507,7 @@ export function moqCatalogToPresentation( // carry the raw values — `toAudioDecoderConfig` reads those rather // than trusting these for decoder configuration. sampleRate: track.samplerate ?? 48_000, - channels: parseChannelConfig(track.channelConfig), + channels: parseChannelConfig(track.channelConfig) ?? 2, }); } else { text.push({ diff --git a/packages/spf/src/media/moq/tests/codec-mapping.test.ts b/packages/spf/src/media/moq/tests/codec-mapping.test.ts index 45e7ab497f..b180ee84a6 100644 --- a/packages/spf/src/media/moq/tests/codec-mapping.test.ts +++ b/packages/spf/src/media/moq/tests/codec-mapping.test.ts @@ -89,4 +89,41 @@ describe('toAudioDecoderConfig', () => { it('pins Opus to 48 kHz without a declared samplerate (RFC 6716 decode rate)', () => { expect(toAudioDecoderConfig(audioTrack({ codecs: ['Opus'] }))).toMatchObject({ sampleRate: 48_000 }); }); + + // A malformed or hostile catalog `samplerate` must not reach + // `AudioDecoderConfig.sampleRate` (a WebIDL `unsigned long`) — reject the + // track instead of configuring the decoder with an invalid rate. + it.each([-44_100, 0, 44_100.5, Infinity, 0x1_00_00_00_00])( + 'returns null for a malformed samplerate (%s)', + (samplerate) => { + const track = audioTrack({ + codecs: ['mp4a.40.2'], + moq: { namespace: ['ns'], name: 'audio', packaging: 'loc', isLive: true, samplerate }, + }); + expect(toAudioDecoderConfig(track)).toBeNull(); + } + ); + + it('derives channel count from the declared channelConfig, including surround layouts', () => { + const track = audioTrack({ + channels: 2, // projection substitute — must not be trusted over channelConfig + moq: { namespace: ['ns'], name: 'audio', packaging: 'loc', isLive: true, channelConfig: '5.1' }, + }); + expect(toAudioDecoderConfig(track)).toMatchObject({ numberOfChannels: 6 }); + }); + + it('defaults to stereo when channelConfig is omitted', () => { + expect(toAudioDecoderConfig(audioTrack())).toMatchObject({ numberOfChannels: 2 }); + }); + + // An omitted channelConfig defaults to stereo (common convention for + // simple streams), but a *declared* value this parser can't resolve to a + // channel count must not silently decode as stereo either. + it('returns null when a declared channelConfig cannot be resolved', () => { + const track = audioTrack({ + channels: 2, + moq: { namespace: ['ns'], name: 'audio', packaging: 'loc', isLive: true, channelConfig: 'JOC' }, + }); + expect(toAudioDecoderConfig(track)).toBeNull(); + }); }); diff --git a/packages/spf/src/media/moq/tests/parse-catalog.test.ts b/packages/spf/src/media/moq/tests/parse-catalog.test.ts index ee56cb52e0..6a7c3ad9b6 100644 --- a/packages/spf/src/media/moq/tests/parse-catalog.test.ts +++ b/packages/spf/src/media/moq/tests/parse-catalog.test.ts @@ -94,8 +94,48 @@ describe('parseMoqCatalog', () => { // `parseInt('5.1')` reads 5 and silently drops the LFE channel. expect(parseChannelConfig('5.1')).toBe(6); expect(parseChannelConfig('7.1.4')).toBe(12); - expect(parseChannelConfig(undefined)).toBe(2); - expect(parseChannelConfig('stereo')).toBe(2); + }); + + it('does not guess stereo for an absent or unrecognized channelConfig', () => { + expect(parseChannelConfig(undefined)).toBeUndefined(); + // A codec-specific or otherwise unrecognized layout string — callers + // decide whether/how to substitute, this parser doesn't guess. + expect(parseChannelConfig('stereo')).toBeUndefined(); + }); + + it('rejects a malformed or hostile channelConfig total instead of overflowing', () => { + // `Number('9'.repeat(400))` overflows `Number.MAX_VALUE` to `Infinity`; + // that must not sail through as a channel count. + expect(parseChannelConfig('9'.repeat(400))).toBeUndefined(); + expect(parseChannelConfig('0')).toBeUndefined(); + // Comfortably past any real-world layout — guards the sane upper bound. + expect(parseChannelConfig('1000')).toBeUndefined(); + }); + + it('substitutes a conventional channel count in the projection while keeping the raw channelConfig', () => { + // `AudioTrack.channels` is a required number, so the projection still + // needs a value even for a layout `parseChannelConfig` can't resolve — + // but the raw string rides along on `moq.channelConfig` so + // `toAudioDecoderConfig` can tell "declared 2" from "unresolved" and + // reject rather than silently decode as stereo. + const text = JSON.stringify({ + version: '1', + tracks: [ + { + name: 'audio', + packaging: 'loc', + isLive: true, + role: 'audio', + codec: 'mp4a.40.2', + samplerate: 44_100, + channelConfig: 'JOC', + }, + ], + }); + const presentation = parseMoqCatalog(text, { url: SOURCE_URL }); + const audio = getTracksByType(presentation, 'audio')[0] as MoqAudioTrack; + expect(audio.channels).toBe(2); + expect(audio.moq.channelConfig).toBe('JOC'); }); it('derives stable track ids from full track names', () => { From 8bbc67aa3c918a157233664f736f3d52f59728ed Mon Sep 17 00:00:00 2001 From: Matt McClure Date: Sat, 1 Aug 2026 18:05:55 +0000 Subject: [PATCH 03/11] fix(spf): close review gaps in the moq adapter and behaviors - volume: a NaN write is dropped (last valid volume kept) so a non-finite value can never reach the GainNode; infinities clamp. - play()/autoplay right after a src-change or pause() suspend no longer reads stale AudioContext state: the in-flight suspend is tracked and every resume path treats it as suspended and chains behind it. - suspendMediaWhilePaused falls through its hold-window derivation chain when pauseHoldSeconds/targetLatency are not non-negative finite numbers, instead of letting setTimeout coerce NaN/negative delays into an immediate suspend. - resolveCatalog ignores joining-fetch entries after the settle deadline, cancels the fetch handle at the deadline, and guards all fetch/subscription callbacks with a per-attempt token so a superseded attempt (e.g. the auth-expiry retry) cannot settle or overwrite the fresh attempt's state. --- .../src/playback/behaviors/resolve-catalog.ts | 37 ++++++- .../behaviors/suspend-media-while-paused.ts | 16 ++- .../behaviors/tests/resolve-catalog.test.ts | 78 ++++++++++++++ .../tests/suspend-media-while-paused.test.ts | 30 ++++++ .../spf/src/playback/engines/moq/adapter.ts | 68 +++++++++--- .../engines/moq/tests/adapter.test.ts | 101 +++++++++++++++++- 6 files changed, 308 insertions(+), 22 deletions(-) diff --git a/packages/spf/src/playback/behaviors/resolve-catalog.ts b/packages/spf/src/playback/behaviors/resolve-catalog.ts index bc4aa7791d..f653162726 100644 --- a/packages/spf/src/playback/behaviors/resolve-catalog.ts +++ b/packages/spf/src/playback/behaviors/resolve-catalog.ts @@ -137,6 +137,12 @@ function setupResolveCatalog({ let subscription: Subscription | undefined; let fetchHandle: FetchHandle | undefined; let settleTimer: ReturnType | undefined; + // Monotonic attempt token: cancelling a subscription/fetch does + // not stop callbacks already in flight (a cancelled fetch stream + // can still surface onEnd/onReset), so the auth-expiry retry + // guards every handler against its superseded attempt — a stale + // settle or object must not touch the fresh attempt's state. + let attempt = 0; const apply = (text: string, objectId: number): void => { // A delta with no prior catalog can't be interpreted (§5.1.6); @@ -164,10 +170,22 @@ function setupResolveCatalog({ const start = (parameters: MessageParameters): void => { // Fresh attempt: the new joining fetch replays the current // group again, so live deltas must buffer until it settles. + const thisAttempt = ++attempt; + const isCurrent = (): boolean => attempt === thisAttempt; fetchSettled = false; bufferedLive.length = 0; clearTimeout(settleTimer); - settleTimer = fetchTimeoutMs > 0 ? setTimeout(settleFetch, fetchTimeoutMs) : undefined; + settleTimer = + fetchTimeoutMs > 0 + ? setTimeout(() => { + // Deadline passed: fall back to live-only and stop the + // replay — a straggling fetch entry would overwrite the + // live catalog with stale state (or apply the next + // delta against the wrong base). + settleFetch(); + fetchHandle?.cancel(); + }, fetchTimeoutMs) + : undefined; subscription = session.subscribe( { @@ -177,6 +195,7 @@ function setupResolveCatalog({ }, { onObject: (object) => { + if (!isCurrent()) return; if (object.status !== 'normal' || object.payload.length === 0) return; const text = utf8Decode(object.payload); if (!fetchSettled) { @@ -226,15 +245,25 @@ function setupResolveCatalog({ }, { onEntry: (entry) => { + // Once settled (deadline passed or replay ended) the live + // subscription owns the catalog — late replay entries are + // stale and must not overwrite it. + if (!isCurrent() || fetchSettled) return; if (entry.kind !== 'object' || entry.payload.length === 0) return; apply(utf8Decode(entry.payload), entry.objectId); }, - onEnd: settleFetch, + onEnd: () => { + if (isCurrent()) settleFetch(); + }, // No history to replay (e.g. nothing published yet) or replay // truncated — fall back to live-only: the next independent // object resolves. - onError: settleFetch, - onReset: settleFetch, + onError: () => { + if (isCurrent()) settleFetch(); + }, + onReset: () => { + if (isCurrent()) settleFetch(); + }, } ); }; diff --git a/packages/spf/src/playback/behaviors/suspend-media-while-paused.ts b/packages/spf/src/playback/behaviors/suspend-media-while-paused.ts index dfcc022b04..878a4d708e 100644 --- a/packages/spf/src/playback/behaviors/suspend-media-while-paused.ts +++ b/packages/spf/src/playback/behaviors/suspend-media-while-paused.ts @@ -80,6 +80,13 @@ export const suspendMediaWhilePaused = defineBehavior({ }) => { const latencyConfig: LatencyControlConfig = { ...DEFAULT_LATENCY_CONTROL_CONFIG, ...config?.latency }; + // setTimeout coerces a non-finite or negative delay to "fire now", so + // an invalid consumer `targetLatency` or `pauseHoldSeconds` would turn + // every pause into an immediate suspend. Invalid values fall through + // to the next step of the derivation instead. + const isUsableHold = (seconds: number | undefined): seconds is number => + seconds !== undefined && Number.isFinite(seconds) && seconds >= 0; + return createMachineReactor({ initial: 'playing' as 'playing' | 'paused', monitor: () => (state.paused.get() === true ? 'paused' : 'playing'), @@ -90,9 +97,12 @@ export const suspendMediaWhilePaused = defineBehavior({ // for exactly one continuous pause. Entry bodies are untracked, // so the target-latency read is pinned at pause time. entry: () => { - const holdSeconds = - config?.pauseHoldSeconds ?? - (state.targetLatency.get() ?? latencyConfig.defaultTargetLatency) + latencyConfig.catchUpThreshold; + const configuredHold = config?.pauseHoldSeconds; + const targetLatency = state.targetLatency.get(); + const holdSeconds = isUsableHold(configuredHold) + ? configuredHold + : (isUsableHold(targetLatency) ? targetLatency : latencyConfig.defaultTargetLatency) + + latencyConfig.catchUpThreshold; const timer = setTimeout(() => { // The resume cleanup clears this timer on the effect flush, // but that scheduling is framework-owned — re-check at fire diff --git a/packages/spf/src/playback/behaviors/tests/resolve-catalog.test.ts b/packages/spf/src/playback/behaviors/tests/resolve-catalog.test.ts index 0678e87f49..c8da3f7629 100644 --- a/packages/spf/src/playback/behaviors/tests/resolve-catalog.test.ts +++ b/packages/spf/src/playback/behaviors/tests/resolve-catalog.test.ts @@ -22,6 +22,14 @@ const DELTA = JSON.stringify({ ], }); +const CATALOG_WITH_AUDIO = JSON.stringify({ + version: '1', + tracks: [ + { name: 'video', packaging: 'loc', isLive: true, role: 'video', codec: 'avc1.64001f', bitrate: 1_000_000 }, + { name: 'audio', packaging: 'loc', isLive: true, role: 'audio', codec: 'opus' }, + ], +}); + // ============================================================================ // Fakes // ============================================================================ @@ -252,6 +260,76 @@ describe('resolveCatalog', () => { reactor.destroy(); }); + // The deadline switches to live-only, but the relay may still deliver + // the joining fetch's replay afterwards. Those entries are older than + // the live catalog by construction — applying one would roll the + // presentation back (or land the next delta on the wrong base). + it('ignores fetch entries that arrive after the settle deadline', async () => { + const { actor, subscriptions, fetches } = createFakeSessionActor(); + const deps = makeDeps(actor, { url: MOQ_URL }); + const reactor = resolveCatalog.setup({ ...deps, config: { catalogFetchTimeoutMs: 10 } }); + + await vi.waitFor(() => expect(fetches).toHaveLength(1)); + // The deadline also cancels the replay stream outright. + await vi.waitFor(() => expect(fetches[0]!.cancelled).toBe(true)); + + subscriptions[0]!.handlers.onObject?.(catalogObject(6, 0, CATALOG_WITH_AUDIO)); + await vi.waitFor(() => expect(isResolvedPresentation(deps.state.presentation.get())).toBe(true)); + expect(getTracksByType(deps.state.presentation.get()!, 'audio')).toHaveLength(1); + + // A straggling replay entry (an older group) must not overwrite it. + fetches[0]!.handlers.onEntry?.({ + kind: 'object', + groupId: 5, + objectId: 0, + priority: 128, + properties: [], + payload: utf8Encode(CATALOG), + }); + fetches[0]!.handlers.onEnd?.(); + expect(getTracksByType(deps.state.presentation.get()!, 'audio')).toHaveLength(1); + expect(getTracksByType(deps.state.presentation.get()!, 'video')).toHaveLength(1); + + reactor.destroy(); + }); + + it('keeps a cancelled first fetch from settling the retry attempt early', async () => { + const { actor, subscriptions, fetches } = createFakeSessionActor(); + const deps = makeDeps(actor, { url: MOQ_URL }); + const reactor = resolveCatalog.setup(deps); + + await vi.waitFor(() => expect(subscriptions).toHaveLength(1)); + subscriptions[0]!.handlers.onError?.({ + errorCode: REQUEST_ERROR_CODE.EXPIRED_AUTH_TOKEN, + retryInterval: 0, + reason: 'expired', + }); + await vi.waitFor(() => expect(fetches).toHaveLength(2)); + + // Cancelling a fetch does not stop callbacks already in flight — the + // first fetch's late settle must not flush the retry's buffer early. + fetches[0]!.handlers.onEnd?.(); + + // Still buffering: the retry's live delta waits for its own fetch... + subscriptions[1]!.handlers.onObject?.(catalogObject(5, 1, DELTA)); + expect(isResolvedPresentation(deps.state.presentation.get())).toBe(false); + + // ...and applies in order once that fetch replays the base catalog. + fetches[1]!.handlers.onEntry?.({ + kind: 'object', + groupId: 5, + objectId: 0, + priority: 128, + properties: [], + payload: utf8Encode(CATALOG), + }); + fetches[1]!.handlers.onEnd?.(); + await vi.waitFor(() => expect(isResolvedPresentation(deps.state.presentation.get())).toBe(true)); + expect(getTracksByType(deps.state.presentation.get()!, 'audio')).toHaveLength(1); + + reactor.destroy(); + }); + it('stays idle for non-moqt sources and before the session is ready', async () => { const { actor, subscriptions } = createFakeSessionActor(); const httpDeps = makeDeps(actor, { url: 'https://example.com/live.m3u8' }); diff --git a/packages/spf/src/playback/behaviors/tests/suspend-media-while-paused.test.ts b/packages/spf/src/playback/behaviors/tests/suspend-media-while-paused.test.ts index 20e9233bef..9e71d8b483 100644 --- a/packages/spf/src/playback/behaviors/tests/suspend-media-while-paused.test.ts +++ b/packages/spf/src/playback/behaviors/tests/suspend-media-while-paused.test.ts @@ -137,6 +137,36 @@ describe('suspendMediaWhilePaused', () => { reactor.destroy(); }); + it('falls back to the default hold when the consumer target latency is invalid', async () => { + const deps = makeDeps(); + // setTimeout would coerce the NaN-derived delay to zero — an invalid + // consumer value must not turn every pause into an immediate suspend. + deps.state.targetLatency.set(Number.NaN); + const reactor = suspendMediaWhilePaused.setup(deps); + + deps.state.paused.set(true); + await vi.advanceTimersByTimeAsync(DEFAULT_HOLD_MS - 100); + expect(deps.state.mediaSuspended.get()).toBeUndefined(); + await vi.advanceTimersByTimeAsync(100); + expect(deps.state.mediaSuspended.get()).toBe(true); + + reactor.destroy(); + }); + + it('ignores an invalid pauseHoldSeconds and derives the hold window instead', async () => { + const deps = makeDeps(); + deps.state.targetLatency.set(2); // derived hold = 2s + 3s catch-up threshold + const reactor = suspendMediaWhilePaused.setup({ ...deps, config: { pauseHoldSeconds: -1 } }); + + deps.state.paused.set(true); + await vi.advanceTimersByTimeAsync(4_900); + expect(deps.state.mediaSuspended.get()).toBeUndefined(); + await vi.advanceTimersByTimeAsync(100); + expect(deps.state.mediaSuspended.get()).toBe(true); + + reactor.destroy(); + }); + it('never suspends when the hold deadline races a resume ahead of the effect flush', async () => { const deps = makeDeps(); const reactor = suspendMediaWhilePaused.setup(deps); diff --git a/packages/spf/src/playback/engines/moq/adapter.ts b/packages/spf/src/playback/engines/moq/adapter.ts index f71b511605..97e238af37 100644 --- a/packages/spf/src/playback/engines/moq/adapter.ts +++ b/packages/spf/src/playback/engines/moq/adapter.ts @@ -128,6 +128,16 @@ export function MoqMediaMixin>(BaseClass: Base) #autoplayAttempted = false; readonly #createAudioContext: () => MoqAudioContext; #audioContext: MoqAudioContext | undefined; + /** + * The most recent fire-and-forget `suspend()` still in flight. + * `state` only reports 'suspended' once the control thread + * acknowledges, so a play()/autoplay issued right after a src-change + * or pause() suspend would read a stale 'running', skip its resume, + * and let the late suspend silence a facade that reports playing. + * Resume paths treat a pending suspend as suspended and chain behind + * it (`#resumeAudio`). + */ + #pendingSuspend: Promise | undefined; #volume = 1; #muted = false; #gain: GainNode | undefined; @@ -167,7 +177,7 @@ export function MoqMediaMixin>(BaseClass: Base) // suspending the context *is* the pause — so without this the old // source's already-scheduled audio keeps playing audibly while // `paused` reports true. - this.#audioContext?.suspend().catch(() => {}); + if (this.#audioContext) this.#suspendAudio(this.#audioContext); // A new load cycle re-arms autoplay (the spec's load algorithm resets // the can-autoplay flag) and clears the old source's audio deferral. this.#autoplayAttempted = false; @@ -223,18 +233,43 @@ export function MoqMediaMixin>(BaseClass: Base) this.#signals.state.audioSuspended.set(true); return; } - if (audioContext.state !== 'suspended') return; + if (!this.#audioContextSuspended(audioContext)) return; this.#signals.state.audioSuspended.set(true); // Chromium queues a pre-gesture resume() and settles it on the first // user activation, so this is both an immediate start attempt and an // unlock signal. Engines that reject instead (Safari) stay deferred // until a host gesture path calls play(). - audioContext - .resume() + this.#resumeAudio(audioContext) .then(() => this.#signals.state.audioSuspended.set(undefined)) .catch(() => {}); } + /** + * Suspend, recording the in-flight promise so resume paths can + * serialize behind it — see {@link #pendingSuspend}. + */ + #suspendAudio(audioContext: MoqAudioContext): void { + const clear = (): void => { + if (this.#pendingSuspend === pending) this.#pendingSuspend = undefined; + }; + const pending: Promise = audioContext.suspend().then(clear, clear); + this.#pendingSuspend = pending; + } + + /** Suspended for playback purposes: an in-flight suspend counts. */ + #audioContextSuspended(audioContext: MoqAudioContext): boolean { + return this.#pendingSuspend !== undefined || audioContext.state === 'suspended'; + } + + /** + * Resume serialized behind any in-flight suspend, so a suspend acked + * after the resume decision cannot silence a playing facade. + */ + #resumeAudio(audioContext: MoqAudioContext): Promise { + const pending = this.#pendingSuspend; + return pending ? pending.then(() => audioContext.resume()) : audioContext.resume(); + } + attach(surface: HTMLCanvasElement): void { this.#signals.context.renderSurface.set(surface); const audioContext = (this.#audioContext ??= this.#createAudioContext()); @@ -242,16 +277,17 @@ export function MoqMediaMixin>(BaseClass: Base) // outside a user gesture — starts 'suspended' (so a play() that ran // before attach() would otherwise stay permanently silent), while // one created inside a gesture can start 'running' while paused. - if (audioContext.state === 'running') { + if (this.#audioContextSuspended(audioContext)) { + if (!this.paused) { + this.#resumeAudio(audioContext) + .then(() => this.#signals.state.audioSuspended.set(undefined)) + .catch(() => {}); + } + } else if (audioContext.state === 'running') { // A running context can always render audio — an autoplay deferral // recorded before the context existed is already settled. this.#signals.state.audioSuspended.set(undefined); - if (this.paused) audioContext.suspend().catch(() => {}); - } else if (!this.paused && audioContext.state === 'suspended') { - audioContext - .resume() - .then(() => this.#signals.state.audioSuspended.set(undefined)) - .catch(() => {}); + if (this.paused) this.#suspendAudio(audioContext); } this.#signals.context.audioContext.set((this.#renderContext ??= this.#createRenderContext(audioContext))); } @@ -300,9 +336,9 @@ export function MoqMediaMixin>(BaseClass: Base) // Safari outside a gesture, or an already-closed context — means // playback did not start, so restore `paused` rather than leaving the // player reporting playing over a frozen clock. - if (this.#audioContext && this.#audioContext.state === 'suspended') { + if (this.#audioContext && this.#audioContextSuspended(this.#audioContext)) { try { - await this.#audioContext.resume(); + await this.#resumeAudio(this.#audioContext); } catch (error) { this.#signals.state.paused.set(true); throw error; @@ -320,7 +356,7 @@ export function MoqMediaMixin>(BaseClass: Base) // toggling `autoplay` on later must not restart a paused player. this.#autoplayAttempted = true; this.#signals.state.paused.set(true); - this.#audioContext?.suspend().catch(() => {}); + if (this.#audioContext) this.#suspendAudio(this.#audioContext); } get paused(): boolean { @@ -334,6 +370,10 @@ export function MoqMediaMixin>(BaseClass: Base) } set volume(value: number) { + // NaN would survive the clamp (the infinities do not) and Web Audio + // rejects a non-finite gain value with a TypeError once attached — + // drop the write instead, keeping the documented [0, 1] invariant. + if (Number.isNaN(value)) return; this.#volume = Math.min(1, Math.max(0, value)); this.#applyGain(); } diff --git a/packages/spf/src/playback/engines/moq/tests/adapter.test.ts b/packages/spf/src/playback/engines/moq/tests/adapter.test.ts index b297522406..16d7b9df75 100644 --- a/packages/spf/src/playback/engines/moq/tests/adapter.test.ts +++ b/packages/spf/src/playback/engines/moq/tests/adapter.test.ts @@ -167,6 +167,28 @@ describe('MoqMediaMixin', () => { media.destroy(); }); + it('ignores a NaN volume write so a non-finite gain never reaches the audio graph', () => { + const audioContext = createFakeAudioContext('running'); + const media = new MoqMediaElement({ createAudioContext: () => audioContext }); + media.attach(document.createElement('canvas')); + + media.volume = 0.5; + // Web Audio rejects a non-finite gain value with a TypeError — the + // facade drops the write and keeps the last valid volume. + media.volume = Number.NaN; + expect(media.volume).toBe(0.5); + expect(audioContext.gain.gain.value).toBe(0.5); + + // The infinities clamp to the documented [0, 1] range on their own. + media.volume = Number.POSITIVE_INFINITY; + expect(media.volume).toBe(1); + media.volume = Number.NEGATIVE_INFINITY; + expect(media.volume).toBe(0); + expect(audioContext.gain.gain.value).toBe(0); + + media.destroy(); + }); + it('starts playback from autoplay and defers audio while the context is suspended', async () => { const audioContext = createFakeAudioContext('suspended'); // Chromium's pre-gesture shape: resume() parks until user activation. @@ -192,7 +214,9 @@ describe('MoqMediaMixin', () => { expect(media.engine.state.loadActivated.get()).toBe(true); expect(media.engine.state.audioSuspended.get()).toBe(true); - // The queued resume settling at user activation is the unlock. + // The queued resume settling at user activation is the unlock. It is + // issued once the src-change suspend has been acknowledged. + await vi.waitFor(() => expect(audioContext.resume).toHaveBeenCalledTimes(1)); activate(); await vi.waitFor(() => expect(media.engine.state.audioSuspended.get()).toBeUndefined()); expect(media.paused).toBe(false); @@ -295,6 +319,81 @@ describe('MoqMediaMixin', () => { media.destroy(); }); + it('chains play() behind a pending src-change suspend instead of trusting the stale running state', async () => { + const audioContext = createFakeAudioContext('suspended'); + const media = new MoqMediaElement({ createAudioContext: () => audioContext }); + media.attach(document.createElement('canvas')); + media.src = 'moqt://relay.test/live#msf:live--catalog'; + await media.play(); + expect(audioContext.state).toBe('running'); + expect(audioContext.resume).toHaveBeenCalledTimes(1); + + // Real contexts only flip `state` once the control thread acknowledges + // — model the src-change suspend as in flight with a stale 'running'. + let ackSuspend!: () => void; + audioContext.suspend.mockImplementationOnce( + () => + new Promise((resolve) => { + ackSuspend = () => { + audioContext.state = 'suspended'; + resolve(); + }; + }) + ); + media.src = 'moqt://relay.test/other#msf:live--catalog'; + expect(audioContext.state).toBe('running'); // suspend not yet acknowledged + + const playing = media.play(); + await Promise.resolve(); + // The resume waits for the suspend ack rather than reading the stale + // state and skipping — a skipped resume would leave the context + // suspended while the facade reports playing. + expect(audioContext.resume).toHaveBeenCalledTimes(1); + ackSuspend(); + await playing; + expect(audioContext.resume).toHaveBeenCalledTimes(2); + expect(audioContext.state).toBe('running'); + expect(media.paused).toBe(false); + + media.destroy(); + }); + + it('defers audio and chains the autoplay resume behind a pending src-change suspend', async () => { + const audioContext = createFakeAudioContext('suspended'); + const media = new MoqMediaElement({ createAudioContext: () => audioContext }); + media.attach(document.createElement('canvas')); + + media.autoplay = true; + media.src = 'moqt://relay.test/live#msf:live--catalog'; + await vi.waitFor(() => expect(media.engine.state.audioSuspended.get()).toBeUndefined()); + expect(audioContext.state).toBe('running'); + + let ackSuspend!: () => void; + audioContext.suspend.mockImplementationOnce( + () => + new Promise((resolve) => { + ackSuspend = () => { + audioContext.state = 'suspended'; + resolve(); + }; + }) + ); + media.src = 'moqt://relay.test/other#msf:live--catalog'; + + // Playback intent applies while the suspend is still in flight; the + // stale 'running' state must not skip the deferral + queued resume. + expect(media.paused).toBe(false); + expect(media.engine.state.audioSuspended.get()).toBe(true); + const resumeCalls = audioContext.resume.mock.calls.length; + + ackSuspend(); + await vi.waitFor(() => expect(media.engine.state.audioSuspended.get()).toBeUndefined()); + expect(audioContext.resume.mock.calls.length).toBe(resumeCalls + 1); + expect(audioContext.state).toBe('running'); + + media.destroy(); + }); + it('forwards engineConfig to the engine', async () => { const createMoqTransport = vi.fn((_connectUrl: string, _protocols: string[]) => ({ // Never delivers server SETUP: the factory call is what's under test. From 24aa7b416cc77d00b8c4b80885969f98577b5cbf Mon Sep 17 00:00:00 2001 From: Matt McClure Date: Sat, 1 Aug 2026 18:06:05 +0000 Subject: [PATCH 04/11] fix(html): report 0x0 simple-moq-video dimensions before first frame A defaults to 300x150, so the event bridge's first dimension poll dispatched a resize exposing the HTML default as the stream's dimensions before anything was decoded; native