Skip to content

Commit

Permalink
perf(dash): improve readability and reduce number of loops in dash pa…
Browse files Browse the repository at this point in the history
…rser (shaka-project#5768)

This change reduces the amount of loops in dash parser, and improves
code readability, as checking config and creating an array of sets is in
a separate method now.

---------

Co-authored-by: Ivan Kohut <ivan.kohut@lamin.ar>
  • Loading branch information
2 people authored and Rodolphe Breton committed Nov 30, 2023
1 parent feee494 commit 9bed236
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions lib/dash/dash_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,40 +850,29 @@ shaka.dash.DashParser = class {
}
}

const audioSets = this.config_.disableAudio ? [] :
this.getSetsOfType_(normalAdaptationSets, ContentType.AUDIO);
const videoSets = this.config_.disableVideo ? [] :
this.getSetsOfType_(normalAdaptationSets, ContentType.VIDEO);
const textSets = this.config_.disableText ? [] :
this.getSetsOfType_(normalAdaptationSets, ContentType.TEXT);
const imageSets = this.config_.disableThumbnails ? [] :
this.getSetsOfType_(normalAdaptationSets, ContentType.IMAGE);

if (!videoSets.length && !audioSets.length) {
const audioStreams = this.getStreamsFromSets_(
this.config_.disableAudio,
normalAdaptationSets,
ContentType.AUDIO);
const videoStreams = this.getStreamsFromSets_(
this.config_.disableVideo,
normalAdaptationSets,
ContentType.VIDEO);
const textStreams = this.getStreamsFromSets_(
this.config_.disableText,
normalAdaptationSets,
ContentType.TEXT);
const imageStreams = this.getStreamsFromSets_(
this.config_.disableThumbnails,
normalAdaptationSets,
ContentType.IMAGE);

if (videoStreams.length === 0 && audioStreams.length === 0) {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.MANIFEST,
shaka.util.Error.Code.DASH_EMPTY_PERIOD);
}

const audioStreams = [];
for (const audioSet of audioSets) {
audioStreams.push(...audioSet.streams);
}

const videoStreams = [];
for (const videoSet of videoSets) {
videoStreams.push(...videoSet.streams);
}

const textStreams = [];
for (const textSet of textSets) {
textStreams.push(...textSet.streams);
}

const imageStreams = [];
for (const imageSet of imageSets) {
imageStreams.push(...imageSet.streams);
shaka.util.Error.Code.DASH_EMPTY_PERIOD,
);
}

return {
Expand All @@ -896,15 +885,26 @@ shaka.dash.DashParser = class {
}

/**
* Gets the streams from the given sets or returns an empty array if disabled
* or no streams are found.
* @param {boolean} disabled
* @param {!Array.<!shaka.dash.DashParser.AdaptationInfo>} adaptationSets
* @param {string} type
* @return {!Array.<!shaka.dash.DashParser.AdaptationInfo>}
* @private
*/
getSetsOfType_(adaptationSets, type) {
return adaptationSets.filter((as) => {
return as.contentType == type;
});
* @param {string} contentType
@private
*/
getStreamsFromSets_(disabled, adaptationSets, contentType) {
if (disabled || !adaptationSets.length) {
return [];
}

return adaptationSets.reduce((all, part) => {
if (part.contentType != contentType) {
return all;
}

all.push(...part.streams);
return all;
}, []);
}

/**
Expand Down

0 comments on commit 9bed236

Please sign in to comment.