Skip to content

Commit

Permalink
Feat: Option for Always Mapping Vod Bitrates By Nearest Value (#113)
Browse files Browse the repository at this point in the history
* add alwaysMapBandwidthByNearest option

* update unittests
  • Loading branch information
Nfrederiksen authored Aug 8, 2023
1 parent 60faf5a commit b27e676
Show file tree
Hide file tree
Showing 10 changed files with 3,727 additions and 702 deletions.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class HLSVod {
if (opts && opts.expectedSubtitleTracks) {
this.expectedSubtitleTracks = opts.expectedSubtitleTracks;
}
if (opts && opts.alwaysMapBandwidthByNearest) {
this.alwaysMapBandwidthByNearest = opts.alwaysMapBandwidthByNearest;
}
this.videoSequencesCount = 0;
this.audioSequencesCount = 0;
this.defaultAudioGroupAndLang = null;
Expand Down Expand Up @@ -116,6 +119,7 @@ class HLSVod {
subtitleSequencesCount: this.subtitleSequencesCount,
mediaStartExcessTime: this.mediaStartExcessTime,
audioCodecsMap: this.audioCodecsMap,
alwaysMapBandwidthByNearest: this.alwaysMapBandwidthByNearest
};
return JSON.stringify(serialized);
}
Expand Down Expand Up @@ -167,6 +171,7 @@ class HLSVod {
this.subtitleSequencesCount = de.subtitleSequencesCount
this.mediaStartExcessTime = de.mediaStartExcessTime;
this.audioCodecsMap = de.audioCodecsMap;
this.alwaysMapBandwidthByNearest = de.alwaysMapBandwidthByNearest;
}

/**
Expand All @@ -186,7 +191,7 @@ class HLSVod {
baseUrl = m[1] + "/";
}
const HAS_AUDIO_DEFAULTS = this.defaultAudioGroupAndLang === null ? false : true;
if (this.previousVod && this.previousVod.getBandwidths().length === m3u.items.StreamItem.length) {
if (!this.alwaysMapBandwidthByNearest && this.previousVod && this.previousVod.getBandwidths().length === m3u.items.StreamItem.length) {
debug(`Previous VOD bandwidths matches amount of current. A mapping is possible`);
const previousBandwidths = this.previousVod.getBandwidths().sort((a, b) => a - b);
this.usageProfileMapping = {};
Expand Down
26 changes: 26 additions & 0 deletions spec/hlsvod_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,9 @@ describe("HLSVod with not equal usage profiles", () => {
mockMasterManifest.push(function () {
return fs.createReadStream("testvectors/hls_abr4/master.m3u8");
});
mockMasterManifest.push(function () {
return fs.createReadStream("testvectors/hls_abr6/master.m3u8");
})
mockMediaManifest.push(function (bandwidth) {
return fs.createReadStream("testvectors/hls1/" + bandwidth + ".m3u8");
});
Expand All @@ -910,6 +913,9 @@ describe("HLSVod with not equal usage profiles", () => {
mockMediaManifest.push(function (bandwidth) {
return fs.createReadStream("testvectors/hls_abr4/" + bandwidth + ".m3u8");
});
mockMediaManifest.push(function (bandwidth) {
return fs.createReadStream("testvectors/hls_abr6/" + bandwidth + ".m3u8");
});
});

it("can find a correct match", (done) => {
Expand Down Expand Up @@ -1200,6 +1206,26 @@ describe("HLSVod with not equal usage profiles", () => {
done();
});
});

it("can match by true nearest when options-> alwaysMapBandwidthByNearest is true", (done) => {
mockVod = new HLSVod("http://mock.com/mock.m3u8", null, 0,0, null, { alwaysMapBandwidthByNearest: 1 });
mockVod2 = new HLSVod("http://mock.com/mock2.m3u8", null, 0,0, null, { alwaysMapBandwidthByNearest: 1 });
mockVod
.load(mockMasterManifest[0], mockMediaManifest[0])
.then(() => {
return mockVod2.loadAfter(mockVod, mockMasterManifest[7], mockMediaManifest[7]);
})
.then(() => {
const seqSegments = mockVod2.getLiveMediaSequenceSegments(0);
const lastIdx = seqSegments["1497000"].length - 1;
expect(seqSegments["1497000"][lastIdx].uri).toEqual("https://mock.vod.media/segment1_0_av.ts");
expect(seqSegments["3496000"][lastIdx].uri).toEqual("https://mock.vod.media/segment1_1_av.ts");
expect(seqSegments["4497000"][lastIdx].uri).toEqual("https://mock.vod.media/segment1_2_av.ts");
expect(seqSegments["5544000"][lastIdx].uri).toEqual("https://mock.vod.media/segment1_3_av.ts");
expect(seqSegments["6655000"][lastIdx].uri).toEqual("https://mock.vod.media/segment1_4_av.ts");
done();
});
});
});
/**
* Changes:
Expand Down
Loading

0 comments on commit b27e676

Please sign in to comment.