@mattebox/player 0.7.0 → 0.9.0

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.
Files changed (138) hide show
  1. package/dist/cdn/mattebox-player.min.js +3 -3
  2. package/dist/controls/icons.d.ts +5 -28
  3. package/dist/controls/icons.d.ts.map +1 -1
  4. package/dist/controls/icons.js +38 -29
  5. package/dist/controls/icons.js.map +1 -1
  6. package/dist/controls/markers.d.ts +39 -0
  7. package/dist/controls/markers.d.ts.map +1 -0
  8. package/dist/controls/markers.js +38 -0
  9. package/dist/controls/markers.js.map +1 -0
  10. package/dist/controls/menu.d.ts +14 -2
  11. package/dist/controls/menu.d.ts.map +1 -1
  12. package/dist/controls/menu.js +50 -4
  13. package/dist/controls/menu.js.map +1 -1
  14. package/dist/controls/native-tracks.d.ts +28 -0
  15. package/dist/controls/native-tracks.d.ts.map +1 -0
  16. package/dist/controls/native-tracks.js +30 -0
  17. package/dist/controls/native-tracks.js.map +1 -0
  18. package/dist/controls/quality-names.d.ts +15 -0
  19. package/dist/controls/quality-names.d.ts.map +1 -0
  20. package/dist/controls/quality-names.js +62 -0
  21. package/dist/controls/quality-names.js.map +1 -0
  22. package/dist/controls/track-names.d.ts +40 -0
  23. package/dist/controls/track-names.d.ts.map +1 -0
  24. package/dist/controls/track-names.js +59 -0
  25. package/dist/controls/track-names.js.map +1 -0
  26. package/dist/element.d.ts +60 -2
  27. package/dist/element.d.ts.map +1 -1
  28. package/dist/element.js +182 -23
  29. package/dist/element.js.map +1 -1
  30. package/dist/elements/audio-menu.d.ts +19 -8
  31. package/dist/elements/audio-menu.d.ts.map +1 -1
  32. package/dist/elements/audio-menu.js +60 -19
  33. package/dist/elements/audio-menu.js.map +1 -1
  34. package/dist/elements/component.d.ts.map +1 -1
  35. package/dist/elements/component.js +10 -0
  36. package/dist/elements/component.js.map +1 -1
  37. package/dist/elements/control-bar.d.ts +2 -0
  38. package/dist/elements/control-bar.d.ts.map +1 -1
  39. package/dist/elements/control-bar.js +23 -7
  40. package/dist/elements/control-bar.js.map +1 -1
  41. package/dist/elements/current-time.d.ts.map +1 -1
  42. package/dist/elements/current-time.js +1 -1
  43. package/dist/elements/current-time.js.map +1 -1
  44. package/dist/elements/drm-badge.d.ts +3 -2
  45. package/dist/elements/drm-badge.d.ts.map +1 -1
  46. package/dist/elements/drm-badge.js +11 -9
  47. package/dist/elements/drm-badge.js.map +1 -1
  48. package/dist/elements/duration.d.ts.map +1 -1
  49. package/dist/elements/duration.js +6 -2
  50. package/dist/elements/duration.js.map +1 -1
  51. package/dist/elements/error-screen.js +1 -1
  52. package/dist/elements/live-button.js +1 -1
  53. package/dist/elements/marker-button.d.ts +22 -0
  54. package/dist/elements/marker-button.d.ts.map +1 -0
  55. package/dist/elements/marker-button.js +95 -0
  56. package/dist/elements/marker-button.js.map +1 -0
  57. package/dist/elements/quality-menu.d.ts +4 -0
  58. package/dist/elements/quality-menu.d.ts.map +1 -1
  59. package/dist/elements/quality-menu.js +22 -8
  60. package/dist/elements/quality-menu.js.map +1 -1
  61. package/dist/elements/scan-button.js +1 -1
  62. package/dist/elements/seek-bar.d.ts +6 -1
  63. package/dist/elements/seek-bar.d.ts.map +1 -1
  64. package/dist/elements/seek-bar.js +37 -25
  65. package/dist/elements/seek-bar.js.map +1 -1
  66. package/dist/elements/shared.js +3 -3
  67. package/dist/elements/skip-button.js +2 -2
  68. package/dist/elements/skip-button.js.map +1 -1
  69. package/dist/elements/spinner.js +1 -1
  70. package/dist/elements/start-button.d.ts +5 -0
  71. package/dist/elements/start-button.d.ts.map +1 -1
  72. package/dist/elements/start-button.js +15 -2
  73. package/dist/elements/start-button.js.map +1 -1
  74. package/dist/elements/subtitles-menu.d.ts +7 -1
  75. package/dist/elements/subtitles-menu.d.ts.map +1 -1
  76. package/dist/elements/subtitles-menu.js +62 -20
  77. package/dist/elements/subtitles-menu.js.map +1 -1
  78. package/dist/elements/title.d.ts.map +1 -1
  79. package/dist/elements/title.js +3 -1
  80. package/dist/elements/title.js.map +1 -1
  81. package/dist/elements/volume-slider.js +1 -1
  82. package/dist/elements/volume.js +1 -1
  83. package/dist/entries/marker-button.d.ts +5 -0
  84. package/dist/entries/marker-button.d.ts.map +1 -0
  85. package/dist/entries/marker-button.js +9 -0
  86. package/dist/entries/marker-button.js.map +1 -0
  87. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.150.0 → _@oxc-project_runtime@0.152.0}/helpers/esm/asyncToGenerator.js +1 -1
  88. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.150.0 → _@oxc-project_runtime@0.152.0}/helpers/esm/defineProperty.js +1 -1
  89. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.150.0 → _@oxc-project_runtime@0.152.0}/helpers/esm/objectSpread2.js +1 -1
  90. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.150.0 → _@oxc-project_runtime@0.152.0}/helpers/esm/toPrimitive.js +1 -1
  91. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.150.0 → _@oxc-project_runtime@0.152.0}/helpers/esm/toPropertyKey.js +1 -1
  92. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.150.0 → _@oxc-project_runtime@0.152.0}/helpers/esm/typeof.js +1 -1
  93. package/dist/es2015/controls/chapters.js +1 -1
  94. package/dist/es2015/controls/icons.js +38 -29
  95. package/dist/es2015/controls/markers.js +36 -0
  96. package/dist/es2015/controls/menu.js +52 -5
  97. package/dist/es2015/controls/native-tracks.js +29 -0
  98. package/dist/es2015/controls/quality-names.js +67 -0
  99. package/dist/es2015/controls/track-names.js +57 -0
  100. package/dist/es2015/element.js +185 -29
  101. package/dist/es2015/elements/audio-menu.js +66 -20
  102. package/dist/es2015/elements/component.js +10 -0
  103. package/dist/es2015/elements/control-bar.js +23 -7
  104. package/dist/es2015/elements/current-time.js +1 -1
  105. package/dist/es2015/elements/drm-badge.js +12 -9
  106. package/dist/es2015/elements/duration.js +6 -2
  107. package/dist/es2015/elements/error-screen.js +1 -1
  108. package/dist/es2015/elements/live-button.js +1 -1
  109. package/dist/es2015/elements/marker-button.js +96 -0
  110. package/dist/es2015/elements/quality-menu.js +23 -8
  111. package/dist/es2015/elements/scan-button.js +1 -1
  112. package/dist/es2015/elements/seek-bar.js +42 -29
  113. package/dist/es2015/elements/shared.js +3 -3
  114. package/dist/es2015/elements/skip-button.js +2 -2
  115. package/dist/es2015/elements/spinner.js +1 -1
  116. package/dist/es2015/elements/start-button.js +15 -2
  117. package/dist/es2015/elements/subtitles-menu.js +72 -26
  118. package/dist/es2015/elements/title.js +3 -1
  119. package/dist/es2015/elements/volume-slider.js +1 -1
  120. package/dist/es2015/elements/volume.js +1 -1
  121. package/dist/es2015/entries/marker-button.js +7 -0
  122. package/dist/es2015/index.js +3 -1
  123. package/dist/es2015/presets.js +1 -1
  124. package/dist/es2015/style.js +1 -1
  125. package/dist/es2015/tags.js +2 -1
  126. package/dist/host.d.ts +3 -0
  127. package/dist/host.d.ts.map +1 -1
  128. package/dist/host.js.map +1 -1
  129. package/dist/index.d.ts +4 -0
  130. package/dist/index.d.ts.map +1 -1
  131. package/dist/index.js +3 -1
  132. package/dist/style.js +1 -1
  133. package/dist/tags.d.ts +1 -0
  134. package/dist/tags.d.ts.map +1 -1
  135. package/dist/tags.js +2 -1
  136. package/dist/tags.js.map +1 -1
  137. package/package.json +4 -5
  138. package/NOTICE +0 -24
@@ -0,0 +1,67 @@
1
+ //#region src/controls/quality-names.ts
2
+ /** The codec families a viewer may recognise, by their RFC 6381 prefix. */
3
+ const CODECS = [
4
+ [/^av(c1|c3)/, "H.264"],
5
+ [/^h(vc1|ev1)/, "HEVC"],
6
+ [/^vp0?9/, "VP9"],
7
+ [/^av01/, "AV1"]
8
+ ];
9
+ /** Dolby Vision, in the codec itself or as a layer over it. */
10
+ function dolbyVision(rendition) {
11
+ var _rendition$codecs, _rendition$supplement;
12
+ const codecs = `${(_rendition$codecs = rendition.codecs) !== null && _rendition$codecs !== void 0 ? _rendition$codecs : ""},${(_rendition$supplement = rendition.supplementalCodecs) !== null && _rendition$supplement !== void 0 ? _rendition$supplement : ""}`;
13
+ return /(^|[,/])\s*(dvh1|dvhe|dav1|dva1)/.test(codecs);
14
+ }
15
+ /** 0 for SDR, 1 for HDR, 2 for Dolby Vision: the sort order and the word. */
16
+ function range(rendition) {
17
+ if (dolbyVision(rendition)) return 2;
18
+ return rendition.videoRange === "PQ" || rendition.videoRange === "HLG" ? 1 : 0;
19
+ }
20
+ function rate(rendition) {
21
+ var _rendition$frameRate;
22
+ return Math.round((_rendition$frameRate = rendition.frameRate) !== null && _rendition$frameRate !== void 0 ? _rendition$frameRate : 0);
23
+ }
24
+ /** The average bitrate where the manifest gives one: closer to what plays. */
25
+ function bitrate(rendition) {
26
+ var _rendition$averageBit;
27
+ const bps = (_rendition$averageBit = rendition.averageBitrate) !== null && _rendition$averageBit !== void 0 ? _rendition$averageBit : rendition.bitrate;
28
+ return bps < 1e6 ? `${Math.round(bps / 1e3)} kbps` : `${(bps / 1e6).toFixed(1)} Mbps`;
29
+ }
30
+ function codec(rendition) {
31
+ var _rendition$codecs2, _CODECS$find;
32
+ const codecs = (_rendition$codecs2 = rendition.codecs) !== null && _rendition$codecs2 !== void 0 ? _rendition$codecs2 : "";
33
+ return (_CODECS$find = CODECS.find(([pattern]) => pattern.test(codecs))) === null || _CODECS$find === void 0 ? void 0 : _CODECS$find[1];
34
+ }
35
+ /** The label of every rendition, best first, twins included. */
36
+ function qualityLabels(renditions) {
37
+ const sorted = [...renditions].sort((a, b) => {
38
+ var _b$height, _a$height, _b$averageBitrate, _a$averageBitrate;
39
+ return ((_b$height = b.height) !== null && _b$height !== void 0 ? _b$height : 0) - ((_a$height = a.height) !== null && _a$height !== void 0 ? _a$height : 0) || rate(b) - rate(a) || range(b) - range(a) || ((_b$averageBitrate = b.averageBitrate) !== null && _b$averageBitrate !== void 0 ? _b$averageBitrate : b.bitrate) - ((_a$averageBitrate = a.averageBitrate) !== null && _a$averageBitrate !== void 0 ? _a$averageBitrate : a.bitrate);
40
+ });
41
+ const bases = sorted.map((r) => {
42
+ return `${r.height === void 0 ? bitrate(r) : `${r.height}p`}${rate(r) > 30 ? String(rate(r)) : ""}${[
43
+ "",
44
+ " HDR",
45
+ " Dolby Vision"
46
+ ][range(r)]}`;
47
+ });
48
+ let labels = bases;
49
+ for (const part of [bitrate, codec]) labels = labels.map((label, i) => {
50
+ const twin = labels.some((other, j) => j !== i && other === label);
51
+ const extra = part(sorted[i]);
52
+ return twin && extra !== void 0 && !label.endsWith(extra) ? `${label} · ${extra}` : label;
53
+ });
54
+ labels = labels.map((label, i) => bases.every((base, j) => base !== bases[i] || labels[j] === label) ? bases[i] : label);
55
+ return sorted.map((r, i) => [r.id, labels[i]]);
56
+ }
57
+ /** The label of each rendition, best first, one entry per distinct label. */
58
+ function qualityNames(renditions) {
59
+ const seen = /* @__PURE__ */ new Set();
60
+ return qualityLabels(renditions).filter(([, label]) => {
61
+ if (seen.has(label)) return false;
62
+ seen.add(label);
63
+ return true;
64
+ });
65
+ }
66
+ //#endregion
67
+ export { qualityLabels, qualityNames };
@@ -0,0 +1,57 @@
1
+ //#region src/controls/track-names.ts
2
+ /**
3
+ * The language's name in `locale`, with a capital first letter as the
4
+ * browsers' own menus show it: French gives "anglais", the menu shows
5
+ * "Anglais". Undefined without `Intl.DisplayNames` (Chromium 81, so not on
6
+ * Tizen 6 or webOS 6), for a code it does not know, and for a locale or a
7
+ * code it rejects.
8
+ */
9
+ function languageName(lang, locale) {
10
+ try {
11
+ const name = new Intl.DisplayNames(locale, {
12
+ type: "language",
13
+ fallback: "none"
14
+ }).of(lang);
15
+ return name.charAt(0).toUpperCase() + name.slice(1);
16
+ } catch (_unused) {
17
+ return;
18
+ }
19
+ }
20
+ const LAYOUTS = {
21
+ 1: "Mono",
22
+ 2: "Stereo",
23
+ 6: "5.1",
24
+ 8: "7.1"
25
+ };
26
+ /**
27
+ * The audio layout of an HLS CHANNELS value (RFC 8216bis §4.4.6.1): "2"
28
+ * reads Stereo, "6" reads 5.1, a "/JOC" suffix reads Dolby Atmos. Any
29
+ * other count reads as written.
30
+ */
31
+ function layout(channels) {
32
+ return channels && (/\/JOC$/.test(channels) ? "Dolby Atmos" : LAYOUTS[channels] || channels);
33
+ }
34
+ /**
35
+ * The entries as menu items. Each reads as its name, else its language's
36
+ * name, else its language code, else `word` ("Track") and its place. An
37
+ * entry that would read like another, badges included, takes in
38
+ * parentheses its first hint that differs from the other's, and at last
39
+ * its place.
40
+ */
41
+ function trackNames(entries, word, locale = document.documentElement.lang || navigator.language) {
42
+ let texts = entries.map(([, name, lang], index) => name || lang && (languageName(lang, locale) || lang) || `${word || "Track"} ${index + 1}`);
43
+ for (let round = 0; round < 3; round++) {
44
+ const keys = texts.map((text, index) => text + entries[index][3]);
45
+ const hint = (index) => round < 2 ? entries[index][4][round] : String(index + 1);
46
+ texts = texts.map((text, index) => hint(index) && keys.some((key, other) => other !== index && key === keys[index] && hint(other) !== hint(index)) ? `${text} (${hint(index)})` : text);
47
+ }
48
+ return entries.map(([value, , , badges], index) => [
49
+ value,
50
+ texts[index],
51
+ void 0,
52
+ void 0,
53
+ badges
54
+ ]);
55
+ }
56
+ //#endregion
57
+ export { languageName, layout, trackNames };
@@ -1,12 +1,19 @@
1
+ import { createMarkers } from "./controls/markers.js";
1
2
  import { namespaces } from "./namespaces.js";
2
- import _asyncToGenerator from "./_virtual/_@oxc-project_runtime@0.150.0/helpers/esm/asyncToGenerator.js";
3
+ import _asyncToGenerator from "./_virtual/_@oxc-project_runtime@0.152.0/helpers/esm/asyncToGenerator.js";
3
4
  import { drmGuard, resolvePreset } from "./presets.js";
4
5
  import { STYLE } from "./style.js";
5
6
  import { errorSurface } from "./surface.js";
6
7
  import { PLAYER } from "./tags.js";
7
- import _objectSpread2 from "./_virtual/_@oxc-project_runtime@0.150.0/helpers/esm/objectSpread2.js";
8
- import { createPlayer, inferType, matteboxHandler, nativeHandler } from "@mattebox/player-core";
8
+ import _objectSpread2 from "./_virtual/_@oxc-project_runtime@0.152.0/helpers/esm/objectSpread2.js";
9
+ import { createPlayer, inferType, matteboxHandler, nativeHandler, preferNativeHls } from "@mattebox/player-core";
9
10
  //#region src/element.ts
11
+ /**
12
+ * `off` keeps HLS on the engine in Safari. Anything else lets Safari play
13
+ * HLS itself: FairPlay, AirPlay, Low-Latency HLS and the platform's
14
+ * fullscreen then work as Apple built them. Changing it reloads.
15
+ */
16
+ const NATIVE_HLS = "native-hls";
10
17
  /** Attributes forwarded onto the video as attributes, never as properties. */
11
18
  const FORWARDED = [
12
19
  "autoplay",
@@ -14,13 +21,17 @@ const FORWARDED = [
14
21
  "poster",
15
22
  "crossorigin"
16
23
  ];
24
+ const LICENSE_URL = "license-url";
25
+ const CERTIFICATE_URL = "certificate-url";
17
26
  /** Everything else the element watches. Changing any of them reloads. */
18
27
  const OWN = [
19
28
  "src",
20
29
  "type",
21
30
  "preset",
22
- "license-url",
23
- "thumbnails"
31
+ LICENSE_URL,
32
+ CERTIFICATE_URL,
33
+ "thumbnails",
34
+ NATIVE_HLS
24
35
  ];
25
36
  /**
26
37
  * The chapters file URL. A session with the engine's `chapters` namespace
@@ -46,11 +57,33 @@ const CONTROLS = "controls";
46
57
  * control the page placed shows. Changing it never reloads.
47
58
  */
48
59
  const PLAYSINLINE = "playsinline";
60
+ /**
61
+ * The size tier the controls draw for: `compact` on a small player, such
62
+ * as a phone held upright, `wide` on a large one, such as a TV or
63
+ * fullscreen, and `regular` between. The element measures itself and sets
64
+ * the attribute, so a stylesheet matches `mattebox-player[layout="compact"]`.
65
+ * A page that writes one of the three values fixes it, and removing the
66
+ * attribute hands it back. The player's own box decides, not the screen:
67
+ * a small embed on a desktop is compact, and a phone in fullscreen is not.
68
+ */
69
+ const LAYOUT = "layout";
70
+ const LAYOUTS = [
71
+ "compact",
72
+ "regular",
73
+ "wide"
74
+ ];
75
+ /** Under either, the player is compact. */
76
+ const COMPACT_WIDTH = 480;
77
+ const COMPACT_HEIGHT = 320;
78
+ /** From both, the player is wide. */
79
+ const WIDE_WIDTH = 1280;
80
+ const WIDE_HEIGHT = 700;
49
81
  /** The video's state, as attributes on the element, read on the events the video fires for it. */
50
82
  const STATE_EVENTS = [
51
83
  "play",
52
84
  "pause",
53
85
  "ended",
86
+ "seeked",
54
87
  "emptied",
55
88
  "volumechange",
56
89
  "loadedmetadata",
@@ -60,6 +93,8 @@ const STATE_EVENTS = [
60
93
  "stalled",
61
94
  "canplay"
62
95
  ];
96
+ /** The stages that register a key system, which a native session's DRM offers. */
97
+ const KEY_SYSTEMS = ["eme-fairplay", "eme-cenc"];
63
98
  /** Every stage the engine ships. A narrower preset is optimization. */
64
99
  const DEFAULT_PRESET = "full";
65
100
  /**
@@ -81,7 +116,8 @@ var MatteboxPlayerElement = class MatteboxPlayerElement extends HTMLElement {
81
116
  ...OWN,
82
117
  CONTROLS,
83
118
  CHAPTERS,
84
- PLAYSINLINE
119
+ PLAYSINLINE,
120
+ LAYOUT
85
121
  ];
86
122
  }
87
123
  /**
@@ -96,14 +132,18 @@ var MatteboxPlayerElement = class MatteboxPlayerElement extends HTMLElement {
96
132
  }
97
133
  constructor(options) {
98
134
  super();
99
- this.options = options !== null && options !== void 0 ? options : defaults;
135
+ this.own = options !== null && options !== void 0 ? options : null;
100
136
  this.focusable = false;
101
137
  this.track = null;
102
138
  this.given = null;
139
+ this.list = null;
140
+ this.tried = "";
103
141
  this.core = null;
104
142
  this.current = null;
105
143
  this.failure = null;
106
144
  this.reflecting = false;
145
+ this.sizes = null;
146
+ this.fixed = false;
107
147
  this.started = false;
108
148
  this.waiting = false;
109
149
  this.offs = [];
@@ -119,9 +159,14 @@ var MatteboxPlayerElement = class MatteboxPlayerElement extends HTMLElement {
119
159
  this.media.addEventListener("playing", () => {
120
160
  this.failure = null;
121
161
  });
162
+ this.marks = createMarkers(this.media, (name, detail) => this.emit(name, detail));
122
163
  this.stage = document.createElement("div");
123
164
  this.stage.setAttribute("part", "stage");
124
- this.stage.append(document.createElement("slot"));
165
+ const slot = document.createElement("slot");
166
+ slot.addEventListener("slotchange", () => {
167
+ if (this.current === null && JSON.stringify(this.wanted()) !== this.tried) this.reload();
168
+ });
169
+ this.stage.append(slot);
125
170
  this.surface = errorSurface(() => {
126
171
  this.reload();
127
172
  });
@@ -134,6 +179,23 @@ var MatteboxPlayerElement = class MatteboxPlayerElement extends HTMLElement {
134
179
  get video() {
135
180
  return this.media;
136
181
  }
182
+ /** The ranges the page named in this source: credits to skip, parts that must not play. */
183
+ get markers() {
184
+ return this.marks;
185
+ }
186
+ /**
187
+ * The sources to try in order, set from script. The `src` attribute wins
188
+ * over them, and they win over the `<source>` children. Setting them
189
+ * loads, and null returns to the children.
190
+ */
191
+ get sources() {
192
+ return this.list;
193
+ }
194
+ set sources(value) {
195
+ this.list = value;
196
+ if (this.marks.list.length > 0) this.marks.set([]);
197
+ this.reload();
198
+ }
137
199
  /** The core, or null until the first source has been set up: resolving a preset is asynchronous. */
138
200
  get player() {
139
201
  return this.core;
@@ -156,8 +218,28 @@ var MatteboxPlayerElement = class MatteboxPlayerElement extends HTMLElement {
156
218
  if (this.media.parentNode !== this) this.append(this.media);
157
219
  for (const name of FORWARDED) this.forward(name);
158
220
  this.reflect();
221
+ if (typeof ResizeObserver !== "undefined") {
222
+ this.sizes = new ResizeObserver(() => {
223
+ this.measure();
224
+ });
225
+ this.sizes.observe(this);
226
+ }
227
+ this.measure();
159
228
  this.reload();
160
229
  }
230
+ /** Sets `layout` from the element's box, unless the page fixed it. */
231
+ measure() {
232
+ if (this.fixed) return;
233
+ const width = this.clientWidth;
234
+ const height = this.clientHeight;
235
+ if (width === 0 || height === 0) return;
236
+ const layout = width < COMPACT_WIDTH || height < COMPACT_HEIGHT ? "compact" : width >= WIDE_WIDTH && height >= WIDE_HEIGHT ? "wide" : "regular";
237
+ if (this.getAttribute(LAYOUT) === layout) return;
238
+ this.reflecting = true;
239
+ this.setAttribute(LAYOUT, layout);
240
+ this.reflecting = false;
241
+ this.emit("layoutchange", layout);
242
+ }
161
243
  /** The two states the video does not hold: whether it has played once, and whether it waits for data. */
162
244
  note(event) {
163
245
  if (event === "playing") {
@@ -199,6 +281,9 @@ var MatteboxPlayerElement = class MatteboxPlayerElement extends HTMLElement {
199
281
  return (type === null || type === void 0 ? void 0 : type.startsWith("audio/")) === true;
200
282
  }
201
283
  disconnectedCallback() {
284
+ var _this$sizes;
285
+ (_this$sizes = this.sizes) === null || _this$sizes === void 0 || _this$sizes.disconnect();
286
+ this.sizes = null;
202
287
  this.enqueue(() => this.teardown());
203
288
  }
204
289
  attributeChangedCallback(name) {
@@ -214,12 +299,21 @@ var MatteboxPlayerElement = class MatteboxPlayerElement extends HTMLElement {
214
299
  this.chapters();
215
300
  return;
216
301
  }
302
+ if (name === LAYOUT) {
303
+ if (this.reflecting) return;
304
+ const value = this.getAttribute(LAYOUT);
305
+ this.fixed = value !== null && LAYOUTS.includes(value);
306
+ if (this.fixed) this.emit("layoutchange", value);
307
+ else this.measure();
308
+ return;
309
+ }
217
310
  if (name === PLAYSINLINE) {
218
311
  this.media.toggleAttribute(PLAYSINLINE, this.getAttribute(PLAYSINLINE) !== "false");
219
312
  return;
220
313
  }
221
314
  if (name === "preset") this.enqueue(() => this.discard());
222
315
  if (name === "src" || name === "type") this.toggleAttribute("audio", this.audio());
316
+ if (name === "src" && this.marks.list.length > 0) this.marks.set([]);
223
317
  this.reload();
224
318
  }
225
319
  /**
@@ -313,32 +407,56 @@ var MatteboxPlayerElement = class MatteboxPlayerElement extends HTMLElement {
313
407
  return _asyncToGenerator(function* () {
314
408
  _this.unmount();
315
409
  if (!_this.isConnected) return;
316
- const url = _this.getAttribute("src");
317
- if (url === null) return;
410
+ const sources = _this.wanted();
411
+ _this.tried = JSON.stringify(sources);
412
+ if (sources.length === 0) return;
318
413
  const core = yield _this.ensure();
319
- const type = _this.getAttribute("type");
320
- const source = type === null ? { url } : {
321
- url,
322
- type
323
- };
324
414
  _this.failure = null;
325
415
  _this.surface.clear();
326
- let session;
327
416
  try {
328
- session = yield core.load(source);
417
+ yield core.load(sources);
329
418
  } catch (_unused) {
330
419
  return;
331
420
  }
332
- if (!_this.isConnected) {
333
- yield core.unload();
334
- return;
335
- }
336
- _this.current = session;
337
- _this.given = null;
338
- _this.configure(session);
339
- _this.chapters();
421
+ if (!_this.isConnected) yield core.unload();
340
422
  })();
341
423
  }
424
+ /**
425
+ * What to load, ranked as a video ranks them: the `src` attribute, then
426
+ * the `sources` property, then the `<source>` children in their order.
427
+ * A child carries its own `license-url` and `certificate-url`.
428
+ */
429
+ wanted() {
430
+ const url = this.getAttribute("src");
431
+ if (url !== null) {
432
+ const type = this.getAttribute("type");
433
+ return [type === null ? { url } : {
434
+ url,
435
+ type
436
+ }];
437
+ }
438
+ const eme = drmGuard().without === void 0;
439
+ if (this.list !== null) return eme ? this.list : this.list.map(({ url, type }) => type === void 0 ? { url } : {
440
+ url,
441
+ type
442
+ });
443
+ const found = [];
444
+ for (const child of Array.from(this.children)) {
445
+ const src = child.localName === "source" ? child.getAttribute("src") : null;
446
+ if (src === null) continue;
447
+ const type = child.getAttribute("type");
448
+ const license = child.getAttribute(LICENSE_URL);
449
+ const certificate = child.getAttribute(CERTIFICATE_URL);
450
+ found.push(_objectSpread2(_objectSpread2({ url: src }, type === null ? {} : { type }), !eme || license === null && certificate === null ? {} : { drm: _objectSpread2(_objectSpread2({}, license === null ? {} : { licenseUrl: license }), certificate === null ? {} : { certificateUrl: certificate }) }));
451
+ }
452
+ return found;
453
+ }
454
+ /** Sets a session up. It runs for every session, the one that replaces a failed source included. */
455
+ adopt(session) {
456
+ this.given = null;
457
+ this.configure(session);
458
+ this.chapters();
459
+ }
342
460
  ensure() {
343
461
  var _this2 = this;
344
462
  return _asyncToGenerator(function* () {
@@ -346,6 +464,7 @@ var MatteboxPlayerElement = class MatteboxPlayerElement extends HTMLElement {
346
464
  const core = createPlayer(_this2.media, { handlers: yield _this2.chain() });
347
465
  _this2.offs.push(core.on("sourcechange", (session) => {
348
466
  _this2.current = session;
467
+ if (session !== null && _this2.isConnected) _this2.adopt(session);
349
468
  _this2.emit("sourcechange", session);
350
469
  }), core.on("error", (error) => {
351
470
  _this2.report(error);
@@ -354,13 +473,29 @@ var MatteboxPlayerElement = class MatteboxPlayerElement extends HTMLElement {
354
473
  return core;
355
474
  })();
356
475
  }
476
+ /**
477
+ * Read at each use, not once in the constructor. Importing the package
478
+ * registers the element, and the parser's elements are constructed then,
479
+ * before the page's own `define()` call can run.
480
+ */
481
+ get options() {
482
+ var _this$own;
483
+ return (_this$own = this.own) !== null && _this$own !== void 0 ? _this$own : defaults;
484
+ }
357
485
  chain() {
358
486
  var _this3 = this;
359
487
  return _asyncToGenerator(function* () {
360
488
  var _this$getAttribute2;
361
489
  if (_this3.options.handlers !== void 0) return _this3.options.handlers;
362
490
  const config = _this3.options.config === void 0 ? {} : { config: _this3.options.config };
363
- if (_this3.options.stages !== void 0) return [matteboxHandler(_objectSpread2({ stages: _this3.options.stages }, config)), nativeHandler()];
491
+ const native = (stages) => nativeHandler({
492
+ prefer: (source, env) => _this3.getAttribute(NATIVE_HLS) !== "off" && preferNativeHls(source, env),
493
+ drm: () => _this3.drm(stages)
494
+ });
495
+ if (_this3.options.stages !== void 0) return [matteboxHandler(_objectSpread2({ stages: _this3.options.stages }, config)), native(() => {
496
+ var _this$options$stages;
497
+ return (_this$options$stages = _this3.options.stages) !== null && _this$options$stages !== void 0 ? _this$options$stages : [];
498
+ })];
364
499
  const name = (_this$getAttribute2 = _this3.getAttribute("preset")) !== null && _this$getAttribute2 !== void 0 ? _this$getAttribute2 : DEFAULT_PRESET;
365
500
  const preset = yield resolvePreset(name);
366
501
  if (preset === null) {
@@ -374,16 +509,37 @@ var MatteboxPlayerElement = class MatteboxPlayerElement extends HTMLElement {
374
509
  });
375
510
  return [nativeHandler()];
376
511
  }
377
- return [matteboxHandler(_objectSpread2(_objectSpread2({ preset }, config), drmGuard())), nativeHandler()];
512
+ return [matteboxHandler(_objectSpread2(_objectSpread2({ preset }, config), drmGuard())), native(() => preset.stages())];
378
513
  })();
379
514
  }
515
+ /**
516
+ * The DRM a native session attaches: the page's `drm` option, the
517
+ * key-system stages of the engine's stack, and the `license-url`
518
+ * attribute. None on a browser without EME, as for the engine.
519
+ */
520
+ drm(stages) {
521
+ if (drmGuard().without !== void 0) return void 0;
522
+ const keySystems = stages().filter((stage) => KEY_SYSTEMS.includes(stage.name));
523
+ const license = this.getAttribute(LICENSE_URL);
524
+ const certificate = this.getAttribute(CERTIFICATE_URL);
525
+ const page = this.options.drm;
526
+ if (keySystems.length === 0 && license === null && page === void 0) return void 0;
527
+ return _objectSpread2(_objectSpread2(_objectSpread2({ keySystems }, page), license === null ? {} : { licenseUrl: license }), certificate === null ? {} : { certificateUrl: certificate });
528
+ }
380
529
  /** What the attributes ask of the session's namespaces, once there is one. */
381
530
  configure(session) {
531
+ var _optional$drm;
382
532
  const engine = session.engine;
383
533
  if (engine === null) return;
384
534
  const optional = namespaces(engine);
385
- const license = this.getAttribute("license-url");
386
- if (license !== null && optional.drm !== void 0) optional.drm.setLicenseUrl(license);
535
+ const own = session.source.drm;
536
+ const license = this.getAttribute(LICENSE_URL);
537
+ if (license !== null && (own === null || own === void 0 ? void 0 : own.licenseUrl) === void 0) (_optional$drm = optional.drm) === null || _optional$drm === void 0 || _optional$drm.setLicenseUrl(license);
538
+ const certificate = this.getAttribute(CERTIFICATE_URL);
539
+ if (certificate !== null && (own === null || own === void 0 ? void 0 : own.certificateUrl) === void 0) {
540
+ var _optional$drm2;
541
+ (_optional$drm2 = optional.drm) === null || _optional$drm2 === void 0 || _optional$drm2.setCertificateUrl(certificate);
542
+ }
387
543
  const track = this.getAttribute("thumbnails");
388
544
  if (track !== null && optional.thumbnails !== void 0) optional.thumbnails.load(track).catch(() => void 0);
389
545
  }
@@ -1,19 +1,34 @@
1
+ import { icon } from "../controls/icons.js";
1
2
  import { show } from "./shared.js";
3
+ import { nativeTracks, onNativeTracks } from "../controls/native-tracks.js";
4
+ import { layout, trackNames } from "../controls/track-names.js";
2
5
  import { MenuElement, single } from "./menu-element.js";
6
+ import { isAudioDescription, isOriginal } from "mattebox";
3
7
  //#region src/elements/audio-menu.ts
4
- /** The language, then the role, then the id: whichever the manifest gave. */
5
- function trackLabel(track) {
6
- const parts = [track.lang, track.role].filter((part) => part !== void 0);
7
- return parts.length === 0 ? track.id : parts.join(" · ");
8
+ /** A track's channel layout, from its first rendition: `audioObjects` `JOC` is Dolby Atmos. */
9
+ function channels(track) {
10
+ const first = track.renditions[0];
11
+ return (first === null || first === void 0 ? void 0 : first.channels) === void 0 ? void 0 : layout(`${first.channels}${first.audioObjects ? `/${first.audioObjects}` : ""}`);
8
12
  }
9
- /** The tracks of one content type as menu items, with the active one's id. */
10
- function trackItems(engine, contentType) {
11
- var _tracks$active$id, _tracks$active;
13
+ /**
14
+ * The tracks of one content type that the engine can play and `listed`
15
+ * keeps, as entries with their badges, and the active one's id when it
16
+ * is listed. A track the browser cannot decode, or that no stage plays,
17
+ * stays out (guide chapter 06).
18
+ */
19
+ function trackItems(engine, contentType, badges = () => [], listed = () => true) {
20
+ var _tracks$active;
12
21
  const tracks = engine.tracks;
13
- const available = tracks.available.filter((track) => track.contentType === contentType);
14
- const items = [];
15
- for (const track of available) items.push([track.id, trackLabel(track)]);
16
- return [items, (_tracks$active$id = (_tracks$active = tracks.active(contentType)) === null || _tracks$active === void 0 ? void 0 : _tracks$active.id) !== null && _tracks$active$id !== void 0 ? _tracks$active$id : null];
22
+ const available = tracks.available.filter((track) => track.contentType === contentType && tracks.selectable(track.id) && listed(track));
23
+ const entries = available.map((track) => [
24
+ track.id,
25
+ track.name,
26
+ track.lang,
27
+ badges(track),
28
+ [channels(track), track.role === "main" ? void 0 : track.role]
29
+ ]);
30
+ const active = (_tracks$active = tracks.active(contentType)) === null || _tracks$active === void 0 ? void 0 : _tracks$active.id;
31
+ return [entries, available.some((track) => track.id === active) ? active : null];
17
32
  }
18
33
  /** Subscribes `tick` to the track events, and answers the unsubscribe. */
19
34
  function onTracks(engine, tick) {
@@ -24,10 +39,16 @@ function onTracks(engine, tick) {
24
39
  }
25
40
  var MbxAudioMenu = class extends MenuElement {
26
41
  static get observedAttributes() {
27
- return ["label", "label-back"];
42
+ return [
43
+ "label",
44
+ "label-back",
45
+ "label-ad",
46
+ "label-original",
47
+ "label-track"
48
+ ];
28
49
  }
29
50
  constructor() {
30
- super(...single("music"));
51
+ super(...single("audio"));
31
52
  this.engine = null;
32
53
  show(this.slots, "default");
33
54
  }
@@ -38,26 +59,51 @@ var MbxAudioMenu = class extends MenuElement {
38
59
  this.follow(player, (engine) => {
39
60
  this.engine = engine;
40
61
  this.render();
41
- if (engine === null) return void 0;
42
- return onTracks(engine, () => {
62
+ const tick = () => {
43
63
  this.render();
44
- });
64
+ };
65
+ return engine === null ? onNativeTracks(player.video, "audioTracks", tick) : onTracks(engine, tick);
45
66
  });
46
67
  }
68
+ /** The native session's audio tracks as entries, and the enabled one's. */
69
+ native(tracks) {
70
+ var _this$getAttribute;
71
+ const ad = (_this$getAttribute = this.getAttribute("label-ad")) !== null && _this$getAttribute !== void 0 ? _this$getAttribute : "AD";
72
+ const entries = tracks.map((track, index) => [
73
+ String(index),
74
+ track.label,
75
+ track.language,
76
+ track.kind === "description" || track.kind === "main-desc" ? [ad] : [],
77
+ []
78
+ ]);
79
+ const on = tracks.findIndex((track) => track.enabled);
80
+ return [entries, on === -1 ? null : String(on)];
81
+ }
47
82
  render() {
83
+ var _this$getAttribute4;
48
84
  super.render();
49
85
  const engine = this.engine;
50
- if (engine === null) {
86
+ const player = this.player;
87
+ const tracks = engine === null && player !== null ? nativeTracks(player, "audioTracks") : void 0;
88
+ if (engine === null && tracks === void 0) {
51
89
  this.hidden = true;
52
90
  return;
53
91
  }
54
- const [items, active] = trackItems(engine, "audio");
92
+ const [entries, active] = tracks !== void 0 ? this.native(tracks) : trackItems(engine, "audio", (track) => {
93
+ var _this$getAttribute2, _this$getAttribute3;
94
+ return [...isAudioDescription(track) ? [(_this$getAttribute2 = this.getAttribute("label-ad")) !== null && _this$getAttribute2 !== void 0 ? _this$getAttribute2 : "AD"] : [], ...isOriginal(track) ? [(_this$getAttribute3 = this.getAttribute("label-original")) !== null && _this$getAttribute3 !== void 0 ? _this$getAttribute3 : "Original"] : []];
95
+ });
96
+ const items = trackNames(entries, this.getAttribute("label-track"));
55
97
  this.menu.fill([{
56
98
  name: "track",
57
99
  items,
100
+ badgeGlyphs: { [(_this$getAttribute4 = this.getAttribute("label-ad")) !== null && _this$getAttribute4 !== void 0 ? _this$getAttribute4 : "AD"]: () => icon("audio-description") },
58
101
  value: active !== null && active !== void 0 ? active : "",
59
102
  onSelect: (value) => {
60
- engine.tracks.select(value);
103
+ if (tracks !== void 0) tracks.forEach((track, index) => {
104
+ track.enabled = String(index) === value;
105
+ });
106
+ else engine === null || engine === void 0 || engine.tracks.select(value);
61
107
  this.render();
62
108
  }
63
109
  }]);
@@ -65,4 +111,4 @@ var MbxAudioMenu = class extends MenuElement {
65
111
  }
66
112
  };
67
113
  //#endregion
68
- export { MbxAudioMenu, onTracks, trackItems, trackLabel };
114
+ export { MbxAudioMenu, onTracks, trackItems };
@@ -18,6 +18,16 @@ var Component = class extends HTMLElement {
18
18
  customElements.upgrade(found);
19
19
  if (!("video" in found)) return;
20
20
  this.player = found;
21
+ const tier = () => {
22
+ const layout = found.getAttribute("layout");
23
+ if (layout === null) this.removeAttribute("layout");
24
+ else this.setAttribute("layout", layout);
25
+ };
26
+ this.listen(found, ["layoutchange"], () => {
27
+ tier();
28
+ this.render();
29
+ });
30
+ tier();
21
31
  this.attach(this.player);
22
32
  };
23
33
  if ("video" in found) attach();