@banou/media-player 0.10.52 → 0.10.54
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/engine/index.js +1 -1
- package/dist/{engine-D_C7Ngyd.js → engine-DZ6BX5XH.js} +232 -189
- package/dist/index.js +144 -121
- package/package.json +2 -2
- package/src/lib/engine/playback.ts +100 -12
- package/src/lib/react/hooks/use-playback.ts +43 -1
|
@@ -287,7 +287,8 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
|
|
|
287
287
|
* Pushed AFTER the revoke so the reversed teardown runs it FIRST.
|
|
288
288
|
*/
|
|
289
289
|
teardown.push(() => {
|
|
290
|
-
|
|
290
|
+
// Prototype call so internal teardown does not look like an explicit viewer pause to wrapper hooks.
|
|
291
|
+
try { HTMLMediaElement.prototype.pause.call(videoElement) } catch {}
|
|
291
292
|
videoElement.removeAttribute('src')
|
|
292
293
|
// no src and no <source> children means the load algorithm goes straight to NETWORK_EMPTY
|
|
293
294
|
// without firing `error`, so this cannot manufacture the failure it exists to prevent
|
|
@@ -375,8 +376,29 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
|
|
|
375
376
|
// whether nothing was removed since, so everything read forward from there is still buffered
|
|
376
377
|
let positioned = true
|
|
377
378
|
let evicting = 0
|
|
379
|
+
// Where the playhead sat when a quota refusal could not free anything inside the kept window, so
|
|
380
|
+
// pump() waits for playback to move, cross a keyframe, or stall for 1 s instead of spinning while paused.
|
|
381
|
+
let quotaAnchor: { time: number, keyframe: number | undefined, at: number } | undefined
|
|
382
|
+
|
|
383
|
+
const MAX_ACTIVE_GOP_S = 30
|
|
384
|
+
|
|
385
|
+
const activeKeyframe = (time: number): number | undefined => {
|
|
386
|
+
const indexes = metadata.indexes
|
|
387
|
+
// A single cue at 0 (unindexed container) is not a keyframe map; a gap past MAX_ACTIVE_GOP_S
|
|
388
|
+
// means cues ended early and must not disable back-eviction for the rest of the file.
|
|
389
|
+
if (!indexes || indexes.length < 2) return undefined
|
|
390
|
+
let start: number | undefined
|
|
391
|
+
for (const { timestamp } of indexes) {
|
|
392
|
+
if (timestamp <= time && (start === undefined || timestamp > start)) start = timestamp
|
|
393
|
+
}
|
|
394
|
+
if (start === undefined || time - start > MAX_ACTIVE_GOP_S) return undefined
|
|
395
|
+
return start
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const bufferedSpan = (ranges: { start: number, end: number }[]) =>
|
|
399
|
+
ranges.reduce((sum, { start, end }) => sum + Math.max(0, end - start), 0)
|
|
378
400
|
|
|
379
|
-
const evict = async (tight = false) => {
|
|
401
|
+
const evict = async (tight = false): Promise<'none' | 'back' | 'far'> => {
|
|
380
402
|
/*
|
|
381
403
|
* Hold off while a prior eviction, a remuxer seek, or a seek prepare is in flight.
|
|
382
404
|
*
|
|
@@ -387,8 +409,10 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
|
|
|
387
409
|
* `tight` still runs, because that one is the answer to a quota refusal and has to be able to
|
|
388
410
|
* free space no matter what else is happening.
|
|
389
411
|
*/
|
|
390
|
-
if (!tight && (evicting > 0 || preparing > 0 || seeking || videoElement.seeking || dragTimer !== undefined)) return
|
|
412
|
+
if (!tight && (evicting > 0 || preparing > 0 || seeking || videoElement.seeking || dragTimer !== undefined)) return 'none'
|
|
391
413
|
evicting++
|
|
414
|
+
let removedBack = false
|
|
415
|
+
let removedFar = false
|
|
392
416
|
try {
|
|
393
417
|
const ct = videoElement.currentTime
|
|
394
418
|
const pre = tight ? PRE_EVICT_TIGHT : PRE_EVICT
|
|
@@ -398,14 +422,30 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
|
|
|
398
422
|
positioned = false
|
|
399
423
|
return unbufferRange(start, end)
|
|
400
424
|
}
|
|
425
|
+
const keyframe = activeKeyframe(ct)
|
|
426
|
+
// Never remove past the active GOP's keyframe: on a 10 s GOP, tight back-eviction (-5 s) lands
|
|
427
|
+
// inside the playing GOP and MSE deletes the whole group underneath the playhead.
|
|
428
|
+
const backEnd = keyframe === undefined ? ct + pre : Math.min(ct + pre, keyframe - 0.1)
|
|
401
429
|
// From 0, never from the reported start: once a group is removed, Firefox 151 reports the start
|
|
402
430
|
// 1 ms after the keyframe it still holds, so a remove from there keeps that frame for good
|
|
403
431
|
// (measured 2026-10-11).
|
|
404
|
-
if (ranges.length && ranges[0].start <
|
|
405
|
-
|
|
432
|
+
if (ranges.length && ranges[0].start < backEnd) {
|
|
433
|
+
const beforeBack = bufferedSpan(ranges)
|
|
434
|
+
await remove(0, backEnd)
|
|
435
|
+
if (!destroyed) removedBack = bufferedSpan(getTimeRanges(sourceBuffer)) < beforeBack - 0.05
|
|
436
|
+
}
|
|
437
|
+
if (destroyed || (!tight && (preparing > 0 || seeking || videoElement.seeking || dragTimer !== undefined))) {
|
|
438
|
+
return removedBack ? 'back' : 'none'
|
|
439
|
+
}
|
|
406
440
|
const afterCt = videoElement.currentTime
|
|
407
|
-
const
|
|
408
|
-
|
|
441
|
+
const afterBackRanges = getTimeRanges(sourceBuffer)
|
|
442
|
+
const farEnd = afterBackRanges.at(-1)?.end ?? 0
|
|
443
|
+
if (farEnd > afterCt + post) {
|
|
444
|
+
const beforeFar = bufferedSpan(afterBackRanges)
|
|
445
|
+
await remove(afterCt + post, farEnd)
|
|
446
|
+
if (!destroyed) removedFar = bufferedSpan(getTimeRanges(sourceBuffer)) < beforeFar - 0.05
|
|
447
|
+
}
|
|
448
|
+
return removedFar ? 'far' : removedBack ? 'back' : 'none'
|
|
409
449
|
} finally {
|
|
410
450
|
evicting--
|
|
411
451
|
}
|
|
@@ -467,6 +507,7 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
|
|
|
467
507
|
seeking = true
|
|
468
508
|
pending = null
|
|
469
509
|
pendingAttempts = 0
|
|
510
|
+
quotaAnchor = undefined
|
|
470
511
|
lastSeekPosition = time
|
|
471
512
|
positioned = true
|
|
472
513
|
const generation = ++seekGeneration
|
|
@@ -497,26 +538,53 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
|
|
|
497
538
|
const flushPending = async () => {
|
|
498
539
|
const segment = pending
|
|
499
540
|
if (!segment) return
|
|
500
|
-
|
|
501
|
-
await appendBuffer(segment)
|
|
541
|
+
const accept = () => {
|
|
502
542
|
trackAppendedEnd()
|
|
543
|
+
quotaAnchor = undefined
|
|
503
544
|
if (pending === segment) {
|
|
504
545
|
pending = null
|
|
505
546
|
pendingAttempts = 0
|
|
506
547
|
}
|
|
548
|
+
}
|
|
549
|
+
try {
|
|
550
|
+
await appendBuffer(segment)
|
|
551
|
+
accept()
|
|
507
552
|
} catch (error) {
|
|
508
553
|
if (pending !== segment) return
|
|
509
554
|
// Chrome refuses appends past a per-element video budget of roughly 150MB
|
|
510
555
|
if ((error as DOMException)?.name === 'QuotaExceededError') {
|
|
511
|
-
const evicted = await evict(true).
|
|
556
|
+
const evicted = await evict(true).catch(() => 'none' as const)
|
|
512
557
|
if (pending !== segment) return
|
|
513
|
-
if (evicted) {
|
|
558
|
+
if (evicted === 'far') {
|
|
559
|
+
quotaAnchor = undefined
|
|
514
560
|
// Reads only move forward, so restart at the far edge of what was kept rather than at
|
|
515
561
|
// the playhead, which would walk straight back into the same refusal.
|
|
516
562
|
finished = false
|
|
517
563
|
await seekTo(playheadRange()?.end ?? videoElement.currentTime)
|
|
518
564
|
return
|
|
519
565
|
}
|
|
566
|
+
if (evicted === 'back') {
|
|
567
|
+
// Only data behind the playhead was freed, so `segment` is still the next contiguous
|
|
568
|
+
// fragment; retry it in place without seeking back over the active GOP.
|
|
569
|
+
try {
|
|
570
|
+
await appendBuffer(segment)
|
|
571
|
+
accept()
|
|
572
|
+
return
|
|
573
|
+
} catch (retryError) {
|
|
574
|
+
if (pending !== segment) return
|
|
575
|
+
if ((retryError as DOMException)?.name !== 'QuotaExceededError') {
|
|
576
|
+
pendingAttempts += 1
|
|
577
|
+
if (pendingAttempts < MAX_APPEND_ATTEMPTS) return
|
|
578
|
+
pending = null
|
|
579
|
+
throw retryError
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
if (playheadRange()) {
|
|
584
|
+
const ct = videoElement.currentTime
|
|
585
|
+
quotaAnchor = { time: ct, keyframe: activeKeyframe(ct), at: Date.now() }
|
|
586
|
+
return
|
|
587
|
+
}
|
|
520
588
|
}
|
|
521
589
|
pendingAttempts += 1
|
|
522
590
|
if (pendingAttempts < MAX_APPEND_ATTEMPTS) return
|
|
@@ -529,6 +597,13 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
|
|
|
529
597
|
// a settling drag is about to reposition the remuxer, so reading forward from where it
|
|
530
598
|
// happens to sit is throwing a read away
|
|
531
599
|
if (reading || seeking || dragTimer !== undefined || destroyed) return
|
|
600
|
+
if (quotaAnchor !== undefined) {
|
|
601
|
+
const ct = videoElement.currentTime
|
|
602
|
+
const moved = Math.abs(ct - quotaAnchor.time) >= 1 || activeKeyframe(ct) !== quotaAnchor.keyframe
|
|
603
|
+
const waiting = !videoElement.paused && videoElement.readyState <= HTMLMediaElement.HAVE_CURRENT_DATA && Date.now() - quotaAnchor.at >= 1_000
|
|
604
|
+
if (!moved && !waiting) return
|
|
605
|
+
quotaAnchor = undefined
|
|
606
|
+
}
|
|
532
607
|
if (!pending && (finished || !needsData())) return
|
|
533
608
|
const generation = ++readGeneration
|
|
534
609
|
const seekAtStart = seekGeneration
|
|
@@ -604,6 +679,18 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
|
|
|
604
679
|
*/
|
|
605
680
|
const hasRunway = (time: number) => runwayFrom(time) >= SEEK_RUNWAY || (endInBuffer() && atEnd(time))
|
|
606
681
|
|
|
682
|
+
const endBufferedInSource = () =>
|
|
683
|
+
lastAppendedEnd > 0 && (getTimeRanges(sourceBuffer).at(-1)?.end ?? 0) >= lastAppendedEnd - 0.1
|
|
684
|
+
const samePreparedEnd = (time: number) =>
|
|
685
|
+
finished &&
|
|
686
|
+
endBufferedInSource() &&
|
|
687
|
+
atEnd(time) &&
|
|
688
|
+
(Math.abs(time - lastSeekPosition) < 0.001 || lastSeekPosition >= lastAppendedEnd - 0.1)
|
|
689
|
+
const alreadyPrepared = (time: number) =>
|
|
690
|
+
positioned &&
|
|
691
|
+
(((seeking || runwayFrom(time) > 0) && Math.abs(time - lastSeekPosition) < 0.001) ||
|
|
692
|
+
samePreparedEnd(time))
|
|
693
|
+
|
|
607
694
|
/**
|
|
608
695
|
* Put the data in place for a position the playhead has NOT moved to yet.
|
|
609
696
|
*
|
|
@@ -625,6 +712,7 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
|
|
|
625
712
|
const prepareSeek = async (time: number) => {
|
|
626
713
|
if (destroyed || elementFailed || videoElement.error) return
|
|
627
714
|
if (playableAt(time) && hasRunway(time)) return
|
|
715
|
+
if (alreadyPrepared(time)) return
|
|
628
716
|
// A drag still settling holds every read, so the runway would never be read, and its settle would then position the
|
|
629
717
|
// remuxer back where the drag stopped. A seek back under 250 ms after a seek forward is one (Firefox 153 and 157,
|
|
630
718
|
// 2026-10-23).
|
|
@@ -742,7 +830,7 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
|
|
|
742
830
|
* away everything the first one had done. The benchmark caught it as a seek that took twice as
|
|
743
831
|
* long as seeking the old way.
|
|
744
832
|
*/
|
|
745
|
-
if (
|
|
833
|
+
if (alreadyPrepared(time)) return
|
|
746
834
|
finished = false
|
|
747
835
|
if (dragTimer) clearTimeout(dragTimer)
|
|
748
836
|
if (!dragging) {
|
|
@@ -146,6 +146,27 @@ export const usePlayback = (
|
|
|
146
146
|
const subtitleChoiceFor = useRef<number>(undefined)
|
|
147
147
|
const sizeRef = useRef(size)
|
|
148
148
|
sizeRef.current = size
|
|
149
|
+
// Calling pause() on an already-paused element (before ready, during a rebuild, or while waiting for
|
|
150
|
+
// metadata) fires no 'pause' event, so observe the call directly to keep autoplay from overriding it.
|
|
151
|
+
const onPauseCallRef = useRef<(() => void) | null>(null)
|
|
152
|
+
const pausedBeforeLoadRef = useRef(false)
|
|
153
|
+
useEffect(() => {
|
|
154
|
+
if (!video) return
|
|
155
|
+
const onPlay = () => { pausedBeforeLoadRef.current = false }
|
|
156
|
+
const ownPause = Object.getOwnPropertyDescriptor(video, 'pause')
|
|
157
|
+
const nativePause = video.pause
|
|
158
|
+
video.pause = function pause(this: HTMLVideoElement) {
|
|
159
|
+
if (onPauseCallRef.current) onPauseCallRef.current()
|
|
160
|
+
else pausedBeforeLoadRef.current = true
|
|
161
|
+
return nativePause.call(this)
|
|
162
|
+
}
|
|
163
|
+
video.addEventListener('play', onPlay)
|
|
164
|
+
return () => {
|
|
165
|
+
video.removeEventListener('play', onPlay)
|
|
166
|
+
if (ownPause) Object.defineProperty(video, 'pause', ownPause)
|
|
167
|
+
else delete (video as Partial<HTMLVideoElement>).pause
|
|
168
|
+
}
|
|
169
|
+
}, [video])
|
|
149
170
|
const streams = useRef<{ audio: AudioStream[], subtitles: SubtitleStream[] }>({ audio: [], subtitles: [] })
|
|
150
171
|
const playingAudio = useCallback(() => streams.current.audio.find((track) => track.streamIndex === player.selectedAudioTrack), [player])
|
|
151
172
|
|
|
@@ -333,6 +354,8 @@ export const usePlayback = (
|
|
|
333
354
|
if (resumeRef.current && resumeRef.current.size !== size) resumeRef.current = null
|
|
334
355
|
let cancelled = false
|
|
335
356
|
let reachedReady = false
|
|
357
|
+
let pausedBeforeReady = pausedBeforeLoadRef.current
|
|
358
|
+
pausedBeforeLoadRef.current = false
|
|
336
359
|
// Chrome sets `video.paused = true` before dispatching `error` and then queues one `pause` event,
|
|
337
360
|
// while Firefox and WebKit leave `paused` false. Latch only that single error-induced pause so a
|
|
338
361
|
// real pause right before a MediaError or during recovery backoff is never dropped.
|
|
@@ -340,9 +363,13 @@ export const usePlayback = (
|
|
|
340
363
|
let pausedByError = false
|
|
341
364
|
let ignoreErrorPause = false
|
|
342
365
|
const onPlay = () => {
|
|
366
|
+
pausedBeforeReady = false
|
|
343
367
|
wantPlaying = true
|
|
344
368
|
pausedByError = false
|
|
345
369
|
ignoreErrorPause = false
|
|
370
|
+
if (!reachedReady && resumeRef.current?.size === size) {
|
|
371
|
+
resumeRef.current = { ...resumeRef.current, play: true }
|
|
372
|
+
}
|
|
346
373
|
}
|
|
347
374
|
const onPause = () => {
|
|
348
375
|
if (ignoreErrorPause) {
|
|
@@ -358,6 +385,20 @@ export const usePlayback = (
|
|
|
358
385
|
ignoreErrorPause = true
|
|
359
386
|
}
|
|
360
387
|
}
|
|
388
|
+
const onPauseCall = () => {
|
|
389
|
+
wantPlaying = false
|
|
390
|
+
pausedByError = false
|
|
391
|
+
ignoreErrorPause = false
|
|
392
|
+
if (!reachedReady) {
|
|
393
|
+
pausedBeforeReady = true
|
|
394
|
+
resumeRef.current = {
|
|
395
|
+
time: resumeRef.current?.size === size ? resumeRef.current.time : 0,
|
|
396
|
+
size,
|
|
397
|
+
play: false,
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
onPauseCallRef.current = onPauseCall
|
|
361
402
|
video.addEventListener('play', onPlay)
|
|
362
403
|
video.addEventListener('pause', onPause)
|
|
363
404
|
video.addEventListener('error', onError)
|
|
@@ -451,7 +492,7 @@ export const usePlayback = (
|
|
|
451
492
|
if (sameMedia && resume.time > 0) video.currentTime = resume.time
|
|
452
493
|
// a seek carried to 0 is where the new element already is, so no 'seeked' comes to take it off the clock
|
|
453
494
|
else player.setSourceState({ seekingTo: undefined })
|
|
454
|
-
const shouldPlay = sameMedia ? resume.play : autoplayRef.current
|
|
495
|
+
const shouldPlay = !pausedBeforeReady && (sameMedia ? resume.play : autoplayRef.current)
|
|
455
496
|
wantPlaying = shouldPlay
|
|
456
497
|
if (shouldPlay) video.play().catch(() => { if (video.paused && !pausedByError) wantPlaying = false })
|
|
457
498
|
},
|
|
@@ -496,6 +537,7 @@ export const usePlayback = (
|
|
|
496
537
|
})()
|
|
497
538
|
return () => {
|
|
498
539
|
cancelled = true
|
|
540
|
+
if (onPauseCallRef.current === onPauseCall) onPauseCallRef.current = null
|
|
499
541
|
video.removeEventListener('play', onPlay)
|
|
500
542
|
video.removeEventListener('pause', onPause)
|
|
501
543
|
video.removeEventListener('error', onError)
|