Skip to content

Commit

Permalink
refactor(dash): improve readability and reduce number of loops in das…
Browse files Browse the repository at this point in the history
…h parser
  • Loading branch information
Ivan Kohut committed Oct 18, 2023
1 parent e79c5eb commit 685ec39
Showing 1 changed file with 40 additions and 39 deletions.
79 changes: 40 additions & 39 deletions lib/dash/dash_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,40 +822,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 @@ -868,15 +857,27 @@ 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 this.getSetsOfType_(adaptationSets, contentType)
.reduce((acc, currSet) => {
if (currSet.contentType != contentType) {
return acc;
}

acc.push(...currSet.streams);
return acc;
}, []);
}

/**
Expand Down

0 comments on commit 685ec39

Please sign in to comment.