@banou/media-player 0.10.41 → 0.10.43
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 +248 -232
- package/dist/react/hooks/use-frame-step.d.ts +3 -3
- package/package.json +1 -1
- package/src/lib/audio/volume-normalizer.ts +15 -1
- package/src/lib/react/hooks/use-frame-step.ts +28 -11
- package/src/lib/react/hooks/use-playback.ts +10 -6
- package/src/lib/react/hooks/use-thumbnails.ts +22 -1
- package/src/lib/utils/chapters.ts +2 -2
|
@@ -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
|
|
11
|
-
* faster, a 60 Hz display presents every other frame of a 60 fps file. A step while a seek waits for its data
|
|
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
|
@@ -80,6 +80,8 @@ export const attachVolumeNormalizer = (
|
|
|
80
80
|
let enabled = options.enabled !== false
|
|
81
81
|
let node: AudioWorkletNode | undefined
|
|
82
82
|
let released = false
|
|
83
|
+
// paused, so `follow` keeps the context suspended rather than resuming it
|
|
84
|
+
let held = false
|
|
83
85
|
const inputGainDb = () => 20 * Math.log10(Math.max(element.volume, 1e-6))
|
|
84
86
|
|
|
85
87
|
const document = element.ownerDocument
|
|
@@ -92,7 +94,8 @@ export const attachVolumeNormalizer = (
|
|
|
92
94
|
}
|
|
93
95
|
}
|
|
94
96
|
const follow = () => {
|
|
95
|
-
|
|
97
|
+
if (held && context.state === 'running') context.suspend().catch(() => {})
|
|
98
|
+
const suspended = !released && !held && context.state === 'suspended'
|
|
96
99
|
listen(suspended)
|
|
97
100
|
// Suspended by something else once routed: Chrome freezes the element and Firefox plays it silent until the
|
|
98
101
|
// context runs, and the page's activation already allows a resume.
|
|
@@ -111,6 +114,17 @@ export const attachVolumeNormalizer = (
|
|
|
111
114
|
// the live detector describes where the playhead was, and a new file owes nothing to the last one's level
|
|
112
115
|
element.addEventListener('seeking', () => created.port.postMessage({ reset: 'seek' }))
|
|
113
116
|
element.addEventListener('emptied', () => created.port.postMessage({ reset: 'source' }))
|
|
117
|
+
// Paused, a running context wakes the audio threads 390 to 620 times a second and a suspended one 20 to 30 (Chrome
|
|
118
|
+
// 156, 2026-11-17), while Firefox 157 wakes about 700 times a second either way. Resumed by play, the sound flows
|
|
119
|
+
// on within 45 ms in Chrome 156 and 80 ms in Firefox 157.
|
|
120
|
+
element.addEventListener('pause', () => {
|
|
121
|
+
held = true
|
|
122
|
+
if (context.state === 'running') context.suspend().catch(() => {})
|
|
123
|
+
})
|
|
124
|
+
element.addEventListener('play', () => {
|
|
125
|
+
held = false
|
|
126
|
+
resume()
|
|
127
|
+
})
|
|
114
128
|
node = created
|
|
115
129
|
return created
|
|
116
130
|
})()
|
|
@@ -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
|
|
44
|
-
* faster, a 60 Hz display presents every other frame of a 60 fps file. A step while a seek waits for its data
|
|
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
|
-
|
|
77
|
-
|
|
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
|
-
|
|
101
|
-
//
|
|
102
|
-
//
|
|
103
|
-
const
|
|
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
|
|
124
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 })
|
|
@@ -8,6 +8,11 @@ import { createThumbnailGenerator } from '../../engine'
|
|
|
8
8
|
// backs off because each failed init costs a wasm worker
|
|
9
9
|
const RETRY_DELAY = 5_000
|
|
10
10
|
const MAX_RETRY_DELAY = 60_000
|
|
11
|
+
// libav-wasm's thumbnailer starts with a read of this much from offset 0
|
|
12
|
+
const HEAD_BYTES = 1_000_000
|
|
13
|
+
|
|
14
|
+
const holdsHead = (ranges: [number, number][] | undefined, length: number) =>
|
|
15
|
+
!ranges || ranges.some(([start, end]) => start <= 0 && end >= Math.min(HEAD_BYTES, length))
|
|
11
16
|
|
|
12
17
|
export type UseSeekThumbnailsOptions = {
|
|
13
18
|
/** Optional alongside `length`/`read`: with no bytes there is nothing to generate from. */
|
|
@@ -46,6 +51,7 @@ export const useSeekThumbnails = ({
|
|
|
46
51
|
)
|
|
47
52
|
const rangesRef = useRef(ranges)
|
|
48
53
|
rangesRef.current = ranges
|
|
54
|
+
const retryOnHead = useRef<(() => void) | null>(null)
|
|
49
55
|
|
|
50
56
|
useEffect(() => {
|
|
51
57
|
if (!length || !read || !publicPath || !workerUrl) return
|
|
@@ -54,6 +60,8 @@ export const useSeekThumbnails = ({
|
|
|
54
60
|
let retry: ReturnType<typeof setTimeout> | undefined
|
|
55
61
|
let delay = RETRY_DELAY
|
|
56
62
|
const boot = () => {
|
|
63
|
+
retryOnHead.current = null
|
|
64
|
+
const early = !holdsHead(rangesRef.current, length)
|
|
57
65
|
createThumbnailGenerator({
|
|
58
66
|
publicPath,
|
|
59
67
|
workerUrl,
|
|
@@ -72,11 +80,21 @@ export const useSeekThumbnails = ({
|
|
|
72
80
|
if (cancelled) return
|
|
73
81
|
retry = setTimeout(boot, delay)
|
|
74
82
|
delay = Math.min(delay * 2, MAX_RETRY_DELAY)
|
|
83
|
+
// A reopened torrent refuses the head until the engine reports its pieces, so a boot that began before
|
|
84
|
+
// the head was readable goes again as soon as it is. Only that boot: any other failure keeps the backoff.
|
|
85
|
+
if (!early) return
|
|
86
|
+
const again = () => {
|
|
87
|
+
clearTimeout(retry)
|
|
88
|
+
boot()
|
|
89
|
+
}
|
|
90
|
+
if (holdsHead(rangesRef.current, length)) again()
|
|
91
|
+
else retryOnHead.current = again
|
|
75
92
|
})
|
|
76
93
|
}
|
|
77
94
|
boot()
|
|
78
95
|
return () => {
|
|
79
96
|
cancelled = true
|
|
97
|
+
retryOnHead.current = null
|
|
80
98
|
clearTimeout(retry)
|
|
81
99
|
generatorRef.current = null
|
|
82
100
|
generator?.destroy()
|
|
@@ -84,7 +102,10 @@ export const useSeekThumbnails = ({
|
|
|
84
102
|
}
|
|
85
103
|
}, [length, publicPath, workerUrl])
|
|
86
104
|
|
|
87
|
-
useEffect(() => {
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
generatorRef.current?.update(ranges)
|
|
107
|
+
if (length && holdsHead(ranges, length)) retryOnHead.current?.()
|
|
108
|
+
}, [ranges])
|
|
88
109
|
|
|
89
110
|
const requestThumbnail = useCallback((time: number | undefined) => {
|
|
90
111
|
generatorRef.current?.prioritize(time)
|
|
@@ -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
|
|
195
|
-
*
|
|
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)
|