@banou/media-player 0.10.31 → 0.10.32
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 +3 -0
- package/dist/engine/index.js +1 -1
- package/dist/engine/playback.d.ts +4 -3
- package/dist/engine/source-buffer.d.ts +0 -1
- package/dist/{engine-5XbZqrD3.js → engine-BZDbaSfM.js} +66 -67
- package/dist/index.js +14 -14
- package/dist/volume-normalizer-worklet.js +40 -26
- package/package.json +2 -2
- package/src/lib/audio/loudness-analysis.ts +3 -3
- package/src/lib/audio/playback-tap.ts +3 -3
- package/src/lib/audio/volume-normalizer-worklet.d.ts +1 -1
- package/src/lib/audio/volume-normalizer-worklet.js +40 -26
- package/src/lib/engine/playback.ts +15 -17
- package/src/lib/engine/source-buffer.ts +2 -6
package/README.md
CHANGED
|
@@ -94,6 +94,9 @@ reads as a browser bug rather than a missing asset. Serve both.
|
|
|
94
94
|
|
|
95
95
|
`libavWorkerUrl` (`libav-wasm/build/worker.js`) is named individually and still copies fine.
|
|
96
96
|
|
|
97
|
+
Copy all three from the libav-wasm this package depends on. A worker from before libav-wasm 0.9.0 writes another
|
|
98
|
+
timeline and reports no `timestampOffset`, so playback fails at load.
|
|
99
|
+
|
|
97
100
|
`cjkFontUrl` is this package's own `noto-sans-jp.otf` (`import cjkFontUrl from
|
|
98
101
|
'@banou/media-player/noto-sans-jp.otf?no-inline&url'`), Noto Sans JP cut to the JIS X 0208 kanji. jassub's libass has no
|
|
99
102
|
system font fallback, so without it Japanese in a style whose font has no Japanese, which is how releases set song
|
package/dist/engine/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as e, c as t, d as n, f as r, i, l as a, n as o, o as s, r as c, s as l, t as u, u as d } from "../engine-
|
|
1
|
+
import { a as e, c as t, d as n, f as r, i, l as a, n as o, o as s, r as c, s as l, t as u, u as d } from "../engine-BZDbaSfM.js";
|
|
2
2
|
export { i as DEFAULT_BUFFER_SIZE, e as MediaElementError, a as SUBTITLES_OFF, u as createPictureInPicture, d as createSubtitleRenderer, c as createThumbnailGenerator, n as getTimeRanges, s as isMediaElementError, o as pictureInPictureMode, l as startPlayback, t as terminateRemuxer, r as updateSourceBuffer };
|
|
@@ -23,9 +23,9 @@ export type MediaChapter = {
|
|
|
23
23
|
};
|
|
24
24
|
/**
|
|
25
25
|
* What the remuxer produced, in the order it was appended: the init segment and the input audio stream it carries,
|
|
26
|
-
* a run at load and at every remuxer seek (`pts` is the timestampOffset its fragments are appended at
|
|
27
|
-
* fragment, and the end of the file once read. The buffers
|
|
28
|
-
* consumer copies what it keeps.
|
|
26
|
+
* a run at load and at every remuxer seek (`pts` is the timestampOffset its fragments are appended at, the same for
|
|
27
|
+
* every run, and `from` the time the run starts at), each fragment, and the end of the file once read. The buffers
|
|
28
|
+
* are playback's own and may be appended again, so a consumer copies what it keeps.
|
|
29
29
|
*/
|
|
30
30
|
export type PlaybackOutput = {
|
|
31
31
|
type: 'init';
|
|
@@ -35,6 +35,7 @@ export type PlaybackOutput = {
|
|
|
35
35
|
} | {
|
|
36
36
|
type: 'run';
|
|
37
37
|
pts: number;
|
|
38
|
+
from: number;
|
|
38
39
|
} | {
|
|
39
40
|
type: 'fragment';
|
|
40
41
|
data: ArrayBuffer;
|
|
@@ -7,6 +7,5 @@ export declare const getTimeRanges: (sourceBuffer: SourceBuffer) => TimeRange[];
|
|
|
7
7
|
export declare const updateSourceBuffer: (sourceBuffer: SourceBuffer, mediaSource: MediaSource) => {
|
|
8
8
|
appendBuffer: (buffer: ArrayBuffer | Uint8Array) => Promise<void>;
|
|
9
9
|
unbufferRange: (start: number, end: number) => Promise<void>;
|
|
10
|
-
updateTimestampOffset: (timestampOffset: number) => Promise<void>;
|
|
11
10
|
endOfStream: (allowed?: () => boolean) => Promise<void>;
|
|
12
11
|
};
|
|
@@ -74,9 +74,6 @@ var i = {
|
|
|
74
74
|
return {
|
|
75
75
|
appendBuffer: (t) => r(() => (e.appendBuffer(t), c(e))),
|
|
76
76
|
unbufferRange: (t, n) => r(() => t >= n ? Promise.resolve() : (e.remove(t, n), c(e))),
|
|
77
|
-
updateTimestampOffset: (t) => r(async () => {
|
|
78
|
-
e.timestampOffset = t;
|
|
79
|
-
}),
|
|
80
77
|
endOfStream: (n = () => !0) => r(async () => {
|
|
81
78
|
if (t.readyState === "open" && !e.updating && n()) try {
|
|
82
79
|
t.endOfStream();
|
|
@@ -285,7 +282,7 @@ var i = {
|
|
|
285
282
|
}, a = () => {
|
|
286
283
|
try {
|
|
287
284
|
let n = j.addSourceBuffer(g);
|
|
288
|
-
n.mode = "segments";
|
|
285
|
+
n.mode = "segments", n.timestampOffset = e.timestampOffset;
|
|
289
286
|
let r = e.info.input.duration;
|
|
290
287
|
Number.isFinite(r) && r > 0 && (j.duration = r), i(), t(n);
|
|
291
288
|
} catch (e) {
|
|
@@ -295,7 +292,7 @@ var i = {
|
|
|
295
292
|
i(), n(o("The MediaSource closed before a SourceBuffer could be created"));
|
|
296
293
|
};
|
|
297
294
|
j.addEventListener("sourceopen", a, { once: !0 }), j.addEventListener("sourceclose", s, { once: !0 }), j.addEventListener("error", s, { once: !0 });
|
|
298
|
-
}), { appendBuffer: R, unbufferRange: de,
|
|
295
|
+
}), { appendBuffer: R, unbufferRange: de, endOfStream: fe } = l(L, j);
|
|
299
296
|
await R(e.data), N?.({
|
|
300
297
|
type: "init",
|
|
301
298
|
data: e.data,
|
|
@@ -303,65 +300,67 @@ var i = {
|
|
|
303
300
|
audioStreamIndex: a
|
|
304
301
|
}), N?.({
|
|
305
302
|
type: "run",
|
|
306
|
-
pts:
|
|
303
|
+
pts: e.timestampOffset,
|
|
304
|
+
from: 0
|
|
307
305
|
}), e.subtitles?.length && c.pushFragments(e.subtitles), x?.();
|
|
308
|
-
let z = !1, B = !1, V = 0, H,
|
|
306
|
+
let z = !1, B = !1, V = 0, H, pe = () => H ?? n.currentTime, U = !1, me = !1, he = 0, W = 0, G = null, K = 0, q = 0, J = !1, ge = 0, Y = !0, X = (e, t, n = !1) => {
|
|
309
307
|
let r = e?.message === "Cancelled";
|
|
310
308
|
t && r || (console.error(e), (!J || n) && (J = !0, re?.(r ? o("Reading the video file failed", void 0, { cause: e }) : e)));
|
|
311
|
-
},
|
|
309
|
+
}, _e = async (e = !1) => {
|
|
312
310
|
if (V > 0 && !e) return;
|
|
313
311
|
let t = n.currentTime, r = e ? T : S, i = e ? E : C, a = s(L), o = (e, t) => (Y = !1, de(e, t));
|
|
314
312
|
a.length && a[0].start < t + r && await o(0, t + r);
|
|
315
313
|
for (let { end: e } of a) e > t + i && await o(t + i, e);
|
|
316
|
-
},
|
|
314
|
+
}, ve = () => {
|
|
317
315
|
let e = s(L);
|
|
318
316
|
if (!e.length) return !0;
|
|
319
|
-
let t =
|
|
320
|
-
return
|
|
321
|
-
},
|
|
322
|
-
let t =
|
|
317
|
+
let t = pe(), n = e.find((e) => e.start <= t + k && t < e.end);
|
|
318
|
+
return Ce() && xe(t) && (!n || n.start <= t) || n && n.end >= t + C ? !1 : !n || n.end < t + w;
|
|
319
|
+
}, ye = (e) => s(L).find((t) => t.start <= e + k && e < t.end), be = () => ye(n.currentTime), xe = (e = n.currentTime) => {
|
|
320
|
+
let t = ye(e);
|
|
323
321
|
return e >= q - .1 || !!t && t.end >= q - .1;
|
|
324
|
-
},
|
|
322
|
+
}, Se = () => {
|
|
325
323
|
let e = s(L).map((e) => e.end);
|
|
326
|
-
e.length && (q = Math.max(q, ...e)), U && (
|
|
327
|
-
},
|
|
328
|
-
B = !0, G = null, K = 0,
|
|
329
|
-
let
|
|
324
|
+
e.length && (q = Math.max(q, ...e)), U && (me = !0);
|
|
325
|
+
}, Ce = () => U || me && (s(L).at(-1)?.end ?? 0) >= q - .1, we = async (t) => {
|
|
326
|
+
B = !0, G = null, K = 0, ge = t, Y = !0;
|
|
327
|
+
let n = ++W;
|
|
330
328
|
try {
|
|
331
|
-
let { data:
|
|
332
|
-
if (I ||
|
|
333
|
-
N?.({
|
|
329
|
+
let { data: r, pts: i, subtitles: a, finished: o } = await P.seek(t);
|
|
330
|
+
if (I || n !== W) return;
|
|
331
|
+
o && (U = !0), a?.length && c.pushFragments(a), N?.({
|
|
334
332
|
type: "run",
|
|
335
|
-
pts:
|
|
336
|
-
|
|
333
|
+
pts: e.timestampOffset,
|
|
334
|
+
from: i
|
|
335
|
+
}), r.byteLength && N?.({
|
|
337
336
|
type: "fragment",
|
|
338
|
-
data:
|
|
339
|
-
}),
|
|
337
|
+
data: r
|
|
338
|
+
}), o && N?.({ type: "end" }), r.byteLength && (await R(r), Se());
|
|
340
339
|
} catch (e) {
|
|
341
|
-
|
|
340
|
+
n === W && (Y = !1), X(e, I || n !== W);
|
|
342
341
|
} finally {
|
|
343
|
-
|
|
342
|
+
n === W && (B = !1);
|
|
344
343
|
}
|
|
345
|
-
},
|
|
344
|
+
}, Te = async () => {
|
|
346
345
|
let e = G;
|
|
347
346
|
if (e) try {
|
|
348
|
-
await R(e),
|
|
347
|
+
await R(e), Se(), G === e && (G = null, K = 0);
|
|
349
348
|
} catch (t) {
|
|
350
349
|
if (G !== e) return;
|
|
351
350
|
if (t?.name === "QuotaExceededError") {
|
|
352
|
-
let t = await
|
|
351
|
+
let t = await _e(!0).then(() => !0, () => !1);
|
|
353
352
|
if (G !== e) return;
|
|
354
353
|
if (t) {
|
|
355
|
-
U = !1, await
|
|
354
|
+
U = !1, await we(be()?.end ?? n.currentTime);
|
|
356
355
|
return;
|
|
357
356
|
}
|
|
358
357
|
}
|
|
359
358
|
if (K += 1, K < D) return;
|
|
360
359
|
throw G = null, t;
|
|
361
360
|
}
|
|
362
|
-
},
|
|
363
|
-
if (z || B || $ !== void 0 || I || !G && (U || !
|
|
364
|
-
let e = ++
|
|
361
|
+
}, Ee = async () => {
|
|
362
|
+
if (z || B || $ !== void 0 || I || !G && (U || !ve())) return;
|
|
363
|
+
let e = ++he, t = W, n = () => I || e !== he || t !== W;
|
|
365
364
|
z = !0;
|
|
366
365
|
try {
|
|
367
366
|
if (!G) {
|
|
@@ -372,78 +371,78 @@ var i = {
|
|
|
372
371
|
}), r && N?.({ type: "end" }), !e.byteLength)) return;
|
|
373
372
|
G = e, K = 0;
|
|
374
373
|
}
|
|
375
|
-
await
|
|
374
|
+
await Te(), !n() && J && (J = !1, ie?.());
|
|
376
375
|
} catch (e) {
|
|
377
376
|
X(e, n());
|
|
378
377
|
} finally {
|
|
379
|
-
e ===
|
|
378
|
+
e === he && (z = !1);
|
|
380
379
|
}
|
|
381
|
-
},
|
|
382
|
-
let t =
|
|
380
|
+
}, De = (e) => s(L).some((t) => t.start <= e && e < t.end && t.end >= ge) && ke(e), Oe = (e) => s(L).some((t) => t.start <= e && e < t.end), Z = (e) => {
|
|
381
|
+
let t = ye(e);
|
|
383
382
|
return t ? t.end - e : 0;
|
|
384
|
-
},
|
|
385
|
-
if (!I && !(
|
|
383
|
+
}, ke = (e) => Z(e) >= A || Ce() && xe(e), Ae = async (e) => {
|
|
384
|
+
if (!I && !(Oe(e) && ke(e))) {
|
|
386
385
|
$ && clearTimeout($), $ = void 0, U = !1, V++, H = e;
|
|
387
386
|
try {
|
|
388
|
-
await
|
|
387
|
+
await we(e);
|
|
389
388
|
let t = W;
|
|
390
389
|
for (let n = 0; n < ee && !(I || t !== W || U || Z(e) >= A); n++) {
|
|
391
390
|
let t = Z(e);
|
|
392
|
-
if (await
|
|
391
|
+
if (await Ee(), Z(e) <= t) break;
|
|
393
392
|
}
|
|
394
393
|
} finally {
|
|
395
394
|
V--, H === e && (H = void 0);
|
|
396
395
|
}
|
|
397
396
|
}
|
|
398
|
-
},
|
|
399
|
-
let t = performance.now(), n = t -
|
|
397
|
+
}, je = 0, Me = null, Q, Ne = (e) => {
|
|
398
|
+
let t = performance.now(), n = t - je;
|
|
400
399
|
if (n >= te) {
|
|
401
|
-
|
|
400
|
+
je = t, Me = null, ae?.(e);
|
|
402
401
|
return;
|
|
403
402
|
}
|
|
404
|
-
|
|
403
|
+
Me = e, !Q && (Q = setTimeout(() => {
|
|
405
404
|
Q = void 0;
|
|
406
|
-
let e =
|
|
407
|
-
|
|
405
|
+
let e = Me;
|
|
406
|
+
Me = null, !(e === null || I) && (je = performance.now(), ae?.(e));
|
|
408
407
|
}, te - n));
|
|
409
408
|
};
|
|
410
409
|
F.push(() => {
|
|
411
410
|
Q && clearTimeout(Q);
|
|
412
411
|
});
|
|
413
|
-
let
|
|
412
|
+
let Pe = 0, $;
|
|
414
413
|
F.push(() => {
|
|
415
414
|
$ && clearTimeout($);
|
|
416
415
|
});
|
|
417
|
-
let
|
|
416
|
+
let Fe = () => {
|
|
418
417
|
let t = n.currentTime, r = e.info.input.duration || n.duration;
|
|
419
|
-
r > 0 &&
|
|
420
|
-
let i = performance.now(), a = i -
|
|
421
|
-
if (
|
|
418
|
+
r > 0 && Ne(Math.min(Math.max(t / r, 0), 1));
|
|
419
|
+
let i = performance.now(), a = i - Pe < ne;
|
|
420
|
+
if (Pe = i, !De(t) && !(Y && (B || Z(t) > 0) && Math.abs(t - ge) < .001)) {
|
|
422
421
|
if (U = !1, $ && clearTimeout($), !a) {
|
|
423
|
-
$ = void 0,
|
|
422
|
+
$ = void 0, we(t);
|
|
424
423
|
return;
|
|
425
424
|
}
|
|
426
425
|
$ = setTimeout(() => {
|
|
427
426
|
if ($ = void 0, I) return;
|
|
428
427
|
let e = n.currentTime;
|
|
429
|
-
|
|
428
|
+
De(e) || (U = !1, we(e));
|
|
430
429
|
}, ne);
|
|
431
430
|
}
|
|
432
431
|
};
|
|
433
|
-
n.addEventListener("seeking",
|
|
434
|
-
let
|
|
435
|
-
if (
|
|
436
|
-
|
|
432
|
+
n.addEventListener("seeking", Fe), F.push(() => n.removeEventListener("seeking", Fe));
|
|
433
|
+
let Ie = !1, Le = () => {
|
|
434
|
+
if (Ie) return;
|
|
435
|
+
Ie = !0;
|
|
437
436
|
let e = n.error;
|
|
438
437
|
X(new b(`the media element failed: ${e?.message ?? "unknown"}`, { cause: e }), !1, !0);
|
|
439
438
|
};
|
|
440
|
-
n.addEventListener("error",
|
|
441
|
-
let
|
|
442
|
-
|
|
439
|
+
n.addEventListener("error", Le), F.push(() => n.removeEventListener("error", Le));
|
|
440
|
+
let Re = setInterval(() => {
|
|
441
|
+
Ie || n.error || (_e().catch(() => {}), Ee().catch((e) => X(e, !1)), Ce() && !G && xe() && j.readyState === "open" && fe(() => !B).catch(() => {}));
|
|
443
442
|
}, 100);
|
|
444
|
-
return F.push(() => clearInterval(
|
|
443
|
+
return F.push(() => clearInterval(Re)), {
|
|
445
444
|
destroy: le,
|
|
446
|
-
prepareSeek:
|
|
445
|
+
prepareSeek: Ae,
|
|
447
446
|
videoElement: n,
|
|
448
447
|
selectSubtitleStream: (e) => c.selectStream(e),
|
|
449
448
|
indexes: e.indexes ?? [],
|
|
@@ -584,8 +583,8 @@ var i = {
|
|
|
584
583
|
} catch (e) {
|
|
585
584
|
throw M(c), e;
|
|
586
585
|
}
|
|
587
|
-
}, ue = 1280, L = 1920, R = 250, de = 100, fe = () => typeof HTMLCanvasElement < "u" && "captureStream" in HTMLCanvasElement.prototype,
|
|
588
|
-
let { video: t, subtitles: n } = e, r = e.mode ??
|
|
586
|
+
}, ue = 1280, L = 1920, R = 250, de = 100, fe = () => typeof HTMLCanvasElement < "u" && "captureStream" in HTMLCanvasElement.prototype, z = () => typeof document < "u" && typeof HTMLVideoElement < "u" && typeof HTMLVideoElement.prototype.requestPictureInPicture == "function" && !!document.pictureInPictureEnabled, B = () => typeof HTMLVideoElement < "u" && "mozCaptureStream" in HTMLVideoElement.prototype, V = () => fe() ? z() ? "window" : B() ? "burn-in" : null : null, H = (e) => {
|
|
587
|
+
let { video: t, subtitles: n } = e, r = e.mode ?? V() ?? "window", i = e.maxWidth ?? (r === "burn-in" ? L : ue), a, o, s, c = !1, l = !1, u = !1, d = !1, f, p = () => r === "burn-in" ? !!a : !!a && document.pictureInPictureElement === a.mirror, m = (e, r) => {
|
|
589
588
|
r.drawImage(t, 0, 0, e.width, e.height);
|
|
590
589
|
let i = n.querySelector("canvas");
|
|
591
590
|
i && i.width > 0 && i.height > 0 && r.drawImage(i, 0, 0, e.width, e.height);
|
|
@@ -701,4 +700,4 @@ var i = {
|
|
|
701
700
|
};
|
|
702
701
|
};
|
|
703
702
|
//#endregion
|
|
704
|
-
export { b as a, M as c, s as d, l as f, j as i, u as l, a as m,
|
|
703
|
+
export { b as a, M as c, s as d, l as f, j as i, u as l, a as m, V as n, x as o, i as p, le as r, re as s, H as t, y as u };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { c as e, l as t, m as n, n as r, o as i, p as a, r as o, s, t as c, u as l } from "./engine-
|
|
1
|
+
import { c as e, l as t, m as n, n as r, o as i, p as a, r as o, s, t as c, u as l } from "./engine-BZDbaSfM.js";
|
|
2
2
|
import { languageOf as u, preferredTrack as d } from "./language/index.js";
|
|
3
3
|
import { n as f, r as p, t as m } from "./chapters-EBYXcEml.js";
|
|
4
4
|
import { Children as h, Fragment as g, createContext as _, isValidElement as v, useCallback as y, useContext as b, useEffect as x, useMemo as S, useRef as C, useState as w, useSyncExternalStore as T } from "react";
|
|
@@ -750,7 +750,8 @@ var se = {
|
|
|
750
750
|
}, [n.data]), O = !0, E = !0, p.postMessage({
|
|
751
751
|
type: "run",
|
|
752
752
|
run: D,
|
|
753
|
-
pts:
|
|
753
|
+
pts: n.timestampOffset,
|
|
754
|
+
from: 0,
|
|
754
755
|
start: !0
|
|
755
756
|
}); !a;) {
|
|
756
757
|
let e = P();
|
|
@@ -769,15 +770,16 @@ var se = {
|
|
|
769
770
|
await B(!1), A.set(Math.round(e * 10), g);
|
|
770
771
|
let t = z.seek(Math.max(0, e - Ze));
|
|
771
772
|
f(!0);
|
|
772
|
-
let
|
|
773
|
+
let r = await I(t);
|
|
773
774
|
if (a) return;
|
|
774
|
-
if (!
|
|
775
|
-
D++, O = !0, k =
|
|
775
|
+
if (!r) continue;
|
|
776
|
+
D++, O = !0, k = r.pts, p.postMessage({
|
|
776
777
|
type: "run",
|
|
777
778
|
run: D,
|
|
778
|
-
pts: n.
|
|
779
|
-
|
|
780
|
-
|
|
779
|
+
pts: n.timestampOffset,
|
|
780
|
+
from: r.pts,
|
|
781
|
+
start: r.pts <= 0
|
|
782
|
+
}), await V(r), r.finished && await B(!0);
|
|
781
783
|
}
|
|
782
784
|
} catch {
|
|
783
785
|
if (a) return;
|
|
@@ -807,7 +809,8 @@ var se = {
|
|
|
807
809
|
type: "run",
|
|
808
810
|
run: ++D,
|
|
809
811
|
pts: e.pts,
|
|
810
|
-
|
|
812
|
+
from: e.from,
|
|
813
|
+
start: !t && e.from <= 0,
|
|
811
814
|
join: t
|
|
812
815
|
});
|
|
813
816
|
else if (e.type === "end") r(!0);
|
|
@@ -893,7 +896,7 @@ var se = {
|
|
|
893
896
|
return {
|
|
894
897
|
push: (n) => {
|
|
895
898
|
n.type === "init" ? (e = n, t = void 0) : n.type === "run" ? t = {
|
|
896
|
-
|
|
899
|
+
output: n,
|
|
897
900
|
begun: !1
|
|
898
901
|
} : n.type === "fragment" && t && (t.begun = !0), r()?.take(n);
|
|
899
902
|
},
|
|
@@ -902,10 +905,7 @@ var se = {
|
|
|
902
905
|
audioStreamIndex: i,
|
|
903
906
|
take: a
|
|
904
907
|
};
|
|
905
|
-
return n = o, r() && e && (a(e), t && a({
|
|
906
|
-
type: "run",
|
|
907
|
-
pts: t.pts
|
|
908
|
-
}, t.begun)), () => {
|
|
908
|
+
return n = o, r() && e && (a(e), t && a(t.output, t.begun)), () => {
|
|
909
909
|
n === o && (n = void 0);
|
|
910
910
|
};
|
|
911
911
|
}
|
|
@@ -440,8 +440,9 @@ export const audioSamplesOf = (fragment, track) => {
|
|
|
440
440
|
/**
|
|
441
441
|
* The analysis: 1 ms bins of whatever audio it is given, the normal level learned from them, and the cut for the
|
|
442
442
|
* blocks around the playhead. `toWorklet` takes `{ block, gain, finger }` (10 s of bins from `block * 10` s, NaN where
|
|
443
|
-
* the cut or the audio is not known)
|
|
444
|
-
* the worklet keeps
|
|
443
|
+
* the cut or the audio is not known), `{ drop }` for each block past the 60 sent nearest the playhead, which is all
|
|
444
|
+
* the worklet keeps, and `{ end }`, the bin the file's audio ends at, once a run reaches it; `toPage` takes the
|
|
445
|
+
* coverage reports the page schedules reads on.
|
|
445
446
|
* Separate from the worker's messages so a test can drive it with decoded audio.
|
|
446
447
|
*/
|
|
447
448
|
export const createAnalysis = ({ toWorklet, toPage = () => {} }) => {
|
|
@@ -556,8 +557,6 @@ export const createAnalysis = ({ toWorklet, toPage = () => {} }) => {
|
|
|
556
557
|
finger[bin - start] = Math.log(chunk.finger[bin % BLOCK_BINS] + 1e-10)
|
|
557
558
|
if (bin < known) gain[bin - start] = curve[bin - from]
|
|
558
559
|
}
|
|
559
|
-
// the last cut holds past the end of the file, where playback's audio can run on by its offset
|
|
560
|
-
if (b >= endBin && b > start && b < end) gain.fill(gain[b - 1 - start], b - start)
|
|
561
560
|
}
|
|
562
561
|
return { gain, finger }
|
|
563
562
|
}
|
|
@@ -616,7 +615,10 @@ export const createAnalysis = ({ toWorklet, toPage = () => {} }) => {
|
|
|
616
615
|
endRun: (finished) => {
|
|
617
616
|
if (!run) return
|
|
618
617
|
run.binner?.flush()
|
|
619
|
-
if (finished && run.last >= 0)
|
|
618
|
+
if (finished && run.last >= 0) {
|
|
619
|
+
endBin = run.last + 1
|
|
620
|
+
toWorklet({ end: endBin })
|
|
621
|
+
}
|
|
620
622
|
run = null
|
|
621
623
|
schedule()
|
|
622
624
|
},
|
|
@@ -644,10 +646,11 @@ export const createAnalysis = ({ toWorklet, toPage = () => {} }) => {
|
|
|
644
646
|
*
|
|
645
647
|
* { type: 'port', port } the worklet's end of a MessageChannel, for the blocks
|
|
646
648
|
* { type: 'init', data, codec } the init segment and the codec string MediaSource was given
|
|
647
|
-
* { type: 'run', run, pts, start }
|
|
648
|
-
*
|
|
649
|
-
*
|
|
650
|
-
*
|
|
649
|
+
* { type: 'run', run, pts, from, start }
|
|
650
|
+
* a run of fragments starts at `from` s, `pts` added to their times as the
|
|
651
|
+
* SourceBuffer's timestampOffset adds it for playback; `start` for the one
|
|
652
|
+
* from the start, and `join: true` for one whose first fragments went by
|
|
653
|
+
* unseen, which starts at its first sample
|
|
651
654
|
* { type: 'fragment', run, data } answered { type: 'ack', run, spans, end, normal } once its audio is queued
|
|
652
655
|
* for decoding: the analysed seconds, the file's end once known, and the
|
|
653
656
|
* level learned so far in LUFS
|
|
@@ -682,10 +685,10 @@ const serve = (scope) => {
|
|
|
682
685
|
})
|
|
683
686
|
/*
|
|
684
687
|
* Each output is placed by its own chunk's timestamp, never the one AudioData carries: Chrome 155 stamps outputs by
|
|
685
|
-
* counting samples from the first chunk,
|
|
686
|
-
*
|
|
687
|
-
*
|
|
688
|
-
* 2026-10-17).
|
|
688
|
+
* counting samples from the first chunk, so past a gap in the timestamps, which MediaSource plays as a gap, what
|
|
689
|
+
* follows comes out early by the gap. Before 0.9.0 libav-wasm's first audio sample of a run lasted until the video's
|
|
690
|
+
* first frame, 62 ms more than its own length in Matroska and mp4 with B-frames and 82 ms in FLAC, too far for the
|
|
691
|
+
* first match to find (HOR-498 build, 2026-10-17).
|
|
689
692
|
*/
|
|
690
693
|
let stamps = []
|
|
691
694
|
const open = (id) => {
|
|
@@ -722,7 +725,7 @@ const serve = (scope) => {
|
|
|
722
725
|
pts = data.pts
|
|
723
726
|
open(runId)
|
|
724
727
|
joining = !!data.join
|
|
725
|
-
if (!joining) analysis.startRun(runId,
|
|
728
|
+
if (!joining) analysis.startRun(runId, data.from, data.start)
|
|
726
729
|
} else if (data.type === 'fragment') {
|
|
727
730
|
if (data.run !== runId || decoder.state !== 'configured') return
|
|
728
731
|
const samples = audioSamplesOf(data.data, track)
|
|
@@ -781,13 +784,14 @@ const HANDOVER_SECONDS = 0.05
|
|
|
781
784
|
const CROSSFADE_SECONDS = 0.05
|
|
782
785
|
/*
|
|
783
786
|
* How far either way the live audio is searched for in the analysed audio, in ms: first, and after a seek, against
|
|
784
|
-
* where the last match put it. Played from the start, the fixtures matched 21 to 60 ms off, and FLAC past 60 ms
|
|
785
|
-
*
|
|
787
|
+
* where the last match put it. Played from the start, the fixtures matched 21 to 60 ms off, and FLAC past 60 ms, while
|
|
788
|
+
* libav-wasm stretched each run's first audio sample to the video's first frame (HOR-498 build, 2026-10-17); with each
|
|
789
|
+
* track at its own time (0.9.0), within 3.4 ms (2026-10-30).
|
|
786
790
|
*/
|
|
787
791
|
const LOCK_RANGE_MS = 250
|
|
788
792
|
const RELOCK_RANGE_MS = 30
|
|
789
793
|
// how far from the last match a seek may move the audio, which the deepest cut in doubt covers, in ms: FLAC moved 82 ms
|
|
790
|
-
// across the first remuxer seek
|
|
794
|
+
// across the first remuxer seek while libav-wasm stretched a run's first audio sample, and at most 3 ms since 0.9.0
|
|
791
795
|
const DOUBT_MS = 100
|
|
792
796
|
// how far from the best offset another must come close for the match to be in question, in ms
|
|
793
797
|
const CLEAR_MS = 60
|
|
@@ -805,14 +809,15 @@ const SCORES_PER_QUANTUM = 40_000
|
|
|
805
809
|
* context.currentTime read in one task names the media time of the audio reaching a worklet that same render frame,
|
|
806
810
|
* to the sample for a 48 kHz file and within about 2 ms for a 44.1 kHz one, except for the odd pair off by up to
|
|
807
811
|
* 13 ms; so a jump of more than 25 ms re-anchors the timeline, and the median of the next five pairs settles it. Where
|
|
808
|
-
* playback's audio sits against the analysed audio
|
|
809
|
-
* the fixtures, the same file 44 ms apart before and after a seek
|
|
810
|
-
*
|
|
811
|
-
*
|
|
812
|
-
*
|
|
813
|
-
*
|
|
814
|
-
*
|
|
815
|
-
*
|
|
812
|
+
* playback's audio sits against the analysed audio changed with each run of the remuxer behind it while libav-wasm
|
|
813
|
+
* moved each run by its own amount: -18 to +44 ms on the fixtures, the same file 44 ms apart before and after a seek
|
|
814
|
+
* (HOR-498 build, 2026-10-17); with each track at its own time, -3.4 to +1.2 ms (0.9.0, 2026-10-30). So the offset
|
|
815
|
+
* is found from the audio itself: the live fingerprint against the analysed one, every 250 ms at normal speed, to a
|
|
816
|
+
* fraction of a millisecond. The same match catches the rare stall (once, 60 ms of silence came in mid-file and the
|
|
817
|
+
* audio after it ran 11 ms ahead of the clock). Nothing is cut ahead until the first match, which also proves the
|
|
818
|
+
* blocks belong to this audio; after a seek, until the next one, each quantum gets the deepest cut within the doubt,
|
|
819
|
+
* never less than the cut asks, as it does away from normal speed (Chrome's time stretching moves the audio up to
|
|
820
|
+
* about 30 ms either way).
|
|
816
821
|
*/
|
|
817
822
|
const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcessor {
|
|
818
823
|
constructor({ processorOptions = {} } = {}) {
|
|
@@ -867,6 +872,8 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
|
|
|
867
872
|
this.shortTime = 0
|
|
868
873
|
|
|
869
874
|
this.blocks = new Map()
|
|
875
|
+
// the bin the file's audio ends at; the last cut holds past it, where playback's audio can run on
|
|
876
|
+
this.end = Infinity
|
|
870
877
|
this.analysis = null
|
|
871
878
|
this.fingerFilter = fingerprintFilter(sampleRate)
|
|
872
879
|
this.fingerState = []
|
|
@@ -909,6 +916,7 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
|
|
|
909
916
|
if (data?.analysis !== undefined) {
|
|
910
917
|
this.analysis?.close()
|
|
911
918
|
this.blocks.clear()
|
|
919
|
+
this.end = Infinity
|
|
912
920
|
this.search = null
|
|
913
921
|
this.analysis = data.analysis
|
|
914
922
|
if (this.analysis) this.analysis.onmessage = ({ data: block }) => this.receive(block)
|
|
@@ -930,6 +938,7 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
|
|
|
930
938
|
receive(message) {
|
|
931
939
|
if (typeof message?.drop === 'number') this.blocks.delete(message.drop)
|
|
932
940
|
else if (typeof message?.block === 'number') this.blocks.set(message.block, message)
|
|
941
|
+
else if (typeof message?.end === 'number') this.end = message.end
|
|
933
942
|
this.lastBlock = undefined
|
|
934
943
|
}
|
|
935
944
|
|
|
@@ -937,7 +946,11 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
|
|
|
937
946
|
const frame = ctx * sampleRate
|
|
938
947
|
const model = this.model
|
|
939
948
|
if (paused) {
|
|
940
|
-
|
|
949
|
+
// More than 25 ms from where the clock had it, the media is where the pair says: a seek moved it, its audio
|
|
950
|
+
// already playing (10 to 27 ms after the move in Chrome 155, where the main thread once took 52 ms to say so,
|
|
951
|
+
// 2026-10-30), or a pause was told late. The seek's reset, posted on the same event, is what puts it in doubt.
|
|
952
|
+
const held = model && !model.paused ? this.mediaAt(frame) : media
|
|
953
|
+
this.model = { paused: true, media: Math.abs(media - held) <= 0.025 ? held : media }
|
|
941
954
|
this.liveCount = 0
|
|
942
955
|
return
|
|
943
956
|
}
|
|
@@ -973,6 +986,7 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
|
|
|
973
986
|
}
|
|
974
987
|
|
|
975
988
|
gainBin(i) {
|
|
989
|
+
if (i >= this.end) i = this.end - 1
|
|
976
990
|
const block = this.blockOf(i)
|
|
977
991
|
return block.gain ? block.gain[i - block.block * BLOCK_BINS] : NaN
|
|
978
992
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@banou/media-player",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.32",
|
|
4
4
|
"description": "A video player for containers and codecs the browser cannot play natively, remuxed on the fly",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"@videojs/react": "10.0.0-beta.26",
|
|
55
55
|
"ass-compiler": "^0.1.16",
|
|
56
56
|
"jassub": "^2.5.23",
|
|
57
|
-
"libav-wasm": "^0.8.
|
|
57
|
+
"libav-wasm": "^0.8.14",
|
|
58
58
|
"lucide-react": "^0.536.0",
|
|
59
59
|
"osra": "^0.6.12",
|
|
60
60
|
"react-tooltip": "^5.28.0"
|
|
@@ -202,7 +202,7 @@ export const startLoudnessAnalysis = ({ node, element, scriptUrl, source }: {
|
|
|
202
202
|
worker.postMessage({ type: 'init', data: init.data, codec: init.info.output.audioMimeType }, [init.data])
|
|
203
203
|
active = true
|
|
204
204
|
started = true
|
|
205
|
-
worker.postMessage({ type: 'run', run, pts: 0, start: true })
|
|
205
|
+
worker.postMessage({ type: 'run', run, pts: init.timestampOffset, from: 0, start: true })
|
|
206
206
|
while (!stopped) {
|
|
207
207
|
const next = decide()
|
|
208
208
|
if (next === 'done') break
|
|
@@ -229,7 +229,7 @@ export const startLoudnessAnalysis = ({ node, element, scriptUrl, source }: {
|
|
|
229
229
|
run++
|
|
230
230
|
active = true
|
|
231
231
|
runStart = result.pts
|
|
232
|
-
worker.postMessage({ type: 'run', run, pts: result.pts, start: result.pts <= 0 })
|
|
232
|
+
worker.postMessage({ type: 'run', run, pts: init.timestampOffset, from: result.pts, start: result.pts <= 0 })
|
|
233
233
|
await send(result)
|
|
234
234
|
if (result.finished) await endRun(true)
|
|
235
235
|
}
|
|
@@ -260,7 +260,7 @@ export const startLoudnessAnalysis = ({ node, element, scriptUrl, source }: {
|
|
|
260
260
|
} else if (output.type === 'run') {
|
|
261
261
|
end(false)
|
|
262
262
|
open = true
|
|
263
|
-
worker.postMessage({ type: 'run', run: ++run, pts: output.pts, start: !join && output.
|
|
263
|
+
worker.postMessage({ type: 'run', run: ++run, pts: output.pts, from: output.from, start: !join && output.from <= 0, join })
|
|
264
264
|
} else if (output.type === 'end') end(true)
|
|
265
265
|
else if (open) {
|
|
266
266
|
const data = output.data.slice(0)
|
|
@@ -12,7 +12,7 @@ export type PlaybackTap = {
|
|
|
12
12
|
|
|
13
13
|
export const createPlaybackTap = (): PlaybackTap => {
|
|
14
14
|
let init: Extract<PlaybackOutput, { type: 'init' }> | undefined
|
|
15
|
-
let run: {
|
|
15
|
+
let run: { output: Extract<PlaybackOutput, { type: 'run' }>, begun: boolean } | undefined
|
|
16
16
|
let listener: { audioStreamIndex: number, take: (output: PlaybackOutput, join?: boolean) => void } | undefined
|
|
17
17
|
const live = () => listener && init?.audioStreamIndex === listener.audioStreamIndex ? listener : undefined
|
|
18
18
|
return {
|
|
@@ -20,7 +20,7 @@ export const createPlaybackTap = (): PlaybackTap => {
|
|
|
20
20
|
if (output.type === 'init') {
|
|
21
21
|
init = output
|
|
22
22
|
run = undefined
|
|
23
|
-
} else if (output.type === 'run') run = {
|
|
23
|
+
} else if (output.type === 'run') run = { output, begun: false }
|
|
24
24
|
else if (output.type === 'fragment' && run) run.begun = true
|
|
25
25
|
live()?.take(output)
|
|
26
26
|
},
|
|
@@ -29,7 +29,7 @@ export const createPlaybackTap = (): PlaybackTap => {
|
|
|
29
29
|
listener = mine
|
|
30
30
|
if (live() && init) {
|
|
31
31
|
take(init)
|
|
32
|
-
if (run) take(
|
|
32
|
+
if (run) take(run.output, run.begun)
|
|
33
33
|
}
|
|
34
34
|
return () => { if (listener === mine) listener = undefined }
|
|
35
35
|
},
|
|
@@ -32,7 +32,7 @@ export type AudioTrack = {
|
|
|
32
32
|
export const audioTrackOf: (init: ArrayBuffer) => AudioTrack | undefined
|
|
33
33
|
export const audioSamplesOf: (fragment: ArrayBuffer, track: AudioTrack) => { time: number, duration: number, data: Uint8Array }[]
|
|
34
34
|
|
|
35
|
-
export type BlockMessage = { block: number, gain: Float32Array, finger: Float32Array } | { drop: number }
|
|
35
|
+
export type BlockMessage = { block: number, gain: Float32Array, finger: Float32Array } | { drop: number } | { end: number }
|
|
36
36
|
export type Analysis = {
|
|
37
37
|
startRun: (id: number, time: number, start?: boolean) => void
|
|
38
38
|
push: (planes: Float32Array[], frames: number, rate: number, time: number) => void
|
|
@@ -440,8 +440,9 @@ export const audioSamplesOf = (fragment, track) => {
|
|
|
440
440
|
/**
|
|
441
441
|
* The analysis: 1 ms bins of whatever audio it is given, the normal level learned from them, and the cut for the
|
|
442
442
|
* blocks around the playhead. `toWorklet` takes `{ block, gain, finger }` (10 s of bins from `block * 10` s, NaN where
|
|
443
|
-
* the cut or the audio is not known)
|
|
444
|
-
* the worklet keeps
|
|
443
|
+
* the cut or the audio is not known), `{ drop }` for each block past the 60 sent nearest the playhead, which is all
|
|
444
|
+
* the worklet keeps, and `{ end }`, the bin the file's audio ends at, once a run reaches it; `toPage` takes the
|
|
445
|
+
* coverage reports the page schedules reads on.
|
|
445
446
|
* Separate from the worker's messages so a test can drive it with decoded audio.
|
|
446
447
|
*/
|
|
447
448
|
export const createAnalysis = ({ toWorklet, toPage = () => {} }) => {
|
|
@@ -556,8 +557,6 @@ export const createAnalysis = ({ toWorklet, toPage = () => {} }) => {
|
|
|
556
557
|
finger[bin - start] = Math.log(chunk.finger[bin % BLOCK_BINS] + 1e-10)
|
|
557
558
|
if (bin < known) gain[bin - start] = curve[bin - from]
|
|
558
559
|
}
|
|
559
|
-
// the last cut holds past the end of the file, where playback's audio can run on by its offset
|
|
560
|
-
if (b >= endBin && b > start && b < end) gain.fill(gain[b - 1 - start], b - start)
|
|
561
560
|
}
|
|
562
561
|
return { gain, finger }
|
|
563
562
|
}
|
|
@@ -616,7 +615,10 @@ export const createAnalysis = ({ toWorklet, toPage = () => {} }) => {
|
|
|
616
615
|
endRun: (finished) => {
|
|
617
616
|
if (!run) return
|
|
618
617
|
run.binner?.flush()
|
|
619
|
-
if (finished && run.last >= 0)
|
|
618
|
+
if (finished && run.last >= 0) {
|
|
619
|
+
endBin = run.last + 1
|
|
620
|
+
toWorklet({ end: endBin })
|
|
621
|
+
}
|
|
620
622
|
run = null
|
|
621
623
|
schedule()
|
|
622
624
|
},
|
|
@@ -644,10 +646,11 @@ export const createAnalysis = ({ toWorklet, toPage = () => {} }) => {
|
|
|
644
646
|
*
|
|
645
647
|
* { type: 'port', port } the worklet's end of a MessageChannel, for the blocks
|
|
646
648
|
* { type: 'init', data, codec } the init segment and the codec string MediaSource was given
|
|
647
|
-
* { type: 'run', run, pts, start }
|
|
648
|
-
*
|
|
649
|
-
*
|
|
650
|
-
*
|
|
649
|
+
* { type: 'run', run, pts, from, start }
|
|
650
|
+
* a run of fragments starts at `from` s, `pts` added to their times as the
|
|
651
|
+
* SourceBuffer's timestampOffset adds it for playback; `start` for the one
|
|
652
|
+
* from the start, and `join: true` for one whose first fragments went by
|
|
653
|
+
* unseen, which starts at its first sample
|
|
651
654
|
* { type: 'fragment', run, data } answered { type: 'ack', run, spans, end, normal } once its audio is queued
|
|
652
655
|
* for decoding: the analysed seconds, the file's end once known, and the
|
|
653
656
|
* level learned so far in LUFS
|
|
@@ -682,10 +685,10 @@ const serve = (scope) => {
|
|
|
682
685
|
})
|
|
683
686
|
/*
|
|
684
687
|
* Each output is placed by its own chunk's timestamp, never the one AudioData carries: Chrome 155 stamps outputs by
|
|
685
|
-
* counting samples from the first chunk,
|
|
686
|
-
*
|
|
687
|
-
*
|
|
688
|
-
* 2026-10-17).
|
|
688
|
+
* counting samples from the first chunk, so past a gap in the timestamps, which MediaSource plays as a gap, what
|
|
689
|
+
* follows comes out early by the gap. Before 0.9.0 libav-wasm's first audio sample of a run lasted until the video's
|
|
690
|
+
* first frame, 62 ms more than its own length in Matroska and mp4 with B-frames and 82 ms in FLAC, too far for the
|
|
691
|
+
* first match to find (HOR-498 build, 2026-10-17).
|
|
689
692
|
*/
|
|
690
693
|
let stamps = []
|
|
691
694
|
const open = (id) => {
|
|
@@ -722,7 +725,7 @@ const serve = (scope) => {
|
|
|
722
725
|
pts = data.pts
|
|
723
726
|
open(runId)
|
|
724
727
|
joining = !!data.join
|
|
725
|
-
if (!joining) analysis.startRun(runId,
|
|
728
|
+
if (!joining) analysis.startRun(runId, data.from, data.start)
|
|
726
729
|
} else if (data.type === 'fragment') {
|
|
727
730
|
if (data.run !== runId || decoder.state !== 'configured') return
|
|
728
731
|
const samples = audioSamplesOf(data.data, track)
|
|
@@ -781,13 +784,14 @@ const HANDOVER_SECONDS = 0.05
|
|
|
781
784
|
const CROSSFADE_SECONDS = 0.05
|
|
782
785
|
/*
|
|
783
786
|
* How far either way the live audio is searched for in the analysed audio, in ms: first, and after a seek, against
|
|
784
|
-
* where the last match put it. Played from the start, the fixtures matched 21 to 60 ms off, and FLAC past 60 ms
|
|
785
|
-
*
|
|
787
|
+
* where the last match put it. Played from the start, the fixtures matched 21 to 60 ms off, and FLAC past 60 ms, while
|
|
788
|
+
* libav-wasm stretched each run's first audio sample to the video's first frame (HOR-498 build, 2026-10-17); with each
|
|
789
|
+
* track at its own time (0.9.0), within 3.4 ms (2026-10-30).
|
|
786
790
|
*/
|
|
787
791
|
const LOCK_RANGE_MS = 250
|
|
788
792
|
const RELOCK_RANGE_MS = 30
|
|
789
793
|
// how far from the last match a seek may move the audio, which the deepest cut in doubt covers, in ms: FLAC moved 82 ms
|
|
790
|
-
// across the first remuxer seek
|
|
794
|
+
// across the first remuxer seek while libav-wasm stretched a run's first audio sample, and at most 3 ms since 0.9.0
|
|
791
795
|
const DOUBT_MS = 100
|
|
792
796
|
// how far from the best offset another must come close for the match to be in question, in ms
|
|
793
797
|
const CLEAR_MS = 60
|
|
@@ -805,14 +809,15 @@ const SCORES_PER_QUANTUM = 40_000
|
|
|
805
809
|
* context.currentTime read in one task names the media time of the audio reaching a worklet that same render frame,
|
|
806
810
|
* to the sample for a 48 kHz file and within about 2 ms for a 44.1 kHz one, except for the odd pair off by up to
|
|
807
811
|
* 13 ms; so a jump of more than 25 ms re-anchors the timeline, and the median of the next five pairs settles it. Where
|
|
808
|
-
* playback's audio sits against the analysed audio
|
|
809
|
-
* the fixtures, the same file 44 ms apart before and after a seek
|
|
810
|
-
*
|
|
811
|
-
*
|
|
812
|
-
*
|
|
813
|
-
*
|
|
814
|
-
*
|
|
815
|
-
*
|
|
812
|
+
* playback's audio sits against the analysed audio changed with each run of the remuxer behind it while libav-wasm
|
|
813
|
+
* moved each run by its own amount: -18 to +44 ms on the fixtures, the same file 44 ms apart before and after a seek
|
|
814
|
+
* (HOR-498 build, 2026-10-17); with each track at its own time, -3.4 to +1.2 ms (0.9.0, 2026-10-30). So the offset
|
|
815
|
+
* is found from the audio itself: the live fingerprint against the analysed one, every 250 ms at normal speed, to a
|
|
816
|
+
* fraction of a millisecond. The same match catches the rare stall (once, 60 ms of silence came in mid-file and the
|
|
817
|
+
* audio after it ran 11 ms ahead of the clock). Nothing is cut ahead until the first match, which also proves the
|
|
818
|
+
* blocks belong to this audio; after a seek, until the next one, each quantum gets the deepest cut within the doubt,
|
|
819
|
+
* never less than the cut asks, as it does away from normal speed (Chrome's time stretching moves the audio up to
|
|
820
|
+
* about 30 ms either way).
|
|
816
821
|
*/
|
|
817
822
|
const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcessor {
|
|
818
823
|
constructor({ processorOptions = {} } = {}) {
|
|
@@ -867,6 +872,8 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
|
|
|
867
872
|
this.shortTime = 0
|
|
868
873
|
|
|
869
874
|
this.blocks = new Map()
|
|
875
|
+
// the bin the file's audio ends at; the last cut holds past it, where playback's audio can run on
|
|
876
|
+
this.end = Infinity
|
|
870
877
|
this.analysis = null
|
|
871
878
|
this.fingerFilter = fingerprintFilter(sampleRate)
|
|
872
879
|
this.fingerState = []
|
|
@@ -909,6 +916,7 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
|
|
|
909
916
|
if (data?.analysis !== undefined) {
|
|
910
917
|
this.analysis?.close()
|
|
911
918
|
this.blocks.clear()
|
|
919
|
+
this.end = Infinity
|
|
912
920
|
this.search = null
|
|
913
921
|
this.analysis = data.analysis
|
|
914
922
|
if (this.analysis) this.analysis.onmessage = ({ data: block }) => this.receive(block)
|
|
@@ -930,6 +938,7 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
|
|
|
930
938
|
receive(message) {
|
|
931
939
|
if (typeof message?.drop === 'number') this.blocks.delete(message.drop)
|
|
932
940
|
else if (typeof message?.block === 'number') this.blocks.set(message.block, message)
|
|
941
|
+
else if (typeof message?.end === 'number') this.end = message.end
|
|
933
942
|
this.lastBlock = undefined
|
|
934
943
|
}
|
|
935
944
|
|
|
@@ -937,7 +946,11 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
|
|
|
937
946
|
const frame = ctx * sampleRate
|
|
938
947
|
const model = this.model
|
|
939
948
|
if (paused) {
|
|
940
|
-
|
|
949
|
+
// More than 25 ms from where the clock had it, the media is where the pair says: a seek moved it, its audio
|
|
950
|
+
// already playing (10 to 27 ms after the move in Chrome 155, where the main thread once took 52 ms to say so,
|
|
951
|
+
// 2026-10-30), or a pause was told late. The seek's reset, posted on the same event, is what puts it in doubt.
|
|
952
|
+
const held = model && !model.paused ? this.mediaAt(frame) : media
|
|
953
|
+
this.model = { paused: true, media: Math.abs(media - held) <= 0.025 ? held : media }
|
|
941
954
|
this.liveCount = 0
|
|
942
955
|
return
|
|
943
956
|
}
|
|
@@ -973,6 +986,7 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
|
|
|
973
986
|
}
|
|
974
987
|
|
|
975
988
|
gainBin(i) {
|
|
989
|
+
if (i >= this.end) i = this.end - 1
|
|
976
990
|
const block = this.blockOf(i)
|
|
977
991
|
return block.gain ? block.gain[i - block.block * BLOCK_BINS] : NaN
|
|
978
992
|
}
|
|
@@ -26,13 +26,13 @@ export type MediaChapter = { start: number, end: number, title: string }
|
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* What the remuxer produced, in the order it was appended: the init segment and the input audio stream it carries,
|
|
29
|
-
* a run at load and at every remuxer seek (`pts` is the timestampOffset its fragments are appended at
|
|
30
|
-
* fragment, and the end of the file once read. The buffers
|
|
31
|
-
* consumer copies what it keeps.
|
|
29
|
+
* a run at load and at every remuxer seek (`pts` is the timestampOffset its fragments are appended at, the same for
|
|
30
|
+
* every run, and `from` the time the run starts at), each fragment, and the end of the file once read. The buffers
|
|
31
|
+
* are playback's own and may be appended again, so a consumer copies what it keeps.
|
|
32
32
|
*/
|
|
33
33
|
export type PlaybackOutput =
|
|
34
34
|
| { type: 'init', data: ArrayBuffer, audioMimeType: string, audioStreamIndex: number }
|
|
35
|
-
| { type: 'run', pts: number }
|
|
35
|
+
| { type: 'run', pts: number, from: number }
|
|
36
36
|
| { type: 'fragment', data: ArrayBuffer }
|
|
37
37
|
| { type: 'end' }
|
|
38
38
|
|
|
@@ -126,10 +126,11 @@ const SOURCE_OPEN_TIMEOUT = 15_000
|
|
|
126
126
|
* How far past the playhead a range may start and still count as the one holding it, which decides how
|
|
127
127
|
* much to read and never whether to fetch.
|
|
128
128
|
*
|
|
129
|
-
*
|
|
130
|
-
* on most fixtures and 166 ms on anime-chapters,
|
|
131
|
-
*
|
|
132
|
-
*
|
|
129
|
+
* A range starts at its first presented frame. While libav-wasm moved every track by the reorder delay (before
|
|
130
|
+
* 0.9.0), Firefox 151 reported each range 82 ms past its keyframe on most fixtures and 166 ms on anime-chapters, and
|
|
131
|
+
* without the slack a playhead paused at 0 read the whole file and a prepared seek to a keyframe built no runway
|
|
132
|
+
* (measured 2026-10-11). Firefox 157 and Chrome 155 now report the keyframe or earlier (measured 2026-10-30), and the
|
|
133
|
+
* slack covers a file whose first picture comes after 0.
|
|
133
134
|
*/
|
|
134
135
|
const BOUNDARY_SLACK = 1
|
|
135
136
|
/*
|
|
@@ -264,6 +265,7 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
|
|
|
264
265
|
try {
|
|
265
266
|
const sb = mediaSource.addSourceBuffer(mime)
|
|
266
267
|
sb.mode = 'segments'
|
|
268
|
+
sb.timestampOffset = metadata.timestampOffset
|
|
267
269
|
const duration = metadata.info.input.duration
|
|
268
270
|
if (Number.isFinite(duration) && duration > 0) mediaSource.duration = duration
|
|
269
271
|
cleanup()
|
|
@@ -279,10 +281,10 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
|
|
|
279
281
|
mediaSource.addEventListener('error', onFail, { once: true })
|
|
280
282
|
})
|
|
281
283
|
|
|
282
|
-
const { appendBuffer, unbufferRange,
|
|
284
|
+
const { appendBuffer, unbufferRange, endOfStream } = updateSourceBuffer(sourceBuffer, mediaSource)
|
|
283
285
|
await appendBuffer(metadata.data)
|
|
284
286
|
onOutput?.({ type: 'init', data: metadata.data, audioMimeType: metadata.info.output.audioMimeType, audioStreamIndex: selectedAudio })
|
|
285
|
-
onOutput?.({ type: 'run', pts: 0 })
|
|
287
|
+
onOutput?.({ type: 'run', pts: metadata.timestampOffset, from: 0 })
|
|
286
288
|
if (metadata.subtitles?.length) subtitles.pushFragments(metadata.subtitles)
|
|
287
289
|
onReady?.()
|
|
288
290
|
|
|
@@ -428,9 +430,7 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
|
|
|
428
430
|
if (destroyed || generation !== seekGeneration) return
|
|
429
431
|
if (done) finished = true
|
|
430
432
|
if (fragments?.length) subtitles.pushFragments(fragments)
|
|
431
|
-
|
|
432
|
-
if (destroyed || generation !== seekGeneration) return
|
|
433
|
-
onOutput?.({ type: 'run', pts })
|
|
433
|
+
onOutput?.({ type: 'run', pts: metadata.timestampOffset, from: pts })
|
|
434
434
|
if (data.byteLength) onOutput?.({ type: 'fragment', data })
|
|
435
435
|
if (done) onOutput?.({ type: 'end' })
|
|
436
436
|
if (data.byteLength) {
|
|
@@ -531,8 +531,7 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
|
|
|
531
531
|
* Whether the element can play from a GIVEN time without a fetch.
|
|
532
532
|
*
|
|
533
533
|
* Exact. A seek that lands before a range waits there for good once the gap passes 60 to 90 ms in
|
|
534
|
-
* Chrome 153, or 250 to 300 ms in Firefox 151
|
|
535
|
-
* and a target inside it costs a remuxer seek (24 fps fixtures, measured 2026-10-11).
|
|
534
|
+
* Chrome 153, or 250 to 300 ms in Firefox 151 (24 fps fixtures, measured 2026-10-11).
|
|
536
535
|
*/
|
|
537
536
|
const playableAt = (time: number) =>
|
|
538
537
|
getTimeRanges(sourceBuffer).some((r) => r.start <= time && time < r.end)
|
|
@@ -692,8 +691,7 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
|
|
|
692
691
|
* once its budget runs out, with the seek or the runway's reads still running. Without this, the
|
|
693
692
|
* move fires `seeking`, which starts the identical read again and bumps the generation, throwing
|
|
694
693
|
* away everything the first one had done. The benchmark caught it as a seek that took twice as
|
|
695
|
-
* long as seeking the old way.
|
|
696
|
-
* range starts the reorder delay past the target, so the target never counts as buffered.
|
|
694
|
+
* long as seeking the old way.
|
|
697
695
|
*/
|
|
698
696
|
if (positioned && (seeking || runwayFrom(time) > 0) && Math.abs(time - lastSeekPosition) < 0.001) return
|
|
699
697
|
finished = false
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { labelledError } from '../labels'
|
|
2
2
|
|
|
3
|
-
// appendBuffer
|
|
4
|
-
// goes through one chain
|
|
3
|
+
// appendBuffer and remove must never overlap an in-flight update, so every op goes through one chain
|
|
5
4
|
|
|
6
5
|
export type TimeRange = { index: number, start: number, end: number }
|
|
7
6
|
|
|
@@ -49,9 +48,6 @@ export const updateSourceBuffer = (sourceBuffer: SourceBuffer, mediaSource: Medi
|
|
|
49
48
|
return waitForUpdate(sourceBuffer)
|
|
50
49
|
})
|
|
51
50
|
|
|
52
|
-
const updateTimestampOffset = (timestampOffset: number) =>
|
|
53
|
-
enqueue(async () => { sourceBuffer.timestampOffset = timestampOffset })
|
|
54
|
-
|
|
55
51
|
/**
|
|
56
52
|
* endOfStream throws mid-update and is undone by the next remove(), so the caller re-arms it.
|
|
57
53
|
* `allowed` is asked when the call reaches the front of the queue, not when it is queued.
|
|
@@ -62,5 +58,5 @@ export const updateSourceBuffer = (sourceBuffer: SourceBuffer, mediaSource: Medi
|
|
|
62
58
|
try { mediaSource.endOfStream() } catch {}
|
|
63
59
|
})
|
|
64
60
|
|
|
65
|
-
return { appendBuffer, unbufferRange,
|
|
61
|
+
return { appendBuffer, unbufferRange, endOfStream }
|
|
66
62
|
}
|