libav-wasm 0.8.4 → 0.8.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -54,6 +54,22 @@ const seeked = await remuxer.seek(90)
54
54
  await remuxer.destroy()
55
55
  ```
56
56
 
57
+ ### `read` gives up the buffer it returns
58
+
59
+ The buffer `read` resolves with is **transferred** to the worker, not copied, so it is detached in your
60
+ code once you hand it over. Returning a fresh buffer per read is the normal shape and needs no thought: a
61
+ `fetch(...).arrayBuffer()`, a buffer transferred out of another worker, or a `slice` all qualify. Only a
62
+ consumer answering out of a cache it keeps has to do anything, and what it has to do is return a copy.
63
+
64
+ Copying the whole payload across the worker boundary on every read is not cheap: at the default 2.5 MB
65
+ buffer a session moves close to a gigabyte, and transferring instead measured 15% off a full remux
66
+ (2264.8 ms to 1918.3 ms, 382 reads, medians over 7 alternated rounds). Pass `transferReads: false` to go
67
+ back to copying.
68
+
69
+ Handing back bytes you kept detaches them, and a detached buffer reads as zero bytes, which ffmpeg takes
70
+ for EOF: the file would truncate silently at that offset. The first time a read comes back empty before
71
+ the end of the file, the library says so on the console.
72
+
57
73
  `makeThumbnailer` opens the same file with no muxer, no encoder and no stream map, for `readKeyframe(t)`
58
74
  alone. Use it rather than a remuxer: `readKeyframe` seeks backward on the input, which an output muxer
59
75
  cannot follow, and a thumbnailer has no muxer to damage.
package/build/index.d.ts CHANGED
@@ -11,6 +11,15 @@ export type MakeTransmuxerOptions = {
11
11
  bufferSize?: number;
12
12
  /** Input stream index of the audio track to mux; defaults to the first audio stream */
13
13
  audioStreamIndex?: number;
14
+ /**
15
+ * Set false to copy each read across to the worker instead of handing it over. Defaults to true.
16
+ *
17
+ * `read` gives up ownership of the buffer it returns: it is DETACHED once handed over, so a consumer
18
+ * answering out of its own cache has to return a copy rather than the bytes it keeps. That is the
19
+ * normal shape already (a fetch, a worker transfer, a slice all produce a fresh buffer), and moving
20
+ * rather than cloning measured 15% off a session, since the whole payload crosses the hop every read.
21
+ */
22
+ transferReads?: boolean;
14
23
  };
15
24
  export declare const makeRemuxer: (options: MakeTransmuxerOptions) => Promise<{
16
25
  worker: Worker;
package/build/index.js CHANGED
@@ -2405,7 +2405,7 @@ var abortSignalToPromise = (abortSignal) => new Promise((resolve, reject) => {
2405
2405
  * Split out because a thumbnailer and a remuxer need identical plumbing and nothing else in common: only
2406
2406
  * which wasm entry point they open the file with, and which calls they then expose.
2407
2407
  */
2408
- var makeSession = async ({ publicPath, workerUrl, workerOptions, read, length, bufferSize, audioStreamIndex }) => {
2408
+ var makeSession = async ({ publicPath, workerUrl, workerOptions, read, length, bufferSize, audioStreamIndex, transferReads = true }) => {
2409
2409
  const worker = new Worker(workerUrl, workerOptions);
2410
2410
  const { makeRemuxer } = await Lr({}, { transport: worker });
2411
2411
  const remuxer = await makeRemuxer({
@@ -2415,15 +2415,33 @@ var makeSession = async ({ publicPath, workerUrl, workerOptions, read, length, b
2415
2415
  audioStreamIndex
2416
2416
  });
2417
2417
  const queue = new PQueue({ concurrency: 1 });
2418
+ /**
2419
+ * The one way transferring can go wrong, said out loud instead of left to be debugged.
2420
+ *
2421
+ * A consumer that answers out of its own cache hands back a buffer we detached on an earlier read, and
2422
+ * a detached buffer reports zero bytes. ffmpeg reads zero bytes as EOF, so the file silently truncates
2423
+ * at whatever offset that happened rather than failing anywhere near the cause. Zero bytes at or past
2424
+ * the end of the file is ordinary EOF and says nothing, so only a short read BEFORE the end is a tell.
2425
+ */
2426
+ let warnedDetached = false;
2427
+ const warnIfDetached = (buffer, offset) => {
2428
+ if (warnedDetached || !transferReads || buffer.byteLength > 0 || offset >= length) return;
2429
+ warnedDetached = true;
2430
+ console.warn(`libav-wasm: read(${offset}) returned 0 bytes, ${length - offset} bytes before the end of the file. Reads are transferred, so the buffer read() returns is detached in the caller and handing back bytes you keep returns that same detached buffer. Return a copy, or pass transferReads: false.`);
2431
+ };
2418
2432
  const wasmRead = (abortController) => (offset, size) => {
2419
2433
  if (abortController.signal.aborted) return Promise.resolve({
2420
2434
  resolved: (/* @__PURE__ */ new Uint8Array(0)).buffer,
2421
2435
  rejected: true
2422
2436
  });
2423
- return Promise.race([read(Number(offset), Number(size)).then((buffer) => ({
2424
- resolved: new Uint8Array(buffer).buffer,
2425
- rejected: false
2426
- }), () => ({
2437
+ return Promise.race([read(Number(offset), Number(size)).then((buffer) => {
2438
+ warnIfDetached(buffer, Number(offset));
2439
+ const owned = new Uint8Array(buffer).buffer;
2440
+ return {
2441
+ resolved: transferReads ? Je(owned) : owned,
2442
+ rejected: false
2443
+ };
2444
+ }, () => ({
2427
2445
  resolved: (/* @__PURE__ */ new Uint8Array(0)).buffer,
2428
2446
  rejected: true
2429
2447
  })), abortSignalToPromise(abortController.signal).then(() => ({
Binary file
package/build/libav.wasm CHANGED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libav-wasm",
3
- "version": "0.8.4",
3
+ "version": "0.8.6",
4
4
  "main": "build/index.js",
5
5
  "files": [
6
6
  "build"
@@ -19,6 +19,7 @@
19
19
  "vite-build-dev": "vp build --watch",
20
20
  "worker-build-dev": "nodemon -e ts --watch src/worker --exec \"npm run build-worker\"",
21
21
  "build": "npm run make-docker && vp build && npm run build-worker && npm run types && npm run copy-wasm",
22
+ "prepublishOnly": "npm run build",
22
23
  "types": "tsc",
23
24
  "copy-wasm": "copyfiles -u 1 dist/libav.wasm dist/libav-jspi.wasm build && shx mv ./build/worker/index.d.ts ./build/worker.d.ts && shx rm -rf ./build/worker",
24
25
  "preview": "vp preview",