@banou/media-player 0.8.4 → 0.8.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,5 +6,5 @@ export { createThumbnailGenerator } from './thumbnails';
6
6
  export type { ThumbnailGenerator, ThumbnailGeneratorOptions, ThumbnailImage } from './thumbnails';
7
7
  export { getTimeRanges, updateSourceBuffer } from './source-buffer';
8
8
  export type { TimeRange } from './source-buffer';
9
- export { createPictureInPicture } from './picture-in-picture';
10
- export type { PictureInPictureController, PictureInPictureOptions } from './picture-in-picture';
9
+ export { createPictureInPicture, pictureInPictureMode } from './picture-in-picture';
10
+ export type { PictureInPictureController, PictureInPictureOptions, PictureInPictureMode } from './picture-in-picture';
@@ -1,2 +1,2 @@
1
- import { a as e, c as t, i as n, l as r, n as i, o as a, r as o, s, t as c } from "../engine-Dmc7JSd9.js";
2
- export { o as DEFAULT_BUFFER_SIZE, a as SUBTITLES_OFF, c as createPictureInPicture, s as createSubtitleRenderer, i as createThumbnailGenerator, t as getTimeRanges, n as startPlayback, e as terminateRemuxer, r as updateSourceBuffer };
1
+ import { a as e, c as t, i as n, l as r, n as i, o as a, r as o, s, t as c, u as l } from "../engine--OlGG-Ve.js";
2
+ export { n as DEFAULT_BUFFER_SIZE, s as SUBTITLES_OFF, c as createPictureInPicture, t as createSubtitleRenderer, o as createThumbnailGenerator, r as getTimeRanges, i as pictureInPictureMode, e as startPlayback, a as terminateRemuxer, l as updateSourceBuffer };
@@ -6,22 +6,34 @@
6
6
  * `captureStream()` backs a hidden mirror element, which is the one that enters the window. The
7
7
  * original element keeps playing and stays the only audio source.
8
8
  */
9
+ /**
10
+ * `window` opens a real picture in picture window. `burn-in` cannot, and instead paints the
11
+ * composite into the player itself so the browser's OWN picture in picture control carries the
12
+ * subtitles with it.
13
+ */
14
+ export type PictureInPictureMode = 'window' | 'burn-in';
9
15
  export type PictureInPictureOptions = {
10
16
  video: HTMLVideoElement;
11
17
  /** The subtitle canvas. jassub sizes it to the video's content rect, so it maps 1:1. */
12
18
  canvas: HTMLCanvasElement;
13
19
  maxWidth?: number;
14
- /** Where the hidden mirror is mounted. It must be in the document. Defaults to the video's parent. */
20
+ /** Where the mirror is mounted. It must be in the document. Defaults to the video's parent. */
15
21
  container?: HTMLElement;
16
- /**
17
- * Toggle used when compositing is unavailable or fails. The React layer passes the player store's
18
- * own action, which handles Safari and exits fullscreen first; the default here does neither.
19
- */
20
- fallback?: () => Promise<void>;
22
+ /** Overrides detection. Passing a mode the browser cannot honour is the caller's problem. */
23
+ mode?: PictureInPictureMode;
24
+ /** Burn-in only, so the chrome can light the control up and say what happened. */
25
+ onBurnedInChange?: (burnedIn: boolean) => void;
21
26
  };
22
27
  export type PictureInPictureController = {
23
28
  toggle: () => Promise<void>;
24
29
  destroy: () => void;
30
+ mode: PictureInPictureMode;
25
31
  };
32
+ /**
33
+ * null means offer no control at all. That covers Safari, whose picture in picture is
34
+ * `webkitSetPresentationMode` and not the W3C API, so neither arm here can drive it. A dead button
35
+ * is worse than no button.
36
+ */
37
+ export declare const pictureInPictureMode: () => PictureInPictureMode | null;
26
38
  /** Takes over the document's Media Session play/pause handlers while a session is open. */
27
39
  export declare const createPictureInPicture: (options: PictureInPictureOptions) => PictureInPictureController;
@@ -220,82 +220,82 @@ var a = (e) => Array(e.buffered.length).fill(void 0).map((t, n) => ({
220
220
  i(), n(/* @__PURE__ */ Error("The MediaSource closed before a SourceBuffer could be created"));
221
221
  };
222
222
  C.addEventListener("sourceopen", a, { once: !0 }), C.addEventListener("sourceclose", o, { once: !0 }), C.addEventListener("error", o, { once: !0 });
223
- }), { appendBuffer: L, unbufferRange: R, updateTimestampOffset: ee, endOfStream: te } = s(T, C);
223
+ }), { appendBuffer: L, unbufferRange: R, updateTimestampOffset: z, endOfStream: B } = s(T, C);
224
224
  await L(e.data), e.subtitles?.length && o.pushFragments(e.subtitles), D?.();
225
- let z = !1, B = !1, V = !1, H = 0, U = 0, W = null, G = 0, K = 0, q = !1, J = 0, Y = (e, t) => {
225
+ let V = !1, H = !1, U = !1, W = 0, G = 0, K = null, q = 0, J = 0, Y = !1, X = 0, Z = (e, t) => {
226
226
  let n = e?.message === "Cancelled";
227
- t && n || (console.error(e), !q && (q = !0, O?.(n ? Error("Reading the video file failed", { cause: e }) : e)));
228
- }, X = async (e = !1) => {
227
+ t && n || (console.error(e), !Y && (Y = !0, O?.(n ? Error("Reading the video file failed", { cause: e }) : e)));
228
+ }, Q = async (e = !1) => {
229
229
  let t = n.currentTime, r = e ? v : h, i = e ? y : g;
230
230
  for (let { start: e, end: n } of a(T)) e < t + r && await R(e, t + r), n > t + i && await R(t + i, n);
231
- }, ne = () => {
231
+ }, ee = () => {
232
232
  let e = a(T);
233
233
  if (!e.length) return !0;
234
234
  let t = n.currentTime;
235
235
  if (Math.max(...e.map((e) => e.end)) >= t + g) return !1;
236
236
  let r = e.find((e) => e.start <= t + S && t < e.end);
237
237
  return !r || r.end < t + _;
238
- }, Z = () => {
238
+ }, $ = () => {
239
239
  let e = n.currentTime;
240
240
  return a(T).find((t) => t.start <= e + S && e < t.end);
241
- }, re = () => {
242
- let e = Z();
243
- return !!e && e.end >= K - .1;
244
- }, Q = () => {
241
+ }, te = () => {
242
+ let e = $();
243
+ return !!e && e.end >= J - .1;
244
+ }, ne = () => {
245
245
  let e = a(T).map((e) => e.end);
246
- e.length && (K = Math.max(K, ...e));
247
- }, $ = async (e) => {
248
- B = !0, W = null, G = 0, J = e;
249
- let t = ++U;
246
+ e.length && (J = Math.max(J, ...e));
247
+ }, re = async (e) => {
248
+ H = !0, K = null, q = 0, X = e;
249
+ let t = ++G;
250
250
  try {
251
251
  let { data: n, pts: r, subtitles: i } = await N.seek(e);
252
- if (F || t !== U || (i?.length && o.pushFragments(i), await ee(r), F || t !== U)) return;
253
- n.byteLength && (await L(n), Q());
252
+ if (F || t !== G || (i?.length && o.pushFragments(i), await z(r), F || t !== G)) return;
253
+ n.byteLength && (await L(n), ne());
254
254
  } catch (e) {
255
- Y(e, F || t !== U);
255
+ Z(e, F || t !== G);
256
256
  } finally {
257
- t === U && (B = !1);
257
+ t === G && (H = !1);
258
258
  }
259
259
  }, ie = async () => {
260
- let e = W;
260
+ let e = K;
261
261
  if (e) try {
262
- await L(e), Q(), W === e && (W = null, G = 0);
262
+ await L(e), ne(), K === e && (K = null, q = 0);
263
263
  } catch (t) {
264
- if (W !== e) return;
264
+ if (K !== e) return;
265
265
  if (t?.name === "QuotaExceededError") {
266
- let t = await X(!0).then(() => !0, () => !1);
267
- if (W !== e) return;
266
+ let t = await Q(!0).then(() => !0, () => !1);
267
+ if (K !== e) return;
268
268
  if (t) {
269
- V = !1, await $(Z()?.end ?? n.currentTime);
269
+ U = !1, await re($()?.end ?? n.currentTime);
270
270
  return;
271
271
  }
272
272
  }
273
- if (G += 1, G < b) return;
274
- throw W = null, t;
273
+ if (q += 1, q < b) return;
274
+ throw K = null, t;
275
275
  }
276
276
  }, ae = async () => {
277
- if (z || B || F || !W && (V || !ne())) return;
278
- let e = ++H, t = U, n = () => F || e !== H || t !== U;
279
- z = !0;
277
+ if (V || H || F || !K && (U || !ee())) return;
278
+ let e = ++W, t = G, n = () => F || e !== W || t !== G;
279
+ V = !0;
280
280
  try {
281
- if (!W) {
281
+ if (!K) {
282
282
  let { data: e, subtitles: t, finished: r } = await N.read();
283
- if (n() || (r && (V = !0), t?.length && o.pushFragments(t), !e.byteLength)) return;
284
- W = e, G = 0;
283
+ if (n() || (r && (U = !0), t?.length && o.pushFragments(t), !e.byteLength)) return;
284
+ K = e, q = 0;
285
285
  }
286
- await ie(), !n() && q && (q = !1, k?.());
286
+ await ie(), !n() && Y && (Y = !1, k?.());
287
287
  } catch (e) {
288
- Y(e, n());
288
+ Z(e, n());
289
289
  } finally {
290
- e === H && (z = !1);
290
+ e === W && (V = !1);
291
291
  }
292
- }, oe = (e) => e >= J && !!Z(), se = () => {
292
+ }, oe = (e) => e >= X && !!$(), se = () => {
293
293
  let t = n.currentTime, r = e.info.input.duration || n.duration;
294
- r > 0 && A?.(Math.min(Math.max(t / r, 0), 1)), !oe(t) && (V = !1, $(t));
294
+ r > 0 && A?.(Math.min(Math.max(t / r, 0), 1)), !oe(t) && (U = !1, re(t));
295
295
  };
296
296
  n.addEventListener("seeking", se), P.push(() => n.removeEventListener("seeking", se));
297
297
  let ce = setInterval(() => {
298
- X().catch(() => {}), ae().catch((e) => Y(e, !1)), V && !W && re() && C.readyState === "open" && te().catch(() => {});
298
+ Q().catch(() => {}), ae().catch((e) => Z(e, !1)), U && !K && te() && C.readyState === "open" && B().catch(() => {});
299
299
  }, 100);
300
300
  return P.push(() => clearInterval(ce)), {
301
301
  destroy: I,
@@ -383,88 +383,119 @@ var a = (e) => Array(e.buffered.length).fill(void 0).map((t, n) => ({
383
383
  } catch (e) {
384
384
  throw w(c), e;
385
385
  }
386
- }, N = 1280, P = 250, F = () => typeof HTMLCanvasElement < "u" && "captureStream" in HTMLCanvasElement.prototype && typeof document < "u" && document.pictureInPictureEnabled, I = (e) => {
387
- let { video: t, canvas: n, maxWidth: r = N } = e, i, a, o, s = !1, c = !1, l = !1, u = () => !!i && document.pictureInPictureElement === i.mirror, d = (e, r) => {
386
+ }, N = 1280, P = 1920, F = 250, I = 100, L = () => typeof HTMLCanvasElement < "u" && "captureStream" in HTMLCanvasElement.prototype, R = () => typeof document < "u" && typeof HTMLVideoElement < "u" && typeof HTMLVideoElement.prototype.requestPictureInPicture == "function" && !!document.pictureInPictureEnabled, z = () => typeof HTMLVideoElement < "u" && "mozCaptureStream" in HTMLVideoElement.prototype, B = () => L() ? R() ? "window" : z() ? "burn-in" : null : null, V = (e) => {
387
+ let { video: t, canvas: n } = e, r = e.mode ?? B() ?? "window", i = e.maxWidth ?? (r === "burn-in" ? P : N), 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) => {
388
388
  r.drawImage(t, 0, 0, e.width, e.height), n.width > 0 && n.height > 0 && r.drawImage(n, 0, 0, e.width, e.height);
389
- }, f = () => {
390
- i && d(i.composite, i.context);
391
- }, p = "requestVideoFrameCallback" in t, m = () => {
392
- f(), h();
393
389
  }, h = () => {
394
- l || !i || (a = p ? t.requestVideoFrameCallback(m) : requestAnimationFrame(m));
395
- }, g = () => {
396
- a !== void 0 && (p ? t.cancelVideoFrameCallback(a) : cancelAnimationFrame(a)), o !== void 0 && clearInterval(o), a = void 0, o = void 0;
397
- }, _ = () => {
398
- "mediaSession" in navigator && (navigator.mediaSession.playbackState = t.paused ? "paused" : "playing");
390
+ a && m(a.composite, a.context);
391
+ }, g = "requestVideoFrameCallback" in t, _ = () => {
392
+ h(), v();
399
393
  }, v = () => {
400
- s || !t.paused || t.play().catch(() => {});
394
+ d || !a || (o = g ? t.requestVideoFrameCallback(_) : requestAnimationFrame(_));
401
395
  }, y = () => {
402
- s || (t.paused || t.pause(), s = !0, i?.mirror.play().catch(() => {}).finally(() => {
403
- s = !1;
404
- }));
396
+ o !== void 0 && (g ? t.cancelVideoFrameCallback(o) : cancelAnimationFrame(o)), s !== void 0 && clearInterval(s), o = void 0, s = void 0;
405
397
  }, b = () => {
406
- _(), i?.mirror.play().catch(() => {});
398
+ "mediaSession" in navigator && (navigator.mediaSession.playbackState = t.paused ? "paused" : "playing");
407
399
  }, x = () => {
408
- _();
409
- }, S = [["play", () => {
400
+ c || l || !t.paused || t.play().catch(() => {});
401
+ }, S = () => {
402
+ c || l || (t.paused || t.pause(), r !== "burn-in" && (c = !0, a?.mirror.play().catch(() => {}).finally(() => {
403
+ c = !1;
404
+ })));
405
+ }, C = () => {
406
+ b(), a?.mirror.play().catch(() => {});
407
+ }, w = () => {
408
+ b(), r === "burn-in" && a?.mirror.pause();
409
+ }, T = () => {
410
+ if (r !== "burn-in" || !a || !t.paused || l) return;
411
+ l = !0;
412
+ let { mirror: e } = a;
413
+ h(), e.play().then(() => new Promise((e) => {
414
+ setTimeout(e, I);
415
+ })).catch(() => {}).finally(() => {
416
+ e.pause(), setTimeout(() => {
417
+ l = !1;
418
+ }, I);
419
+ });
420
+ }, E = [["play", () => {
410
421
  t.play().catch(() => {});
411
- }], ["pause", () => t.pause()]], C = (e) => {
412
- if ("mediaSession" in navigator) for (let [t, n] of S) try {
422
+ }], ["pause", () => t.pause()]], D = (e) => {
423
+ if ("mediaSession" in navigator) for (let [t, n] of E) try {
413
424
  navigator.mediaSession.setActionHandler(t, e ? n : null);
414
425
  } catch {}
415
- }, w = () => {
416
- if (g(), C(!1), t.removeEventListener("play", b), t.removeEventListener("pause", x), !i) return;
417
- let { stream: e, mirror: n } = i;
418
- n.removeEventListener("play", v), n.removeEventListener("pause", y), n.removeEventListener("leavepictureinpicture", T);
426
+ }, O = () => {
427
+ if (y(), D(!1), t.removeEventListener("play", C), t.removeEventListener("pause", w), t.removeEventListener("seeked", T), !a) {
428
+ f?.();
429
+ return;
430
+ }
431
+ let { stream: e, mirror: n } = a;
432
+ n.removeEventListener("play", x), n.removeEventListener("pause", S), n.removeEventListener("leavepictureinpicture", k);
419
433
  for (let t of e.getTracks()) t.stop();
420
- n.remove(), i = void 0;
421
- }, T = () => w(), E = e.fallback ?? (async () => {
422
- document.pictureInPictureElement === t ? await document.exitPictureInPicture() : await t.requestPictureInPicture();
423
- }), D = async () => {
424
- let n = Math.min(t.videoWidth || r, r), a = Math.round(n * ((t.videoHeight || 9) / (t.videoWidth || 16))), s = document.createElement("canvas");
425
- s.width = n, s.height = a;
426
- let c = s.getContext("2d");
427
- if (!c) throw Error("the picture in picture composite has no 2d context");
428
- let l = document.createElement("video");
429
- l.muted = !0, l.playsInline = !0, l.style.cssText = "position:fixed;width:1px;height:1px;opacity:0;pointer-events:none;left:-1px;top:-1px", d(s, c), i = {
430
- composite: s,
431
- context: c,
432
- stream: s.captureStream(),
433
- mirror: l
434
- }, l.srcObject = i.stream, (e.container ?? t.parentElement ?? document.body).appendChild(l), h(), o = setInterval(() => {
435
- t.paused && f();
436
- }, P), l.play().catch(() => {}), l.readyState === 0 && await new Promise((e) => {
434
+ n.remove(), a = void 0, f?.();
435
+ }, k = () => O(), A = (e) => {
436
+ let r = t.style.opacity, i = n.style.display;
437
+ 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 = () => {
438
+ f = void 0, t.style.opacity = r, n.style.display = i;
439
+ };
440
+ }, j = async () => {
441
+ let n = Math.min(t.videoWidth || i, i), o = Math.round(n * ((t.videoHeight || 9) / (t.videoWidth || 16))), c = document.createElement("canvas");
442
+ c.width = n, c.height = o;
443
+ let l = c.getContext("2d");
444
+ if (!l) throw Error("the picture in picture composite has no 2d context");
445
+ let u = document.createElement("video");
446
+ u.muted = !0, u.playsInline = !0, r === "burn-in" ? A(u) : u.style.cssText = "position:fixed;width:1px;height:1px;opacity:0;pointer-events:none;left:-1px;top:-1px", m(c, l);
447
+ let f = {
448
+ composite: c,
449
+ context: l,
450
+ stream: c.captureStream(),
451
+ mirror: u
452
+ };
453
+ if (a = f, u.srcObject = f.stream, (e.container ?? t.parentElement ?? document.body).appendChild(u), v(), s = setInterval(() => {
454
+ t.paused && h();
455
+ }, F), u.play().catch(() => {}), u.readyState === 0 && await new Promise((e) => {
437
456
  let t = () => {
438
457
  clearTimeout(n), e();
439
458
  }, n = setTimeout(t, 1e3);
440
- l.addEventListener("loadedmetadata", t, { once: !0 });
441
- }), l.addEventListener("play", v), l.addEventListener("pause", y), l.addEventListener("leavepictureinpicture", T), t.addEventListener("play", b), t.addEventListener("pause", x), C(!0), _(), await l.requestPictureInPicture();
459
+ u.addEventListener("loadedmetadata", t, { once: !0 });
460
+ }), d || a !== f) {
461
+ u.remove();
462
+ return;
463
+ }
464
+ if (u.addEventListener("play", x), u.addEventListener("pause", S), u.addEventListener("leavepictureinpicture", k), t.addEventListener("play", C), t.addEventListener("pause", w), t.addEventListener("seeked", T), D(!0), b(), r === "burn-in") {
465
+ t.paused && u.pause(), e.onBurnedInChange?.(!0);
466
+ return;
467
+ }
468
+ await u.requestPictureInPicture();
442
469
  };
443
470
  return {
444
471
  toggle: async () => {
445
- if (!(l || c)) {
446
- if (u()) {
447
- await document.exitPictureInPicture().catch(() => {});
448
- return;
449
- }
450
- if (!F()) {
451
- await E();
452
- return;
453
- }
454
- document.pictureInPictureElement && await document.exitPictureInPicture().catch(() => {}), c = !0;
472
+ if (!(d || u)) {
473
+ u = !0;
455
474
  try {
456
- await D();
457
- } catch (e) {
458
- console.warn("subtitle compositing for picture in picture failed, falling back", e), w(), await E().catch(() => {});
475
+ if (p()) {
476
+ if (r === "burn-in") {
477
+ O(), e.onBurnedInChange?.(!1);
478
+ return;
479
+ }
480
+ await document.exitPictureInPicture().catch(() => {});
481
+ return;
482
+ }
483
+ r !== "burn-in" && document.pictureInPictureElement && await document.exitPictureInPicture().catch(() => {});
484
+ try {
485
+ await j();
486
+ } catch (t) {
487
+ console.warn("subtitle compositing for picture in picture failed", t), O(), r === "burn-in" && e.onBurnedInChange?.(!1);
488
+ }
459
489
  } finally {
460
- c = !1;
490
+ u = !1;
461
491
  }
462
492
  }
463
493
  },
494
+ mode: r,
464
495
  destroy: () => {
465
- l = !0, u() && document.exitPictureInPicture().catch(() => {}), w();
496
+ d = !0, r !== "burn-in" && p() && document.exitPictureInPicture().catch(() => {}), O(), r === "burn-in" && e.onBurnedInChange?.(!1);
466
497
  }
467
498
  };
468
499
  };
469
500
  //#endregion
470
- export { w as a, a as c, T as i, s as l, M as n, c as o, C as r, m as s, I as t };
501
+ export { T as a, m as c, C as i, a as l, B as n, w as o, M as r, c as s, V as t, s as u };