Skip to content

Commit

Permalink
Add several utility functions.
Browse files Browse the repository at this point in the history
Change-Id: Ifbd2582747d2cb54d4abda789b0989b7a6b77aa6
  • Loading branch information
TheModMaker committed Mar 29, 2016
1 parent 340f3aa commit 8b85e58
Show file tree
Hide file tree
Showing 13 changed files with 298 additions and 125 deletions.
2 changes: 2 additions & 0 deletions build/types/core
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
+../../lib/util/event_manager.js
+../../lib/util/fake_event.js
+../../lib/util/fake_event_target.js
+../../lib/util/functional.js
+../../lib/util/i_destroyable.js
+../../lib/util/language_utils.js
+../../lib/util/map_utils.js
+../../lib/util/multi_map.js
+../../lib/util/pssh.js
+../../lib/util/public_promise.js
Expand Down
17 changes: 11 additions & 6 deletions lib/dash/content_protection.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ goog.provide('shaka.dash.ContentProtection');

goog.require('goog.asserts');
goog.require('shaka.util.Error');
goog.require('shaka.util.Functional');
goog.require('shaka.util.MapUtils');
goog.require('shaka.util.XmlUtils');


Expand Down Expand Up @@ -107,6 +109,8 @@ shaka.dash.ContentProtection.MP4Protection_ =
shaka.dash.ContentProtection.parseFromAdaptationSet = function(
elems, callback) {
var ContentProtection = shaka.dash.ContentProtection;
var Functional = shaka.util.Functional;
var MapUtils = shaka.util.MapUtils;
var parsed = ContentProtection.parseElements_(elems);

// Find the default key ID and init data. Create a new array of all the
Expand All @@ -126,12 +130,12 @@ shaka.dash.ContentProtection.parseFromAdaptationSet = function(

// Get the default key ID; if there are multiple, they must all match.
var keyIds = parsed.map(function(elem) { return elem.keyId; })
.filter(function(keyId) { return keyId != null; });
.filter(Functional.isNotNull);
/** @type {?string} */
var defaultKeyId = null;
if (keyIds.length > 0) {
defaultKeyId = keyIds[0];
if (keyIds.some(function(keyId) { return keyId != defaultKeyId; })) {
if (keyIds.some(Functional.isNotEqualFunc(defaultKeyId))) {
throw new shaka.util.Error(
shaka.util.Error.Category.MANIFEST,
shaka.util.Error.Code.DASH_CONFLICTING_KEY_IDS);
Expand All @@ -154,8 +158,7 @@ shaka.dash.ContentProtection.parseFromAdaptationSet = function(
// supported.
var keySystems = ContentProtection.defaultKeySystems_;
drmInfos =
Object.keys(keySystems)
.map(function(uri) { return keySystems[uri]; })
MapUtils.values(keySystems)
.map(function(keySystem) {
return ContentProtection.createDrmInfo_(keySystem, defaultInit);
});
Expand Down Expand Up @@ -251,6 +254,7 @@ shaka.dash.ContentProtection.createDrmInfo_ = function(keySystem, initData) {
*/
shaka.dash.ContentProtection.convertElements_ = function(
defaultInit, callback, elements) {
var Functional = shaka.util.Functional;
return elements.map(
/**
* @param {shaka.dash.ContentProtection.Element} element
Expand All @@ -269,7 +273,7 @@ shaka.dash.ContentProtection.convertElements_ = function(
callback, 'ContentProtection callback is required');
return callback(element.node) || [];
}
}).reduce(function(all, part) { return all.concat(part); }, []);
}).reduce(Functional.collapseArrays, []);
};


Expand All @@ -282,6 +286,7 @@ shaka.dash.ContentProtection.convertElements_ = function(
* @private
*/
shaka.dash.ContentProtection.parseElements_ = function(elems) {
var Functional = shaka.util.Functional;
return elems.map(
/**
* @param {!Element} elem
Expand Down Expand Up @@ -338,6 +343,6 @@ shaka.dash.ContentProtection.parseElements_ = function(elems) {
init: (init.length > 0 ? init : null)
};
return element;
}).filter(function(e) { return e != null; });
}).filter(Functional.isNotNull);
};

87 changes: 44 additions & 43 deletions lib/dash/dash_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ goog.require('shaka.media.PresentationTimeline');
goog.require('shaka.media.SegmentReference');
goog.require('shaka.net.NetworkingEngine');
goog.require('shaka.util.Error');
goog.require('shaka.util.Functional');
goog.require('shaka.util.LanguageUtils');
goog.require('shaka.util.MultiMap');
goog.require('shaka.util.StringUtils');
Expand Down Expand Up @@ -361,8 +362,9 @@ shaka.dash.DashParser.prototype.requestManifest_ = function() {
*/
shaka.dash.DashParser.prototype.parseManifest_ =
function(data, finalManifestUri) {
var XmlUtils = shaka.util.XmlUtils;
var Error = shaka.util.Error;
var Functional = shaka.util.Functional;
var XmlUtils = shaka.util.XmlUtils;

var string = shaka.util.StringUtils.fromBytesAutoDetect(data);
var parser = new DOMParser();
Expand Down Expand Up @@ -391,7 +393,7 @@ shaka.dash.DashParser.prototype.parseManifest_ =
/** @type {!Array.<string>} */
var locations = XmlUtils.findChildren(mpd, 'Location')
.map(XmlUtils.getContents)
.filter(function(l) { return l != null; });
.filter(Functional.isNotNull);
if (locations.length > 0) {
this.manifestUris_ = locations;
manifestBaseUris = locations;
Expand Down Expand Up @@ -491,6 +493,7 @@ shaka.dash.DashParser.prototype.parseManifest_ =
*/
shaka.dash.DashParser.prototype.parsePeriods_ = function(
context, baseUris, mpd) {
var Functional = shaka.util.Functional;
var XmlUtils = shaka.util.XmlUtils;
var presentationDuration = XmlUtils.parseAttr(
mpd, 'mediaPresentationDuration', XmlUtils.parseDuration);
Expand Down Expand Up @@ -531,7 +534,7 @@ shaka.dash.DashParser.prototype.parsePeriods_ = function(
// If there are any new periods, call the callback and add them to the
// manifest. If this is the first parse, it will see all of them as new.
var periodId = context.period.id;
if (this.periodIds_.every(function(id) { return id != periodId; })) {
if (this.periodIds_.every(Functional.isNotEqualFunc(periodId))) {
this.filterPeriod_(period);
this.periodIds_.push(periodId);
if (this.manifest_)
Expand Down Expand Up @@ -887,6 +890,7 @@ shaka.dash.DashParser.prototype.createFrame_ = function(
* @private
*/
shaka.dash.DashParser.prototype.createStreamSets_ = function(adaptationSets) {
var Functional = shaka.util.Functional;
/**
* A map of ID to the group it belongs to. Multiple IDs can map to the same
* group. Each entry in the group will map back to the same array.
Expand Down Expand Up @@ -920,9 +924,7 @@ shaka.dash.DashParser.prototype.createStreamSets_ = function(adaptationSets) {
/** @type {!Array.<!Array.<shaka.dash.DashParser.AdaptationInfo>>} */
var seenGroups = [];

// TODO: Use MapUtils.
Object.keys(groupMap).map(function(groupId) {
var group = groupMap[groupId];
shaka.util.MapUtils.values(groupMap).forEach(function(group) {
if (seenGroups.indexOf(group) >= 0)
return;

Expand Down Expand Up @@ -954,10 +956,10 @@ shaka.dash.DashParser.prototype.createStreamSets_ = function(adaptationSets) {
primary: sets.some(function(s) { return s.main; }),
drmInfos:
sets.map(function(s) { return s.drmInfos; })
.reduce(function(all, part) { return all.concat(part); }, []),
.reduce(Functional.collapseArrays, []),
streams:
sets.map(function(s) { return s.streams; })
.reduce(function(all, part) { return all.concat(part); }, [])
.reduce(Functional.collapseArrays, [])
};
ret.push(streamSet);
}); // forEach lang
Expand Down Expand Up @@ -1043,45 +1045,44 @@ shaka.dash.DashParser.prototype.requestForTiming_ = function(uri, method) {
/**
* Parses an array of UTCTiming elements.
*
* @param {Array.<!Element>} elems
* @param {!Array.<!Element>} elems
* @return {!Promise.<number>}
* @private
*/
shaka.dash.DashParser.prototype.parseUtcTiming_ = function(elems) {
var promise = elems.reduce(function(parent, elem) {
return parent.catch(function() {
var scheme = elem.getAttribute('schemeIdUri');
var value = elem.getAttribute('value');
switch (scheme) {
// See DASH IOP Guidelines Section 4.7
// http://goo.gl/CQFNJT
case 'urn:mpeg:dash:utc:http-head:2014':
// Some old ISO23009-1 drafts used 2012.
case 'urn:mpeg:dash:utc:http-head:2012':
return this.requestForTiming_(value, 'HEAD');
case 'urn:mpeg:dash:utc:http-xsdate:2014':
case 'urn:mpeg:dash:utc:http-iso:2014':
case 'urn:mpeg:dash:utc:http-xsdate:2012':
case 'urn:mpeg:dash:utc:http-iso:2012':
return this.requestForTiming_(value, 'GET');
case 'urn:mpeg:dash:utc:direct:2014':
case 'urn:mpeg:dash:utc:direct:2012':
var date = Date.parse(value);
return isNaN(date) ? 0 : (date - Date.now());

case 'urn:mpeg:dash:utc:http-ntp:2014':
case 'urn:mpeg:dash:utc:ntp:2014':
case 'urn:mpeg:dash:utc:sntp:2014':
shaka.log.warning('NTP UTCTiming scheme is not supported');
return Promise.reject();
default:
shaka.log.warning('Unrecognized scheme in UTCTiming element', scheme);
return Promise.reject();
}
}.bind(this));
}.bind(this), Promise.reject());

return promise.catch(function() { return 0; });
var Functional = shaka.util.Functional;
return Functional.createFallbackPromiseChain(elems, function(elem) {
var scheme = elem.getAttribute('schemeIdUri');
var value = elem.getAttribute('value');
switch (scheme) {
// See DASH IOP Guidelines Section 4.7
// http://goo.gl/CQFNJT
case 'urn:mpeg:dash:utc:http-head:2014':
// Some old ISO23009-1 drafts used 2012.
case 'urn:mpeg:dash:utc:http-head:2012':
return this.requestForTiming_(value, 'HEAD');
case 'urn:mpeg:dash:utc:http-xsdate:2014':
case 'urn:mpeg:dash:utc:http-iso:2014':
case 'urn:mpeg:dash:utc:http-xsdate:2012':
case 'urn:mpeg:dash:utc:http-iso:2012':
return this.requestForTiming_(value, 'GET');
case 'urn:mpeg:dash:utc:direct:2014':
case 'urn:mpeg:dash:utc:direct:2012':
var date = Date.parse(value);
return isNaN(date) ? 0 : (date - Date.now());

case 'urn:mpeg:dash:utc:http-ntp:2014':
case 'urn:mpeg:dash:utc:ntp:2014':
case 'urn:mpeg:dash:utc:sntp:2014':
shaka.log.warning('NTP UTCTiming scheme is not supported');
return Promise.reject();
default:
shaka.log.warning(
'Unrecognized scheme in UTCTiming element', scheme);
return Promise.reject();
}
}.bind(this))
.catch(function() { return 0; });
};


Expand Down
10 changes: 7 additions & 3 deletions lib/dash/mpd_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ goog.require('goog.Uri');
goog.require('goog.asserts');
goog.require('shaka.log');
goog.require('shaka.media.SegmentReference');
goog.require('shaka.util.Functional');
goog.require('shaka.util.XmlUtils');


Expand Down Expand Up @@ -352,6 +353,7 @@ shaka.dash.MpdUtils.fitSegmentReferences = function(
* @return {!Array.<string>}
*/
shaka.dash.MpdUtils.resolveUris = function(baseUris, relativeUris) {
var Functional = shaka.util.Functional;
if (relativeUris.length == 0)
return baseUris;

Expand All @@ -361,7 +363,7 @@ shaka.dash.MpdUtils.resolveUris = function(baseUris, relativeUris) {
// Then flatten the Arrays into a single Array.
return baseUris.map(function(uri) { return new goog.Uri(uri); })
.map(function(base) { return relativeAsGoog.map(base.resolve.bind(base)); })
.reduce(function(all, part) { return all.concat(part); }, [])
.reduce(Functional.collapseArrays, [])
.map(function(uri) { return uri.toString(); });
};

Expand Down Expand Up @@ -432,6 +434,7 @@ shaka.dash.MpdUtils.parseSegmentInfo = function(context, callback) {
* @return {?string}
*/
shaka.dash.MpdUtils.inheritAttribute = function(context, callback, attribute) {
var Functional = shaka.util.Functional;
goog.asserts.assert(
callback(context.representation),
'There must be at least one element of the given type');
Expand All @@ -441,7 +444,7 @@ shaka.dash.MpdUtils.inheritAttribute = function(context, callback, attribute) {
callback(context.representation),
callback(context.adaptationSet),
callback(context.period)
].filter(function(s) { return s != null; });
].filter(Functional.isNotNull);

return nodes
.map(function(s) { return s.getAttribute(attribute); })
Expand All @@ -459,6 +462,7 @@ shaka.dash.MpdUtils.inheritAttribute = function(context, callback, attribute) {
* @return {Element}
*/
shaka.dash.MpdUtils.inheritChild = function(context, callback, child) {
var Functional = shaka.util.Functional;
goog.asserts.assert(
callback(context.representation),
'There must be at least one element of the given type');
Expand All @@ -468,7 +472,7 @@ shaka.dash.MpdUtils.inheritChild = function(context, callback, child) {
callback(context.representation),
callback(context.adaptationSet),
callback(context.period)
].filter(function(s) { return s != null; });
].filter(Functional.isNotNull);

var XmlUtils = shaka.util.XmlUtils;
return nodes
Expand Down
4 changes: 3 additions & 1 deletion lib/dash/segment_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ goog.require('shaka.dash.SegmentBase');
goog.require('shaka.media.SegmentIndex');
goog.require('shaka.media.SegmentReference');
goog.require('shaka.util.Error');
goog.require('shaka.util.Functional');
goog.require('shaka.util.XmlUtils');


Expand Down Expand Up @@ -295,12 +296,13 @@ shaka.dash.SegmentList.createSegmentReferences_ = function(
* @private
*/
shaka.dash.SegmentList.parseMediaSegments_ = function(context) {
var Functional = shaka.util.Functional;
/** @type {!Array.<!Element>} */
var segmentLists = [
context.representation.segmentList,
context.adaptationSet.segmentList,
context.period.segmentList
].filter(function(s) { return s != null; });
].filter(Functional.isNotNull);

var XmlUtils = shaka.util.XmlUtils;
// Search each SegmentList for one with at least one SegmentURL element,
Expand Down
9 changes: 6 additions & 3 deletions lib/media/drm_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ goog.require('shaka.log');
goog.require('shaka.util.ArrayUtils');
goog.require('shaka.util.Error');
goog.require('shaka.util.EventManager');
goog.require('shaka.util.Functional');
goog.require('shaka.util.IDestroyable');
goog.require('shaka.util.PublicPromise');
goog.require('shaka.util.Uint8ArrayUtils');
Expand Down Expand Up @@ -92,13 +93,14 @@ shaka.media.DrmEngine.ActiveSession;

/** @override */
shaka.media.DrmEngine.prototype.destroy = function() {
var Functional = shaka.util.Functional;
this.destroyed_ = true;

this.activeSessions_.forEach(function(activeSession) {
// Ignore any errors when closing the sessions. One such error would be
// an invalid state error triggered by closing a session which has not
// generated any key requests.
activeSession.session.close().catch(function() {});
activeSession.session.close().catch(Functional.noop);
});

var async = [];
Expand All @@ -107,7 +109,7 @@ shaka.media.DrmEngine.prototype.destroy = function() {

if (this.video_) {
goog.asserts.assert(!this.video_.src, 'video src must be removed first!');
async.push(this.video_.setMediaKeys(null).catch(function() {}));
async.push(this.video_.setMediaKeys(null).catch(Functional.noop));
}

this.drmInfos_ = [];
Expand Down Expand Up @@ -428,6 +430,7 @@ shaka.media.DrmEngine.prototype.queryMediaKeys_ =
* @private
*/
shaka.media.DrmEngine.prototype.fillInDrmInfoDefaults_ = function(drmInfo) {
var MapUtils = shaka.util.MapUtils;
var keySystem = drmInfo.keySystem;

if (!keySystem) {
Expand All @@ -441,7 +444,7 @@ shaka.media.DrmEngine.prototype.fillInDrmInfoDefaults_ = function(drmInfo) {
if (server) {
drmInfo.licenseServerUri = server;
} else if (keySystem == 'org.w3.clearkey') {
var hasClearKeys = Object.keys(this.config_.clearKeys).length != 0;
var hasClearKeys = !MapUtils.empty(this.config_.clearKeys);
if (hasClearKeys) {
this.configureClearKey_(drmInfo);
} else {
Expand Down
Loading

0 comments on commit 8b85e58

Please sign in to comment.