Skip to content

Commit

Permalink
fix(dash): Fix performance regression
Browse files Browse the repository at this point in the history
  • Loading branch information
Alvaro Velad committed Mar 25, 2022
1 parent 89409ce commit dabaf47
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 28 deletions.
20 changes: 9 additions & 11 deletions lib/dash/dash_parser.js
Expand Up @@ -22,7 +22,6 @@ goog.require('shaka.text.TextEngine');
goog.require('shaka.util.CmcdManager');
goog.require('shaka.util.Error');
goog.require('shaka.util.Functional');
goog.require('shaka.util.Iterables');
goog.require('shaka.util.LanguageUtils');
goog.require('shaka.util.ManifestParserUtils');
goog.require('shaka.util.MimeUtils');
Expand Down Expand Up @@ -104,7 +103,7 @@ shaka.dash.DashParser = class {

/**
* Period IDs seen in previous manifest.
* @private {!Array.<number>}
* @private {!Array.<string>}
*/
this.lastManifestUpdatePeriodIds_ = [];

Expand Down Expand Up @@ -556,23 +555,22 @@ shaka.dash.DashParser = class {
const periods = [];
let prevEnd = 0;
const periodNodes = XmlUtils.findChildren(mpd, 'Period');
// This uses a for-loop rather than a for-of loop because this needs to look
// ahead to the next element.
const enumerate = (it) => shaka.util.Iterables.enumerate(it);
for (const {i, item: elem, next} of enumerate(periodNodes)) {
for (let i = 0; i < periodNodes.length; i++) {
const elem = periodNodes[i];
const start = /** @type {number} */ (
XmlUtils.parseAttr(elem, 'start', XmlUtils.parseDuration, prevEnd));
const periodId = elem.id;
const givenDuration =
XmlUtils.parseAttr(elem, 'duration', XmlUtils.parseDuration);

let periodDuration = null;
if (next) {
if (i + 1 < periodNodes.length) {
const nextPeriod = periodNodes[i + 1];
// "The difference between the start time of a Period and the start time
// of the following Period is the duration of the media content
// represented by this Period."
const nextStart =
XmlUtils.parseAttr(next, 'start', XmlUtils.parseDuration);
XmlUtils.parseAttr(nextPeriod, 'start', XmlUtils.parseDuration);
if (nextStart != null) {
periodDuration = nextStart - start;
}
Expand Down Expand Up @@ -637,7 +635,7 @@ shaka.dash.DashParser = class {
start: start,
duration: periodDuration,
node: elem,
isLastPeriod: periodDuration == null || !next,
isLastPeriod: periodDuration == null || (i + 1 == periodNodes.length),
};
const period = this.parsePeriod_(context, baseUris, info);
periods.push(period);
Expand All @@ -647,12 +645,12 @@ shaka.dash.DashParser = class {
}

if (periodDuration == null) {
if (next) {
if (i + 1 < periodNodes.length) {
// If the duration is still null and we aren't at the end, then we
// will skip any remaining periods.
shaka.log.warning(
'Skipping Period', i + 1, 'and any subsequent Periods:', 'Period',
i + 1, 'does not have a valid start time.', next);
i + 1, 'does not have a valid start time.', periodNodes[i + 1]);
}

// The duration is unknown, so the end is unknown.
Expand Down
15 changes: 7 additions & 8 deletions lib/dash/mpd_utils.js
Expand Up @@ -12,7 +12,6 @@ goog.require('shaka.net.NetworkingEngine');
goog.require('shaka.util.AbortableOperation');
goog.require('shaka.util.Error');
goog.require('shaka.util.Functional');
goog.require('shaka.util.Iterables');
goog.require('shaka.util.ManifestParserUtils');
goog.require('shaka.util.XmlUtils');
goog.requireType('shaka.dash.DashParser');
Expand Down Expand Up @@ -142,8 +141,8 @@ shaka.dash.MpdUtils = class {
const timeline = [];
let lastEndTime = -unscaledPresentationTimeOffset;

const enumerate = (it) => shaka.util.Iterables.enumerate(it);
for (const {item: timePoint, next} of enumerate(timePoints)) {
for (let i = 0; i < timePoints.length; ++i) {
const timePoint = timePoints[i];
let t = XmlUtils.parseAttr(timePoint, 't', XmlUtils.parseNonNegativeInt);
const d =
XmlUtils.parseAttr(timePoint, 'd', XmlUtils.parseNonNegativeInt);
Expand All @@ -165,9 +164,10 @@ shaka.dash.MpdUtils = class {

let repeat = r || 0;
if (repeat < 0) {
if (next) {
const nextStartTime =
XmlUtils.parseAttr(next, 't', XmlUtils.parseNonNegativeInt);
if (i + 1 < timePoints.length) {
const nextTimePoint = timePoints[i + 1];
const nextStartTime = XmlUtils.parseAttr(
nextTimePoint, 't', XmlUtils.parseNonNegativeInt);
if (nextStartTime == null) {
shaka.log.warning(
'An "S" element cannot have a negative repeat',
Expand Down Expand Up @@ -225,8 +225,7 @@ shaka.dash.MpdUtils = class {
timeline[timeline.length - 1].end = startTime / timescale;
}

for (const _ of shaka.util.Iterables.range(repeat + 1)) {
shaka.util.Functional.ignored(_);
for (let j = 0; j <= repeat; ++j) {
const endTime = startTime + d;
const item = {
start: startTime / timescale,
Expand Down
8 changes: 5 additions & 3 deletions lib/dash/segment_template.js
Expand Up @@ -14,7 +14,6 @@ goog.require('shaka.media.InitSegmentReference');
goog.require('shaka.media.SegmentIndex');
goog.require('shaka.media.SegmentReference');
goog.require('shaka.util.Error');
goog.require('shaka.util.Iterables');
goog.require('shaka.util.ManifestParserUtils');
goog.require('shaka.util.ObjectUtils');
goog.requireType('shaka.dash.DashParser');
Expand Down Expand Up @@ -504,8 +503,11 @@ shaka.dash.SegmentTemplate = class {

/** @type {!Array.<!shaka.media.SegmentReference>} */
const references = [];
const enum_ = (it) => shaka.util.Iterables.enumerate(it);
for (const {i, item: {start, unscaledStart, end}} of enum_(info.timeline)) {
for (let i = 0; i < info.timeline.length; i++) {
const start = info.timeline[i].start;
const unscaledStart = info.timeline[i].unscaledStart;
const end = info.timeline[i].end;

// Note: i = k - 1, where k indicates the k'th segment listed in the MPD.
// (See section 5.3.9.5.3 of the DASH spec.)
const segmentReplacement = i + info.startNumber;
Expand Down
5 changes: 2 additions & 3 deletions lib/util/string_utils.js
Expand Up @@ -158,9 +158,8 @@ shaka.util.StringUtils = class {
static toUTF16(str, littleEndian) {
const result = new ArrayBuffer(str.length * 2);
const view = new DataView(result);
const enumerate = (it) => shaka.util.Iterables.enumerate(it);
for (const {i, item} of enumerate(str)) {
const value = item.charCodeAt(0);
for (let i = 0; i < str.length; ++i) {
const value = str.charCodeAt(i);
view.setUint16(/* position= */ i * 2, value, littleEndian);
}
return result;
Expand Down
5 changes: 2 additions & 3 deletions lib/util/uint8array_utils.js
Expand Up @@ -74,9 +74,8 @@ shaka.util.Uint8ArrayUtils = class {
// byte.
const bytes = window.atob(str.replace(/-/g, '+').replace(/_/g, '/'));
const result = new Uint8Array(bytes.length);
const enumerate = (it) => shaka.util.Iterables.enumerate(it);
for (const {i, item} of enumerate(bytes)) {
result[i] = item.charCodeAt(0);
for (let i = 0; i < bytes.length; ++i) {
result[i] = bytes.charCodeAt(i);
}
return result;
}
Expand Down

0 comments on commit dabaf47

Please sign in to comment.