@banou/media-player 0.10.53 → 0.10.55
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/{ar-CWVutin3.js → ar-CSjwFY8l.js} +2 -0
- package/dist/{cs-CmwRGxyE.js → cs-Dk6IDwX9.js} +2 -0
- package/dist/{de-DLdKEf3t.js → de-G319tpSe.js} +2 -0
- package/dist/{el-Ckj-z6DX.js → el-CtRdEeUl.js} +2 -0
- package/dist/engine/index.d.ts +2 -0
- package/dist/engine/index.js +2 -2
- package/dist/engine/playback.d.ts +2 -0
- package/dist/engine/subtitle-convert.d.ts +8 -0
- package/dist/engine/subtitles.d.ts +2 -0
- package/dist/{engine-03XT1hpH.js → engine-tBJeFD6J.js} +341 -203
- package/dist/{es-419-zORhhU8D.js → es-419-BzuTB161.js} +2 -0
- package/dist/{fr-BtPmNkri.js → fr-BC0Ncjsm.js} +2 -0
- package/dist/{he-Bu79qEth.js → he-UutSGaSy.js} +2 -0
- package/dist/{hi-BsH3tSHa.js → hi-kNW0VH4d.js} +2 -0
- package/dist/{hu-BkYBW-YP.js → hu-UJBXN0lB.js} +2 -0
- package/dist/{id-nUaBCV4o.js → id-DYADKCdp.js} +2 -0
- package/dist/index.d.ts +4 -3
- package/dist/index.js +1188 -953
- package/dist/{it-CqPSHyFA.js → it-GodcqBoI.js} +2 -0
- package/dist/{ja-DTjvriHw.js → ja-r3FM4zNQ.js} +2 -0
- package/dist/{ko-dV8T7XOJ.js → ko-kNn9SZJT.js} +2 -0
- package/dist/labels.d.ts +2 -0
- package/dist/{ms-VZQ7BI0z.js → ms-B_95sgk7.js} +2 -0
- package/dist/{nl-Q6iDxbDz.js → nl-DUua8lM5.js} +2 -0
- package/dist/{pl-BN7bK74l.js → pl-DYN3cCvL.js} +2 -0
- package/dist/{pt-BR-CPFILeFW.js → pt-BR-CRH5nyyj.js} +2 -0
- package/dist/react/components/chrome.d.ts +3 -2
- package/dist/react/components/control-bar.d.ts +3 -2
- package/dist/react/hooks/use-playback.d.ts +1 -5
- package/dist/react/video-player.d.ts +29 -1
- package/dist/remote/index.d.ts +25 -2
- package/dist/remote/index.js +134 -68
- package/dist/remote/protocol.d.ts +22 -6
- package/dist/{ro-Bwcm-i3k.js → ro-lXdsKpft.js} +2 -0
- package/dist/{ru-D1FMz751.js → ru-Du_uJXdo.js} +2 -0
- package/dist/{sv-ci9K0Lkk.js → sv-CpEL6oGd.js} +2 -0
- package/dist/{th-DR-UBONQ.js → th-CQac_Jcd.js} +2 -0
- package/dist/{tr-BqhdfT22.js → tr-DLRAnIOh.js} +2 -0
- package/dist/{uk-8pkZqdni.js → uk-Cra5hJzT.js} +2 -0
- package/dist/{vi-3mKZ9G_M.js → vi-L2-IGJYQ.js} +2 -0
- package/dist/{zh-Hans-CDgigWKY.js → zh-Hans-Dp0UkPYn.js} +2 -0
- package/dist/{zh-Hant-3WSNI0ne.js → zh-Hant-CS16XV8X.js} +2 -0
- package/package.json +1 -1
- package/src/lib/engine/index.ts +3 -0
- package/src/lib/engine/playback.ts +18 -1
- package/src/lib/engine/subtitle-convert.ts +186 -0
- package/src/lib/engine/subtitles.ts +46 -7
- package/src/lib/index.tsx +7 -0
- package/src/lib/labels.ts +2 -0
- package/src/lib/locales/ar.json +2 -0
- package/src/lib/locales/cs.json +2 -0
- package/src/lib/locales/de.json +2 -0
- package/src/lib/locales/el.json +2 -0
- package/src/lib/locales/es-419.json +2 -0
- package/src/lib/locales/fr.json +2 -0
- package/src/lib/locales/he.json +2 -0
- package/src/lib/locales/hi.json +2 -0
- package/src/lib/locales/hu.json +2 -0
- package/src/lib/locales/id.json +2 -0
- package/src/lib/locales/it.json +2 -0
- package/src/lib/locales/ja.json +2 -0
- package/src/lib/locales/ko.json +2 -0
- package/src/lib/locales/ms.json +2 -0
- package/src/lib/locales/nl.json +2 -0
- package/src/lib/locales/pl.json +2 -0
- package/src/lib/locales/pt-BR.json +2 -0
- package/src/lib/locales/ro.json +2 -0
- package/src/lib/locales/ru.json +2 -0
- package/src/lib/locales/sv.json +2 -0
- package/src/lib/locales/th.json +2 -0
- package/src/lib/locales/tr.json +2 -0
- package/src/lib/locales/uk.json +2 -0
- package/src/lib/locales/vi.json +2 -0
- package/src/lib/locales/zh-Hans.json +2 -0
- package/src/lib/locales/zh-Hant.json +2 -0
- package/src/lib/react/components/chrome.tsx +4 -3
- package/src/lib/react/components/control-bar.tsx +51 -6
- package/src/lib/react/hooks/use-playback.ts +207 -15
- package/src/lib/react/video-player.tsx +116 -3
- package/src/lib/remote/index.ts +149 -12
- package/src/lib/remote/protocol.ts +32 -7
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { AudioStream, PlaybackController, PlaybackOutput, SubtitleStream } from '../../engine'
|
|
2
|
+
import type { Labels } from '../labels'
|
|
2
3
|
import type { PlaybackErrorEntry } from '../source-feature'
|
|
3
|
-
import type { MediaPlayerLocalOptions } from '../video-player'
|
|
4
|
+
import type { ExternalSubtitleTrack, MediaPlayerLocalOptions } from '../video-player'
|
|
4
5
|
|
|
5
6
|
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
6
7
|
|
|
7
|
-
import { isMediaElementError, startPlayback } from '../../engine'
|
|
8
|
+
import { isMediaElementError, startPlayback, toAssSubtitles } from '../../engine'
|
|
8
9
|
import { usePlayer } from '../player'
|
|
9
10
|
import { useLabels } from '../labels'
|
|
10
11
|
import { toNamedTracks } from '../../utils/track-label'
|
|
@@ -71,12 +72,21 @@ const causeChain = (error: unknown) => {
|
|
|
71
72
|
* Owns the engine for the life of a source: start, teardown, and every piece of state the pipeline
|
|
72
73
|
* discovers. Nothing is mirrored in React state, so the store is the only place any of it lives.
|
|
73
74
|
*/
|
|
75
|
+
// Prefixed in the store so string external track ids never collide with numeric muxed stream indices.
|
|
76
|
+
const EXT_SUBTITLE_PREFIX = 'ext:'
|
|
77
|
+
|
|
78
|
+
const isExternalTrackId = (id: string | number | undefined): id is string =>
|
|
79
|
+
typeof id === 'string' && id.startsWith(EXT_SUBTITLE_PREFIX)
|
|
80
|
+
|
|
81
|
+
const rawExternalTrackId = (storeId: string) => storeId.slice(EXT_SUBTITLE_PREFIX.length)
|
|
82
|
+
|
|
74
83
|
export const usePlayback = (
|
|
75
84
|
video: HTMLVideoElement | null,
|
|
76
85
|
subtitles: HTMLElement | null,
|
|
77
86
|
/** null when the media is remote: there are no bytes, so there is no pipeline to run. */
|
|
78
87
|
options: MediaPlayerLocalOptions | null,
|
|
79
88
|
onOutput?: (output: PlaybackOutput) => void,
|
|
89
|
+
externalSubtitlesRevision?: number,
|
|
80
90
|
) => {
|
|
81
91
|
const player = usePlayer()
|
|
82
92
|
// The five asset urls are required on the local arm, so a default only ever applies when options is
|
|
@@ -84,8 +94,13 @@ export const usePlayback = (
|
|
|
84
94
|
const {
|
|
85
95
|
read, size, publicPath = '', libavWorkerUrl = '', jassubWorkerUrl = '', jassubWasmUrl = '', jassubWasmWorkerUrl = '',
|
|
86
96
|
jassubLegacyWasmUrl, jassubLegacyWasmWorkerUrl, defaultFontUrl, cjkFontUrl, bufferSize, autoplay = false, chapters,
|
|
87
|
-
seekPrepareBudgetMs = SEEK_PREPARE_BUDGET_MS,
|
|
97
|
+
seekPrepareBudgetMs = SEEK_PREPARE_BUDGET_MS, externalSubtitles,
|
|
88
98
|
} = options ?? ({} as Partial<MediaPlayerLocalOptions>)
|
|
99
|
+
const externalSubtitlesRef = useRef<readonly ExternalSubtitleTrack[]>(externalSubtitles ?? [])
|
|
100
|
+
externalSubtitlesRef.current = externalSubtitles ?? []
|
|
101
|
+
const externalKey = JSON.stringify(
|
|
102
|
+
externalSubtitles?.map((track) => [track.id, track.language, track.label, track.format, track.content ?? null]) ?? [],
|
|
103
|
+
)
|
|
89
104
|
|
|
90
105
|
/*
|
|
91
106
|
* Through a ref because a caller's array is a new identity every render.
|
|
@@ -121,9 +136,27 @@ export const usePlayback = (
|
|
|
121
136
|
// The streak belongs to one media, not to the player, and a pending rebuild of the old one must
|
|
122
137
|
// not land on the new one. Nor may a seek still on its way to the old one stay on the clock: cleared
|
|
123
138
|
// here and not on `emptied`, which a rebuild of the same media fires too.
|
|
139
|
+
const loadedExternalRef = useRef(new Map<string, { raw: string, ass: string }>())
|
|
140
|
+
const activeExternalRef = useRef<{ id: string, ass: string } | null>(null)
|
|
141
|
+
const subtitleSelectionGen = useRef(0)
|
|
142
|
+
|
|
124
143
|
useEffect(() => {
|
|
125
144
|
restarts.current = { count: 0, at: 0 }
|
|
126
|
-
|
|
145
|
+
streams.current = { audio: [], subtitles: [] }
|
|
146
|
+
activeExternalRef.current = null
|
|
147
|
+
loadedExternalRef.current.clear()
|
|
148
|
+
subtitleSelectionGen.current++
|
|
149
|
+
if (size !== undefined) {
|
|
150
|
+
player.setSourceState({
|
|
151
|
+
playbackErrors: [],
|
|
152
|
+
seekingTo: undefined,
|
|
153
|
+
selectedSubtitleTrack: undefined,
|
|
154
|
+
subtitleTracks: externalSubtitlesRef.current.map((track) => ({
|
|
155
|
+
id: `${EXT_SUBTITLE_PREFIX}${track.id}`,
|
|
156
|
+
label: track.label.trim() || track.language || track.id,
|
|
157
|
+
})),
|
|
158
|
+
})
|
|
159
|
+
}
|
|
127
160
|
return () => { if (restartTimer.current) clearTimeout(restartTimer.current) }
|
|
128
161
|
// `player` is stable; listing it would not re-run this, and the reset belongs to the media
|
|
129
162
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
@@ -197,19 +230,92 @@ export const usePlayback = (
|
|
|
197
230
|
const labels = useLabels()
|
|
198
231
|
const labelsRef = useRef(labels)
|
|
199
232
|
labelsRef.current = labels
|
|
233
|
+
const buildSubtitleTracks = useCallback((currentLabels: Labels) => [
|
|
234
|
+
...toNamedTracks(streams.current.subtitles, currentLabels),
|
|
235
|
+
...externalSubtitlesRef.current.map((track) => ({
|
|
236
|
+
id: `${EXT_SUBTITLE_PREFIX}${track.id}`,
|
|
237
|
+
label: track.label.trim() || track.language || track.id,
|
|
238
|
+
})),
|
|
239
|
+
], [])
|
|
240
|
+
|
|
241
|
+
const loadExternalAss = useCallback(async (track: ExternalSubtitleTrack, mediaSize: number): Promise<string> => {
|
|
242
|
+
const cacheKey = `${mediaSize}:${track.id}`
|
|
243
|
+
const cached = loadedExternalRef.current.get(cacheKey)
|
|
244
|
+
if (track.content !== undefined) {
|
|
245
|
+
if (cached && cached.raw === track.content) return cached.ass
|
|
246
|
+
const ass = toAssSubtitles(track.content, track.format, track.label)
|
|
247
|
+
loadedExternalRef.current.set(cacheKey, { raw: track.content, ass })
|
|
248
|
+
return ass
|
|
249
|
+
}
|
|
250
|
+
if (cached) return cached.ass
|
|
251
|
+
if (!track.load) throw new Error('External subtitle track has no content or loader')
|
|
252
|
+
const raw = await track.load()
|
|
253
|
+
const ass = toAssSubtitles(raw, track.format, track.label)
|
|
254
|
+
if (sizeRef.current === mediaSize) loadedExternalRef.current.set(cacheKey, { raw, ass })
|
|
255
|
+
return ass
|
|
256
|
+
}, [])
|
|
257
|
+
|
|
200
258
|
// named again when the labels change, as they do when a catalogue arrives after the streams
|
|
201
259
|
useEffect(() => {
|
|
202
260
|
const { audio, subtitles } = streams.current
|
|
203
|
-
if (subtitles.length
|
|
261
|
+
if (subtitles.length || (sizeRef.current !== undefined && externalSubtitlesRef.current.length)) {
|
|
262
|
+
player.setSourceState({ subtitleTracks: buildSubtitleTracks(labels) })
|
|
263
|
+
}
|
|
204
264
|
if (audio.length) player.setSourceState({ audioTracks: toNamedTracks(audio, labels) })
|
|
205
|
-
}, [player, labels])
|
|
265
|
+
}, [player, labels, buildSubtitleTracks])
|
|
206
266
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
const streamIndex = typeof id === 'number' ? id : undefined
|
|
267
|
+
const selectSubtitleTrack = useCallback((id: string | number | undefined): void | Promise<void> => {
|
|
268
|
+
const mediaSize = sizeRef.current
|
|
269
|
+
const gen = ++subtitleSelectionGen.current
|
|
211
270
|
const wasOff = player.selectedSubtitleTrack === undefined
|
|
212
|
-
subtitleChoiceFor.current =
|
|
271
|
+
subtitleChoiceFor.current = mediaSize
|
|
272
|
+
|
|
273
|
+
if (id === undefined) {
|
|
274
|
+
activeExternalRef.current = null
|
|
275
|
+
player.setSourceState({ selectedSubtitleTrack: undefined })
|
|
276
|
+
controllerRef.current?.clearExternalSubtitleTrack()
|
|
277
|
+
controllerRef.current?.selectSubtitleStream(undefined)
|
|
278
|
+
onSubtitleLanguageRef.current?.(null)
|
|
279
|
+
return
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (isExternalTrackId(id)) {
|
|
283
|
+
const rawId = rawExternalTrackId(id)
|
|
284
|
+
const track = externalSubtitlesRef.current.find((entry) => entry.id === rawId)
|
|
285
|
+
if (!track || mediaSize === undefined) return Promise.reject(new Error('External subtitle track not found'))
|
|
286
|
+
|
|
287
|
+
const activate = (ass: string) => {
|
|
288
|
+
activeExternalRef.current = { id, ass }
|
|
289
|
+
player.setSourceState({ selectedSubtitleTrack: id })
|
|
290
|
+
controllerRef.current?.setExternalSubtitleTrack(ass)
|
|
291
|
+
const language = languageOf({ language: track.language, title: track.label })
|
|
292
|
+
?? (wasOff ? track.label.trim() || 'und' : undefined)
|
|
293
|
+
if (language !== undefined) onSubtitleLanguageRef.current?.(language)
|
|
294
|
+
if (language && playsIn(playingAudio(), language)) onAlwaysSubtitlesRef.current?.(true)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const cacheKey = `${mediaSize}:${track.id}`
|
|
298
|
+
const cached = loadedExternalRef.current.get(cacheKey)
|
|
299
|
+
if (track.content !== undefined) {
|
|
300
|
+
const ass = cached && cached.raw === track.content
|
|
301
|
+
? cached.ass
|
|
302
|
+
: toAssSubtitles(track.content, track.format, track.label)
|
|
303
|
+
loadedExternalRef.current.set(cacheKey, { raw: track.content, ass })
|
|
304
|
+
activate(ass)
|
|
305
|
+
return
|
|
306
|
+
}
|
|
307
|
+
if (cached) {
|
|
308
|
+
activate(cached.ass)
|
|
309
|
+
return
|
|
310
|
+
}
|
|
311
|
+
return loadExternalAss(track, mediaSize).then((ass) => {
|
|
312
|
+
if (subtitleSelectionGen.current !== gen || sizeRef.current !== mediaSize) return
|
|
313
|
+
activate(ass)
|
|
314
|
+
})
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const streamIndex = typeof id === 'number' ? id : undefined
|
|
318
|
+
activeExternalRef.current = null
|
|
213
319
|
player.setSourceState({ selectedSubtitleTrack: streamIndex })
|
|
214
320
|
controllerRef.current?.selectSubtitleStream(streamIndex)
|
|
215
321
|
const stream = streams.current.subtitles.find((subtitle) => subtitle.streamIndex === streamIndex)
|
|
@@ -218,7 +324,60 @@ export const usePlayback = (
|
|
|
218
324
|
: languageOf(stream) ?? (wasOff ? stream?.title?.trim() || 'und' : undefined)
|
|
219
325
|
if (language !== undefined) onSubtitleLanguageRef.current?.(language)
|
|
220
326
|
if (language && playsIn(playingAudio(), language)) onAlwaysSubtitlesRef.current?.(true)
|
|
221
|
-
}, [player, playingAudio])
|
|
327
|
+
}, [player, playingAudio, loadExternalAss])
|
|
328
|
+
|
|
329
|
+
const autoSelectExternalIfNeeded = useCallback(() => {
|
|
330
|
+
const mediaSize = sizeRef.current
|
|
331
|
+
if (!controllerRef.current || mediaSize === undefined) return
|
|
332
|
+
if (subtitleChoiceFor.current === mediaSize) return
|
|
333
|
+
const pref = preferencesRef.current.subtitles
|
|
334
|
+
if (pref == null) return
|
|
335
|
+
const playing = playingAudio()
|
|
336
|
+
const audioList = playing ? [playing] : []
|
|
337
|
+
const muxedStart = startingTracks(audioList, streams.current.subtitles, preferencesRef.current).subtitles
|
|
338
|
+
// Muxed tracks matched the viewer's preference, or subtitles should stay off because audio is in it.
|
|
339
|
+
if (muxedStart === null || muxedStart >= 0) return
|
|
340
|
+
|
|
341
|
+
const ext = externalSubtitlesRef.current
|
|
342
|
+
if (!ext.length) return
|
|
343
|
+
const extStart = startingTracks(
|
|
344
|
+
audioList,
|
|
345
|
+
ext.map((track) => ({ language: track.language, title: track.label })),
|
|
346
|
+
preferencesRef.current,
|
|
347
|
+
).subtitles
|
|
348
|
+
if (extStart === null || extStart < 0) return
|
|
349
|
+
const match = ext[extStart]
|
|
350
|
+
if (!match) return
|
|
351
|
+
const storeId = `${EXT_SUBTITLE_PREFIX}${match.id}`
|
|
352
|
+
if (player.selectedSubtitleTrack === storeId) return
|
|
353
|
+
|
|
354
|
+
const gen = ++subtitleSelectionGen.current
|
|
355
|
+
const apply = (ass: string) => {
|
|
356
|
+
activeExternalRef.current = { id: storeId, ass }
|
|
357
|
+
player.setSourceState({ selectedSubtitleTrack: storeId })
|
|
358
|
+
controllerRef.current?.setExternalSubtitleTrack(ass)
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const cacheKey = `${mediaSize}:${match.id}`
|
|
362
|
+
const cached = loadedExternalRef.current.get(cacheKey)
|
|
363
|
+
if (match.content !== undefined) {
|
|
364
|
+
const ass = cached && cached.raw === match.content
|
|
365
|
+
? cached.ass
|
|
366
|
+
: toAssSubtitles(match.content, match.format, match.label)
|
|
367
|
+
loadedExternalRef.current.set(cacheKey, { raw: match.content, ass })
|
|
368
|
+
apply(ass)
|
|
369
|
+
return
|
|
370
|
+
}
|
|
371
|
+
if (cached) {
|
|
372
|
+
apply(cached.ass)
|
|
373
|
+
return
|
|
374
|
+
}
|
|
375
|
+
void loadExternalAss(match, mediaSize).then((ass) => {
|
|
376
|
+
if (subtitleSelectionGen.current !== gen || sizeRef.current !== mediaSize) return
|
|
377
|
+
if (subtitleChoiceFor.current === mediaSize) return
|
|
378
|
+
apply(ass)
|
|
379
|
+
}, () => {})
|
|
380
|
+
}, [player, playingAudio, loadExternalAss])
|
|
222
381
|
|
|
223
382
|
/*
|
|
224
383
|
* Counts the seeks asked for, so a seek that a newer one replaced never moves the playhead, nor clears `seekingTo`.
|
|
@@ -348,6 +507,33 @@ export const usePlayback = (
|
|
|
348
507
|
setSourceState({ selectSubtitleTrack, selectAudioTrack, requestSeek })
|
|
349
508
|
}, [setSourceState, selectSubtitleTrack, selectAudioTrack, requestSeek])
|
|
350
509
|
|
|
510
|
+
const prevRevisionRef = useRef(externalSubtitlesRevision)
|
|
511
|
+
useEffect(() => {
|
|
512
|
+
if (prevRevisionRef.current !== externalSubtitlesRevision) {
|
|
513
|
+
prevRevisionRef.current = externalSubtitlesRevision
|
|
514
|
+
loadedExternalRef.current.clear()
|
|
515
|
+
}
|
|
516
|
+
if (size === undefined) return
|
|
517
|
+
const active = activeExternalRef.current
|
|
518
|
+
if (active) {
|
|
519
|
+
const currentTrack = externalSubtitlesRef.current.find((track) => `${EXT_SUBTITLE_PREFIX}${track.id}` === active.id)
|
|
520
|
+
if (!currentTrack) {
|
|
521
|
+
activeExternalRef.current = null
|
|
522
|
+
controllerRef.current?.clearExternalSubtitleTrack()
|
|
523
|
+
player.setSourceState({ selectedSubtitleTrack: undefined })
|
|
524
|
+
} else if (currentTrack.content !== undefined) {
|
|
525
|
+
const ass = toAssSubtitles(currentTrack.content, currentTrack.format, currentTrack.label)
|
|
526
|
+
loadedExternalRef.current.set(`${size}:${currentTrack.id}`, { raw: currentTrack.content, ass })
|
|
527
|
+
if (ass !== active.ass) {
|
|
528
|
+
activeExternalRef.current = { id: active.id, ass }
|
|
529
|
+
controllerRef.current?.setExternalSubtitleTrack(ass)
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
player.setSourceState({ subtitleTracks: buildSubtitleTracks(labelsRef.current) })
|
|
534
|
+
autoSelectExternalIfNeeded()
|
|
535
|
+
}, [player, setSourceState, size, externalKey, externalSubtitlesRevision, buildSubtitleTracks, autoSelectExternalIfNeeded])
|
|
536
|
+
|
|
351
537
|
useEffect(() => {
|
|
352
538
|
if (!video || !subtitles || !size || !read) return
|
|
353
539
|
// Discard stale resume state once another media starts loading, even if it aborts before onReady.
|
|
@@ -506,9 +692,10 @@ export const usePlayback = (
|
|
|
506
692
|
// for the audio playing, which may be one the viewer picked
|
|
507
693
|
const playing = playingAudio()
|
|
508
694
|
const start = startingTracks(playing ? [playing] : [], subtitles, preferencesRef.current).subtitles
|
|
695
|
+
const keepChoice = subtitleChoiceFor.current === size || activeExternalRef.current !== null
|
|
509
696
|
player.setSourceState({
|
|
510
|
-
subtitleTracks:
|
|
511
|
-
...
|
|
697
|
+
subtitleTracks: buildSubtitleTracks(labelsRef.current),
|
|
698
|
+
...keepChoice ? {} : { selectedSubtitleTrack: start === null ? undefined : subtitles[Math.max(start, 0)]?.streamIndex },
|
|
512
699
|
})
|
|
513
700
|
},
|
|
514
701
|
onAudioStreams: (audio, selected) => {
|
|
@@ -530,7 +717,12 @@ export const usePlayback = (
|
|
|
530
717
|
})
|
|
531
718
|
// the renderer starts on the first track, so the one the store holds (off included) is applied to it
|
|
532
719
|
const chosen = player.selectedSubtitleTrack
|
|
533
|
-
|
|
720
|
+
if (activeExternalRef.current && typeof chosen === 'string' && chosen === activeExternalRef.current.id) {
|
|
721
|
+
controller.setExternalSubtitleTrack(activeExternalRef.current.ass)
|
|
722
|
+
} else {
|
|
723
|
+
controller.selectSubtitleStream(typeof chosen === 'number' ? chosen : undefined)
|
|
724
|
+
}
|
|
725
|
+
autoSelectExternalIfNeeded()
|
|
534
726
|
} catch (error) {
|
|
535
727
|
fail(error)
|
|
536
728
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/// <reference types="@emotion/react/types/css-prop" />
|
|
2
2
|
import type { ReactNode } from 'react'
|
|
3
|
-
import type { MediaChapter } from '../engine'
|
|
3
|
+
import type { ExternalSubtitleFormat, MediaChapter } from '../engine'
|
|
4
4
|
import type { DownloadedRange, VolumeNormalizerControl } from './source-feature'
|
|
5
5
|
import type { DelegatedSelection, DelegatedTracks, ExternalThumbnails, PassThroughPictureInPicture, PlayerMedia } from './media'
|
|
6
|
-
import type { ExposePlayerOptions, ServedPlayer } from '../remote'
|
|
6
|
+
import type { ExposePlayerOptions, RemoteExternalSubtitleTrack, ServedPlayer } from '../remote'
|
|
7
7
|
|
|
8
8
|
import { useEffect, useMemo, useRef, useState } from 'react'
|
|
9
9
|
import { css } from '@emotion/react'
|
|
@@ -47,6 +47,30 @@ export type HostFullscreen = {
|
|
|
47
47
|
request: () => void
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
/** One step in episode navigation (previous or next episode) shown next to the play control. */
|
|
51
|
+
export type EpisodeStep = {
|
|
52
|
+
/** Custom tooltip and accessible label for this button; falls back to the localized default label. */
|
|
53
|
+
title?: string
|
|
54
|
+
/** Invoked when the viewer clicks this button. */
|
|
55
|
+
request: () => void
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Previous and next episode actions shown next to the play button when present. */
|
|
59
|
+
export type EpisodeNavigation = {
|
|
60
|
+
previous?: EpisodeStep | null
|
|
61
|
+
next?: EpisodeStep | null
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** An external subtitle track loaded outside the media container and rendered via `SubtitleRenderer`. */
|
|
65
|
+
export type ExternalSubtitleTrack = {
|
|
66
|
+
id: string
|
|
67
|
+
language: string
|
|
68
|
+
label: string
|
|
69
|
+
format: ExternalSubtitleFormat
|
|
70
|
+
content?: string
|
|
71
|
+
load?: () => Promise<string>
|
|
72
|
+
}
|
|
73
|
+
|
|
50
74
|
/** Shared by both arms: nothing here depends on who owns the media. */
|
|
51
75
|
type CommonOptions = {
|
|
52
76
|
/**
|
|
@@ -80,6 +104,9 @@ type CommonOptions = {
|
|
|
80
104
|
/** The viewer flipped the switch, to `autoplayNext`: what a host saves to pass back as `autoplayNext`. */
|
|
81
105
|
onAutoplayNextChange?: (autoplayNext: boolean) => void
|
|
82
106
|
|
|
107
|
+
/** Previous and next episode navigation controls shown next to play. An embedder setting them via `expose` wins. */
|
|
108
|
+
episodeNavigation?: EpisodeNavigation
|
|
109
|
+
|
|
83
110
|
/**
|
|
84
111
|
* Named spans of the timeline, drawn as segments on the seekbar.
|
|
85
112
|
*
|
|
@@ -294,6 +321,11 @@ export type MediaPlayerLocalOptions =
|
|
|
294
321
|
* viewer's own language is both. True, the default, starts them as `preferredSubtitleLanguage` says.
|
|
295
322
|
*/
|
|
296
323
|
alwaysShowSubtitles?: boolean
|
|
324
|
+
/**
|
|
325
|
+
* Sidecar subtitle tracks rendered through `SubtitleRenderer` alongside any muxed streams.
|
|
326
|
+
* An embedder setting tracks via `expose` wins while connected.
|
|
327
|
+
*/
|
|
328
|
+
externalSubtitles?: ExternalSubtitleTrack[]
|
|
297
329
|
}
|
|
298
330
|
|
|
299
331
|
/**
|
|
@@ -400,6 +432,11 @@ const PlayerRoot = ({ options, children }: { options: MediaPlayerOptions, childr
|
|
|
400
432
|
const [endingOffered, setEndingOffered] = useState(true)
|
|
401
433
|
const [embedderAutoplayNext, setEmbedderAutoplayNext] = useState<boolean | null>(null)
|
|
402
434
|
const [autoplayNext, setAutoplayNext] = useState(options.autoplayNext)
|
|
435
|
+
const [embedderPrevious, setEmbedderPrevious] = useState<boolean | string | null | undefined>(undefined)
|
|
436
|
+
const [embedderNext, setEmbedderNext] = useState<boolean | string | null | undefined>(undefined)
|
|
437
|
+
const [embedderSubtitles, setEmbedderSubtitles] = useState<RemoteExternalSubtitleTrack[] | null>(null)
|
|
438
|
+
const [embedderSubtitlesRevision, setEmbedderSubtitlesRevision] = useState(0)
|
|
439
|
+
const pendingSubtitleLoads = useRef(new Map<string, { resolve: (content: string) => void, reject: (error: Error) => void }>())
|
|
403
440
|
const [served] = useState<ServedPlayer & { chapters: MediaChapter[], endingOffered: boolean, autoplayNext: boolean | null }>(
|
|
404
441
|
() => Object.assign(new EventTarget(), {
|
|
405
442
|
chapters: [],
|
|
@@ -412,8 +449,39 @@ const PlayerRoot = ({ options, children }: { options: MediaPlayerOptions, childr
|
|
|
412
449
|
setEmbedderAutoplayNext(on)
|
|
413
450
|
if (on !== null) setAutoplayNext(on)
|
|
414
451
|
},
|
|
452
|
+
onPreviousEpisode: (step: boolean | string | null | undefined) => setEmbedderPrevious(step),
|
|
453
|
+
onNextEpisode: (step: boolean | string | null | undefined) => setEmbedderNext(step),
|
|
454
|
+
onExternalSubtitles: (tracks: RemoteExternalSubtitleTrack[] | null) => {
|
|
455
|
+
if (tracks === null) {
|
|
456
|
+
for (const { reject } of pendingSubtitleLoads.current.values()) {
|
|
457
|
+
reject(new Error('External subtitle host disconnected'))
|
|
458
|
+
}
|
|
459
|
+
pendingSubtitleLoads.current.clear()
|
|
460
|
+
} else {
|
|
461
|
+
const ids = new Set(tracks.map((track) => track.id))
|
|
462
|
+
for (const [trackId, entry] of [...pendingSubtitleLoads.current.entries()]) {
|
|
463
|
+
if (!ids.has(trackId)) {
|
|
464
|
+
pendingSubtitleLoads.current.delete(trackId)
|
|
465
|
+
entry.reject(new Error('External subtitle track was removed'))
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
setEmbedderSubtitles(tracks)
|
|
470
|
+
setEmbedderSubtitlesRevision((rev) => rev + 1)
|
|
471
|
+
},
|
|
472
|
+
onProvideExternalSubtitle: (trackId: string, content: string | null, error?: string) => {
|
|
473
|
+
const pending = pendingSubtitleLoads.current.get(trackId)
|
|
474
|
+
if (!pending) return
|
|
475
|
+
pendingSubtitleLoads.current.delete(trackId)
|
|
476
|
+
if (content !== null && !error) pending.resolve(content)
|
|
477
|
+
else pending.reject(new Error(error ?? 'External subtitle track failed to load'))
|
|
478
|
+
},
|
|
415
479
|
}),
|
|
416
480
|
)
|
|
481
|
+
useEffect(() => () => {
|
|
482
|
+
for (const { reject } of pendingSubtitleLoads.current.values()) reject(new Error('Player unmounted'))
|
|
483
|
+
pendingSubtitleLoads.current.clear()
|
|
484
|
+
}, [])
|
|
417
485
|
const exposeOptions = expose === true ? {} : expose || undefined
|
|
418
486
|
const latestExpose = useRef(exposeOptions)
|
|
419
487
|
latestExpose.current = exposeOptions
|
|
@@ -457,12 +525,56 @@ const PlayerRoot = ({ options, children }: { options: MediaPlayerOptions, childr
|
|
|
457
525
|
request: () => served.dispatchEvent(new Event('fullscreenrequest')),
|
|
458
526
|
})
|
|
459
527
|
|
|
528
|
+
const episodeNavigation = useMemo<EpisodeNavigation | undefined>(() => {
|
|
529
|
+
const resolveStep = (
|
|
530
|
+
override: boolean | string | null | undefined,
|
|
531
|
+
prop: EpisodeStep | null | undefined,
|
|
532
|
+
event: 'previousrequest' | 'nextrequest',
|
|
533
|
+
): EpisodeStep | null | undefined => {
|
|
534
|
+
if (override === undefined) return prop
|
|
535
|
+
if (override === false || override === null) return null
|
|
536
|
+
return {
|
|
537
|
+
...typeof override === 'string' && override ? { title: override } : {},
|
|
538
|
+
request: () => served.dispatchEvent(new Event(event)),
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
const previous = resolveStep(embedderPrevious, options.episodeNavigation?.previous, 'previousrequest')
|
|
542
|
+
const next = resolveStep(embedderNext, options.episodeNavigation?.next, 'nextrequest')
|
|
543
|
+
return previous !== undefined || next !== undefined ? { previous, next } : undefined
|
|
544
|
+
}, [embedderPrevious, embedderNext, options.episodeNavigation, served])
|
|
545
|
+
|
|
546
|
+
const remoteExternalSubtitles = useMemo<ExternalSubtitleTrack[] | undefined>(() => {
|
|
547
|
+
if (!embedderSubtitles) return undefined
|
|
548
|
+
return embedderSubtitles.map((track) => ({
|
|
549
|
+
id: track.id,
|
|
550
|
+
language: track.language,
|
|
551
|
+
label: track.label,
|
|
552
|
+
format: track.format,
|
|
553
|
+
...track.content !== undefined ? { content: track.content } : {},
|
|
554
|
+
load: track.content !== undefined
|
|
555
|
+
? undefined
|
|
556
|
+
: () => new Promise<string>((resolve, reject) => {
|
|
557
|
+
const previous = pendingSubtitleLoads.current.get(track.id)
|
|
558
|
+
if (previous) previous.reject(new Error('Replaced by a newer subtitle load request'))
|
|
559
|
+
pendingSubtitleLoads.current.set(track.id, { resolve, reject })
|
|
560
|
+
served.dispatchEvent(new CustomEvent('subtitleloadrequest', { detail: { trackId: track.id } }))
|
|
561
|
+
}),
|
|
562
|
+
}))
|
|
563
|
+
}, [embedderSubtitles, served])
|
|
564
|
+
|
|
565
|
+
const playbackOptions = useMemo(() => {
|
|
566
|
+
if (!local) return null
|
|
567
|
+
return remoteExternalSubtitles !== undefined
|
|
568
|
+
? { ...local, externalSubtitles: remoteExternalSubtitles }
|
|
569
|
+
: local
|
|
570
|
+
}, [local, remoteExternalSubtitles])
|
|
571
|
+
|
|
460
572
|
// one per file, as `size` is the source's identity everywhere here
|
|
461
573
|
const tap = useMemo(() => createPlaybackTap(), [size])
|
|
462
574
|
|
|
463
575
|
// Each of these no-ops on null inputs, which is what a remote arm supplies: it renders no <video>,
|
|
464
576
|
// so there is nothing for them to attach to and nothing to guard at the call site.
|
|
465
|
-
usePlayback(video, subtitleLayer,
|
|
577
|
+
usePlayback(video, subtitleLayer, playbackOptions, readsDownload ? tap.push : undefined, embedderSubtitlesRevision)
|
|
466
578
|
|
|
467
579
|
const normalizeVolumeProp = local?.normalizeVolume ?? true
|
|
468
580
|
const [normalizeVolume, setNormalizeVolume] = useState(normalizeVolumeProp)
|
|
@@ -652,6 +764,7 @@ const PlayerRoot = ({ options, children }: { options: MediaPlayerOptions, childr
|
|
|
652
764
|
shortcuts={options.shortcuts}
|
|
653
765
|
fullscreen={fullscreen}
|
|
654
766
|
endingOffered={endingOffered}
|
|
767
|
+
episodeNavigation={episodeNavigation}
|
|
655
768
|
>
|
|
656
769
|
{children}
|
|
657
770
|
</Chrome>
|