8000 fix(codec): stream all streamable Blobs, not just files ≥500 MB by johannboehme · Pull Request #51 · johannboehme/tk-1 · GitHub
[go: up one dir, main page]

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
5 changes: 4 additions & 1 deletion frontend/src/local/codec/codec-resolver.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ async function opfsFixture(name: string): Promise<File> {
}

describe("codec resolver: decodeAudioToMonoPcm (real Chromium)", () => {
it("default path picks WebCodecs (decodeAudioData) for a standard MP4", async () => {
it("default path decodes a standard MP4 Blob via WebCodecs (now the streaming sub-decoder)", async () => {
// A streamable Blob auto-routes to the streaming decoder regardless of
// size; both streaming and the whole-file path report backend "webcodecs",
// so we only assert the decode succeeded with the right shape.
const blob = await fetchFixture();
const result = await decodeAudioToMonoPcm(blob, 22050);
expect(result.backend).toBe("webcodecs");
Expand Down
67 changes: 46 additions & 21 deletions frontend/src/local/codec/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
* operation. Callers (sync, render) don't know whether WebCodecs or
* ffmpeg.wasm did the work — they only see PCM / chunks coming out.
*
* Routing strategy:
* 1. **Big files (≥ STREAMING_THRESHOLD)**: route to the streaming
* decoder for the detected container (mp4 / mp3 / wav). The
* whole-file paths (`decodeAudioData`, ffmpeg.wasm MEMFS) blow up
* past Chromium's ~2 GiB ArrayBuffer cap; streaming reads in 4 MiB
* batches and feeds WebCodecs `AudioDecoder` chunks at a time.
* 2. **Small files**: keep the existing behaviour — try WebCodecs
* `decodeAudioData` first, fall back to ffmpeg.wasm on failure.
* Routing strategy (capability-based, not size-based):
* 1. **Streamable Blobs** (source is a `Blob`/`File` that sniffs as
* mp4 / mp3 / wav): route to the streaming decoder for that
* container — regardless of file size. The whole-file paths
* (`decodeAudioData`, ffmpeg.wasm MEMFS) decode into a single
* ArrayBuffer and blow up on long recordings: a 45 MB MP3 expands to
* ~1 GB of PCM and overflows Chromium's allocator long before the
* ~2 GiB ArrayBuffer cap. Streaming reads in 4 MiB batches and feeds
* WebCodecs `AudioDecoder` chunks at a time, so memory stays bounded.
* If streaming fails on a small file, we fall back to the whole-file
* path below.
* 2. **Everything else** (non-streamable containers like ogg/flac/opus,
* or `ArrayBuffer` sources that can't be sliced incrementally): try
* WebCodecs `decodeAudioData` first, fall back to ffmpeg.wasm on
* failure.
*
* The result reports which backend won (`.backend`) so the UI can show it
* to the user — that's the "Mechanismus-Indikator" requirement from the
Expand All @@ -23,12 +30,12 @@ import {
} from "./webcodecs/audio-decode";
import { sniffContainer } from "./streaming/sniff-container";

/** Files at or above this size go through the streaming decoder path.
* Files below it stay on the existing whole-file fast path. The
* threshold is well below Chromium's ArrayBuffer cap (~2 GiB) so we
* never let the fast path try a file it cannot handle. 500 MiB also
* exercises the streaming path in normal-usage testing — not just
* pathological huge files. */
/** Fallback ceiling for streamable files. Streamable Blobs always try
* the streaming decoder first (see routing strategy above); if streaming
* *fails*, a file below this size falls back to the whole-file path,
* while a file at or above it surfaces the streaming error directly — a
* whole-file retry on something this large would only OOM. Well below
* Chromium's ArrayBuffer cap (~2 GiB). */
const STREAMING_THRESHOLD = 500 * 1024 * 1024;

let ffmpegAudioDecodeImpl:
Expand Down Expand Up @@ -83,14 +90,26 @@ export async function decodeAudioToMonoPcm(
return runStreamingDecode(source, targetSampleRate, opts.onProgress);
}

// Auto-route by file size.
const isBlob = source instanceof Blob;
const tooBigForFastPath = isBlob && source.size >= STREAMING_THRESHOLD;
if (tooBigForFastPath) {
return runStreamingDecode(source, targetSampleRate, opts.onProgress);
// Auto-route. Prefer the streaming decoder whenever the source is a Blob
// whose container we can stream (mp3 / mp4 / wav) — independent of size.
// The whole-file paths choke on long recordings, so streaming is the safe
// default; non-streamable containers and ArrayBuffer sources drop through
// to the whole-file path below.
let streamingErr: unknown;
if (source instanceof Blob && (await sniffContainer(source)) !== null) {
try {
return await runStreamingDecode(source, targetSampleRate, opts.onProgress);
} catch (err) {
// Above the fallback ceiling a whole-file retry would only OOM, so
// surface the streaming failure directly.
if (source.size >= STREAMING_THRESHOLD) throw err;
streamingErr = err;
}
}

// Small file: try WebCodecs / decodeAudioData first, fall back on failure.
// Whole-file path: ArrayBuffer source, non-streamable container, or a small
// streamable file whose streaming decode failed. Try WebCodecs /
// decodeAudioData first, fall back to ffmpeg.wasm on failure.
opts.onProgress?.(0);
let webcodecsErr: unknown;
try {
Expand Down Expand Up @@ -121,7 +140,13 @@ export async function decodeAudioToMonoPcm(
? webcodecsErr.message
: String(webcodecsErr);
const ffMsg = ffErr instanceof Error ? ffErr.message : String(ffErr);
throw new Error(`Audio decode failed (ffmpeg: ${ffMsg}; webcodecs: ${webMsg})`);
const streamMsg =
streamingErr !== undefined
? `; streaming: ${streamingErr instanceof Error ? streamingErr.message : String(streamingErr)}`
: "";
throw new Error(
`Audio decode failed (ffmpeg: ${ffMsg}; webcodecs: ${webMsg}${streamMsg})`,
);
}
}

Expand Down
31 changes: 31 additions & 0 deletions frontend/src/local/codec/streaming-routing.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,35 @@ describe("codec resolver: streaming-routing", () => {
decodeAudioToMonoPcm(blob, 22050, { forceBackend: "streaming" }),
).rejects.toThrow(/streamable/i);
});

it("auto-routes a small MP3 Blob through the streaming path (sub-progress emitted)", async () => {
// Regression for the [decoding-studio-audio] OOM: a long (~45 MB) MP3
// expands to ~1 GB of PCM and blows up the whole-file decodeAudioData /
// ffmpeg paths. Streamable Blobs must take the memory-safe streaming path
// regardless of file size — not only when they cross the old 500 MB gate.
// We can't tell streaming from whole-file by `backend` (both report
// "webcodecs"), so we use the progress signature instead: streaming emits
// a fraction after each read batch (0 < f < 1); the whole-file path only
// ever emits 0 then 1.
if (typeof AudioDecoder === "undefined") return;
const blob = await fetchBlob(MP3_FIXTURE);
const fracs: number[] = [];
const result = await decodeAudioToMonoPcm(blob, 22050, {
onProgress: (f) => fracs.push(f),
});
expect(result.pcm.length).toBeGreaterThan(0);
expect(fracs.some((f) => f > 0 && f < 1)).toBe(true);
});

it("auto-routes an ArrayBuffer source through the whole-file path (no sub-progress)", async () => {
// ArrayBuffer sources can't be streamed (no incremental slicing), so they
// must fall through to decodeAudioData -> ffmpeg, which reports only 0/1.
const ab = await (await fetchBlob(MP4_FIXTURE)).arrayBuffer();
const fracs: number[] = [];
const result = await decodeAudioToMonoPcm(ab, 22050, {
onProgress: (f) => fracs.push(f),
});
expect(result.pcm.length).toBeGreaterThan(0);
expect(fracs.some((f) => f > 0 && f < 1)).toBe(false);
});
});
Loading
0