@banou/media-player 0.10.37 → 0.10.38
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 +9 -0
- package/dist/engine/index.js +1 -1
- package/dist/{engine-x5oIkHAN.js → engine-eF2uRLyY.js} +167 -158
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/src/lib/engine/picture-in-picture.ts +56 -8
package/README.md
CHANGED
|
@@ -234,6 +234,15 @@ reflect the real element froze the window: seeking while paused left the old sce
|
|
|
234
234
|
transport state is carried by the Media Session instead, which is what the window reads for its
|
|
235
235
|
play/pause button.
|
|
236
236
|
|
|
237
|
+
Frames are painted on the page's frame callbacks, which follow the browser window, not the picture
|
|
238
|
+
in picture one: they stop with the browser window minimized or off screen, while the window is still
|
|
239
|
+
on screen. A timer then paints at 60 a second instead. The page's own video is hidden while the
|
|
240
|
+
window is open, as Chrome's own picture in picture does, because a video shown in the page moves its
|
|
241
|
+
picture only on the page's rendering, so every paint would read the same frame for 250 ms. The
|
|
242
|
+
subtitles are rendered on the page's rendering too, so once they fall two seconds behind the video
|
|
243
|
+
they are left out of the window rather than shown wrong. `scripts/measure-picture-in-picture.mjs`
|
|
244
|
+
measures each state.
|
|
245
|
+
|
|
237
246
|
The window outlives the player. A player unmounted while its window is open leaves it open for a
|
|
238
247
|
second, and the next player mounted in the same document in that time plays in it, at the shape of
|
|
239
248
|
its own video. So an app that mounts a new player for the next episode keeps the window without a
|
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-eF2uRLyY.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 };
|
|
@@ -131,11 +131,11 @@ var i = {
|
|
|
131
131
|
}, !0);
|
|
132
132
|
}).catch(C);
|
|
133
133
|
}));
|
|
134
|
-
},
|
|
134
|
+
}, D = [
|
|
135
135
|
"loadedmetadata",
|
|
136
136
|
"seeked",
|
|
137
137
|
"pause"
|
|
138
|
-
],
|
|
138
|
+
], O = () => E(), k = (e) => {
|
|
139
139
|
let c = document.createElement("canvas");
|
|
140
140
|
r.append(c);
|
|
141
141
|
try {
|
|
@@ -158,11 +158,11 @@ var i = {
|
|
|
158
158
|
return;
|
|
159
159
|
}
|
|
160
160
|
f.ready.catch(C);
|
|
161
|
-
for (let e of
|
|
161
|
+
for (let e of D) t.addEventListener(e, O);
|
|
162
162
|
s && w((e) => {
|
|
163
163
|
u = Promise.resolve(e.addFonts([s])).then(E, C);
|
|
164
164
|
}), E();
|
|
165
|
-
},
|
|
165
|
+
}, A = (e) => {
|
|
166
166
|
c && !l && e.some((e) => p.test(e.Text)) && (l = !0, w(async (e) => {
|
|
167
167
|
await u, await e.addFonts([c]), E();
|
|
168
168
|
}));
|
|
@@ -181,7 +181,7 @@ var i = {
|
|
|
181
181
|
streamIndex: n.streamIndex,
|
|
182
182
|
title: n.title,
|
|
183
183
|
language: n.language
|
|
184
|
-
}), x?.([...g]), b === void 0 && (b = n.streamIndex), !f && !S &&
|
|
184
|
+
}), x?.([...g]), b === void 0 && (b = n.streamIndex), !f && !S && k(e);
|
|
185
185
|
} else {
|
|
186
186
|
let e = h.get(n.streamIndex);
|
|
187
187
|
if (!e) continue;
|
|
@@ -192,7 +192,7 @@ var i = {
|
|
|
192
192
|
let a = v(e, n);
|
|
193
193
|
r.set(i, a), b === n.streamIndex && t.push(a.assEvent);
|
|
194
194
|
}
|
|
195
|
-
t.length && (w((e) => Promise.all(t.map((t) => e.createEvent(t)))),
|
|
195
|
+
t.length && (w((e) => Promise.all(t.map((t) => e.createEvent(t)))), A(t), E());
|
|
196
196
|
},
|
|
197
197
|
selectStream: (e) => {
|
|
198
198
|
let t = e ?? -1;
|
|
@@ -206,7 +206,7 @@ var i = {
|
|
|
206
206
|
for (let n of r) t.push(e.createEvent(n));
|
|
207
207
|
}
|
|
208
208
|
return Promise.all(t);
|
|
209
|
-
}),
|
|
209
|
+
}), A(r), E();
|
|
210
210
|
},
|
|
211
211
|
getStreams: () => [...g],
|
|
212
212
|
getSelectedStream: () => b,
|
|
@@ -217,7 +217,7 @@ var i = {
|
|
|
217
217
|
S = !0;
|
|
218
218
|
let e = f;
|
|
219
219
|
f = void 0;
|
|
220
|
-
for (let e of
|
|
220
|
+
for (let e of D) t.removeEventListener(e, O);
|
|
221
221
|
e?.destroy().catch(() => {});
|
|
222
222
|
}
|
|
223
223
|
};
|
|
@@ -226,13 +226,13 @@ var i = {
|
|
|
226
226
|
constructor(e, t) {
|
|
227
227
|
super(e, t), this.name = "MediaElementError";
|
|
228
228
|
}
|
|
229
|
-
}, x = (e) => !!e && typeof e == "object" && e.mediaElement === !0, S = -20, C = 60, w = 30, T = -5, E = 20,
|
|
229
|
+
}, x = (e) => !!e && typeof e == "object" && e.mediaElement === !0, S = -20, C = 60, w = 30, T = -5, E = 20, D = 5, O = 15e3, k = 1, A = 3, ee = 12, j = 200, te = 250, M = 25e5, N = (e) => {
|
|
230
230
|
let { worker: t } = e, n = setTimeout(() => t.terminate(), 2e3);
|
|
231
231
|
e.destroy().catch(() => {}).finally(() => {
|
|
232
232
|
clearTimeout(n), t.terminate();
|
|
233
233
|
});
|
|
234
234
|
}, ne = async (t) => {
|
|
235
|
-
let { videoElement: n, subtitleContainer: r, read: i, length: a, publicPath: c, libavWorkerUrl: u, jassubWorkerUrl: d, jassubWasmUrl: f, jassubLegacyWasmUrl: p, defaultFontUrl: m, cjkFontUrl: h, bufferSize: g = M, audioStreamIndex: _, chooseAudioStream: v, onReady: x, onError: ne, onRecovered: P, onSeek:
|
|
235
|
+
let { videoElement: n, subtitleContainer: r, read: i, length: a, publicPath: c, libavWorkerUrl: u, jassubWorkerUrl: d, jassubWasmUrl: f, jassubLegacyWasmUrl: p, defaultFontUrl: m, cjkFontUrl: h, bufferSize: g = M, audioStreamIndex: _, chooseAudioStream: v, onReady: x, onError: ne, onRecovered: P, onSeek: F, onSubtitleStreams: re, onAudioStreams: ie, onOutput: I } = t, ae = (t) => e({
|
|
236
236
|
publicPath: c,
|
|
237
237
|
workerUrl: u,
|
|
238
238
|
workerOptions: { type: "module" },
|
|
@@ -240,20 +240,20 @@ var i = {
|
|
|
240
240
|
length: a,
|
|
241
241
|
audioStreamIndex: t,
|
|
242
242
|
read: i
|
|
243
|
-
}),
|
|
244
|
-
if (!
|
|
245
|
-
|
|
246
|
-
for (let e of [...
|
|
243
|
+
}), L = await ae(_), R = [], z = !1, oe = () => {
|
|
244
|
+
if (!z) {
|
|
245
|
+
z = !0;
|
|
246
|
+
for (let e of [...R].reverse()) try {
|
|
247
247
|
e();
|
|
248
248
|
} catch {}
|
|
249
249
|
}
|
|
250
250
|
};
|
|
251
|
-
|
|
251
|
+
R.push(() => N(L));
|
|
252
252
|
try {
|
|
253
|
-
let e = await
|
|
254
|
-
_ === void 0 && t !== void 0 && t !== e.audioStreams?.[0]?.streamIndex && (N(
|
|
253
|
+
let e = await L.init(), t = _ ?? v?.(e.audioStreams ?? []);
|
|
254
|
+
_ === void 0 && t !== void 0 && t !== e.audioStreams?.[0]?.streamIndex && (N(L), L = await ae(t), e = await L.init());
|
|
255
255
|
let i = e.audioStreams ?? [], a = i.some((e) => e.streamIndex === t) ? t : i[0]?.streamIndex ?? -1;
|
|
256
|
-
|
|
256
|
+
ie?.(i, a);
|
|
257
257
|
let c = y({
|
|
258
258
|
video: n,
|
|
259
259
|
container: r,
|
|
@@ -262,22 +262,22 @@ var i = {
|
|
|
262
262
|
legacyWasmUrl: p,
|
|
263
263
|
defaultFontUrl: m,
|
|
264
264
|
cjkFontUrl: h,
|
|
265
|
-
onStreams:
|
|
265
|
+
onStreams: re
|
|
266
266
|
});
|
|
267
|
-
if (
|
|
267
|
+
if (R.push(() => c.destroy()), e.attachments?.length && c.pushAttachments(e.attachments), !e.info.output.videoMimeType) throw o("The video codec in this file could not be identified");
|
|
268
268
|
let u = [e.info.output.videoMimeType, e.info.output.audioMimeType].filter(Boolean).join(","), g = `video/mp4; codecs="${u}"`;
|
|
269
269
|
if (!MediaSource.isTypeSupported(g)) throw o("This browser cannot play the codecs in this file: {codecs}", { codecs: u });
|
|
270
|
-
let M = new MediaSource(),
|
|
271
|
-
|
|
270
|
+
let M = new MediaSource(), se = URL.createObjectURL(M);
|
|
271
|
+
R.push(() => URL.revokeObjectURL(se)), n.src = se, R.push(() => {
|
|
272
272
|
try {
|
|
273
273
|
n.pause();
|
|
274
274
|
} catch {}
|
|
275
275
|
n.removeAttribute("src"), n.load();
|
|
276
276
|
});
|
|
277
|
-
let
|
|
277
|
+
let B = await new Promise((t, n) => {
|
|
278
278
|
let r = setTimeout(() => {
|
|
279
279
|
i(), n(o("The MediaSource never opened"));
|
|
280
|
-
},
|
|
280
|
+
}, O), i = () => {
|
|
281
281
|
clearTimeout(r), M.removeEventListener("sourceopen", a), M.removeEventListener("sourceclose", s), M.removeEventListener("error", s);
|
|
282
282
|
}, a = () => {
|
|
283
283
|
try {
|
|
@@ -292,156 +292,156 @@ var i = {
|
|
|
292
292
|
i(), n(o("The MediaSource closed before a SourceBuffer could be created"));
|
|
293
293
|
};
|
|
294
294
|
M.addEventListener("sourceopen", a, { once: !0 }), M.addEventListener("sourceclose", s, { once: !0 }), M.addEventListener("error", s, { once: !0 });
|
|
295
|
-
}), { appendBuffer:
|
|
296
|
-
await
|
|
295
|
+
}), { appendBuffer: ce, unbufferRange: le, endOfStream: ue } = l(B, M);
|
|
296
|
+
await ce(e.data), I?.({
|
|
297
297
|
type: "init",
|
|
298
298
|
data: e.data,
|
|
299
299
|
audioMimeType: e.info.output.audioMimeType,
|
|
300
300
|
audioStreamIndex: a
|
|
301
|
-
}),
|
|
301
|
+
}), I?.({
|
|
302
302
|
type: "run",
|
|
303
303
|
pts: e.timestampOffset,
|
|
304
304
|
from: 0
|
|
305
305
|
}), e.subtitles?.length && c.pushFragments(e.subtitles), x?.();
|
|
306
|
-
let
|
|
306
|
+
let V = !1, H = !1, U = 0, W, de = () => W ?? n.currentTime, G = !1, fe = !1, K = 0, q = 0, J = null, Y = 0, X = 0, pe = !1, me = 0, he = !0, ge = (e, t, n = !1) => {
|
|
307
307
|
let r = e?.message === "Cancelled";
|
|
308
308
|
t && r || (console.error(e), (!pe || n) && (pe = !0, ne?.(r ? o("Reading the video file failed", void 0, { cause: e }) : e)));
|
|
309
309
|
}, _e = async (e = !1) => {
|
|
310
|
-
if (
|
|
311
|
-
let t = n.currentTime, r = e ? T : S, i = e ? E : C, a = s(
|
|
310
|
+
if (U > 0 && !e) return;
|
|
311
|
+
let t = n.currentTime, r = e ? T : S, i = e ? E : C, a = s(B), o = (e, t) => (he = !1, le(e, t));
|
|
312
312
|
a.length && a[0].start < t + r && await o(0, t + r);
|
|
313
313
|
for (let { end: e } of a) e > t + i && await o(t + i, e);
|
|
314
314
|
}, ve = () => {
|
|
315
|
-
let e = s(
|
|
315
|
+
let e = s(B);
|
|
316
316
|
if (!e.length) return !0;
|
|
317
|
-
let t =
|
|
317
|
+
let t = de(), n = e.find((e) => e.start <= t + k && t < e.end);
|
|
318
318
|
return Ce() && xe(t) && (!n || n.start <= t) || n && n.end >= t + C ? !1 : !n || n.end < t + w;
|
|
319
|
-
}, ye = (e) => s(
|
|
319
|
+
}, ye = (e) => s(B).find((t) => t.start <= e + k && e < t.end), be = () => ye(n.currentTime), xe = (e = n.currentTime) => {
|
|
320
320
|
let t = ye(e);
|
|
321
321
|
return e >= X - .1 || !!t && t.end >= X - .1;
|
|
322
322
|
}, Se = () => {
|
|
323
|
-
let e = s(
|
|
324
|
-
e.length && (X = Math.max(X, ...e)),
|
|
325
|
-
}, Ce = () =>
|
|
326
|
-
|
|
323
|
+
let e = s(B).map((e) => e.end);
|
|
324
|
+
e.length && (X = Math.max(X, ...e)), G && (fe = !0);
|
|
325
|
+
}, Ce = () => G || fe && (s(B).at(-1)?.end ?? 0) >= X - .1, we = async (t) => {
|
|
326
|
+
H = !0, J = null, Y = 0, me = t, he = !0;
|
|
327
327
|
let n = ++q;
|
|
328
328
|
try {
|
|
329
|
-
let { data: r, pts: i, subtitles: a, finished: o } = await
|
|
330
|
-
if (
|
|
331
|
-
o && (
|
|
329
|
+
let { data: r, pts: i, subtitles: a, finished: o } = await L.seek(t);
|
|
330
|
+
if (z || n !== q) return;
|
|
331
|
+
o && (G = !0), a?.length && c.pushFragments(a), I?.({
|
|
332
332
|
type: "run",
|
|
333
333
|
pts: e.timestampOffset,
|
|
334
334
|
from: i
|
|
335
|
-
}), r.byteLength &&
|
|
335
|
+
}), r.byteLength && I?.({
|
|
336
336
|
type: "fragment",
|
|
337
337
|
data: r
|
|
338
|
-
}), o &&
|
|
338
|
+
}), o && I?.({ type: "end" }), r.byteLength && (await ce(r), Se());
|
|
339
339
|
} catch (e) {
|
|
340
|
-
n === q && (he = !1), ge(e,
|
|
340
|
+
n === q && (he = !1), ge(e, z || n !== q);
|
|
341
341
|
} finally {
|
|
342
|
-
n === q && (
|
|
342
|
+
n === q && (H = !1);
|
|
343
343
|
}
|
|
344
344
|
}, Te = async () => {
|
|
345
345
|
let e = J;
|
|
346
346
|
if (e) try {
|
|
347
|
-
await
|
|
347
|
+
await ce(e), Se(), J === e && (J = null, Y = 0);
|
|
348
348
|
} catch (t) {
|
|
349
349
|
if (J !== e) return;
|
|
350
350
|
if (t?.name === "QuotaExceededError") {
|
|
351
351
|
let t = await _e(!0).then(() => !0, () => !1);
|
|
352
352
|
if (J !== e) return;
|
|
353
353
|
if (t) {
|
|
354
|
-
|
|
354
|
+
G = !1, await we(be()?.end ?? n.currentTime);
|
|
355
355
|
return;
|
|
356
356
|
}
|
|
357
357
|
}
|
|
358
|
-
if (Y += 1, Y <
|
|
358
|
+
if (Y += 1, Y < D) return;
|
|
359
359
|
throw J = null, t;
|
|
360
360
|
}
|
|
361
361
|
}, Ee = async () => {
|
|
362
|
-
if (
|
|
363
|
-
let e = ++K, t = q, n = () =>
|
|
364
|
-
|
|
362
|
+
if (V || H || $ !== void 0 || z || !J && (G || !ve())) return;
|
|
363
|
+
let e = ++K, t = q, n = () => z || e !== K || t !== q;
|
|
364
|
+
V = !0;
|
|
365
365
|
try {
|
|
366
366
|
if (!J) {
|
|
367
|
-
let { data: e, subtitles: t, finished: r } = await
|
|
368
|
-
if (n() || (r && (
|
|
367
|
+
let { data: e, subtitles: t, finished: r } = await L.read();
|
|
368
|
+
if (n() || (r && (G = !0), t?.length && c.pushFragments(t), e.byteLength && I?.({
|
|
369
369
|
type: "fragment",
|
|
370
370
|
data: e
|
|
371
|
-
}), r &&
|
|
371
|
+
}), r && I?.({ type: "end" }), !e.byteLength)) return;
|
|
372
372
|
J = e, Y = 0;
|
|
373
373
|
}
|
|
374
374
|
await Te(), !n() && pe && (pe = !1, P?.());
|
|
375
375
|
} catch (e) {
|
|
376
376
|
ge(e, n());
|
|
377
377
|
} finally {
|
|
378
|
-
e === K && (
|
|
378
|
+
e === K && (V = !1);
|
|
379
379
|
}
|
|
380
|
-
}, De = (e) => s(
|
|
380
|
+
}, De = (e) => s(B).some((t) => t.start <= e && e < t.end && t.end >= me) && ke(e), Oe = (e) => s(B).some((t) => t.start <= e && e < t.end), Z = (e) => {
|
|
381
381
|
let t = ye(e);
|
|
382
382
|
return t ? t.end - e : 0;
|
|
383
|
-
}, ke = (e) => Z(e) >=
|
|
384
|
-
if (!
|
|
385
|
-
$ && clearTimeout($), $ = void 0,
|
|
383
|
+
}, ke = (e) => Z(e) >= A || Ce() && xe(e), Ae = async (e) => {
|
|
384
|
+
if (!z && !(Oe(e) && ke(e))) {
|
|
385
|
+
$ && clearTimeout($), $ = void 0, G = !1, U++, W = e;
|
|
386
386
|
try {
|
|
387
387
|
await we(e);
|
|
388
388
|
let t = q;
|
|
389
|
-
for (let n = 0; n <
|
|
389
|
+
for (let n = 0; n < ee && !(z || t !== q || G || Z(e) >= A); n++) {
|
|
390
390
|
let t = Z(e);
|
|
391
391
|
if (await Ee(), Z(e) <= t) break;
|
|
392
392
|
}
|
|
393
393
|
} finally {
|
|
394
|
-
|
|
394
|
+
U--, W === e && (W = void 0);
|
|
395
395
|
}
|
|
396
396
|
}
|
|
397
397
|
}, je = 0, Me = null, Q, Ne = (e) => {
|
|
398
398
|
let t = performance.now(), n = t - je;
|
|
399
|
-
if (n >=
|
|
400
|
-
je = t, Me = null,
|
|
399
|
+
if (n >= j) {
|
|
400
|
+
je = t, Me = null, F?.(e);
|
|
401
401
|
return;
|
|
402
402
|
}
|
|
403
403
|
Me = e, !Q && (Q = setTimeout(() => {
|
|
404
404
|
Q = void 0;
|
|
405
405
|
let e = Me;
|
|
406
|
-
Me = null, !(e === null ||
|
|
407
|
-
},
|
|
406
|
+
Me = null, !(e === null || z) && (je = performance.now(), F?.(e));
|
|
407
|
+
}, j - n));
|
|
408
408
|
};
|
|
409
|
-
|
|
409
|
+
R.push(() => {
|
|
410
410
|
Q && clearTimeout(Q);
|
|
411
411
|
});
|
|
412
412
|
let Pe = 0, $;
|
|
413
|
-
|
|
413
|
+
R.push(() => {
|
|
414
414
|
$ && clearTimeout($);
|
|
415
415
|
});
|
|
416
416
|
let Fe = () => {
|
|
417
417
|
let t = n.currentTime, r = e.info.input.duration || n.duration;
|
|
418
418
|
r > 0 && Ne(Math.min(Math.max(t / r, 0), 1));
|
|
419
|
-
let i = performance.now(), a = i - Pe <
|
|
420
|
-
if (Pe = i, !De(t) && !(he && (
|
|
421
|
-
if (
|
|
419
|
+
let i = performance.now(), a = i - Pe < te;
|
|
420
|
+
if (Pe = i, !De(t) && !(he && (H || Z(t) > 0) && Math.abs(t - me) < .001)) {
|
|
421
|
+
if (G = !1, $ && clearTimeout($), !a) {
|
|
422
422
|
$ = void 0, we(t);
|
|
423
423
|
return;
|
|
424
424
|
}
|
|
425
425
|
$ = setTimeout(() => {
|
|
426
|
-
if ($ = void 0,
|
|
426
|
+
if ($ = void 0, z) return;
|
|
427
427
|
let e = n.currentTime;
|
|
428
|
-
De(e) || (
|
|
429
|
-
},
|
|
428
|
+
De(e) || (G = !1, we(e));
|
|
429
|
+
}, te);
|
|
430
430
|
}
|
|
431
431
|
};
|
|
432
|
-
n.addEventListener("seeking", Fe),
|
|
432
|
+
n.addEventListener("seeking", Fe), R.push(() => n.removeEventListener("seeking", Fe));
|
|
433
433
|
let Ie = !1, Le = () => {
|
|
434
434
|
if (Ie) return;
|
|
435
435
|
Ie = !0;
|
|
436
436
|
let e = n.error;
|
|
437
437
|
ge(new b(`the media element failed: ${e?.message ?? "unknown"}`, { cause: e }), !1, !0);
|
|
438
438
|
};
|
|
439
|
-
n.addEventListener("error", Le),
|
|
439
|
+
n.addEventListener("error", Le), R.push(() => n.removeEventListener("error", Le));
|
|
440
440
|
let Re = setInterval(() => {
|
|
441
|
-
Ie || n.error || (_e().catch(() => {}), Ee().catch((e) => ge(e, !1)), Ce() && !J && xe() && M.readyState === "open" &&
|
|
441
|
+
Ie || n.error || (_e().catch(() => {}), Ee().catch((e) => ge(e, !1)), Ce() && !J && xe() && M.readyState === "open" && ue(() => !H).catch(() => {}));
|
|
442
442
|
}, 100);
|
|
443
|
-
return
|
|
444
|
-
destroy:
|
|
443
|
+
return R.push(() => clearInterval(Re)), {
|
|
444
|
+
destroy: oe,
|
|
445
445
|
prepareSeek: Ae,
|
|
446
446
|
videoElement: n,
|
|
447
447
|
selectSubtitleStream: (e) => c.selectStream(e),
|
|
@@ -452,10 +452,10 @@ var i = {
|
|
|
452
452
|
audioMimeType: e.info.output.audioMimeType
|
|
453
453
|
};
|
|
454
454
|
} catch (e) {
|
|
455
|
-
throw
|
|
455
|
+
throw oe(), e;
|
|
456
456
|
}
|
|
457
|
-
}, P = 5,
|
|
458
|
-
let { publicPath: n, workerUrl: r, length: i, read: a, onThumbnails: o, width: s =
|
|
457
|
+
}, P = 5, F = 500, re = 320, ie = 1e6, I = 3, ae = 1e4, L = 1.5, R = 4e6, z = 3e4, oe = async (e) => {
|
|
458
|
+
let { publicPath: n, workerUrl: r, length: i, read: a, onThumbnails: o, width: s = re } = e, c = await t({
|
|
459
459
|
publicPath: n,
|
|
460
460
|
workerUrl: r,
|
|
461
461
|
workerOptions: { type: "module" },
|
|
@@ -463,7 +463,7 @@ var i = {
|
|
|
463
463
|
read: a
|
|
464
464
|
});
|
|
465
465
|
try {
|
|
466
|
-
let t = await c.init(), n = t.duration, r = Math.max(e.interval ?? P, n /
|
|
466
|
+
let t = await c.init(), n = t.duration, r = Math.max(e.interval ?? P, n / F), a = [], l = (e) => {
|
|
467
467
|
let t = [];
|
|
468
468
|
for (let [a, o] of e.entries()) {
|
|
469
469
|
let s = t.at(-1);
|
|
@@ -471,7 +471,7 @@ var i = {
|
|
|
471
471
|
timestamp: o.timestamp,
|
|
472
472
|
endTime: n,
|
|
473
473
|
startByte: o.pos,
|
|
474
|
-
endByte: Math.min((e[a + 1]?.pos ?? i) +
|
|
474
|
+
endByte: Math.min((e[a + 1]?.pos ?? i) + ie, i),
|
|
475
475
|
done: !1,
|
|
476
476
|
attempts: 0
|
|
477
477
|
});
|
|
@@ -510,7 +510,7 @@ var i = {
|
|
|
510
510
|
}), o(e);
|
|
511
511
|
}, x = async (e) => {
|
|
512
512
|
if (d) return;
|
|
513
|
-
let t = await Promise.race([c.readKeyframe(e.timestamp), new Promise((e, t) => setTimeout(() => t(/* @__PURE__ */ Error("timed out")),
|
|
513
|
+
let t = await Promise.race([c.readKeyframe(e.timestamp), new Promise((e, t) => setTimeout(() => t(/* @__PURE__ */ Error("timed out")), ae))]), n = await createImageBitmap(new Blob([t], { type: "image/png" })), r = new OffscreenCanvas(s, Math.max(1, Math.round(n.height * (s / n.width))));
|
|
514
514
|
r.getContext("2d").drawImage(n, 0, 0, r.width, r.height), n.close();
|
|
515
515
|
let i = await r.convertToBlob({
|
|
516
516
|
type: "image/webp",
|
|
@@ -534,7 +534,7 @@ var i = {
|
|
|
534
534
|
if (!f.length) return;
|
|
535
535
|
let e = f.splice(y(), 1)[0];
|
|
536
536
|
p = !0, x(e).catch(() => {
|
|
537
|
-
e.attempts += 1, e.done = e.attempts >=
|
|
537
|
+
e.attempts += 1, e.done = e.attempts >= I;
|
|
538
538
|
}).finally(() => {
|
|
539
539
|
p = !1, S();
|
|
540
540
|
});
|
|
@@ -543,7 +543,7 @@ var i = {
|
|
|
543
543
|
}, w = async (e) => {
|
|
544
544
|
_ = !0;
|
|
545
545
|
try {
|
|
546
|
-
let t = await Promise.race([c.init(), new Promise((e, t) => setTimeout(() => t(/* @__PURE__ */ Error("timed out")),
|
|
546
|
+
let t = await Promise.race([c.init(), new Promise((e, t) => setTimeout(() => t(/* @__PURE__ */ Error("timed out")), z))]);
|
|
547
547
|
if (d) return;
|
|
548
548
|
h = e;
|
|
549
549
|
let n = l(t.indexes);
|
|
@@ -569,7 +569,7 @@ var i = {
|
|
|
569
569
|
if (d) return;
|
|
570
570
|
v = e;
|
|
571
571
|
let t = T(e);
|
|
572
|
-
h < 0 ? h = t : (t >= Math.max(h *
|
|
572
|
+
h < 0 ? h = t : (t >= Math.max(h * L, h + R) || t >= i && h < i) && (g = !0), E(e), S();
|
|
573
573
|
},
|
|
574
574
|
prioritize: (e) => {
|
|
575
575
|
d || (m = e);
|
|
@@ -583,130 +583,136 @@ var i = {
|
|
|
583
583
|
} catch (e) {
|
|
584
584
|
throw N(c), e;
|
|
585
585
|
}
|
|
586
|
-
},
|
|
587
|
-
|
|
586
|
+
}, se = 1280, B = 1920, ce = 100, le = 1e3 / 60, ue = 250, V = 2, H = 100, U = 1e3, W, de = (e) => {
|
|
587
|
+
W?.close();
|
|
588
588
|
let t = () => {
|
|
589
|
-
clearTimeout(r), e.mirror.removeEventListener("leavepictureinpicture", n),
|
|
589
|
+
clearTimeout(r), e.mirror.removeEventListener("leavepictureinpicture", n), W = void 0;
|
|
590
590
|
}, n = () => {
|
|
591
591
|
t(), document.pictureInPictureElement === e.mirror && document.exitPictureInPicture().catch(() => {});
|
|
592
592
|
for (let t of e.stream.getTracks()) t.stop();
|
|
593
593
|
e.mirror.remove();
|
|
594
|
-
}, r = setTimeout(n,
|
|
595
|
-
e.mirror.addEventListener("leavepictureinpicture", n),
|
|
594
|
+
}, r = setTimeout(n, U);
|
|
595
|
+
e.mirror.addEventListener("leavepictureinpicture", n), W = {
|
|
596
596
|
session: e,
|
|
597
597
|
close: n,
|
|
598
598
|
cancel: t
|
|
599
599
|
};
|
|
600
|
-
},
|
|
601
|
-
let e =
|
|
600
|
+
}, G = () => {
|
|
601
|
+
let e = W;
|
|
602
602
|
return e?.cancel(), e?.session;
|
|
603
|
-
},
|
|
604
|
-
let { video: t, subtitles: n } = e, r = e.mode ??
|
|
603
|
+
}, fe = () => typeof HTMLCanvasElement < "u" && "captureStream" in HTMLCanvasElement.prototype, K = () => typeof document < "u" && typeof HTMLVideoElement < "u" && typeof HTMLVideoElement.prototype.requestPictureInPicture == "function" && !!document.pictureInPictureEnabled, q = () => typeof HTMLVideoElement < "u" && "mozCaptureStream" in HTMLVideoElement.prototype, J = () => fe() ? K() ? "window" : q() ? "burn-in" : null : null, Y = (e) => {
|
|
604
|
+
let { video: t, subtitles: n } = e, r = e.mode ?? J() ?? "window", i = e.maxWidth ?? (r === "burn-in" ? B : se), a, o, s, c = 0, l = 0, u = 0, d = !1, f = !1, p = !1, m = !1, h, g = () => r === "burn-in" ? !!a : !!a && document.pictureInPictureElement === a.mirror, _ = (e, r) => {
|
|
605
605
|
r.drawImage(t, 0, 0, e.width, e.height);
|
|
606
606
|
let i = n.querySelector("canvas");
|
|
607
|
-
i && i.width > 0 && i.height > 0 && r.drawImage(i, 0, 0, e.width, e.height);
|
|
608
|
-
},
|
|
607
|
+
i && i.width > 0 && i.height > 0 && Math.abs(t.currentTime - u) <= V && r.drawImage(i, 0, 0, e.width, e.height);
|
|
608
|
+
}, v = () => {
|
|
609
609
|
let e = Math.min(t.videoWidth || i, i);
|
|
610
610
|
return {
|
|
611
611
|
width: e,
|
|
612
612
|
height: Math.round(e * ((t.videoHeight || 9) / (t.videoWidth || 16)))
|
|
613
613
|
};
|
|
614
|
-
},
|
|
614
|
+
}, y = () => {
|
|
615
615
|
if (!a) return;
|
|
616
|
-
let { composite: e, context: n } = a, { width: r, height: i } =
|
|
616
|
+
let { composite: e, context: n } = a, { width: r, height: i } = v();
|
|
617
617
|
t.videoWidth && (e.width !== r || e.height !== i) && Object.assign(e, {
|
|
618
618
|
width: r,
|
|
619
619
|
height: i
|
|
620
|
-
}),
|
|
621
|
-
},
|
|
622
|
-
|
|
623
|
-
}, y = () => {
|
|
624
|
-
!d && a && (o = _ ? t.requestVideoFrameCallback(v) : requestAnimationFrame(v));
|
|
625
|
-
}, b = () => {
|
|
626
|
-
o !== void 0 && (_ ? t.cancelVideoFrameCallback(o) : cancelAnimationFrame(o)), s !== void 0 && clearInterval(s), o = void 0, s = void 0;
|
|
627
|
-
}, x = () => {
|
|
628
|
-
"mediaSession" in navigator && (navigator.mediaSession.playbackState = t.paused ? "paused" : "playing");
|
|
620
|
+
}), l = performance.now(), _(e, n);
|
|
621
|
+
}, b = "requestVideoFrameCallback" in t, x = () => {
|
|
622
|
+
c = performance.now(), u = t.currentTime, y(), S();
|
|
629
623
|
}, S = () => {
|
|
630
|
-
|
|
624
|
+
!m && a && (o = b ? t.requestVideoFrameCallback(x) : requestAnimationFrame(x));
|
|
631
625
|
}, C = () => {
|
|
632
|
-
|
|
633
|
-
c = !1;
|
|
634
|
-
})));
|
|
626
|
+
o !== void 0 && (b ? t.cancelVideoFrameCallback(o) : cancelAnimationFrame(o)), s !== void 0 && clearInterval(s), o = void 0, s = void 0;
|
|
635
627
|
}, w = () => {
|
|
636
|
-
|
|
628
|
+
"mediaSession" in navigator && (navigator.mediaSession.playbackState = t.paused ? "paused" : "playing");
|
|
637
629
|
}, T = () => {
|
|
638
|
-
|
|
630
|
+
d || f || !t.paused || t.play().catch(() => {});
|
|
639
631
|
}, E = () => {
|
|
640
|
-
|
|
641
|
-
|
|
632
|
+
d || f || (t.paused || t.pause(), r !== "burn-in" && (d = !0, a?.mirror.play().catch(() => {}).finally(() => {
|
|
633
|
+
d = !1;
|
|
634
|
+
})));
|
|
635
|
+
}, D = () => {
|
|
636
|
+
w(), a?.mirror.play().catch(() => {});
|
|
637
|
+
}, O = () => {
|
|
638
|
+
w(), r === "burn-in" && a?.mirror.pause();
|
|
639
|
+
}, k = () => {
|
|
640
|
+
if (r !== "burn-in" || !a || !t.paused || f) return;
|
|
641
|
+
f = !0;
|
|
642
642
|
let { mirror: e } = a;
|
|
643
|
-
|
|
644
|
-
setTimeout(e,
|
|
643
|
+
y(), e.play().then(() => new Promise((e) => {
|
|
644
|
+
setTimeout(e, H);
|
|
645
645
|
})).catch(() => {}).finally(() => {
|
|
646
646
|
e.pause(), setTimeout(() => {
|
|
647
|
-
|
|
648
|
-
},
|
|
647
|
+
f = !1;
|
|
648
|
+
}, H);
|
|
649
649
|
});
|
|
650
|
-
},
|
|
650
|
+
}, A = [["play", () => {
|
|
651
651
|
t.play().catch(() => {});
|
|
652
|
-
}], ["pause", () => t.pause()]],
|
|
653
|
-
if ("mediaSession" in navigator) for (let [t, n] of
|
|
652
|
+
}], ["pause", () => t.pause()]], ee = (e) => {
|
|
653
|
+
if ("mediaSession" in navigator) for (let [t, n] of A) try {
|
|
654
654
|
navigator.mediaSession.setActionHandler(t, e ? n : null);
|
|
655
655
|
} catch {}
|
|
656
|
-
},
|
|
657
|
-
|
|
658
|
-
|
|
656
|
+
}, j = () => {
|
|
657
|
+
u = t.currentTime, S(), s = setInterval(() => {
|
|
658
|
+
let e = performance.now();
|
|
659
|
+
e - c < ce || t.paused && e - l < ue || y();
|
|
659
660
|
}, le);
|
|
660
|
-
},
|
|
661
|
-
e.addEventListener("play",
|
|
662
|
-
},
|
|
663
|
-
|
|
661
|
+
}, te = (e) => {
|
|
662
|
+
e.addEventListener("play", T), e.addEventListener("pause", E), e.addEventListener("leavepictureinpicture", ne), t.addEventListener("play", D), t.addEventListener("pause", O), t.addEventListener("seeked", k), ee(!0), w();
|
|
663
|
+
}, M = () => {
|
|
664
|
+
C(), ee(!1), t.removeEventListener("play", D), t.removeEventListener("pause", O), t.removeEventListener("seeked", k);
|
|
664
665
|
let e = a;
|
|
665
|
-
return a = void 0, e?.mirror.removeEventListener("play",
|
|
666
|
-
},
|
|
667
|
-
let e =
|
|
666
|
+
return a = void 0, e?.mirror.removeEventListener("play", T), e?.mirror.removeEventListener("pause", E), e?.mirror.removeEventListener("leavepictureinpicture", ne), e;
|
|
667
|
+
}, N = () => {
|
|
668
|
+
let e = M();
|
|
668
669
|
if (e) {
|
|
669
670
|
for (let t of e.stream.getTracks()) t.stop();
|
|
670
671
|
e.mirror.remove();
|
|
671
672
|
}
|
|
672
|
-
|
|
673
|
-
},
|
|
673
|
+
h?.();
|
|
674
|
+
}, ne = () => N(), P = () => {
|
|
675
|
+
let e = t.style.visibility;
|
|
676
|
+
t.style.visibility = "hidden", h = () => {
|
|
677
|
+
h = void 0, t.style.visibility = e;
|
|
678
|
+
};
|
|
679
|
+
}, F = (e) => {
|
|
674
680
|
let r = t.style.opacity, i = n.style.display;
|
|
675
|
-
e.style.cssText = "position:absolute;inset:0;width:100%;height:100%;object-fit:contain;background:#000;z-index:1", t.style.opacity = "0", n.style.display = "none",
|
|
676
|
-
|
|
681
|
+
e.style.cssText = "position:absolute;inset:0;width:100%;height:100%;object-fit:contain;background:#000;z-index:1", t.style.opacity = "0", n.style.display = "none", h = () => {
|
|
682
|
+
h = void 0, t.style.opacity = r, n.style.display = i;
|
|
677
683
|
};
|
|
678
|
-
},
|
|
679
|
-
let n = Object.assign(document.createElement("canvas"),
|
|
684
|
+
}, re = async () => {
|
|
685
|
+
let n = Object.assign(document.createElement("canvas"), v()), i = n.getContext("2d");
|
|
680
686
|
if (!i) throw Error("the picture in picture composite has no 2d context");
|
|
681
687
|
let o = document.createElement("video");
|
|
682
|
-
o.muted = !0, o.playsInline = !0, r === "burn-in" ?
|
|
688
|
+
o.muted = !0, o.playsInline = !0, r === "burn-in" ? F(o) : o.style.cssText = "position:fixed;width:1px;height:1px;opacity:0;pointer-events:none;left:-1px;top:-1px", _(n, i);
|
|
683
689
|
let s = {
|
|
684
690
|
composite: n,
|
|
685
691
|
context: i,
|
|
686
692
|
stream: n.captureStream(),
|
|
687
693
|
mirror: o
|
|
688
694
|
};
|
|
689
|
-
if (a = s, o.srcObject = s.stream, (e.container ?? (r === "burn-in" ? t.parentElement : null) ?? document.body).appendChild(o),
|
|
695
|
+
if (a = s, o.srcObject = s.stream, (e.container ?? (r === "burn-in" ? t.parentElement : null) ?? document.body).appendChild(o), j(), o.play().catch(() => {}), o.readyState === 0 && await new Promise((e) => {
|
|
690
696
|
let t = () => {
|
|
691
697
|
clearTimeout(n), e();
|
|
692
698
|
}, n = setTimeout(t, 1e3);
|
|
693
699
|
o.addEventListener("loadedmetadata", t, { once: !0 });
|
|
694
|
-
}),
|
|
700
|
+
}), m || a !== s) {
|
|
695
701
|
o.remove();
|
|
696
702
|
return;
|
|
697
703
|
}
|
|
698
|
-
if (
|
|
704
|
+
if (te(o), r === "burn-in") {
|
|
699
705
|
t.paused && o.pause(), e.onBurnedInChange?.(!0);
|
|
700
706
|
return;
|
|
701
707
|
}
|
|
702
|
-
await o.requestPictureInPicture();
|
|
703
|
-
},
|
|
704
|
-
if (!(
|
|
705
|
-
|
|
708
|
+
await o.requestPictureInPicture(), a === s && P();
|
|
709
|
+
}, ie = async () => {
|
|
710
|
+
if (!(m || p)) {
|
|
711
|
+
p = !0;
|
|
706
712
|
try {
|
|
707
|
-
if (
|
|
713
|
+
if (g()) {
|
|
708
714
|
if (r === "burn-in") {
|
|
709
|
-
|
|
715
|
+
N(), e.onBurnedInChange?.(!1);
|
|
710
716
|
return;
|
|
711
717
|
}
|
|
712
718
|
await document.exitPictureInPicture().catch(() => {});
|
|
@@ -714,23 +720,26 @@ var i = {
|
|
|
714
720
|
}
|
|
715
721
|
r !== "burn-in" && document.pictureInPictureElement && await document.exitPictureInPicture().catch(() => {});
|
|
716
722
|
try {
|
|
717
|
-
await
|
|
723
|
+
await re();
|
|
718
724
|
} catch (t) {
|
|
719
|
-
console.warn("subtitle compositing for picture in picture failed", t),
|
|
725
|
+
console.warn("subtitle compositing for picture in picture failed", t), N(), r === "burn-in" && e.onBurnedInChange?.(!1);
|
|
720
726
|
}
|
|
721
727
|
} finally {
|
|
722
|
-
|
|
728
|
+
p = !1;
|
|
723
729
|
}
|
|
724
730
|
}
|
|
725
|
-
},
|
|
726
|
-
return
|
|
727
|
-
toggle:
|
|
731
|
+
}, I = r === "window" ? G() : void 0;
|
|
732
|
+
return I && (a = I, I.context.fillRect(0, 0, I.composite.width, I.composite.height), j(), te(I.mirror), P()), {
|
|
733
|
+
toggle: ie,
|
|
728
734
|
mode: r,
|
|
729
735
|
destroy: () => {
|
|
730
|
-
if (
|
|
731
|
-
|
|
736
|
+
if (m = !0, r === "window" && g()) {
|
|
737
|
+
let e = M();
|
|
738
|
+
return h?.(), de(e);
|
|
739
|
+
}
|
|
740
|
+
N(), r === "burn-in" && e.onBurnedInChange?.(!1);
|
|
732
741
|
}
|
|
733
742
|
};
|
|
734
743
|
};
|
|
735
744
|
//#endregion
|
|
736
|
-
export { b as a, N as c, s as d, l as f, M as i, u as l, a as m,
|
|
745
|
+
export { b as a, N as c, s as d, l as f, M as i, u as l, a as m, J as n, x as o, i as p, oe as r, ne as s, Y 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-eF2uRLyY.js";
|
|
2
2
|
import { languageOf as u, playsIn as d, startingTracks as f } from "./language/index.js";
|
|
3
3
|
import { n as p, r as m, t as h } from "./chapters-EBYXcEml.js";
|
|
4
4
|
import { Children as g, Fragment as _, createContext as v, isValidElement as y, useCallback as b, useContext as x, useEffect as S, useMemo as C, useRef as w, useState as T, useSyncExternalStore as E } from "react";
|
package/package.json
CHANGED
|
@@ -13,8 +13,23 @@ const DEFAULT_MAX_WIDTH = 1280
|
|
|
13
13
|
* thumbnail-sized window does. A 4K source is still halved; see the note on `compositeWidth`.
|
|
14
14
|
*/
|
|
15
15
|
const BURN_IN_MAX_WIDTH = 1920
|
|
16
|
-
/**
|
|
17
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Frame callbacks follow the BROWSER window's rendering, not the picture in picture window's: none with that window
|
|
18
|
+
* minimized or off screen (one a second on niri), while the picture in picture window is still shown. A timer paints
|
|
19
|
+
* once they have been silent this long. A paused video presents no frames either, and is repainted only every
|
|
20
|
+
* `PAUSED_REPAINT_MS`, for a seek or a subtitle change. `scripts/measure-picture-in-picture.mjs`, 2026-11-09, Chrome 156
|
|
21
|
+
* on weston, a 24 fps video, minimized or off screen: 0 different frames a second in the window before, 24 with this
|
|
22
|
+
* and `hidePicture`.
|
|
23
|
+
*/
|
|
24
|
+
const STALLED_MS = 100
|
|
25
|
+
const REPAINT_TICK_MS = 1000 / 60
|
|
26
|
+
const PAUSED_REPAINT_MS = 250
|
|
27
|
+
/**
|
|
28
|
+
* The subtitle surface is rendered on the same frame callbacks, from a worker whose frames reach the page only while
|
|
29
|
+
* the page renders, so with the browser window off screen it keeps its last line. Drawn only while it is at most this
|
|
30
|
+
* far behind the video's time, which a page rendering once a second still is.
|
|
31
|
+
*/
|
|
32
|
+
const STALE_SUBTITLES_S = 2
|
|
18
33
|
/** Long enough for a paused mirror to present one frame of a fresh seek before it stops again. */
|
|
19
34
|
const FLUSH_FRAME_MS = 100
|
|
20
35
|
|
|
@@ -139,7 +154,11 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
139
154
|
|
|
140
155
|
let session: Session | undefined
|
|
141
156
|
let handle: number | undefined
|
|
142
|
-
let
|
|
157
|
+
let repaint: ReturnType<typeof setInterval> | undefined
|
|
158
|
+
let lastFrame = 0
|
|
159
|
+
let lastPaint = 0
|
|
160
|
+
/** The video's time at the last frame callback, which the subtitle surface was rendered for. */
|
|
161
|
+
let frameTime = 0
|
|
143
162
|
let syncing = false
|
|
144
163
|
/** Burn-in only: a deliberate play/pause pair to push one frame, which must not reach the video. */
|
|
145
164
|
let flushing = false
|
|
@@ -162,7 +181,7 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
162
181
|
// jassub has transferred reports the size of the last frame its worker committed, so the zero
|
|
163
182
|
// check still keeps drawImage off a source with no dimensions to read.
|
|
164
183
|
const surface = subtitles.querySelector('canvas')
|
|
165
|
-
if (surface && surface.width > 0 && surface.height > 0) {
|
|
184
|
+
if (surface && surface.width > 0 && surface.height > 0 && Math.abs(video.currentTime - frameTime) <= STALE_SUBTITLES_S) {
|
|
166
185
|
context.drawImage(surface, 0, 0, composite.width, composite.height)
|
|
167
186
|
}
|
|
168
187
|
}
|
|
@@ -181,12 +200,15 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
181
200
|
if (video.videoWidth && (composite.width !== width || composite.height !== height)) {
|
|
182
201
|
Object.assign(composite, { width, height })
|
|
183
202
|
}
|
|
203
|
+
lastPaint = performance.now()
|
|
184
204
|
draw(composite, context)
|
|
185
205
|
}
|
|
186
206
|
|
|
187
207
|
const useFrameCallback = 'requestVideoFrameCallback' in video
|
|
188
208
|
|
|
189
209
|
const loop = () => {
|
|
210
|
+
lastFrame = performance.now()
|
|
211
|
+
frameTime = video.currentTime
|
|
190
212
|
paint()
|
|
191
213
|
schedule()
|
|
192
214
|
}
|
|
@@ -201,9 +223,9 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
201
223
|
if (useFrameCallback) video.cancelVideoFrameCallback(handle)
|
|
202
224
|
else cancelAnimationFrame(handle)
|
|
203
225
|
}
|
|
204
|
-
if (
|
|
226
|
+
if (repaint !== undefined) clearInterval(repaint)
|
|
205
227
|
handle = undefined
|
|
206
|
-
|
|
228
|
+
repaint = undefined
|
|
207
229
|
}
|
|
208
230
|
|
|
209
231
|
const publishState = () => {
|
|
@@ -286,8 +308,13 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
286
308
|
}
|
|
287
309
|
|
|
288
310
|
const start = () => {
|
|
311
|
+
frameTime = video.currentTime
|
|
289
312
|
schedule()
|
|
290
|
-
|
|
313
|
+
repaint = setInterval(() => {
|
|
314
|
+
const now = performance.now()
|
|
315
|
+
if (now - lastFrame < STALLED_MS || (video.paused && now - lastPaint < PAUSED_REPAINT_MS)) return
|
|
316
|
+
paint()
|
|
317
|
+
}, REPAINT_TICK_MS)
|
|
291
318
|
}
|
|
292
319
|
|
|
293
320
|
const listen = (mirror: HTMLVideoElement) => {
|
|
@@ -330,6 +357,21 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
330
357
|
|
|
331
358
|
const onLeave = () => teardown()
|
|
332
359
|
|
|
360
|
+
/**
|
|
361
|
+
* Window only, while it is open. A video shown in the page moves its picture on the page's rendering only, so with the
|
|
362
|
+
* browser window minimized or off screen every read got the same frame for 250 ms: 4 different frames a second
|
|
363
|
+
* however fast the timer painted. A hidden one gives a fresh frame on each read. Chrome's own picture in picture
|
|
364
|
+
* leaves only a placeholder in the page too.
|
|
365
|
+
*/
|
|
366
|
+
const hidePicture = () => {
|
|
367
|
+
const visibility = video.style.visibility
|
|
368
|
+
video.style.visibility = 'hidden'
|
|
369
|
+
restore = () => {
|
|
370
|
+
restore = undefined
|
|
371
|
+
video.style.visibility = visibility
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
333
375
|
/**
|
|
334
376
|
* Swap the composite in as the player's picture.
|
|
335
377
|
*
|
|
@@ -409,6 +451,7 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
409
451
|
}
|
|
410
452
|
|
|
411
453
|
await mirror.requestPictureInPicture()
|
|
454
|
+
if (session === mine) hidePicture()
|
|
412
455
|
}
|
|
413
456
|
|
|
414
457
|
const toggle = async () => {
|
|
@@ -450,6 +493,7 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
450
493
|
taken.context.fillRect(0, 0, taken.composite.width, taken.composite.height)
|
|
451
494
|
start()
|
|
452
495
|
listen(taken.mirror)
|
|
496
|
+
hidePicture()
|
|
453
497
|
}
|
|
454
498
|
|
|
455
499
|
return {
|
|
@@ -457,7 +501,11 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
457
501
|
mode,
|
|
458
502
|
destroy: () => {
|
|
459
503
|
destroyed = true
|
|
460
|
-
if (mode === 'window' && active())
|
|
504
|
+
if (mode === 'window' && active()) {
|
|
505
|
+
const held = detach()!
|
|
506
|
+
restore?.()
|
|
507
|
+
return handOver(held)
|
|
508
|
+
}
|
|
461
509
|
teardown()
|
|
462
510
|
if (mode === 'burn-in') options.onBurnedInChange?.(false)
|
|
463
511
|
},
|