@banou/media-player 0.10.36 → 0.10.37
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
|
@@ -234,8 +234,12 @@ 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
|
-
|
|
238
|
-
|
|
237
|
+
The window outlives the player. A player unmounted while its window is open leaves it open for a
|
|
238
|
+
second, and the next player mounted in the same document in that time plays in it, at the shape of
|
|
239
|
+
its own video. So an app that mounts a new player for the next episode keeps the window without a
|
|
240
|
+
click, which a new window would need. If no player takes the window in that second, it closes.
|
|
241
|
+
|
|
242
|
+
Where that path is unavailable the player offers no picture in picture control.
|
|
239
243
|
|
|
240
244
|
### Picture in picture for a media you do not own
|
|
241
245
|
|
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-x5oIkHAN.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 };
|
|
@@ -24,7 +24,10 @@ export type PictureInPictureOptions = {
|
|
|
24
24
|
*/
|
|
25
25
|
subtitles: HTMLElement;
|
|
26
26
|
maxWidth?: number;
|
|
27
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* Where the mirror is mounted. It must be in the document. Defaults to the video's parent in burn-in, where the
|
|
29
|
+
* mirror is the picture, and to the body in a window, so the window can outlive the player (see `destroy`).
|
|
30
|
+
*/
|
|
28
31
|
container?: HTMLElement;
|
|
29
32
|
/** Overrides detection. Passing a mode the browser cannot honour is the caller's problem. */
|
|
30
33
|
mode?: PictureInPictureMode;
|
|
@@ -33,9 +36,17 @@ export type PictureInPictureOptions = {
|
|
|
33
36
|
};
|
|
34
37
|
export type PictureInPictureController = {
|
|
35
38
|
toggle: () => Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Burn-in, or a window that is not open, ends here. An open window stays open for a second, and the
|
|
41
|
+
* next controller created in the document in that time draws its own video into it, at that video's
|
|
42
|
+
* shape: an app that replaces the player for the next episode keeps the window with no click, which
|
|
43
|
+
* a new window would need. If no controller takes it in that second, it closes.
|
|
44
|
+
*/
|
|
36
45
|
destroy: () => void;
|
|
37
46
|
mode: PictureInPictureMode;
|
|
38
47
|
};
|
|
48
|
+
/** How long a destroyed controller's window waits for the next one; a remounted player asks within a few frames. */
|
|
49
|
+
export declare const HAND_OVER_MS = 1000;
|
|
39
50
|
/**
|
|
40
51
|
* null means offer no control at all. That covers Safari, whose picture in picture is
|
|
41
52
|
* `webkitSetPresentationMode` and not the W3C API, so neither arm here can drive it. A dead button
|
|
@@ -131,11 +131,11 @@ var i = {
|
|
|
131
131
|
}, !0);
|
|
132
132
|
}).catch(C);
|
|
133
133
|
}));
|
|
134
|
-
},
|
|
134
|
+
}, ee = [
|
|
135
135
|
"loadedmetadata",
|
|
136
136
|
"seeked",
|
|
137
137
|
"pause"
|
|
138
|
-
],
|
|
138
|
+
], D = () => E(), O = (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 ee) t.addEventListener(e, D);
|
|
162
162
|
s && w((e) => {
|
|
163
163
|
u = Promise.resolve(e.addFonts([s])).then(E, C);
|
|
164
164
|
}), E();
|
|
165
|
-
},
|
|
165
|
+
}, k = (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 && O(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)))), k(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
|
+
}), k(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 ee) t.removeEventListener(e, D);
|
|
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, ee = 5, D = 15e3, O = 1, k = 3, te = 12, A = 200, j = 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
|
-
},
|
|
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 =
|
|
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: re, onSubtitleStreams: ie, onAudioStreams: ae, onOutput: F } = t, oe = (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
|
+
}), I = await oe(_), L = [], R = !1, se = () => {
|
|
244
|
+
if (!R) {
|
|
245
|
+
R = !0;
|
|
246
|
+
for (let e of [...L].reverse()) try {
|
|
247
247
|
e();
|
|
248
248
|
} catch {}
|
|
249
249
|
}
|
|
250
250
|
};
|
|
251
|
-
|
|
251
|
+
L.push(() => N(I));
|
|
252
252
|
try {
|
|
253
|
-
let e = await
|
|
254
|
-
_ === void 0 && t !== void 0 && t !== e.audioStreams?.[0]?.streamIndex && (
|
|
253
|
+
let e = await I.init(), t = _ ?? v?.(e.audioStreams ?? []);
|
|
254
|
+
_ === void 0 && t !== void 0 && t !== e.audioStreams?.[0]?.streamIndex && (N(I), I = await oe(t), e = await I.init());
|
|
255
255
|
let i = e.audioStreams ?? [], a = i.some((e) => e.streamIndex === t) ? t : i[0]?.streamIndex ?? -1;
|
|
256
|
-
|
|
256
|
+
ae?.(i, a);
|
|
257
257
|
let c = y({
|
|
258
258
|
video: n,
|
|
259
259
|
container: r,
|
|
@@ -262,186 +262,186 @@ var i = {
|
|
|
262
262
|
legacyWasmUrl: p,
|
|
263
263
|
defaultFontUrl: m,
|
|
264
264
|
cjkFontUrl: h,
|
|
265
|
-
onStreams:
|
|
265
|
+
onStreams: ie
|
|
266
266
|
});
|
|
267
|
-
if (
|
|
267
|
+
if (L.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
|
|
271
|
-
|
|
270
|
+
let M = new MediaSource(), ce = URL.createObjectURL(M);
|
|
271
|
+
L.push(() => URL.revokeObjectURL(ce)), n.src = ce, L.push(() => {
|
|
272
272
|
try {
|
|
273
273
|
n.pause();
|
|
274
274
|
} catch {}
|
|
275
275
|
n.removeAttribute("src"), n.load();
|
|
276
276
|
});
|
|
277
|
-
let
|
|
277
|
+
let z = await new Promise((t, n) => {
|
|
278
278
|
let r = setTimeout(() => {
|
|
279
279
|
i(), n(o("The MediaSource never opened"));
|
|
280
|
-
},
|
|
281
|
-
clearTimeout(r),
|
|
280
|
+
}, D), i = () => {
|
|
281
|
+
clearTimeout(r), M.removeEventListener("sourceopen", a), M.removeEventListener("sourceclose", s), M.removeEventListener("error", s);
|
|
282
282
|
}, a = () => {
|
|
283
283
|
try {
|
|
284
|
-
let n =
|
|
284
|
+
let n = M.addSourceBuffer(g);
|
|
285
285
|
n.mode = "segments", n.timestampOffset = e.timestampOffset;
|
|
286
286
|
let r = e.info.input.duration;
|
|
287
|
-
Number.isFinite(r) && r > 0 && (
|
|
287
|
+
Number.isFinite(r) && r > 0 && (M.duration = r), i(), t(n);
|
|
288
288
|
} catch (e) {
|
|
289
289
|
i(), n(e);
|
|
290
290
|
}
|
|
291
291
|
}, s = () => {
|
|
292
292
|
i(), n(o("The MediaSource closed before a SourceBuffer could be created"));
|
|
293
293
|
};
|
|
294
|
-
|
|
295
|
-
}), { appendBuffer:
|
|
296
|
-
await
|
|
294
|
+
M.addEventListener("sourceopen", a, { once: !0 }), M.addEventListener("sourceclose", s, { once: !0 }), M.addEventListener("error", s, { once: !0 });
|
|
295
|
+
}), { appendBuffer: le, unbufferRange: ue, endOfStream: de } = l(z, M);
|
|
296
|
+
await le(e.data), F?.({
|
|
297
297
|
type: "init",
|
|
298
298
|
data: e.data,
|
|
299
299
|
audioMimeType: e.info.output.audioMimeType,
|
|
300
300
|
audioStreamIndex: a
|
|
301
|
-
}),
|
|
301
|
+
}), F?.({
|
|
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 B = !1, V = !1, H = 0, U, fe = () => U ?? n.currentTime, W = !1, G = !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
|
-
t && r || (console.error(e), (!
|
|
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 (H > 0 && !e) return;
|
|
311
|
+
let t = n.currentTime, r = e ? T : S, i = e ? E : C, a = s(z), o = (e, t) => (he = !1, ue(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(z);
|
|
316
316
|
if (!e.length) return !0;
|
|
317
|
-
let t =
|
|
317
|
+
let t = fe(), n = e.find((e) => e.start <= t + O && 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(z).find((t) => t.start <= e + O && e < t.end), be = () => ye(n.currentTime), xe = (e = n.currentTime) => {
|
|
320
320
|
let t = ye(e);
|
|
321
|
-
return e >=
|
|
321
|
+
return e >= X - .1 || !!t && t.end >= X - .1;
|
|
322
322
|
}, Se = () => {
|
|
323
|
-
let e = s(
|
|
324
|
-
e.length && (
|
|
325
|
-
}, Ce = () =>
|
|
326
|
-
|
|
327
|
-
let n = ++
|
|
323
|
+
let e = s(z).map((e) => e.end);
|
|
324
|
+
e.length && (X = Math.max(X, ...e)), W && (G = !0);
|
|
325
|
+
}, Ce = () => W || G && (s(z).at(-1)?.end ?? 0) >= X - .1, we = async (t) => {
|
|
326
|
+
V = !0, J = null, Y = 0, me = t, he = !0;
|
|
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 I.seek(t);
|
|
330
|
+
if (R || n !== q) return;
|
|
331
|
+
o && (W = !0), a?.length && c.pushFragments(a), F?.({
|
|
332
332
|
type: "run",
|
|
333
333
|
pts: e.timestampOffset,
|
|
334
334
|
from: i
|
|
335
|
-
}), r.byteLength &&
|
|
335
|
+
}), r.byteLength && F?.({
|
|
336
336
|
type: "fragment",
|
|
337
337
|
data: r
|
|
338
|
-
}), o &&
|
|
338
|
+
}), o && F?.({ type: "end" }), r.byteLength && (await le(r), Se());
|
|
339
339
|
} catch (e) {
|
|
340
|
-
n ===
|
|
340
|
+
n === q && (he = !1), ge(e, R || n !== q);
|
|
341
341
|
} finally {
|
|
342
|
-
n ===
|
|
342
|
+
n === q && (V = !1);
|
|
343
343
|
}
|
|
344
344
|
}, Te = async () => {
|
|
345
|
-
let e =
|
|
345
|
+
let e = J;
|
|
346
346
|
if (e) try {
|
|
347
|
-
await
|
|
347
|
+
await le(e), Se(), J === e && (J = null, Y = 0);
|
|
348
348
|
} catch (t) {
|
|
349
|
-
if (
|
|
349
|
+
if (J !== e) return;
|
|
350
350
|
if (t?.name === "QuotaExceededError") {
|
|
351
351
|
let t = await _e(!0).then(() => !0, () => !1);
|
|
352
|
-
if (
|
|
352
|
+
if (J !== e) return;
|
|
353
353
|
if (t) {
|
|
354
|
-
|
|
354
|
+
W = !1, await we(be()?.end ?? n.currentTime);
|
|
355
355
|
return;
|
|
356
356
|
}
|
|
357
357
|
}
|
|
358
|
-
if (
|
|
359
|
-
throw
|
|
358
|
+
if (Y += 1, Y < ee) return;
|
|
359
|
+
throw J = null, t;
|
|
360
360
|
}
|
|
361
361
|
}, Ee = async () => {
|
|
362
|
-
if (
|
|
363
|
-
let e = ++
|
|
364
|
-
|
|
362
|
+
if (B || V || $ !== void 0 || R || !J && (W || !ve())) return;
|
|
363
|
+
let e = ++K, t = q, n = () => R || e !== K || t !== q;
|
|
364
|
+
B = !0;
|
|
365
365
|
try {
|
|
366
|
-
if (!
|
|
367
|
-
let { data: e, subtitles: t, finished: r } = await
|
|
368
|
-
if (n() || (r && (
|
|
366
|
+
if (!J) {
|
|
367
|
+
let { data: e, subtitles: t, finished: r } = await I.read();
|
|
368
|
+
if (n() || (r && (W = !0), t?.length && c.pushFragments(t), e.byteLength && F?.({
|
|
369
369
|
type: "fragment",
|
|
370
370
|
data: e
|
|
371
|
-
}), r &&
|
|
372
|
-
|
|
371
|
+
}), r && F?.({ type: "end" }), !e.byteLength)) return;
|
|
372
|
+
J = e, Y = 0;
|
|
373
373
|
}
|
|
374
|
-
await Te(), !n() &&
|
|
374
|
+
await Te(), !n() && pe && (pe = !1, P?.());
|
|
375
375
|
} catch (e) {
|
|
376
|
-
|
|
376
|
+
ge(e, n());
|
|
377
377
|
} finally {
|
|
378
|
-
e ===
|
|
378
|
+
e === K && (B = !1);
|
|
379
379
|
}
|
|
380
|
-
}, De = (e) => s(
|
|
380
|
+
}, De = (e) => s(z).some((t) => t.start <= e && e < t.end && t.end >= me) && ke(e), Oe = (e) => s(z).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) >= k || Ce() && xe(e), Ae = async (e) => {
|
|
384
|
+
if (!R && !(Oe(e) && ke(e))) {
|
|
385
|
+
$ && clearTimeout($), $ = void 0, W = !1, H++, U = e;
|
|
386
386
|
try {
|
|
387
387
|
await we(e);
|
|
388
|
-
let t =
|
|
389
|
-
for (let n = 0; n <
|
|
388
|
+
let t = q;
|
|
389
|
+
for (let n = 0; n < te && !(R || t !== q || W || Z(e) >= k); n++) {
|
|
390
390
|
let t = Z(e);
|
|
391
391
|
if (await Ee(), Z(e) <= t) break;
|
|
392
392
|
}
|
|
393
393
|
} finally {
|
|
394
|
-
|
|
394
|
+
H--, U === e && (U = 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 >= A) {
|
|
400
|
+
je = t, Me = null, re?.(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 || R) && (je = performance.now(), re?.(e));
|
|
407
|
+
}, A - n));
|
|
408
408
|
};
|
|
409
|
-
|
|
409
|
+
L.push(() => {
|
|
410
410
|
Q && clearTimeout(Q);
|
|
411
411
|
});
|
|
412
412
|
let Pe = 0, $;
|
|
413
|
-
|
|
413
|
+
L.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) && !(
|
|
421
|
-
if (
|
|
419
|
+
let i = performance.now(), a = i - Pe < j;
|
|
420
|
+
if (Pe = i, !De(t) && !(he && (V || Z(t) > 0) && Math.abs(t - me) < .001)) {
|
|
421
|
+
if (W = !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, R) return;
|
|
427
427
|
let e = n.currentTime;
|
|
428
|
-
De(e) || (
|
|
429
|
-
},
|
|
428
|
+
De(e) || (W = !1, we(e));
|
|
429
|
+
}, j);
|
|
430
430
|
}
|
|
431
431
|
};
|
|
432
|
-
n.addEventListener("seeking", Fe),
|
|
432
|
+
n.addEventListener("seeking", Fe), L.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), L.push(() => n.removeEventListener("error", Le));
|
|
440
440
|
let Re = setInterval(() => {
|
|
441
|
-
Ie || n.error || (_e().catch(() => {}), Ee().catch((e) =>
|
|
441
|
+
Ie || n.error || (_e().catch(() => {}), Ee().catch((e) => ge(e, !1)), Ce() && !J && xe() && M.readyState === "open" && de(() => !V).catch(() => {}));
|
|
442
442
|
}, 100);
|
|
443
|
-
return
|
|
444
|
-
destroy:
|
|
443
|
+
return L.push(() => clearInterval(Re)), {
|
|
444
|
+
destroy: se,
|
|
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 se(), e;
|
|
456
456
|
}
|
|
457
|
-
},
|
|
458
|
-
let { publicPath: n, workerUrl: r, length: i, read: a, onThumbnails: o, width: s =
|
|
457
|
+
}, P = 5, re = 500, ie = 320, ae = 1e6, F = 3, oe = 1e4, I = 1.5, L = 4e6, R = 3e4, se = async (e) => {
|
|
458
|
+
let { publicPath: n, workerUrl: r, length: i, read: a, onThumbnails: o, width: s = ie } = 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 ??
|
|
466
|
+
let t = await c.init(), n = t.duration, r = Math.max(e.interval ?? P, n / re), 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) + ae, 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")), oe))]), 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 >= F;
|
|
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")), R))]);
|
|
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 * I, h + L) || t >= i && h < i) && (g = !0), E(e), S();
|
|
573
573
|
},
|
|
574
574
|
prioritize: (e) => {
|
|
575
575
|
d || (m = e);
|
|
@@ -577,127 +577,160 @@ var i = {
|
|
|
577
577
|
destroy: () => {
|
|
578
578
|
d = !0, f.length = 0;
|
|
579
579
|
for (let e of u) URL.revokeObjectURL(e.url);
|
|
580
|
-
u = [],
|
|
580
|
+
u = [], N(c);
|
|
581
581
|
}
|
|
582
582
|
};
|
|
583
583
|
} catch (e) {
|
|
584
|
-
throw
|
|
584
|
+
throw N(c), e;
|
|
585
585
|
}
|
|
586
|
-
},
|
|
587
|
-
|
|
586
|
+
}, ce = 1280, z = 1920, le = 250, ue = 100, de = 1e3, B, V = (e) => {
|
|
587
|
+
B?.close();
|
|
588
|
+
let t = () => {
|
|
589
|
+
clearTimeout(r), e.mirror.removeEventListener("leavepictureinpicture", n), B = void 0;
|
|
590
|
+
}, n = () => {
|
|
591
|
+
t(), document.pictureInPictureElement === e.mirror && document.exitPictureInPicture().catch(() => {});
|
|
592
|
+
for (let t of e.stream.getTracks()) t.stop();
|
|
593
|
+
e.mirror.remove();
|
|
594
|
+
}, r = setTimeout(n, de);
|
|
595
|
+
e.mirror.addEventListener("leavepictureinpicture", n), B = {
|
|
596
|
+
session: e,
|
|
597
|
+
close: n,
|
|
598
|
+
cancel: t
|
|
599
|
+
};
|
|
600
|
+
}, H = () => {
|
|
601
|
+
let e = B;
|
|
602
|
+
return e?.cancel(), e?.session;
|
|
603
|
+
}, U = () => typeof HTMLCanvasElement < "u" && "captureStream" in HTMLCanvasElement.prototype, fe = () => typeof document < "u" && typeof HTMLVideoElement < "u" && typeof HTMLVideoElement.prototype.requestPictureInPicture == "function" && !!document.pictureInPictureEnabled, W = () => typeof HTMLVideoElement < "u" && "mozCaptureStream" in HTMLVideoElement.prototype, G = () => U() ? fe() ? "window" : W() ? "burn-in" : null : null, K = (e) => {
|
|
604
|
+
let { video: t, subtitles: n } = e, r = e.mode ?? G() ?? "window", i = e.maxWidth ?? (r === "burn-in" ? z : ce), 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) => {
|
|
588
605
|
r.drawImage(t, 0, 0, e.width, e.height);
|
|
589
606
|
let i = n.querySelector("canvas");
|
|
590
607
|
i && i.width > 0 && i.height > 0 && r.drawImage(i, 0, 0, e.width, e.height);
|
|
591
608
|
}, h = () => {
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
609
|
+
let e = Math.min(t.videoWidth || i, i);
|
|
610
|
+
return {
|
|
611
|
+
width: e,
|
|
612
|
+
height: Math.round(e * ((t.videoHeight || 9) / (t.videoWidth || 16)))
|
|
613
|
+
};
|
|
614
|
+
}, g = () => {
|
|
615
|
+
if (!a) return;
|
|
616
|
+
let { composite: e, context: n } = a, { width: r, height: i } = h();
|
|
617
|
+
t.videoWidth && (e.width !== r || e.height !== i) && Object.assign(e, {
|
|
618
|
+
width: r,
|
|
619
|
+
height: i
|
|
620
|
+
}), m(e, n);
|
|
621
|
+
}, _ = "requestVideoFrameCallback" in t, v = () => {
|
|
622
|
+
g(), y();
|
|
597
623
|
}, y = () => {
|
|
598
|
-
|
|
624
|
+
!d && a && (o = _ ? t.requestVideoFrameCallback(v) : requestAnimationFrame(v));
|
|
599
625
|
}, b = () => {
|
|
600
|
-
|
|
626
|
+
o !== void 0 && (_ ? t.cancelVideoFrameCallback(o) : cancelAnimationFrame(o)), s !== void 0 && clearInterval(s), o = void 0, s = void 0;
|
|
601
627
|
}, x = () => {
|
|
602
|
-
|
|
628
|
+
"mediaSession" in navigator && (navigator.mediaSession.playbackState = t.paused ? "paused" : "playing");
|
|
603
629
|
}, S = () => {
|
|
630
|
+
c || l || !t.paused || t.play().catch(() => {});
|
|
631
|
+
}, C = () => {
|
|
604
632
|
c || l || (t.paused || t.pause(), r !== "burn-in" && (c = !0, a?.mirror.play().catch(() => {}).finally(() => {
|
|
605
633
|
c = !1;
|
|
606
634
|
})));
|
|
607
|
-
}, C = () => {
|
|
608
|
-
b(), a?.mirror.play().catch(() => {});
|
|
609
635
|
}, w = () => {
|
|
610
|
-
|
|
636
|
+
x(), a?.mirror.play().catch(() => {});
|
|
611
637
|
}, T = () => {
|
|
638
|
+
x(), r === "burn-in" && a?.mirror.pause();
|
|
639
|
+
}, E = () => {
|
|
612
640
|
if (r !== "burn-in" || !a || !t.paused || l) return;
|
|
613
641
|
l = !0;
|
|
614
642
|
let { mirror: e } = a;
|
|
615
|
-
|
|
616
|
-
setTimeout(e,
|
|
643
|
+
g(), e.play().then(() => new Promise((e) => {
|
|
644
|
+
setTimeout(e, ue);
|
|
617
645
|
})).catch(() => {}).finally(() => {
|
|
618
646
|
e.pause(), setTimeout(() => {
|
|
619
647
|
l = !1;
|
|
620
|
-
},
|
|
648
|
+
}, ue);
|
|
621
649
|
});
|
|
622
|
-
},
|
|
650
|
+
}, ee = [["play", () => {
|
|
623
651
|
t.play().catch(() => {});
|
|
624
652
|
}], ["pause", () => t.pause()]], D = (e) => {
|
|
625
|
-
if ("mediaSession" in navigator) for (let [t, n] of
|
|
653
|
+
if ("mediaSession" in navigator) for (let [t, n] of ee) try {
|
|
626
654
|
navigator.mediaSession.setActionHandler(t, e ? n : null);
|
|
627
655
|
} catch {}
|
|
628
656
|
}, O = () => {
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
657
|
+
y(), s = setInterval(() => {
|
|
658
|
+
t.paused && g();
|
|
659
|
+
}, le);
|
|
660
|
+
}, k = (e) => {
|
|
661
|
+
e.addEventListener("play", S), e.addEventListener("pause", C), e.addEventListener("leavepictureinpicture", j), t.addEventListener("play", w), t.addEventListener("pause", T), t.addEventListener("seeked", E), D(!0), x();
|
|
662
|
+
}, te = () => {
|
|
663
|
+
b(), D(!1), t.removeEventListener("play", w), t.removeEventListener("pause", T), t.removeEventListener("seeked", E);
|
|
664
|
+
let e = a;
|
|
665
|
+
return a = void 0, e?.mirror.removeEventListener("play", S), e?.mirror.removeEventListener("pause", C), e?.mirror.removeEventListener("leavepictureinpicture", j), e;
|
|
666
|
+
}, A = () => {
|
|
667
|
+
let e = te();
|
|
668
|
+
if (e) {
|
|
669
|
+
for (let t of e.stream.getTracks()) t.stop();
|
|
670
|
+
e.mirror.remove();
|
|
632
671
|
}
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
for (let t of e.getTracks()) t.stop();
|
|
636
|
-
n.remove(), a = void 0, f?.();
|
|
637
|
-
}, k = () => O(), A = (e) => {
|
|
672
|
+
f?.();
|
|
673
|
+
}, j = () => A(), M = (e) => {
|
|
638
674
|
let r = t.style.opacity, i = n.style.display;
|
|
639
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", f = () => {
|
|
640
676
|
f = void 0, t.style.opacity = r, n.style.display = i;
|
|
641
677
|
};
|
|
642
|
-
},
|
|
643
|
-
let n =
|
|
644
|
-
|
|
645
|
-
let
|
|
646
|
-
|
|
647
|
-
let
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
stream: c.captureStream(),
|
|
653
|
-
mirror: u
|
|
678
|
+
}, N = async () => {
|
|
679
|
+
let n = Object.assign(document.createElement("canvas"), h()), i = n.getContext("2d");
|
|
680
|
+
if (!i) throw Error("the picture in picture composite has no 2d context");
|
|
681
|
+
let o = document.createElement("video");
|
|
682
|
+
o.muted = !0, o.playsInline = !0, r === "burn-in" ? M(o) : o.style.cssText = "position:fixed;width:1px;height:1px;opacity:0;pointer-events:none;left:-1px;top:-1px", m(n, i);
|
|
683
|
+
let s = {
|
|
684
|
+
composite: n,
|
|
685
|
+
context: i,
|
|
686
|
+
stream: n.captureStream(),
|
|
687
|
+
mirror: o
|
|
654
688
|
};
|
|
655
|
-
if (a =
|
|
656
|
-
t.paused && h();
|
|
657
|
-
}, R), u.play().catch(() => {}), u.readyState === 0 && await new Promise((e) => {
|
|
689
|
+
if (a = s, o.srcObject = s.stream, (e.container ?? (r === "burn-in" ? t.parentElement : null) ?? document.body).appendChild(o), O(), o.play().catch(() => {}), o.readyState === 0 && await new Promise((e) => {
|
|
658
690
|
let t = () => {
|
|
659
691
|
clearTimeout(n), e();
|
|
660
692
|
}, n = setTimeout(t, 1e3);
|
|
661
|
-
|
|
662
|
-
}), d || a !==
|
|
663
|
-
|
|
693
|
+
o.addEventListener("loadedmetadata", t, { once: !0 });
|
|
694
|
+
}), d || a !== s) {
|
|
695
|
+
o.remove();
|
|
664
696
|
return;
|
|
665
697
|
}
|
|
666
|
-
if (
|
|
667
|
-
t.paused &&
|
|
698
|
+
if (k(o), r === "burn-in") {
|
|
699
|
+
t.paused && o.pause(), e.onBurnedInChange?.(!0);
|
|
668
700
|
return;
|
|
669
701
|
}
|
|
670
|
-
await
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
if (r === "burn-in") {
|
|
679
|
-
O(), e.onBurnedInChange?.(!1);
|
|
680
|
-
return;
|
|
681
|
-
}
|
|
682
|
-
await document.exitPictureInPicture().catch(() => {});
|
|
702
|
+
await o.requestPictureInPicture();
|
|
703
|
+
}, ne = async () => {
|
|
704
|
+
if (!(d || u)) {
|
|
705
|
+
u = !0;
|
|
706
|
+
try {
|
|
707
|
+
if (p()) {
|
|
708
|
+
if (r === "burn-in") {
|
|
709
|
+
A(), e.onBurnedInChange?.(!1);
|
|
683
710
|
return;
|
|
684
711
|
}
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
await ee();
|
|
688
|
-
} catch (t) {
|
|
689
|
-
console.warn("subtitle compositing for picture in picture failed", t), O(), r === "burn-in" && e.onBurnedInChange?.(!1);
|
|
690
|
-
}
|
|
691
|
-
} finally {
|
|
692
|
-
u = !1;
|
|
712
|
+
await document.exitPictureInPicture().catch(() => {});
|
|
713
|
+
return;
|
|
693
714
|
}
|
|
715
|
+
r !== "burn-in" && document.pictureInPictureElement && await document.exitPictureInPicture().catch(() => {});
|
|
716
|
+
try {
|
|
717
|
+
await N();
|
|
718
|
+
} catch (t) {
|
|
719
|
+
console.warn("subtitle compositing for picture in picture failed", t), A(), r === "burn-in" && e.onBurnedInChange?.(!1);
|
|
720
|
+
}
|
|
721
|
+
} finally {
|
|
722
|
+
u = !1;
|
|
694
723
|
}
|
|
695
|
-
}
|
|
724
|
+
}
|
|
725
|
+
}, P = r === "window" ? H() : void 0;
|
|
726
|
+
return P && (a = P, P.context.fillRect(0, 0, P.composite.width, P.composite.height), O(), k(P.mirror)), {
|
|
727
|
+
toggle: ne,
|
|
696
728
|
mode: r,
|
|
697
729
|
destroy: () => {
|
|
698
|
-
d = !0, r
|
|
730
|
+
if (d = !0, r === "window" && p()) return V(te());
|
|
731
|
+
A(), r === "burn-in" && e.onBurnedInChange?.(!1);
|
|
699
732
|
}
|
|
700
733
|
};
|
|
701
734
|
};
|
|
702
735
|
//#endregion
|
|
703
|
-
export { b as a,
|
|
736
|
+
export { b as a, N as c, s as d, l as f, M as i, u as l, a as m, G as n, x as o, i as p, se as r, ne as s, K 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-x5oIkHAN.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
|
@@ -37,7 +37,10 @@ export type PictureInPictureOptions = {
|
|
|
37
37
|
*/
|
|
38
38
|
subtitles: HTMLElement
|
|
39
39
|
maxWidth?: number
|
|
40
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* Where the mirror is mounted. It must be in the document. Defaults to the video's parent in burn-in, where the
|
|
42
|
+
* mirror is the picture, and to the body in a window, so the window can outlive the player (see `destroy`).
|
|
43
|
+
*/
|
|
41
44
|
container?: HTMLElement
|
|
42
45
|
/** Overrides detection. Passing a mode the browser cannot honour is the caller's problem. */
|
|
43
46
|
mode?: PictureInPictureMode
|
|
@@ -47,6 +50,12 @@ export type PictureInPictureOptions = {
|
|
|
47
50
|
|
|
48
51
|
export type PictureInPictureController = {
|
|
49
52
|
toggle: () => Promise<void>
|
|
53
|
+
/**
|
|
54
|
+
* Burn-in, or a window that is not open, ends here. An open window stays open for a second, and the
|
|
55
|
+
* next controller created in the document in that time draws its own video into it, at that video's
|
|
56
|
+
* shape: an app that replaces the player for the next episode keeps the window with no click, which
|
|
57
|
+
* a new window would need. If no controller takes it in that second, it closes.
|
|
58
|
+
*/
|
|
50
59
|
destroy: () => void
|
|
51
60
|
mode: PictureInPictureMode
|
|
52
61
|
}
|
|
@@ -58,6 +67,36 @@ type Session = {
|
|
|
58
67
|
mirror: HTMLVideoElement
|
|
59
68
|
}
|
|
60
69
|
|
|
70
|
+
/** How long a destroyed controller's window waits for the next one; a remounted player asks within a few frames. */
|
|
71
|
+
export const HAND_OVER_MS = 1000
|
|
72
|
+
|
|
73
|
+
let handedOver: { session: Session, close: () => void, cancel: () => void } | undefined
|
|
74
|
+
|
|
75
|
+
const handOver = (session: Session) => {
|
|
76
|
+
handedOver?.close()
|
|
77
|
+
const cancel = () => {
|
|
78
|
+
clearTimeout(timer)
|
|
79
|
+
session.mirror.removeEventListener('leavepictureinpicture', close)
|
|
80
|
+
handedOver = undefined
|
|
81
|
+
}
|
|
82
|
+
const close = () => {
|
|
83
|
+
cancel()
|
|
84
|
+
if (document.pictureInPictureElement === session.mirror) void document.exitPictureInPicture().catch(() => {})
|
|
85
|
+
for (const track of session.stream.getTracks()) track.stop()
|
|
86
|
+
session.mirror.remove()
|
|
87
|
+
}
|
|
88
|
+
const timer = setTimeout(close, HAND_OVER_MS)
|
|
89
|
+
session.mirror.addEventListener('leavepictureinpicture', close)
|
|
90
|
+
handedOver = { session, close, cancel }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The window a destroyed controller left, until it closes. */
|
|
94
|
+
const takeOver = () => {
|
|
95
|
+
const held = handedOver
|
|
96
|
+
held?.cancel()
|
|
97
|
+
return held?.session
|
|
98
|
+
}
|
|
99
|
+
|
|
61
100
|
const canComposite = () =>
|
|
62
101
|
typeof HTMLCanvasElement !== 'undefined' && 'captureStream' in HTMLCanvasElement.prototype
|
|
63
102
|
|
|
@@ -128,8 +167,21 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
128
167
|
}
|
|
129
168
|
}
|
|
130
169
|
|
|
170
|
+
// Sized off the intrinsic dimensions, never the layout, so only a video of another size resizes a
|
|
171
|
+
// canvas that a MediaStream is capturing. A window taken over starts at the size of the video before.
|
|
172
|
+
const compositeSize = () => {
|
|
173
|
+
const width = Math.min(video.videoWidth || maxWidth, maxWidth)
|
|
174
|
+
return { width, height: Math.round(width * ((video.videoHeight || 9) / (video.videoWidth || 16))) }
|
|
175
|
+
}
|
|
176
|
+
|
|
131
177
|
const paint = () => {
|
|
132
|
-
if (session)
|
|
178
|
+
if (!session) return
|
|
179
|
+
const { composite, context } = session
|
|
180
|
+
const { width, height } = compositeSize()
|
|
181
|
+
if (video.videoWidth && (composite.width !== width || composite.height !== height)) {
|
|
182
|
+
Object.assign(composite, { width, height })
|
|
183
|
+
}
|
|
184
|
+
draw(composite, context)
|
|
133
185
|
}
|
|
134
186
|
|
|
135
187
|
const useFrameCallback = 'requestVideoFrameCallback' in video
|
|
@@ -233,26 +285,46 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
233
285
|
}
|
|
234
286
|
}
|
|
235
287
|
|
|
236
|
-
const
|
|
288
|
+
const start = () => {
|
|
289
|
+
schedule()
|
|
290
|
+
pausedRepaint = setInterval(() => { if (video.paused) paint() }, PAUSED_REPAINT_INTERVAL)
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const listen = (mirror: HTMLVideoElement) => {
|
|
294
|
+
mirror.addEventListener('play', onMirrorPlay)
|
|
295
|
+
mirror.addEventListener('pause', onMirrorPause)
|
|
296
|
+
mirror.addEventListener('leavepictureinpicture', onLeave)
|
|
297
|
+
video.addEventListener('play', onVideoPlay)
|
|
298
|
+
video.addEventListener('pause', onVideoPause)
|
|
299
|
+
video.addEventListener('seeked', onVideoSeeked)
|
|
300
|
+
bindMediaSession(true)
|
|
301
|
+
publishState()
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** Lets go of the session, mirror and window left as they are. */
|
|
305
|
+
const detach = () => {
|
|
237
306
|
stopLoop()
|
|
238
307
|
bindMediaSession(false)
|
|
239
308
|
video.removeEventListener('play', onVideoPlay)
|
|
240
309
|
video.removeEventListener('pause', onVideoPause)
|
|
241
310
|
video.removeEventListener('seeked', onVideoSeeked)
|
|
242
|
-
|
|
243
|
-
// a burn-in that never built a session can still have swapped the picture
|
|
244
|
-
restore?.()
|
|
245
|
-
return
|
|
246
|
-
}
|
|
247
|
-
const { stream, mirror } = session
|
|
248
|
-
mirror.removeEventListener('play', onMirrorPlay)
|
|
249
|
-
mirror.removeEventListener('pause', onMirrorPause)
|
|
250
|
-
mirror.removeEventListener('leavepictureinpicture', onLeave)
|
|
251
|
-
for (const track of stream.getTracks()) track.stop()
|
|
252
|
-
mirror.remove()
|
|
311
|
+
const held = session
|
|
253
312
|
session = undefined
|
|
313
|
+
held?.mirror.removeEventListener('play', onMirrorPlay)
|
|
314
|
+
held?.mirror.removeEventListener('pause', onMirrorPause)
|
|
315
|
+
held?.mirror.removeEventListener('leavepictureinpicture', onLeave)
|
|
316
|
+
return held
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const teardown = () => {
|
|
320
|
+
const held = detach()
|
|
321
|
+
if (held) {
|
|
322
|
+
for (const track of held.stream.getTracks()) track.stop()
|
|
323
|
+
held.mirror.remove()
|
|
324
|
+
}
|
|
254
325
|
// AFTER the mirror is gone. Restoring first puts the real picture and the overlay back while the
|
|
255
326
|
// composite is still mounted and painting, which is the doubled subtitles this mode exists to avoid.
|
|
327
|
+
// A burn-in that never built a session can still have swapped the picture.
|
|
256
328
|
restore?.()
|
|
257
329
|
}
|
|
258
330
|
|
|
@@ -285,14 +357,7 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
285
357
|
}
|
|
286
358
|
|
|
287
359
|
const enter = async () => {
|
|
288
|
-
|
|
289
|
-
// MediaStream is already capturing.
|
|
290
|
-
const width = Math.min(video.videoWidth || maxWidth, maxWidth)
|
|
291
|
-
const height = Math.round(width * ((video.videoHeight || 9) / (video.videoWidth || 16)))
|
|
292
|
-
|
|
293
|
-
const composite = document.createElement('canvas')
|
|
294
|
-
composite.width = width
|
|
295
|
-
composite.height = height
|
|
360
|
+
const composite = Object.assign(document.createElement('canvas'), compositeSize())
|
|
296
361
|
// The alpha channel is unused but has to stay: with `{ alpha: false }` Chrome's captureStream
|
|
297
362
|
// delivers the first frame and then nothing, with the track still "live" and no error anywhere.
|
|
298
363
|
const context = composite.getContext('2d')
|
|
@@ -312,10 +377,9 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
312
377
|
const mine: Session = { composite, context, stream: composite.captureStream(), mirror }
|
|
313
378
|
session = mine
|
|
314
379
|
mirror.srcObject = mine.stream
|
|
315
|
-
;(options.container ?? video.parentElement ?? document.body).appendChild(mirror)
|
|
380
|
+
;(options.container ?? (mode === 'burn-in' ? video.parentElement : null) ?? document.body).appendChild(mirror)
|
|
316
381
|
|
|
317
|
-
|
|
318
|
-
pausedRepaint = setInterval(() => { if (video.paused) paint() }, PAUSED_REPAINT_INTERVAL)
|
|
382
|
+
start()
|
|
319
383
|
void mirror.play().catch(() => {})
|
|
320
384
|
|
|
321
385
|
// requestPictureInPicture rejects while readyState is 0, and the wait is short enough that the
|
|
@@ -335,14 +399,7 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
335
399
|
return
|
|
336
400
|
}
|
|
337
401
|
|
|
338
|
-
mirror
|
|
339
|
-
mirror.addEventListener('pause', onMirrorPause)
|
|
340
|
-
mirror.addEventListener('leavepictureinpicture', onLeave)
|
|
341
|
-
video.addEventListener('play', onVideoPlay)
|
|
342
|
-
video.addEventListener('pause', onVideoPause)
|
|
343
|
-
video.addEventListener('seeked', onVideoSeeked)
|
|
344
|
-
bindMediaSession(true)
|
|
345
|
-
publishState()
|
|
402
|
+
listen(mirror)
|
|
346
403
|
|
|
347
404
|
if (mode === 'burn-in') {
|
|
348
405
|
// No window to open: the browser's own control does that, on the element now on screen.
|
|
@@ -386,12 +443,21 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
386
443
|
}
|
|
387
444
|
}
|
|
388
445
|
|
|
446
|
+
const taken = mode === 'window' ? takeOver() : undefined
|
|
447
|
+
if (taken) {
|
|
448
|
+
session = taken
|
|
449
|
+
// the last frame of the video before would otherwise stay up until this one has a frame
|
|
450
|
+
taken.context.fillRect(0, 0, taken.composite.width, taken.composite.height)
|
|
451
|
+
start()
|
|
452
|
+
listen(taken.mirror)
|
|
453
|
+
}
|
|
454
|
+
|
|
389
455
|
return {
|
|
390
456
|
toggle,
|
|
391
457
|
mode,
|
|
392
458
|
destroy: () => {
|
|
393
459
|
destroyed = true
|
|
394
|
-
if (mode
|
|
460
|
+
if (mode === 'window' && active()) return handOver(detach()!)
|
|
395
461
|
teardown()
|
|
396
462
|
if (mode === 'burn-in') options.onBurnedInChange?.(false)
|
|
397
463
|
},
|