Skip to content

Commit

Permalink
solved bug regarding overwrighting and shifting of subtitltes
Browse files Browse the repository at this point in the history
  • Loading branch information
lauta committed May 3, 2023
1 parent b5d5c7a commit bc6b0aa
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 94 deletions.
38 changes: 21 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class HLSVod {
if (opts && opts.shouldContainSubtitles) {
this.shouldContainSubtitles = opts.shouldContainSubtitles;
}
if (opts && opts.expectedSubtitleTracks) {
this.expectedSubtitleTracks = opts.expectedSubtitleTracks;
}
this.videoSequencesCount = 0;
this.audioSequencesCount = 0;
this.defaultAudioGroupAndLang = null;
Expand All @@ -89,6 +92,7 @@ class HLSVod {
audioSegments: this.audioSegments,
subtitleSegments: this.subtitleSegments,
shouldContainSubtitles: this.shouldContainSubtitles,
expectedSubtitleTracks: this.expectedSubtitleTracks,
mediaSequences: this.mediaSequences,
SEQUENCE_DURATION: this.SEQUENCE_DURATION,
targetDuration: this.targetDuration,
Expand Down Expand Up @@ -132,6 +136,7 @@ class HLSVod {
this.audioSegments = de.audioSegments;
this.subtitleSegments = de.subtitleSegments;
this.shouldContainSubtitles = de.shouldContainSubtitles;
this.expectedSubtitleTracks = de.expectedSubtitleTracks;
this.mediaSequences = de.mediaSequences;
this.SEQUENCE_DURATION = de.SEQUENCE_DURATION;
this.targetDuration = de.targetDuration;
Expand Down Expand Up @@ -359,6 +364,11 @@ class HLSVod {
if (streamItem.get("subtitles")) {
if (!this.subtitleSliceEndpoint) {
reject(new Error("Missing subtitle slice URL"));
continue;
}
if (!this.expectedSubtitleTracks) {
reject(new Error("There are no expected subtitle tracks"));
continue;
}
let subtitleGroupId = streamItem.get("subtitles");
if (!this.subtitleSegments[subtitleGroupId]) {
Expand All @@ -381,12 +391,17 @@ class HLSVod {
} else {
itemLang = item.get("language");
}
if (!this.expectedSubtitleTracks.find((track) => track.language === itemLang || track.name === itemLang)) {
return;
}

// Initialize lang. in new group.
if (!this.subtitleSegments[subtitleGroupId][itemLang]) {
this.subtitleSegments[subtitleGroupId][itemLang] = [];
}
return (item = itemLang);
});
}).filter((item) => item !== undefined);


// # Inject default language's segments to every new language relative to previous VOD.
// # For the case when this is a VOD following another, every language new or old should
Expand All @@ -402,20 +417,6 @@ class HLSVod {
}
}

// # Need to clean up langs. loaded from prev. VOD that current VOD doesn't have.
// # Necessary, for the case when getLiveMediaSequenceSubtitleSegments() tries to
// # access an subtitleGroup's language that the current VOD never had. A False-Positive.
let allLangs = Object.keys(this.subtitleSegments[subtitleGroupId]);
let toRemove = [];
allLangs.map((junkLang) => {
if (!subtitleLanguages.includes(junkLang)) {
toRemove.push(junkLang);
}
});
toRemove.map((junkLang) => {
delete this.subtitleSegments[subtitleGroupId][junkLang];
});

// # For each lang, find the lang playlist uri and do _loadSubtitleManifest() on it.
for (let j = 0; j < subtitleLanguages.length; j++) {
let subtitleLang = subtitleLanguages[j];
Expand Down Expand Up @@ -452,6 +453,9 @@ class HLSVod {
if (!this.dummySubtitleEndpoint) {
reject(new Error("Loaded VOD does not contain subtitles and there is no dummy subtitle segment URL configured"));
}
if (!this.expectedSubtitleTracks) {
reject(new Error("There are no expected subtitle tracks"));
}
if (!this.subtitleSliceEndpoint) {
reject(new Error("Missing subtitle slice URL"));
}
Expand Down Expand Up @@ -1887,9 +1891,9 @@ class HLSVod {
this.subtitleSegments[subtitleGroupId][subtitleLang] = [];
}
if (lastMediaSubtitleSequence && lastMediaSubtitleSequence.length > 0) {
let start = 0;
let start = this.sequenceAlwaysContainNewSegments ? 0 : 1;
if (lastMediaSubtitleSequence[0] && lastMediaSubtitleSequence[0].discontinuity) {
start = 1;
start = this.sequenceAlwaysContainNewSegments ? 1 : 2;
}
for (let idx = start; idx < lastMediaSubtitleSequence.length; idx++) {
let q = lastMediaSubtitleSequence[idx];
Expand Down
Loading

0 comments on commit bc6b0aa

Please sign in to comment.