Skip to content

Commit

Permalink
fixed issue with no matching langs
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan Lautakoksi committed Sep 6, 2023
1 parent 3c1ac7c commit e626574
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 44 deletions.
51 changes: 43 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ class HLSVod {
let audioGroups = {};
let subtitleGroups = {};
let addedAudioLanguage = {};
let backupSegments = {};

const numberOfCodecs = Object.keys(this.audioCodecsMap)
let codecsMatchingInVod = 0;
Expand All @@ -261,6 +262,7 @@ class HLSVod {

for (let i = 0; i < m3u.items.StreamItem.length; i++) {
const streamItem = m3u.items.StreamItem[i];
let generateSegments = true;
if (streamItem.get("audio")) {
let audioGroupId = streamItem.get("audio");
const audioCodecs = streamItem.get("codecs").split(",").find(c => {
Expand All @@ -276,7 +278,10 @@ class HLSVod {
}
let audioGroupIdToUse = audioGroupId;
if (!this.audioSegments[audioGroupId]) {
audioGroupIdToUse = this.audioSegments[Object.keys(this.audioSegments)[0]];
audioGroupIdToUse = Object.keys(this.audioSegments)[0];
}
if (backupSegments.length === 0) {
backupSegments[audioGroupId] = {};
}

debug(`Lookup media item for '${audioGroupId}'`);
Expand Down Expand Up @@ -304,9 +309,13 @@ class HLSVod {
this.audioCodecsMap[audioCodecs] = {};
}
} else {
if (!this.audioSegments[audioGroupId][itemLang]) {
if (!this.audioSegments[audioGroupIdToUse][itemLang] && Object.keys(backupSegments[audioGroupIdToUse]).length !== 0) {
return;
} else if (Object.keys(backupSegments[audioGroupIdToUse]).length === 0) {
backupSegments[audioGroupIdToUse][itemLang] = [];

} else {
generateSegments = false;
addedAudioLanguage[itemLang] = true;
}

Expand Down Expand Up @@ -359,7 +368,26 @@ class HLSVod {

// # For each lang, find the lang playlist uri and do _loadAudioManifest() on it.
for (let j = 0; j < audioLanguages.length; j++) {
let audioLang = audioLanguages[j];
let audioLang = Object.keys(backupSegments[audioGroupIdToUse])[0];
let audioUri = audioGroupItems[0].get("uri");
if (!audioUri) {
//# if mediaItems dont have uris
let audioVariant = m3u.items.StreamItem.find((item) => {
return !item.get("resolution") && item.get("audio") === audioGroupId;
});
if (audioVariant) {
audioUri = audioVariant.get("uri");
}
}
if (audioUri) {
let audioManifestUrl = urlResolve(baseUrl, audioUri);
audioManifestPromises.push(this._loadAudioManifest(audioManifestUrl, audioGroupIdToUse, audioLang, _injectAudioManifest));
} else {
debug(`No media item for '${audioGroupId}' in "${audioLang}" was found, skipping`);
}
}
if (generateSegments) {
let audioLang = [];
let audioUri = audioGroupItems[j].get("uri");
if (!audioUri) {
//# if mediaItems dont have uris
Expand All @@ -379,7 +407,6 @@ class HLSVod {
// # otherwise it just would've loaded OVER the latest occurrent of the LANG in GroupID.
if (!audioGroups[audioGroupIdToUse][audioLang]) {
audioGroups[audioGroupIdToUse][audioLang] = true;
console.log(audioGroupIdToUse, audioLang, 10)
audioManifestPromises.push(this._loadAudioManifest(audioManifestUrl, audioGroupIdToUse, audioLang, _injectAudioManifest));
} else {
debug(`Audio manifest for language "${audioLang}" from '${audioGroupIdToUse}' in already loaded, skipping`);
Expand Down Expand Up @@ -503,8 +530,18 @@ class HLSVod {
allAudioLanguagesCount += Object.keys(this.audioSegments[groupIds[i]]).length
}


if (addedAudioLanguage.length < allAudioLanguagesCount) {
if(addedAudioLanguage.length === 0) {
const backupGroupId = Object.keys(backupSegments)[0];
const backupLang = Object.keys(backupSegments[backupGroupId])[0];
for(let i = 0; i < groupIds.length;i++) {
const langs = Object.keys(this.audioSegments[groupIds[i]])
for (let j = 0; j < langs.length; j++) {
this.audioSegments[groupIds[i]][langs[j]].concat(backupSegments[backupGroupId][backupLang])
}
}
}

if (addedAudioLanguage.length < allAudioLanguagesCount && addedAudioLanguage.length !== 0) {
let isAdded = [];
for(let i = 0; i < groupIds.length;i++) {
const langs = Object.keys(this.audioSegments[groupIds[i]])
Expand Down Expand Up @@ -861,8 +898,6 @@ class HLSVod {
return [];
}
}
/*console.log(audioGroupId, audioLanguage, this.mediaSequences[seqIdx].audioSegments)
console.log(this.audioSegments)*/
if (!this.mediaSequences[seqIdx].audioSegments[audioGroupId][audioLanguage]) {
const fallbackLang = this._getFirstAudioLanguageWithSegments(audioGroupId);
return this.mediaSequences[seqIdx].audioSegments[audioGroupId][fallbackLang];
Expand Down
43 changes: 7 additions & 36 deletions spec/hlsvod_audio_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,52 +623,23 @@ describe("HLSVod with demuxed audio", () => {
};
});

it("load vod after vod with the same languages", (done) => {
mockVod = new HLSVod("http://mock.com/mock.m3u8", null, 0, 0, null, hlsOpts);
mockVod2 = new HLSVod("http://mock.com/mock2.m3u8", null, 0, 0, null, hlsOpts);

mockVod
.load(mockMasterManifestOneLang, mockMediaManifestOneLang, mockAudioManifestOneLang)
.then(() => {
mockVod2.loadAfter(mockVod, mockMasterManifestOneLang, mockMediaManifestOneLang, mockAudioManifestOneLang)
.then(() => {
const m3u8 = mockVod2.getLiveMediaAudioSequences(0, "acc", "en", 0);
const m3u8_2 = mockVod2.getLiveMediaAudioSequences(0, "aac", "en", 1);
const subStrings = m3u8.split("\n")
const subStrings2 = m3u8_2.split("\n")
expect(subStrings[33]).toEqual("https://vod.streaming.a2d.tv/3e542405-583b-4edc-93ab-eca86427d148/ab92a690-62de-11ed-aa51-c96fb4f9434f_20337209.ism/hls/ab92a690-62de-11ed-aa51-c96fb4f9434f_20337209-textstream_swe=1000-693.webvtt");
expect(subStrings2[32]).toEqual("#EXT-X-DISCONTINUITY")
expect(subStrings2[33]).toEqual("#EXTINF:3.000,");
expect(subStrings2[34]).toEqual("https://d3t8zrj2x5ol3r.cloudfront.net/u/file~text_vtt~dummy.vtt/1/s/webvtt.vtt");
done();
});
});
});

it("load vod after vod with one matching languages", (done) => {
mockVod = new HLSVod("http://mock.com/mock.m3u8", null, 0, 0, null, hlsOpts);
mockVod2 = new HLSVod("http://mock.com/mock2.m3u8", null, 0, 0, null, hlsOpts);
mockVod = new HLSVod("http://mock.com/mock.m3u8");
mockVod2 = new HLSVod("http://mock.com/mock2.m3u8");

mockVod
.load(mockMasterManifestOneLang, mockMediaManifestOneLang, mockAudioManifestOneLang)
.then(() => {
mockVod2.loadAfter(mockVod, mockMasterManifestOneLang, mockMediaManifestOneLang, mockAudioManifestOneLang)
mockVod2.loadAfter(mockVod, mockMasterManifestMultipleLangs, mockMediaManifestMultipleLangs, mockAudioManifestMultipleLangs)
.then(() => {
const m3u8 = mockVod2.getLiveMediaAudioSequences(0, "acc", "en", 0);
const m3u8_2 = mockVod2.getLiveMediaAudioSequences(0, "aac", "en", 1);
const subStrings = m3u8.split("\n")
const subStrings2 = m3u8_2.split("\n")
expect(subStrings[33]).toEqual("https://vod.streaming.a2d.tv/3e542405-583b-4edc-93ab-eca86427d148/ab92a690-62de-11ed-aa51-c96fb4f9434f_20337209.ism/hls/ab92a690-62de-11ed-aa51-c96fb4f9434f_20337209-textstream_swe=1000-693.webvtt");
expect(subStrings2[32]).toEqual("#EXT-X-DISCONTINUITY")
expect(subStrings2[33]).toEqual("#EXTINF:3.000,");
expect(subStrings2[34]).toEqual("https://d3t8zrj2x5ol3r.cloudfront.net/u/file~text_vtt~dummy.vtt/1/s/webvtt.vtt");
expect(mockVod2.getAudioLangsForAudioGroup("aac").length).toEqual(1);
done();
});
});

});

fit("load vod after vod with no matching languages", (done) => {
it("load vod after vod with no matching languages", (done) => {
mockVod = new HLSVod("http://mock.com/mock.m3u8", null, 0, 0, null, hlsOpts);
mockVod2 = new HLSVod("http://mock.com/mock2.m3u8", null, 0, 0, null, hlsOpts);

Expand All @@ -677,14 +648,14 @@ describe("HLSVod with demuxed audio", () => {
.then(() => {
mockVod2.loadAfter(mockVod, mockMasterManifestMultipleLangs2, mockMediaManifestMultipleLangs2, mockAudioManifestMultipleLangs2)
.then(() => {
/*const m3u8 = mockVod2.getLiveMediaAudioSequences(0, "audio-aacl-96", "en", 0);
const m3u8 = mockVod2.getLiveMediaAudioSequences(0, "audio-aacl-96", "en", 0);
const m3u8_2 = mockVod2.getLiveMediaAudioSequences(0, "audio-aacl-96", "en", 1);
const subStrings = m3u8.split("\n")
const subStrings2 = m3u8_2.split("\n")
expect(subStrings[33]).toEqual("https://vod.streaming.a2d.tv/3e542405-583b-4edc-93ab-eca86427d148/ab92a690-62de-11ed-aa51-c96fb4f9434f_20337209.ism/hls/ab92a690-62de-11ed-aa51-c96fb4f9434f_20337209-textstream_swe=1000-693.webvtt");
expect(subStrings2[32]).toEqual("#EXT-X-DISCONTINUITY")
expect(subStrings2[33]).toEqual("#EXTINF:3.000,");
expect(subStrings2[34]).toEqual("https://d3t8zrj2x5ol3r.cloudfront.net/u/file~text_vtt~dummy.vtt/1/s/webvtt.vtt");*/
expect(subStrings2[34]).toEqual("https://d3t8zrj2x5ol3r.cloudfront.net/u/file~text_vtt~dummy.vtt/1/s/webvtt.vtt");
done();
});
});
Expand Down

0 comments on commit e626574

Please sign in to comment.