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 13, 2023
1 parent e79c5eb commit 3de6bfd
Showing 1 changed file with 37 additions and 31 deletions.
68 changes: 37 additions & 31 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 @@ -867,6 +856,23 @@ 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} contentType
@private
*/
getStreamsFromSets_(disabled, adaptationSets, contentType) {
if (disabled || !adaptationSets.length) {
return [];
}

return this.getSetsOfType_(adaptationSets, contentType)
.reduce((acc, currSet) => acc.concat(currSet.streams), []);
}

/**
* @param {!Array.<!shaka.dash.DashParser.AdaptationInfo>} adaptationSets
* @param {string} type
Expand Down

0 comments on commit 3de6bfd

Please sign in to comment.