shaka-player 4.7.13 → 4.8.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 (114) hide show
  1. package/AUTHORS +1 -0
  2. package/CONTRIBUTORS +1 -0
  3. package/LICENSE +54 -0
  4. package/README.md +22 -1
  5. package/dist/controls.css +1 -1
  6. package/dist/controls.css.map +1 -1
  7. package/dist/deps.js +34 -23
  8. package/dist/locales.js +44 -34
  9. package/dist/shaka-player.compiled.d.ts +194 -44
  10. package/dist/shaka-player.compiled.debug.d.ts +194 -44
  11. package/dist/shaka-player.compiled.debug.externs.js +474 -337
  12. package/dist/shaka-player.compiled.debug.js +949 -832
  13. package/dist/shaka-player.compiled.debug.map +4 -4
  14. package/dist/shaka-player.compiled.externs.js +474 -337
  15. package/dist/shaka-player.compiled.js +1237 -1171
  16. package/dist/shaka-player.compiled.map +4 -4
  17. package/dist/shaka-player.ui.d.ts +257 -47
  18. package/dist/shaka-player.ui.debug.d.ts +257 -47
  19. package/dist/shaka-player.ui.debug.externs.js +591 -376
  20. package/dist/shaka-player.ui.debug.js +1165 -965
  21. package/dist/shaka-player.ui.debug.map +4 -4
  22. package/dist/shaka-player.ui.externs.js +591 -376
  23. package/dist/shaka-player.ui.js +1521 -1404
  24. package/dist/shaka-player.ui.map +4 -4
  25. package/externs/device_sensor_event.js +18 -0
  26. package/externs/hisense.js +16 -0
  27. package/externs/playstation.js +31 -0
  28. package/externs/shaka/abr_manager.js +3 -1
  29. package/externs/shaka/ads.js +5 -0
  30. package/externs/shaka/codecs.js +13 -0
  31. package/externs/shaka/manifest.js +58 -6
  32. package/externs/shaka/net.js +13 -1
  33. package/externs/shaka/player.js +184 -13
  34. package/externs/shaka/text.js +19 -0
  35. package/externs/tizen.js +41 -0
  36. package/externs/webos.js +19 -0
  37. package/externs/xbox.js +43 -0
  38. package/lib/abr/simple_abr_manager.js +9 -1
  39. package/lib/ads/ad_manager.js +19 -2
  40. package/lib/ads/client_side_ad_manager.js +5 -3
  41. package/lib/ads/media_tailor_ad_manager.js +9 -0
  42. package/lib/cast/cast_receiver.js +0 -14
  43. package/lib/cast/cast_utils.js +1 -0
  44. package/lib/cea/mp4_cea_parser.js +6 -1
  45. package/lib/dash/content_protection.js +97 -53
  46. package/lib/dash/dash_parser.js +230 -176
  47. package/lib/dash/mpd_utils.js +72 -65
  48. package/lib/dash/segment_base.js +27 -21
  49. package/lib/dash/segment_list.js +18 -16
  50. package/lib/dash/segment_template.js +26 -21
  51. package/lib/hls/hls_parser.js +358 -164
  52. package/lib/media/buffering_observer.js +25 -1
  53. package/lib/media/drm_engine.js +91 -71
  54. package/lib/media/manifest_filterer.js +220 -0
  55. package/lib/media/media_source_engine.js +34 -5
  56. package/lib/media/preload_manager.js +827 -0
  57. package/lib/media/presentation_timeline.js +11 -0
  58. package/lib/media/segment_index.js +40 -30
  59. package/lib/media/segment_prefetch.js +140 -43
  60. package/lib/media/segment_reference.js +31 -8
  61. package/lib/media/segment_utils.js +208 -25
  62. package/lib/media/streaming_engine.js +319 -63
  63. package/lib/mss/content_protection.js +31 -25
  64. package/lib/mss/mss_parser.js +74 -55
  65. package/lib/net/networking_engine.js +49 -4
  66. package/lib/offline/storage.js +3 -3
  67. package/lib/player.js +882 -673
  68. package/lib/polyfill/media_capabilities.js +16 -1
  69. package/lib/text/simple_text_displayer.js +9 -0
  70. package/lib/text/stub_text_displayer.js +7 -0
  71. package/lib/text/text_engine.js +16 -3
  72. package/lib/text/ttml_text_parser.js +116 -114
  73. package/lib/text/ui_text_displayer.js +28 -11
  74. package/lib/text/vtt_text_parser.js +54 -115
  75. package/lib/transmuxer/opus.js +91 -0
  76. package/lib/transmuxer/ts_transmuxer.js +115 -0
  77. package/lib/util/cmcd_manager.js +152 -3
  78. package/lib/util/dom_utils.js +11 -0
  79. package/lib/util/error.js +5 -0
  80. package/lib/util/fake_event.js +2 -0
  81. package/lib/util/manifest_parser_utils.js +97 -3
  82. package/lib/util/mp4_box_parsers.js +346 -17
  83. package/lib/util/mp4_generator.js +23 -0
  84. package/lib/util/periods.js +19 -0
  85. package/lib/util/platform.js +161 -9
  86. package/lib/util/player_configuration.js +61 -3
  87. package/lib/util/stats.js +16 -0
  88. package/lib/util/stream_utils.js +16 -6
  89. package/lib/util/string_utils.js +36 -0
  90. package/lib/util/tXml.js +814 -0
  91. package/lib/util/ts_parser.js +50 -0
  92. package/lib/util/xml_utils.js +10 -338
  93. package/package.json +2 -2
  94. package/shaka-player.uncompiled.js +8 -0
  95. package/ui/controls.js +187 -5
  96. package/ui/enums.js +2 -0
  97. package/ui/externs/ui.js +30 -3
  98. package/ui/gl_matrix/matrix_4x4.js +568 -0
  99. package/ui/gl_matrix/matrix_quaternion.js +45 -0
  100. package/ui/less/containers.less +11 -0
  101. package/ui/locales/en-GB.json +2 -0
  102. package/ui/locales/en.json +2 -0
  103. package/ui/locales/es-419.json +2 -0
  104. package/ui/locales/es.json +2 -0
  105. package/ui/locales/source.json +8 -0
  106. package/ui/recenter_vr.js +126 -0
  107. package/ui/resolution_selection.js +25 -5
  108. package/ui/statistics_button.js +12 -0
  109. package/ui/text_selection.js +1 -1
  110. package/ui/toggle_stereoscopic.js +127 -0
  111. package/ui/ui.js +74 -38
  112. package/ui/vr_manager.js +557 -0
  113. package/ui/vr_utils.js +232 -0
  114. package/ui/vr_webgl.js +697 -0
@@ -0,0 +1,814 @@
1
+ /*! @license
2
+ * tXml
3
+ * Copyright 2015 Tobias Nickel
4
+ * SPDX-License-Identifier: MIT
5
+ */
6
+
7
+ goog.provide('shaka.util.TXml');
8
+
9
+ goog.require('shaka.util.StringUtils');
10
+ goog.require('shaka.log');
11
+
12
+ /**
13
+ * This code is a modified version of the tXml library.
14
+ *
15
+ * @author: Tobias Nickel
16
+ * created: 06.04.2015
17
+ * https://github.com/TobiasNickel/tXml
18
+ */
19
+
20
+ /**
21
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
22
+ * of this software and associated documentation files (the "Software"), to deal
23
+ * in the Software without restriction, including without limitation the rights
24
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25
+ * copies of the Software, and to permit persons to whom the Software is
26
+ * furnished to do so, subject to the following conditions:
27
+ *
28
+ * The above copyright notice and this permission notice shall be included in
29
+ * all copies or substantial portions of the Software.
30
+ *
31
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37
+ * SOFTWARE.
38
+ */
39
+
40
+
41
+ shaka.util.TXml = class {
42
+ /**
43
+ * Parse some data
44
+ * @param {BufferSource} data
45
+ * @param {string=} expectedRootElemName
46
+ * @return {shaka.extern.xml.Node | null}
47
+ */
48
+ static parseXml(data, expectedRootElemName) {
49
+ const xmlString = shaka.util.StringUtils.fromBytesAutoDetect(data);
50
+ return shaka.util.TXml.parseXmlString(xmlString, expectedRootElemName);
51
+ }
52
+
53
+ /**
54
+ * Parse some data
55
+ * @param {string} xmlString
56
+ * @param {string=} expectedRootElemName
57
+ * @return {shaka.extern.xml.Node | null}
58
+ */
59
+ static parseXmlString(xmlString, expectedRootElemName) {
60
+ const result = shaka.util.TXml.parse(xmlString);
61
+ if (!expectedRootElemName && result.length) {
62
+ return result[0];
63
+ }
64
+ const rootNode = result.find((n) => n.tagName === expectedRootElemName);
65
+ if (rootNode) {
66
+ return rootNode;
67
+ }
68
+
69
+ shaka.log.error('parseXml root element not found!');
70
+ return null;
71
+ }
72
+
73
+ /**
74
+ * Parse some data
75
+ * @param {string} schema
76
+ * @return {string}
77
+ */
78
+ static getKnownNameSpace(schema) {
79
+ if (shaka.util.TXml.knownNameSpaces_.has(schema)) {
80
+ return shaka.util.TXml.knownNameSpaces_.get(schema);
81
+ }
82
+ return '';
83
+ }
84
+
85
+ /**
86
+ * Parse some data
87
+ * @param {string} schema
88
+ * @param {string} NS
89
+ */
90
+ static setKnownNameSpace(schema, NS) {
91
+ shaka.util.TXml.knownNameSpaces_.set(schema, NS);
92
+ }
93
+
94
+ /**
95
+ * parseXML / html into a DOM Object,
96
+ * with no validation and some failure tolerance
97
+ * @param {string} S your XML to parse
98
+ * @return {Array.<shaka.extern.xml.Node>}
99
+ */
100
+ static parse(S) {
101
+ let pos = 0;
102
+
103
+ const openBracket = '<';
104
+ const openBracketCC = '<'.charCodeAt(0);
105
+ const closeBracket = '>';
106
+ const closeBracketCC = '>'.charCodeAt(0);
107
+ const minusCC = '-'.charCodeAt(0);
108
+ const slashCC = '/'.charCodeAt(0);
109
+ const exclamationCC = '!'.charCodeAt(0);
110
+ const singleQuoteCC = '\''.charCodeAt(0);
111
+ const doubleQuoteCC = '"'.charCodeAt(0);
112
+ const openCornerBracketCC = '['.charCodeAt(0);
113
+
114
+ /**
115
+ * parsing a list of entries
116
+ */
117
+ function parseChildren(tagName, preserveSpace = false) {
118
+ /** @type {Array.<shaka.extern.xml.Node | string>} */
119
+ const children = [];
120
+ while (S[pos]) {
121
+ if (S.charCodeAt(pos) == openBracketCC) {
122
+ if (S.charCodeAt(pos + 1) === slashCC) {
123
+ const closeStart = pos + 2;
124
+ pos = S.indexOf(closeBracket, pos);
125
+
126
+ const closeTag = S.substring(closeStart, pos);
127
+ let indexOfCloseTag = closeTag.indexOf(tagName);
128
+ if (indexOfCloseTag == -1) {
129
+ // handle VTT closing tags like <c.lime></c>
130
+ const indexOfPeriod = tagName.indexOf('.');
131
+ if (indexOfPeriod > 0) {
132
+ const shortTag = tagName.substring(0, indexOfPeriod);
133
+ indexOfCloseTag = closeTag.indexOf(shortTag);
134
+ }
135
+ }
136
+ // eslint-disable-next-line no-restricted-syntax
137
+ if (indexOfCloseTag == -1) {
138
+ const parsedText = S.substring(0, pos).split('\n');
139
+ throw new Error(
140
+ 'Unexpected close tag\nLine: ' + (parsedText.length - 1) +
141
+ '\nColumn: ' +
142
+ (parsedText[parsedText.length - 1].length + 1) +
143
+ '\nChar: ' + S[pos],
144
+ );
145
+ }
146
+
147
+ if (pos + 1) {
148
+ pos += 1;
149
+ }
150
+
151
+ return children;
152
+ } else if (S.charCodeAt(pos + 1) === exclamationCC) {
153
+ if (S.charCodeAt(pos + 2) == minusCC) {
154
+ while (pos !== -1 && !(S.charCodeAt(pos) === closeBracketCC &&
155
+ S.charCodeAt(pos - 1) == minusCC &&
156
+ S.charCodeAt(pos - 2) == minusCC &&
157
+ pos != -1)) {
158
+ pos = S.indexOf(closeBracket, pos + 1);
159
+ }
160
+ if (pos === -1) {
161
+ pos = S.length;
162
+ }
163
+ } else if (
164
+ S.charCodeAt(pos + 2) === openCornerBracketCC &&
165
+ S.charCodeAt(pos + 8) === openCornerBracketCC &&
166
+ S.substr(pos + 3, 5).toLowerCase() === 'cdata'
167
+ ) {
168
+ // cdata
169
+ const cdataEndIndex = S.indexOf(']]>', pos);
170
+ if (cdataEndIndex == -1) {
171
+ children.push(S.substr(pos + 9));
172
+ pos = S.length;
173
+ } else {
174
+ children.push(S.substring(pos + 9, cdataEndIndex));
175
+ pos = cdataEndIndex + 3;
176
+ }
177
+ continue;
178
+ }
179
+ pos++;
180
+ continue;
181
+ }
182
+ const node = parseNode(preserveSpace);
183
+ children.push(node);
184
+ if (typeof node === 'string') {
185
+ return children;
186
+ }
187
+ if (node.tagName[0] === '?' && node.children) {
188
+ children.push(...node.children);
189
+ node.children = [];
190
+ }
191
+ } else {
192
+ const text = parseText();
193
+ if (preserveSpace) {
194
+ if (text.length > 0) {
195
+ children.push(text);
196
+ }
197
+ } else if (children.length &&
198
+ text.length == 1 && text[0] == '\n') {
199
+ children.push(text);
200
+ } else {
201
+ const trimmed = text.trim();
202
+ if (trimmed.length > 0) {
203
+ children.push(text);
204
+ }
205
+ }
206
+ pos++;
207
+ }
208
+ }
209
+ return children;
210
+ }
211
+
212
+ /**
213
+ * returns the text outside of texts until the first '<'
214
+ */
215
+ function parseText() {
216
+ const start = pos;
217
+ pos = S.indexOf(openBracket, pos) - 1;
218
+ if (pos === -2) {
219
+ pos = S.length;
220
+ }
221
+ return S.slice(start, pos + 1);
222
+ }
223
+ /**
224
+ * returns text until the first nonAlphabetic letter
225
+ */
226
+ const nameSpacer = '\r\n\t>/= ';
227
+
228
+ /**
229
+ * Parse text in current context
230
+ * @return {string}
231
+ */
232
+ function parseName() {
233
+ const start = pos;
234
+ while (nameSpacer.indexOf(S[pos]) === -1 && S[pos]) {
235
+ pos++;
236
+ }
237
+ return S.slice(start, pos);
238
+ }
239
+
240
+ /**
241
+ * Parse text in current context
242
+ * @param {boolean} preserveSpace Preserve the space between nodes
243
+ * @return {shaka.extern.xml.Node | string}
244
+ */
245
+ function parseNode(preserveSpace) {
246
+ pos++;
247
+ const tagName = parseName();
248
+ const attributes = {};
249
+ let children = [];
250
+
251
+ // parsing attributes
252
+ while (S.charCodeAt(pos) !== closeBracketCC && S[pos]) {
253
+ const c = S.charCodeAt(pos);
254
+ // abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
255
+ if ((c > 64 && c < 91) || (c > 96 && c < 123)) {
256
+ const name = parseName();
257
+ // search beginning of the string
258
+ let code = S.charCodeAt(pos);
259
+ while (code && code !== singleQuoteCC && code !== doubleQuoteCC &&
260
+ !((code > 64 && code < 91) || (code > 96 && code < 123)) &&
261
+ code !== closeBracketCC) {
262
+ pos++;
263
+ code = S.charCodeAt(pos);
264
+ }
265
+ let value = parseString();
266
+ if (code === singleQuoteCC || code === doubleQuoteCC) {
267
+ if (pos === -1) {
268
+ /** @type {shaka.extern.xml.Node} */
269
+ const node = {
270
+ tagName,
271
+ attributes,
272
+ children,
273
+ parent: null,
274
+ };
275
+ for (let i = 0; i < children.length; i++) {
276
+ if (typeof children[i] !== 'string') {
277
+ children[i].parent = node;
278
+ }
279
+ }
280
+ return node;
281
+ }
282
+ } else {
283
+ value = null;
284
+ pos--;
285
+ }
286
+ if (name.startsWith('xmlns:')) {
287
+ const segs = name.split(':');
288
+ shaka.util.TXml.setKnownNameSpace(
289
+ /** @type {string} */ (value), segs[1]);
290
+ }
291
+ if (tagName === 'tt' &&
292
+ name === 'xml:space' &&
293
+ value === 'preserve') {
294
+ preserveSpace = true;
295
+ }
296
+ attributes[name] = value;
297
+ }
298
+ pos++;
299
+ }
300
+
301
+ if (S.charCodeAt(pos - 1) !== slashCC) {
302
+ pos++;
303
+ const contents = parseChildren(tagName, preserveSpace);
304
+ children = contents;
305
+ } else {
306
+ pos++;
307
+ }
308
+ /** @type {shaka.extern.xml.Node} */
309
+ const node = {
310
+ tagName,
311
+ attributes,
312
+ children,
313
+ parent: null,
314
+ };
315
+ const childrenLength = children.length;
316
+ for (let i = 0; i < childrenLength; i++) {
317
+ const childrenValue = children[i];
318
+ if (typeof childrenValue !== 'string') {
319
+ childrenValue.parent = node;
320
+ } else if (i == childrenLength - 1 && childrenValue == '\n') {
321
+ children.pop();
322
+ }
323
+ }
324
+ return node;
325
+ }
326
+
327
+ /**
328
+ * Parse string in current context
329
+ * @return {string}
330
+ */
331
+ function parseString() {
332
+ const startChar = S[pos];
333
+ const startpos = pos + 1;
334
+ pos = S.indexOf(startChar, startpos);
335
+ return S.slice(startpos, pos);
336
+ }
337
+
338
+ return parseChildren('');
339
+ }
340
+
341
+ /**
342
+ * Verifies if the element is a TXml node.
343
+ * @param {!shaka.extern.xml.Node} elem The XML element.
344
+ * @return {!boolean} Is the element a TXml node
345
+ */
346
+ static isNode(elem) {
347
+ return !!(elem.tagName);
348
+ }
349
+
350
+ /**
351
+ * Checks if a node is of type text.
352
+ * @param {!shaka.extern.xml.Node | string} elem The XML element.
353
+ * @return {boolean} True if it is a text node.
354
+ */
355
+ static isText(elem) {
356
+ return typeof elem === 'string';
357
+ }
358
+
359
+ /**
360
+ * gets child XML elements.
361
+ * @param {!shaka.extern.xml.Node} elem The parent XML element.
362
+ * @return {!Array.<!shaka.extern.xml.Node>} The child XML elements.
363
+ */
364
+ static getChildNodes(elem) {
365
+ const found = [];
366
+ if (!elem.children) {
367
+ return [];
368
+ }
369
+ for (const child of elem.children) {
370
+ if (typeof child !== 'string') {
371
+ found.push(child);
372
+ }
373
+ }
374
+ return found;
375
+ }
376
+
377
+ /**
378
+ * Finds child XML elements.
379
+ * @param {!shaka.extern.xml.Node} elem The parent XML element.
380
+ * @param {string} name The child XML element's tag name.
381
+ * @return {!Array.<!shaka.extern.xml.Node>} The child XML elements.
382
+ */
383
+ static findChildren(elem, name) {
384
+ const found = [];
385
+ if (!elem.children) {
386
+ return [];
387
+ }
388
+ for (const child of elem.children) {
389
+ if (child.tagName === name) {
390
+ found.push(child);
391
+ }
392
+ }
393
+ return found;
394
+ }
395
+
396
+ /**
397
+ * Gets inner text.
398
+ * @param {!shaka.extern.xml.Node | string} node The XML element.
399
+ * @return {?string} The text contents, or null if there are none.
400
+ */
401
+ static getTextContents(node) {
402
+ const StringUtils = shaka.util.StringUtils;
403
+ if (typeof node === 'string') {
404
+ return StringUtils.htmlUnescape(node);
405
+ }
406
+ const textContent = node.children.reduce(
407
+ (acc, curr) => (typeof curr === 'string' ? acc + curr : acc),
408
+ '',
409
+ );
410
+ if (textContent === '') {
411
+ return null;
412
+ }
413
+ return StringUtils.htmlUnescape(textContent);
414
+ }
415
+
416
+ /**
417
+ * Gets the text contents of a node.
418
+ * @param {!shaka.extern.xml.Node} node The XML element.
419
+ * @return {?string} The text contents, or null if there are none.
420
+ */
421
+ static getContents(node) {
422
+ if (!Array.from(node.children).every(
423
+ (n) => typeof n === 'string' )) {
424
+ return null;
425
+ }
426
+
427
+ // Read merged text content from all text nodes.
428
+ let text = shaka.util.TXml.getTextContents(node);
429
+ if (text) {
430
+ text = text.trim();
431
+ }
432
+ return text;
433
+ }
434
+
435
+ /**
436
+ * Finds child XML elements recursively.
437
+ * @param {!shaka.extern.xml.Node} elem The parent XML element.
438
+ * @param {string} name The child XML element's tag name.
439
+ * @param {!Array.<!shaka.extern.xml.Node>} found accumulator for found nodes
440
+ * @return {!Array.<!shaka.extern.xml.Node>} The child XML elements.
441
+ */
442
+ static getElementsByTagName(elem, name, found = []) {
443
+ if (elem.tagName === name) {
444
+ found.push(elem);
445
+ }
446
+ if (elem.children) {
447
+ for (const child of elem.children) {
448
+ shaka.util.TXml.getElementsByTagName(child, name, found);
449
+ }
450
+ }
451
+ return found;
452
+ }
453
+
454
+ /**
455
+ * Finds a child XML element.
456
+ * @param {!shaka.extern.xml.Node} elem The parent XML element.
457
+ * @param {string} name The child XML element's tag name.
458
+ * @return {shaka.extern.xml.Node | null} The child XML element,
459
+ * or null if a child XML element
460
+ * does not exist with the given tag name OR if there exists more than one
461
+ * child XML element with the given tag name.
462
+ */
463
+ static findChild(elem, name) {
464
+ const children = shaka.util.TXml.findChildren(elem, name);
465
+ if (children.length != 1) {
466
+ return null;
467
+ }
468
+ return children[0];
469
+ }
470
+
471
+ /**
472
+ * Finds a namespace-qualified child XML element.
473
+ * @param {!shaka.extern.xml.Node} elem The parent XML element.
474
+ * @param {string} ns The child XML element's namespace URI.
475
+ * @param {string} name The child XML element's local name.
476
+ * @return {shaka.extern.xml.Node | null} The child XML element, or null
477
+ * if a child XML element
478
+ * does not exist with the given tag name OR if there exists more than one
479
+ * child XML element with the given tag name.
480
+ */
481
+ static findChildNS(elem, ns, name) {
482
+ const children = shaka.util.TXml.findChildrenNS(elem, ns, name);
483
+ if (children.length != 1) {
484
+ return null;
485
+ }
486
+ return children[0];
487
+ }
488
+
489
+ /**
490
+ * Parses an attribute by its name.
491
+ * @param {!shaka.extern.xml.Node} elem The XML element.
492
+ * @param {string} name The attribute name.
493
+ * @param {function(string): (T|null)} parseFunction A function that parses
494
+ * the attribute.
495
+ * @param {(T|null)=} defaultValue The attribute's default value, if not
496
+ * specified, the attibute's default value is null.
497
+ * @return {(T|null)} The parsed attribute on success, or the attribute's
498
+ * default value if the attribute does not exist or could not be parsed.
499
+ * @template T
500
+ */
501
+ static parseAttr(elem, name, parseFunction, defaultValue = null) {
502
+ let parsedValue = null;
503
+
504
+ const value = elem.attributes[name];
505
+ if (value != null) {
506
+ parsedValue = parseFunction(value);
507
+ }
508
+ return parsedValue == null ? defaultValue : parsedValue;
509
+ }
510
+
511
+ /**
512
+ * Gets a namespace-qualified attribute.
513
+ * @param {!shaka.extern.xml.Node} elem The element to get from.
514
+ * @param {string} ns The namespace URI.
515
+ * @param {string} name The local name of the attribute.
516
+ * @return {?string} The attribute's value, or null if not present.
517
+ */
518
+ static getAttributeNS(elem, ns, name) {
519
+ const schemaNS = shaka.util.TXml.getKnownNameSpace(ns);
520
+ // Think this is equivalent
521
+ const attribute = elem.attributes[`${schemaNS}:${name}`];
522
+ return attribute || null;
523
+ }
524
+
525
+ /**
526
+ * Finds namespace-qualified child XML elements.
527
+ * @param {!shaka.extern.xml.Node} elem The parent XML element.
528
+ * @param {string} ns The child XML element's namespace URI.
529
+ * @param {string} name The child XML element's local name.
530
+ * @return {!Array.<!shaka.extern.xml.Node>} The child XML elements.
531
+ */
532
+ static findChildrenNS(elem, ns, name) {
533
+ const schemaNS = shaka.util.TXml.getKnownNameSpace(ns);
534
+ const found = [];
535
+ if (elem.children) {
536
+ for (const child of elem.children) {
537
+ if (child && child.tagName === `${schemaNS}:${name}`) {
538
+ found.push(child);
539
+ }
540
+ }
541
+ }
542
+ return found;
543
+ }
544
+
545
+ /**
546
+ * Gets a namespace-qualified attribute.
547
+ * @param {!shaka.extern.xml.Node} elem The element to get from.
548
+ * @param {!Array.<string>} nsList The lis of namespace URIs.
549
+ * @param {string} name The local name of the attribute.
550
+ * @return {?string} The attribute's value, or null if not present.
551
+ */
552
+ static getAttributeNSList(elem, nsList, name) {
553
+ for (const ns of nsList) {
554
+ const attr = shaka.util.TXml.getAttributeNS(
555
+ elem, ns, name,
556
+ );
557
+ if (attr) {
558
+ return attr;
559
+ }
560
+ }
561
+ return null;
562
+ }
563
+
564
+
565
+ /**
566
+ * Parses an XML date string.
567
+ * @param {string} dateString
568
+ * @return {?number} The parsed date in seconds on success; otherwise, return
569
+ * null.
570
+ */
571
+ static parseDate(dateString) {
572
+ if (!dateString) {
573
+ return null;
574
+ }
575
+
576
+ // Times in the manifest should be in UTC. If they don't specify a timezone,
577
+ // Date.parse() will use the local timezone instead of UTC. So manually add
578
+ // the timezone if missing ('Z' indicates the UTC timezone).
579
+ // Format: YYYY-MM-DDThh:mm:ss.ssssss
580
+ if (/^\d+-\d+-\d+T\d+:\d+:\d+(\.\d+)?$/.test(dateString)) {
581
+ dateString += 'Z';
582
+ }
583
+
584
+ const result = Date.parse(dateString);
585
+ return isNaN(result) ? null : (result / 1000.0);
586
+ }
587
+
588
+
589
+ /**
590
+ * Parses an XML duration string.
591
+ * Negative values are not supported. Years and months are treated as exactly
592
+ * 365 and 30 days respectively.
593
+ * @param {string} durationString The duration string, e.g., "PT1H3M43.2S",
594
+ * which means 1 hour, 3 minutes, and 43.2 seconds.
595
+ * @return {?number} The parsed duration in seconds on success; otherwise,
596
+ * return null.
597
+ * @see {@link http://www.datypic.com/sc/xsd/t-xsd_duration.html}
598
+ */
599
+ static parseDuration(durationString) {
600
+ if (!durationString) {
601
+ return null;
602
+ }
603
+
604
+ const re = '^P(?:([0-9]*)Y)?(?:([0-9]*)M)?(?:([0-9]*)D)?' +
605
+ '(?:T(?:([0-9]*)H)?(?:([0-9]*)M)?(?:([0-9.]*)S)?)?$';
606
+ const matches = new RegExp(re).exec(durationString);
607
+
608
+ if (!matches) {
609
+ shaka.log.warning('Invalid duration string:', durationString);
610
+ return null;
611
+ }
612
+
613
+ // Note: Number(null) == 0 but Number(undefined) == NaN.
614
+ const years = Number(matches[1] || null);
615
+ const months = Number(matches[2] || null);
616
+ const days = Number(matches[3] || null);
617
+ const hours = Number(matches[4] || null);
618
+ const minutes = Number(matches[5] || null);
619
+ const seconds = Number(matches[6] || null);
620
+
621
+ // Assume a year always has 365 days and a month always has 30 days.
622
+ const d = (60 * 60 * 24 * 365) * years +
623
+ (60 * 60 * 24 * 30) * months +
624
+ (60 * 60 * 24) * days +
625
+ (60 * 60) * hours +
626
+ 60 * minutes +
627
+ seconds;
628
+ return isFinite(d) ? d : null;
629
+ }
630
+
631
+
632
+ /**
633
+ * Parses a range string.
634
+ * @param {string} rangeString The range string, e.g., "101-9213".
635
+ * @return {?{start: number, end: number}} The parsed range on success;
636
+ * otherwise, return null.
637
+ */
638
+ static parseRange(rangeString) {
639
+ const matches = /([0-9]+)-([0-9]+)/.exec(rangeString);
640
+
641
+ if (!matches) {
642
+ return null;
643
+ }
644
+
645
+ const start = Number(matches[1]);
646
+ if (!isFinite(start)) {
647
+ return null;
648
+ }
649
+
650
+ const end = Number(matches[2]);
651
+ if (!isFinite(end)) {
652
+ return null;
653
+ }
654
+
655
+ return {start: start, end: end};
656
+ }
657
+
658
+
659
+ /**
660
+ * Parses an integer.
661
+ * @param {string} intString The integer string.
662
+ * @return {?number} The parsed integer on success; otherwise, return null.
663
+ */
664
+ static parseInt(intString) {
665
+ const n = Number(intString);
666
+ return (n % 1 === 0) ? n : null;
667
+ }
668
+
669
+
670
+ /**
671
+ * Parses a positive integer.
672
+ * @param {string} intString The integer string.
673
+ * @return {?number} The parsed positive integer on success; otherwise,
674
+ * return null.
675
+ */
676
+ static parsePositiveInt(intString) {
677
+ const n = Number(intString);
678
+ return (n % 1 === 0) && (n > 0) ? n : null;
679
+ }
680
+
681
+
682
+ /**
683
+ * Parses a non-negative integer.
684
+ * @param {string} intString The integer string.
685
+ * @return {?number} The parsed non-negative integer on success; otherwise,
686
+ * return null.
687
+ */
688
+ static parseNonNegativeInt(intString) {
689
+ const n = Number(intString);
690
+ return (n % 1 === 0) && (n >= 0) ? n : null;
691
+ }
692
+
693
+
694
+ /**
695
+ * Parses a floating point number.
696
+ * @param {string} floatString The floating point number string.
697
+ * @return {?number} The parsed floating point number on success; otherwise,
698
+ * return null. May return -Infinity or Infinity.
699
+ */
700
+ static parseFloat(floatString) {
701
+ const n = Number(floatString);
702
+ return !isNaN(n) ? n : null;
703
+ }
704
+
705
+
706
+ /**
707
+ * Parses a boolean.
708
+ * @param {string} booleanString The boolean string.
709
+ * @return {boolean} The boolean
710
+ */
711
+ static parseBoolean(booleanString) {
712
+ if (!booleanString) {
713
+ return false;
714
+ }
715
+ return booleanString.toLowerCase() === 'true';
716
+ }
717
+
718
+
719
+ /**
720
+ * Evaluate a division expressed as a string.
721
+ * @param {string} exprString
722
+ * The expression to evaluate, e.g. "200/2". Can also be a single number.
723
+ * @return {?number} The evaluated expression as floating point number on
724
+ * success; otherwise return null.
725
+ */
726
+ static evalDivision(exprString) {
727
+ let res;
728
+ let n;
729
+ if ((res = exprString.match(/^(\d+)\/(\d+)$/))) {
730
+ n = Number(res[1]) / Number(res[2]);
731
+ } else {
732
+ n = Number(exprString);
733
+ }
734
+ return !isNaN(n) ? n : null;
735
+ }
736
+
737
+
738
+ /**
739
+ * Parse xPath strings for segments and id targets.
740
+ * @param {string} exprString
741
+ * @return {!Array<!shaka.util.TXml.PathNode>}
742
+ */
743
+ static parseXpath(exprString) {
744
+ const returnPaths = [];
745
+ // Split string by paths but ignore '/' in quotes
746
+ const paths = exprString
747
+ .split(/\/+(?=(?:[^'"]*['"][^'"]*['"])*[^'"]*$)/);
748
+ for (const path of paths) {
749
+ const nodeName = path.match(/\b([A-Z])\w+/);
750
+
751
+ // We only want the id attribute in which case
752
+ // /'(.*?)'/ will suffice to get it.
753
+ const idAttr = path.match(/(@id='(.*?)')/);
754
+ if (nodeName) {
755
+ returnPaths.push({
756
+ name: nodeName[0],
757
+ id: idAttr ?
758
+ idAttr[0].match(/'(.*?)'/)[0].replace(/'/gm, '') : null,
759
+ });
760
+ }
761
+ }
762
+ return returnPaths;
763
+ }
764
+
765
+
766
+ /**
767
+ * Converts a tXml node to DOM element.
768
+ * @param {shaka.extern.xml.Node} node
769
+ * @param {boolean=} doParents
770
+ * @param {boolean=} doChildren
771
+ * @return {!Element}
772
+ */
773
+ static txmlNodeToDomElement(node, doParents = true, doChildren = true) {
774
+ const TXml = shaka.util.TXml;
775
+ const element = document.createElement(node.tagName);
776
+
777
+ for (const k in node.attributes) {
778
+ const v = node.attributes[k];
779
+ element.setAttribute(k, v);
780
+ }
781
+
782
+ if (doParents && node.parent) {
783
+ const parentElement = TXml.txmlNodeToDomElement(
784
+ node.parent, /* doParents= */ true, /* doChildren= */ false);
785
+ parentElement.appendChild(element);
786
+ }
787
+
788
+ if (doChildren) {
789
+ for (const child of node.children) {
790
+ let childElement;
791
+ if (typeof child == 'string') {
792
+ childElement = new Text(child);
793
+ } else {
794
+ childElement = TXml.txmlNodeToDomElement(
795
+ child, /* doParents= */ false, /* doChildren= */ true);
796
+ }
797
+ element.appendChild(childElement);
798
+ }
799
+ }
800
+
801
+ return element;
802
+ }
803
+ };
804
+
805
+ shaka.util.TXml.knownNameSpaces_ = new Map([]);
806
+
807
+
808
+ /**
809
+ * @typedef {{
810
+ * name: string,
811
+ * id: ?string
812
+ * }}
813
+ */
814
+ shaka.util.TXml.PathNode;