@banou/media-player 0.10.35 → 0.10.37
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 +24 -9
- package/dist/engine/index.js +1 -1
- package/dist/engine/picture-in-picture.d.ts +12 -1
- package/dist/{engine-BZDbaSfM.js → engine-x5oIkHAN.js} +209 -176
- package/dist/index.js +604 -600
- package/dist/language/index.d.ts +39 -5
- package/dist/language/index.js +53 -18
- package/dist/react/video-player.d.ts +18 -2
- package/package.json +1 -1
- package/src/lib/engine/picture-in-picture.ts +100 -34
- package/src/lib/language/index.ts +94 -13
- package/src/lib/react/hooks/use-playback.ts +20 -19
- package/src/lib/react/video-player.tsx +26 -7
package/dist/language/index.d.ts
CHANGED
|
@@ -22,10 +22,44 @@ export declare const languageTag: (text: string | undefined) => string | undefin
|
|
|
22
22
|
* the first.
|
|
23
23
|
*/
|
|
24
24
|
export declare const preferredTrack: (preference: string, tracks: readonly LanguageTrack[]) => number;
|
|
25
|
+
/** The viewer's preferences, as `startingTracks` reads them. Each may be left out. */
|
|
26
|
+
export type TrackPreferences = {
|
|
27
|
+
/**
|
|
28
|
+
* The audio language, as `preferredTrack` reads it. A media without a full track in it, or with none given, plays its
|
|
29
|
+
* original: a commentary or described audio in it is no dub.
|
|
30
|
+
*/
|
|
31
|
+
audio?: string;
|
|
32
|
+
/**
|
|
33
|
+
* The language the media was made in, as its source knows it (`ja` for anime): its full track is the original.
|
|
34
|
+
* Without it, or in a file with no track in it, the original is the file's default.
|
|
35
|
+
*/
|
|
36
|
+
original?: string;
|
|
37
|
+
/** The subtitle language, as `preferredTrack` reads it, or null for none. A media without it shows its default. */
|
|
38
|
+
subtitles?: string | null;
|
|
39
|
+
/** False starts subtitles off when the audio plays in their language. True when left out. */
|
|
40
|
+
alwaysSubtitles?: boolean;
|
|
41
|
+
};
|
|
42
|
+
/** Whether `track` plays in the language `language` names, as a tag or a name, read by the track's tag, else its title. */
|
|
43
|
+
export declare const playsIn: (track: LanguageTrack | undefined, language: string) => boolean;
|
|
44
|
+
/**
|
|
45
|
+
* The tracks a media starts on, by index: the full audio in `preferences.audio`, else the original, else -1 for the
|
|
46
|
+
* file's default, its first track; and the subtitles in `preferences.subtitles`, else -1 for the file's default, or null
|
|
47
|
+
* for none, when they are off or when `alwaysSubtitles` is false and the audio plays in their language.
|
|
48
|
+
*/
|
|
49
|
+
export declare const startingTracks: (audio: readonly LanguageTrack[], subtitles: readonly LanguageTrack[], { audio: audioLanguage, original, subtitles: subtitleLanguage, alwaysSubtitles }: TrackPreferences) => {
|
|
50
|
+
audio: number;
|
|
51
|
+
subtitles: number | null;
|
|
52
|
+
};
|
|
53
|
+
type Selection = Omit<DelegatedSelection, 'select'>;
|
|
25
54
|
/**
|
|
26
|
-
* For a media the player does not own, whose host applies the viewer's
|
|
27
|
-
* to select
|
|
28
|
-
*
|
|
29
|
-
*
|
|
55
|
+
* For a media the player does not own, whose host applies the viewer's preferences itself: the option of
|
|
56
|
+
* `selections[kind]` to select as `startingTracks` starts it, null for the row that turns subtitles off, or undefined
|
|
57
|
+
* to leave the selection as it is: the source's own default, or already selected. The audio selected stands for the
|
|
58
|
+
* default, and the subtitles are chosen for it, so a host switching both asks for the subtitles once the audio has
|
|
59
|
+
* switched. Options are matched by their labels; disabled ones are skipped.
|
|
30
60
|
*/
|
|
31
|
-
export declare const preferredSelection: (
|
|
61
|
+
export declare const preferredSelection: (kind: 'audio' | 'subtitles', selections: {
|
|
62
|
+
audio?: Selection;
|
|
63
|
+
subtitles?: Selection;
|
|
64
|
+
}, preferences: TrackPreferences) => string | null | undefined;
|
|
65
|
+
export {};
|
package/dist/language/index.js
CHANGED
|
@@ -46,37 +46,72 @@ var e = "aa ab ae af ak am an ar as av ay az ba be bg bi bm bn bo br bs ca ce ch
|
|
|
46
46
|
for (let e of t) for (let t of [...r, e]) for (let n of ["dialect", "standard"]) i(e, t, n);
|
|
47
47
|
for (let t of e) for (let e of [...r, t]) i(t, e, "dialect");
|
|
48
48
|
return l = n;
|
|
49
|
-
}, d = (
|
|
49
|
+
}, d, f = () => {
|
|
50
|
+
if (d) return d;
|
|
51
|
+
let e = /* @__PURE__ */ new Map(), n = [
|
|
52
|
+
"en",
|
|
53
|
+
globalThis.navigator?.language,
|
|
54
|
+
...t
|
|
55
|
+
].filter((e) => !!e);
|
|
56
|
+
for (let r of new Set(t.map((e) => new Intl.Locale(e).region).filter((e) => !!e))) for (let t of n) try {
|
|
57
|
+
let n = new Intl.DisplayNames([t], {
|
|
58
|
+
type: "region",
|
|
59
|
+
fallback: "none"
|
|
60
|
+
}).of(r);
|
|
61
|
+
n && e.set(o(n), r);
|
|
62
|
+
} catch {}
|
|
63
|
+
return d = e;
|
|
64
|
+
}, p = (e) => {
|
|
50
65
|
let t = e?.trim();
|
|
51
66
|
if (!t) return;
|
|
52
67
|
let n = u(), r = n.get(o(t));
|
|
53
68
|
if (r) return c(r);
|
|
54
69
|
let [, i, s] = /^([^([]+)(?:\(([^)]*)\))?/.exec(t.normalize("NFKC")) ?? [], l = i ? n.get(o(i)) : void 0;
|
|
55
70
|
if (!l) return;
|
|
56
|
-
let d = s?.trim();
|
|
57
|
-
return (
|
|
58
|
-
},
|
|
71
|
+
let d = s?.trim(), p = !d || a.has(d) ? void 0 : /^(?:[A-Z]{2}|\d{3})$/.test(d) ? d : f().get(o(d));
|
|
72
|
+
return (p ? c(`${l}-${p}`) : void 0) ?? c(l);
|
|
73
|
+
}, m = (e) => {
|
|
59
74
|
let t = e?.trim();
|
|
60
|
-
return t ? c(t) ??
|
|
61
|
-
},
|
|
62
|
-
let t =
|
|
75
|
+
return t ? c(t) ?? p(t) : void 0;
|
|
76
|
+
}, h = (e) => m(e?.language) ? e.language.trim() : p(e?.title) ? e.title.trim() : void 0, g = (e) => {
|
|
77
|
+
let t = m(e);
|
|
63
78
|
return t && (t.variant ? `${t.language}-${t.variant}` : t.language);
|
|
64
|
-
},
|
|
79
|
+
}, _ = ({ language: e = "", title: t = "" }) => {
|
|
65
80
|
let n = `${e} ${t}`;
|
|
66
81
|
return r.test(n) && !i.test(n);
|
|
67
|
-
},
|
|
68
|
-
let n =
|
|
82
|
+
}, v = ({ language: e, title: t }) => {
|
|
83
|
+
let n = m(e), r = p(t);
|
|
84
|
+
return n && !n.variant && r?.variant && r.language === n.language ? r : n ?? r;
|
|
85
|
+
}, y = (e, t) => {
|
|
86
|
+
let n = m(e), r = -1, i = 0;
|
|
69
87
|
return n && t.forEach((t, a) => {
|
|
70
|
-
let s =
|
|
88
|
+
let s = v(t);
|
|
71
89
|
if (s?.language !== n.language) return;
|
|
72
|
-
let c =
|
|
90
|
+
let c = h(t), l = 1 + +(s.variant === n.variant) + (c && o(c) === o(e) ? 2 : 0) + (_(t) ? 0 : 4);
|
|
73
91
|
l > i && (r = a, i = l);
|
|
74
92
|
}), r === -1 ? t.findIndex(({ title: t }) => !!t?.trim() && o(t) === o(e)) : r;
|
|
75
|
-
},
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
93
|
+
}, b = (e, t) => {
|
|
94
|
+
let n = e && (m(e.language) ?? p(e.title));
|
|
95
|
+
return !!n && m(t)?.language === n.language;
|
|
96
|
+
}, x = (e, t, { audio: n, original: r, subtitles: i, alwaysSubtitles: a = !0 }) => {
|
|
97
|
+
let o = n === void 0 ? -1 : y(n, e), s = o !== -1 && !_(e[o]) ? o : -1, c = s !== -1 || r === void 0 ? s : y(r, e);
|
|
98
|
+
return i === null ? {
|
|
99
|
+
audio: c,
|
|
100
|
+
subtitles: null
|
|
101
|
+
} : i === void 0 ? {
|
|
102
|
+
audio: c,
|
|
103
|
+
subtitles: -1
|
|
104
|
+
} : {
|
|
105
|
+
audio: c,
|
|
106
|
+
subtitles: !a && b(e[Math.max(c, 0)], i) ? null : y(i, t)
|
|
107
|
+
};
|
|
108
|
+
}, S = (e, t, n) => {
|
|
109
|
+
let r = t[e];
|
|
110
|
+
if (!r) return;
|
|
111
|
+
let i = r.options.filter((e) => !e.disabled), a = i.map(({ label: e }) => ({ title: e })), o = t.audio?.options.find(({ id: e }) => e === t.audio.selectedId), s = e === "audio" ? x(a, [], n).audio : x(o ? [{ title: o.label }] : [], a, n).subtitles;
|
|
112
|
+
if (s === null) return r.offLabel && r.selectedId !== null ? null : void 0;
|
|
113
|
+
let c = i[s]?.id;
|
|
114
|
+
return c !== void 0 && c !== r.selectedId ? c : void 0;
|
|
80
115
|
};
|
|
81
116
|
//#endregion
|
|
82
|
-
export {
|
|
117
|
+
export { h as languageOf, g as languageTag, b as playsIn, S as preferredSelection, y as preferredTrack, x as startingTracks };
|
|
@@ -147,6 +147,11 @@ type CommonOptions = {
|
|
|
147
147
|
* with them on too.
|
|
148
148
|
*/
|
|
149
149
|
onPreferredSubtitleLanguageChange?: (language: string | null) => void;
|
|
150
|
+
/**
|
|
151
|
+
* True when the viewer picked subtitles in the language the audio plays in: what an app saves to pass back as
|
|
152
|
+
* `alwaysShowSubtitles`, so the next media in it starts with them on too.
|
|
153
|
+
*/
|
|
154
|
+
onAlwaysShowSubtitlesChange?: (always: boolean) => void;
|
|
150
155
|
};
|
|
151
156
|
/**
|
|
152
157
|
* Bytes in: the player owns the `<video>`, and libav feeds it a fragment at a time.
|
|
@@ -224,8 +229,8 @@ export type MediaPlayerLocalOptions = CommonOptions & MediaPlayerSource & {
|
|
|
224
229
|
onNormalizeVolumeChange?: (normalizeVolume: boolean) => void;
|
|
225
230
|
/**
|
|
226
231
|
* The language a file starts its audio in, as `onPreferredAudioLanguageChange` reported it or as any tag or name
|
|
227
|
-
* (`ja`, `jpn`, `Japanese`).
|
|
228
|
-
* track the viewer picks wins for that file. Read when a file starts, so a change applies from the next one.
|
|
232
|
+
* (`ja`, `jpn`, `Japanese`). Only a full track counts, never a commentary or an audio description, and a file with
|
|
233
|
+
* none plays its original. A track the viewer picks wins for that file. Read when a file starts, so a change applies from the next one.
|
|
229
234
|
*/
|
|
230
235
|
preferredAudioLanguage?: string;
|
|
231
236
|
/**
|
|
@@ -234,6 +239,17 @@ export type MediaPlayerLocalOptions = CommonOptions & MediaPlayerSource & {
|
|
|
234
239
|
* language starts on the track titled as it is.
|
|
235
240
|
*/
|
|
236
241
|
preferredSubtitleLanguage?: string | null;
|
|
242
|
+
/**
|
|
243
|
+
* The language the media was made in, as its source knows it (`ja` for anime): a file with no full track in
|
|
244
|
+
* `preferredAudioLanguage` plays its full track in this one, else its default, its first track. Read as the
|
|
245
|
+
* preferences are.
|
|
246
|
+
*/
|
|
247
|
+
originalLanguage?: string;
|
|
248
|
+
/**
|
|
249
|
+
* False starts a file with subtitles off when its audio plays in `preferredSubtitleLanguage`, as it does when the
|
|
250
|
+
* viewer's own language is both. True, the default, starts them as `preferredSubtitleLanguage` says.
|
|
251
|
+
*/
|
|
252
|
+
alwaysShowSubtitles?: boolean;
|
|
237
253
|
};
|
|
238
254
|
/**
|
|
239
255
|
* A media the caller owns and the player only drives.
|
package/package.json
CHANGED
|
@@ -37,7 +37,10 @@ export type PictureInPictureOptions = {
|
|
|
37
37
|
*/
|
|
38
38
|
subtitles: HTMLElement
|
|
39
39
|
maxWidth?: number
|
|
40
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* Where the mirror is mounted. It must be in the document. Defaults to the video's parent in burn-in, where the
|
|
42
|
+
* mirror is the picture, and to the body in a window, so the window can outlive the player (see `destroy`).
|
|
43
|
+
*/
|
|
41
44
|
container?: HTMLElement
|
|
42
45
|
/** Overrides detection. Passing a mode the browser cannot honour is the caller's problem. */
|
|
43
46
|
mode?: PictureInPictureMode
|
|
@@ -47,6 +50,12 @@ export type PictureInPictureOptions = {
|
|
|
47
50
|
|
|
48
51
|
export type PictureInPictureController = {
|
|
49
52
|
toggle: () => Promise<void>
|
|
53
|
+
/**
|
|
54
|
+
* Burn-in, or a window that is not open, ends here. An open window stays open for a second, and the
|
|
55
|
+
* next controller created in the document in that time draws its own video into it, at that video's
|
|
56
|
+
* shape: an app that replaces the player for the next episode keeps the window with no click, which
|
|
57
|
+
* a new window would need. If no controller takes it in that second, it closes.
|
|
58
|
+
*/
|
|
50
59
|
destroy: () => void
|
|
51
60
|
mode: PictureInPictureMode
|
|
52
61
|
}
|
|
@@ -58,6 +67,36 @@ type Session = {
|
|
|
58
67
|
mirror: HTMLVideoElement
|
|
59
68
|
}
|
|
60
69
|
|
|
70
|
+
/** How long a destroyed controller's window waits for the next one; a remounted player asks within a few frames. */
|
|
71
|
+
export const HAND_OVER_MS = 1000
|
|
72
|
+
|
|
73
|
+
let handedOver: { session: Session, close: () => void, cancel: () => void } | undefined
|
|
74
|
+
|
|
75
|
+
const handOver = (session: Session) => {
|
|
76
|
+
handedOver?.close()
|
|
77
|
+
const cancel = () => {
|
|
78
|
+
clearTimeout(timer)
|
|
79
|
+
session.mirror.removeEventListener('leavepictureinpicture', close)
|
|
80
|
+
handedOver = undefined
|
|
81
|
+
}
|
|
82
|
+
const close = () => {
|
|
83
|
+
cancel()
|
|
84
|
+
if (document.pictureInPictureElement === session.mirror) void document.exitPictureInPicture().catch(() => {})
|
|
85
|
+
for (const track of session.stream.getTracks()) track.stop()
|
|
86
|
+
session.mirror.remove()
|
|
87
|
+
}
|
|
88
|
+
const timer = setTimeout(close, HAND_OVER_MS)
|
|
89
|
+
session.mirror.addEventListener('leavepictureinpicture', close)
|
|
90
|
+
handedOver = { session, close, cancel }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The window a destroyed controller left, until it closes. */
|
|
94
|
+
const takeOver = () => {
|
|
95
|
+
const held = handedOver
|
|
96
|
+
held?.cancel()
|
|
97
|
+
return held?.session
|
|
98
|
+
}
|
|
99
|
+
|
|
61
100
|
const canComposite = () =>
|
|
62
101
|
typeof HTMLCanvasElement !== 'undefined' && 'captureStream' in HTMLCanvasElement.prototype
|
|
63
102
|
|
|
@@ -128,8 +167,21 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
128
167
|
}
|
|
129
168
|
}
|
|
130
169
|
|
|
170
|
+
// Sized off the intrinsic dimensions, never the layout, so only a video of another size resizes a
|
|
171
|
+
// canvas that a MediaStream is capturing. A window taken over starts at the size of the video before.
|
|
172
|
+
const compositeSize = () => {
|
|
173
|
+
const width = Math.min(video.videoWidth || maxWidth, maxWidth)
|
|
174
|
+
return { width, height: Math.round(width * ((video.videoHeight || 9) / (video.videoWidth || 16))) }
|
|
175
|
+
}
|
|
176
|
+
|
|
131
177
|
const paint = () => {
|
|
132
|
-
if (session)
|
|
178
|
+
if (!session) return
|
|
179
|
+
const { composite, context } = session
|
|
180
|
+
const { width, height } = compositeSize()
|
|
181
|
+
if (video.videoWidth && (composite.width !== width || composite.height !== height)) {
|
|
182
|
+
Object.assign(composite, { width, height })
|
|
183
|
+
}
|
|
184
|
+
draw(composite, context)
|
|
133
185
|
}
|
|
134
186
|
|
|
135
187
|
const useFrameCallback = 'requestVideoFrameCallback' in video
|
|
@@ -233,26 +285,46 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
233
285
|
}
|
|
234
286
|
}
|
|
235
287
|
|
|
236
|
-
const
|
|
288
|
+
const start = () => {
|
|
289
|
+
schedule()
|
|
290
|
+
pausedRepaint = setInterval(() => { if (video.paused) paint() }, PAUSED_REPAINT_INTERVAL)
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const listen = (mirror: HTMLVideoElement) => {
|
|
294
|
+
mirror.addEventListener('play', onMirrorPlay)
|
|
295
|
+
mirror.addEventListener('pause', onMirrorPause)
|
|
296
|
+
mirror.addEventListener('leavepictureinpicture', onLeave)
|
|
297
|
+
video.addEventListener('play', onVideoPlay)
|
|
298
|
+
video.addEventListener('pause', onVideoPause)
|
|
299
|
+
video.addEventListener('seeked', onVideoSeeked)
|
|
300
|
+
bindMediaSession(true)
|
|
301
|
+
publishState()
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** Lets go of the session, mirror and window left as they are. */
|
|
305
|
+
const detach = () => {
|
|
237
306
|
stopLoop()
|
|
238
307
|
bindMediaSession(false)
|
|
239
308
|
video.removeEventListener('play', onVideoPlay)
|
|
240
309
|
video.removeEventListener('pause', onVideoPause)
|
|
241
310
|
video.removeEventListener('seeked', onVideoSeeked)
|
|
242
|
-
|
|
243
|
-
// a burn-in that never built a session can still have swapped the picture
|
|
244
|
-
restore?.()
|
|
245
|
-
return
|
|
246
|
-
}
|
|
247
|
-
const { stream, mirror } = session
|
|
248
|
-
mirror.removeEventListener('play', onMirrorPlay)
|
|
249
|
-
mirror.removeEventListener('pause', onMirrorPause)
|
|
250
|
-
mirror.removeEventListener('leavepictureinpicture', onLeave)
|
|
251
|
-
for (const track of stream.getTracks()) track.stop()
|
|
252
|
-
mirror.remove()
|
|
311
|
+
const held = session
|
|
253
312
|
session = undefined
|
|
313
|
+
held?.mirror.removeEventListener('play', onMirrorPlay)
|
|
314
|
+
held?.mirror.removeEventListener('pause', onMirrorPause)
|
|
315
|
+
held?.mirror.removeEventListener('leavepictureinpicture', onLeave)
|
|
316
|
+
return held
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const teardown = () => {
|
|
320
|
+
const held = detach()
|
|
321
|
+
if (held) {
|
|
322
|
+
for (const track of held.stream.getTracks()) track.stop()
|
|
323
|
+
held.mirror.remove()
|
|
324
|
+
}
|
|
254
325
|
// AFTER the mirror is gone. Restoring first puts the real picture and the overlay back while the
|
|
255
326
|
// composite is still mounted and painting, which is the doubled subtitles this mode exists to avoid.
|
|
327
|
+
// A burn-in that never built a session can still have swapped the picture.
|
|
256
328
|
restore?.()
|
|
257
329
|
}
|
|
258
330
|
|
|
@@ -285,14 +357,7 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
285
357
|
}
|
|
286
358
|
|
|
287
359
|
const enter = async () => {
|
|
288
|
-
|
|
289
|
-
// MediaStream is already capturing.
|
|
290
|
-
const width = Math.min(video.videoWidth || maxWidth, maxWidth)
|
|
291
|
-
const height = Math.round(width * ((video.videoHeight || 9) / (video.videoWidth || 16)))
|
|
292
|
-
|
|
293
|
-
const composite = document.createElement('canvas')
|
|
294
|
-
composite.width = width
|
|
295
|
-
composite.height = height
|
|
360
|
+
const composite = Object.assign(document.createElement('canvas'), compositeSize())
|
|
296
361
|
// The alpha channel is unused but has to stay: with `{ alpha: false }` Chrome's captureStream
|
|
297
362
|
// delivers the first frame and then nothing, with the track still "live" and no error anywhere.
|
|
298
363
|
const context = composite.getContext('2d')
|
|
@@ -312,10 +377,9 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
312
377
|
const mine: Session = { composite, context, stream: composite.captureStream(), mirror }
|
|
313
378
|
session = mine
|
|
314
379
|
mirror.srcObject = mine.stream
|
|
315
|
-
;(options.container ?? video.parentElement ?? document.body).appendChild(mirror)
|
|
380
|
+
;(options.container ?? (mode === 'burn-in' ? video.parentElement : null) ?? document.body).appendChild(mirror)
|
|
316
381
|
|
|
317
|
-
|
|
318
|
-
pausedRepaint = setInterval(() => { if (video.paused) paint() }, PAUSED_REPAINT_INTERVAL)
|
|
382
|
+
start()
|
|
319
383
|
void mirror.play().catch(() => {})
|
|
320
384
|
|
|
321
385
|
// requestPictureInPicture rejects while readyState is 0, and the wait is short enough that the
|
|
@@ -335,14 +399,7 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
335
399
|
return
|
|
336
400
|
}
|
|
337
401
|
|
|
338
|
-
mirror
|
|
339
|
-
mirror.addEventListener('pause', onMirrorPause)
|
|
340
|
-
mirror.addEventListener('leavepictureinpicture', onLeave)
|
|
341
|
-
video.addEventListener('play', onVideoPlay)
|
|
342
|
-
video.addEventListener('pause', onVideoPause)
|
|
343
|
-
video.addEventListener('seeked', onVideoSeeked)
|
|
344
|
-
bindMediaSession(true)
|
|
345
|
-
publishState()
|
|
402
|
+
listen(mirror)
|
|
346
403
|
|
|
347
404
|
if (mode === 'burn-in') {
|
|
348
405
|
// No window to open: the browser's own control does that, on the element now on screen.
|
|
@@ -386,12 +443,21 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
|
|
|
386
443
|
}
|
|
387
444
|
}
|
|
388
445
|
|
|
446
|
+
const taken = mode === 'window' ? takeOver() : undefined
|
|
447
|
+
if (taken) {
|
|
448
|
+
session = taken
|
|
449
|
+
// the last frame of the video before would otherwise stay up until this one has a frame
|
|
450
|
+
taken.context.fillRect(0, 0, taken.composite.width, taken.composite.height)
|
|
451
|
+
start()
|
|
452
|
+
listen(taken.mirror)
|
|
453
|
+
}
|
|
454
|
+
|
|
389
455
|
return {
|
|
390
456
|
toggle,
|
|
391
457
|
mode,
|
|
392
458
|
destroy: () => {
|
|
393
459
|
destroyed = true
|
|
394
|
-
if (mode
|
|
460
|
+
if (mode === 'window' && active()) return handOver(detach()!)
|
|
395
461
|
teardown()
|
|
396
462
|
if (mode === 'burn-in') options.onBurnedInChange?.(false)
|
|
397
463
|
},
|
|
@@ -55,6 +55,24 @@ const nameIndex = () => {
|
|
|
55
55
|
return names = index
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
let regions: Map<string, string> | undefined
|
|
59
|
+
// the regions FORMS name, by name in each of their languages: Crunchyroll's 'Español (América Latina)' is es-419 by the
|
|
60
|
+
// Portuguese name for 419, which Spanish's own CLDR names Latinoamérica
|
|
61
|
+
const regionIndex = () => {
|
|
62
|
+
if (regions) return regions
|
|
63
|
+
const index = new Map<string, string>()
|
|
64
|
+
const locales = ['en', globalThis.navigator?.language, ...FORMS].filter((locale): locale is string => !!locale)
|
|
65
|
+
for (const region of new Set(FORMS.map((tag) => new Intl.Locale(tag).region).filter((region): region is string => !!region))) {
|
|
66
|
+
for (const locale of locales) {
|
|
67
|
+
try {
|
|
68
|
+
const name = new Intl.DisplayNames([locale], { type: 'region', fallback: 'none' }).of(region)
|
|
69
|
+
if (name) index.set(key(name), region)
|
|
70
|
+
} catch {}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return regions = index
|
|
74
|
+
}
|
|
75
|
+
|
|
58
76
|
// a title or a label is read by name only: as a tag, 'SDH' would be Southern Kurdish and 'UK' Ukrainian
|
|
59
77
|
const parseName = (text: string | undefined): Language | undefined => {
|
|
60
78
|
const trimmed = text?.trim()
|
|
@@ -65,9 +83,11 @@ const parseName = (text: string | undefined): Language | undefined => {
|
|
|
65
83
|
const [, base, qualifier] = /^([^([]+)(?:\(([^)]*)\))?/.exec(trimmed.normalize('NFKC')) ?? []
|
|
66
84
|
const named = base ? index.get(key(base)) : undefined
|
|
67
85
|
if (!named) return undefined
|
|
68
|
-
const
|
|
69
|
-
// through Intl.Locale, which reads the 'UK' of 'English (UK)' as GB
|
|
70
|
-
|
|
86
|
+
const qualified = qualifier?.trim()
|
|
87
|
+
// a code goes through Intl.Locale, which reads the 'UK' of 'English (UK)' as GB
|
|
88
|
+
const region = !qualified || TRACK_NOTES.has(qualified) ? undefined
|
|
89
|
+
: /^(?:[A-Z]{2}|\d{3})$/.test(qualified) ? qualified : regionIndex().get(key(qualified))
|
|
90
|
+
return (region ? fromTag(`${named}-${region}`) : undefined) ?? fromTag(named)
|
|
71
91
|
}
|
|
72
92
|
|
|
73
93
|
const parseLanguage = (text: string | undefined) => {
|
|
@@ -97,6 +117,14 @@ const isPartial = ({ language = '', title = '' }: LanguageTrack) => {
|
|
|
97
117
|
return PARTIAL.test(text) && !FULL.test(text)
|
|
98
118
|
}
|
|
99
119
|
|
|
120
|
+
// a tag naming no region or script takes the one its title names in the same language, as MKV tags carry none:
|
|
121
|
+
// `chi` titled 'Chinese (Traditional)' is zh-Hant
|
|
122
|
+
const spokenIn = ({ language, title }: LanguageTrack) => {
|
|
123
|
+
const tagged = parseLanguage(language)
|
|
124
|
+
const named = parseName(title)
|
|
125
|
+
return tagged && !tagged.variant && named?.variant && named.language === tagged.language ? named : tagged ?? named
|
|
126
|
+
}
|
|
127
|
+
|
|
100
128
|
/**
|
|
101
129
|
* The index of the track to play for a preferred language (a tag or a name, as `languageOf` gives it), else of the
|
|
102
130
|
* track titled exactly as the preference, or -1 when there is neither. A full track wins over a signs, songs, forced
|
|
@@ -108,7 +136,7 @@ export const preferredTrack = (preference: string, tracks: readonly LanguageTrac
|
|
|
108
136
|
let best = -1
|
|
109
137
|
let bestScore = 0
|
|
110
138
|
if (wanted) tracks.forEach((track, index) => {
|
|
111
|
-
const found =
|
|
139
|
+
const found = spokenIn(track)
|
|
112
140
|
if (found?.language !== wanted.language) return
|
|
113
141
|
const named = languageOf(track)
|
|
114
142
|
const score = 1
|
|
@@ -123,19 +151,72 @@ export const preferredTrack = (preference: string, tracks: readonly LanguageTrac
|
|
|
123
151
|
return best !== -1 ? best : tracks.findIndex(({ title }) => !!title?.trim() && key(title) === key(preference))
|
|
124
152
|
}
|
|
125
153
|
|
|
154
|
+
/** The viewer's preferences, as `startingTracks` reads them. Each may be left out. */
|
|
155
|
+
export type TrackPreferences = {
|
|
156
|
+
/**
|
|
157
|
+
* The audio language, as `preferredTrack` reads it. A media without a full track in it, or with none given, plays its
|
|
158
|
+
* original: a commentary or described audio in it is no dub.
|
|
159
|
+
*/
|
|
160
|
+
audio?: string
|
|
161
|
+
/**
|
|
162
|
+
* The language the media was made in, as its source knows it (`ja` for anime): its full track is the original.
|
|
163
|
+
* Without it, or in a file with no track in it, the original is the file's default.
|
|
164
|
+
*/
|
|
165
|
+
original?: string
|
|
166
|
+
/** The subtitle language, as `preferredTrack` reads it, or null for none. A media without it shows its default. */
|
|
167
|
+
subtitles?: string | null
|
|
168
|
+
/** False starts subtitles off when the audio plays in their language. True when left out. */
|
|
169
|
+
alwaysSubtitles?: boolean
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Whether `track` plays in the language `language` names, as a tag or a name, read by the track's tag, else its title. */
|
|
173
|
+
export const playsIn = (track: LanguageTrack | undefined, language: string): boolean => {
|
|
174
|
+
const spoken = track && (parseLanguage(track.language) ?? parseName(track.title))
|
|
175
|
+
return !!spoken && parseLanguage(language)?.language === spoken.language
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* The tracks a media starts on, by index: the full audio in `preferences.audio`, else the original, else -1 for the
|
|
180
|
+
* file's default, its first track; and the subtitles in `preferences.subtitles`, else -1 for the file's default, or null
|
|
181
|
+
* for none, when they are off or when `alwaysSubtitles` is false and the audio plays in their language.
|
|
182
|
+
*/
|
|
183
|
+
export const startingTracks = (
|
|
184
|
+
audio: readonly LanguageTrack[],
|
|
185
|
+
subtitles: readonly LanguageTrack[],
|
|
186
|
+
{ audio: audioLanguage, original, subtitles: subtitleLanguage, alwaysSubtitles = true }: TrackPreferences,
|
|
187
|
+
): { audio: number, subtitles: number | null } => {
|
|
188
|
+
const match = audioLanguage === undefined ? -1 : preferredTrack(audioLanguage, audio)
|
|
189
|
+
const wanted = match !== -1 && !isPartial(audio[match]!) ? match : -1
|
|
190
|
+
const audioIndex = wanted !== -1 || original === undefined ? wanted : preferredTrack(original, audio)
|
|
191
|
+
if (subtitleLanguage === null) return { audio: audioIndex, subtitles: null }
|
|
192
|
+
if (subtitleLanguage === undefined) return { audio: audioIndex, subtitles: -1 }
|
|
193
|
+
const hidden = !alwaysSubtitles && playsIn(audio[Math.max(audioIndex, 0)], subtitleLanguage)
|
|
194
|
+
return { audio: audioIndex, subtitles: hidden ? null : preferredTrack(subtitleLanguage, subtitles) }
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
type Selection = Omit<DelegatedSelection, 'select'>
|
|
198
|
+
|
|
126
199
|
/**
|
|
127
|
-
* For a media the player does not own, whose host applies the viewer's
|
|
128
|
-
* to select
|
|
129
|
-
*
|
|
130
|
-
*
|
|
200
|
+
* For a media the player does not own, whose host applies the viewer's preferences itself: the option of
|
|
201
|
+
* `selections[kind]` to select as `startingTracks` starts it, null for the row that turns subtitles off, or undefined
|
|
202
|
+
* to leave the selection as it is: the source's own default, or already selected. The audio selected stands for the
|
|
203
|
+
* default, and the subtitles are chosen for it, so a host switching both asks for the subtitles once the audio has
|
|
204
|
+
* switched. Options are matched by their labels; disabled ones are skipped.
|
|
131
205
|
*/
|
|
132
206
|
export const preferredSelection = (
|
|
133
|
-
|
|
134
|
-
|
|
207
|
+
kind: 'audio' | 'subtitles',
|
|
208
|
+
selections: { audio?: Selection, subtitles?: Selection },
|
|
209
|
+
preferences: TrackPreferences,
|
|
135
210
|
): string | null | undefined => {
|
|
136
|
-
|
|
137
|
-
if (
|
|
211
|
+
const selection = selections[kind]
|
|
212
|
+
if (!selection) return undefined
|
|
138
213
|
const options = selection.options.filter((option) => !option.disabled)
|
|
139
|
-
const
|
|
214
|
+
const tracks = options.map(({ label }) => ({ title: label }))
|
|
215
|
+
const playing = selections.audio?.options.find(({ id }) => id === selections.audio!.selectedId)
|
|
216
|
+
const index = kind === 'audio'
|
|
217
|
+
? startingTracks(tracks, [], preferences).audio
|
|
218
|
+
: startingTracks(playing ? [{ title: playing.label }] : [], tracks, preferences).subtitles
|
|
219
|
+
if (index === null) return selection.offLabel && selection.selectedId !== null ? null : undefined
|
|
220
|
+
const id = options[index]?.id
|
|
140
221
|
return id !== undefined && id !== selection.selectedId ? id : undefined
|
|
141
222
|
}
|
|
@@ -8,7 +8,7 @@ import { isMediaElementError, startPlayback } from '../../engine'
|
|
|
8
8
|
import { usePlayer } from '../player'
|
|
9
9
|
import { useLabels } from '../labels'
|
|
10
10
|
import { toNamedTracks } from '../../utils/track-label'
|
|
11
|
-
import { languageOf,
|
|
11
|
+
import { languageOf, playsIn, startingTracks } from '../../language'
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* A wedged element is rebuilt, however many times it takes.
|
|
@@ -42,13 +42,6 @@ const SEEK_PREPARE_BUDGET_MS = 500
|
|
|
42
42
|
*/
|
|
43
43
|
const SEEK_DISPLAY_LIMIT_MS = 15_000
|
|
44
44
|
|
|
45
|
-
/** The subtitle stream a media starts on: the first in the preferred language, else the first; none when it is off. */
|
|
46
|
-
const preferredSubtitle = (preference: string | null | undefined, subtitles: SubtitleStream[]) => {
|
|
47
|
-
if (preference === null) return undefined
|
|
48
|
-
const index = preference === undefined ? 0 : preferredTrack(preference, subtitles)
|
|
49
|
-
return subtitles[Math.max(index, 0)]?.streamIndex
|
|
50
|
-
}
|
|
51
|
-
|
|
52
45
|
const messageOf = (error: unknown) =>
|
|
53
46
|
error instanceof Error ? error.message : String(error)
|
|
54
47
|
|
|
@@ -153,17 +146,24 @@ export const usePlayback = (
|
|
|
153
146
|
const sizeRef = useRef(size)
|
|
154
147
|
sizeRef.current = size
|
|
155
148
|
const streams = useRef<{ audio: AudioStream[], subtitles: SubtitleStream[] }>({ audio: [], subtitles: [] })
|
|
149
|
+
const playingAudio = useCallback(() => streams.current.audio.find((track) => track.streamIndex === player.selectedAudioTrack), [player])
|
|
156
150
|
|
|
157
151
|
const readRef = useRef(read)
|
|
158
152
|
readRef.current = read
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
153
|
+
const preferences = {
|
|
154
|
+
audio: options?.preferredAudioLanguage,
|
|
155
|
+
original: options?.originalLanguage,
|
|
156
|
+
subtitles: options?.preferredSubtitleLanguage,
|
|
157
|
+
alwaysSubtitles: options?.alwaysShowSubtitles,
|
|
158
|
+
}
|
|
159
|
+
const preferencesRef = useRef(preferences)
|
|
160
|
+
preferencesRef.current = preferences
|
|
163
161
|
const onAudioLanguageRef = useRef(options?.onPreferredAudioLanguageChange)
|
|
164
162
|
onAudioLanguageRef.current = options?.onPreferredAudioLanguageChange
|
|
165
163
|
const onSubtitleLanguageRef = useRef(options?.onPreferredSubtitleLanguageChange)
|
|
166
164
|
onSubtitleLanguageRef.current = options?.onPreferredSubtitleLanguageChange
|
|
165
|
+
const onAlwaysSubtitlesRef = useRef(options?.onAlwaysShowSubtitlesChange)
|
|
166
|
+
onAlwaysSubtitlesRef.current = options?.onAlwaysShowSubtitlesChange
|
|
167
167
|
const onSeekRef = useRef(options?.onSeek)
|
|
168
168
|
onSeekRef.current = options?.onSeek
|
|
169
169
|
const onPlaybackErrorRef = useRef(options?.onPlaybackError)
|
|
@@ -193,7 +193,8 @@ export const usePlayback = (
|
|
|
193
193
|
? null
|
|
194
194
|
: languageOf(stream) ?? (wasOff ? stream?.title?.trim() || 'und' : undefined)
|
|
195
195
|
if (language !== undefined) onSubtitleLanguageRef.current?.(language)
|
|
196
|
-
|
|
196
|
+
if (language && playsIn(playingAudio(), language)) onAlwaysSubtitlesRef.current?.(true)
|
|
197
|
+
}, [player, playingAudio])
|
|
197
198
|
|
|
198
199
|
/**
|
|
199
200
|
* Seek with the data already there, or after the deadline, whichever comes first.
|
|
@@ -382,10 +383,7 @@ export const usePlayback = (
|
|
|
382
383
|
cjkFontUrl,
|
|
383
384
|
bufferSize,
|
|
384
385
|
audioStreamIndex,
|
|
385
|
-
chooseAudioStream: (audio) =>
|
|
386
|
-
const preference = preferredAudioRef.current
|
|
387
|
-
return preference === undefined ? undefined : audio[preferredTrack(preference, audio)]?.streamIndex
|
|
388
|
-
},
|
|
386
|
+
chooseAudioStream: (audio) => audio[startingTracks(audio, [], preferencesRef.current).audio]?.streamIndex,
|
|
389
387
|
onReady: () => {
|
|
390
388
|
if (cancelled) return
|
|
391
389
|
player.setSourceState({ ready: true })
|
|
@@ -404,9 +402,12 @@ export const usePlayback = (
|
|
|
404
402
|
onSubtitleStreams: (subtitles) => {
|
|
405
403
|
if (cancelled) return
|
|
406
404
|
streams.current.subtitles = subtitles
|
|
405
|
+
// for the audio playing, which may be one the viewer picked
|
|
406
|
+
const playing = playingAudio()
|
|
407
|
+
const start = startingTracks(playing ? [playing] : [], subtitles, preferencesRef.current).subtitles
|
|
407
408
|
player.setSourceState({
|
|
408
409
|
subtitleTracks: toNamedTracks(subtitles, labelsRef.current),
|
|
409
|
-
...subtitleChoiceFor.current === size ? {} : { selectedSubtitleTrack:
|
|
410
|
+
...subtitleChoiceFor.current === size ? {} : { selectedSubtitleTrack: start === null ? undefined : subtitles[Math.max(start, 0)]?.streamIndex },
|
|
410
411
|
})
|
|
411
412
|
},
|
|
412
413
|
onAudioStreams: (audio, selected) => {
|
|
@@ -445,7 +446,7 @@ export const usePlayback = (
|
|
|
445
446
|
// identity. A streaming consumer passes a fresh closure on every state update, which is several
|
|
446
447
|
// times a second, and the restart loop reads as "Loading metadata" forever at a flat 0 B/s.
|
|
447
448
|
}, [
|
|
448
|
-
player, video, subtitles, size, publicPath, libavWorkerUrl, jassubWorkerUrl, jassubWasmUrl,
|
|
449
|
+
player, playingAudio, video, subtitles, size, publicPath, libavWorkerUrl, jassubWorkerUrl, jassubWasmUrl,
|
|
449
450
|
jassubLegacyWasmUrl, defaultFontUrl, cjkFontUrl, bufferSize, audioStreamIndex, autoplay, restartToken,
|
|
450
451
|
])
|
|
451
452
|
}
|