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
4 changes: 2 additions & 2 deletions bun.lock

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

2 changes: 1 addition & 1 deletion js/hang/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"release": "bun ../common/release.ts"
},
"dependencies": {
"@kixelated/libavjs-webcodecs-polyfill": "^0.5.5",
"@kixelated/libavjs-webcodecs-polyfill": "^0.5.6",
"@moq/clock": "workspace:^",
"@moq/json": "workspace:^",
"@moq/libav": "workspace:^",
Expand Down
2 changes: 1 addition & 1 deletion js/hang/src/util/libav.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ test("the polyfill deinterleaves every channel to f32-planar", async () => {
}

const tail = new Float32Array(2);
audio.copyTo(tail, { planeIndex: 1, format: "f32-planar", frameOffset: 1 });
audio.copyTo(tail, { planeIndex: 1, format: "f32-planar", frameOffset: 1, frameCount: 2 });
expect(Array.from(tail)).toEqual([1, 2].map((frame) => data[frame * 2 + 1] / scale));
// A view smaller than the copy is refused, even with room in the buffer behind it.
const backing = new Float32Array(8);
Expand Down
74 changes: 1 addition & 73 deletions js/hang/src/util/libav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ export async function polyfill(): Promise<boolean> {
console.warn("using libav WebCodecs polyfill; performance may be degraded");

// @moq/libav is libav.js's opus-af variant plus the AAC decoder, which no published variant has.
// I forked libavjs-webcodecs-polyfill to avoid Typescript errors; there's no changes otherwise.
// I forked libavjs-webcodecs-polyfill to fix its TypeScript packaging and planar audio.
loading = Promise.all([import("@moq/libav"), import("@kixelated/libavjs-webcodecs-polyfill")]).then(
async ([{ default: LibAV }, libav]) => {
const native = globalThis.AudioDecoder;
await libav.load({ LibAV, polyfill: true });
fixPlanarCopy(libav.AudioData as unknown as typeof AudioData);
if (native) return true;

type Config = Parameters<InstanceType<typeof libav.AudioDecoder>["configure"]>[0];
Expand All @@ -28,14 +27,7 @@ export async function polyfill(): Promise<boolean> {
aac(config) ? { ...config, codec: { libavjs: { codec: "aac" } } } : config;

class AudioDecoder extends libav.AudioDecoder {
#planar = false;

constructor(init: ConstructorParameters<typeof libav.AudioDecoder>[0]) {
super({ ...init, output: (data) => init.output(this.#planar ? planar(data) : data) });
}

override configure(config: Config): void {
this.#planar = aac(config);
super.configure(route(config));
}

Expand All @@ -51,67 +43,3 @@ export async function polyfill(): Promise<boolean> {
}
return await loading;
}

// libav's AAC decoder always emits planar float, but the polyfill concatenates the planes and labels
// the result interleaved "f32". Relabel it so channels read back from the right plane.
function planar<T extends AudioData>(data: T): T {
if (data.format !== "f32") return data;
const samples = new Float32Array(data.numberOfFrames * data.numberOfChannels);
data.copyTo(samples, { planeIndex: 0 });
const relabeled = new (data.constructor as typeof AudioData)({
format: "f32-planar",
sampleRate: data.sampleRate,
numberOfFrames: data.numberOfFrames,
numberOfChannels: data.numberOfChannels,
timestamp: data.timestamp,
data: samples,
});
data.close();
return relabeled as T;
}

// Each interleaved sample format, with the offset and scale that map it to [-1, 1].
const INTERLEAVED = {
u8: [Uint8Array, 0x80, 0x80],
s16: [Int16Array, 0, 0x8000],
s32: [Int32Array, 0, 0x80000000],
f32: [Float32Array, 0, 1],
} as const;

// The polyfill converts interleaved samples to "f32-planar" from the wrong offset, so every channel
// after the first reads past its frames (NaN) instead of its own samples. libopus decodes
// interleaved, so stereo Opus lost its right channel. Deinterleave from a same-format copy instead,
// which the polyfill gets right.
function fixPlanarCopy(Data: typeof AudioData): void {
const copyTo = Data.prototype.copyTo;
Data.prototype.copyTo = function (this: AudioData, destination: AllowSharedBufferSource, options) {
const format = this.format ?? "";
if (options.format !== "f32-planar" || !(format in INTERLEAVED)) return copyTo.call(this, destination, options);
const [View, offset, scale] = INTERLEAVED[format as keyof typeof INTERLEAVED];
const channels = this.numberOfChannels;
const index = (value: number) => Number.isSafeInteger(value) && value >= 0;
if (!index(options.planeIndex) || options.planeIndex >= channels) {
throw new RangeError(`no plane ${options.planeIndex}`);
}

const start = options.frameOffset ?? 0;
const count = options.frameCount ?? this.numberOfFrames - start;
if (!index(start) || !index(count) || start >= this.numberOfFrames || start + count > this.numberOfFrames) {
throw new RangeError("frames out of range");
}
// Bounded by the caller's view, not the buffer behind it.
if (count * Float32Array.BYTES_PER_ELEMENT > destination.byteLength) {
throw new RangeError("destination too small");
}
const samples = new View(this.numberOfFrames * channels);
copyTo.call(this, samples, { planeIndex: 0 });
const out = new Float32Array(count);
for (let i = 0; i < count; i++)
out[i] = (samples[(start + i) * channels + options.planeIndex] - offset) / scale;
// Copied as bytes: a destination need not be 4-byte aligned.
const bytes = ArrayBuffer.isView(destination)
? new Uint8Array(destination.buffer, destination.byteOffset, out.byteLength)
: new Uint8Array(destination, 0, out.byteLength);
bytes.set(new Uint8Array(out.buffer));
};
}
1 change: 0 additions & 1 deletion quest/m1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ tracks, so per-group stats stop costing cluster announces.
- [JSON encoder options without a downcast](/quest/m1/json-encoder-options.md) - moq-mux's `json_snapshot` takes its `deltas` switch through a typed path any custom catalog entry can use, not an `Any` downcast
- [Tooling tests gate every merge](/quest/m1/gh-test-gate.md) - every queue Check runs `just gh test`, so a change to the CI selector or release tooling cannot merge without its own tests
- [CI housekeeping](/quest/m1/ci-housekeeping.md) - validate queue nextest at 24 threads and remove the actionlint queue ignore after its release
- [libav polyfill fork fix](/quest/m1/libav-polyfill-fork.md) - our polyfill fork copies planar audio correctly and `@moq/hang` drops its patch
- [Interop AAC](/quest/m1/interop-aac.md) - the Rust interop publisher adds an AAC tone and every subscriber checks it beside Opus
- [Interop pre-skip](/quest/m1/interop-preskip.md) - the interop tone check anchors phase to timestamps, so a misapplied pre-skip fails
- [moq-net internals](/quest/m1/net-internals.md) - no per-group session clones, no transport type parameter in lite and IETF internals, and the `Local` check measured
Expand Down
19 changes: 0 additions & 19 deletions quest/m1/libav-polyfill-fork.md

This file was deleted.

Loading