@ac-kit/format-dash 0.0.0-stage → 0.1.1

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 (92) hide show
  1. package/CHANGELOG.md +89 -0
  2. package/LICENSE +28 -0
  3. package/README.md +180 -2
  4. package/dist/_template.d.ts +34 -0
  5. package/dist/_template.d.ts.map +1 -0
  6. package/dist/_template.js +90 -0
  7. package/dist/_template.js.map +1 -0
  8. package/dist/_xlink.d.ts +9 -0
  9. package/dist/_xlink.d.ts.map +1 -0
  10. package/dist/_xlink.js +14 -0
  11. package/dist/_xlink.js.map +1 -0
  12. package/dist/dash-namespaces.d.ts +27 -0
  13. package/dist/dash-namespaces.d.ts.map +1 -0
  14. package/dist/dash-namespaces.js +30 -0
  15. package/dist/dash-namespaces.js.map +1 -0
  16. package/dist/errors.d.ts +127 -0
  17. package/dist/errors.d.ts.map +1 -0
  18. package/dist/errors.js +52 -0
  19. package/dist/errors.js.map +1 -0
  20. package/dist/index.d.ts +12 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +12 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/limits.d.ts +51 -0
  25. package/dist/limits.d.ts.map +1 -0
  26. package/dist/limits.js +39 -0
  27. package/dist/limits.js.map +1 -0
  28. package/dist/model/dash-common.d.ts +124 -0
  29. package/dist/model/dash-common.d.ts.map +1 -0
  30. package/dist/model/dash-common.js +2 -0
  31. package/dist/model/dash-common.js.map +1 -0
  32. package/dist/model/dash-mpd.d.ts +111 -0
  33. package/dist/model/dash-mpd.d.ts.map +1 -0
  34. package/dist/model/dash-mpd.js +2 -0
  35. package/dist/model/dash-mpd.js.map +1 -0
  36. package/dist/model/dash-segment-information.d.ts +90 -0
  37. package/dist/model/dash-segment-information.d.ts.map +1 -0
  38. package/dist/model/dash-segment-information.js +2 -0
  39. package/dist/model/dash-segment-information.js.map +1 -0
  40. package/dist/parse/_attribute-reader.d.ts +94 -0
  41. package/dist/parse/_attribute-reader.d.ts.map +1 -0
  42. package/dist/parse/_attribute-reader.js +382 -0
  43. package/dist/parse/_attribute-reader.js.map +1 -0
  44. package/dist/parse/_context.d.ts +35 -0
  45. package/dist/parse/_context.d.ts.map +1 -0
  46. package/dist/parse/_context.js +39 -0
  47. package/dist/parse/_context.js.map +1 -0
  48. package/dist/parse/_xs-temporal.d.ts +22 -0
  49. package/dist/parse/_xs-temporal.d.ts.map +1 -0
  50. package/dist/parse/_xs-temporal.js +98 -0
  51. package/dist/parse/_xs-temporal.js.map +1 -0
  52. package/dist/parse/parse-dash-mpd.d.ts +67 -0
  53. package/dist/parse/parse-dash-mpd.d.ts.map +1 -0
  54. package/dist/parse/parse-dash-mpd.js +928 -0
  55. package/dist/parse/parse-dash-mpd.js.map +1 -0
  56. package/dist/resolve/_base-url.d.ts +41 -0
  57. package/dist/resolve/_base-url.d.ts.map +1 -0
  58. package/dist/resolve/_base-url.js +69 -0
  59. package/dist/resolve/_base-url.js.map +1 -0
  60. package/dist/resolve/_essential-properties.d.ts +38 -0
  61. package/dist/resolve/_essential-properties.d.ts.map +1 -0
  62. package/dist/resolve/_essential-properties.js +106 -0
  63. package/dist/resolve/_essential-properties.js.map +1 -0
  64. package/dist/resolve/_inherit.d.ts +43 -0
  65. package/dist/resolve/_inherit.d.ts.map +1 -0
  66. package/dist/resolve/_inherit.js +105 -0
  67. package/dist/resolve/_inherit.js.map +1 -0
  68. package/dist/resolve/_segment-runs.d.ts +81 -0
  69. package/dist/resolve/_segment-runs.d.ts.map +1 -0
  70. package/dist/resolve/_segment-runs.js +126 -0
  71. package/dist/resolve/_segment-runs.js.map +1 -0
  72. package/dist/resolve/dash-resolve-diagnostic.d.ts +34 -0
  73. package/dist/resolve/dash-resolve-diagnostic.d.ts.map +1 -0
  74. package/dist/resolve/dash-resolve-diagnostic.js +2 -0
  75. package/dist/resolve/dash-resolve-diagnostic.js.map +1 -0
  76. package/dist/resolve/resolve-dash-representations.d.ts +130 -0
  77. package/dist/resolve/resolve-dash-representations.d.ts.map +1 -0
  78. package/dist/resolve/resolve-dash-representations.js +203 -0
  79. package/dist/resolve/resolve-dash-representations.js.map +1 -0
  80. package/dist/resolve/resolve-dash-segments.d.ts +169 -0
  81. package/dist/resolve/resolve-dash-segments.d.ts.map +1 -0
  82. package/dist/resolve/resolve-dash-segments.js +665 -0
  83. package/dist/resolve/resolve-dash-segments.js.map +1 -0
  84. package/dist/timing/_exact-period-timings.d.ts +28 -0
  85. package/dist/timing/_exact-period-timings.d.ts.map +1 -0
  86. package/dist/timing/_exact-period-timings.js +142 -0
  87. package/dist/timing/_exact-period-timings.js.map +1 -0
  88. package/dist/timing/dash-period-timings.d.ts +29 -0
  89. package/dist/timing/dash-period-timings.d.ts.map +1 -0
  90. package/dist/timing/dash-period-timings.js +23 -0
  91. package/dist/timing/dash-period-timings.js.map +1 -0
  92. package/package.json +63 -3
@@ -0,0 +1,928 @@
1
+ import { withWorkBudget } from "@ac-kit/core";
2
+ import { isXmlWhitespace } from "@ac-kit/format-xml";
3
+ import { decodeXml, isXmlElementInNamespace, parseXml, resolveNamespaces, XmlNamespaceError, xmlDocumentRoot, XmlSyntaxError, trimXsdWhitespace, xmlTextContent, } from "@ac-kit/format-xml";
4
+ import { XLINK_NAMESPACE } from "@ac-kit/model-markup";
5
+ import { DASH_CENC_NAMESPACE, DASH_IF_CONTENT_PROTECTION_NAMESPACES, DASH_MPD_LEGACY_NAMESPACE, DASH_MPD_NAMESPACE, } from "../dash-namespaces.js";
6
+ import { DashLimitError, DashSyntaxError, } from "../errors.js";
7
+ import { DEFAULT_DASH_WORK_BUDGET, dashLimits, } from "../limits.js";
8
+ import { AttributeReader } from "./_attribute-reader.js";
9
+ import { DiagnosticSink } from "./_context.js";
10
+ /**
11
+ * Reads an MPD (ISO/IEC 23009-1, 5th edition schema) into its as-written model.
12
+ * Fetches nothing: remote (xlink) elements are recorded, never resolved.
13
+ *
14
+ * Elements match by namespace: the root's, which must be
15
+ * {@link DASH_MPD_NAMESPACE}, {@link DASH_MPD_LEGACY_NAMESPACE} or none (the
16
+ * last two with an advisory). Every typed attribute is checked against its
17
+ * lexical form; inheritance is not applied; what the model does not type is
18
+ * kept in `otherAttributes` and `otherElements`. ContentProtection is plain
19
+ * data. Durations and date-times are `Temporal` values: requires a `Temporal`
20
+ * global.
21
+ *
22
+ * Time O(n) in the document, charged to `budget`; the recursion follows the MPD
23
+ * schema's fixed depth, never the input's nesting, and wide sibling lists are
24
+ * appended one by one.
25
+ *
26
+ * @throws {XmlSyntaxError} When the source is not well-formed XML or exceeds an
27
+ * XML limit, in both modes.
28
+ * @throws {XmlNamespaceError} When it is not namespace-well-formed, in strict
29
+ * mode.
30
+ * @throws {DashSyntaxError} On the first violation in strict mode; in both
31
+ * modes when the root is not an MPD.
32
+ * @throws {DashLimitError} Past `maxLength`, `maxTimelineEntries` or a private
33
+ * `budget`, in both modes.
34
+ * @throws Whatever a caller's `budget` throws once spent.
35
+ */
36
+ export function parseDashMpd(source, options) {
37
+ return withWorkBudget(options?.budget ?? DEFAULT_DASH_WORK_BUDGET, (budget) => parseDashMpd_(source, options, budget), (cause) => new DashLimitError("budget", cause.limit, null, { cause }));
38
+ }
39
+ function parseDashMpd_(source, options, budget) {
40
+ const limits = dashLimits(options);
41
+ const lenient = options?.mode === "lenient";
42
+ const sink = new DiagnosticSink(lenient, limits.maxWarnings);
43
+ const document = toDocument_(source, limits.maxLength, {
44
+ ...options,
45
+ budget,
46
+ });
47
+ const namespaces = resolveNamespaces(document, {
48
+ mode: lenient ? "lenient" : "strict",
49
+ maxDiagnostics: limits.maxWarnings,
50
+ budget,
51
+ });
52
+ for (const diagnostic of namespaces.diagnostics) {
53
+ sink.violation("namespace", diagnostic.message, diagnostic.position);
54
+ }
55
+ if (namespaces.diagnosticsTruncated) {
56
+ sink.truncated = true;
57
+ }
58
+ const root = xmlDocumentRoot(document);
59
+ const rootNamespace = namespaces.namespaceOf(root);
60
+ const position = root.span?.start;
61
+ if (root.name.local !== "MPD" ||
62
+ (rootNamespace !== DASH_MPD_NAMESPACE &&
63
+ rootNamespace !== DASH_MPD_LEGACY_NAMESPACE &&
64
+ (rootNamespace !== null || root.name.prefix !== null))) {
65
+ sink.fatal("not-an-mpd", `The root element is ${root.name.local} in ${rootNamespace ?? "no namespace"}, not an MPD`, position);
66
+ }
67
+ if (rootNamespace !== DASH_MPD_NAMESPACE) {
68
+ sink.advisory("nonstandard-namespace", `The MPD is in ${rootNamespace === null ? "no namespace" : `the legacy namespace ${rootNamespace}`}, read as ${DASH_MPD_NAMESPACE}`, position);
69
+ }
70
+ budget.spend(1 + root.attributes.length);
71
+ const mpd = readMpd_({
72
+ sink,
73
+ namespaces,
74
+ mpdNamespace: rootNamespace,
75
+ limits,
76
+ budget,
77
+ timelineEntries: 0,
78
+ }, root, position);
79
+ return {
80
+ mpd,
81
+ warnings: sink.warnings,
82
+ warningsTruncated: sink.truncated,
83
+ };
84
+ }
85
+ /**
86
+ * {@link parseDashMpd}, answering `null` instead of throwing `XmlSyntaxError`,
87
+ * `XmlNamespaceError` or {@link DashSyntaxError} — an XML limit is an
88
+ * `XmlSyntaxError`, so it answers `null` too. {@link DashLimitError} and
89
+ * anything thrown by a caller's budget propagate unchanged, even if the budget
90
+ * throws a syntax error.
91
+ */
92
+ export function tryParseDashMpd(source, options) {
93
+ const budget = options?.budget;
94
+ let budgetError;
95
+ const parseOptions = budget === undefined || typeof budget === "number"
96
+ ? options
97
+ : {
98
+ ...options,
99
+ budget: {
100
+ spend(cost) {
101
+ try {
102
+ budget.spend(cost);
103
+ }
104
+ catch (error) {
105
+ budgetError = error;
106
+ throw error;
107
+ }
108
+ },
109
+ },
110
+ };
111
+ try {
112
+ return parseDashMpd(source, parseOptions);
113
+ }
114
+ catch (error) {
115
+ if (error !== budgetError &&
116
+ (error instanceof XmlSyntaxError ||
117
+ error instanceof XmlNamespaceError ||
118
+ error instanceof DashSyntaxError)) {
119
+ return null;
120
+ }
121
+ throw error;
122
+ }
123
+ }
124
+ function toDocument_(source, maxLength, options) {
125
+ if (typeof source !== "string" && !(source instanceof Uint8Array)) {
126
+ return source;
127
+ }
128
+ if (source instanceof Uint8Array && source.byteLength > maxLength * 3) {
129
+ throw new DashLimitError("maxLength", maxLength);
130
+ }
131
+ const text = typeof source === "string" ? source : decodeXml(source);
132
+ if (text.length > maxLength) {
133
+ throw new DashLimitError("maxLength", maxLength);
134
+ }
135
+ return parseXml(text, options);
136
+ }
137
+ /**
138
+ * Hands each child element in the MPD namespace to the handler for its local
139
+ * name, and appends the others, and those with no handler, to `others` in
140
+ * source order — one by one, never spread, so a million siblings cost heap, not
141
+ * stack. Charges the budget one unit per child element and attribute.
142
+ */
143
+ function readChildren_(context, element, position, handlers, others = []) {
144
+ for (const child of element.children) {
145
+ if (child.kind !== "element") {
146
+ continue;
147
+ }
148
+ context.budget.spend(1 + child.attributes.length);
149
+ const handler = isMpdElement_(context, child)
150
+ ? handlers.get(child.name.local)
151
+ : undefined;
152
+ if (handler === undefined) {
153
+ others.push(child);
154
+ }
155
+ else {
156
+ handler(child, child.span?.start ?? position);
157
+ }
158
+ }
159
+ return others;
160
+ }
161
+ function isMpdElement_(context, child) {
162
+ return isXmlElementInNamespace(context.namespaces, child, context.mpdNamespace);
163
+ }
164
+ function textOf_(context, element) {
165
+ const text = xmlTextContent(element);
166
+ context.budget.spend(text.length);
167
+ return trimXsdWhitespace(text);
168
+ }
169
+ function readDescriptor_(context, element, position) {
170
+ const reader = new AttributeReader(context, element, position);
171
+ const schemeIdUri = reader.required("schemeIdUri");
172
+ const value = reader.string("value");
173
+ const id = reader.string("id");
174
+ return {
175
+ schemeIdUri,
176
+ value,
177
+ id,
178
+ otherAttributes: reader.rest(),
179
+ invalidAttributes: reader.invalid(),
180
+ otherElements: readChildren_(context, element, position, new Map()),
181
+ ...spanOf_(element),
182
+ };
183
+ }
184
+ function readContentProtection_(context, element, position) {
185
+ const reader = new AttributeReader(context, element, position);
186
+ const schemeIdUri = reader.required("schemeIdUri");
187
+ const value = reader.string("value");
188
+ const id = reader.string("id");
189
+ const defaultKid = reader.namespaced("default_KID", DASH_CENC_NAMESPACE);
190
+ const robustness = reader.string("robustness");
191
+ const refId = reader.string("refId");
192
+ const ref = reader.string("ref");
193
+ const pssh = [];
194
+ const licenseUrls = [];
195
+ const otherElements = [];
196
+ const { namespaceOf } = context.namespaces;
197
+ for (const child of element.children) {
198
+ if (child.kind !== "element") {
199
+ continue;
200
+ }
201
+ const namespace = namespaceOf(child);
202
+ if (namespace === DASH_CENC_NAMESPACE && child.name.local === "pssh") {
203
+ pssh.push(textOf_(context, child));
204
+ }
205
+ else if (namespace !== null &&
206
+ DASH_IF_CONTENT_PROTECTION_NAMESPACES.includes(namespace) &&
207
+ (child.name.local === "Laurl" || child.name.local === "laurl")) {
208
+ licenseUrls.push(textOf_(context, child));
209
+ }
210
+ else {
211
+ otherElements.push(child);
212
+ }
213
+ }
214
+ return {
215
+ schemeIdUri,
216
+ value,
217
+ id,
218
+ defaultKid,
219
+ robustness,
220
+ refId,
221
+ ref,
222
+ pssh,
223
+ licenseUrls,
224
+ otherAttributes: reader.rest(),
225
+ invalidAttributes: reader.invalid(),
226
+ otherElements,
227
+ ...spanOf_(element),
228
+ };
229
+ }
230
+ function readLabel_(context, element, position) {
231
+ const reader = new AttributeReader(context, element, position);
232
+ const id = reader.unsigned("id");
233
+ const lang = reader.string("lang");
234
+ return {
235
+ id,
236
+ lang,
237
+ text: textOf_(context, element),
238
+ otherAttributes: reader.rest(),
239
+ invalidAttributes: reader.invalid(),
240
+ otherElements: [],
241
+ ...spanOf_(element),
242
+ };
243
+ }
244
+ function readUrl_(context, element, position) {
245
+ const reader = new AttributeReader(context, element, position);
246
+ const sourceUrl = reader.string("sourceURL");
247
+ const range = reader.byteRange("range");
248
+ return {
249
+ sourceUrl,
250
+ range,
251
+ otherAttributes: reader.rest(),
252
+ invalidAttributes: reader.invalid(),
253
+ otherElements: readChildren_(context, element, position, new Map()),
254
+ ...spanOf_(element),
255
+ };
256
+ }
257
+ function readBaseUrl_(context, element, position) {
258
+ const reader = new AttributeReader(context, element, position);
259
+ const serviceLocation = reader.string("serviceLocation");
260
+ const byteRange = reader.string("byteRange");
261
+ const availabilityTimeOffset = reader.double("availabilityTimeOffset");
262
+ const availabilityTimeComplete = reader.boolean("availabilityTimeComplete");
263
+ const timeShiftBufferDepth = reader.duration("timeShiftBufferDepth");
264
+ const rangeAccess = reader.boolean("rangeAccess");
265
+ return {
266
+ url: textOf_(context, element),
267
+ serviceLocation,
268
+ byteRange,
269
+ availabilityTimeOffset,
270
+ availabilityTimeComplete,
271
+ timeShiftBufferDepth,
272
+ rangeAccess,
273
+ otherAttributes: reader.rest(),
274
+ invalidAttributes: reader.invalid(),
275
+ otherElements: [],
276
+ ...spanOf_(element),
277
+ };
278
+ }
279
+ function readSegmentBaseFields_(reader) {
280
+ return {
281
+ timescale: reader.unsigned("timescale"),
282
+ presentationTimeOffset: reader.unsignedLong("presentationTimeOffset"),
283
+ eptDelta: reader.integer("eptDelta"),
284
+ pdDelta: reader.integer("pdDelta"),
285
+ presentationDuration: reader.unsignedLong("presentationDuration"),
286
+ timeShiftBufferDepth: reader.duration("timeShiftBufferDepth"),
287
+ indexRange: reader.byteRange("indexRange"),
288
+ indexRangeExact: reader.boolean("indexRangeExact"),
289
+ availabilityTimeOffset: reader.double("availabilityTimeOffset"),
290
+ availabilityTimeComplete: reader.boolean("availabilityTimeComplete"),
291
+ };
292
+ }
293
+ function readSegmentChildren_(context, element, position, multiple, list) {
294
+ const children = {
295
+ initialization: null,
296
+ representationIndex: null,
297
+ segmentTimeline: null,
298
+ bitstreamSwitching: null,
299
+ segmentUrls: [],
300
+ otherElements: [],
301
+ };
302
+ const handlers = new Map([
303
+ [
304
+ "Initialization",
305
+ (child, at) => {
306
+ children.initialization ??= readUrl_(context, child, at);
307
+ },
308
+ ],
309
+ [
310
+ "RepresentationIndex",
311
+ (child, at) => {
312
+ children.representationIndex ??= readUrl_(context, child, at);
313
+ },
314
+ ],
315
+ ]);
316
+ if (multiple) {
317
+ handlers.set("SegmentTimeline", (child, at) => {
318
+ if (children.segmentTimeline !== null) {
319
+ context.sink.violation("conflicting-segment-information", `${element.name.local} holds two SegmentTimelines`, at);
320
+ children.otherElements.push(child);
321
+ return;
322
+ }
323
+ children.segmentTimeline = readTimeline_(context, child, at);
324
+ });
325
+ handlers.set("BitstreamSwitching", (child, at) => {
326
+ children.bitstreamSwitching ??= readUrl_(context, child, at);
327
+ });
328
+ }
329
+ if (list) {
330
+ handlers.set("SegmentURL", (child, at) => {
331
+ children.segmentUrls.push(readSegmentUrl_(context, child, at));
332
+ });
333
+ }
334
+ readChildren_(context, element, position, handlers, children.otherElements);
335
+ return children;
336
+ }
337
+ function readTimeline_(context, element, position) {
338
+ const entries = [];
339
+ const others = readChildren_(context, element, position, new Map([
340
+ [
341
+ "S",
342
+ (child, at) => {
343
+ context.timelineEntries++;
344
+ if (context.timelineEntries > context.limits.maxTimelineEntries) {
345
+ throw new DashLimitError("maxTimelineEntries", context.limits.maxTimelineEntries);
346
+ }
347
+ const reader = new AttributeReader(context, child, at);
348
+ const t = reader.unsignedLong("t");
349
+ const n = reader.unsignedLongNumber("n");
350
+ const d = reader.required("d") === null ? null : reader.unsignedLong("d");
351
+ const r = reader.string("r") === null ? 0n : reader.integer("r");
352
+ const k = reader.unsignedLongNumber("k");
353
+ const invalidAttributes = reader.invalid();
354
+ entries.push({
355
+ t,
356
+ n,
357
+ d,
358
+ r,
359
+ k,
360
+ ...(invalidAttributes.length > 0 ? { invalidAttributes } : {}),
361
+ ...spanOf_(child),
362
+ });
363
+ },
364
+ ],
365
+ ]));
366
+ return {
367
+ entries,
368
+ otherAttributes: [...element.attributes],
369
+ invalidAttributes: [],
370
+ otherElements: others,
371
+ ...spanOf_(element),
372
+ };
373
+ }
374
+ function readSegmentUrl_(context, element, position) {
375
+ const reader = new AttributeReader(context, element, position);
376
+ const media = reader.string("media");
377
+ const mediaRange = reader.byteRange("mediaRange");
378
+ const index = reader.string("index");
379
+ const indexRange = reader.byteRange("indexRange");
380
+ return {
381
+ media,
382
+ mediaRange,
383
+ index,
384
+ indexRange,
385
+ otherAttributes: reader.rest(),
386
+ invalidAttributes: reader.invalid(),
387
+ otherElements: readChildren_(context, element, position, new Map()),
388
+ ...spanOf_(element),
389
+ };
390
+ }
391
+ function readSegmentBase_(context, element, position) {
392
+ const reader = new AttributeReader(context, element, position);
393
+ const fields = readSegmentBaseFields_(reader);
394
+ const children = readSegmentChildren_(context, element, position, false, false);
395
+ return {
396
+ ...fields,
397
+ initialization: children.initialization,
398
+ representationIndex: children.representationIndex,
399
+ otherAttributes: reader.rest(),
400
+ invalidAttributes: reader.invalid(),
401
+ otherElements: children.otherElements,
402
+ ...spanOf_(element),
403
+ };
404
+ }
405
+ function readSegmentList_(context, element, position) {
406
+ const reader = new AttributeReader(context, element, position);
407
+ const fields = readSegmentBaseFields_(reader);
408
+ const duration = reader.unsigned("duration");
409
+ const startNumber = reader.unsigned("startNumber");
410
+ const endNumber = reader.unsigned("endNumber");
411
+ const xlink = reader.xlink(XLINK_NAMESPACE);
412
+ const children = readSegmentChildren_(context, element, position, true, true);
413
+ return {
414
+ ...fields,
415
+ duration,
416
+ startNumber,
417
+ endNumber,
418
+ xlink,
419
+ initialization: children.initialization,
420
+ representationIndex: children.representationIndex,
421
+ segmentTimeline: children.segmentTimeline,
422
+ bitstreamSwitching: children.bitstreamSwitching,
423
+ segmentUrls: children.segmentUrls,
424
+ otherAttributes: reader.rest(),
425
+ invalidAttributes: reader.invalid(),
426
+ otherElements: children.otherElements,
427
+ ...spanOf_(element),
428
+ };
429
+ }
430
+ function readSegmentTemplate_(context, element, position) {
431
+ const reader = new AttributeReader(context, element, position);
432
+ const fields = readSegmentBaseFields_(reader);
433
+ const duration = reader.unsigned("duration");
434
+ const startNumber = reader.unsigned("startNumber");
435
+ const endNumber = reader.unsigned("endNumber");
436
+ const media = reader.template("media");
437
+ const index = reader.template("index");
438
+ const initializationTemplate = reader.template("initialization", [
439
+ "Number",
440
+ "Time",
441
+ "SubNumber",
442
+ ]);
443
+ const bitstreamSwitchingTemplate = reader.template("bitstreamSwitching", [
444
+ "Number",
445
+ "Time",
446
+ "SubNumber",
447
+ ]);
448
+ const children = readSegmentChildren_(context, element, position, true, false);
449
+ return {
450
+ ...fields,
451
+ duration,
452
+ startNumber,
453
+ endNumber,
454
+ media,
455
+ index,
456
+ initializationTemplate,
457
+ bitstreamSwitchingTemplate,
458
+ initialization: children.initialization,
459
+ representationIndex: children.representationIndex,
460
+ segmentTimeline: children.segmentTimeline,
461
+ bitstreamSwitching: children.bitstreamSwitching,
462
+ otherAttributes: reader.rest(),
463
+ invalidAttributes: reader.invalid(),
464
+ otherElements: children.otherElements,
465
+ ...spanOf_(element),
466
+ };
467
+ }
468
+ /**
469
+ * Collects the segment information of one level; at most one of the three kinds
470
+ * may appear, once (§5.3.9.1).
471
+ */
472
+ class SegmentInformationCollector {
473
+ context_;
474
+ others_;
475
+ segmentBase = null;
476
+ segmentList = null;
477
+ segmentTemplate = null;
478
+ seen_ = false;
479
+ constructor(context_, others_) {
480
+ this.context_ = context_;
481
+ this.others_ = others_;
482
+ }
483
+ register(handlers) {
484
+ handlers.set("SegmentBase", (child, at) => {
485
+ if (this.admit_(child, at)) {
486
+ this.segmentBase = readSegmentBase_(this.context_, child, at);
487
+ }
488
+ });
489
+ handlers.set("SegmentList", (child, at) => {
490
+ if (this.admit_(child, at)) {
491
+ this.segmentList = readSegmentList_(this.context_, child, at);
492
+ }
493
+ });
494
+ handlers.set("SegmentTemplate", (child, at) => {
495
+ if (this.admit_(child, at)) {
496
+ this.segmentTemplate = readSegmentTemplate_(this.context_, child, at);
497
+ }
498
+ });
499
+ }
500
+ result() {
501
+ return {
502
+ segmentBase: this.segmentBase,
503
+ segmentList: this.segmentList,
504
+ segmentTemplate: this.segmentTemplate,
505
+ };
506
+ }
507
+ admit_(child, at) {
508
+ if (!this.seen_) {
509
+ this.seen_ = true;
510
+ return true;
511
+ }
512
+ this.context_.sink.violation("conflicting-segment-information", `${child.name.local} follows other segment information at the same level`, at);
513
+ this.others_.push(child);
514
+ return false;
515
+ }
516
+ }
517
+ function registerCommonElements_(context, handlers) {
518
+ const elements = {
519
+ framePackings: [],
520
+ audioChannelConfigurations: [],
521
+ contentProtections: [],
522
+ outputProtections: [],
523
+ essentialProperties: [],
524
+ supplementalProperties: [],
525
+ inbandEventStreams: [],
526
+ };
527
+ const descriptor = (list) => (child, at) => {
528
+ list.push(readDescriptor_(context, child, at));
529
+ };
530
+ handlers.set("FramePacking", descriptor(elements.framePackings));
531
+ handlers.set("AudioChannelConfiguration", descriptor(elements.audioChannelConfigurations));
532
+ handlers.set("ContentProtection", (child, at) => {
533
+ elements.contentProtections.push(readContentProtection_(context, child, at));
534
+ });
535
+ handlers.set("OutputProtection", descriptor(elements.outputProtections));
536
+ handlers.set("EssentialProperty", descriptor(elements.essentialProperties));
537
+ handlers.set("SupplementalProperty", descriptor(elements.supplementalProperties));
538
+ handlers.set("InbandEventStream", descriptor(elements.inbandEventStreams));
539
+ return elements;
540
+ }
541
+ function readCommonAttributes_(reader) {
542
+ return {
543
+ profiles: reader.commaList("profiles"),
544
+ width: reader.unsigned("width"),
545
+ height: reader.unsigned("height"),
546
+ sar: reader.ratio("sar"),
547
+ frameRate: reader.frameRate("frameRate"),
548
+ audioSamplingRate: reader.unsignedList("audioSamplingRate"),
549
+ mimeType: reader.string("mimeType"),
550
+ segmentProfiles: reader.commaList("segmentProfiles"),
551
+ codecs: reader.string("codecs"),
552
+ containerProfiles: reader.commaList("containerProfiles"),
553
+ maximumSAPPeriod: reader.double("maximumSAPPeriod"),
554
+ startWithSAP: reader.unsigned("startWithSAP"),
555
+ maxPlayoutRate: reader.double("maxPlayoutRate"),
556
+ codingDependency: reader.boolean("codingDependency"),
557
+ scanType: reader.enumeration("scanType", [
558
+ "progressive",
559
+ "interlaced",
560
+ "unknown",
561
+ ]),
562
+ selectionPriority: reader.unsigned("selectionPriority"),
563
+ tag: reader.string("tag"),
564
+ };
565
+ }
566
+ function holdsXmlWhitespace_(text) {
567
+ for (let index = 0; index < text.length; index++) {
568
+ if (isXmlWhitespace(text.charCodeAt(index))) {
569
+ return true;
570
+ }
571
+ }
572
+ return false;
573
+ }
574
+ function readRepresentation_(context, element, position) {
575
+ const reader = new AttributeReader(context, element, position);
576
+ let id = reader.required("id");
577
+ if (id !== null && (id === "" || holdsXmlWhitespace_(id))) {
578
+ reader.refuse("id", "invalid-attribute-value", `Representation @id "${id}" is empty or holds whitespace`);
579
+ id = null;
580
+ }
581
+ const bandwidth = reader.requiredUnsigned("bandwidth");
582
+ const qualityRanking = reader.unsigned("qualityRanking");
583
+ const dependencyId = reader.list("dependencyId") ?? [];
584
+ const associationId = reader.list("associationId") ?? [];
585
+ const associationType = reader.list("associationType") ?? [];
586
+ const mediaStreamStructureId = reader.list("mediaStreamStructureId") ?? [];
587
+ const common = readCommonAttributes_(reader);
588
+ const handlers = new Map();
589
+ const otherElements = [];
590
+ const elements = registerCommonElements_(context, handlers);
591
+ const segments = new SegmentInformationCollector(context, otherElements);
592
+ segments.register(handlers);
593
+ const baseUrls = [];
594
+ handlers.set("BaseURL", (child, at) => {
595
+ baseUrls.push(readBaseUrl_(context, child, at));
596
+ });
597
+ readChildren_(context, element, position, handlers, otherElements);
598
+ return {
599
+ id,
600
+ bandwidth,
601
+ qualityRanking,
602
+ dependencyId,
603
+ associationId,
604
+ associationType,
605
+ mediaStreamStructureId,
606
+ ...common,
607
+ ...elements,
608
+ ...segments.result(),
609
+ baseUrls,
610
+ otherAttributes: reader.rest(),
611
+ invalidAttributes: reader.invalid(),
612
+ otherElements,
613
+ ...spanOf_(element),
614
+ };
615
+ }
616
+ function readAdaptationSet_(context, element, position) {
617
+ const reader = new AttributeReader(context, element, position);
618
+ const xlink = reader.xlink(XLINK_NAMESPACE);
619
+ const id = reader.unsigned("id");
620
+ const group = reader.unsigned("group");
621
+ const lang = reader.string("lang");
622
+ const contentType = reader.string("contentType");
623
+ const par = reader.ratio("par");
624
+ const minBandwidth = reader.unsigned("minBandwidth");
625
+ const maxBandwidth = reader.unsigned("maxBandwidth");
626
+ const minWidth = reader.unsigned("minWidth");
627
+ const maxWidth = reader.unsigned("maxWidth");
628
+ const minHeight = reader.unsigned("minHeight");
629
+ const maxHeight = reader.unsigned("maxHeight");
630
+ const minFrameRate = reader.frameRate("minFrameRate");
631
+ const maxFrameRate = reader.frameRate("maxFrameRate");
632
+ const segmentAlignment = reader.conditionalUnsigned("segmentAlignment");
633
+ const subsegmentAlignment = reader.conditionalUnsigned("subsegmentAlignment");
634
+ const subsegmentStartsWithSAP = reader.unsigned("subsegmentStartsWithSAP");
635
+ const bitstreamSwitching = reader.boolean("bitstreamSwitching");
636
+ const common = readCommonAttributes_(reader);
637
+ const handlers = new Map();
638
+ const otherElements = [];
639
+ const elements = registerCommonElements_(context, handlers);
640
+ const segments = new SegmentInformationCollector(context, otherElements);
641
+ segments.register(handlers);
642
+ const lists = {
643
+ accessibility: [],
644
+ roles: [],
645
+ ratings: [],
646
+ viewpoints: [],
647
+ };
648
+ for (const [name, list] of [
649
+ ["Accessibility", lists.accessibility],
650
+ ["Role", lists.roles],
651
+ ["Rating", lists.ratings],
652
+ ["Viewpoint", lists.viewpoints],
653
+ ]) {
654
+ handlers.set(name, (child, at) => {
655
+ list.push(readDescriptor_(context, child, at));
656
+ });
657
+ }
658
+ const labels = [];
659
+ const groupLabels = [];
660
+ handlers.set("Label", (child, at) => {
661
+ labels.push(readLabel_(context, child, at));
662
+ });
663
+ handlers.set("GroupLabel", (child, at) => {
664
+ groupLabels.push(readLabel_(context, child, at));
665
+ });
666
+ const baseUrls = [];
667
+ handlers.set("BaseURL", (child, at) => {
668
+ baseUrls.push(readBaseUrl_(context, child, at));
669
+ });
670
+ const representations = [];
671
+ handlers.set("Representation", (child, at) => {
672
+ representations.push(readRepresentation_(context, child, at));
673
+ });
674
+ readChildren_(context, element, position, handlers, otherElements);
675
+ return {
676
+ xlink,
677
+ id,
678
+ group,
679
+ lang,
680
+ contentType,
681
+ par,
682
+ minBandwidth,
683
+ maxBandwidth,
684
+ minWidth,
685
+ maxWidth,
686
+ minHeight,
687
+ maxHeight,
688
+ minFrameRate,
689
+ maxFrameRate,
690
+ segmentAlignment,
691
+ subsegmentAlignment,
692
+ subsegmentStartsWithSAP,
693
+ bitstreamSwitching,
694
+ ...common,
695
+ ...elements,
696
+ ...segments.result(),
697
+ ...lists,
698
+ labels,
699
+ groupLabels,
700
+ baseUrls,
701
+ representations,
702
+ otherAttributes: reader.rest(),
703
+ invalidAttributes: reader.invalid(),
704
+ otherElements,
705
+ ...spanOf_(element),
706
+ };
707
+ }
708
+ function readPeriod_(context, element, position) {
709
+ const reader = new AttributeReader(context, element, position);
710
+ const xlink = reader.xlink(XLINK_NAMESPACE);
711
+ const id = reader.string("id");
712
+ const start = reader.duration("start");
713
+ const duration = reader.duration("duration");
714
+ const bitstreamSwitching = reader.boolean("bitstreamSwitching");
715
+ const handlers = new Map();
716
+ const otherElements = [];
717
+ const segments = new SegmentInformationCollector(context, otherElements);
718
+ segments.register(handlers);
719
+ const baseUrls = [];
720
+ handlers.set("BaseURL", (child, at) => {
721
+ baseUrls.push(readBaseUrl_(context, child, at));
722
+ });
723
+ let assetIdentifier = null;
724
+ handlers.set("AssetIdentifier", (child, at) => {
725
+ assetIdentifier ??= readDescriptor_(context, child, at);
726
+ });
727
+ const contentProtections = [];
728
+ handlers.set("ContentProtection", (child, at) => {
729
+ contentProtections.push(readContentProtection_(context, child, at));
730
+ });
731
+ const essentialProperties = [];
732
+ const supplementalProperties = [];
733
+ handlers.set("EssentialProperty", (child, at) => {
734
+ essentialProperties.push(readDescriptor_(context, child, at));
735
+ });
736
+ handlers.set("SupplementalProperty", (child, at) => {
737
+ supplementalProperties.push(readDescriptor_(context, child, at));
738
+ });
739
+ const adaptationSets = [];
740
+ handlers.set("AdaptationSet", (child, at) => {
741
+ adaptationSets.push(readAdaptationSet_(context, child, at));
742
+ });
743
+ readChildren_(context, element, position, handlers, otherElements);
744
+ const seen = new Set();
745
+ for (const adaptationSet of adaptationSets) {
746
+ for (const representation of adaptationSet.representations) {
747
+ if (representation.id === null) {
748
+ continue;
749
+ }
750
+ if (seen.has(representation.id)) {
751
+ context.sink.violation("duplicate-representation-id", `Two Representations of one Period have @id "${representation.id}"`, representation.span?.start ?? position);
752
+ }
753
+ seen.add(representation.id);
754
+ }
755
+ }
756
+ return {
757
+ xlink,
758
+ id,
759
+ start,
760
+ duration,
761
+ bitstreamSwitching,
762
+ ...segments.result(),
763
+ baseUrls,
764
+ assetIdentifier,
765
+ adaptationSets,
766
+ contentProtections,
767
+ essentialProperties,
768
+ supplementalProperties,
769
+ otherAttributes: reader.rest(),
770
+ invalidAttributes: reader.invalid(),
771
+ otherElements,
772
+ ...spanOf_(element),
773
+ };
774
+ }
775
+ function readProgramInformation_(context, element, position) {
776
+ const reader = new AttributeReader(context, element, position);
777
+ const lang = reader.string("lang");
778
+ const moreInformationUrl = reader.string("moreInformationURL");
779
+ const texts = {
780
+ title: null,
781
+ source: null,
782
+ copyright: null,
783
+ };
784
+ const otherElements = readChildren_(context, element, position, new Map([
785
+ [
786
+ "Title",
787
+ (child) => {
788
+ texts.title ??= textOf_(context, child);
789
+ },
790
+ ],
791
+ [
792
+ "Source",
793
+ (child) => {
794
+ texts.source ??= textOf_(context, child);
795
+ },
796
+ ],
797
+ [
798
+ "Copyright",
799
+ (child) => {
800
+ texts.copyright ??= textOf_(context, child);
801
+ },
802
+ ],
803
+ ]));
804
+ return {
805
+ lang,
806
+ moreInformationUrl,
807
+ ...texts,
808
+ otherAttributes: reader.rest(),
809
+ invalidAttributes: reader.invalid(),
810
+ otherElements,
811
+ ...spanOf_(element),
812
+ };
813
+ }
814
+ function readMpd_(context, element, position) {
815
+ const reader = new AttributeReader(context, element, position);
816
+ const id = reader.string("id");
817
+ const profiles = reader.required("profiles") === null ? [] : reader.commaList("profiles");
818
+ const type = reader.string("type") === null
819
+ ? "static"
820
+ : reader.enumeration("type", ["static", "dynamic"]);
821
+ const availabilityStartTime = reader.dateTime("availabilityStartTime");
822
+ if (type === "dynamic" && reader.string("availabilityStartTime") === null) {
823
+ context.sink.violation("missing-attribute", "A dynamic MPD has no @availabilityStartTime", position);
824
+ }
825
+ const availabilityEndTime = reader.dateTime("availabilityEndTime");
826
+ const publishTime = reader.dateTime("publishTime");
827
+ const mediaPresentationDuration = reader.duration("mediaPresentationDuration");
828
+ const minimumUpdatePeriod = reader.duration("minimumUpdatePeriod");
829
+ const minBufferTime = reader.required("minBufferTime") === null
830
+ ? null
831
+ : reader.duration("minBufferTime");
832
+ const timeShiftBufferDepth = reader.duration("timeShiftBufferDepth");
833
+ const suggestedPresentationDelay = reader.duration("suggestedPresentationDelay");
834
+ const maxSegmentDuration = reader.duration("maxSegmentDuration");
835
+ const maxSubsegmentDuration = reader.duration("maxSubsegmentDuration");
836
+ const programInformation = [];
837
+ const baseUrls = [];
838
+ const locations = [];
839
+ const periods = [];
840
+ const utcTimings = [];
841
+ const contentProtections = [];
842
+ const essentialProperties = [];
843
+ const supplementalProperties = [];
844
+ const otherElements = readChildren_(context, element, position, new Map([
845
+ [
846
+ "ProgramInformation",
847
+ (child, at) => {
848
+ programInformation.push(readProgramInformation_(context, child, at));
849
+ },
850
+ ],
851
+ [
852
+ "BaseURL",
853
+ (child, at) => {
854
+ baseUrls.push(readBaseUrl_(context, child, at));
855
+ },
856
+ ],
857
+ [
858
+ "Location",
859
+ (child) => {
860
+ locations.push(textOf_(context, child));
861
+ },
862
+ ],
863
+ [
864
+ "Period",
865
+ (child, at) => {
866
+ periods.push(readPeriod_(context, child, at));
867
+ },
868
+ ],
869
+ [
870
+ "UTCTiming",
871
+ (child, at) => {
872
+ utcTimings.push(readDescriptor_(context, child, at));
873
+ },
874
+ ],
875
+ [
876
+ "ContentProtection",
877
+ (child, at) => {
878
+ contentProtections.push(readContentProtection_(context, child, at));
879
+ },
880
+ ],
881
+ [
882
+ "EssentialProperty",
883
+ (child, at) => {
884
+ essentialProperties.push(readDescriptor_(context, child, at));
885
+ },
886
+ ],
887
+ [
888
+ "SupplementalProperty",
889
+ (child, at) => {
890
+ supplementalProperties.push(readDescriptor_(context, child, at));
891
+ },
892
+ ],
893
+ ]));
894
+ if (periods.length === 0) {
895
+ context.sink.violation("missing-element", "The MPD has no Period", position);
896
+ }
897
+ return {
898
+ id,
899
+ profiles,
900
+ type,
901
+ availabilityStartTime,
902
+ availabilityEndTime,
903
+ publishTime,
904
+ mediaPresentationDuration,
905
+ minimumUpdatePeriod,
906
+ minBufferTime,
907
+ timeShiftBufferDepth,
908
+ suggestedPresentationDelay,
909
+ maxSegmentDuration,
910
+ maxSubsegmentDuration,
911
+ programInformation,
912
+ baseUrls,
913
+ locations,
914
+ periods,
915
+ utcTimings,
916
+ contentProtections,
917
+ essentialProperties,
918
+ supplementalProperties,
919
+ otherAttributes: reader.rest(),
920
+ invalidAttributes: reader.invalid(),
921
+ otherElements,
922
+ ...spanOf_(element),
923
+ };
924
+ }
925
+ function spanOf_(element) {
926
+ return element.span === undefined ? {} : { span: element.span };
927
+ }
928
+ //# sourceMappingURL=parse-dash-mpd.js.map