@banou/media-player 0.10.42 → 0.10.44

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/dist/index.js CHANGED
@@ -187,7 +187,10 @@ var se = {
187
187
  S(() => (F.current = {
188
188
  count: 0,
189
189
  at: 0
190
- }, a.setSourceState({ playbackErrors: [] }), () => {
190
+ }, a.setSourceState({
191
+ playbackErrors: [],
192
+ seekingTo: void 0
193
+ }), () => {
191
194
  I.current && clearTimeout(I.current);
192
195
  }), [c]);
193
196
  let L = w(null), R = w(null), z = w(void 0), B = w(c);
@@ -233,7 +236,7 @@ var se = {
233
236
  return;
234
237
  }
235
238
  let n = typeof window < "u" && window.__mediaPlayerSeekDebug, r = performance.now(), i = !1, o = "prepared", s = () => {
236
- if (!i && (i = !0, a.seek(e), n)) {
239
+ if (!i && (i = !0, L.current === t && (a.seek(e), n))) {
237
240
  let n = t.videoElement, i = 0;
238
241
  if (n) for (let t = 0; t < n.buffered.length; t++) n.buffered.start(t) <= e + 1 && e < n.buffered.end(t) && (i = n.buffered.end(t) - e);
239
242
  console.warn(`[media-player] seek to ${e.toFixed(2)} moved after ${Math.round(performance.now() - r)}ms via ${o}, runway ${i.toFixed(1)}s${o === "deadline" ? " (EXPOSED: moved before its data was ready)" : ""}`);
@@ -339,7 +342,7 @@ var se = {
339
342
  if (n) return;
340
343
  a.setSourceState({ ready: !0 });
341
344
  let t = R.current;
342
- R.current = null, t && t.size === c && t.time > 0 && (e.currentTime = t.time), E && e.play().catch(() => {});
345
+ R.current = null, t && t.size === c && t.time > 0 ? e.currentTime = t.time : a.setSourceState({ seekingTo: void 0 }), E && e.play().catch(() => {});
343
346
  },
344
347
  onError: u,
345
348
  onRecovered: () => {
@@ -378,7 +381,7 @@ var se = {
378
381
  }
379
382
  })(), () => {
380
383
  n = !0, R.current = {
381
- time: e.currentTime,
384
+ time: a.state.seekingTo ?? e.currentTime,
382
385
  size: c
383
386
  }, L.current?.destroy(), L.current = null, a.setSourceState({ ready: !1 });
384
387
  };
@@ -439,31 +442,31 @@ var se = {
439
442
  }) }), () => r({ stepFrame: void 0 })) : void 0;
440
443
  let o, s = !1, c = [], l = () => {
441
444
  s = !0, o = void 0;
442
- }, u = () => {
443
- o = void 0, c.length = 0;
445
+ }, u = () => {}, d = () => {
446
+ o = void 0, c.length = 0, u();
444
447
  };
445
- t.addEventListener("seeking", l), t.addEventListener("emptied", u);
446
- let d = 0, f = (e, n) => {
448
+ t.addEventListener("seeking", l), t.addEventListener("emptied", d);
449
+ let f = 0, p = (e, n) => {
447
450
  let r = s;
448
- !t.paused && !t.seeking && (s = !1), !r && !t.paused && t.playbackRate <= 1 && o && n.presentedFrames === o.presentedFrames + 1 && n.mediaTime > o.mediaTime && (c.push(n.mediaTime - o.mediaTime), c.length > 60 && c.shift()), o = r ? void 0 : n, d = t.requestVideoFrameCallback(f);
451
+ !t.paused && !t.seeking && (s = !1), !r && !t.paused && t.playbackRate <= 1 && o && n.presentedFrames === o.presentedFrames + 1 && n.mediaTime > o.mediaTime && (c.push(n.mediaTime - o.mediaTime), c.length > 60 && c.shift()), o = r ? void 0 : n, f = t.requestVideoFrameCallback(p);
449
452
  };
450
- d = t.requestVideoFrameCallback(f);
451
- let p = () => {
453
+ f = t.requestVideoFrameCallback(p);
454
+ let m = () => {
452
455
  if (c.length < 5) return je;
453
- let e = [...c].sort((e, t) => e - t), t = e[e.length >> 1], n = e.filter((e) => Math.abs(e - t) <= t / 4);
456
+ let e = [...c].sort((e, t) => e - t), t = e[Math.floor(e.length / 10)], n = e.filter((e) => Math.abs(e - t) <= t / 4);
454
457
  return n.reduce((e, t) => e + t, 0) / n.length;
455
- }, m = Ne(async (e) => {
456
- t.paused || (t.pause(), await Me(t, 100));
457
- let r = p(), i = n.state.seekingTo, a = i === void 0 ? o ? o.mediaTime + (e > 0 ? 1.5 : -.5) * r : t.currentTime + e * r : i + e * r, s = Math.max(0, Math.min(a, t.duration || a)), c = new Promise((e) => {
458
- let n = setTimeout(e, 1e3);
459
- t.addEventListener("seeked", () => {
460
- clearTimeout(n), e();
461
- }, { once: !0 });
458
+ }, h = !0, g = Ne(async (e) => {
459
+ if (!h || t.readyState === HTMLMediaElement.HAVE_NOTHING || !t.paused && (t.pause(), await Me(t, 100), !h)) return;
460
+ let r = m(), i = n.state.seekingTo, a = i === void 0 ? o ? o.mediaTime + (e > 0 ? 1.5 : -.5) * r : t.currentTime + e * r : i + e * r, s = Math.max(0, Math.min(a, t.duration || a)), c = new Promise((e) => {
461
+ let n = () => {
462
+ clearTimeout(r), t.removeEventListener("seeked", n), e();
463
+ }, r = setTimeout(n, 15e3);
464
+ u = n, t.addEventListener("seeked", n);
462
465
  }), { requestSeek: l } = n.state;
463
466
  l ? l(s) : t.currentTime = s, await c;
464
467
  });
465
- return r({ stepFrame: m }), () => {
466
- t.removeEventListener("seeking", l), t.removeEventListener("emptied", u), t.cancelVideoFrameCallback(d), r({ stepFrame: void 0 });
468
+ return r({ stepFrame: g }), () => {
469
+ h = !1, t.removeEventListener("seeking", l), t.removeEventListener("emptied", d), t.cancelVideoFrameCallback(f), r({ stepFrame: void 0 });
467
470
  };
468
471
  }, [
469
472
  e,
@@ -1909,7 +1912,7 @@ ${Nt}
1909
1912
  }
1910
1913
  }
1911
1914
  }
1912
- `, Lt = (e, t) => new Date(e).toLocaleTimeString(t), Rt = (e) => e === void 0 || !Number.isFinite(e) ? void 0 : _t(Math.max(0, e)), zt = (e) => e.reduce((e, t) => e + t.count, 0), Bt = (e) => e.map((e, t) => {
1915
+ `, Lt = (e, t) => new Date(e).toLocaleTimeString(t ?? "en"), Rt = (e) => e === void 0 || !Number.isFinite(e) ? void 0 : _t(Math.max(0, e)), zt = (e) => e.reduce((e, t) => e + t.count, 0), Bt = (e) => e.map((e, t) => {
1913
1916
  let n = Rt(e.atMediaTime), r = e.count > 1 ? `${new Date(e.at).toISOString()} to ${new Date(e.lastAt).toISOString()} ×${e.count}` : new Date(e.at).toISOString();
1914
1917
  return [
1915
1918
  `${t + 1}. ${r}${n ? ` (at ${n})` : ""}${e.recovered ? " [recovered]" : ""}`,
@@ -7,9 +7,9 @@ import type { PlayerMedia } from '../media';
7
7
  * so the step starts from the last frame presented while playing and aims at the middle of the next one. A frame a
8
8
  * seek presented is never read for its time: Firefox reports the seek's target as its `mediaTime`.
9
9
  *
10
- * The frame's length is learned from the gaps between frames presented one after another while playing at up to 1x:
11
- * faster, a 60 Hz display presents every other frame of a 60 fps file. A step while a seek waits for its data starts
12
- * from that seek's target.
10
+ * The frame's length is learned from the shortest gaps between frames presented one after another while playing at up
11
+ * to 1x: faster, a 60 Hz display presents every other frame of a 60 fps file. A step while a seek waits for its data
12
+ * starts from that seek's target.
13
13
  *
14
14
  * A media without `requestVideoFrameCallback` is stepped by `hostStep`, when its host can step it.
15
15
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@banou/media-player",
3
- "version": "0.10.42",
3
+ "version": "0.10.44",
4
4
  "description": "A video player for containers and codecs the browser cannot play natively, remuxed on the fly",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -103,7 +103,8 @@ ${popoverStyle}
103
103
  }
104
104
  `
105
105
 
106
- const clock = (at: number, locale: string | undefined) => new Date(at).toLocaleTimeString(locale)
106
+ // English when the player was given no language, as its numbers are, never the browser's own digits
107
+ const clock = (at: number, locale: string | undefined) => new Date(at).toLocaleTimeString(locale ?? 'en')
107
108
 
108
109
  // the chrome's own formatter, so a position in the log reads exactly like the one on the seekbar
109
110
  const mediaTime = (seconds: number | undefined) =>
@@ -40,9 +40,9 @@ const oneAtATime = (step: (direction: 1 | -1) => Promise<void>) => {
40
40
  * so the step starts from the last frame presented while playing and aims at the middle of the next one. A frame a
41
41
  * seek presented is never read for its time: Firefox reports the seek's target as its `mediaTime`.
42
42
  *
43
- * The frame's length is learned from the gaps between frames presented one after another while playing at up to 1x:
44
- * faster, a 60 Hz display presents every other frame of a 60 fps file. A step while a seek waits for its data starts
45
- * from that seek's target.
43
+ * The frame's length is learned from the shortest gaps between frames presented one after another while playing at up
44
+ * to 1x: faster, a 60 Hz display presents every other frame of a 60 fps file. A step while a seek waits for its data
45
+ * starts from that seek's target.
46
46
  *
47
47
  * A media without `requestVideoFrameCallback` is stepped by `hostStep`, when its host can step it.
48
48
  */
@@ -73,8 +73,10 @@ export const useFrameStep = (media: PlayerMedia | null, hostStep?: (direction: 1
73
73
  let bySeek = false
74
74
  const gaps: number[] = []
75
75
  const onSeeking = () => { bySeek = true; played = undefined }
76
- // a new media on the same element
77
- const onEmptied = () => { played = undefined; gaps.length = 0 }
76
+ let settleLanding = () => {}
77
+ // a new media on the same element, or a rebuild of this one: its load ends a step's seek with no seeked (Chrome 156
78
+ // and Firefox 157)
79
+ const onEmptied = () => { played = undefined; gaps.length = 0; settleLanding() }
78
80
  video.addEventListener('seeking', onSeeking)
79
81
  video.addEventListener('emptied', onEmptied)
80
82
  let handle = 0
@@ -97,18 +99,28 @@ export const useFrameStep = (media: PlayerMedia | null, hostStep?: (direction: 1
97
99
  const frameLength = () => {
98
100
  if (gaps.length < 5) return FALLBACK_FRAME
99
101
  const sorted = [...gaps].sort((a, b) => a - b)
100
- const median = sorted[sorted.length >> 1]!
101
- // Matroska's millisecond timestamps give gaps of 41 and 42 ms for a 41.708 ms frame: steps by their median of 42
102
- // drift by a frame within about 65, steps by the mean of the gaps near it do not
103
- const near = sorted.filter((gap) => Math.abs(gap - median) <= median / 4)
102
+ // A frame the display drops lengthens a gap and nothing shortens one, so the frame is read a tenth of the way up:
103
+ // on one loaded CPU, Chrome 156 played a 60 fps file with as few as 14% of its gaps one frame long, and their
104
+ // median, or a fifth of the way up, stepped two frames (2026-11-20)
105
+ const short = sorted[Math.floor(sorted.length / 10)]!
106
+ // Matroska's millisecond timestamps give gaps of 41 and 42 ms for a 41.708 ms frame: steps by 42 alone drift by a
107
+ // frame within about 65, steps by the mean of the gaps near it do not
108
+ const near = sorted.filter((gap) => Math.abs(gap - short) <= short / 4)
104
109
  return near.reduce((sum, gap) => sum + gap, 0) / near.length
105
110
  }
106
111
 
112
+ let active = true
107
113
  const stepFrame = oneAtATime(async (direction) => {
114
+ // a press queued behind a step still landing when the player went away, whose store is destroyed by now
115
+ if (!active) return
116
+ // nothing to step from while a new media or a rebuild has emptied the element: its currentTime reads 0 until a
117
+ // media is back. This also drops a press that was waiting for a step the load ended
118
+ if (video.readyState === HTMLMediaElement.HAVE_NOTHING) return
108
119
  if (!video.paused) {
109
120
  video.pause()
110
121
  // Chrome can still report the frame on screen after the pause
111
122
  await presented(video, 100)
123
+ if (!active) return
112
124
  }
113
125
  const length = frameLength()
114
126
  // off `state`: the store has a getter only for the fields it was created with
@@ -119,9 +131,13 @@ export const useFrameStep = (media: PlayerMedia | null, hostStep?: (direction: 1
119
131
  ? played.mediaTime + (direction > 0 ? 1.5 : -0.5) * length
120
132
  : video.currentTime + direction * length
121
133
  const time = Math.max(0, Math.min(target, video.duration || target))
134
+ // Only a backstop for a seek that never lands, as the chrome's seek display has: in Firefox 157 on one loaded CPU a
135
+ // step at the end of a GOP took up to 1.96 s to land, and a next step sent at 1 s left its frame unshown (2026-11-20)
122
136
  const landed = new Promise<void>((done) => {
123
- const timer = setTimeout(done, 1000)
124
- video.addEventListener('seeked', () => { clearTimeout(timer); done() }, { once: true })
137
+ const settle = () => { clearTimeout(timer); video.removeEventListener('seeked', settle); done() }
138
+ const timer = setTimeout(settle, 15_000)
139
+ settleLanding = settle
140
+ video.addEventListener('seeked', settle)
125
141
  })
126
142
  const { requestSeek } = player.state
127
143
  if (requestSeek) requestSeek(time)
@@ -130,6 +146,7 @@ export const useFrameStep = (media: PlayerMedia | null, hostStep?: (direction: 1
130
146
  })
131
147
  setSourceState({ stepFrame })
132
148
  return () => {
149
+ active = false
133
150
  video.removeEventListener('seeking', onSeeking)
134
151
  video.removeEventListener('emptied', onEmptied)
135
152
  video.cancelVideoFrameCallback(handle)
@@ -119,10 +119,11 @@ export const usePlayback = (
119
119
  const restarts = useRef({ count: 0, at: 0 })
120
120
  const restartTimer = useRef<ReturnType<typeof setTimeout>>(undefined)
121
121
  // The streak belongs to one media, not to the player, and a pending rebuild of the old one must
122
- // not land on the new one.
122
+ // not land on the new one. Nor may a seek still on its way to the old one stay on the clock: cleared
123
+ // here and not on `emptied`, which a rebuild of the same media fires too.
123
124
  useEffect(() => {
124
125
  restarts.current = { count: 0, at: 0 }
125
- player.setSourceState({ playbackErrors: [] })
126
+ player.setSourceState({ playbackErrors: [], seekingTo: undefined })
126
127
  return () => { if (restartTimer.current) clearTimeout(restartTimer.current) }
127
128
  // `player` is stable; listing it would not re-run this, and the reset belongs to the media
128
129
  // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -244,6 +245,8 @@ export const usePlayback = (
244
245
  const move = () => {
245
246
  if (moved) return
246
247
  moved = true
248
+ // the pipeline went away: a new media starts at its own beginning, and a rebuild resumes at this seek itself
249
+ if (controllerRef.current !== controller) return
247
250
  player.seek(time)
248
251
  if (debug) {
249
252
  // eslint-disable-next-line no-console
@@ -392,9 +395,9 @@ export const usePlayback = (
392
395
  const resume = resumeRef.current
393
396
  resumeRef.current = null
394
397
  // only a position belonging to THIS media, or a new file opens where the last one stopped
395
- if (resume && resume.size === size && resume.time > 0) {
396
- video.currentTime = resume.time
397
- }
398
+ if (resume && resume.size === size && resume.time > 0) video.currentTime = resume.time
399
+ // a seek carried to 0 is where the new element already is, so no 'seeked' comes to take it off the clock
400
+ else player.setSourceState({ seekingTo: undefined })
398
401
  if (autoplay) video.play().catch(() => {})
399
402
  },
400
403
  onError: fail,
@@ -438,7 +441,8 @@ export const usePlayback = (
438
441
  })()
439
442
  return () => {
440
443
  cancelled = true
441
- resumeRef.current = { time: video.currentTime, size }
444
+ // a seek still waiting for its data has not moved the element yet, and it is where the viewer asked to be
445
+ resumeRef.current = { time: player.state.seekingTo ?? video.currentTime, size }
442
446
  controllerRef.current?.destroy()
443
447
  controllerRef.current = null
444
448
  player.setSourceState({ ready: false })
@@ -191,8 +191,8 @@ const kindOf = (title: string, hedged: boolean): ChapterKind | undefined => {
191
191
  * word on its own.
192
192
  *
193
193
  * A SEPARATOR is what makes the second one safe. Without it, reading the last word of a title
194
- * would skip a real scene called "Proclamation of a meeting opening", which is an actual chapter
195
- * in a real release. That title has no separator, so it stays one many-word segment and matches
194
+ * would skip a scene called "Announcement of a store opening", the kind of chapter
195
+ * a real release can carry. That title has no separator, so it stays one many-word segment and matches
196
196
  * nothing.
197
197
  */
198
198
  const segments = whole.split(LABEL_SEPARATORS).map((part) => part.trim()).filter(Boolean)