diff --git a/doc/lib/js/publish.md b/doc/lib/js/publish.md index 0982fe5da9f..4d5119d6c6b 100644 --- a/doc/lib/js/publish.md +++ b/doc/lib/js/publish.md @@ -173,7 +173,9 @@ running. It does not retry on demand or bandwidth changes, and enable cycles that keep the same capture do not retry. A new capture source (such as the track a camera or microphone reopens after a device swap), changed encoder settings, or a replacement encoder recovers it. Recovery keeps the catalog ID and uses a -fresh `track` name. +fresh `track` name. A new network broadcast (a rename or a `maxAge` change) +also starts the encoder over, under the catalog ID, with its advertised +maxima reset. ## Strict CSP diff --git a/js/publish/src/audio/capture.test.ts b/js/publish/src/audio/capture.test.ts index b6433737e56..b5a3cba0a2f 100644 --- a/js/publish/src/audio/capture.test.ts +++ b/js/publish/src/audio/capture.test.ts @@ -559,6 +559,7 @@ test("a failed encoder stays failed until the capture source changes", async () const capture = new Capture({ enabled, source: source as never }); const track = new Moq.Track.Producer("audio").accept({ timescale: Moq.Time.Timescale.MILLI }); const rendition = { + slot: new Signal(undefined), config: new Signal(undefined), track: new Signal(track), close: () => {}, diff --git a/js/publish/src/audio/encoder.test.ts b/js/publish/src/audio/encoder.test.ts index 7559920ee08..70db56f10c7 100644 --- a/js/publish/src/audio/encoder.test.ts +++ b/js/publish/src/audio/encoder.test.ts @@ -291,6 +291,7 @@ async function setup(order = new Order(), codec?: Codec, groupDuration?: Milli) }; const rendition = { + slot: new Signal(undefined), config: new Signal(undefined), track: new Signal(track), close: () => { diff --git a/js/publish/src/audio/encoder.ts b/js/publish/src/audio/encoder.ts index 1f324c572c9..ce6a1e25b95 100644 --- a/js/publish/src/audio/encoder.ts +++ b/js/publish/src/audio/encoder.ts @@ -8,14 +8,14 @@ import { type Getter, getter, type Inputs, - Once, type Readonlys, readonlys, Signal, } from "@moq/signals"; import type { Broadcast } from "../broadcast"; -import { errors, watched } from "../internal"; +import { watched } from "../internal"; import { Estimator, type Order } from "../order"; +import { Slot } from "../publication"; import type { AudioFrame, Capture, Format } from "./capture"; import { Gain } from "./gain"; import { Resampler } from "./resampler"; @@ -233,18 +233,18 @@ export class Encoder { */ readonly settled: Computed; - // Each registration keeps its terminal failure even after the public error clears. - #failure = new Signal(new Once()); // The capture inputs #runCaptureRecovery last saw, carried across a gap in the source or format. #seen?: { capture?: Capture; source?: unknown; format?: Format }; #signals = new Effect(); #estimator = new Estimator(); // The estimator's delay, republished whenever it rises. #estimate = new Signal({}); - // What this rendition advertised on each broadcast, keyed by the broadcast or, with none, the - // encoder. A broadcast keeps a rendition's track when it re-registers, so returning to one - // continues its bitrate maximum and last config, while another broadcast starts its own. - #advertised = new WeakMap(); + // What this rendition keeps for the current network broadcast's track: its bitrate maximum, last + // config, and failure. A network broadcast keeps a rendition's track when it re-registers, so + // returning to one continues them, while a new one starts its own. + #slot = new Signal | undefined>(undefined); + // The slot while no network broadcast is serving the rendition. + #local = new Slot(); // Whether the rendition was disabled since a config last resolved, so it keeps advertising its last // config as disabled until the re-enabled capture resolves a new one. A capture that never reopens // leaves it disabled, which is accurate: no frames are coming. @@ -298,6 +298,7 @@ export class Encoder { } }); } + this.#signals.run(this.#runError.bind(this)); this.#signals.run(this.#runSettingsRecovery.bind(this)); this.#signals.run(this.#runCaptureRecovery.bind(this)); this.#signals.run(this.#runRegister.bind(this)); @@ -376,24 +377,34 @@ export class Encoder { this.#rearm(); } - // Clear a latched failure so a fresh registration retries. + // The current slot, read through `effect`. + #current(effect: Effect): Slot { + return effect.get(this.#slot) ?? this.#local; + } + + // The public error is the current slot's, so a new network broadcast starts without one. + #runError(effect: Effect): void { + effect.set(this.#out.error, effect.get(this.#current(effect).error)); + } + + // Clear a latched failure, so the broadcast serves the rendition again under a fresh track name. #rearm(): void { - if (!this.#out.error.peek()) return; - this.#out.error.set(undefined); + const failure = (this.#slot.peek() ?? this.#local).error; + if (!failure.peek()) return; + failure.set(undefined); this.#floor = undefined; - this.#failure.set(new Once()); } // Register the rendition on the broadcast, publish its config, and encode while it hands over a // track. Re-registers cleanly when the broadcast swaps. #runRegister(effect: Effect): void { - const failure = effect.get(this.#failure); const broadcast = effect.get(this.in.broadcast); if (!broadcast) return; const rendition = broadcast.audio(this.name); - errors.set(rendition, failure); effect.cleanup(() => rendition.close()); + effect.proxy(this.#slot, rendition.slot); + effect.cleanup(() => this.#slot.set(undefined)); // Publish the resolved config; undefined (no capture) drops it from the catalog. effect.proxy(rendition.config, this.out.catalog); @@ -436,12 +447,10 @@ export class Encoder { const capture = effect.get(this.in.capture); const format = capture ? effect.get(capture.out.format) : undefined; const track = effect.get(rendition.track); + // A dead encoder can't serve anyone: the broadcast aborts its track with the real error, so + // the current subscriber and every later one get it rather than a track that stays silent. const fatal = effect.get(this.#out.error); - // A dead encoder can't serve anyone, so the current subscriber and every later one get - // the real error rather than a track that stays silent. - if (fatal) track?.abort(fatal); - const onDemand = effect.get(broadcast.in.onDemand) ?? true; const demanded = track && effect.get(track.demand().wanted) !== undefined; this.#encoding = enabled && !!format && !!track && !fatal && (!onDemand || !!demanded); @@ -581,16 +590,11 @@ export class Encoder { #runCatalog(effect: Effect): void { const estimate = effect.get(this.#estimate); const enabled = effect.get(this.in.enabled) && !effect.get(this.#out.error); - const key = effect.get(this.in.broadcast) ?? this; - let track = this.#advertised.get(key); - if (!track) { - track = { bitrate: 0 }; - this.#advertised.set(key, track); - } + const slot = this.#current(effect); if (!enabled) this.#paused = true; const config = enabled ? effect.get(this.#config)?.catalog : undefined; if (!config) { - const last = this.#paused ? track.last : undefined; + const last = this.#paused ? slot.last : undefined; effect.set(this.#out.catalog, last && { ...last, ...estimate, enabled: false }); return; } @@ -598,10 +602,10 @@ export class Encoder { // The catalog bitrate is the most this track has been configured for, so lowering the knob // retunes the encoder without lowering what the catalog advertises. - track.bitrate = Math.max(track.bitrate, config.bitrate ?? 0); - const bitrate = track.bitrate > 0 ? Catalog.u53(track.bitrate) : undefined; - track.last = { ...config, bitrate, ...estimate }; - effect.set(this.#out.catalog, track.last); + slot.bitrate = Math.max(slot.bitrate, config.bitrate ?? 0); + const bitrate = slot.bitrate > 0 ? Catalog.u53(slot.bitrate) : undefined; + slot.last = { ...config, bitrate, ...estimate }; + effect.set(this.#out.catalog, slot.last); } // Collect the encode-only Opus knobs that are set, reading the codec through the effect so the @@ -810,10 +814,10 @@ export class Encoder { } #abort(error: unknown): void { - if (this.#out.error.peek()) return; + const failure = (this.#slot.peek() ?? this.#local).error; + if (failure.peek()) return; const cause = error instanceof Error ? error : new Error(String(error)); - this.#failure.peek().set(cause); - this.#out.error.set(cause); + failure.set(cause); this.#live?.track.abort(cause); } diff --git a/js/publish/src/broadcast.test.ts b/js/publish/src/broadcast.test.ts index bcb192b3736..307ac81a94a 100644 --- a/js/publish/src/broadcast.test.ts +++ b/js/publish/src/broadcast.test.ts @@ -761,3 +761,133 @@ test("identity changes before the first announce take a fresh track name", async broadcast.close(); }); + +// Identity follows hang's `same_identity`: interlacing reinitializes a video decoder. +test("an interlacing change takes a fresh track name", async () => { + const broadcast = new Broadcast({ origin: new Origin.Producer(), name: Path.from("interlaced.hang") }); + const video = broadcast.video("video"); + video.config.set(videoConfig); + await settle(); + video.config.set({ ...videoConfig, interlaced: true }); + await settle(); + expect((await readCatalog(broadcast))?.video?.renditions.video?.track).toBe("video/1"); + expect(video.track.peek()?.name).toBe("video/1"); + + broadcast.close(); +}); + +// Names belong to one network broadcast, so a new one serves each rendition under its ID again. +test("a new network broadcast starts every track name over", async () => { + const name = new Signal(Path.from("one.hang")); + const broadcast = new Broadcast({ origin: new Origin.Producer(), name }); + const video = broadcast.video("video"); + video.config.set(videoConfig); + await settle(); + video.config.set({ ...videoConfig, codec: "vp8" }); + await settle(); + expect((await readCatalog(broadcast))?.video?.renditions.video?.track).toBe("video/1"); + expect(() => broadcast.video("video/1")).toThrow("replacement track"); + + name.set(Path.from("two.hang")); + await settle(); + expect((await readCatalog(broadcast))?.video?.renditions.video?.track).toBeUndefined(); + expect(video.track.peek()?.name).toBe("video"); + expect(video.track.peek()?.appendGroup().sequence).toBe(0); + expect(() => broadcast.video("video/1").close()).not.toThrow(); + + broadcast.close(); +}); + +// A fold that throws publishes nothing, so it must not move a track the catalog still lists. +test("a refused catalog fold renames no track", async () => { + const broadcast = new Broadcast({ origin: new Origin.Producer(), name: Path.from("refused.hang") }); + const video = broadcast.video("video"); + const audio = broadcast.audio("audio"); + video.config.set(videoConfig); + await settle(); + const track = video.track.peek(); + + video.config.set({ ...videoConfig, codec: "vp8" }); + audio.config.set({ ...audioConfig, track: "elsewhere" }); + await settle(); + expect(video.track.peek()).toBe(track); + expect(track?.closed.peek()).toBeUndefined(); + expect((await readCatalog(broadcast))?.video?.renditions.video?.codec).toBe(videoConfig.codec); + + // Once the fold succeeds, the new identity moves to a fresh name. + audio.config.set(audioConfig); + await settle(); + expect((await readCatalog(broadcast))?.video?.renditions.video?.track).toBe("video/1"); + expect(video.track.peek()?.name).toBe("video/1"); + + broadcast.close(); +}); + +// A failure keeps the rendition down, even across a sibling's registration, until it clears. The +// rendition then recovers under a fresh track name, so its group sequence can start over. +test("a failed rendition stays down until its failure clears", async () => { + const broadcast = new Broadcast({ origin: new Origin.Producer(), name: Path.from("failed.hang") }); + const video = broadcast.video("video"); + video.config.set(videoConfig); + await settle(); + const track = video.track.peek(); + const slot = video.slot.peek(); + if (!track || !slot) throw new Error("expected a served rendition"); + + const failure = new Error("encoder failed"); + slot.error.set(failure); + await settle(); + expect(track.closed.peek()).toBe(failure); + expect(video.track.peek()).toBeUndefined(); + broadcast.audio("audio").config.set(audioConfig); + await settle(); + expect(video.track.peek()).toBeUndefined(); + + slot.error.set(undefined); + await settle(); + expect(video.track.peek()?.name).toBe("video/1"); + expect((await readCatalog(broadcast))?.video?.renditions.video?.track).toBe("video/1"); + + broadcast.close(); +}); + +// A producer may abort its track itself, which fails the rendition's slot the same way. +test("aborting a rendition's track fails its slot", async () => { + const broadcast = new Broadcast({ origin: new Origin.Producer(), name: Path.from("aborted.hang") }); + const video = broadcast.video("video"); + video.config.set(videoConfig); + await settle(); + const failure = new Error("encoder failed"); + video.track.peek()?.abort(failure); + await settle(); + expect(video.slot.peek()?.error.peek()).toBe(failure); + + broadcast.close(); +}); + +// What a producer keeps for a rendition belongs to one network broadcast, so a new one starts it over. +test("a new network broadcast starts each rendition's slot over", async () => { + const name = new Signal(Path.from("one.hang")); + const broadcast = new Broadcast({ origin: new Origin.Producer(), name }); + const video = broadcast.video("video"); + video.config.set(videoConfig); + await settle(); + const first = video.slot.peek(); + if (!first) throw new Error("expected a slot"); + first.bitrate = 1_000_000; + first.error.set(new Error("encoder failed")); + await settle(); + expect(video.track.peek()).toBeUndefined(); + + name.set(Path.from("two.hang")); + await settle(); + const second = video.slot.peek(); + expect(second).toBeDefined(); + expect(second).not.toBe(first); + expect(second?.bitrate).toBe(0); + expect(second?.error.peek()).toBeUndefined(); + expect(video.track.peek()?.name).toBe("video"); + expect(video.track.peek()?.closed.peek()).toBeUndefined(); + + broadcast.close(); +}); diff --git a/js/publish/src/broadcast.ts b/js/publish/src/broadcast.ts index 46dc3ea80be..3057e931f64 100644 --- a/js/publish/src/broadcast.ts +++ b/js/publish/src/broadcast.ts @@ -1,13 +1,12 @@ import { Milli } from "@moq/clock"; import * as Catalog from "@moq/hang/catalog"; import * as Container from "@moq/hang/container"; -import * as Json from "@moq/json"; import * as Moq from "@moq/net"; import { Effect, type Getter, getter, type Inputs, type Readonlys, Signal } from "@moq/signals"; import { rotateVideoDimensions } from "@moq/video"; import { CatalogProducer } from "./catalog"; -import { errors } from "./internal"; import { Order } from "./order"; +import { type Identity, identity, type Listing, Publication, type Slot } from "./publication"; import type { Kind, Rendition } from "./rendition"; import { makeRendition } from "./rendition-internal"; @@ -112,31 +111,20 @@ export class Broadcast { // key add/remove; the Rendition values compare by identity, which is stable. readonly #renditions = new Signal>>({}); - // The writable track producer signals backing each Rendition's read-only `track`, keyed by name. + // The writable signals backing each Rendition's read-only `track` and `slot`, keyed by name. // A static network track is exposed here while it is open. On-demand gates non-audio tracks // on a subscriber; audio keeps its handle so it can buffer PCM across demand changes. - readonly #tracks = new Map>(); + // The slot is the current publication's, while there is one. + readonly #handles = new Map< + string, + { track: Signal; slot: Signal } + >(); - // The kind of each rendition track in the current network broadcast. A new one starts over. - #kinds = new Map(); + // The renditions of the current network broadcast, undefined while there is none. + readonly #publication = new Signal(undefined); - // Each catalog ID's track name in the current network broadcast, when it is not the ID itself. - // A replacement for a closed track, or a rendition whose media identity changed, keeps its - // catalog ID under a fresh track name, so a name always means the same content. - readonly #names = new Signal>({}); - - // Every replacement track name minted in the current network broadcast, even one never served. - #replacements = new Set(); - - // The decoder identity of each track name in the current network broadcast, or undefined before - // there is one. Fixed from the broadcast's start rather than its first announce, since an encoder - // may cache groups on a track before anyone can find it, and a late subscriber or a fetch reads - // them under the catalog's config. Kept across a rendition's removal, so a name returning with - // different media takes a fresh name too. - #served: Map | undefined; - - // The identity of each track name in the latest catalog fold, recorded when a network broadcast starts. - #listed = new Map(); + // Each catalog ID in the latest catalog fold, which seeds a new network broadcast's publication. + #listed = new Map(); #signals = new Effect(); @@ -189,31 +177,29 @@ export class Broadcast { if (this.#renditions.peek()[name]) { throw new Error(`rendition already registered: ${name}`); } - // A name keeps its track, so it keeps its kind: different media takes a different name. - const previous = this.#kinds.get(name); - if (previous !== undefined && previous !== kind) { - throw new Error(`rendition ${name} was registered as ${previous}, not ${kind}`); - } - // A replacement track holds or held the name, so another rendition's media owns it. - if (this.#replacements.has(name)) { - throw new Error(`rendition ${name} names another rendition's replacement track`); - } + this.#publication.peek()?.check(name, kind); - const track = new Signal(undefined); - this.#tracks.set(name, track); + const handle = { + track: new Signal(undefined), + slot: new Signal(undefined), + }; + this.#handles.set(name, handle); - const rendition = makeRendition(name, kind, track, () => this.#unregister(name)); + // The slot is the one its producer fills, so it holds this rendition's config. + const slot = handle.slot as Getter | undefined>; + const rendition = makeRendition(name, kind, handle.track, slot, () => this.#unregister(name)); this.#renditions.update((renditions) => ({ ...renditions, [name]: rendition as Rendition })); return rendition; } #unregister(name: string): void { - const track = this.#tracks.get(name); - if (track) { + const handle = this.#handles.get(name); + if (handle) { // The network track stays with the broadcast, so registering the name again continues it. - track.set(undefined); - this.#tracks.delete(name); + handle.track.set(undefined); + handle.slot.set(undefined); + this.#handles.delete(name); } this.#renditions.update((renditions) => { @@ -229,13 +215,14 @@ export class Broadcast { #runCatalog(effect: Effect) { const enabled = effect.get(this.in.enabled); const renditions = effect.get(this.#renditions); - const names = effect.get(this.#names); + const publication = effect.get(this.#publication); + if (publication) effect.get(publication.renames); const video: Record = {}; const audio: Record = {}; const text: Record = {}; - const listed = new Map(); - const renamed: Record = {}; + const listed = new Map(); + const served: [Rendition, string, Identity][] = []; for (const rendition of Object.values(renditions)) { const source = enabled ? effect.get(rendition.config) : undefined; @@ -245,16 +232,10 @@ export class Broadcast { if (target.broadcast !== undefined || target.track !== undefined) { throw new Error(`rendition ${rendition.name} config sets broadcast or track, which the broadcast owns`); } - let track = names[rendition.name] ?? rendition.name; - // Everything else (bitrate, frame rate, coded size, ...) updates the served track in place. - const media = identity(source); - const served = this.#served?.get(track); - if (served && !Json.deepEqual(served, media)) { - track = this.#mint(rendition.name); - renamed[rendition.name] = track; - } - this.#served?.set(track, media); - listed.set(track, media); + const media = identity(rendition.kind, source); + listed.set(rendition.name, { kind: rendition.kind, identity: media }); + const track = publication?.name(rendition, media) ?? rendition.name; + served.push([rendition, track, media]); const config = track !== rendition.name ? { ...(source as object), track } : source; if (rendition.kind === "video") { @@ -299,22 +280,14 @@ export class Broadcast { }); this.#listed = listed; - // The network loop ends each renamed rendition's old track and serves the new name. - if (Object.keys(renamed).length > 0) this.#names.update((names) => ({ ...names, ...renamed })); + // Only once the catalog holds them, so the track loop never ends a track the catalog still lists. + if (publication) for (const [rendition, track, media] of served) publication.commit(rendition, track, media); } - // The next unused `/` track name in the current network broadcast. This publisher owns the - // namespace and reserves `/` for the rendition (see `net`), so skipping every name it has - // served, registered, or listed in the catalog (including an application's own sections) is - // enough to never reuse one. - #mint(id: string): string { - const taken = new Set([ - ...this.#replacements, - ...(this.#served?.keys() ?? []), - ...Object.keys(this.#renditions.peek()), - // Every ID that held a track here, even one removed before it had a config. - ...this.#kinds.keys(), - ]); + // The names outside a publication's own that a replacement must avoid: every registered ID, and + // every ID and track name in the catalog, including an application's own sections. + #reserved(): Set { + const taken = new Set(Object.keys(this.#renditions.peek())); // A no-op edit is the catalog's only read; it publishes nothing. this.catalog.mutate((catalog) => { for (const entries of [ @@ -330,12 +303,7 @@ export class Broadcast { } } }); - for (let n = 1; ; n++) { - const name = `${id}/${n}`; - if (taken.has(name)) continue; - this.#replacements.add(name); - return name; - } + return taken; } #run(effect: Effect) { @@ -368,19 +336,14 @@ export class Broadcast { // Each track lives as long as the broadcast: a track name always means the same content, // so a re-announce or a rendition change keeps writing to it instead of restarting its - // group sequence under the same epoch. Rendition tracks by name, so registering a name - // again finds its track; the catalog's stay out of reach of a colliding rendition. - const tracks = new Map }>(); - this.#names.set({}); - this.#replacements = new Set(); - // From here a name keeps the identity it is first listed with. - this.#served = new Map(this.#listed); - const kinds = new Map(); - this.#kinds = kinds; + // group sequence under the same epoch. The publication keeps rendition tracks by catalog ID, + // so registering an ID again finds its track; the catalog's stay out of reach of a colliding + // rendition. + const publication = new Publication(this.#listed, () => this.#reserved()); + this.#publication.set(publication); effect.cleanup(() => { - for (const { track } of tracks.values()) track.finish(); - for (const { track } of tracks.values()) track.close(); - if (this.#kinds === kinds) this.#kinds = new Map(); + publication.close(); + if (this.#publication.peek() === publication) this.#publication.set(undefined); }); let serving = false; @@ -420,58 +383,54 @@ export class Broadcast { // track is open. Audio keeps it while buffering PCM; on-demand video gates it on a subscriber. effect.run((inner) => { const renditions = inner.get(this.#renditions); - const names = inner.get(this.#names); + inner.get(publication.renames); for (const rendition of Object.values(renditions)) { - const signal = this.#tracks.get(rendition.name); - if (!signal) continue; - - let entry = tracks.get(rendition.name); - const cause = entry && errors.get(entry.owner)?.peek(); - if (entry && cause) entry.track.abort(cause); - const name = names[rendition.name] ?? rendition.name; - if (entry && entry.track.name !== name) { + const handle = this.#handles.get(rendition.name); + if (!handle) continue; + + const entry = publication.entry(rendition); + inner.set(handle.slot, entry.slot); + // A replacement registration is a new producer, so the last one's failure no longer holds. + if (entry.owner && entry.owner !== rendition) entry.slot.error.set(undefined); + const cause = inner.get(entry.slot.error); + // Including an idle track, which no producer holds to abort. + if (entry.track && cause) entry.track.abort(cause); + if (entry.track && entry.track.name !== entry.name) { // The catalog moved this rendition to a new name for new media. End the old track // cleanly on its last identity, so its subscribers follow the catalog. entry.track.finish(); entry.track.close(); - entry = undefined; - } else if (entry && entry.owner !== rendition && entry.track.closed.peek() !== undefined) { - // An error leaves this rendition down. Only a replacement registration may recover, - // with a fresh track name so its group sequence can start over. - const fresh = this.#mint(rendition.name); - this.#names.update((names) => ({ ...names, [rendition.name]: fresh })); + entry.track = undefined; + } else if (entry.track && entry.track.closed.peek() !== undefined) { + // A failure leaves this rendition down until it clears. It then recovers under a fresh + // track name, so its group sequence can start over. + if (!cause) publication.rename(entry); continue; } - if (!entry) { + if (!entry.track) { const info = Container.trackInfo({ maxAge, priority: Catalog.PRIORITY[rendition.kind] }); - const track = broadcast.createTrack( - name, + entry.track = broadcast.createTrack( + entry.name, rendition.kind === "video" ? { ...info, refresh: VIDEO_REFRESH } : info, ); - entry = { track, owner: rendition }; - tracks.set(rendition.name, entry); - kinds.set(rendition.name, rendition.kind); - } else { - entry.owner = rendition; } + entry.owner = rendition; const served = entry.track; - const error = errors.get(rendition); - if (error) { - inner.run((failure) => { - const cause = failure.get(error); - if (cause) served.abort(cause); - }); - } inner.run((gate) => { - if (gate.get(served.closed) !== undefined) return; + const closed = gate.get(served.closed); + if (closed !== undefined) { + // A producer aborting its track directly fails the rendition the same way. + if (closed && !entry.slot.error.peek()) entry.slot.error.set(closed); + return; + } if ( rendition.kind !== "audio" && gate.get(this.in.onDemand) && gate.get(served.demand().wanted) === undefined ) return; - gate.set(signal, served); + gate.set(handle.track, served); }); } }); @@ -482,32 +441,3 @@ export class Broadcast { this.#signals.close(); } } - -// The fields a consumer can't change without reinitializing its decoder (or demuxer, for -// `container`), so changing any of them takes a new track name. -type Identity = Pick< - Partial, - | "codec" - | "description" - | "container" - | "sampleRate" - | "numberOfChannels" - | "displayAspectWidth" - | "displayAspectHeight" ->; - -// Cloned, so a producer mutating a config it shares (such as its container) can't rewrite what a -// name was served with. -function identity(config: unknown): Identity { - const { codec, description, container, sampleRate, numberOfChannels, displayAspectWidth, displayAspectHeight } = - config as Identity; - return structuredClone({ - codec, - description, - container, - sampleRate, - numberOfChannels, - displayAspectWidth, - displayAspectHeight, - }); -} diff --git a/js/publish/src/internal.ts b/js/publish/src/internal.ts index 30ef43dc943..a3222a88221 100644 --- a/js/publish/src/internal.ts +++ b/js/publish/src/internal.ts @@ -1,9 +1,6 @@ import type * as Moq from "@moq/net"; import type { Effect, Getter } from "@moq/signals"; -/** @internal Terminal failures of broadcast-owned rendition tracks, including idle tracks. */ -export const errors = new WeakMap>(); - /** * @internal Whether a subscriber is attached to `track`, scoped to `effect`. Changes only when that * does, not with every group the subscribers want, so a bandwidth reservation keyed on it holds. diff --git a/js/publish/src/publication.ts b/js/publish/src/publication.ts new file mode 100644 index 00000000000..0cb40493265 --- /dev/null +++ b/js/publish/src/publication.ts @@ -0,0 +1,180 @@ +import * as Json from "@moq/json"; +import type * as Moq from "@moq/net"; +import { type Getter, Signal } from "@moq/signals"; +import type { Kind, Rendition } from "./rendition"; + +/** + * @internal What a rendition's producer keeps for one catalog ID in one network broadcast. A new + * network broadcast starts a new slot, so what the producer advertised, and how it failed, start over + * with it. A class, so a signal holding one changes with each new slot. + */ +export class Slot { + /** The coded-size ceiling of the largest accepted probe (video). */ + ceiling?: { width: number; height: number }; + + /** The largest bitrate and frame rate advertised, since the catalog may only raise them. */ + bitrate = 0; + framerate = 0; + + /** The last config published while enabled, which a disabled rendition keeps advertising. */ + last?: Config; + + /** + * The producer's terminal failure. It ends the track and keeps the rendition down until it clears, + * when the rendition recovers under a fresh track name. + */ + readonly error = new Signal(undefined); +} + +/** @internal One catalog ID's rendition in a network broadcast. */ +export type Entry = { + readonly id: string; + readonly kind: Kind; + // The track name the catalog lists: the ID itself until new media or a replacement renames it. + name: string; + // The network track serving it. Kept after its rendition leaves, so registering the ID again + // continues it. + track?: Moq.Track.Producer; + // The registration that last served `track`. + owner?: Rendition; + // What the registration's producer keeps for this ID. + readonly slot: Slot; +}; + +/** @internal What the catalog lists for a rendition: its kind and its config's media identity. */ +export type Listing = { kind: Kind; identity: Identity }; + +/** + * @internal The renditions of one network broadcast, keyed by catalog ID. A new network broadcast + * starts a new publication, so every name, kind, track, and slot starts over with it. + */ +export class Publication { + // Each catalog ID that holds or held a rendition here. + readonly #entries = new Map(); + + // Every track name handed out here, with the catalog ID owning it and the identity it was first + // listed with (none for a replacement never listed). Kept after the ID leaves, so a name always + // means the same content: an encoder may cache groups on a track before anyone can find it, and + // a late subscriber or a fetch reads them under the catalog's config. + readonly #names = new Map(); + + readonly #renames = new Signal(0); + + /** Bumped on every rename, so the catalog fold and the track loop pick up the new name. */ + readonly renames: Getter = this.#renames; + + // Names outside this publication that a minted name must also avoid. + readonly #reserved: () => Iterable; + + /** + * Seeded with the latest catalog fold, so from here a name keeps the identity it is first listed + * with. `reserved` lists the other names a replacement must not take. + */ + constructor(listed: Map, reserved: () => Iterable) { + this.#reserved = reserved; + for (const [id, { kind, identity }] of listed) { + this.#entries.set(id, { id, kind, name: id, slot: new Slot() }); + this.#names.set(id, { id, identity }); + } + } + + /** Throw unless `id` may register as `kind`. */ + check(id: string, kind: Kind): void { + // An ID keeps its track, so it keeps its kind: different media takes a different ID. + const previous = this.#entries.get(id)?.kind; + if (previous !== undefined && previous !== kind) { + throw new Error(`rendition ${id} was registered as ${previous}, not ${kind}`); + } + // A replacement track holds or held the name, so another rendition's media owns it. + const owner = this.#names.get(id)?.id; + if (owner !== undefined && owner !== id) { + throw new Error(`rendition ${id} names another rendition's replacement track`); + } + } + + /** The entry for `rendition`'s catalog ID, created on first sight. */ + entry(rendition: Rendition): Entry { + let entry = this.#entries.get(rendition.name); + if (!entry) { + entry = { id: rendition.name, kind: rendition.kind, name: rendition.name, slot: new Slot() }; + this.#entries.set(rendition.name, entry); + this.#names.set(rendition.name, { id: rendition.name }); + } + return entry; + } + + /** + * The track name `rendition` would serve its config's `identity` under: its current one, or a + * fresh one for a new identity. Everything else updates the served track in place. Changes + * nothing until {@link commit}, so a catalog edit that fails leaves the tracks alone. + */ + name(rendition: Rendition, identity: Identity): string { + const current = this.#entries.get(rendition.name)?.name ?? rendition.name; + const listed = this.#names.get(current)?.identity; + if (listed && !Json.deepEqual(listed, identity)) return this.#mint(rendition.name); + return current; + } + + /** Record that the catalog lists `rendition` under `name` with `identity`. */ + commit(rendition: Rendition, name: string, identity: Identity): void { + const entry = this.entry(rendition); + if (entry.name !== name) this.#move(entry, name); + const listed = this.#names.get(name); + if (listed && !listed.identity) listed.identity = identity; + } + + /** + * Move `entry` to a fresh track name, such as for a replacement registration after its track + * closed. The track loop ends its old track, so that track's subscribers follow the catalog. + */ + rename(entry: Entry): void { + this.#move(entry, this.#mint(entry.id)); + } + + #move(entry: Entry, name: string): void { + this.#names.set(name, { id: entry.id }); + entry.name = name; + this.#renames.update((n) => n + 1); + } + + // The next unused `/` track name. This publisher owns the namespace and reserves `/` for + // the rendition (see `Broadcast.net`), so skipping every name handed out here and every reserved + // one is enough to never reuse one. + #mint(id: string): string { + const taken = new Set([...this.#names.keys(), ...this.#entries.keys(), ...this.#reserved()]); + for (let n = 1; ; n++) { + const name = `${id}/${n}`; + if (!taken.has(name)) return name; + } + } + + /** End every track. */ + close(): void { + for (const { track } of this.#entries.values()) track?.finish(); + for (const { track } of this.#entries.values()) track?.close(); + } +} + +/** + * The fields a consumer can't change without reinitializing its decoder (or demuxer, for + * `container`), so changing any of them takes a new track name. Mirrors hang's + * `VideoConfig::same_identity` and `AudioConfig::same_identity`. + */ +export type Identity = Record; + +const IDENTITY: Record = { + video: ["codec", "description", "container", "displayAspectWidth", "displayAspectHeight", "interlaced"], + audio: ["codec", "description", "container", "sampleRate", "numberOfChannels"], + text: ["container"], +}; + +/** + * @internal The identity of a `kind` config. Cloned, so a producer mutating a config it shares (such + * as its container) can't rewrite what a name was served with. + */ +export function identity(kind: Kind, config: unknown): Identity { + const source = config as Record; + const fields: Identity = {}; + for (const field of IDENTITY[kind]) fields[field] = source[field]; + return structuredClone(fields); +} diff --git a/js/publish/src/rendition-internal.ts b/js/publish/src/rendition-internal.ts index 44eea26e877..9ad9efaf973 100644 --- a/js/publish/src/rendition-internal.ts +++ b/js/publish/src/rendition-internal.ts @@ -1,14 +1,16 @@ import type * as Moq from "@moq/net"; import { type Getter, Signal } from "@moq/signals"; +import type { Slot } from "./publication"; /** The media kind of a rendition, selecting its catalog section (`video`, `audio`, or `text`). */ export type Kind = "video" | "audio" | "text"; -/** Register a handle whose track and teardown belong to its broadcast. */ +/** @internal Register a handle whose track and teardown belong to its broadcast. */ export let makeRendition: ( name: string, kind: Kind, track: Getter, + slot: Getter | undefined>, close: () => void, ) => Rendition; @@ -44,18 +46,31 @@ export class Rendition { */ readonly track: Getter; + /** + * @internal What the producer keeps for this rendition in the current network broadcast, or + * `undefined` while there is none. Setting its `error` ends the track. + */ + readonly slot: Getter | undefined>; + readonly #close: () => void; #closed = false; - private constructor(name: string, kind: Kind, track: Getter, close: () => void) { + private constructor( + name: string, + kind: Kind, + track: Getter, + slot: Getter | undefined>, + close: () => void, + ) { this.name = name; this.kind = kind; this.track = track; + this.slot = slot; this.#close = close; } static { - makeRendition = (name, kind, track, close) => new Rendition(name, kind, track, close); + makeRendition = (name, kind, track, slot, close) => new Rendition(name, kind, track, slot, close); } /** diff --git a/js/publish/src/video/capture.test.ts b/js/publish/src/video/capture.test.ts index 6e94af14905..097f9e46fdc 100644 --- a/js/publish/src/video/capture.test.ts +++ b/js/publish/src/video/capture.test.ts @@ -443,7 +443,7 @@ test("an encoder resuming on a still source encodes a keyframe of the current pi const track = new Moq.Track.Producer("video").accept({ timescale: Moq.Time.Timescale.MILLI }); // No subscriber yet, so the encoder idles while the source sends its only frame. const live = new Signal(undefined); - const rendition = { config: new Signal(undefined), track: live, close: () => {} }; + const rendition = { config: new Signal(undefined), track: live, slot: new Signal(undefined), close: () => {} }; const encoder = new Encoder("video", { enabled: true, broadcast: { video: () => rendition, order: new Order() } as never, @@ -550,7 +550,7 @@ test("an encoder torn down while a frame is in flight closes that frame", async const capture = new Capture({ source: input.source }); const track = new Moq.Track.Producer("video").accept({ timescale: Moq.Time.Timescale.MILLI }); const live = new Signal(undefined); - const rendition = { config: new Signal(undefined), track: live, close: () => {} }; + const rendition = { config: new Signal(undefined), track: live, slot: new Signal(undefined), close: () => {} }; const encoder = new Encoder("video", { enabled: true, broadcast: { video: () => rendition, order: new Order() } as never, diff --git a/js/publish/src/video/encoder-stall.test.ts b/js/publish/src/video/encoder-stall.test.ts index 8441cfb25f5..f03a2f95540 100644 --- a/js/publish/src/video/encoder-stall.test.ts +++ b/js/publish/src/video/encoder-stall.test.ts @@ -110,6 +110,7 @@ test("a slower-than-realtime encoder drops input instead of ratcheting catalog l // An on-time audio rendition shares the broadcast, writing each capture as it happens. const audio = new Estimator(); const rendition = { + slot: new Signal(undefined), config: new Signal(undefined), track: new Signal(track), close: () => { diff --git a/js/publish/src/video/encoder.test.ts b/js/publish/src/video/encoder.test.ts index 0ea3499c4c9..a69d7f1b2ff 100644 --- a/js/publish/src/video/encoder.test.ts +++ b/js/publish/src/video/encoder.test.ts @@ -7,6 +7,7 @@ import * as Moq from "@moq/net"; import { Signal } from "@moq/signals"; import { Broadcast } from "../broadcast"; import { Estimator, Order } from "../order"; +import { Slot } from "../publication"; import { Encoder } from "./encoder"; // How long a fake hardware probe takes on the installed clock. @@ -123,6 +124,7 @@ test("encoding tracks encoder config in its child effect", async () => { const track = new Moq.Track.Producer("video/hd").accept({ timescale: Moq.Time.Timescale.MILLI }); const rendition = { + slot: new Signal(undefined), config: new Signal(undefined), track: new Signal(track), close: () => { @@ -164,6 +166,7 @@ test("an on-demand gap marks a discontinuity and leaves the broadcast-owned trac const track = new Moq.Track.Producer("video").accept({ timescale: Moq.Time.Timescale.MILLI, priority: 60 }); const live = new Signal(track); const rendition = { + slot: new Signal(undefined), config: new Signal(undefined), track: live, close: () => { @@ -218,6 +221,7 @@ test("a bandwidth estimate updates the bitrate without blanking the config or re const track = new Moq.Track.Producer("video").accept({ timescale: Moq.Time.Timescale.MILLI, priority: 60 }); const sub = track.subscribe(); const rendition = { + slot: new Signal(undefined), config: new Signal(undefined), track: new Signal(track), close: () => { @@ -303,6 +307,7 @@ test("every published config fits a probe for its own codec", async () => { const track = new Moq.Track.Producer("video").accept({ timescale: Moq.Time.Timescale.MILLI, priority: 60 }); const sub = track.subscribe(); const rendition = { + slot: new Signal(undefined), config: new Signal(undefined), track: new Signal(track), close: () => { @@ -655,6 +660,7 @@ test("disabling encodes one black keyframe and keeps the rendition with enabled: const track = new Moq.Track.Producer("video").accept({ timescale: Moq.Time.Timescale.MILLI }); const rendition = { + slot: new Signal(undefined), config: new Signal(undefined), track: new Signal(track), close: () => { @@ -822,6 +828,7 @@ test("the probe encodes with the frame rate and bitrate ceiling", async () => { const track = new Moq.Track.Producer("video").accept({ timescale: Moq.Time.Timescale.MILLI, priority: 60 }); const sub = track.subscribe(); const rendition = { + slot: new Signal(undefined), config: new Signal(undefined), track: new Signal(track), close: () => { @@ -1029,6 +1036,7 @@ async function recordKeyframes( const track = new Moq.Track.Producer("video").accept({ timescale: Moq.Time.Timescale.MICRO, ...info }); const rendition = { + slot: new Signal(undefined), config: new Signal(undefined), track: new Signal(track), close: () => { @@ -1333,6 +1341,50 @@ test("a failed video encoder disables only its rendition and recovers on a fresh } }); +// A failure belongs to the network broadcast's track, so a new network broadcast starts without it, +// and with its own maxima. +test("a new network broadcast clears a video encoder failure and its maxima", async () => { + using _videoEncoder = installFakeVideoEncoder(); + using _probe = spyOn(FakeVideoEncoder, "isConfigSupported").mockResolvedValue({ supported: true }); + const flush = async () => { + for (let i = 0; i < 10; i++) await new Promise((resolve) => queueMicrotask(resolve)); + }; + const name = new Signal(Moq.Path.from("one.hang")); + const broadcast = new Broadcast({ origin: new Moq.Origin.Producer(), name }); + const capture = { + in: { source: new Signal({ getSettings: () => ({ frameRate: 30 }), getConstraints: () => ({}) } as never) }, + out: { display: new Signal({ width: 640, height: 480 }), frames: new Signal(undefined) }, + }; + const encoder = new Encoder("video", { broadcast, capture: capture as never, config: { frameRate: 60 } }); + try { + await encoder.out.catalog.changed(); + await flush(); + encoder.config.set({ frameRate: 30 }); + await flush(); + expect(encoder.out.catalog.peek()?.framerate).toBe(60); + + const live = FakeVideoEncoder.instances.find((instance) => instance.state === "configured"); + if (!live) throw new Error("expected live video encoder"); + const failure = new Error("GPU encoder failed"); + live.fail(failure); + await flush(); + expect(encoder.out.error.peek()).toBe(failure); + expect(encoder.out.active.peek()).toBe(false); + + name.set(Moq.Path.from("two.hang")); + await flush(); + expect(encoder.out.error.peek()).toBeUndefined(); + expect(encoder.out.active.peek()).toBe(true); + const catalog = encoder.out.catalog.peek(); + expect(catalog?.enabled).not.toBe(false); + expect(catalog?.track).toBeUndefined(); + expect(catalog?.framerate).toBe(30); + } finally { + encoder.close(); + broadcast.close(); + } +}); + // A track's coded size is a ceiling fixed for its name: a smaller capture resizes in band, opening a // group whose keyframe carries the new size, and the catalog never hears about it. test("a shrinking capture resizes in band below the advertised ceiling", async () => { @@ -1404,6 +1456,7 @@ test("a shrinking capture resizes in band below the advertised ceiling", async ( const track = new Moq.Track.Producer("video").accept({ timescale: Moq.Time.Timescale.MILLI }); const rendition = { + slot: new Signal(undefined), config: new Signal(undefined), track: new Signal(track), close: () => { @@ -1476,6 +1529,7 @@ test("the catalog bitrate and frame rate only rise", async () => { const track = new Moq.Track.Producer("video").accept({ timescale: Moq.Time.Timescale.MILLI, priority: 60 }); const sub = track.subscribe(); const rendition = { + slot: new Signal(undefined), config: new Signal(undefined), track: new Signal(track), close: () => { @@ -1527,16 +1581,22 @@ test("the catalog bitrate and frame rate only rise", async () => { } }); -// Another broadcast is another track, so the ceiling and maxima start over there, while returning to a -// broadcast continues the track it kept. -test("each broadcast keeps its own ceiling and maxima", async () => { +// Another slot is another track, so the ceiling and maxima start over there, while returning to a +// slot continues the track it kept. A new network broadcast, or another broadcast, hands over a new slot. +test("each slot keeps its own ceiling and maxima", async () => { using _videoEncoder = installFakeVideoEncoder(); - const rendition = () => ({ - config: new Signal(undefined), - track: new Signal(undefined), - close: () => {}, - }); - const first = { video: rendition, order: new Order() }; + // A broadcast serving its one rendition from one slot, as a network broadcast does. + const fake = () => { + const slot = new Signal(new Slot()); + const rendition = () => ({ + config: new Signal(undefined), + track: new Signal(undefined), + slot, + close: () => {}, + }); + return { video: rendition, order: new Order() }; + }; + const first = fake(); const broadcast = new Signal(first); const capture = { in: { source: new Signal({ getSettings: () => ({ frameRate: 30 }), getConstraints: () => ({}) }) }, @@ -1558,7 +1618,7 @@ test("each broadcast keeps its own ceiling and maxima", async () => { expect(encoder.out.catalog.peek()).toMatchObject({ codedWidth: 1920, codedHeight: 1072, framerate: 60 }); const bitrate = encoder.out.catalog.peek()?.bitrate ?? 0; - broadcast.set({ video: rendition, order: new Order() }); + broadcast.set(fake()); // The previous broadcast's probe never describes the new track, even while its own runs. for (let i = 0; i < 5; i++) await Promise.resolve(); expect(encoder.out.catalog.peek()).toBeUndefined(); @@ -1581,7 +1641,7 @@ test("each broadcast keeps its own ceiling and maxima", async () => { enabled.set(false); await settle(); expect(encoder.out.catalog.peek()?.enabled).toBe(false); - broadcast.set({ video: rendition, order: new Order() }); + broadcast.set(fake()); await settle(); expect(encoder.out.catalog.peek()).toBeUndefined(); } finally { diff --git a/js/publish/src/video/encoder.ts b/js/publish/src/video/encoder.ts index 042f193871f..186d7395690 100644 --- a/js/publish/src/video/encoder.ts +++ b/js/publish/src/video/encoder.ts @@ -8,14 +8,14 @@ import { type Getter, getter, type Inputs, - Once, type Readonlys, readonlys, Signal, } from "@moq/signals"; import type { Broadcast } from "../broadcast"; -import { errors, watched } from "../internal"; +import { watched } from "../internal"; import { Estimator, type Order } from "../order"; +import { Slot } from "../publication"; import { hardwareReliable } from "../support/video"; import type { Capture } from "./capture"; import { normalizeSource, type Source } from "./types"; @@ -162,10 +162,10 @@ export class Encoder { // The output dimensions of the video in pixels. #dimensions = new Signal<{ width: number; height: number } | undefined>(undefined); - // The coded-size ceiling to probe for the current broadcast: each side's largest of its accepted + // The coded-size ceiling to probe for the current slot: each side's largest of its accepted // ceiling and the current size. A smaller capture resizes in band below it without changing what // the catalog advertises. - #coded = new Signal<{ broadcast?: Broadcast; width: number; height: number } | undefined>(undefined); + #coded = new Signal<{ slot: Slot; width: number; height: number } | undefined>(undefined); // The config the browser accepted and encoded a probe frame with, and the codec string it reported. // Kept through a re-probe, so the rendition stays in the catalog until the new probe replaces it. @@ -198,8 +198,6 @@ export class Encoder { */ readonly settled: Computed; - // Each registration keeps its terminal failure even after the public error clears. - #failure = new Signal(new Once()); // The capture inputs #runCaptureRecovery last saw, carried across a gap in the source or display. #seen?: { capture?: Capture; @@ -210,10 +208,12 @@ export class Encoder { #estimator = new Estimator(); // The estimator's delay, republished whenever it rises. #estimate = new Signal({}); - // What this rendition advertised on each broadcast, keyed by the broadcast or, for a local preview, - // the encoder. A broadcast keeps a rendition's track when it re-registers, so returning to one - // continues its ceiling, maxima, and last config, while another broadcast starts its own. - #advertised = new WeakMap(); + // What this rendition keeps for the current network broadcast's track: its ceiling, maxima, last + // config, and failure. A network broadcast keeps a rendition's track when it re-registers, so + // returning to one continues them, while a new one starts its own. + #slot = new Signal | undefined>(undefined); + // The slot while no network broadcast is serving the rendition, such as for a local preview. + #local = new Slot(); // Whether the rendition was disabled since a config last resolved, so it keeps advertising its last // config as disabled until the re-enabled capture resolves a new one. A capture that never reopens // leaves it disabled, which is accurate: no frames are coming. @@ -270,20 +270,20 @@ export class Encoder { } }); } + this.#signals.run(this.#runError.bind(this)); this.#signals.run(this.#runSettingsRecovery.bind(this)); this.#signals.run(this.#runCaptureRecovery.bind(this)); this.#signals.run(this.#runRegister.bind(this)); } - // What this rendition advertised on `broadcast`'s track. - #track(broadcast: Broadcast | undefined): Advertised { - const key = broadcast ?? this; - let track = this.#advertised.get(key); - if (!track) { - track = { bitrate: 0, framerate: 0 }; - this.#advertised.set(key, track); - } - return track; + // The current slot, read through `effect`. + #current(effect: Effect): Slot { + return effect.get(this.#slot) ?? this.#local; + } + + // The public error is the current slot's, so a new network broadcast starts without one. + #runError(effect: Effect): void { + effect.set(this.#out.error, effect.get(this.#current(effect).error)); } // Count a failure until `effect` reruns with new inputs. @@ -320,23 +320,21 @@ export class Encoder { this.#rearm(); } - // Clear a latched failure so a fresh registration retries. + // Clear a latched failure, so the broadcast serves the rendition again under a fresh track name. #rearm(): void { - if (!this.#out.error.peek()) return; - this.#out.error.set(undefined); - this.#failure.set(new Once()); + (this.#slot.peek() ?? this.#local).error.set(undefined); } // Register the rendition on the broadcast and drive its catalog + encode loop. Re-registers cleanly // when the broadcast swaps. #runRegister(effect: Effect): void { - const failure = effect.get(this.#failure); const broadcast = effect.get(this.in.broadcast); if (!broadcast) return; const rendition = broadcast.video(this.name); - errors.set(rendition, failure); effect.cleanup(() => rendition.close()); + effect.proxy(this.#slot, rendition.slot); + effect.cleanup(() => this.#slot.set(undefined)); // Publish the resolved catalog config; undefined (until one resolves) leaves it out of the catalog. effect.proxy(rendition.config, this.out.catalog); @@ -578,10 +576,10 @@ export class Encoder { } #abort(error: unknown, track: Moq.Track.Producer): void { - if (this.#out.error.peek()) return; + const failure = (this.#slot.peek() ?? this.#local).error; + if (failure.peek()) return; const cause = error instanceof Error ? error : new Error(String(error)); - this.#failure.peek().set(cause); - this.#out.error.set(cause); + failure.set(cause); track.abort(cause); } @@ -593,11 +591,11 @@ export class Encoder { #runCatalog(effect: Effect): void { const estimate = effect.get(this.#estimate); const enabled = effect.get(this.in.enabled) && !effect.get(this.#out.error); - const track = this.#track(effect.get(this.in.broadcast)); + const slot = this.#current(effect); if (!enabled) this.#paused = true; const live = enabled ? effect.get(this.#live) : undefined; if (!live) { - const last = this.#paused ? track.last : undefined; + const last = this.#paused ? slot.last : undefined; effect.set(this.#out.catalog, last && { ...last, ...estimate, enabled: false }); return; } @@ -606,12 +604,12 @@ export class Encoder { // Advertise the codec string the probe's encoder reported rather than the one we configured, // so it names the profile and level the bitstream actually carries at the ceiling. const { config, reported } = live.detected; - track.bitrate = Math.max(track.bitrate, config.bitrate); - track.framerate = Math.max(track.framerate, config.framerate); + slot.bitrate = Math.max(slot.bitrate, config.bitrate); + slot.framerate = Math.max(slot.framerate, config.framerate); const catalog: Catalog.VideoConfig = { codec: reported, - bitrate: Catalog.u53(track.bitrate), - framerate: track.framerate, + bitrate: Catalog.u53(slot.bitrate), + framerate: slot.framerate, codedWidth: Catalog.u53(config.width), codedHeight: Catalog.u53(config.height), optimizeForLatency: true, @@ -621,7 +619,7 @@ export class Encoder { ...estimate, }; - track.last = catalog; + slot.last = catalog; effect.set(this.#out.catalog, catalog); } @@ -642,9 +640,9 @@ export class Encoder { // Captured now: the run's signal is replaced once a rerun starts. const superseded = effect.abort; effect.spawn(async () => { - const track = this.#track(dimensions.broadcast); + const { slot } = dimensions; try { - let detected: Detected; + let detected: Probe; try { detected = await this.#bestCodec(dimensions, target); } catch (err) { @@ -652,7 +650,7 @@ export class Encoder { // down to fit it, until a smaller capture reruns the probe. Re-probing the ceiling with the // current target, rather than reusing the old probe, still refuses a target the hardware // can't meet and survives a disable that dropped the old probe. - const accepted = track.ceiling; + const accepted = slot.ceiling; if ( superseded.aborted || !accepted || @@ -670,8 +668,8 @@ export class Encoder { // Only an accepted probe commits a ceiling, so a size the hardware refuses never sticks. const { width, height } = detected.config; - track.ceiling = { width, height }; - this.#codec.set({ ...detected, broadcast: dimensions.broadcast }); + slot.ceiling = { width, height }; + this.#codec.set({ ...detected, slot }); } catch (err) { // A newer probe reports its own outcome. if (superseded.aborted) return; @@ -690,12 +688,12 @@ export class Encoder { #runResolved(effect: Effect): void { if (!effect.get(this.in.enabled)) return; - // A probe for another broadcast describes another track, so wait for this one's. - const broadcast = effect.get(this.in.broadcast); + // A probe for another slot describes another track, so wait for this one's. + const slot = this.#current(effect); const detected = effect.get(this.#codec); const dimensions = effect.get(this.#dimensions); const target = effect.get(this.#target); - if (!detected || detected.broadcast !== broadcast || !dimensions || !target) return; + if (!detected || detected.slot !== slot || !dimensions || !target) return; // A capture that outgrew the probe encodes scaled down to fit until the re-probe at the new // ceiling lands, so the live config never exceeds what the catalog advertises. @@ -758,24 +756,24 @@ export class Encoder { effect.set(this.#dimensions, { width, height }); } - // The ceiling to probe: each side's largest of this broadcast's accepted ceiling and the current - // size. Reruns once a probe commits a ceiling, which leaves it unchanged. + // The ceiling to probe: each side's largest of this slot's accepted ceiling and the current size. + // Reruns once a probe commits a ceiling, which leaves it unchanged. #runCeiling(effect: Effect): void { const dimensions = effect.get(this.#dimensions); if (!dimensions) return; - const broadcast = effect.get(this.in.broadcast); + const slot = this.#current(effect); effect.get(this.#codec); - const ceiling = this.#track(broadcast).ceiling; + const ceiling = slot.ceiling; effect.set(this.#coded, { - broadcast, + slot, width: Math.max(ceiling?.width ?? 0, dimensions.width), height: Math.max(ceiling?.height ?? 0, dimensions.height), }); } // Try to determine the best config for the given settings. - async #bestCodec(dimensions: { width: number; height: number }, target: Target): Promise { + async #bestCodec(dimensions: { width: number; height: number }, target: Target): Promise { // A list of codecs to try, in order of preference. Only full RFC 6381 strings: Chrome, Firefox, // and Safari all refuse a bare `avc1` or `vp09`, and native players can't decode without the profile. const HARDWARE_CODECS = [ @@ -840,7 +838,7 @@ export class Encoder { // The full config, like moq-video's probe: an encoder may pick its level from the frame rate // or bitrate. The bitrate is the ceiling rather than the grant, so a bandwidth estimate never // re-probes. - const config: Detected["config"] = { + const config: Probe["config"] = { codec, width: dimensions.width, height: dimensions.height, @@ -900,26 +898,16 @@ type Target = { }; // A hardware probe result. -type Detected = { +type Probe = { // The config the browser accepted and encoded a probe frame with. Its bitrate is the ceiling, // before any bandwidth grant. config: VideoEncoderConfig & { framerate: number; bitrate: number }; // The codec string the encoder reported for it, which the catalog advertises. reported: string; - // The broadcast the probe ran for; another one starts its own ceiling. - broadcast?: Broadcast; }; -// What a rendition advertised on one broadcast's track. -type Advertised = { - // The coded-size ceiling of the largest accepted probe. - ceiling?: { width: number; height: number }; - // The largest bitrate and frame rate advertised, since the catalog may only raise them. - bitrate: number; - framerate: number; - // The last config published while enabled, which a disabled rendition keeps advertising. - last?: Catalog.VideoConfig; -}; +// A probe and the slot it ran for; another slot starts its own ceiling. +type Detected = Probe & { slot: Slot }; // The most this rendition ever sends with `codec`, before a bandwidth grant caps it. function ceiling(codec: string, dimensions: { width: number; height: number }, target: Target): number { diff --git a/quest/m1/cut-audit/README.md b/quest/m1/cut-audit/README.md index 7b71567913c..8bd15026ecb 100644 --- a/quest/m1/cut-audit/README.md +++ b/quest/m1/cut-audit/README.md @@ -42,8 +42,7 @@ internal and can ship after the cut: [moq-transport request leases](/quest/m2/ietf-request-lease.md), [group heads filled in place](/quest/m2/ietf-fill-refill.md), [one relay serve path](/quest/m2/relay-serve-path.md), -moqsrc pad actors (since landed), and -[publish rendition identity](/quest/m2/publish-publication.md). Other +moqsrc pad actors and publish rendition identity (both since landed). Other redesign evidence went into the quests that already own the mechanism: [subscribe ranges](/quest/m1/subscribe-ranges/README.md), [drop set](/quest/m1/drop-set.md), diff --git a/quest/m2/README.md b/quest/m2/README.md index df1ee6dd000..c328706b853 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -116,7 +116,6 @@ lite-07; bitrate enforcement is not part of this release. - [One relay serve path for every runtime](/quest/m2/relay-serve-path.md) - io_uring serves through the shared `Connection::serve` and config mapping - [A parked read of a resumed group reads its budget without re-syncing](/quest/m2/recover-budget.md) - measure, then drop the per-frame reader sync - [Transcode rungs scale off the runtime and keep bounded state](/quest/m2/transcode-rungs.md) - resize on encoder threads, memoize rungs by shape, one writer per rung track -- [@moq/publish owns rendition identity in one publication](/quest/m2/publish-publication.md) - one map per broadcast replaces five - [One port](/quest/m2/one-port/README.md) - one UDP port carries QUIC, SRT, and WebRTC media through an embedder hook, and one TCP port carries HTTP, RTMP, and RTMPS - [qmux receive leases](/quest/m2/qmux-receive-lease.md) - WebSocket, TCP, and WebRTC sessions bound incomplete messages by their receive window, at a per-session cost the maintainer picks - [QUIC caps](/quest/m2/rate-quic.md) - paced MAX_DATA credit in and a capped pacer out hold a QUIC session to its token's bitrate diff --git a/quest/m2/publish-publication.md b/quest/m2/publish-publication.md deleted file mode 100644 index 86ea4c9fd7c..00000000000 --- a/quest/m2/publish-publication.md +++ /dev/null @@ -1,24 +0,0 @@ -# [M] @moq/publish owns rendition identity in one publication - -## Goal - -`@moq/publish` keeps each network broadcast's renditions in one publication -object mapping catalog id to kind, name, track, and owner, so renames, -replacements, and resets are structural instead of kept in agreement across -five maps and per-encoder state. - -## Plan - -From the 2026-10-24 pre-cut audit at 151a529670. Internal only. - -Rendition identity fixes keep landing in `js/publish` (e24828a601, -fe311b3d34, 2938e78f77, 3791aecce3, 6e06a956b0, 644789413d, b32b7a7421, -d0337260a6). The state is spread over five name-keyed maps in `Broadcast` -(`#kinds`, `#names`, `#replacements`, `#served`, `#listed`), plus a -per-Broadcast `#advertised` WeakMap and three failure layers in each -encoder. Replace them with one publication object per network broadcast, -mirroring Rust's identity rules (`hang::catalog::VideoConfig::same_identity` -and `AudioConfig::same_identity`, enforced by `moq-mux` since -[moq-dev/moq#7110](https://github.com/moq-dev/moq/pull/7110)). - -Public API: none. Wire: none.