@banou/media-player 0.10.24 → 0.10.25

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.
@@ -10,5 +10,7 @@ import type { PlayerMedia } from '../media';
10
10
  * The frame's length is learned from the gaps between frames presented one after another while playing at up to 1x:
11
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
12
  * from that seek's target.
13
+ *
14
+ * A media without `requestVideoFrameCallback` is stepped by `hostStep`, when its host can step it.
13
15
  */
14
- export declare const useFrameStep: (media: PlayerMedia | null) => void;
16
+ export declare const useFrameStep: (media: PlayerMedia | null, hostStep?: (direction: 1 | -1) => Promise<void>) => void;
@@ -218,7 +218,10 @@ export type SourceState = {
218
218
  * caller should seek directly.
219
219
  */
220
220
  requestSeek?: (time: number) => void;
221
- /** One frame back or forward, pausing first. Undefined for a media whose frames this document cannot see. */
221
+ /**
222
+ * One frame back or forward, pausing first. Undefined for a media whose frames this document cannot see, unless its
223
+ * host steps it (the remote arm's `stepFrame`).
224
+ */
222
225
  stepFrame?: (direction: 1 | -1) => void;
223
226
  /** Set when the pipeline fails. Cleared when it recovers. */
224
227
  playbackError: unknown;
@@ -367,7 +370,10 @@ export declare const sourceFeature: import("@videojs/react").PlayerFeature<{
367
370
  * caller should seek directly.
368
371
  */
369
372
  requestSeek?: (time: number) => void;
370
- /** One frame back or forward, pausing first. Undefined for a media whose frames this document cannot see. */
373
+ /**
374
+ * One frame back or forward, pausing first. Undefined for a media whose frames this document cannot see, unless its
375
+ * host steps it (the remote arm's `stepFrame`).
376
+ */
371
377
  stepFrame?: (direction: 1 | -1) => void;
372
378
  /** Set when the pipeline fails. Cleared when it recovers. */
373
379
  playbackError: unknown;
@@ -217,6 +217,15 @@ export type MediaPlayerRemoteOptions = CommonOptions & {
217
217
  * host takes on by passing it. Read by value, so it may be written inline.
218
218
  */
219
219
  pictureInPicture?: PassThroughPictureInPicture;
220
+ /**
221
+ * Steps the media one frame back (-1) or forward (1), made by the host inside the media's own
222
+ * document, where its frames can be seen; pausing first is the host's. Used only for a media
223
+ * without `requestVideoFrameCallback`, as one in another document is: a media with it is stepped
224
+ * by the player, and without either `<` and `>` do nothing. One press before the promise settles
225
+ * waits for it and the rest are dropped, so a held key steps as fast as the host lands them, and
226
+ * a rejection is logged. Read by value, so it may be written inline.
227
+ */
228
+ stepFrame?: (direction: 1 | -1) => Promise<void>;
220
229
  };
221
230
  export type MediaPlayerOptions = MediaPlayerLocalOptions | MediaPlayerRemoteOptions;
222
231
  export declare const MediaPlayer: (options: MediaPlayerOptions & {
@@ -564,8 +564,10 @@ export const createAnalysis = ({ toWorklet, toPage = () => {} }) => {
564
564
  toWorklet({ drop: block })
565
565
  }
566
566
  if (!Number.isFinite(level)) return
567
- for (let block = Math.max(0, here - 1); block <= here + 3; block++) {
568
- if (block * BLOCK_BINS >= endBin) break
567
+ // the playhead's own block first: under load each took 21 to 43 ms, and sent after the one before it, a seek's
568
+ // cut over HTTP reached the worklet 64 to 75 ms after the seek, its audio 72 to 83 ms (HOR-888, 2026-10-23)
569
+ for (const block of [here, here - 1, here + 1, here + 2, here + 3]) {
570
+ if (block < 0 || block * BLOCK_BINS >= endBin) continue
569
571
  const start = block * BLOCK_BINS
570
572
  const known = signature(start - MARGIN_BINS, start + BLOCK_BINS + MARGIN_BINS)
571
573
  const last = sent.get(block)
@@ -1064,14 +1066,16 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
1064
1066
  * finds it at all, is taken only once the next search, 250 ms later, peaks within 2 ms of it, clear or not: voiced
1065
1067
  * dialogue repeats with its pitch, so a peak a pitch period off can come out ahead, and a FLAC run once matched 45 ms
1066
1068
  * off with every check passed. Over the whole 250 ms either way an Opus file's matches were never that clear (HOR-498
1067
- * build, 2026-10-17).
1069
+ * build, 2026-10-17). Kept past that next one, a candidate that a match came back to after four elsewhere locked an
1070
+ * Opus run under load 18 ms off, every cut that much late (HOR-888, 2026-10-23).
1068
1071
  */
1069
1072
  found(offset, clear) {
1070
1073
  const near = (other) => other !== null && Math.abs(offset - other) <= 0.002
1071
- if (near(this.candidate)) {
1074
+ const confirmed = near(this.candidate)
1075
+ this.candidate = null
1076
+ if (confirmed) {
1072
1077
  this.offset = offset
1073
1078
  this.doubt = false
1074
- this.candidate = null
1075
1079
  } else if (clear) {
1076
1080
  if (!this.doubt && near(this.offset)) this.offset = offset
1077
1081
  else this.candidate = offset
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@banou/media-player",
3
- "version": "0.10.24",
3
+ "version": "0.10.25",
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",
@@ -564,8 +564,10 @@ export const createAnalysis = ({ toWorklet, toPage = () => {} }) => {
564
564
  toWorklet({ drop: block })
565
565
  }
566
566
  if (!Number.isFinite(level)) return
567
- for (let block = Math.max(0, here - 1); block <= here + 3; block++) {
568
- if (block * BLOCK_BINS >= endBin) break
567
+ // the playhead's own block first: under load each took 21 to 43 ms, and sent after the one before it, a seek's
568
+ // cut over HTTP reached the worklet 64 to 75 ms after the seek, its audio 72 to 83 ms (HOR-888, 2026-10-23)
569
+ for (const block of [here, here - 1, here + 1, here + 2, here + 3]) {
570
+ if (block < 0 || block * BLOCK_BINS >= endBin) continue
569
571
  const start = block * BLOCK_BINS
570
572
  const known = signature(start - MARGIN_BINS, start + BLOCK_BINS + MARGIN_BINS)
571
573
  const last = sent.get(block)
@@ -1064,14 +1066,16 @@ const defineProcessor = () => class VolumeNormalizer extends AudioWorkletProcess
1064
1066
  * finds it at all, is taken only once the next search, 250 ms later, peaks within 2 ms of it, clear or not: voiced
1065
1067
  * dialogue repeats with its pitch, so a peak a pitch period off can come out ahead, and a FLAC run once matched 45 ms
1066
1068
  * off with every check passed. Over the whole 250 ms either way an Opus file's matches were never that clear (HOR-498
1067
- * build, 2026-10-17).
1069
+ * build, 2026-10-17). Kept past that next one, a candidate that a match came back to after four elsewhere locked an
1070
+ * Opus run under load 18 ms off, every cut that much late (HOR-888, 2026-10-23).
1068
1071
  */
1069
1072
  found(offset, clear) {
1070
1073
  const near = (other) => other !== null && Math.abs(offset - other) <= 0.002
1071
- if (near(this.candidate)) {
1074
+ const confirmed = near(this.candidate)
1075
+ this.candidate = null
1076
+ if (confirmed) {
1072
1077
  this.offset = offset
1073
1078
  this.doubt = false
1074
- this.candidate = null
1075
1079
  } else if (clear) {
1076
1080
  if (!this.doubt && near(this.offset)) this.offset = offset
1077
1081
  else this.candidate = offset
@@ -1,6 +1,6 @@
1
1
  import type { PlayerMedia } from '../media'
2
2
 
3
- import { useEffect } from 'react'
3
+ import { useEffect, useRef } from 'react'
4
4
 
5
5
  import { usePlayer } from '../player'
6
6
 
@@ -12,6 +12,26 @@ const presented = (video: HTMLVideoElement, timeout: number) => new Promise<void
12
12
  video.requestVideoFrameCallback(() => { clearTimeout(timer); done() })
13
13
  })
14
14
 
15
+ // one press waits for the step on its way, so two quick presses move two frames, and the rest are dropped, so a held key
16
+ // steps as fast as frames land
17
+ const oneAtATime = (step: (direction: 1 | -1) => Promise<void>) => {
18
+ let busy = false
19
+ let next: 1 | -1 | undefined
20
+ const run = async (direction: 1 | -1) => {
21
+ if (busy) { next = direction; return }
22
+ busy = true
23
+ try {
24
+ await step(direction)
25
+ } finally {
26
+ busy = false
27
+ const queued = next
28
+ next = undefined
29
+ if (queued) void run(queued)
30
+ }
31
+ }
32
+ return run
33
+ }
34
+
15
35
  /**
16
36
  * Offers `stepFrame` for a media whose frames this document can see, one with `requestVideoFrameCallback`.
17
37
  *
@@ -23,13 +43,31 @@ const presented = (video: HTMLVideoElement, timeout: number) => new Promise<void
23
43
  * The frame's length is learned from the gaps between frames presented one after another while playing at up to 1x:
24
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
25
45
  * from that seek's target.
46
+ *
47
+ * A media without `requestVideoFrameCallback` is stepped by `hostStep`, when its host can step it.
26
48
  */
27
- export const useFrameStep = (media: PlayerMedia | null) => {
49
+ export const useFrameStep = (media: PlayerMedia | null, hostStep?: (direction: 1 | -1) => Promise<void>) => {
28
50
  const player = usePlayer()
29
51
  const setSourceState = usePlayer((state) => state.setSourceState)
52
+ const host = useRef(hostStep)
53
+ host.current = hostStep
54
+ const hosted = !!hostStep
30
55
  useEffect(() => {
31
56
  const video = media as HTMLVideoElement | null
32
- if (!video || typeof video.requestVideoFrameCallback !== 'function') return
57
+ if (!video) return
58
+ if (typeof video.requestVideoFrameCallback !== 'function') {
59
+ if (!hosted) return
60
+ setSourceState({
61
+ stepFrame: oneAtATime(async (direction) => {
62
+ try {
63
+ await host.current?.(direction)
64
+ } catch (error) {
65
+ console.warn('the host could not step a frame', error)
66
+ }
67
+ }),
68
+ })
69
+ return () => setSourceState({ stepFrame: undefined })
70
+ }
33
71
 
34
72
  let played: VideoFrameCallbackMetadata | undefined
35
73
  let bySeek = false
@@ -66,38 +104,30 @@ export const useFrameStep = (media: PlayerMedia | null) => {
66
104
  return near.reduce((sum, gap) => sum + gap, 0) / near.length
67
105
  }
68
106
 
69
- let busy = false
70
- const stepFrame = async (direction: 1 | -1) => {
71
- // a key repeat while a step is on its way is dropped, so a held key steps as fast as frames land
72
- if (busy) return
73
- busy = true
74
- try {
75
- if (!video.paused) {
76
- video.pause()
77
- // Chrome can still report the frame on screen after the pause
78
- await presented(video, 100)
79
- }
80
- const length = frameLength()
81
- // off `state`: the store has a getter only for the fields it was created with
82
- const pending = player.state.seekingTo
83
- const target = pending !== undefined
84
- ? pending + direction * length
85
- : played
86
- ? played.mediaTime + (direction > 0 ? 1.5 : -0.5) * length
87
- : video.currentTime + direction * length
88
- const time = Math.max(0, Math.min(target, video.duration || target))
89
- const landed = new Promise<void>((done) => {
90
- const timer = setTimeout(done, 1000)
91
- video.addEventListener('seeked', () => { clearTimeout(timer); done() }, { once: true })
92
- })
93
- const { requestSeek } = player.state
94
- if (requestSeek) requestSeek(time)
95
- else video.currentTime = time
96
- await landed
97
- } finally {
98
- busy = false
107
+ const stepFrame = oneAtATime(async (direction) => {
108
+ if (!video.paused) {
109
+ video.pause()
110
+ // Chrome can still report the frame on screen after the pause
111
+ await presented(video, 100)
99
112
  }
100
- }
113
+ const length = frameLength()
114
+ // off `state`: the store has a getter only for the fields it was created with
115
+ const pending = player.state.seekingTo
116
+ const target = pending !== undefined
117
+ ? pending + direction * length
118
+ : played
119
+ ? played.mediaTime + (direction > 0 ? 1.5 : -0.5) * length
120
+ : video.currentTime + direction * length
121
+ const time = Math.max(0, Math.min(target, video.duration || target))
122
+ const landed = new Promise<void>((done) => {
123
+ const timer = setTimeout(done, 1000)
124
+ video.addEventListener('seeked', () => { clearTimeout(timer); done() }, { once: true })
125
+ })
126
+ const { requestSeek } = player.state
127
+ if (requestSeek) requestSeek(time)
128
+ else video.currentTime = time
129
+ await landed
130
+ })
101
131
  setSourceState({ stepFrame })
102
132
  return () => {
103
133
  video.removeEventListener('seeking', onSeeking)
@@ -105,5 +135,5 @@ export const useFrameStep = (media: PlayerMedia | null) => {
105
135
  video.cancelVideoFrameCallback(handle)
106
136
  setSourceState({ stepFrame: undefined })
107
137
  }
108
- }, [media, player, setSourceState])
138
+ }, [media, hosted, player, setSourceState])
109
139
  }
@@ -238,7 +238,10 @@ export type SourceState = {
238
238
  */
239
239
  requestSeek?: (time: number) => void
240
240
 
241
- /** One frame back or forward, pausing first. Undefined for a media whose frames this document cannot see. */
241
+ /**
242
+ * One frame back or forward, pausing first. Undefined for a media whose frames this document cannot see, unless its
243
+ * host steps it (the remote arm's `stepFrame`).
244
+ */
242
245
  stepFrame?: (direction: 1 | -1) => void
243
246
 
244
247
  /** Set when the pipeline fails. Cleared when it recovers. */
@@ -244,6 +244,15 @@ export type MediaPlayerRemoteOptions =
244
244
  * host takes on by passing it. Read by value, so it may be written inline.
245
245
  */
246
246
  pictureInPicture?: PassThroughPictureInPicture
247
+ /**
248
+ * Steps the media one frame back (-1) or forward (1), made by the host inside the media's own
249
+ * document, where its frames can be seen; pausing first is the host's. Used only for a media
250
+ * without `requestVideoFrameCallback`, as one in another document is: a media with it is stepped
251
+ * by the player, and without either `<` and `>` do nothing. One press before the promise settles
252
+ * waits for it and the rest are dropped, so a held key steps as fast as the host lands them, and
253
+ * a rejection is logged. Read by value, so it may be written inline.
254
+ */
255
+ stepFrame?: (direction: 1 | -1) => Promise<void>
247
256
  }
248
257
 
249
258
  export type MediaPlayerOptions = MediaPlayerLocalOptions | MediaPlayerRemoteOptions
@@ -274,7 +283,7 @@ const PlayerRoot = ({ options, children }: { options: MediaPlayerOptions, childr
274
283
  // permanent no-op.
275
284
  const media = remote?.media ?? video
276
285
  useEffect(() => { setMedia?.(media); return () => setMedia?.(null) }, [media, setMedia])
277
- useFrameStep(media)
286
+ useFrameStep(media, remote?.stepFrame)
278
287
 
279
288
  // Served after it is attached, and re-served when the media changes: what an embedder drives is
280
289
  // whatever this player drives at the time. Keyed on the option's `origin` and `channel`, not on the