mattebox 0.18.0 → 0.19.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 (82) hide show
  1. package/dist/cdn/mattebox.dash-drm.min.js +3 -3
  2. package/dist/cdn/mattebox.dash-ll.min.js +3 -3
  3. package/dist/cdn/mattebox.dash.min.js +2 -2
  4. package/dist/cdn/mattebox.dual-drm.min.js +3 -3
  5. package/dist/cdn/mattebox.dual-ll.min.js +3 -3
  6. package/dist/cdn/mattebox.dual-ts-drm.min.js +3 -3
  7. package/dist/cdn/mattebox.dual-ts.min.js +3 -3
  8. package/dist/cdn/mattebox.dual.min.js +3 -3
  9. package/dist/cdn/mattebox.hls-drm.min.js +3 -3
  10. package/dist/cdn/mattebox.hls-ll.min.js +3 -3
  11. package/dist/cdn/mattebox.hls-ts-drm.min.js +3 -3
  12. package/dist/cdn/mattebox.hls-ts.min.js +3 -3
  13. package/dist/cdn/mattebox.hls.min.js +3 -3
  14. package/dist/cdn/mattebox.kernel.min.js +1 -1
  15. package/dist/cdn/mattebox.min.js +5 -5
  16. package/dist/containers/projection.d.ts +36 -0
  17. package/dist/containers/projection.d.ts.map +1 -0
  18. package/dist/containers/projection.js +68 -0
  19. package/dist/containers/projection.js.map +1 -0
  20. package/dist/containers/webm/cues.d.ts +59 -0
  21. package/dist/containers/webm/cues.d.ts.map +1 -0
  22. package/dist/containers/webm/cues.js +218 -0
  23. package/dist/containers/webm/cues.js.map +1 -0
  24. package/dist/containers/webm/index.d.ts +36 -0
  25. package/dist/containers/webm/index.d.ts.map +1 -0
  26. package/dist/containers/webm/index.js +271 -0
  27. package/dist/containers/webm/index.js.map +1 -0
  28. package/dist/es2015/containers/projection.js +68 -0
  29. package/dist/es2015/containers/webm/cues.js +197 -0
  30. package/dist/es2015/containers/webm/index.js +258 -0
  31. package/dist/es2015/kernel/reducer.js +31 -2
  32. package/dist/es2015/presets/full/index.js +6 -3
  33. package/dist/es2015/presets/tiers/dash-ll.js +2 -1
  34. package/dist/es2015/presets/tiers/dash.js +7 -2
  35. package/dist/es2015/protocols/dash-cmaf/index.js +11 -5
  36. package/dist/es2015/protocols/dash-cmaf/parse.js +40 -12
  37. package/dist/es2015/protocols/dash-live/index.js +7 -4
  38. package/dist/es2015/protocols/dash-ll/index.js +3 -2
  39. package/dist/es2015/stages/projection/index.js +41 -0
  40. package/dist/kernel/reducer.d.ts.map +1 -1
  41. package/dist/kernel/reducer.js +44 -1
  42. package/dist/kernel/reducer.js.map +1 -1
  43. package/dist/presets/full/index.d.ts.map +1 -1
  44. package/dist/presets/full/index.js +5 -2
  45. package/dist/presets/full/index.js.map +1 -1
  46. package/dist/presets/tiers/dash-ll.d.ts.map +1 -1
  47. package/dist/presets/tiers/dash-ll.js +2 -1
  48. package/dist/presets/tiers/dash-ll.js.map +1 -1
  49. package/dist/presets/tiers/dash.d.ts +7 -1
  50. package/dist/presets/tiers/dash.d.ts.map +1 -1
  51. package/dist/presets/tiers/dash.js +3 -2
  52. package/dist/presets/tiers/dash.js.map +1 -1
  53. package/dist/protocols/dash-cmaf/index.d.ts +8 -3
  54. package/dist/protocols/dash-cmaf/index.d.ts.map +1 -1
  55. package/dist/protocols/dash-cmaf/index.js +12 -5
  56. package/dist/protocols/dash-cmaf/index.js.map +1 -1
  57. package/dist/protocols/dash-cmaf/parse.d.ts +6 -1
  58. package/dist/protocols/dash-cmaf/parse.d.ts.map +1 -1
  59. package/dist/protocols/dash-cmaf/parse.js +54 -7
  60. package/dist/protocols/dash-cmaf/parse.js.map +1 -1
  61. package/dist/protocols/dash-live/index.d.ts +6 -3
  62. package/dist/protocols/dash-live/index.d.ts.map +1 -1
  63. package/dist/protocols/dash-live/index.js +7 -4
  64. package/dist/protocols/dash-live/index.js.map +1 -1
  65. package/dist/protocols/dash-ll/index.d.ts.map +1 -1
  66. package/dist/protocols/dash-ll/index.js +3 -2
  67. package/dist/protocols/dash-ll/index.js.map +1 -1
  68. package/dist/stages/eme-wiseplay/index.d.ts +3 -2
  69. package/dist/stages/eme-wiseplay/index.d.ts.map +1 -1
  70. package/dist/stages/eme-wiseplay/index.js.map +1 -1
  71. package/dist/stages/projection/index.d.ts +3 -0
  72. package/dist/stages/projection/index.d.ts.map +1 -0
  73. package/dist/stages/projection/index.js +45 -0
  74. package/dist/stages/projection/index.js.map +1 -0
  75. package/dist/types/ir.d.ts +7 -1
  76. package/dist/types/ir.d.ts.map +1 -1
  77. package/dist/types/kernel.d.ts +3 -1
  78. package/dist/types/kernel.d.ts.map +1 -1
  79. package/dist/types/kernel.js.map +1 -1
  80. package/dist/types/messages.d.ts +13 -1
  81. package/dist/types/messages.d.ts.map +1 -1
  82. package/package.json +1 -1
@@ -0,0 +1,271 @@
1
+ /**
2
+ * WebM segments in DASH. MSE takes `video/webm` and `audio/webm` as they
3
+ * are (W3C WebM Byte Stream Format,
4
+ * https://www.w3.org/TR/mse-byte-stream-format-webm/), so nothing is
5
+ * rewrapped. The stage does two things:
6
+ *
7
+ * - It provides the `video/webm` and `audio/webm` content handlers, so the
8
+ * DASH adapters keep WebM Representations instead of leaving them out.
9
+ * - It resolves a WebM `SegmentBase`: it fetches the initialization range
10
+ * and the Cues at `indexRange`, and turns them into one byte-range
11
+ * segment per Cluster, as Shaka Player and dash.js do.
12
+ *
13
+ * A `SegmentTemplate` WebM Representation needs no index and plays from
14
+ * the MPD alone. Inert module: exports a factory, registers nothing at
15
+ * import time.
16
+ */
17
+ import { scheduled, tickAfter } from '../../kernel/effects.js';
18
+ import { findRendition } from '../../kernel/presentation.js';
19
+ import { activeRenditions } from '../../kernel/rendition-select.js';
20
+ import { isUnresolved } from '../../kernel/timeline.js';
21
+ import { cuesToSegments, readCues, readInit } from './cues.js';
22
+ /** The fetch of a rendition's initialization range: `webm:init:<renditionId>`. */
23
+ const INIT_TOKEN = 'webm:init:';
24
+ /** The fetch of a rendition's Cues: `webm:cues:<renditionId>`. */
25
+ const CUES_TOKEN = 'webm:cues:';
26
+ /** A failed index's retry: `webm:retry:<renditionId>`. */
27
+ const RETRY_TOKEN = 'webm:retry:';
28
+ /** The constraint source that excludes renditions whose index failed. */
29
+ const UNAVAILABLE = 'webm:unavailable';
30
+ /** The default wait before a failed index is tried again, as the DASH adapter waits. */
31
+ const RETRY_SECONDS = 15;
32
+ /** The slice before any load. */
33
+ const INITIAL = { pending: {}, asked: {}, inits: {}, cues: {}, failed: {}, seen: '' };
34
+ /** WebM `SegmentBase` addressing not resolved yet, or null for anything else. */
35
+ function asWebm(addressing) {
36
+ return addressing !== undefined && isUnresolved(addressing) && addressing.format === 'webm'
37
+ ? addressing
38
+ : null;
39
+ }
40
+ /** Loops a message back into the bus through a zero-delay schedule effect. */
41
+ function feed(message) {
42
+ return scheduled('webm:loopback', message);
43
+ }
44
+ /** A copy of `record` without `key`. */
45
+ function without(record, key) {
46
+ const copy = { ...record };
47
+ delete copy[key];
48
+ return copy;
49
+ }
50
+ /** The slice with every trace of one rendition's index gone, its in-flight tokens too. */
51
+ function forget(state, renditionId) {
52
+ const pending = { ...state.pending };
53
+ delete pending[`${INIT_TOKEN}${renditionId}`];
54
+ delete pending[`${CUES_TOKEN}${renditionId}`];
55
+ return {
56
+ ...state,
57
+ pending,
58
+ asked: without(state.asked, renditionId),
59
+ inits: without(state.inits, renditionId),
60
+ cues: without(state.cues, renditionId),
61
+ };
62
+ }
63
+ /** The error a WebM index that cannot be read reports. */
64
+ function invalid(renditionId, reason) {
65
+ return {
66
+ category: 'media',
67
+ code: 'MEDIA_CONTAINER_INVALID',
68
+ fatal: false,
69
+ recoverable: false,
70
+ context: { renditionId, reason },
71
+ };
72
+ }
73
+ /**
74
+ * An index that failed to load or could not be read. The rendition is
75
+ * excluded until its retry, as the DASH adapter does for a `sidx`. When
76
+ * that leaves a media track with no rendition, the load fails.
77
+ */
78
+ function unavailable(state, kernel, renditionId, error, retryMs) {
79
+ const failed = { ...state.failed, [renditionId]: true };
80
+ const next = { ...forget(state, renditionId), failed };
81
+ const effects = [
82
+ {
83
+ kind: 'emit',
84
+ event: 'error',
85
+ payload: {
86
+ category: error.category,
87
+ code: error.code,
88
+ fatal: false,
89
+ recoverable: false,
90
+ renditionId,
91
+ },
92
+ },
93
+ ];
94
+ const track = kernel.presentation === null ? null : findRendition(kernel.presentation, renditionId)?.track;
95
+ const media = track?.contentType === 'video' || track?.contentType === 'audio';
96
+ if (track != null && media && track.renditions.every((r) => r.id in failed)) {
97
+ return [
98
+ next,
99
+ [...effects, feed({ type: 'MANIFEST_FAILED', error: { ...error, fatal: true } })],
100
+ ];
101
+ }
102
+ effects.push(feed({
103
+ type: 'CONSTRAIN',
104
+ source: UNAVAILABLE,
105
+ constraint: { excludeIds: Object.keys(failed) },
106
+ }));
107
+ if (Number.isFinite(retryMs))
108
+ effects.push(tickAfter(`${RETRY_TOKEN}${renditionId}`, retryMs));
109
+ return [next, effects];
110
+ }
111
+ /** The retry of a failed index: the rendition returns to arbitration and is fetched when needed. */
112
+ function retry(state, renditionId) {
113
+ if (!(renditionId in state.failed))
114
+ return [state, []];
115
+ const failed = without(state.failed, renditionId);
116
+ return [
117
+ { ...state, failed },
118
+ [
119
+ feed({
120
+ type: 'CONSTRAIN',
121
+ source: UNAVAILABLE,
122
+ constraint: { excludeIds: Object.keys(failed) },
123
+ }),
124
+ ],
125
+ ];
126
+ }
127
+ /**
128
+ * One fetched part of an index. Once both parts of a rendition are read,
129
+ * its segments go to the kernel as a PLAYLIST_REFRESHED, which merges them
130
+ * as it merges a resolved `sidx`.
131
+ */
132
+ function received(state, kernel, token, bytes, retryMs) {
133
+ const renditionId = state.pending[token];
134
+ let next = { ...state, pending: without(state.pending, token) };
135
+ const rendition = kernel.presentation === null ? null : findRendition(kernel.presentation, renditionId);
136
+ const addressing = asWebm(rendition?.rendition.segments);
137
+ const initRange = rendition?.rendition.init?.byteRange;
138
+ // The rendition left the presentation (a reload replaced it): nothing to do.
139
+ if (addressing === null || initRange === undefined)
140
+ return [forget(next, renditionId), []];
141
+ const data = new Uint8Array(bytes);
142
+ if (token.startsWith(INIT_TOKEN)) {
143
+ const init = readInit(data);
144
+ if (init === null) {
145
+ return unavailable(next, kernel, renditionId, invalid(renditionId, 'no WebM Info'), retryMs);
146
+ }
147
+ next = { ...next, inits: { ...next.inits, [renditionId]: init } };
148
+ }
149
+ else {
150
+ const cues = readCues(data);
151
+ if (cues === null) {
152
+ return unavailable(next, kernel, renditionId, invalid(renditionId, 'no WebM Cues'), retryMs);
153
+ }
154
+ next = { ...next, cues: { ...next.cues, [renditionId]: cues } };
155
+ }
156
+ const init = next.inits[renditionId];
157
+ const cues = next.cues[renditionId];
158
+ if (init === undefined || cues === undefined)
159
+ return [next, []];
160
+ const segments = cuesToSegments(init, initRange, cues, addressing);
161
+ if (segments.length === 0) {
162
+ const reason = 'WebM Cues hold no segment';
163
+ return unavailable(next, kernel, renditionId, invalid(renditionId, reason), retryMs);
164
+ }
165
+ // Stays asked until the merge lands.
166
+ return [
167
+ { ...next, inits: without(next.inits, renditionId), cues: without(next.cues, renditionId) },
168
+ [
169
+ feed({
170
+ type: 'PLAYLIST_REFRESHED',
171
+ trackId: renditionId,
172
+ renditionId,
173
+ mediaSequence: 0,
174
+ segments,
175
+ }),
176
+ ],
177
+ ];
178
+ }
179
+ /**
180
+ * The renditions whose index the selection needs and this slice has not
181
+ * asked for: the same set the DASH adapter resolves for a `sidx`, the
182
+ * playing video rendition and its ladder neighbours, and every rendition
183
+ * of the active audio and text tracks.
184
+ */
185
+ function needed(state, kernel) {
186
+ const out = [];
187
+ const types = ['video', 'audio', 'text'];
188
+ for (const { rendition } of activeRenditions(kernel, types, kernel.quality.active)) {
189
+ if (asWebm(rendition.segments) === null || rendition.init?.byteRange === undefined)
190
+ continue;
191
+ if (rendition.id in state.asked || rendition.id in state.failed)
192
+ continue;
193
+ out.push(rendition);
194
+ }
195
+ return out;
196
+ }
197
+ /** The slice reducer. Pure: it reads bytes from facts and answers with effects. */
198
+ export const createReduceWebm = (retryMs) => (slice, msg, kernel) => {
199
+ const state = slice ?? INITIAL;
200
+ if (msg.type === 'LOAD' || msg.type === 'UNLOAD' || msg.type === 'DETACH') {
201
+ return [INITIAL, []];
202
+ }
203
+ if (msg.type === 'TICK' && msg.token.startsWith(RETRY_TOKEN)) {
204
+ return retry(state, msg.token.slice(RETRY_TOKEN.length));
205
+ }
206
+ // The merge landed: the rendition is resolved, or gone.
207
+ if (msg.type === 'PLAYLIST_REFRESHED' && msg.renditionId !== undefined) {
208
+ if (msg.renditionId in state.asked)
209
+ return [forget(state, msg.renditionId), []];
210
+ }
211
+ if (msg.type === 'SEGMENT_LOADED' && msg.trackId in state.pending) {
212
+ return received(state, kernel, msg.trackId, msg.bytes, retryMs);
213
+ }
214
+ if (msg.type === 'SEGMENT_FAILED' && msg.trackId in state.pending) {
215
+ // The transport already retried under its policy.
216
+ const renditionId = state.pending[msg.trackId];
217
+ return unavailable(state, kernel, renditionId, msg.error, retryMs);
218
+ }
219
+ // Look at the selection again when it changed, as the DASH adapter does.
220
+ if (kernel.lifecycle.phase !== 'ready' || kernel.presentation === null)
221
+ return [state, []];
222
+ const key = `${kernel.quality.version}:${kernel.quality.active}`;
223
+ if (key === state.seen && msg.type !== 'RESUME')
224
+ return [state, []];
225
+ const effects = [];
226
+ const pending = { ...state.pending };
227
+ const asked = { ...state.asked };
228
+ for (const rendition of needed(state, kernel)) {
229
+ const addressing = asWebm(rendition.segments);
230
+ const init = rendition.init;
231
+ asked[rendition.id] = true;
232
+ // Both ranges at once: the Cues need the Info's scale and the
233
+ // Segment's start, which only the initialization range holds.
234
+ for (const [prefix, url, range] of [
235
+ [INIT_TOKEN, init.url, init.byteRange],
236
+ [CUES_TOKEN, addressing.url, addressing.indexRange],
237
+ ]) {
238
+ const token = `${prefix}${rendition.id}`;
239
+ pending[token] = rendition.id;
240
+ // The rendition rides along so a failure counts toward steering failover.
241
+ effects.push({
242
+ kind: 'fetch',
243
+ token,
244
+ url,
245
+ ...(range !== undefined ? { range } : {}),
246
+ renditionId: rendition.id,
247
+ });
248
+ }
249
+ }
250
+ return [{ ...state, pending, asked, seen: key }, effects];
251
+ };
252
+ /**
253
+ * The stage factory. Add it beside a DASH adapter:
254
+ * `mattebox({ stages: [dashCmaf(), webm()] })`.
255
+ */
256
+ export default function webm(options = {}) {
257
+ const retryMs = (options.unavailableRetrySeconds ?? RETRY_SECONDS) * 1000;
258
+ return {
259
+ name: 'webm',
260
+ provides: [
261
+ 'webm',
262
+ { contentType: 'video', mimeType: 'video/webm' },
263
+ { contentType: 'audio', mimeType: 'audio/webm' },
264
+ ],
265
+ requires: ['dash-cmaf'],
266
+ install(ctx) {
267
+ ctx.reduce('webm', createReduceWebm(retryMs));
268
+ },
269
+ };
270
+ }
271
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/containers/webm/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAC7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAMxD,OAAO,EAAiB,cAAc,EAAE,QAAQ,EAAE,QAAQ,EAAiB,MAAM,WAAW,CAAC;AAE7F,kFAAkF;AAClF,MAAM,UAAU,GAAG,YAAY,CAAC;AAChC,kEAAkE;AAClE,MAAM,UAAU,GAAG,YAAY,CAAC;AAChC,0DAA0D;AAC1D,MAAM,WAAW,GAAG,aAAa,CAAC;AAClC,yEAAyE;AACzE,MAAM,WAAW,GAAG,kBAAkB,CAAC;AACvC,wFAAwF;AACxF,MAAM,aAAa,GAAG,EAAE,CAAC;AA4BzB,iCAAiC;AACjC,MAAM,OAAO,GAAc,EAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,KAAK,EAAE,E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,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE;YAChD,EAAE,WAAW,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE;SACjD;QACD,QAAQ,EAAE,CAAC,WAAW,CAAC;QACvB,OAAO,CAAC,GAAG;YACT,GAAG,CAAC,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,OAAO,CAAiB,CAAC,CAAC;QAChE,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,68 @@
1
+ import { findBox, findBoxes, fourcc, fullBox, sampleEntries, viewOf } from "./mp4-box/index.js";
2
+ // src/containers/projection.ts
3
+ /** A quarter of the sphere's width in 0.32 fixed point: 0.25 × 2³². */
4
+ const QUARTER = 1073741824;
5
+ /** The box `type` under `parent`, or null. Each step searches one level of children. */
6
+ function under(parent, ...path) {
7
+ let at = parent;
8
+ for (const type of path) {
9
+ var _findBox$payload, _findBox;
10
+ if (at === null) return null;
11
+ at = (_findBox$payload = (_findBox = findBox(at, type)) === null || _findBox === void 0 ? void 0 : _findBox.payload) !== null && _findBox$payload !== void 0 ? _findBox$payload : null;
12
+ }
13
+ return at;
14
+ }
15
+ /** Apple's `prji` kind: `equi` is 360°, `hequ` 180°. */
16
+ function apple(children) {
17
+ var _fullBox;
18
+ const prji = under(children, "vexu", "proj", "prji");
19
+ const body = prji === null ? void 0 : (_fullBox = fullBox(prji)) === null || _fullBox === void 0 ? void 0 : _fullBox.body;
20
+ if (body === void 0 || body.byteLength < 4) return null;
21
+ const kind = fourcc(body, 0);
22
+ return kind === "equi" ? "equirectangular" : kind === "hequ" ? "half-equirectangular" : null;
23
+ }
24
+ /**
25
+ * Google's `equi` bounds. Nothing cropped is the whole sphere. A quarter
26
+ * cropped from the left and from the right leaves the front half, 180°
27
+ * wide and full height: what a half-equirectangular picture covers.
28
+ */
29
+ function google(children) {
30
+ var _fullBox2;
31
+ const equi = under(children, "sv3d", "proj", "equi");
32
+ const body = equi === null ? void 0 : (_fullBox2 = fullBox(equi)) === null || _fullBox2 === void 0 ? void 0 : _fullBox2.body;
33
+ if (body === void 0 || body.byteLength < 16) return null;
34
+ const view = viewOf(body);
35
+ const [top, bottom, left, right] = [
36
+ 0,
37
+ 4,
38
+ 8,
39
+ 12
40
+ ].map((at) => view.getUint32(at));
41
+ if (top !== 0 || bottom !== 0) return null;
42
+ if (left === 0 && right === 0) return "equirectangular";
43
+ return left === QUARTER && right === QUARTER ? "half-equirectangular" : null;
44
+ }
45
+ /**
46
+ * The projection the first video track's sample entry states, or null for
47
+ * a flat picture, another projection, or bytes that are not an init
48
+ * segment. Never throws.
49
+ */
50
+ function readProjection(init) {
51
+ for (const trak of findBoxes(init, "moov/trak")) {
52
+ var _fullBox3;
53
+ const hdlr = findBox(trak.payload, "mdia/hdlr");
54
+ const handler = hdlr === null ? void 0 : (_fullBox3 = fullBox(hdlr.payload)) === null || _fullBox3 === void 0 ? void 0 : _fullBox3.body;
55
+ if (handler === void 0 || handler.byteLength < 8 || fourcc(handler, 4) !== "vide") continue;
56
+ const stsd = findBox(trak.payload, "mdia/minf/stbl/stsd");
57
+ if (stsd === null) continue;
58
+ for (const entry of sampleEntries(stsd.payload)) {
59
+ var _apple;
60
+ if (entry.body.byteLength < 78) continue;
61
+ const children = entry.body.subarray(78);
62
+ const found = (_apple = apple(children)) !== null && _apple !== void 0 ? _apple : google(children);
63
+ if (found !== null) return found;
64
+ }
65
+ }
66
+ return null;
67
+ }
68
+ export { readProjection };
@@ -0,0 +1,197 @@
1
+ // src/containers/webm/cues.ts
2
+ /** EBML header, RFC 8794 §11.2.1. */
3
+ const EBML_ID = 440786851;
4
+ /** Segment, RFC 9559 §5.1. */
5
+ const SEGMENT_ID = 408125543;
6
+ /** Info, RFC 9559 §5.1.2. */
7
+ const INFO_ID = 357149030;
8
+ /** TimestampScale (formerly TimecodeScale), RFC 9559 §5.1.2.9. */
9
+ const TIMESTAMP_SCALE_ID = 2807729;
10
+ /** Duration, RFC 9559 §5.1.2.10. */
11
+ const DURATION_ID = 17545;
12
+ /** Cues, RFC 9559 §5.1.5. */
13
+ const CUES_ID = 475249515;
14
+ /** CuePoint, RFC 9559 §5.1.5.1. */
15
+ const CUE_POINT_ID = 187;
16
+ /** CueTime, RFC 9559 §5.1.5.1.1. */
17
+ const CUE_TIME_ID = 179;
18
+ /** CueTrackPositions, RFC 9559 §5.1.5.1.2. */
19
+ const CUE_TRACK_POSITIONS_ID = 183;
20
+ /** CueClusterPosition, RFC 9559 §5.1.5.1.2.2. */
21
+ const CUE_CLUSTER_POSITION_ID = 241;
22
+ /** The TimestampScale default: one tick is a millisecond. RFC 9559 §5.1.2.9. */
23
+ const DEFAULT_TIMESTAMP_SCALE = 1e6;
24
+ /**
25
+ * Reads a variable-size integer at `at`, RFC 8794 §4. `marker` keeps the
26
+ * length marker bit, as an Element ID does (§5); without it the value is
27
+ * a data size. Returns null when the bytes end early or the first byte is
28
+ * zero, which no valid VINT starts with.
29
+ */
30
+ function readVint(bytes, at, marker) {
31
+ const first = bytes[at];
32
+ if (first === void 0 || first === 0) return null;
33
+ let length = 1;
34
+ while ((first & 128 >> length - 1) === 0) length += 1;
35
+ if (at + length > bytes.byteLength) return null;
36
+ let value = marker ? first : first & 255 >> length;
37
+ let unknown = value === 255 >> length;
38
+ for (let i = 1; i < length; i += 1) {
39
+ const byte = bytes[at + i];
40
+ if (byte !== 255) unknown = false;
41
+ value = value * 256 + byte;
42
+ }
43
+ return {
44
+ value,
45
+ length,
46
+ unknown: !marker && unknown
47
+ };
48
+ }
49
+ /**
50
+ * Reads the element header at `at`. Its data is clamped to `limit`, so a
51
+ * Segment whose data runs past the bytes fetched still reads as far as
52
+ * they go, as Shaka's EBML parser reads a partial element. An unknown
53
+ * size extends to `limit`.
54
+ */
55
+ function readElement(bytes, at, limit) {
56
+ const id = readVint(bytes, at, true);
57
+ if (id === null) return null;
58
+ const size = readVint(bytes, at + id.length, false);
59
+ if (size === null) return null;
60
+ const start = at + id.length + size.length;
61
+ if (start > limit) return null;
62
+ const declared = size.unknown ? null : size.value;
63
+ const end = declared === null ? limit : Math.min(limit, start + declared);
64
+ return {
65
+ id: id.value,
66
+ start,
67
+ end,
68
+ size: declared
69
+ };
70
+ }
71
+ /** The child elements between `start` and `end`, in order. Stops at the first header it cannot read. */
72
+ function childrenOf(bytes, start, end) {
73
+ const found = [];
74
+ let at = start;
75
+ while (at < end) {
76
+ const element = readElement(bytes, at, end);
77
+ if (element === null) break;
78
+ found.push(element);
79
+ at = element.end;
80
+ }
81
+ return found;
82
+ }
83
+ /** An unsigned integer element, RFC 8794 §7.2: big-endian, zero to eight bytes. */
84
+ function readUint(bytes, element) {
85
+ let value = 0;
86
+ for (let i = element.start; i < element.end; i += 1) value = value * 256 + bytes[i];
87
+ return value;
88
+ }
89
+ /** A float element, RFC 8794 §7.3: zero, four or eight bytes, big-endian IEEE 754. */
90
+ function readFloat(bytes, element) {
91
+ const length = element.end - element.start;
92
+ const view = new DataView(bytes.buffer, bytes.byteOffset + element.start, length);
93
+ if (length === 4) return view.getFloat32(0);
94
+ if (length === 8) return view.getFloat64(0);
95
+ return length === 0 ? 0 : null;
96
+ }
97
+ /**
98
+ * Reads the initialization range: the EBML header, then the Segment
99
+ * header and its Info. Null when the bytes do not start with an EBML
100
+ * header followed by a Segment, or hold no Info.
101
+ */
102
+ function readInit(bytes) {
103
+ const header = readElement(bytes, 0, bytes.byteLength);
104
+ if (header === null || header.id !== EBML_ID) return null;
105
+ const segment = readElement(bytes, header.end, bytes.byteLength);
106
+ if (segment === null || segment.id !== SEGMENT_ID) return null;
107
+ const info = childrenOf(bytes, segment.start, segment.end).find((e) => e.id === INFO_ID);
108
+ if (info === void 0) return null;
109
+ let scale = DEFAULT_TIMESTAMP_SCALE;
110
+ let ticks = null;
111
+ for (const element of childrenOf(bytes, info.start, info.end)) if (element.id === TIMESTAMP_SCALE_ID) scale = readUint(bytes, element) || scale;
112
+ else if (element.id === DURATION_ID) ticks = readFloat(bytes, element);
113
+ const tickSeconds = scale / 1e9;
114
+ return {
115
+ segmentStart: segment.start,
116
+ segmentSize: segment.size,
117
+ tickSeconds,
118
+ duration: ticks === null ? null : ticks * tickSeconds
119
+ };
120
+ }
121
+ /**
122
+ * Reads the Cues element at the start of the index range. Each CuePoint
123
+ * gives its CueTime and the CueClusterPosition of its first
124
+ * CueTrackPositions. A DASH Representation carries one track, so the
125
+ * first is the one. Null when the bytes do not start with Cues.
126
+ */
127
+ function readCues(bytes) {
128
+ const cues = readElement(bytes, 0, bytes.byteLength);
129
+ if (cues === null || cues.id !== CUES_ID) return null;
130
+ const points = [];
131
+ for (const point of childrenOf(bytes, cues.start, cues.end)) {
132
+ if (point.id !== CUE_POINT_ID) continue;
133
+ let time = null;
134
+ let position = null;
135
+ for (const element of childrenOf(bytes, point.start, point.end)) if (element.id === CUE_TIME_ID) time = readUint(bytes, element);
136
+ else if (element.id === CUE_TRACK_POSITIONS_ID && position === null) {
137
+ const cluster = childrenOf(bytes, element.start, element.end).find((e) => e.id === CUE_CLUSTER_POSITION_ID);
138
+ if (cluster !== void 0) position = readUint(bytes, cluster);
139
+ }
140
+ if (time !== null && position !== null) points.push({
141
+ time,
142
+ position
143
+ });
144
+ }
145
+ return points;
146
+ }
147
+ /**
148
+ * Turns the cue points into byte-range segments, one per Cluster. A
149
+ * segment runs from its Cluster to the byte before the next one. The last
150
+ * ends where the Cues start when they follow it, else at the end of the
151
+ * Segment when its size is known, else at the end of the file (an
152
+ * open-ended range). Its duration runs to the Info Duration, as in Shaka
153
+ * Player; without a Duration the index cannot place the last segment and
154
+ * no segment is returned.
155
+ *
156
+ * Several CuePoints can name one Cluster (one per track, or a
157
+ * CueRelativePosition inside it): only the first of them opens a segment.
158
+ */
159
+ function cuesToSegments(init, initRange, points, addressing) {
160
+ var _addressing$presentat;
161
+ if (init.duration === null) return [];
162
+ const base = initRange.start + init.segmentStart;
163
+ const clusters = [];
164
+ for (const point of points) {
165
+ const previous = clusters[clusters.length - 1];
166
+ if (previous !== void 0 && point.position <= previous.position) continue;
167
+ clusters.push(point);
168
+ }
169
+ const pto = (_addressing$presentat = addressing.presentationTimeOffset) !== null && _addressing$presentat !== void 0 ? _addressing$presentat : 0;
170
+ const indexStart = addressing.indexRange.start;
171
+ const segments = [];
172
+ for (let i = 0; i < clusters.length; i += 1) {
173
+ const point = clusters[i];
174
+ const next = clusters[i + 1];
175
+ const start = point.time * init.tickSeconds;
176
+ const end = next === void 0 ? init.duration : next.time * init.tickSeconds;
177
+ const byteStart = base + point.position;
178
+ let byteEnd;
179
+ if (next !== void 0) byteEnd = base + next.position - 1;
180
+ else if (indexStart > byteStart) byteEnd = indexStart - 1;
181
+ else if (init.segmentSize !== null) byteEnd = base + init.segmentSize - 1;
182
+ else byteEnd = Number.POSITIVE_INFINITY;
183
+ if (end <= start) continue;
184
+ segments.push({
185
+ seq: segments.length,
186
+ start: start - pto,
187
+ duration: end - start,
188
+ url: addressing.url,
189
+ byteRange: {
190
+ start: byteStart,
191
+ end: byteEnd
192
+ }
193
+ });
194
+ }
195
+ return segments;
196
+ }
197
+ export { cuesToSegments, readCues, readInit };