Skip to content

Commit

Permalink
feat!: Remove DOM Parser (#6063)
Browse files Browse the repository at this point in the history
## Background: 
The native DOM Parser can perform poorly on some older devices, this
approach is faster on newer devices but is considerably better on older
devices.
This PR replaces the usage of the DOM Parser for DASH, MSS, VTT and
TTML.

The draw back of this approach that it does not include any validation
at the cost of better performance.
  • Loading branch information
dave-nicholas authored Jan 22, 2024
1 parent 8f0db8e commit 7116a34
Show file tree
Hide file tree
Showing 29 changed files with 1,752 additions and 1,542 deletions.
4 changes: 2 additions & 2 deletions build/conformance.textproto
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ requirement: {
value: "DOMParser.prototype.parseFromString"
error_message:
"Using \"DOMParser.parseFromString\" directly is not allowed; "
"use shaka.util.XmlUtils.parseXmlString instead."
whitelist_regexp: "lib/util/xml_utils.js"
"use shaka.util.TXml.parseXmlString instead."
whitelist_regexp: "lib/util/tXml.js"
whitelist_regexp: "test/"
}

Expand Down
2 changes: 1 addition & 1 deletion build/types/core
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
+../../lib/util/timer.js
+../../lib/util/ts_parser.js
+../../lib/util/uint8array_utils.js
+../../lib/util/xml_utils.js
+../../lib/util/tXml.js

+../../third_party/closure-uri/uri.js
+../../third_party/closure-uri/utils.js
Expand Down
3 changes: 3 additions & 0 deletions docs/tutorials/upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ application:
- Configuration changes:
- `streaming.forceTransmuxTS` has been renamed to `streaming.forceTransmux`
(deprecated in v4.3.0)
- `manifest.dash.manifestPreprocessor` callback now receives a type of `shaka.externs.xml.Node` instead of `Element`.
- `manifest.mss.manifestPreprocessor` callback now receives a type of `shaka.externs.xml.Node` instead of `Element`.

- Plugin changes:
- `Transmuxer` plugins now has three new parameters in `transmux()` method.

- Player API Changes:
- The constructor no longer takes `mediaElement` as a parameter; use the `attach` method to attach to a media element instead. (Deprecated in v4.6)
- The `TimelineRegionInfo.eventElement` property is now a type of `shaka.externs.xml.Node` instead of `Element`
34 changes: 28 additions & 6 deletions externs/shaka/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ shaka.extern.MetadataFrame;
* startTime: number,
* endTime: number,
* id: string,
* eventElement: Element
* eventElement: ?shaka.extern.xml.Node
* }}
*
* @description
Expand All @@ -539,7 +539,7 @@ shaka.extern.MetadataFrame;
* The presentation time (in seconds) that the region should end.
* @property {string} id
* Specifies an identifier for this instance of the region.
* @property {Element} eventElement
* @property {?shaka.extern.xml.Node} eventElement
* The XML element that defines the Event.
* @exportDoc
*/
Expand Down Expand Up @@ -840,6 +840,28 @@ shaka.extern.DrmConfiguration;
shaka.extern.InitDataTransform;


/**
* @typedef {{
* tagName: !string,
* attributes: !Object<string, string>,
* children: !Array.<shaka.extern.xml.Node | string>,
* parent: ?shaka.extern.xml.Node
* }}
*
* @description
* Data structure for xml nodes as simple objects
*
* @property {!string} tagName
* The name of the element
* @property {!object} attributes
* The attributes of the element
* @property {!Array.<shaka.extern.xml.Node | string>} children
* The child nodes or string body of the element
* @property {?shaka.extern.xml.Node} parent
* The parent of the current element
*/
shaka.extern.xml.Node;

/**
* @typedef {{
* clockSyncUri: string,
Expand All @@ -853,7 +875,7 @@ shaka.extern.InitDataTransform;
* ignoreEmptyAdaptationSet: boolean,
* ignoreMaxSegmentDuration: boolean,
* keySystemsByURI: !Object.<string, string>,
* manifestPreprocessor: function(!Element),
* manifestPreprocessor: function(!shaka.extern.xml.Node),
* sequenceMode: boolean,
* enableAudioGroups: boolean,
* multiTypeVariantsAllowed: boolean,
Expand Down Expand Up @@ -907,7 +929,7 @@ shaka.extern.InitDataTransform;
* @property {Object.<string, string>} keySystemsByURI
* A map of scheme URI to key system name. Defaults to default key systems
* mapping handled by Shaka.
* @property {function(!Element)} manifestPreprocessor
* @property {function(!shaka.extern.xml.Node)} manifestPreprocessor
* Called immediately after the DASH manifest has been parsed into an
* XMLDocument. Provides a way for applications to perform efficient
* preprocessing of the manifest.
Expand Down Expand Up @@ -1025,12 +1047,12 @@ shaka.extern.HlsManifestConfiguration;

/**
* @typedef {{
* manifestPreprocessor: function(!Element),
* manifestPreprocessor: function(!shaka.extern.xml.Node),
* sequenceMode: boolean,
* keySystemsBySystemId: !Object.<string, string>
* }}
*
* @property {function(!Element)} manifestPreprocessor
* @property {function(!shaka.extern.xml.Node)} manifestPreprocessor
* Called immediately after the MSS manifest has been parsed into an
* XMLDocument. Provides a way for applications to perform efficient
* preprocessing of the manifest.
Expand Down
2 changes: 1 addition & 1 deletion lib/ads/ad_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ shaka.ads.AdManager = class extends shaka.util.FakeEventTarget {
if (this.ssAdManager_ && region.schemeIdUri == 'urn:google:dai:2018') {
const type = region.schemeIdUri;
const data = region.eventElement ?
region.eventElement.getAttribute('messageData') : null;
region.eventElement.attributes['messageData'] : null;
const timestamp = region.startTime;
this.ssAdManager_.onTimedMetadata(type, data, timestamp);
}
Expand Down
107 changes: 64 additions & 43 deletions lib/dash/content_protection.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ goog.require('shaka.util.Error');
goog.require('shaka.util.ManifestParserUtils');
goog.require('shaka.util.Pssh');
goog.require('shaka.util.StringUtils');
goog.require('shaka.util.TXml');
goog.require('shaka.util.Uint8ArrayUtils');
goog.require('shaka.util.XmlUtils');


/**
Expand All @@ -25,7 +25,7 @@ shaka.dash.ContentProtection = class {
/**
* Parses info from the ContentProtection elements at the AdaptationSet level.
*
* @param {!Array.<!Element>} elems
* @param {!Array.<!shaka.extern.xml.Node>} elems
* @param {boolean} ignoreDrmInfo
* @param {!Object.<string, string>} keySystemsByURI
* @return {shaka.dash.ContentProtection.Context}
Expand Down Expand Up @@ -137,7 +137,7 @@ shaka.dash.ContentProtection = class {
* Parses the given ContentProtection elements found at the Representation
* level. This may update the |context|.
*
* @param {!Array.<!Element>} elems
* @param {!Array.<!shaka.extern.xml.Node>} elems
* @param {shaka.dash.ContentProtection.Context} context
* @param {boolean} ignoreDrmInfo
* @param {!Object.<string, string>} keySystemsByURI
Expand Down Expand Up @@ -191,17 +191,20 @@ shaka.dash.ContentProtection = class {
* @return {string}
*/
static getWidevineLicenseUrl(element) {
const dashIfLaurlNode = shaka.util.XmlUtils.findChildNS(
const dashIfLaurlNode = shaka.util.TXml.findChildNS(
element.node, shaka.dash.ContentProtection.DashIfNamespaceUri_,
'Laurl',
);
if (dashIfLaurlNode && dashIfLaurlNode.textContent) {
return dashIfLaurlNode.textContent;
if (dashIfLaurlNode) {
const textContents = shaka.util.TXml.getTextContents(dashIfLaurlNode);
if (textContents) {
return textContents;
}
}
const mslaurlNode = shaka.util.XmlUtils.findChildNS(
const mslaurlNode = shaka.util.TXml.findChildNS(
element.node, 'urn:microsoft', 'laurl');
if (mslaurlNode) {
return mslaurlNode.getAttribute('licenseUrl') || '';
return mslaurlNode.attributes['licenseUrl'] || '';
}
return '';
}
Expand All @@ -214,21 +217,27 @@ shaka.dash.ContentProtection = class {
* @return {string}
*/
static getClearKeyLicenseUrl(element) {
const dashIfLaurlNode = shaka.util.XmlUtils.findChildNS(
const dashIfLaurlNode = shaka.util.TXml.findChildNS(
element.node, shaka.dash.ContentProtection.DashIfNamespaceUri_,
'Laurl',
);
if (dashIfLaurlNode && dashIfLaurlNode.textContent) {
return dashIfLaurlNode.textContent;
if (dashIfLaurlNode) {
const textContents = shaka.util.TXml.getTextContents(dashIfLaurlNode);
if (textContents) {
return textContents;
}
}
const clearKeyLaurlNode = shaka.util.XmlUtils.findChildNS(
const clearKeyLaurlNode = shaka.util.TXml.findChildNS(
element.node, shaka.dash.ContentProtection.ClearKeyNamespaceUri_,
'Laurl',
);
if (clearKeyLaurlNode &&
clearKeyLaurlNode.getAttribute('Lic_type') === 'EME-1.0') {
if (clearKeyLaurlNode.textContent) {
return clearKeyLaurlNode.textContent;
clearKeyLaurlNode.attributes['Lic_type'] === 'EME-1.0') {
if (clearKeyLaurlNode) {
const textContents = shaka.util.TXml.getTextContents(clearKeyLaurlNode);
if (textContents) {
return textContents;
}
}
}
return '';
Expand Down Expand Up @@ -312,18 +321,21 @@ shaka.dash.ContentProtection = class {
/**
* PlayReady Header format: https://goo.gl/dBzxNA
*
* @param {!Element} xml
* @param {!shaka.extern.xml.Node} xml
* @return {string}
* @private
*/
static getLaurl_(xml) {
const TXml = shaka.util.TXml;
// LA_URL element is optional and no more than one is
// allowed inside the DATA element. Only absolute URLs are allowed.
// If the LA_URL element exists, it must not be empty.
for (const elem of xml.getElementsByTagName('DATA')) {
for (const child of elem.childNodes) {
if (child instanceof Element && child.tagName == 'LA_URL') {
return child.textContent;
for (const elem of TXml.getElementsByTagName(xml, 'DATA')) {
if (elem.children) {
for (const child of elem.children) {
if (child.tagName == 'LA_URL') {
return /** @type{string} */(shaka.util.TXml.getTextContents(child));
}
}
}
}
Expand All @@ -340,25 +352,30 @@ shaka.dash.ContentProtection = class {
* @return {string}
*/
static getPlayReadyLicenseUrl(element) {
const dashIfLaurlNode = shaka.util.XmlUtils.findChildNS(
const dashIfLaurlNode = shaka.util.TXml.findChildNS(
element.node, shaka.dash.ContentProtection.DashIfNamespaceUri_,
'Laurl',
);
if (dashIfLaurlNode && dashIfLaurlNode.textContent) {
return dashIfLaurlNode.textContent;
if (dashIfLaurlNode) {
const textContents = shaka.util.TXml.getTextContents(dashIfLaurlNode);
if (textContents) {
return textContents;
}
}

const proNode = shaka.util.XmlUtils.findChildNS(
const proNode = shaka.util.TXml.findChildNS(
element.node, 'urn:microsoft:playready', 'pro');

if (!proNode) {
if (!proNode || !shaka.util.TXml.getTextContents(proNode)) {
return '';
}

const ContentProtection = shaka.dash.ContentProtection;
const PLAYREADY_RECORD_TYPES = ContentProtection.PLAYREADY_RECORD_TYPES;

const bytes = shaka.util.Uint8ArrayUtils.fromBase64(proNode.textContent);
const textContent =
/** @type {string} */ (shaka.util.TXml.getTextContents(proNode));
const bytes = shaka.util.Uint8ArrayUtils.fromBase64(textContent);
const records = ContentProtection.parseMsPro_(bytes);
const record = records.filter((record) => {
return record.type === PLAYREADY_RECORD_TYPES.RIGHTS_MANAGEMENT;
Expand All @@ -369,7 +386,7 @@ shaka.dash.ContentProtection = class {
}

const xml = shaka.util.StringUtils.fromUTF16(record.value, true);
const rootElement = shaka.util.XmlUtils.parseXmlString(xml, 'WRMHEADER');
const rootElement = shaka.util.TXml.parseXmlString(xml, 'WRMHEADER');
if (!rootElement) {
return '';
}
Expand All @@ -386,13 +403,16 @@ shaka.dash.ContentProtection = class {
* @private
*/
static getInitDataFromPro_(element) {
const proNode = shaka.util.XmlUtils.findChildNS(
const proNode = shaka.util.TXml.findChildNS(
element.node, 'urn:microsoft:playready', 'pro');
if (!proNode) {
if (!proNode || !shaka.util.TXml.getTextContents(proNode)) {
return null;
}

const Uint8ArrayUtils = shaka.util.Uint8ArrayUtils;
const data = Uint8ArrayUtils.fromBase64(proNode.textContent);
const textContent =
/** @type{string} */ (shaka.util.TXml.getTextContents(proNode));
const data = Uint8ArrayUtils.fromBase64(textContent);
const systemId = new Uint8Array([
0x9a, 0x04, 0xf0, 0x79, 0x98, 0x40, 0x42, 0x86,
0xab, 0x92, 0xe6, 0x5b, 0xe0, 0x88, 0x5f, 0x95,
Expand Down Expand Up @@ -492,7 +512,7 @@ shaka.dash.ContentProtection = class {
* Parses the given ContentProtection elements. If there is an error, it
* removes those elements.
*
* @param {!Array.<!Element>} elems
* @param {!Array.<!shaka.extern.xml.Node>} elems
* @return {!Array.<shaka.dash.ContentProtection.Element>}
* @private
*/
Expand All @@ -513,20 +533,21 @@ shaka.dash.ContentProtection = class {
/**
* Parses the given ContentProtection element.
*
* @param {!Element} elem
* @param {!shaka.extern.xml.Node} elem
* @return {?shaka.dash.ContentProtection.Element}
* @private
*/
static parseElement_(elem) {
const NS = shaka.dash.ContentProtection.CencNamespaceUri_;
const TXml = shaka.util.TXml;

/** @type {?string} */
let schemeUri = elem.getAttribute('schemeIdUri');
let schemeUri = elem.attributes['schemeIdUri'];
/** @type {?string} */
let keyId = shaka.util.XmlUtils.getAttributeNS(elem, NS, 'default_KID');
let keyId = TXml.getAttributeNS(elem, NS, 'default_KID');
/** @type {!Array.<string>} */
const psshs = shaka.util.XmlUtils.findChildrenNS(elem, NS, 'pssh')
.map(shaka.util.XmlUtils.getContents);
const psshs = TXml.findChildrenNS(elem, NS, 'pssh')
.map(TXml.getContents);

if (!schemeUri) {
shaka.log.error('Missing required schemeIdUri attribute on',
Expand Down Expand Up @@ -590,7 +611,7 @@ shaka.dash.ContentProtection = class {
}

const namespace = 'urn:mpeg:dash:schema:sea:2012';
const segmentEncryption = shaka.util.XmlUtils.findChildNS(
const segmentEncryption = shaka.util.TXml.findChildNS(
element.node, namespace, 'SegmentEncryption');

if (!segmentEncryption) {
Expand All @@ -602,15 +623,15 @@ shaka.dash.ContentProtection = class {

const aesSchemeIdUri = 'urn:mpeg:dash:sea:aes128-cbc:2013';
const segmentEncryptionSchemeIdUri =
segmentEncryption.getAttribute('schemeIdUri');
segmentEncryption.attributes['schemeIdUri'];
if (segmentEncryptionSchemeIdUri != aesSchemeIdUri) {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.MANIFEST,
shaka.util.Error.Code.DASH_UNSUPPORTED_AES_128);
}

const cryptoPeriod = shaka.util.XmlUtils.findChildNS(
const cryptoPeriod = shaka.util.TXml.findChildNS(
element.node, namespace, 'CryptoPeriod');

if (!cryptoPeriod) {
Expand All @@ -620,8 +641,8 @@ shaka.dash.ContentProtection = class {
shaka.util.Error.Code.DASH_UNSUPPORTED_AES_128);
}

const ivHex = cryptoPeriod.getAttribute('IV');
const keyUri = cryptoPeriod.getAttribute('keyUriTemplate');
const ivHex = cryptoPeriod.attributes['IV'];
const keyUri = cryptoPeriod.attributes['keyUriTemplate'];
if (!ivHex || !keyUri) {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
Expand Down Expand Up @@ -721,7 +742,7 @@ shaka.dash.ContentProtection.Aes128Info;

/**
* @typedef {{
* node: !Element,
* node: !shaka.extern.xml.Node,
* schemeUri: string,
* keyId: ?string,
* init: Array.<shaka.extern.InitDataOverride>
Expand All @@ -730,7 +751,7 @@ shaka.dash.ContentProtection.Aes128Info;
* @description
* The parsed result of a single ContentProtection element.
*
* @property {!Element} node
* @property {!shaka.extern.xml.Node} node
* The ContentProtection XML element.
* @property {string} schemeUri
* The scheme URI.
Expand Down
Loading

0 comments on commit 7116a34

Please sign in to comment.