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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions js/watch/api/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export namespace Audio {
available: Signals.Signal<Record<string, Hang.Catalog.Audio.AudioConfig>>;
catalog: Signals.Signal<Hang.Catalog.Audio | undefined>;
config: Signals.Signal<Hang.Catalog.Audio.AudioConfig | undefined>;
error: Signals.Signal<Error | undefined>;
jitter: Signals.Signal<Clock.Milli | undefined>;
track: Signals.Signal<string | undefined>;
};
Expand Down
121 changes: 118 additions & 3 deletions js/watch/src/audio/source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ function broadcast(renditions: Record<string, Catalog.AudioConfig>) {
return { catalog, broadcast: { out: { catalog } } as unknown as Broadcast };
}

// Wait on the probe's causal boundary, rather than a timer or a guessed number of ticks.
// Wait out the probe, then a tick for the effects its result wakes. A probe served wholly from
// the cache sets and clears `pending` in one run, which notifies nothing, so start with a tick
// that lets the rerun happen rather than waiting on a change that may never come.
async function settled(source: Source): Promise<void> {
if (!source.pending.peek()) await source.pending.changed();
while (source.pending.peek()) await source.pending.changed();
await yieldNow();
while (source.pending.peek()) {
await source.pending.changed();
await yieldNow();
}
}

test("a rejected rendition probe preserves its supported siblings", async () => {
Expand Down Expand Up @@ -195,3 +200,113 @@ test("holds on a broadcast whose renditions match the previous one", async () =>
source.close();
}
});

async function quietly(fn: () => Promise<void>): Promise<void> {
const warn = console.warn;
console.warn = () => {};
try {
await fn();
} finally {
console.warn = warn;
}
}

test("reports NotSupportedError when no rendition is supported", () =>
quietly(async () => {
const state = broadcast({ mic: config("opus") });
const source = new Source({ broadcast: state.broadcast, supported: async () => false });
try {
await settled(source);
expect(source.out.error.peek()?.name).toBe("NotSupportedError");
expect(source.out.available.peek()).toEqual({});

state.catalog.set({ audio: { renditions: {} } });
await settled(source);
expect(source.out.error.peek()).toBeUndefined();
} finally {
source.close();
}
}));

test("a rejecting probe alone is unsupported", () =>
quietly(async () => {
const source = new Source({
broadcast: broadcast({ mic: config("opus") }).broadcast,
supported: async () => {
throw new TypeError("probe failed");
},
});
try {
await settled(source);
expect(source.out.error.peek()?.name).toBe("NotSupportedError");
expect(source.out.track.peek()).toBeUndefined();
} finally {
source.close();
}
}));

test("does not wedge the effect when a support probe never settles", async () => {
const supported = new Signal<(config: Catalog.AudioConfig) => Promise<boolean>>(() => new Promise(() => {}));
const source = new Source({ broadcast: broadcast({ mic: config("opus") }).broadcast, supported });
try {
if (!source.pending.peek()) await source.pending.changed();
expect(source.out.available.peek()).toEqual({});
expect(source.out.error.peek()).toBeUndefined();

// Swapping the probe reruns the effect, which has to tear the parked one down first.
supported.set(async () => true);
await settled(source);
expect(source.out.track.peek()).toBe("mic");
} finally {
source.close();
}
});

test("a catalog replaced while its probe is pending publishes only the replacement", async () => {
const probes = new Map<string, (supported: boolean) => void>();
const state = broadcast({ old: config("opus") });
const source = new Source({
broadcast: state.broadcast,
supported: (c) => new Promise((resolve) => probes.set(c.codec, resolve)),
});
try {
if (!source.pending.peek()) await source.pending.changed();
const old = probes.get("opus");
expect(old).toBeDefined();

state.catalog.set({ audio: { renditions: { next: config("mp4a.40.2") } } });
while (!probes.has("mp4a.40.2")) await yieldNow();

// The torn-down probe settles late; its result must not land.
old?.(true);
probes.get("mp4a.40.2")?.(true);
await settled(source);
expect(Object.keys(source.out.available.peek())).toEqual(["next"]);
expect(source.out.track.peek()).toBe("next");
} finally {
source.close();
}
});

test("serves unchanged configs from the cache when the catalog changes", async () => {
const state = broadcast({ mic: config("opus") });
const probed: string[] = [];
const source = new Source({
broadcast: state.broadcast,
supported: async (c) => {
probed.push(c.codec);
return true;
},
});
try {
await settled(source);
expect(probed).toEqual(["opus"]);

state.catalog.set({ audio: { renditions: { mic: config("opus"), backup: config("mp4a.40.2") } } });
await settled(source);
expect(probed).toEqual(["opus", "mp4a.40.2"]);
expect(Object.keys(source.out.available.peek())).toEqual(["mic", "backup"]);
} finally {
source.close();
}
});
53 changes: 14 additions & 39 deletions js/watch/src/audio/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Milli } from "@moq/clock";
import type * as Catalog from "@moq/hang/catalog";
import { Effect, type Getter, getter, type Inputs, type Readonlys, readonlys, Signal } from "@moq/signals";
import type { Broadcast } from "../broadcast";
import { SupportProbe } from "../support-probe";

/** What the viewer wants from the audio rendition. */
export type Target = {
Expand Down Expand Up @@ -32,6 +33,11 @@ export type SourceInput = {
export type SourceOutput = {
catalog: Signal<Catalog.Audio | undefined>;
available: Signal<Record<string, Catalog.AudioConfig>>;

// Why no rendition can be chosen, or undefined while healthy or still probing. A
// `NotSupportedError` means the browser can decode none of the catalog's renditions.
error: Signal<Error | undefined>;

track: Signal<string | undefined>;
config: Signal<Catalog.AudioConfig | undefined>;

Expand All @@ -47,18 +53,20 @@ export class Source {
/** The wired inputs, read-only. */
readonly in: Readonlys<SourceInput>;

#probe = new SupportProbe<Catalog.AudioConfig>("audio");

readonly #out: SourceOutput = {
catalog: new Signal<Catalog.Audio | undefined>(undefined),
available: new Signal<Record<string, Catalog.AudioConfig>>({}),
error: this.#probe.error,
track: new Signal<string | undefined>(undefined),
config: new Signal<Catalog.AudioConfig | undefined>(undefined),
jitter: new Signal<Milli | undefined>(undefined),
};
/** The derived outputs, read-only. */
readonly out = readonlys(this.#out);
#pending = new Signal(false);
/** Whether rendition support is still being resolved. @internal */
readonly pending: Getter<boolean> = this.#pending;
readonly pending: Getter<boolean> = this.#probe.pending;

// The last selection, which a disable holds while no enabled rendition remains.
#held?: { broadcast: Broadcast; track: string };
Expand Down Expand Up @@ -92,46 +100,13 @@ export class Source {
}

#runSupported(effect: Effect): void {
const supported = effect.get(this.in.supported);
// Read the catalog from the broadcast itself, so the probe result is tagged with the right one.
const broadcast = effect.get(this.in.broadcast);
const renditions = (broadcast && effect.get(broadcast.out.catalog)?.audio?.renditions) ?? {};
const supported = effect.get(this.in.supported);
if (!supported) return;

effect.set(this.#pending, true, false);
effect.spawn(async () => {
try {
const available: Record<string, Catalog.AudioConfig> = {};

for (const [name, config] of Object.entries(renditions)) {
// `supported` comes from the consumer, so we cannot assume it ever settles. A rerun
// waits for the tasks it spawned, so an unraced probe would hold the next run shut.
let isSupported = false;
try {
isSupported = !!(await effect.race(supported(config)));
} catch (err) {
if (effect.abort.aborted) return;
console.warn(
`[Source] audio rendition ${name} (${config.codec}) support probe failed; treating as unsupported`,
err,
);
}

// Torn down: stop probing and publish nothing, since the rerun redoes this.
if (effect.abort.aborted) return;

if (isSupported) available[name] = config;
}

if (Object.keys(available).length === 0 && Object.keys(renditions).length > 0) {
console.warn("no supported audio renditions found:", renditions);
}

this.#probed.set(broadcast);
this.#out.available.set(available);
} finally {
this.#pending.set(false);
}
this.#probe.run(effect, supported, renditions, (available) => {
this.#probed.set(broadcast);
this.#out.available.set(available);
});
}

Expand Down
100 changes: 100 additions & 0 deletions js/watch/src/support-probe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { type Effect, type Getter, Signal } from "@moq/signals";

/** Internal key used to describe which fields can change a support probe's result. */
export const supportCacheKey = Symbol("supportCacheKey");

/** A function that checks if a rendition configuration can be played. */
type Supported<Config extends { codec: string }> = ((config: Config) => Promise<boolean>) & {
[supportCacheKey]?: (config: Config) => string;
};

/**
* Probes which renditions a `supported` function accepts, shared by the audio and video sources.
*
* A result is cached per rendition until the rendition's config changes, keyed by the probe's
* `supportCacheKey` or else the whole config.
*/
export class SupportProbe<Config extends { codec: string }> {
readonly #kind: string;

readonly #pending = new Signal(false);
/** Whether rendition support is still being resolved. */
readonly pending: Getter<boolean> = this.#pending;

/** A `NotSupportedError` once no rendition of a non-empty catalog is supported. */
readonly error = new Signal<Error | undefined>(undefined);

#cache = new WeakMap<Supported<Config>, Map<string, { key: string; supported: boolean }>>();

constructor(kind: "audio" | "video") {
this.#kind = kind;
}

/**
* Probe `renditions` within `effect`, then `publish` the supported ones.
* Nothing is published when the effect is torn down first, since its rerun redoes this.
*/
run(
effect: Effect,
supported: Supported<Config> | undefined,
renditions: Record<string, Config>,
publish: (available: Record<string, Config>) => void,
): void {
this.error.set(undefined);
if (!supported) return;

let cache = this.#cache.get(supported);
if (!cache) {
cache = new Map();
this.#cache.set(supported, cache);
}
const names = new Set(Object.keys(renditions));
for (const name of cache.keys()) {
if (!names.has(name)) cache.delete(name);
}

const cacheKey = supported[supportCacheKey] ?? ((config: Config) => JSON.stringify(config));

effect.set(this.#pending, true, false);
effect.spawn(async () => {
const available: Record<string, Config> = {};

for (const [name, config] of Object.entries(renditions)) {
const key = cacheKey(config);
const cached = cache.get(name);
let isSupported = cached?.key === key ? cached.supported : undefined;
if (isSupported === undefined) {
try {
// `supported` comes from the consumer, so we cannot assume it ever settles. A
// rerun waits for the tasks it spawned, so an unraced probe would hold it shut.
const result = await effect.race(supported(config));
// Undefined when torn down; the abort check below handles it.
if (result !== undefined) {
isSupported = !!result;
// Recompute the key: a probe may rewrite the config it accepted.
cache.set(name, { key: cacheKey(config), supported: isSupported });
}
} catch (err) {
if (effect.abort.aborted) return;
console.warn(
`[Source] ${this.#kind} rendition ${name} (${config.codec}) support probe failed; treating as unsupported`,
err,
);
}
}

// Torn down: stop probing and publish nothing, since the rerun redoes this.
if (effect.abort.aborted) return;

if (isSupported) available[name] = config;
}

if (Object.keys(available).length === 0 && Object.keys(renditions).length > 0) {
console.warn(`[Source] no supported ${this.#kind} renditions found:`, renditions);
this.error.set(new DOMException(`no supported ${this.#kind} renditions`, "NotSupportedError"));
}
publish(available);
this.#pending.set(false);
});
}
}
63 changes: 63 additions & 0 deletions js/watch/src/ui/components/unsupported-indicator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { expect, test } from "bun:test";
import { yieldNow } from "@moq/clock";
import { Effect, Signal } from "@moq/signals";
import type MoqWatch from "../../element";
import { unsupportedIndicator } from "./unsupported-indicator";

const unsupported = () => new DOMException("no supported renditions", "NotSupportedError");

test("reports unsupported audio only when the broadcast has no video", async () => {
// The only DOM the component touches: a container holding a text span.
const container = { style: { display: "" }, setAttribute: () => {}, appendChild: () => {} };
const text = { textContent: "" };
const original = globalThis.document;
let created = 0;
globalThis.document = {
createElement: () => (created++ === 0 ? container : text),
} as unknown as Document;

const video = {
catalog: new Signal<{ renditions: Record<string, { codec: string }> } | undefined>(undefined),
error: new Signal<Error | undefined>(undefined),
};
const audio = {
catalog: new Signal({ renditions: { mic: { codec: "opus" } } }),
error: new Signal<Error | undefined>(undefined),
};
const watch = {
broadcast: { out: { status: new Signal("live") } },
video: { source: { out: video } },
audio: { source: { out: audio } },
} as unknown as MoqWatch;

const effect = new Effect();
try {
unsupportedIndicator(effect, watch);
await yieldNow();
expect(container.style.display).toBe("none");

audio.error.set(unsupported());
await yieldNow();
expect(container.style.display).toBe("");
expect(text.textContent).toBe("This audio codec is not supported by your browser: opus");

// A video section the reachability filter emptied is no video, so audio still reports.
video.catalog.set({ renditions: {} });
await yieldNow();
expect(container.style.display).toBe("");
expect(text.textContent).toBe("This audio codec is not supported by your browser: opus");

// Playable video carries the broadcast, so unsupported audio alone is not reported.
video.catalog.set({ renditions: { hd: { codec: "avc1.640028" } } });
await yieldNow();
expect(container.style.display).toBe("none");

video.error.set(unsupported());
await yieldNow();
expect(container.style.display).toBe("");
expect(text.textContent).toBe("This video codec is not supported by your browser: avc1.640028");
} finally {
effect.close();
globalThis.document = original;
}
});
Loading