@banou/media-player 0.10.27 → 0.10.29

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.
@@ -0,0 +1,25 @@
1
+ import type { DelegatedSelection } from '../react/media';
2
+ /** A track as far as its language goes: a tag (`jpn`, `en-US`) or a name (`Japanese`, `English (US)`), and its title. */
3
+ export type LanguageTrack = {
4
+ language?: string;
5
+ title?: string;
6
+ };
7
+ /**
8
+ * What a track names its language by, to save as the viewer's preference: its tag, else its title, or undefined when
9
+ * neither names a language.
10
+ */
11
+ export declare const languageOf: (track: LanguageTrack | undefined) => string | undefined;
12
+ /**
13
+ * The index of the track to play for a preferred language (a tag or a name, as `languageOf` gives it), else of the
14
+ * track titled exactly as the preference, or -1 when there is neither. A full track wins over a signs, songs, forced
15
+ * or commentary one, then the track named exactly as the preference, then the one in the same region or script, then
16
+ * the first.
17
+ */
18
+ export declare const preferredTrack: (preference: string, tracks: readonly LanguageTrack[]) => number;
19
+ /**
20
+ * For a media the player does not own, whose host applies the viewer's preference itself: the option of `selection`
21
+ * to select for `preference` (as `onPreferredAudioLanguageChange` or `onPreferredSubtitleLanguageChange` reported
22
+ * it), null for the row that turns the feature off, or undefined to leave the selection as it is: no preference,
23
+ * nothing in that language, or already selected. Options are matched by their labels; disabled ones are skipped.
24
+ */
25
+ export declare const preferredSelection: (selection: Omit<DelegatedSelection, 'select'>, preference: string | null | undefined) => string | null | undefined;
@@ -0,0 +1,75 @@
1
+ //#region src/lib/language/index.ts
2
+ 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 co cr cs cu cv cy da de dv dz ee el en eo es et eu fa ff fi fj fo fr fy ga gd gl gn gu gv ha he hi ho hr ht hu hy hz ia id ie ig ii ik io is it iu ja jv ka kg ki kj kk kl km kn ko kr ks ku kv kw ky la lb lg li ln lo lt lu lv mg mh mi mk ml mn mr ms mt my na nb nd ne ng nl nn no nr nv ny oc oj om or os pa pi pl ps pt qu rm rn ro ru rw sa sc sd se sg si sk sl sm sn so sq sr ss st su sv sw ta te tg th ti tk tn to tr ts tt tw ty ug uk ur uz ve vi vo wa wo xh yi yo za zh zu fil yue".split(" "), t = [
3
+ "en-US",
4
+ "en-GB",
5
+ "es-419",
6
+ "es-ES",
7
+ "fr-CA",
8
+ "pt-BR",
9
+ "pt-PT",
10
+ "zh-Hans",
11
+ "zh-Hant"
12
+ ], n = /* @__PURE__ */ new Set([
13
+ "und",
14
+ "zxx",
15
+ "mul",
16
+ "mis"
17
+ ]), r = /sign|song|forced|commentary|karaoke|descri/i, i = /full|dialog/i, a = (e) => e.normalize("NFKC").toLowerCase().replace(/\s+/g, " ").trim(), o = new Intl.DisplayNames(["en"], {
18
+ type: "language",
19
+ fallback: "none"
20
+ }), s = (e) => {
21
+ if (/^[a-z]{2,3}(?:[-_][a-z\d]{2,8})*$/i.test(e)) try {
22
+ let t = new Intl.Locale(e.replace(/_/g, "-"));
23
+ return n.has(t.language) || !o.of(t.language) ? void 0 : {
24
+ language: t.language,
25
+ variant: t.region ?? t.script
26
+ };
27
+ } catch {
28
+ return;
29
+ }
30
+ }, c, l = () => {
31
+ if (c) return c;
32
+ let n = /* @__PURE__ */ new Map(), r = [...new Set(["en", globalThis.navigator?.language].filter((e) => !!e))], i = (e, t, r) => {
33
+ try {
34
+ let i = new Intl.DisplayNames([t], {
35
+ type: "language",
36
+ languageDisplay: r,
37
+ fallback: "none"
38
+ }).of(e);
39
+ i && !n.has(a(i)) && n.set(a(i), e);
40
+ } catch {}
41
+ };
42
+ for (let e of t) for (let t of [...r, e]) for (let n of ["dialect", "standard"]) i(e, t, n);
43
+ for (let t of e) for (let e of [...r, t]) i(t, e, "dialect");
44
+ return c = n;
45
+ }, u = (e) => {
46
+ let t = e?.trim();
47
+ if (!t) return;
48
+ let n = l(), r = n.get(a(t));
49
+ if (r) return s(r);
50
+ let [, i, o] = /^([^([]+)(?:\(([^)]*)\))?/.exec(t.normalize("NFKC")) ?? [], c = i ? n.get(a(i)) : void 0;
51
+ if (!c) return;
52
+ let u = o?.trim();
53
+ return (u && /^(?:[A-Z]{2}|\d{3})$/.test(u) ? s(`${c}-${u}`) : void 0) ?? s(c);
54
+ }, d = (e) => {
55
+ let t = e?.trim();
56
+ return t ? s(t) ?? u(t) : void 0;
57
+ }, f = (e) => d(e?.language) ? e.language.trim() : u(e?.title) ? e.title.trim() : void 0, p = ({ language: e = "", title: t = "" }) => {
58
+ let n = `${e} ${t}`;
59
+ return r.test(n) && !i.test(n);
60
+ }, m = (e, t) => {
61
+ let n = d(e), r = -1, i = 0;
62
+ return n && t.forEach((t, o) => {
63
+ let s = d(t.language) ?? u(t.title);
64
+ if (s?.language !== n.language) return;
65
+ let c = f(t), l = 1 + +(s.variant === n.variant) + (c && a(c) === a(e) ? 2 : 0) + (p(t) ? 0 : 4);
66
+ l > i && (r = o, i = l);
67
+ }), r === -1 ? t.findIndex(({ title: t }) => !!t?.trim() && a(t) === a(e)) : r;
68
+ }, h = (e, t) => {
69
+ if (t === void 0) return;
70
+ if (t === null) return e.offLabel && e.selectedId !== null ? null : void 0;
71
+ let n = e.options.filter((e) => !e.disabled), r = n[m(t, n.map(({ label: e }) => ({ title: e })))]?.id;
72
+ return r !== void 0 && r !== e.selectedId ? r : void 0;
73
+ };
74
+ //#endregion
75
+ export { f as languageOf, h as preferredSelection, m as preferredTrack };
@@ -1,4 +1,5 @@
1
1
  import type { ReactNode, Ref } from 'react';
2
+ import type { HostFullscreen } from '../video-player';
2
3
  export type ChromeProps = {
3
4
  ref?: Ref<HTMLDivElement> | ((element: HTMLDivElement | null) => void);
4
5
  /** Absent means render no video element: the media belongs to someone else and arrives as children. */
@@ -9,7 +10,9 @@ export type ChromeProps = {
9
10
  /** False draws no control bar at all, leaving the picture, the title and the overlay. */
10
11
  controls?: boolean;
11
12
  shortcuts?: 'window' | 'player';
13
+ /** Fullscreen kept by the host: a press calls its `request` instead of fullscreening the player. */
14
+ fullscreen?: HostFullscreen;
12
15
  children?: ReactNode;
13
16
  };
14
- export declare const Chrome: ({ ref, onVideoRef, onSubtitleRef, overlay, controls, shortcuts, children }: ChromeProps) => import("@emotion/react/jsx-runtime").JSX.Element;
17
+ export declare const Chrome: ({ ref, onVideoRef, onSubtitleRef, overlay, controls, shortcuts, fullscreen, children }: ChromeProps) => import("@emotion/react/jsx-runtime").JSX.Element;
15
18
  export default Chrome;
@@ -1,6 +1,8 @@
1
1
  import type { RefObject } from 'react';
2
- export declare const ControlBar: ({ shortcuts, root }: {
2
+ import type { HostFullscreen } from '../video-player';
3
+ export declare const ControlBar: ({ shortcuts, root, fullscreen: host }: {
3
4
  shortcuts?: 'window' | 'player';
4
5
  root: RefObject<HTMLDivElement | null>;
6
+ fullscreen?: HostFullscreen;
5
7
  }) => import("@emotion/react/jsx-runtime").JSX.Element;
6
8
  export default ControlBar;
@@ -5,6 +5,8 @@ export declare enum buttonSize {
5
5
  md = "md",
6
6
  lg = "lg"
7
7
  }
8
+ /** Whether the host page keeps the player's fullscreen: the `fullscreen` prop, or a mirror that took it over. */
9
+ export declare const HostKeepsFullscreen: import("react").Context<boolean>;
8
10
  interface TooltipDisplayProps {
9
11
  id: string;
10
12
  toolTipText: ReactNode;
@@ -20,6 +20,19 @@ export type MediaPlayerSource = {
20
20
  size?: undefined;
21
21
  readsDownload?: undefined;
22
22
  };
23
+ /**
24
+ * Fullscreen kept by the host page rather than by the player, for a page that fullscreens an element of its own around
25
+ * the player, so that fullscreen outlasts the player in it.
26
+ */
27
+ export type HostFullscreen = {
28
+ /** Whether the host shows the player fullscreen: the button shows it, and the `'player'` shortcuts follow it. */
29
+ active: boolean;
30
+ /**
31
+ * The viewer pressed fullscreen (the button, `f` or a double click): the host enters or leaves it. Called once per
32
+ * press, during that press's event, so the host's own `requestFullscreen` has the press's activation.
33
+ */
34
+ request: () => void;
35
+ };
23
36
  /** Shared by both arms: nothing here depends on who owns the media. */
24
37
  type CommonOptions = {
25
38
  /**
@@ -72,6 +85,12 @@ type CommonOptions = {
72
85
  * is fullscreen, so Space and the arrow keys keep scrolling the page and pressing its buttons everywhere else.
73
86
  */
74
87
  shortcuts?: 'window' | 'player';
88
+ /**
89
+ * Hand fullscreen to the host page: the player then never calls `requestFullscreen`, each press of its fullscreen
90
+ * calls `request` once, and the button and the `'player'` shortcuts follow `active`. Read by value, so it may be
91
+ * written inline. Without it the player fullscreens itself.
92
+ */
93
+ fullscreen?: HostFullscreen;
75
94
  /**
76
95
  * The app's own content over the video: a download readout, a badge, a logo, anything the player
77
96
  * itself has no opinion about.
@@ -106,6 +125,19 @@ type CommonOptions = {
106
125
  */
107
126
  onSeek?: (fraction: number) => void;
108
127
  onPlaybackError?: (error: unknown) => void;
128
+ /**
129
+ * The viewer picked an audio track in the settings menu, named by its language as the file or the source names it:
130
+ * its tag (`jpn`, `en-US`), else its title or label (`Japanese`). What an app saves to pass back as
131
+ * `preferredAudioLanguage`; the player saves nothing. A track that names no language reports nothing, and on a media
132
+ * the player does not own the report waits for the source's switch to succeed.
133
+ */
134
+ onPreferredAudioLanguageChange?: (language: string) => void;
135
+ /**
136
+ * As `onPreferredAudioLanguageChange`, for the subtitles menu, with null for subtitles turned off. A track that names
137
+ * no language reports only when it turns subtitles on, by its title or label (else `und`), so the next media starts
138
+ * with them on too.
139
+ */
140
+ onPreferredSubtitleLanguageChange?: (language: string | null) => void;
109
141
  };
110
142
  /**
111
143
  * Bytes in: the player owns the `<video>`, and libav feeds it a fragment at a time.
@@ -181,6 +213,18 @@ export type MediaPlayerLocalOptions = CommonOptions & MediaPlayerSource & {
181
213
  normalizeVolume?: boolean;
182
214
  /** The viewer flipped the switch, to `normalizeVolume`: what an app saves to pass back as `normalizeVolume`. */
183
215
  onNormalizeVolumeChange?: (normalizeVolume: boolean) => void;
216
+ /**
217
+ * The language a file starts its audio in, as `onPreferredAudioLanguageChange` reported it or as any tag or name
218
+ * (`ja`, `jpn`, `Japanese`). Its full track wins over a commentary, and a file with none plays its default. A
219
+ * track the viewer picks wins for that file. Read when a file starts, so a change applies from the next one.
220
+ */
221
+ preferredAudioLanguage?: string;
222
+ /**
223
+ * As `preferredAudioLanguage`, for subtitles, where signs and songs or forced tracks count as partial and a file
224
+ * with none starts on its first track. null starts every file with subtitles off, and a preference that names no
225
+ * language starts on the track titled as it is.
226
+ */
227
+ preferredSubtitleLanguage?: string | null;
184
228
  };
185
229
  /**
186
230
  * A media the caller owns and the player only drives.
@@ -1,8 +1,9 @@
1
1
  import type { Transport } from 'osra';
2
+ import type { MediaChapter } from '../engine';
2
3
  import type { PlayerMedia } from '../react/media';
3
4
  import type { Writable } from './protocol';
4
5
  export type { PlayerEvent, PlayerSnapshot, PlayerUpdate } from './protocol';
5
- export { DEFAULT_PLAYER_ID, PLAYER_CHANNEL, PLAYER_EVENTS } from './protocol';
6
+ export { DEFAULT_PLAYER_ID, PLAYER_CHANNEL, PLAYER_EVENTS, SERVED_EVENTS } from './protocol';
6
7
  /**
7
8
  * Where a player serves from, and to whom.
8
9
  *
@@ -41,6 +42,18 @@ export type ExposePlayerOptions = {
41
42
  * and a channel that closed in between would drop every embedder listening to the others.
42
43
  */
43
44
  signal?: AbortSignal;
45
+ /** What `<MediaPlayer expose>` serves beside its media. A document serving a bare element leaves it out. */
46
+ player?: ServedPlayer;
47
+ };
48
+ /**
49
+ * The player around a served media, as `<MediaPlayer expose>` passes it: its chapters, sent with every snapshot and
50
+ * announced on its `chapterschange`, and its fullscreen, which a mirror made with `fullscreen: true` takes over. While
51
+ * one has it, `onHostFullscreen` is told the state that mirror sets, and null once none has it; the player then
52
+ * dispatches `fullscreenrequest` for each press of its fullscreen, which reaches the mirror.
53
+ */
54
+ export type ServedPlayer = EventTarget & {
55
+ readonly chapters: MediaChapter[];
56
+ onHostFullscreen: (active: boolean | null) => void;
44
57
  };
45
58
  /**
46
59
  * Serve `media` to this document's embedder. Returns the teardown for THIS media.
@@ -72,6 +85,13 @@ type Mirrored = Omit<PlayerMedia, Writable>;
72
85
  export type MediaPlayerHandle = {
73
86
  readonly [K in keyof Mirrored]: Mirrored[K];
74
87
  } & Readonly<Required<Pick<PlayerMedia, 'src' | 'currentSrc' | 'ended' | 'buffered' | 'seekable' | 'error' | 'videoWidth' | 'videoHeight'>>> & Required<Pick<PlayerMedia, Writable>> & {
88
+ /** The far player's chapters, empty for a bare element. A `chapterschange` event follows each change. */
89
+ readonly chapters: MediaChapter[];
90
+ /**
91
+ * With the `fullscreen` option, whether this page shows the far player fullscreen: its button and its `'player'`
92
+ * shortcuts follow it. Set it on every change. Without the option it stays here.
93
+ */
94
+ fullscreen: boolean;
75
95
  /**
76
96
  * Settles once the far side has first answered with its state; reads before that answer the
77
97
  * empty defaults. Stays pending for as long as nobody answers, and rejects with an AbortError on
@@ -129,6 +149,12 @@ export type MediaPlayerHandleOptions = {
129
149
  channel?: string;
130
150
  /** closes the mirror: `ready` rejects with an AbortError and nothing more is heard */
131
151
  signal?: AbortSignal;
152
+ /**
153
+ * Take the far player's fullscreen over, when it is a `<MediaPlayer expose>`: its button, `f` and double click then
154
+ * call no `requestFullscreen` and fire one `fullscreenrequest` on the handle each, inside the press's activation,
155
+ * and show the handle's `fullscreen`. Released on `destroy()`. A bare element ignores it.
156
+ */
157
+ fullscreen?: boolean;
132
158
  };
133
159
  /**
134
160
  * Mirror a player another document is serving, as a media of this one.