@mattebox/player 0.6.0 → 0.7.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 (241) hide show
  1. package/dist/cdn/mattebox-player.min.js +3 -435
  2. package/dist/controls/airplay.js +28 -44
  3. package/dist/controls/airplay.js.map +1 -1
  4. package/dist/controls/chapters.d.ts +22 -11
  5. package/dist/controls/chapters.d.ts.map +1 -1
  6. package/dist/controls/chapters.js +87 -73
  7. package/dist/controls/chapters.js.map +1 -1
  8. package/dist/controls/cues.d.ts +0 -22
  9. package/dist/controls/cues.d.ts.map +1 -1
  10. package/dist/controls/cues.js +161 -191
  11. package/dist/controls/cues.js.map +1 -1
  12. package/dist/controls/fullscreen.js +52 -70
  13. package/dist/controls/fullscreen.js.map +1 -1
  14. package/dist/controls/icons.d.ts +1 -1
  15. package/dist/controls/icons.d.ts.map +1 -1
  16. package/dist/controls/icons.js +46 -70
  17. package/dist/controls/icons.js.map +1 -1
  18. package/dist/controls/menu.d.ts +7 -4
  19. package/dist/controls/menu.d.ts.map +1 -1
  20. package/dist/controls/menu.js +241 -223
  21. package/dist/controls/menu.js.map +1 -1
  22. package/dist/controls/pip.js +35 -49
  23. package/dist/controls/pip.js.map +1 -1
  24. package/dist/controls/ranges.js +42 -37
  25. package/dist/controls/ranges.js.map +1 -1
  26. package/dist/controls/session.d.ts +6 -1
  27. package/dist/controls/session.d.ts.map +1 -1
  28. package/dist/controls/session.js +41 -26
  29. package/dist/controls/session.js.map +1 -1
  30. package/dist/controls/slider.d.ts +2 -2
  31. package/dist/controls/slider.d.ts.map +1 -1
  32. package/dist/controls/slider.js +151 -159
  33. package/dist/controls/slider.js.map +1 -1
  34. package/dist/controls/time.js +17 -17
  35. package/dist/controls/time.js.map +1 -1
  36. package/dist/dom.d.ts +6 -0
  37. package/dist/dom.d.ts.map +1 -1
  38. package/dist/dom.js +28 -19
  39. package/dist/dom.js.map +1 -1
  40. package/dist/element-entry.js +9 -6
  41. package/dist/element-entry.js.map +1 -1
  42. package/dist/element.d.ts +4 -2
  43. package/dist/element.d.ts.map +1 -1
  44. package/dist/element.js +384 -443
  45. package/dist/element.js.map +1 -1
  46. package/dist/elements/airplay-button.js +51 -53
  47. package/dist/elements/airplay-button.js.map +1 -1
  48. package/dist/elements/audio-menu.js +62 -63
  49. package/dist/elements/audio-menu.js.map +1 -1
  50. package/dist/elements/chapters-menu.d.ts.map +1 -1
  51. package/dist/elements/chapters-menu.js +75 -65
  52. package/dist/elements/chapters-menu.js.map +1 -1
  53. package/dist/elements/component.d.ts +1 -1
  54. package/dist/elements/component.d.ts.map +1 -1
  55. package/dist/elements/component.js +86 -89
  56. package/dist/elements/component.js.map +1 -1
  57. package/dist/elements/control-bar.d.ts +18 -5
  58. package/dist/elements/control-bar.d.ts.map +1 -1
  59. package/dist/elements/control-bar.js +282 -319
  60. package/dist/elements/control-bar.js.map +1 -1
  61. package/dist/elements/current-time.js +26 -25
  62. package/dist/elements/current-time.js.map +1 -1
  63. package/dist/elements/drm-badge.d.ts.map +1 -1
  64. package/dist/elements/drm-badge.js +107 -141
  65. package/dist/elements/drm-badge.js.map +1 -1
  66. package/dist/elements/duration.d.ts.map +1 -1
  67. package/dist/elements/duration.js +24 -21
  68. package/dist/elements/duration.js.map +1 -1
  69. package/dist/elements/error-screen.d.ts.map +1 -1
  70. package/dist/elements/error-screen.js +68 -102
  71. package/dist/elements/error-screen.js.map +1 -1
  72. package/dist/elements/fullscreen-button.js +48 -54
  73. package/dist/elements/fullscreen-button.js.map +1 -1
  74. package/dist/elements/live-button.d.ts.map +1 -1
  75. package/dist/elements/live-button.js +67 -94
  76. package/dist/elements/live-button.js.map +1 -1
  77. package/dist/elements/menu-element.js +31 -28
  78. package/dist/elements/menu-element.js.map +1 -1
  79. package/dist/elements/mute-button.js +58 -59
  80. package/dist/elements/mute-button.js.map +1 -1
  81. package/dist/elements/pip-button.js +54 -54
  82. package/dist/elements/pip-button.js.map +1 -1
  83. package/dist/elements/play-button.d.ts.map +1 -1
  84. package/dist/elements/play-button.js +73 -58
  85. package/dist/elements/play-button.js.map +1 -1
  86. package/dist/elements/quality-menu.js +66 -69
  87. package/dist/elements/quality-menu.js.map +1 -1
  88. package/dist/elements/remaining-time.d.ts.map +1 -1
  89. package/dist/elements/remaining-time.js +27 -24
  90. package/dist/elements/remaining-time.js.map +1 -1
  91. package/dist/elements/scan-button.d.ts +17 -0
  92. package/dist/elements/scan-button.d.ts.map +1 -0
  93. package/dist/elements/scan-button.js +117 -0
  94. package/dist/elements/scan-button.js.map +1 -0
  95. package/dist/elements/seek-bar.d.ts +93 -14
  96. package/dist/elements/seek-bar.d.ts.map +1 -1
  97. package/dist/elements/seek-bar.js +475 -335
  98. package/dist/elements/seek-bar.js.map +1 -1
  99. package/dist/elements/shared.d.ts +3 -11
  100. package/dist/elements/shared.d.ts.map +1 -1
  101. package/dist/elements/shared.js +44 -144
  102. package/dist/elements/shared.js.map +1 -1
  103. package/dist/elements/skip-button.d.ts.map +1 -1
  104. package/dist/elements/skip-button.js +54 -55
  105. package/dist/elements/skip-button.js.map +1 -1
  106. package/dist/elements/spacer.d.ts.map +1 -1
  107. package/dist/elements/spacer.js +12 -7
  108. package/dist/elements/spacer.js.map +1 -1
  109. package/dist/elements/speed-menu.js +60 -55
  110. package/dist/elements/speed-menu.js.map +1 -1
  111. package/dist/elements/spinner.d.ts.map +1 -1
  112. package/dist/elements/spinner.js +52 -78
  113. package/dist/elements/spinner.js.map +1 -1
  114. package/dist/elements/start-button.d.ts.map +1 -1
  115. package/dist/elements/start-button.js +107 -120
  116. package/dist/elements/start-button.js.map +1 -1
  117. package/dist/elements/subtitles-menu.js +95 -103
  118. package/dist/elements/subtitles-menu.js.map +1 -1
  119. package/dist/elements/time-element.d.ts.map +1 -1
  120. package/dist/elements/time-element.js +36 -28
  121. package/dist/elements/time-element.js.map +1 -1
  122. package/dist/elements/title.d.ts.map +1 -1
  123. package/dist/elements/title.js +105 -137
  124. package/dist/elements/title.js.map +1 -1
  125. package/dist/elements/volume-slider.d.ts.map +1 -1
  126. package/dist/elements/volume-slider.js +47 -45
  127. package/dist/elements/volume-slider.js.map +1 -1
  128. package/dist/elements/volume.d.ts.map +1 -1
  129. package/dist/elements/volume.js +42 -50
  130. package/dist/elements/volume.js.map +1 -1
  131. package/dist/entries/airplay-button.js +7 -7
  132. package/dist/entries/airplay-button.js.map +1 -1
  133. package/dist/entries/audio-menu.js +7 -6
  134. package/dist/entries/audio-menu.js.map +1 -1
  135. package/dist/entries/chapters-menu.js +7 -7
  136. package/dist/entries/chapters-menu.js.map +1 -1
  137. package/dist/entries/control-bar.js +7 -6
  138. package/dist/entries/control-bar.js.map +1 -1
  139. package/dist/entries/current-time.js +7 -6
  140. package/dist/entries/current-time.js.map +1 -1
  141. package/dist/entries/drm-badge.js +7 -6
  142. package/dist/entries/drm-badge.js.map +1 -1
  143. package/dist/entries/duration.js +7 -6
  144. package/dist/entries/duration.js.map +1 -1
  145. package/dist/entries/error-screen.js +7 -6
  146. package/dist/entries/error-screen.js.map +1 -1
  147. package/dist/entries/fullscreen-button.js +7 -6
  148. package/dist/entries/fullscreen-button.js.map +1 -1
  149. package/dist/entries/live-button.js +7 -6
  150. package/dist/entries/live-button.js.map +1 -1
  151. package/dist/entries/mute-button.js +7 -6
  152. package/dist/entries/mute-button.js.map +1 -1
  153. package/dist/entries/pip-button.js +7 -6
  154. package/dist/entries/pip-button.js.map +1 -1
  155. package/dist/entries/play-button.js +7 -6
  156. package/dist/entries/play-button.js.map +1 -1
  157. package/dist/entries/quality-menu.js +7 -6
  158. package/dist/entries/quality-menu.js.map +1 -1
  159. package/dist/entries/remaining-time.js +7 -7
  160. package/dist/entries/remaining-time.js.map +1 -1
  161. package/dist/entries/scan-button.d.ts +5 -0
  162. package/dist/entries/scan-button.d.ts.map +1 -0
  163. package/dist/entries/scan-button.js +9 -0
  164. package/dist/entries/scan-button.js.map +1 -0
  165. package/dist/entries/seek-bar.js +7 -6
  166. package/dist/entries/seek-bar.js.map +1 -1
  167. package/dist/entries/skip-button.js +7 -6
  168. package/dist/entries/skip-button.js.map +1 -1
  169. package/dist/entries/spacer.js +7 -6
  170. package/dist/entries/spacer.js.map +1 -1
  171. package/dist/entries/speed-menu.js +7 -6
  172. package/dist/entries/speed-menu.js.map +1 -1
  173. package/dist/entries/spinner.js +7 -7
  174. package/dist/entries/spinner.js.map +1 -1
  175. package/dist/entries/start-button.js +7 -6
  176. package/dist/entries/start-button.js.map +1 -1
  177. package/dist/entries/subtitles-menu.js +7 -6
  178. package/dist/entries/subtitles-menu.js.map +1 -1
  179. package/dist/entries/title.js +7 -7
  180. package/dist/entries/title.js.map +1 -1
  181. package/dist/entries/volume-slider.js +7 -6
  182. package/dist/entries/volume-slider.js.map +1 -1
  183. package/dist/entries/volume.js +9 -8
  184. package/dist/entries/volume.js.map +1 -1
  185. package/dist/es2015/controls/chapters.js +40 -7
  186. package/dist/es2015/controls/cues.js +1 -8
  187. package/dist/es2015/controls/icons.js +3 -1
  188. package/dist/es2015/controls/menu.js +27 -8
  189. package/dist/es2015/controls/session.js +16 -4
  190. package/dist/es2015/controls/slider.js +5 -4
  191. package/dist/es2015/dom.js +10 -1
  192. package/dist/es2015/element.js +23 -7
  193. package/dist/es2015/elements/chapters-menu.js +18 -10
  194. package/dist/es2015/elements/control-bar.js +41 -35
  195. package/dist/es2015/elements/drm-badge.js +1 -25
  196. package/dist/es2015/elements/duration.js +2 -1
  197. package/dist/es2015/elements/error-screen.js +1 -30
  198. package/dist/es2015/elements/live-button.js +1 -29
  199. package/dist/es2015/elements/menu-element.js +2 -2
  200. package/dist/es2015/elements/play-button.js +10 -3
  201. package/dist/es2015/elements/remaining-time.js +2 -1
  202. package/dist/es2015/elements/scan-button.js +118 -0
  203. package/dist/es2015/elements/seek-bar.js +264 -71
  204. package/dist/es2015/elements/shared.js +5 -106
  205. package/dist/es2015/elements/skip-button.js +2 -1
  206. package/dist/es2015/elements/spacer.js +2 -1
  207. package/dist/es2015/elements/spinner.js +1 -32
  208. package/dist/es2015/elements/start-button.js +8 -26
  209. package/dist/es2015/elements/time-element.js +1 -4
  210. package/dist/es2015/elements/title.js +1 -45
  211. package/dist/es2015/elements/volume-slider.js +2 -1
  212. package/dist/es2015/elements/volume.js +1 -11
  213. package/dist/es2015/entries/scan-button.js +7 -0
  214. package/dist/es2015/index.js +3 -1
  215. package/dist/es2015/style.js +1 -101
  216. package/dist/es2015/tags.js +2 -1
  217. package/dist/host.js +19 -17
  218. package/dist/host.js.map +1 -1
  219. package/dist/index.d.ts +3 -0
  220. package/dist/index.d.ts.map +1 -1
  221. package/dist/index.js +55 -27
  222. package/dist/labels.js +13 -9
  223. package/dist/labels.js.map +1 -1
  224. package/dist/namespaces.d.ts +9 -26
  225. package/dist/namespaces.d.ts.map +1 -1
  226. package/dist/namespaces.js +6 -2
  227. package/dist/namespaces.js.map +1 -1
  228. package/dist/presets.js +33 -28
  229. package/dist/presets.js.map +1 -1
  230. package/dist/style.d.ts +1 -20
  231. package/dist/style.d.ts.map +1 -1
  232. package/dist/style.js +4 -100
  233. package/dist/style.js.map +1 -1
  234. package/dist/surface.js +25 -21
  235. package/dist/surface.js.map +1 -1
  236. package/dist/tags.d.ts +1 -0
  237. package/dist/tags.d.ts.map +1 -1
  238. package/dist/tags.js +31 -26
  239. package/dist/tags.js.map +1 -1
  240. package/package.json +4 -4
  241. package/dist/index.js.map +0 -1
package/dist/element.js CHANGED
@@ -1,456 +1,397 @@
1
- /**
2
- * <mattebox-player>: a real <video> as a light-DOM child, so the integrator
3
- * can reach it, and the error surface in shadow DOM. The element never
4
- * forwards or wraps a media element member: play, pause, seek, volume and
5
- * the rest live on the video, where the browser, Picture-in-Picture and the
6
- * OS media session already find them.
7
- *
8
- * The controls are elements the page places inside, beside the video, and
9
- * the stage slots them over the picture. The element appends none of its
10
- * own: the composition is the page's in every mode, and `none` hides every
11
- * child but the video. The element reflects the video's state as
12
- * attributes on itself, `paused`, `playing`, `ended`, `muted`, `audio`,
13
- * `started` and `waiting`, for the page's stylesheet and the controls
14
- * alike.
15
- *
16
- * No class fields, `#private` or otherwise: the ES2015 build would lower
17
- * them into runtime helpers, and the emit check bans helpers. Statics are
18
- * getters and instance state is declared, then assigned in the constructor.
19
- */
20
- import { createPlayer, inferType, matteboxHandler, nativeHandler } from '@mattebox/player-core';
21
- import { namespaces } from './namespaces.js';
22
- import { drmGuard, resolvePreset } from './presets.js';
23
- import { STYLE } from './style.js';
24
- import { errorSurface } from './surface.js';
25
- import { PLAYER } from './tags.js';
1
+ import { namespaces } from "./namespaces.js";
2
+ import { drmGuard, resolvePreset } from "./presets.js";
3
+ import { STYLE } from "./style.js";
4
+ import { errorSurface } from "./surface.js";
5
+ import { PLAYER } from "./tags.js";
6
+ import { createPlayer, inferType, matteboxHandler, nativeHandler } from "@mattebox/player-core";
7
+ //#region src/element.ts
26
8
  /** Attributes forwarded onto the video as attributes, never as properties. */
27
- const FORWARDED = ['autoplay', 'muted', 'poster', 'crossorigin'];
9
+ const FORWARDED = [
10
+ "autoplay",
11
+ "muted",
12
+ "poster",
13
+ "crossorigin"
14
+ ];
28
15
  /** Everything else the element watches. Changing any of them reloads. */
29
- const OWN = ['src', 'type', 'preset', 'license-url', 'thumbnails'];
16
+ const OWN = [
17
+ "src",
18
+ "type",
19
+ "preset",
20
+ "license-url",
21
+ "thumbnails"
22
+ ];
30
23
  /**
31
- * The chapters track URL. A `<track kind="chapters">` on the video, hidden
32
- * so its cues load and nothing is drawn: chapters are the browser's own
33
- * text track, which the seek bar and the chapters menu read, and which a
34
- * page can also put there itself. Changing it swaps the track and never
35
- * reloads the source.
36
- */
37
- const CHAPTERS = 'chapters';
24
+ * The chapters file URL. A session with the engine's `chapters` namespace
25
+ * loads it there: through the engine's transport, so request hooks apply,
26
+ * and in every format the engine reads. Any other session gets a
27
+ * `<track kind="chapters">` on the video, hidden so its cues load and
28
+ * nothing is drawn. The seek bar and the chapters menu read either.
29
+ * Changing it never reloads the source.
30
+ */
31
+ const CHAPTERS = "chapters";
38
32
  /**
39
- * The one attribute whose meaning the element owns: `native` keeps the
40
- * browser's controls on the video, `custom` swaps them for the controls the
41
- * page placed inside, `none` takes the browser's off and hides every child
42
- * but the video. Anything else is `native`. Changing it never touches the
43
- * session.
44
- */
45
- const CONTROLS = 'controls';
33
+ * The one attribute whose meaning the element owns: `native` keeps the
34
+ * browser's controls on the video, `custom` swaps them for the controls the
35
+ * page placed inside, `none` takes the browser's off and hides every child
36
+ * but the video. Anything else is `native`. Changing it never touches the
37
+ * session.
38
+ */
39
+ const CONTROLS = "controls";
46
40
  /**
47
- * Inline playback, on until the page writes `playsinline="false"`, the way
48
- * `spellcheck` and `draggable` are on until told otherwise. An iPhone takes
49
- * a video without `playsinline` to its own fullscreen on play, where no
50
- * control the page placed shows. Changing it never reloads.
51
- */
52
- const PLAYSINLINE = 'playsinline';
41
+ * Inline playback, on until the page writes `playsinline="false"`, the way
42
+ * `spellcheck` and `draggable` are on until told otherwise. An iPhone takes
43
+ * a video without `playsinline` to its own fullscreen on play, where no
44
+ * control the page placed shows. Changing it never reloads.
45
+ */
46
+ const PLAYSINLINE = "playsinline";
53
47
  /** The video's state, as attributes on the element, read on the events the video fires for it. */
54
48
  const STATE_EVENTS = [
55
- 'play',
56
- 'pause',
57
- 'ended',
58
- 'emptied',
59
- 'volumechange',
60
- 'loadedmetadata',
61
- 'resize',
62
- 'playing',
63
- 'waiting',
64
- 'stalled',
65
- 'canplay',
49
+ "play",
50
+ "pause",
51
+ "ended",
52
+ "emptied",
53
+ "volumechange",
54
+ "loadedmetadata",
55
+ "resize",
56
+ "playing",
57
+ "waiting",
58
+ "stalled",
59
+ "canplay"
66
60
  ];
67
61
  /** Every stage the engine ships. A narrower preset is optimization. */
68
- const DEFAULT_PRESET = 'full';
62
+ const DEFAULT_PRESET = "full";
69
63
  /**
70
- * What `define()` recorded, for the elements the parser creates. It is the
71
- * one value shared across elements, because a page that writes only markup
72
- * still has to be able to say what the engine carries.
73
- */
64
+ * What `define()` recorded, for the elements the parser creates. It is the
65
+ * one value shared across elements, because a page that writes only markup
66
+ * still has to be able to say what the engine carries.
67
+ */
74
68
  let defaults = {};
75
69
  /**
76
- * The interface above merges only `addEventListener` and `removeEventListener`
77
- * overloads, which the class inherits from HTMLElement. The rule it suppresses
78
- * guards against properties an interface declares and a constructor never
79
- * assigns, and there are none.
80
- */
81
- // biome-ignore lint/suspicious/noUnsafeDeclarationMerging: method overloads only, no properties
82
- export class MatteboxPlayerElement extends HTMLElement {
83
- static get observedAttributes() {
84
- return [...FORWARDED, ...OWN, CONTROLS, CHAPTERS, PLAYSINLINE];
85
- }
86
- /**
87
- * Registers the element once, and records the stages markup-only pages
88
- * get. The controls register through their own entries, the package's
89
- * root entry importing every one; a control connected before its player
90
- * is defined waits for the definition.
91
- */
92
- static define(options) {
93
- if (options !== undefined)
94
- defaults = options;
95
- if (customElements.get(PLAYER) === undefined) {
96
- customElements.define(PLAYER, MatteboxPlayerElement);
97
- }
98
- }
99
- constructor(options) {
100
- super();
101
- this.options = options ?? defaults;
102
- this.focusable = false;
103
- this.track = null;
104
- this.core = null;
105
- this.current = null;
106
- this.failure = null;
107
- this.reflecting = false;
108
- this.started = false;
109
- this.waiting = false;
110
- this.offs = [];
111
- this.queue = Promise.resolve();
112
- this.pending = false;
113
- this.media = document.createElement('video');
114
- // Native until the `controls` attribute says otherwise, which arrives
115
- // through attributeChangedCallback: a constructor must not read attributes.
116
- this.media.controls = true;
117
- // Inline is the default, and the player's own attribute has not arrived
118
- // yet. Set as an attribute: not every browser has the `playsInline`
119
- // property. The fullscreen button still reaches the iPhone's fullscreen,
120
- // through `webkitEnterFullscreen`.
121
- this.media.setAttribute(PLAYSINLINE, '');
122
- for (const name of STATE_EVENTS) {
123
- this.media.addEventListener(name, () => {
124
- this.note(name);
125
- this.reflect();
126
- });
127
- }
128
- // An error over a picture that then plays is wrong by definition.
129
- this.media.addEventListener('playing', () => {
130
- this.failure = null;
131
- });
132
- this.stage = document.createElement('div');
133
- this.stage.setAttribute('part', 'stage');
134
- this.stage.append(document.createElement('slot'));
135
- this.surface = errorSurface(() => {
136
- this.reload();
137
- });
138
- const root = this.attachShadow({ mode: 'open' });
139
- const style = document.createElement('style');
140
- style.textContent = STYLE;
141
- root.append(style, this.stage, this.surface.root);
142
- }
143
- /** The media element. Everything the browser already does is here. */
144
- get video() {
145
- return this.media;
146
- }
147
- /** The core, or null until the first source has been set up: resolving a preset is asynchronous. */
148
- get player() {
149
- return this.core;
150
- }
151
- /** The engine feeding the video, or null for a native session and before the first load. */
152
- get engine() {
153
- return this.current?.engine ?? null;
154
- }
155
- /**
156
- * The fatal error of the current load, or null once a load starts or
157
- * playback resumes. The `error` event says when; this says what, for a
158
- * control that attaches after the event, such as an error screen parsed
159
- * after a source that failed while the page was still loading.
160
- */
161
- get error() {
162
- return this.failure;
163
- }
164
- connectedCallback() {
165
- if (this.media.parentNode !== this)
166
- this.append(this.media);
167
- for (const name of FORWARDED)
168
- this.forward(name);
169
- this.reflect();
170
- this.reload();
171
- }
172
- /** The two states the video does not hold: whether it has played once, and whether it waits for data. */
173
- note(event) {
174
- if (event === 'playing') {
175
- this.started = true;
176
- this.waiting = false;
177
- }
178
- else if (event === 'waiting' || event === 'stalled') {
179
- this.waiting = true;
180
- }
181
- else if (event === 'canplay') {
182
- this.waiting = false;
183
- }
184
- else if (event === 'emptied') {
185
- // A new source starts over: the title shows again before its first play.
186
- this.started = false;
187
- this.waiting = false;
188
- }
189
- }
190
- /** The video's state on the element, for `mattebox-player[paused]` and the like. */
191
- reflect() {
192
- const video = this.media;
193
- this.reflecting = true;
194
- this.toggleAttribute('paused', video.paused);
195
- this.toggleAttribute('playing', !video.paused);
196
- this.toggleAttribute('ended', video.ended);
197
- // `muted` is also forwarded: the page's attribute sets the video, and
198
- // the video's state sets the attribute. Reflection never forwards, so
199
- // a read can only ever change the attribute, never the video.
200
- this.toggleAttribute('muted', video.muted);
201
- this.toggleAttribute('audio', this.audio());
202
- this.toggleAttribute('started', this.started);
203
- this.toggleAttribute('waiting', this.waiting);
204
- this.reflecting = false;
205
- }
206
- /**
207
- * Whether the source has no picture. Once the metadata is in, the video's
208
- * size decides: zero by zero is sound alone, and `resize` catches a stream
209
- * that moves to or from an audio-only rendition. Before that, the type the
210
- * page wrote or the URL implies is the hint, so an mp3 is `audio` from
211
- * the start rather than from its metadata.
212
- */
213
- audio() {
214
- const video = this.media;
215
- if (video.readyState >= HTMLMediaElement.HAVE_METADATA) {
216
- return video.videoWidth === 0 && video.videoHeight === 0;
217
- }
218
- const src = this.getAttribute('src');
219
- const type = this.getAttribute('type') ?? (src === null ? undefined : inferType(src));
220
- return type?.startsWith('audio/') === true;
221
- }
222
- disconnectedCallback() {
223
- // A single-page app must not leave a pipeline attached to a video it
224
- // navigated away from. Reconnecting loads the `src` again.
225
- this.enqueue(() => this.teardown());
226
- }
227
- attributeChangedCallback(name) {
228
- if (FORWARDED.includes(name)) {
229
- if (!this.reflecting)
230
- this.forward(name);
231
- return;
232
- }
233
- if (name === CONTROLS) {
234
- this.mode();
235
- return;
236
- }
237
- if (name === CHAPTERS) {
238
- this.chapters();
239
- return;
240
- }
241
- if (name === PLAYSINLINE) {
242
- // On the video it is a boolean attribute, so off is absent.
243
- this.media.toggleAttribute(PLAYSINLINE, this.getAttribute(PLAYSINLINE) !== 'false');
244
- return;
245
- }
246
- // The preset decides the chain, so it is the one attribute that rebuilds it.
247
- if (name === 'preset')
248
- this.enqueue(() => this.discard());
249
- // The source's type is the hint for `audio` until its metadata arrives.
250
- // Only `audio`: a full reflection here would drop a `muted` the page
251
- // wrote before its own callback forwards it.
252
- if (name === 'src' || name === 'type')
253
- this.toggleAttribute('audio', this.audio());
254
- this.reload();
255
- }
256
- /**
257
- * Applies the `controls` attribute: the video's own controls and the
258
- * tabindex. The controls themselves are elements the page placed, and
259
- * they attach on their own; the element appends none.
260
- */
261
- mode() {
262
- const value = this.getAttribute(CONTROLS);
263
- const custom = value === 'custom';
264
- this.media.controls = !custom && value !== 'none';
265
- // Under custom controls the error screen element carries a fatal error.
266
- if (custom)
267
- this.surface.clear();
268
- if (!custom) {
269
- if (this.focusable)
270
- this.removeAttribute('tabindex');
271
- this.focusable = false;
272
- }
273
- else if (!this.hasAttribute('tabindex')) {
274
- // A bare video is not focusable, so without this a click on it would
275
- // leave the shortcuts nowhere to listen. A page that set its own
276
- // tabindex keeps it.
277
- this.tabIndex = 0;
278
- this.focusable = true;
279
- }
280
- }
281
- /** Keeps a hidden chapters track on the video for the `chapters` attribute, or none. */
282
- chapters() {
283
- const url = this.getAttribute(CHAPTERS);
284
- if (url === null) {
285
- this.track?.remove();
286
- this.track = null;
287
- return;
288
- }
289
- const held = this.track;
290
- if (held !== null && held.parentNode === this.media && held.getAttribute('src') === url)
291
- return;
292
- // A new element each time: one removed mid-load, as the engine's attach
293
- // does, stays in its error state, and neither a new `src` nor a second
294
- // insertion starts it loading again.
295
- held?.remove();
296
- const track = document.createElement('track');
297
- track.kind = 'chapters';
298
- track.src = url;
299
- this.track = track;
300
- this.media.append(track);
301
- // Set after the insertion: a track element's mode exists once it is in
302
- // a media element, and `hidden` is what makes its cues load unseen.
303
- track.track.mode = 'hidden';
304
- }
305
- forward(name) {
306
- const value = this.getAttribute(name);
307
- if (value === null)
308
- this.media.removeAttribute(name);
309
- else
310
- this.media.setAttribute(name, value);
311
- // A video reads `muted` into its state only when it is created, and this
312
- // one was created before any attribute reached it. Muted autoplay and
313
- // the autoplay policy read the state, so the attribute alone would not do.
314
- if (name === 'muted')
315
- this.media.muted = value !== null;
316
- }
317
- enqueue(task) {
318
- this.queue = this.queue.then(task, task).catch(() => undefined);
319
- }
320
- /**
321
- * One run per burst. The parser sets every attribute and then connects the
322
- * element before any queued task starts, and each of those would load on
323
- * its own; the flag folds them into the one run that sees the final state.
324
- */
325
- reload() {
326
- if (this.pending)
327
- return;
328
- this.pending = true;
329
- this.enqueue(() => {
330
- this.pending = false;
331
- return this.run();
332
- });
333
- }
334
- emit(name, detail) {
335
- this.dispatchEvent(new CustomEvent(name, { detail, bubbles: true, composed: true }));
336
- }
337
- report(error) {
338
- if (error.fatal) {
339
- this.failure = error;
340
- // Under custom controls the error screen element is the surface, and
341
- // the element's own row under the video stays quiet.
342
- if (this.getAttribute(CONTROLS) !== 'custom')
343
- this.surface.show(error);
344
- }
345
- this.emit('error', error);
346
- }
347
- async run() {
348
- this.unmount();
349
- if (!this.isConnected)
350
- return;
351
- const url = this.getAttribute('src');
352
- if (url === null)
353
- return;
354
- const core = await this.ensure();
355
- const type = this.getAttribute('type');
356
- const source = type === null ? { url } : { url, type };
357
- this.failure = null;
358
- this.surface.clear();
359
- let session;
360
- try {
361
- session = await core.load(source);
362
- }
363
- catch {
364
- // The core already reported it on the error event, which the surface
365
- // and the element's own event both carry. Nothing to add here.
366
- return;
367
- }
368
- if (!this.isConnected) {
369
- await core.unload();
370
- return;
371
- }
372
- this.current = session;
373
- this.configure(session);
374
- // The engine's attach empties the media element to reset its resource
375
- // selection (`kernel/mse.ts`), and the chapters track goes with the
376
- // `<source>` children it means. Wanted: the track to survive attach.
377
- // Had to: put it back once the session is in. The surface that would
378
- // make it one call: an attach that removes `<source>` children alone.
379
- this.chapters();
380
- }
381
- async ensure() {
382
- if (this.core !== null)
383
- return this.core;
384
- const core = createPlayer(this.media, { handlers: await this.chain() });
385
- this.offs.push(core.on('sourcechange', (session) => {
386
- // Before the event goes out, so a control that reads `engine` on
387
- // `sourcechange` sees the session the event is about.
388
- this.current = session;
389
- this.emit('sourcechange', session);
390
- }), core.on('error', (error) => {
391
- this.report(error);
392
- }));
393
- this.core = core;
394
- return core;
395
- }
396
- async chain() {
397
- if (this.options.handlers !== undefined)
398
- return this.options.handlers;
399
- const config = this.options.config === undefined ? {} : { config: this.options.config };
400
- if (this.options.stages !== undefined) {
401
- return [matteboxHandler({ stages: this.options.stages, ...config }), nativeHandler()];
402
- }
403
- const name = this.getAttribute('preset') ?? DEFAULT_PRESET;
404
- const preset = await resolvePreset(name);
405
- if (preset === null) {
406
- this.report({
407
- category: 'config',
408
- code: 'CONFIG_INVALID',
409
- fatal: true,
410
- recoverable: false,
411
- handler: null,
412
- context: { preset: name },
413
- });
414
- return [nativeHandler()];
415
- }
416
- return [matteboxHandler({ preset, ...config, ...drmGuard() }), nativeHandler()];
417
- }
418
- /** What the attributes ask of the session's namespaces, once there is one. */
419
- configure(session) {
420
- const engine = session.engine;
421
- if (engine === null)
422
- return;
423
- const optional = namespaces(engine);
424
- const license = this.getAttribute('license-url');
425
- // After the session exists, not before: passing a license URL up front
426
- // means constructing eme-core, and importing a stage to configure it
427
- // would bundle it.
428
- if (license !== null && optional.drm !== undefined)
429
- optional.drm.setLicenseUrl(license);
430
- const track = this.getAttribute('thumbnails');
431
- if (track !== null && optional.thumbnails !== undefined) {
432
- // Loaded here; drawn by the seek bar's preview under `controls="custom"`.
433
- // Native controls expose no scrub position to anchor a preview to, so
434
- // under `native` the track only answers `engine.thumbnails.at(time)` for
435
- // the app. See docs/guide/03-the-element.md.
436
- void optional.thumbnails.load(track).catch(() => undefined);
437
- }
438
- }
439
- unmount() {
440
- this.current = null;
441
- }
442
- async teardown() {
443
- this.unmount();
444
- if (this.core !== null)
445
- await this.core.unload();
446
- }
447
- /** Drops the player itself, so the next run builds a chain from the new preset. */
448
- async discard() {
449
- await this.teardown();
450
- for (const off of this.offs)
451
- off();
452
- this.offs = [];
453
- this.core = null;
454
- }
455
- }
70
+ * The interface above merges only `addEventListener` and `removeEventListener`
71
+ * overloads, which the class inherits from HTMLElement. The rule it suppresses
72
+ * guards against properties an interface declares and a constructor never
73
+ * assigns, and there are none.
74
+ */
75
+ var MatteboxPlayerElement = class MatteboxPlayerElement extends HTMLElement {
76
+ static get observedAttributes() {
77
+ return [
78
+ ...FORWARDED,
79
+ ...OWN,
80
+ CONTROLS,
81
+ CHAPTERS,
82
+ PLAYSINLINE
83
+ ];
84
+ }
85
+ /**
86
+ * Registers the element once, and records the stages markup-only pages
87
+ * get. The controls register through their own entries, the package's
88
+ * root entry importing every one; a control connected before its player
89
+ * is defined waits for the definition.
90
+ */
91
+ static define(options) {
92
+ if (options !== void 0) defaults = options;
93
+ if (customElements.get("mattebox-player") === void 0) customElements.define(PLAYER, MatteboxPlayerElement);
94
+ }
95
+ constructor(options) {
96
+ super();
97
+ this.options = options ?? defaults;
98
+ this.focusable = false;
99
+ this.track = null;
100
+ this.given = null;
101
+ this.core = null;
102
+ this.current = null;
103
+ this.failure = null;
104
+ this.reflecting = false;
105
+ this.started = false;
106
+ this.waiting = false;
107
+ this.offs = [];
108
+ this.queue = Promise.resolve();
109
+ this.pending = false;
110
+ this.media = document.createElement("video");
111
+ this.media.controls = true;
112
+ this.media.setAttribute(PLAYSINLINE, "");
113
+ for (const name of STATE_EVENTS) this.media.addEventListener(name, () => {
114
+ this.note(name);
115
+ this.reflect();
116
+ });
117
+ this.media.addEventListener("playing", () => {
118
+ this.failure = null;
119
+ });
120
+ this.stage = document.createElement("div");
121
+ this.stage.setAttribute("part", "stage");
122
+ this.stage.append(document.createElement("slot"));
123
+ this.surface = errorSurface(() => {
124
+ this.reload();
125
+ });
126
+ const root = this.attachShadow({ mode: "open" });
127
+ const style = document.createElement("style");
128
+ style.textContent = STYLE;
129
+ root.append(style, this.stage, this.surface.root);
130
+ }
131
+ /** The media element. Everything the browser already does is here. */
132
+ get video() {
133
+ return this.media;
134
+ }
135
+ /** The core, or null until the first source has been set up: resolving a preset is asynchronous. */
136
+ get player() {
137
+ return this.core;
138
+ }
139
+ /** The engine feeding the video, or null for a native session and before the first load. */
140
+ get engine() {
141
+ return this.current?.engine ?? null;
142
+ }
143
+ /**
144
+ * The fatal error of the current load, or null once a load starts or
145
+ * playback resumes. The `error` event says when; this says what, for a
146
+ * control that attaches after the event, such as an error screen parsed
147
+ * after a source that failed while the page was still loading.
148
+ */
149
+ get error() {
150
+ return this.failure;
151
+ }
152
+ connectedCallback() {
153
+ if (this.media.parentNode !== this) this.append(this.media);
154
+ for (const name of FORWARDED) this.forward(name);
155
+ this.reflect();
156
+ this.reload();
157
+ }
158
+ /** The two states the video does not hold: whether it has played once, and whether it waits for data. */
159
+ note(event) {
160
+ if (event === "playing") {
161
+ this.started = true;
162
+ this.waiting = false;
163
+ } else if (event === "waiting" || event === "stalled") this.waiting = true;
164
+ else if (event === "canplay") this.waiting = false;
165
+ else if (event === "emptied") {
166
+ this.started = false;
167
+ this.waiting = false;
168
+ }
169
+ }
170
+ /** The video's state on the element, for `mattebox-player[paused]` and the like. */
171
+ reflect() {
172
+ const video = this.media;
173
+ this.reflecting = true;
174
+ this.toggleAttribute("paused", video.paused);
175
+ this.toggleAttribute("playing", !video.paused);
176
+ this.toggleAttribute("ended", video.ended);
177
+ this.toggleAttribute("muted", video.muted);
178
+ this.toggleAttribute("audio", this.audio());
179
+ this.toggleAttribute("started", this.started);
180
+ this.toggleAttribute("waiting", this.waiting);
181
+ this.reflecting = false;
182
+ }
183
+ /**
184
+ * Whether the source has no picture. Once the metadata is in, the video's
185
+ * size decides: zero by zero is sound alone, and `resize` catches a stream
186
+ * that moves to or from an audio-only rendition. Before that, the type the
187
+ * page wrote or the URL implies is the hint, so an mp3 is `audio` from
188
+ * the start rather than from its metadata.
189
+ */
190
+ audio() {
191
+ const video = this.media;
192
+ if (video.readyState >= HTMLMediaElement.HAVE_METADATA) return video.videoWidth === 0 && video.videoHeight === 0;
193
+ const src = this.getAttribute("src");
194
+ return (this.getAttribute("type") ?? (src === null ? void 0 : inferType(src)))?.startsWith("audio/") === true;
195
+ }
196
+ disconnectedCallback() {
197
+ this.enqueue(() => this.teardown());
198
+ }
199
+ attributeChangedCallback(name) {
200
+ if (FORWARDED.includes(name)) {
201
+ if (!this.reflecting) this.forward(name);
202
+ return;
203
+ }
204
+ if (name === CONTROLS) {
205
+ this.mode();
206
+ return;
207
+ }
208
+ if (name === CHAPTERS) {
209
+ this.chapters();
210
+ return;
211
+ }
212
+ if (name === PLAYSINLINE) {
213
+ this.media.toggleAttribute(PLAYSINLINE, this.getAttribute(PLAYSINLINE) !== "false");
214
+ return;
215
+ }
216
+ if (name === "preset") this.enqueue(() => this.discard());
217
+ if (name === "src" || name === "type") this.toggleAttribute("audio", this.audio());
218
+ this.reload();
219
+ }
220
+ /**
221
+ * Applies the `controls` attribute: the video's own controls and the
222
+ * tabindex. The controls themselves are elements the page placed, and
223
+ * they attach on their own; the element appends none.
224
+ */
225
+ mode() {
226
+ const value = this.getAttribute(CONTROLS);
227
+ const custom = value === "custom";
228
+ this.media.controls = !custom && value !== "none";
229
+ if (custom) this.surface.clear();
230
+ if (!custom) {
231
+ if (this.focusable) this.removeAttribute("tabindex");
232
+ this.focusable = false;
233
+ } else if (!this.hasAttribute("tabindex")) {
234
+ this.tabIndex = 0;
235
+ this.focusable = true;
236
+ }
237
+ }
238
+ /** Hands the `chapters` attribute to the engine, or keeps a hidden chapters track on the video for it. */
239
+ chapters() {
240
+ const url = this.getAttribute(CHAPTERS);
241
+ const engine = this.current?.engine ?? null;
242
+ const api = engine === null ? void 0 : namespaces(engine).chapters;
243
+ if (api !== void 0) {
244
+ this.track?.remove();
245
+ this.track = null;
246
+ if (url === this.given) return;
247
+ this.given = url;
248
+ if (url === null) api.set([]);
249
+ else api.load(url).catch(() => void 0);
250
+ return;
251
+ }
252
+ if (url === null) {
253
+ this.track?.remove();
254
+ this.track = null;
255
+ return;
256
+ }
257
+ const held = this.track;
258
+ if (held !== null && held.parentNode === this.media && held.getAttribute("src") === url) return;
259
+ held?.remove();
260
+ const track = document.createElement("track");
261
+ track.kind = "chapters";
262
+ track.src = url;
263
+ this.track = track;
264
+ this.media.append(track);
265
+ track.track.mode = "hidden";
266
+ }
267
+ forward(name) {
268
+ const value = this.getAttribute(name);
269
+ if (value === null) this.media.removeAttribute(name);
270
+ else this.media.setAttribute(name, value);
271
+ if (name === "muted") this.media.muted = value !== null;
272
+ }
273
+ enqueue(task) {
274
+ this.queue = this.queue.then(task, task).catch(() => void 0);
275
+ }
276
+ /**
277
+ * One run per burst. The parser sets every attribute and then connects the
278
+ * element before any queued task starts, and each of those would load on
279
+ * its own; the flag folds them into the one run that sees the final state.
280
+ */
281
+ reload() {
282
+ if (this.pending) return;
283
+ this.pending = true;
284
+ this.enqueue(() => {
285
+ this.pending = false;
286
+ return this.run();
287
+ });
288
+ }
289
+ emit(name, detail) {
290
+ this.dispatchEvent(new CustomEvent(name, {
291
+ detail,
292
+ bubbles: true,
293
+ composed: true
294
+ }));
295
+ }
296
+ report(error) {
297
+ if (error.fatal) {
298
+ this.failure = error;
299
+ if (this.getAttribute(CONTROLS) !== "custom") this.surface.show(error);
300
+ }
301
+ this.emit("error", error);
302
+ }
303
+ async run() {
304
+ this.unmount();
305
+ if (!this.isConnected) return;
306
+ const url = this.getAttribute("src");
307
+ if (url === null) return;
308
+ const core = await this.ensure();
309
+ const type = this.getAttribute("type");
310
+ const source = type === null ? { url } : {
311
+ url,
312
+ type
313
+ };
314
+ this.failure = null;
315
+ this.surface.clear();
316
+ let session;
317
+ try {
318
+ session = await core.load(source);
319
+ } catch {
320
+ return;
321
+ }
322
+ if (!this.isConnected) {
323
+ await core.unload();
324
+ return;
325
+ }
326
+ this.current = session;
327
+ this.given = null;
328
+ this.configure(session);
329
+ this.chapters();
330
+ }
331
+ async ensure() {
332
+ if (this.core !== null) return this.core;
333
+ const core = createPlayer(this.media, { handlers: await this.chain() });
334
+ this.offs.push(core.on("sourcechange", (session) => {
335
+ this.current = session;
336
+ this.emit("sourcechange", session);
337
+ }), core.on("error", (error) => {
338
+ this.report(error);
339
+ }));
340
+ this.core = core;
341
+ return core;
342
+ }
343
+ async chain() {
344
+ if (this.options.handlers !== void 0) return this.options.handlers;
345
+ const config = this.options.config === void 0 ? {} : { config: this.options.config };
346
+ if (this.options.stages !== void 0) return [matteboxHandler({
347
+ stages: this.options.stages,
348
+ ...config
349
+ }), nativeHandler()];
350
+ const name = this.getAttribute("preset") ?? DEFAULT_PRESET;
351
+ const preset = await resolvePreset(name);
352
+ if (preset === null) {
353
+ this.report({
354
+ category: "config",
355
+ code: "CONFIG_INVALID",
356
+ fatal: true,
357
+ recoverable: false,
358
+ handler: null,
359
+ context: { preset: name }
360
+ });
361
+ return [nativeHandler()];
362
+ }
363
+ return [matteboxHandler({
364
+ preset,
365
+ ...config,
366
+ ...drmGuard()
367
+ }), nativeHandler()];
368
+ }
369
+ /** What the attributes ask of the session's namespaces, once there is one. */
370
+ configure(session) {
371
+ const engine = session.engine;
372
+ if (engine === null) return;
373
+ const optional = namespaces(engine);
374
+ const license = this.getAttribute("license-url");
375
+ if (license !== null && optional.drm !== void 0) optional.drm.setLicenseUrl(license);
376
+ const track = this.getAttribute("thumbnails");
377
+ if (track !== null && optional.thumbnails !== void 0) optional.thumbnails.load(track).catch(() => void 0);
378
+ }
379
+ unmount() {
380
+ this.current = null;
381
+ }
382
+ async teardown() {
383
+ this.unmount();
384
+ if (this.core !== null) await this.core.unload();
385
+ }
386
+ /** Drops the player itself, so the next run builds a chain from the new preset. */
387
+ async discard() {
388
+ await this.teardown();
389
+ for (const off of this.offs) off();
390
+ this.offs = [];
391
+ this.core = null;
392
+ }
393
+ };
394
+ //#endregion
395
+ export { MatteboxPlayerElement };
396
+
456
397
  //# sourceMappingURL=element.js.map