Skip to content

Commit

Permalink
feat: Implementation of support for additional audio segments in relo…
Browse files Browse the repository at this point in the history
…ad (#115)
  • Loading branch information
Johan Lautakoski authored Aug 16, 2023
1 parent b6b7d8a commit e6a0ad0
Show file tree
Hide file tree
Showing 8 changed files with 567 additions and 8 deletions.
122 changes: 114 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,36 @@ class HLSVod {
allBandwidths.forEach((bw) => (this.segments[bw] = this.segments[bw].slice(targetPos)));
}

if (!this._isEmpty(this.audioSegments)) {
// TODO: slice all audio tracks, in all audio groups
if (!this._isEmpty(this.audioSegments) && additionalAudioSegments) {
const groupIds = this.getAudioGroups();
const lang = this.getAudioLangsForAudioGroup(groupIds[0])[0];

if (mediaSeqNo > 0) {
let targetUri = "";
let size = this.mediaSequences[mediaSeqNo].audioSegments[groupIds[0]][lang].length;
for (let idx = size - 1; idx >= 0; idx--) {
const segItem = this.mediaSequences[mediaSeqNo].audioSegments[groupIds[0]][lang][idx];
if (segItem.uri) {
targetUri = segItem.uri;
break;
}
}
let targetPos = 0;
for (let i = mediaSeqNo; i < this.audioSegments[groupIds[0]][lang].length; i++) {
if (this.audioSegments[groupIds[0]][lang][i].uri === targetUri) {
targetPos = i;
break;
}
}
for (let i = 0; i < groupIds.length; i++) {
const groupId = groupIds[i];
const langs = this.getAudioLangsForAudioGroup(groupId);
for (let j = 0; j < langs.length; j++) {
const lang = langs[j];
this.audioSegments[groupId][lang] = this.audioSegments[groupId][lang].slice(targetPos);
}
}
}
}

// Find nearest BW in SFL and prepend them to the corresponding segments bandwidth
Expand All @@ -565,8 +593,34 @@ class HLSVod {
this.segments[bw] = additionalSegments[nearestBw].concat(this.segments[bw]);
});

if (!this._isEmpty(this.audioSegments)) {
// TODO: Prepend segments to all audio tracks, in all audio groups
if (!this._isEmpty(this.audioSegments) && additionalAudioSegments) {
const groupIdsInVod = this.getAudioGroups();
const groupIdsInSegments = Object.keys(additionalAudioSegments)

for (let i = 0; i < groupIdsInSegments.length; i++) {
let groupIdForVod = groupIdsInSegments[i];
let indexOfGroupId = groupIdsInVod.indexOf(groupIdsInSegments[i]);
if (indexOfGroupId < 0) {
groupIdForVod = groupIdsInVod[0]
} else {
groupIdForVod = groupIdsInVod[indexOfGroupId]
}

const langsInVod = this.getAudioLangsForAudioGroup(groupIdForVod);
const langsInSegment = Object.keys(additionalAudioSegments[groupIdsInSegments[i]]);

for (let j = 0; j < langsInSegment.length; j++) {
let langForVod = langsInSegment[j];
let indexOfLang = langsInVod.indexOf(langsInSegment[j]);
if (indexOfLang < 0) {
langForVod = langsInVod[0]
} else {
langForVod = langsInVod[indexOfLang]
}
this.audioSegments[groupIdForVod][langForVod] = additionalAudioSegments[groupIdsInSegments[i]][langsInSegment[j]].concat(this.audioSegments[groupIdForVod][langForVod]);

}
}
}
} else {
if (mediaSeqNo >= 0) {
Expand All @@ -582,25 +636,73 @@ class HLSVod {
allBandwidths.forEach((bw) => (this.segments[bw] = this.segments[bw].slice(targetPos, targetPos + size)));
}

if (!this._isEmpty(this.audioSegments)) {
// TODO: slice all audio tracks, in all audio groups
if (!this._isEmpty(this.audioSegments) && additionalAudioSegments) {
const groupIds = this.getAudioGroups();
const lang = this.getAudioLangsForAudioGroup(groupIds[0])[0];
if (mediaSeqNo >= 0) {
let size = this.mediaSequences[mediaSeqNo].audioSegments[groupIds[0]][lang].length;
let targetUri = this.mediaSequences[mediaSeqNo].audioSegments[groupIds[0]][lang][0].uri;
let targetPos = 0;
for (let i = mediaSeqNo; i < this.audioSegments[groupIds[0]][lang].length; i++) {
if (this.audioSegments[groupIds[0]][lang][i].uri === targetUri) {
targetPos = i;
break;
}
}
for (let i = 0; i < groupIds.length; i++) {
const groupId = groupIds[i];
const langs = this.getAudioLangsForAudioGroup(groupId);
for (let j = 0; j < langs.length; j++) {
const lang = langs[j];
this.audioSegments[groupId][lang] = this.audioSegments[groupId][lang].slice(targetPos, targetPos + size);
}
}
}
}

allBandwidths.forEach((bw) => {
let nearestBw = this._getNearestBandwidthInList(bw, Object.keys(additionalSegments));
this.segments[bw] = this.segments[bw].concat(additionalSegments[nearestBw]);
});

if (!this._isEmpty(this.audioSegments)) {
// TODO: Prepend segments to all audio tracks, in all audio groups
if (!this._isEmpty(this.audioSegments) && additionalAudioSegments) {
const groupIdsInVod = this.getAudioGroups();
const groupIdsInSegments = Object.keys(additionalAudioSegments)

for (let i = 0; i < groupIdsInSegments.length; i++) {
let groupIdForVod = groupIdsInSegments[i];
let indexOfGroupId = groupIdsInVod.indexOf(groupIdsInSegments[i]);
if (indexOfGroupId < 0) {
groupIdForVod = groupIdsInVod[0]
} else {
groupIdForVod = groupIdsInVod[indexOfGroupId]
}

const langsInVod = this.getAudioLangsForAudioGroup(groupIdForVod);
const langsInSegment = Object.keys(additionalAudioSegments[groupIdsInSegments[i]]);

for (let j = 0; j < langsInSegment.length; j++) {
let langForVod = langsInSegment[j];
let indexOfLang = langsInVod.indexOf(langsInSegment[j]);
if (indexOfLang < 0) {
langForVod = langsInVod[0]
} else {
langForVod = langsInVod[indexOfLang]
}
this.audioSegments[groupIdForVod][langForVod] = this.audioSegments[groupIdForVod][langForVod].concat(additionalAudioSegments[groupIdsInSegments[i]][langsInSegment[j]]);

}
}
}
}

// Clean up/Reset HLSVod data since we are going to create new data
this.mediaSequences = [];
this.audioSequences = [];
this.mediaSequenceValues = {};
this.mediaSequenceValuesAudio = {};
this.discontinuities = {};
this.discontinuitiesAudio = {};
this.deltaTimes = [];
this.deltaTimesAudio = [];

Expand Down Expand Up @@ -664,6 +766,10 @@ class HLSVod {
return this.segments;
}

getAudioSegments() {
return this.audioSegments;
}

/**
* Get all audio segments (duration, uri) for a specific media sequence based on audio group and lang
*
Expand Down
Loading

0 comments on commit e6a0ad0

Please sign in to comment.