@banou/media-player 0.10.22 → 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.
- package/README.md +5 -2
- package/dist/engine/index.js +1 -1
- package/dist/{engine-Cr_7OImR.js → engine-BdouHadr.js} +6 -6
- package/dist/index.js +305 -235
- package/dist/react/hooks/use-frame-step.d.ts +16 -0
- package/dist/react/player.d.ts +1 -0
- package/dist/react/source-feature.d.ts +10 -0
- package/dist/react/video-player.d.ts +9 -0
- package/dist/volume-normalizer-worklet.js +9 -5
- package/package.json +3 -3
- package/src/lib/audio/volume-normalizer-worklet.js +9 -5
- package/src/lib/engine/playback.ts +16 -7
- package/src/lib/engine/subtitles.ts +11 -7
- package/src/lib/react/components/control-bar.tsx +6 -1
- package/src/lib/react/hooks/use-frame-step.ts +139 -0
- package/src/lib/react/source-feature.ts +6 -0
- package/src/lib/react/video-player.tsx +11 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { PlayerMedia } from '../media';
|
|
2
|
+
/**
|
|
3
|
+
* Offers `stepFrame` for a media whose frames this document can see, one with `requestVideoFrameCallback`.
|
|
4
|
+
*
|
|
5
|
+
* Paused by a seek, the element shows the frame holding `currentTime` (Chrome 155 and Firefox 157), so a step is
|
|
6
|
+
* `currentTime` plus or minus a frame. Paused from playing, Chrome shows the frame after that one in 15 of 40 pauses,
|
|
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
|
+
* seek presented is never read for its time: Firefox reports the seek's target as its `mediaTime`.
|
|
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.
|
|
13
|
+
*
|
|
14
|
+
* A media without `requestVideoFrameCallback` is stepped by `hostStep`, when its host can step it.
|
|
15
|
+
*/
|
|
16
|
+
export declare const useFrameStep: (media: PlayerMedia | null, hostStep?: (direction: 1 | -1) => Promise<void>) => void;
|
package/dist/react/player.d.ts
CHANGED
|
@@ -25,6 +25,7 @@ export declare const Player: import("@videojs/react").CreatePlayerResult<import(
|
|
|
25
25
|
burnedInSubtitles: boolean;
|
|
26
26
|
volumeNormalizer: import("./source-feature").VolumeNormalizerControl | null;
|
|
27
27
|
requestSeek?: (time: number) => void;
|
|
28
|
+
stepFrame?: (direction: 1 | -1) => void;
|
|
28
29
|
playbackError: unknown;
|
|
29
30
|
playbackErrors: import("./source-feature").PlaybackErrorEntry[];
|
|
30
31
|
ready: boolean;
|
|
@@ -218,6 +218,11 @@ export type SourceState = {
|
|
|
218
218
|
* caller should seek directly.
|
|
219
219
|
*/
|
|
220
220
|
requestSeek?: (time: number) => void;
|
|
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
|
+
*/
|
|
225
|
+
stepFrame?: (direction: 1 | -1) => void;
|
|
221
226
|
/** Set when the pipeline fails. Cleared when it recovers. */
|
|
222
227
|
playbackError: unknown;
|
|
223
228
|
/**
|
|
@@ -365,6 +370,11 @@ export declare const sourceFeature: import("@videojs/react").PlayerFeature<{
|
|
|
365
370
|
* caller should seek directly.
|
|
366
371
|
*/
|
|
367
372
|
requestSeek?: (time: number) => void;
|
|
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
|
+
*/
|
|
377
|
+
stepFrame?: (direction: 1 | -1) => void;
|
|
368
378
|
/** Set when the pipeline fails. Cleared when it recovers. */
|
|
369
379
|
playbackError: unknown;
|
|
370
380
|
/**
|
|
@@ -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
|
-
|
|
568
|
-
|
|
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
|
-
|
|
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.
|
|
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",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"@videojs/core": "10.0.0-beta.26",
|
|
44
44
|
"@videojs/react": "10.0.0-beta.26",
|
|
45
45
|
"ass-compiler": "^0.1.16",
|
|
46
|
-
"jassub": "^2.5.
|
|
47
|
-
"libav-wasm": "^0.8.
|
|
46
|
+
"jassub": "^2.5.23",
|
|
47
|
+
"libav-wasm": "^0.8.12",
|
|
48
48
|
"lucide-react": "^0.536.0",
|
|
49
49
|
"osra": "^0.6.12",
|
|
50
50
|
"react-tooltip": "^5.28.0"
|
|
@@ -564,8 +564,10 @@ export const createAnalysis = ({ toWorklet, toPage = () => {} }) => {
|
|
|
564
564
|
toWorklet({ drop: block })
|
|
565
565
|
}
|
|
566
566
|
if (!Number.isFinite(level)) return
|
|
567
|
-
|
|
568
|
-
|
|
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
|
-
|
|
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
|
|
@@ -499,14 +499,18 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
|
|
|
499
499
|
* BOTH the input and the output context and re-runs write_header, because the fragmented mp4 muxer
|
|
500
500
|
* cannot rewind. Skipping it when the target is already playable is most of what a seek costs.
|
|
501
501
|
*
|
|
502
|
-
* Buffered alone is not enough:
|
|
503
|
-
* somewhere ahead
|
|
504
|
-
* would stall at the end of this range instead.
|
|
505
|
-
*
|
|
506
|
-
*
|
|
507
|
-
*
|
|
502
|
+
* Buffered alone is not enough: the range holding the target has to reach the last remuxer position,
|
|
503
|
+
* since the reader is somewhere ahead of it and a gap between them can never be filled without a seek,
|
|
504
|
+
* so playback would stall at the end of this range instead. A target behind that position is just as
|
|
505
|
+
* safe once its range reaches it: seeking there anyway re-read the target's whole GOP over buffered
|
|
506
|
+
* data, and a quick seek forward during that re-read showed a frame from 15 s away in Chrome 155
|
|
507
|
+
* (measured 2026-10-23).
|
|
508
|
+
* Nor is reaching it enough: the range can be an island ahead of the reader, so the target also needs
|
|
509
|
+
* its runway, or the element plays to the island's end and waits there until the reader has read the
|
|
510
|
+
* whole gap, which over a torrent is as long as downloading it.
|
|
508
511
|
*/
|
|
509
|
-
const alreadyPlayable = (time: number) =>
|
|
512
|
+
const alreadyPlayable = (time: number) =>
|
|
513
|
+
getTimeRanges(sourceBuffer).some((r) => r.start <= time && time < r.end && r.end >= lastSeekPosition) && hasRunway(time)
|
|
510
514
|
|
|
511
515
|
/**
|
|
512
516
|
* Whether the element can play from a GIVEN time without a fetch.
|
|
@@ -558,6 +562,11 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
|
|
|
558
562
|
const prepareSeek = async (time: number) => {
|
|
559
563
|
if (destroyed) return
|
|
560
564
|
if (playableAt(time) && hasRunway(time)) return
|
|
565
|
+
// A drag still settling holds every read, so the runway would never be read, and its settle would then position the
|
|
566
|
+
// remuxer back where the drag stopped. A seek back under 250 ms after a seek forward is one (Firefox 153 and 157,
|
|
567
|
+
// 2026-10-23).
|
|
568
|
+
if (dragTimer) clearTimeout(dragTimer)
|
|
569
|
+
dragTimer = undefined
|
|
561
570
|
finished = false
|
|
562
571
|
preparing++
|
|
563
572
|
prepareTarget = time
|
|
@@ -10,7 +10,7 @@ export type SubtitleStream = { streamIndex: number, title: string, language: str
|
|
|
10
10
|
export const SUBTITLES_OFF = -1
|
|
11
11
|
|
|
12
12
|
type SubtitleHeaderPart = { type: 'header', streamIndex: number, content: string, eventsContent: string, styles: Map<string, number> }
|
|
13
|
-
type SubtitleDialoguePart = { type: 'dialogue', streamIndex: number,
|
|
13
|
+
type SubtitleDialoguePart = { type: 'dialogue', streamIndex: number, assEvent: ASSEvent }
|
|
14
14
|
|
|
15
15
|
export type SubtitleRendererOptions = {
|
|
16
16
|
video: HTMLVideoElement
|
|
@@ -125,7 +125,6 @@ const toDialoguePart = (header: SubtitleHeaderPart, fragment: SubtitleFragment &
|
|
|
125
125
|
return {
|
|
126
126
|
type: 'dialogue',
|
|
127
127
|
streamIndex: fragment.streamIndex,
|
|
128
|
-
index: dialogueIndex,
|
|
129
128
|
assEvent: {
|
|
130
129
|
Name: event.Name ?? '',
|
|
131
130
|
// libass wants the raw effect field and ass-compiler has already destructured it into an
|
|
@@ -158,7 +157,7 @@ export const createSubtitleRenderer = (options: SubtitleRendererOptions) => {
|
|
|
158
157
|
let attachments: [string, Uint8Array][] = []
|
|
159
158
|
const headers = new Map<number, SubtitleHeaderPart>()
|
|
160
159
|
const streams: SubtitleStream[] = []
|
|
161
|
-
const dialogues = new Map<number, Map<
|
|
160
|
+
const dialogues = new Map<number, Map<string, SubtitleDialoguePart>>()
|
|
162
161
|
let selected: number | undefined
|
|
163
162
|
let onStreams = options.onStreams
|
|
164
163
|
/**
|
|
@@ -333,11 +332,16 @@ export const createSubtitleRenderer = (options: SubtitleRendererOptions) => {
|
|
|
333
332
|
} else {
|
|
334
333
|
const header = headers.get(fragment.streamIndex)
|
|
335
334
|
if (!header) continue
|
|
336
|
-
let
|
|
337
|
-
if (!
|
|
335
|
+
let byLine = dialogues.get(fragment.streamIndex)
|
|
336
|
+
if (!byLine) { byLine = new Map(); dialogues.set(fragment.streamIndex, byLine) }
|
|
337
|
+
// Everything but the leading ReadOrder: a line libav converts from SubRip, WebVTT or mov_text
|
|
338
|
+
// takes it from a counter in FFmpeg's decoder, so the same cue read again after a seek comes
|
|
339
|
+
// back under a new one. The cost: an ASS line repeating another in every other field draws
|
|
340
|
+
// once, where libass draws both.
|
|
341
|
+
const key = `${fragment.start},${fragment.end},${fragment.content.slice(fragment.content.indexOf(',') + 1)}`
|
|
342
|
+
if (byLine.has(key)) continue
|
|
338
343
|
const part = toDialoguePart(header, fragment)
|
|
339
|
-
|
|
340
|
-
byIndex.set(part.index, part)
|
|
344
|
+
byLine.set(key, part)
|
|
341
345
|
if (selected === fragment.streamIndex) incoming.push(part.assEvent)
|
|
342
346
|
}
|
|
343
347
|
}
|
|
@@ -178,6 +178,7 @@ export const ControlBar = (
|
|
|
178
178
|
// Keyboard seeks go through the same door as the seek bar: data first, then the playhead. See
|
|
179
179
|
// `requestSeek` on the source state for why an element that seeks into a hole is the problem.
|
|
180
180
|
const requestSeek = usePlayer((state) => state.requestSeek)
|
|
181
|
+
const stepFrame = usePlayer((state) => state.stepFrame)
|
|
181
182
|
const [volumeElement, setVolumeElement] = useState<HTMLButtonElement | null>(null)
|
|
182
183
|
const passThroughButton = useRef<HTMLButtonElement>(null)
|
|
183
184
|
const refocus = useRef<ReturnType<typeof setInterval>>(undefined)
|
|
@@ -215,6 +216,9 @@ export const ControlBar = (
|
|
|
215
216
|
// on the window, so typing into a consumer's own input opts out
|
|
216
217
|
const target = targetOf(ev)
|
|
217
218
|
if (target?.isContentEditable || /^(input|textarea|select)$/i.test(target?.tagName ?? '')) return
|
|
219
|
+
// Ctrl+F, Alt+Left and the like are the browser's, but Windows reports AltGr, which types < and > on some
|
|
220
|
+
// layouts, as Ctrl and Alt
|
|
221
|
+
if ((ev.ctrlKey || ev.metaKey || ev.altKey) && !ev.getModifierState('AltGraph')) return
|
|
218
222
|
const fromPlayer = root.current?.contains(target ?? null) || (target === document.body && engaged.current)
|
|
219
223
|
if (shortcuts === 'player' && !player.fullscreen && !fromPlayer) return
|
|
220
224
|
|
|
@@ -237,6 +241,7 @@ export const ControlBar = (
|
|
|
237
241
|
else if (ev.key === 'ArrowLeft') {
|
|
238
242
|
seek(Math.max(player.currentTime - SEEK_STEP, 0))
|
|
239
243
|
}
|
|
244
|
+
else if ((ev.key === '<' || ev.key === '>') && stepFrame) stepFrame(ev.key === '>' ? 1 : -1)
|
|
240
245
|
else {
|
|
241
246
|
shouldPreventDefault = false
|
|
242
247
|
}
|
|
@@ -253,7 +258,7 @@ export const ControlBar = (
|
|
|
253
258
|
window.removeEventListener('pointerdown', engage, true)
|
|
254
259
|
window.removeEventListener('focusin', engage, true)
|
|
255
260
|
}
|
|
256
|
-
}, [player, modifyVolume, requestSeek, shortcuts, root])
|
|
261
|
+
}, [player, modifyVolume, requestSeek, stepFrame, shortcuts, root])
|
|
257
262
|
|
|
258
263
|
const handleWheel = useCallback((e: WheelEvent) => {
|
|
259
264
|
e.preventDefault()
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import type { PlayerMedia } from '../media'
|
|
2
|
+
|
|
3
|
+
import { useEffect, useRef } from 'react'
|
|
4
|
+
|
|
5
|
+
import { usePlayer } from '../player'
|
|
6
|
+
|
|
7
|
+
/** Film's frame, until the media has been seen playing. */
|
|
8
|
+
const FALLBACK_FRAME = 1001 / 24000
|
|
9
|
+
|
|
10
|
+
const presented = (video: HTMLVideoElement, timeout: number) => new Promise<void>((done) => {
|
|
11
|
+
const timer = setTimeout(done, timeout)
|
|
12
|
+
video.requestVideoFrameCallback(() => { clearTimeout(timer); done() })
|
|
13
|
+
})
|
|
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
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Offers `stepFrame` for a media whose frames this document can see, one with `requestVideoFrameCallback`.
|
|
37
|
+
*
|
|
38
|
+
* Paused by a seek, the element shows the frame holding `currentTime` (Chrome 155 and Firefox 157), so a step is
|
|
39
|
+
* `currentTime` plus or minus a frame. Paused from playing, Chrome shows the frame after that one in 15 of 40 pauses,
|
|
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
|
+
* seek presented is never read for its time: Firefox reports the seek's target as its `mediaTime`.
|
|
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.
|
|
46
|
+
*
|
|
47
|
+
* A media without `requestVideoFrameCallback` is stepped by `hostStep`, when its host can step it.
|
|
48
|
+
*/
|
|
49
|
+
export const useFrameStep = (media: PlayerMedia | null, hostStep?: (direction: 1 | -1) => Promise<void>) => {
|
|
50
|
+
const player = usePlayer()
|
|
51
|
+
const setSourceState = usePlayer((state) => state.setSourceState)
|
|
52
|
+
const host = useRef(hostStep)
|
|
53
|
+
host.current = hostStep
|
|
54
|
+
const hosted = !!hostStep
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
const video = media as HTMLVideoElement | null
|
|
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
|
+
}
|
|
71
|
+
|
|
72
|
+
let played: VideoFrameCallbackMetadata | undefined
|
|
73
|
+
let bySeek = false
|
|
74
|
+
const gaps: number[] = []
|
|
75
|
+
const onSeeking = () => { bySeek = true; played = undefined }
|
|
76
|
+
// a new media on the same element
|
|
77
|
+
const onEmptied = () => { played = undefined; gaps.length = 0 }
|
|
78
|
+
video.addEventListener('seeking', onSeeking)
|
|
79
|
+
video.addEventListener('emptied', onEmptied)
|
|
80
|
+
let handle = 0
|
|
81
|
+
const onFrame = (_: number, frame: VideoFrameCallbackMetadata) => {
|
|
82
|
+
const fromSeek = bySeek
|
|
83
|
+
// the first frame once a seek has landed while playing can still be the seek's own
|
|
84
|
+
if (!video.paused && !video.seeking) bySeek = false
|
|
85
|
+
if (
|
|
86
|
+
!fromSeek && !video.paused && video.playbackRate <= 1 &&
|
|
87
|
+
played && frame.presentedFrames === played.presentedFrames + 1 && frame.mediaTime > played.mediaTime
|
|
88
|
+
) {
|
|
89
|
+
gaps.push(frame.mediaTime - played.mediaTime)
|
|
90
|
+
if (gaps.length > 60) gaps.shift()
|
|
91
|
+
}
|
|
92
|
+
played = fromSeek ? undefined : frame
|
|
93
|
+
handle = video.requestVideoFrameCallback(onFrame)
|
|
94
|
+
}
|
|
95
|
+
handle = video.requestVideoFrameCallback(onFrame)
|
|
96
|
+
|
|
97
|
+
const frameLength = () => {
|
|
98
|
+
if (gaps.length < 5) return FALLBACK_FRAME
|
|
99
|
+
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)
|
|
104
|
+
return near.reduce((sum, gap) => sum + gap, 0) / near.length
|
|
105
|
+
}
|
|
106
|
+
|
|
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)
|
|
112
|
+
}
|
|
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
|
+
})
|
|
131
|
+
setSourceState({ stepFrame })
|
|
132
|
+
return () => {
|
|
133
|
+
video.removeEventListener('seeking', onSeeking)
|
|
134
|
+
video.removeEventListener('emptied', onEmptied)
|
|
135
|
+
video.cancelVideoFrameCallback(handle)
|
|
136
|
+
setSourceState({ stepFrame: undefined })
|
|
137
|
+
}
|
|
138
|
+
}, [media, hosted, player, setSourceState])
|
|
139
|
+
}
|
|
@@ -238,6 +238,12 @@ export type SourceState = {
|
|
|
238
238
|
*/
|
|
239
239
|
requestSeek?: (time: number) => void
|
|
240
240
|
|
|
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
|
+
*/
|
|
245
|
+
stepFrame?: (direction: 1 | -1) => void
|
|
246
|
+
|
|
241
247
|
/** Set when the pipeline fails. Cleared when it recovers. */
|
|
242
248
|
playbackError: unknown
|
|
243
249
|
/**
|
|
@@ -11,6 +11,7 @@ import { useContainerAttach, useMediaAttach } from '@videojs/react'
|
|
|
11
11
|
|
|
12
12
|
import { Player, usePlayer } from './player'
|
|
13
13
|
import { usePlayback } from './hooks/use-playback'
|
|
14
|
+
import { useFrameStep } from './hooks/use-frame-step'
|
|
14
15
|
import { useSeekThumbnails } from './hooks/use-thumbnails'
|
|
15
16
|
import { usePictureInPicture } from './hooks/use-picture-in-picture'
|
|
16
17
|
import { usePassThroughPictureInPicture } from './hooks/use-pass-through-picture-in-picture'
|
|
@@ -243,6 +244,15 @@ export type MediaPlayerRemoteOptions =
|
|
|
243
244
|
* host takes on by passing it. Read by value, so it may be written inline.
|
|
244
245
|
*/
|
|
245
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>
|
|
246
256
|
}
|
|
247
257
|
|
|
248
258
|
export type MediaPlayerOptions = MediaPlayerLocalOptions | MediaPlayerRemoteOptions
|
|
@@ -273,6 +283,7 @@ const PlayerRoot = ({ options, children }: { options: MediaPlayerOptions, childr
|
|
|
273
283
|
// permanent no-op.
|
|
274
284
|
const media = remote?.media ?? video
|
|
275
285
|
useEffect(() => { setMedia?.(media); return () => setMedia?.(null) }, [media, setMedia])
|
|
286
|
+
useFrameStep(media, remote?.stepFrame)
|
|
276
287
|
|
|
277
288
|
// Served after it is attached, and re-served when the media changes: what an embedder drives is
|
|
278
289
|
// whatever this player drives at the time. Keyed on the option's `origin` and `channel`, not on the
|