@banou/media-player 0.10.50 → 0.10.52
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-Cdwc76gM.js → engine-D_C7Ngyd.js} +252 -219
- package/dist/index.js +170 -148
- package/dist/remote/index.js +60 -39
- package/package.json +1 -1
- package/src/lib/engine/picture-in-picture.ts +38 -4
- package/src/lib/engine/playback.ts +104 -86
- package/src/lib/react/hooks/use-playback.ts +61 -15
- package/src/lib/remote/index.ts +57 -23
|
@@ -131,17 +131,17 @@ export const usePlayback = (
|
|
|
131
131
|
|
|
132
132
|
const controllerRef = useRef<PlaybackController | null>(null)
|
|
133
133
|
/**
|
|
134
|
-
* Where to pick playback back up, and WHICH media that
|
|
134
|
+
* Where to pick playback back up, whether it was running, and WHICH media that state belongs to.
|
|
135
135
|
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
136
|
+
* Both travel together so a rebuild of the pipeline is invisible: switching audio track or
|
|
137
|
+
* recovering from a MediaError tears the pipeline down and starts again. Without the position the
|
|
138
|
+
* viewer dropped back to zero; without `play`, an autoplay player started playing again after a
|
|
139
|
+
* rebuild while paused, and a non-autoplay player stayed paused after a rebuild while playing.
|
|
140
140
|
*
|
|
141
141
|
* `size` is the identity, because it is the one piece of the source in the effect's deps. `read` is
|
|
142
142
|
* deliberately not, since a streaming consumer passes a fresh closure several times a second.
|
|
143
143
|
*/
|
|
144
|
-
const resumeRef = useRef<{ time: number, size: number } | null>(null)
|
|
144
|
+
const resumeRef = useRef<{ time: number, size: number, play: boolean } | null>(null)
|
|
145
145
|
// The size of the media the viewer picked subtitles for: until then each pipeline starts on the preferred track.
|
|
146
146
|
const subtitleChoiceFor = useRef<number>(undefined)
|
|
147
147
|
const sizeRef = useRef(size)
|
|
@@ -151,6 +151,8 @@ export const usePlayback = (
|
|
|
151
151
|
|
|
152
152
|
const readRef = useRef(read)
|
|
153
153
|
readRef.current = read
|
|
154
|
+
const autoplayRef = useRef(autoplay)
|
|
155
|
+
autoplayRef.current = autoplay
|
|
154
156
|
const preferences = {
|
|
155
157
|
audio: options?.preferredAudioLanguage,
|
|
156
158
|
original: options?.originalLanguage,
|
|
@@ -327,7 +329,38 @@ export const usePlayback = (
|
|
|
327
329
|
|
|
328
330
|
useEffect(() => {
|
|
329
331
|
if (!video || !subtitles || !size || !read) return
|
|
332
|
+
// Discard stale resume state once another media starts loading, even if it aborts before onReady.
|
|
333
|
+
if (resumeRef.current && resumeRef.current.size !== size) resumeRef.current = null
|
|
330
334
|
let cancelled = false
|
|
335
|
+
let reachedReady = false
|
|
336
|
+
// Chrome sets `video.paused = true` before dispatching `error` and then queues one `pause` event,
|
|
337
|
+
// while Firefox and WebKit leave `paused` false. Latch only that single error-induced pause so a
|
|
338
|
+
// real pause right before a MediaError or during recovery backoff is never dropped.
|
|
339
|
+
let wantPlaying = false
|
|
340
|
+
let pausedByError = false
|
|
341
|
+
let ignoreErrorPause = false
|
|
342
|
+
const onPlay = () => {
|
|
343
|
+
wantPlaying = true
|
|
344
|
+
pausedByError = false
|
|
345
|
+
ignoreErrorPause = false
|
|
346
|
+
}
|
|
347
|
+
const onPause = () => {
|
|
348
|
+
if (ignoreErrorPause) {
|
|
349
|
+
ignoreErrorPause = false
|
|
350
|
+
return
|
|
351
|
+
}
|
|
352
|
+
wantPlaying = false
|
|
353
|
+
pausedByError = false
|
|
354
|
+
}
|
|
355
|
+
const onError = () => {
|
|
356
|
+
if (wantPlaying && video.paused) {
|
|
357
|
+
pausedByError = true
|
|
358
|
+
ignoreErrorPause = true
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
video.addEventListener('play', onPlay)
|
|
362
|
+
video.addEventListener('pause', onPause)
|
|
363
|
+
video.addEventListener('error', onError)
|
|
331
364
|
player.setSourceState({ playbackError: null, ready: false })
|
|
332
365
|
|
|
333
366
|
/**
|
|
@@ -409,14 +442,18 @@ export const usePlayback = (
|
|
|
409
442
|
chooseAudioStream: (audio) => audio[startingTracks(audio, [], preferencesRef.current).audio]?.streamIndex,
|
|
410
443
|
onReady: () => {
|
|
411
444
|
if (cancelled) return
|
|
445
|
+
reachedReady = true
|
|
412
446
|
player.setSourceState({ ready: true })
|
|
413
447
|
const resume = resumeRef.current
|
|
414
448
|
resumeRef.current = null
|
|
449
|
+
const sameMedia = !!resume && resume.size === size
|
|
415
450
|
// only a position belonging to THIS media, or a new file opens where the last one stopped
|
|
416
|
-
if (
|
|
451
|
+
if (sameMedia && resume.time > 0) video.currentTime = resume.time
|
|
417
452
|
// a seek carried to 0 is where the new element already is, so no 'seeked' comes to take it off the clock
|
|
418
453
|
else player.setSourceState({ seekingTo: undefined })
|
|
419
|
-
|
|
454
|
+
const shouldPlay = sameMedia ? resume.play : autoplayRef.current
|
|
455
|
+
wantPlaying = shouldPlay
|
|
456
|
+
if (shouldPlay) video.play().catch(() => { if (video.paused && !pausedByError) wantPlaying = false })
|
|
420
457
|
},
|
|
421
458
|
onError: fail,
|
|
422
459
|
onRecovered: () => { if (!cancelled) player.setSourceState({ playbackError: null }) },
|
|
@@ -459,19 +496,28 @@ export const usePlayback = (
|
|
|
459
496
|
})()
|
|
460
497
|
return () => {
|
|
461
498
|
cancelled = true
|
|
462
|
-
|
|
463
|
-
|
|
499
|
+
video.removeEventListener('play', onPlay)
|
|
500
|
+
video.removeEventListener('pause', onPause)
|
|
501
|
+
video.removeEventListener('error', onError)
|
|
502
|
+
if (reachedReady) {
|
|
503
|
+
// a seek still waiting for its data has not moved the element yet, and it is where the viewer asked to be
|
|
504
|
+
resumeRef.current = {
|
|
505
|
+
time: player.state.seekingTo ?? video.currentTime,
|
|
506
|
+
size,
|
|
507
|
+
play: !video.ended && (pausedByError ? wantPlaying : !video.paused),
|
|
508
|
+
}
|
|
509
|
+
} else if (resumeRef.current?.size === size && player.state.seekingTo !== undefined) {
|
|
510
|
+
resumeRef.current = { ...resumeRef.current, time: player.state.seekingTo }
|
|
511
|
+
}
|
|
464
512
|
controllerRef.current?.destroy()
|
|
465
513
|
controllerRef.current = null
|
|
466
514
|
player.setSourceState({ ready: false })
|
|
467
515
|
}
|
|
468
|
-
// `read`
|
|
469
|
-
//
|
|
470
|
-
// identity. A streaming consumer passes a fresh closure on every state update, which is several
|
|
471
|
-
// times a second, and the restart loop reads as "Loading metadata" forever at a flat 0 B/s.
|
|
516
|
+
// `read` and `autoplay` are read through refs: `read` changes identity on streaming updates, and
|
|
517
|
+
// toggling `autoplay` does not need a full pipeline rebuild.
|
|
472
518
|
}, [
|
|
473
519
|
player, playingAudio, video, subtitles, size, publicPath, libavWorkerUrl, jassubWorkerUrl, jassubWasmUrl,
|
|
474
520
|
jassubWasmWorkerUrl, jassubLegacyWasmUrl, jassubLegacyWasmWorkerUrl, defaultFontUrl, cjkFontUrl, bufferSize,
|
|
475
|
-
audioStreamIndex,
|
|
521
|
+
audioStreamIndex, restartToken,
|
|
476
522
|
])
|
|
477
523
|
}
|
package/src/lib/remote/index.ts
CHANGED
|
@@ -618,6 +618,12 @@ export const mediaPlayer = (
|
|
|
618
618
|
// whether the far side has said anything about THIS player yet, which is what `ready` means
|
|
619
619
|
let answered = false
|
|
620
620
|
let fullscreen = false
|
|
621
|
+
let pauseCount = 0
|
|
622
|
+
const pauseListeners = new Set<() => void>()
|
|
623
|
+
const notePause = () => {
|
|
624
|
+
pauseCount++
|
|
625
|
+
for (const listener of Array.from(pauseListeners)) listener()
|
|
626
|
+
}
|
|
621
627
|
|
|
622
628
|
const detach = linkTo(target, options).attach({
|
|
623
629
|
id: playerId,
|
|
@@ -637,6 +643,7 @@ export const mediaPlayer = (
|
|
|
637
643
|
// told for any update that changes them: a catch-up or a switch's `emptied` brings chapters with no word of them
|
|
638
644
|
if (!events.includes('chapterschange') && JSON.stringify(state.chapters) !== chapters) events.push('chapterschange')
|
|
639
645
|
if (!events.includes('autoplaynextchange') && state.autoplayNext !== autoplayNext) events.push('autoplaynextchange')
|
|
646
|
+
if (!first && event === 'pause') notePause()
|
|
640
647
|
for (const each of events) dispatch(each)
|
|
641
648
|
if (first) settleReady()
|
|
642
649
|
},
|
|
@@ -711,48 +718,75 @@ export const mediaPlayer = (
|
|
|
711
718
|
// like an element's, `play` settles with the far side's own answer, so an autoplay refusal over
|
|
712
719
|
// there rejects over here, and the mirror goes back to paused when it does
|
|
713
720
|
player.play = () => { state.paused = false; return call('play').catch(error => { state.paused = true; throw error }) }
|
|
714
|
-
// Whether the far player is actually running, shortly after being asked to. Its own `playing`
|
|
715
|
-
// settles this the moment it arrives; the timeout is for the refusal that announces nothing, and
|
|
716
|
-
// reads the state the call itself reported on its way out.
|
|
717
|
-
const startedPlaying = () => new Promise<boolean>(resolve => {
|
|
718
|
-
// Deliberately NOT short-circuiting on `state.paused` here: `play()` has just written it
|
|
719
|
-
// optimistically, so it reads as playing whatever the far side does. Only an event from over
|
|
720
|
-
// there, or the state left behind once the call has answered, can settle this.
|
|
721
|
-
const finish = (started: boolean) => {
|
|
722
|
-
clearTimeout(timer)
|
|
723
|
-
player.removeEventListener('playing', ok)
|
|
724
|
-
player.removeEventListener('play', ok)
|
|
725
|
-
resolve(started)
|
|
726
|
-
}
|
|
727
|
-
const ok = () => finish(true)
|
|
728
|
-
player.addEventListener('playing', ok)
|
|
729
|
-
player.addEventListener('play', ok)
|
|
730
|
-
const timer = setTimeout(() => finish(!state.paused), PLAY_CONFIRM_MS)
|
|
731
|
-
})
|
|
732
721
|
|
|
722
|
+
// Ask the far side to play and confirm from its own events, listening BEFORE `play()` goes out:
|
|
723
|
+
// the far side's `play`/`playing` routinely arrives before the RPC settles, and missing it made
|
|
724
|
+
// `autoplay()` wait out PLAY_CONFIRM_MS and retry muted over a viewer pause in that window.
|
|
733
725
|
const askToPlay = async () => {
|
|
734
726
|
// read before `play()` writes its optimism over it
|
|
735
727
|
const alreadyPlaying = !state.paused
|
|
728
|
+
const pauseAtStart = pauseCount
|
|
729
|
+
let heardPlay = false
|
|
730
|
+
let settle: ((started: boolean) => void) | undefined
|
|
731
|
+
let settled: Promise<boolean> | undefined
|
|
732
|
+
let onPlay: (() => void) | undefined
|
|
733
|
+
let onPause: (() => void) | undefined
|
|
734
|
+
let onAbort: (() => void) | undefined
|
|
735
|
+
const cleanup = () => {
|
|
736
|
+
if (onPlay) {
|
|
737
|
+
player.removeEventListener('playing', onPlay)
|
|
738
|
+
player.removeEventListener('play', onPlay)
|
|
739
|
+
}
|
|
740
|
+
if (onPause) pauseListeners.delete(onPause)
|
|
741
|
+
if (onAbort) signal.removeEventListener('abort', onAbort)
|
|
742
|
+
}
|
|
743
|
+
if (!alreadyPlaying) {
|
|
744
|
+
settled = new Promise<boolean>(resolve => {
|
|
745
|
+
settle = started => { cleanup(); resolve(started) }
|
|
746
|
+
})
|
|
747
|
+
onPlay = () => { heardPlay = true; settle?.(true) }
|
|
748
|
+
onPause = () => settle?.(false)
|
|
749
|
+
onAbort = () => settle?.(false)
|
|
750
|
+
player.addEventListener('playing', onPlay)
|
|
751
|
+
player.addEventListener('play', onPlay)
|
|
752
|
+
pauseListeners.add(onPause)
|
|
753
|
+
signal.addEventListener('abort', onAbort, { once: true })
|
|
754
|
+
}
|
|
736
755
|
let refusal: unknown
|
|
737
756
|
try { await player.play() } catch (error) { refusal = error }
|
|
738
|
-
if (alreadyPlaying) return { started: true, refusal }
|
|
757
|
+
if (alreadyPlaying) return { started: true, paused: false, refusal }
|
|
758
|
+
const pausedSince = () => pauseCount !== pauseAtStart
|
|
759
|
+
if (heardPlay || pausedSince() || signal.aborted) {
|
|
760
|
+
cleanup()
|
|
761
|
+
return { started: heardPlay && !pausedSince(), paused: pausedSince(), refusal }
|
|
762
|
+
}
|
|
739
763
|
// the far side's own word, not the promise's: a wrapper that resolves a refused play is common
|
|
740
764
|
// enough that trusting the promise is what left followers paused for ever
|
|
741
|
-
|
|
765
|
+
const timer = setTimeout(() => settle?.(!state.paused && !pausedSince()), PLAY_CONFIRM_MS)
|
|
766
|
+
try {
|
|
767
|
+
const started = await settled!
|
|
768
|
+
return { started: started && !pausedSince(), paused: pausedSince(), refusal }
|
|
769
|
+
} finally {
|
|
770
|
+
clearTimeout(timer)
|
|
771
|
+
cleanup()
|
|
772
|
+
}
|
|
742
773
|
}
|
|
743
774
|
|
|
744
775
|
player.autoplay = async () => {
|
|
776
|
+
const pauseAtStart = pauseCount
|
|
745
777
|
const first = await askToPlay()
|
|
746
778
|
if (first.started) return { muted: player.muted }
|
|
747
|
-
// already muted and still refused, or torn down
|
|
748
|
-
if (player.muted || signal.aborted)
|
|
779
|
+
// paused mid-attempt, already muted and still refused, or torn down: nothing left to trade away
|
|
780
|
+
if (first.paused || pauseCount !== pauseAtStart || player.muted || signal.aborted) {
|
|
781
|
+
throw first.refusal ?? notStartedError()
|
|
782
|
+
}
|
|
749
783
|
player.muted = true
|
|
750
784
|
const second = await askToPlay()
|
|
751
785
|
if (second.started) return { muted: true }
|
|
752
786
|
player.muted = false
|
|
753
787
|
throw first.refusal ?? second.refusal ?? notStartedError()
|
|
754
788
|
}
|
|
755
|
-
player.pause = () => { state.paused = true; call('pause').catch(() => {}) }
|
|
789
|
+
player.pause = () => { state.paused = true; notePause(); call('pause').catch(() => {}) }
|
|
756
790
|
player.load = () => { call('load').catch(() => {}) }
|
|
757
791
|
Object.defineProperty(player, 'ready', { value: ready, enumerable: false })
|
|
758
792
|
player.destroy = () => controller.abort()
|