Skip to content

Commit

Permalink
Fix/reload update pt2 (#38)
Browse files Browse the repository at this point in the history
* chore: small update/fix to reload function

Co-authored-by: Oscar Nord <[email protected]>

* chore: added better slice targeting in reload()

...for reloading segments in front of current ones

* chore: added a new way to map reload segments by

* chore: updated specs for reload changes

* chore: reverted changes to the concat method.

now the only change is reload insert infront and the specs

* docs: clean up comments

Co-authored-by: Oscar Nord <[email protected]>
  • Loading branch information
Nfrederiksen and oscnord authored Aug 27, 2021
1 parent 1d038c0 commit 9b967f2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
19 changes: 16 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ class HLSVod {
* @param {number} mediaSeqNo The media Sequence index that is the live index.
* @param {object} additionalSegments New group of segments to merge with a possible subset of this.segments
* @param {object} additionalAudioSegments New group of audio segments to merge with a possible subset of this.segments
* @param {boolean} insertAfter Whether the additional segments are to be added in front of the live index or behind.
* @param {boolean} insertAfter Whether the additional segments are to be added in front of the live index or behind
* @returns A promise that new Media Sequences have been made
*/
reload(mediaSeqNo, additionalSegments, additionalAudioSegments, insertAfter) {
Expand All @@ -339,39 +339,52 @@ class HLSVod {
if (!insertAfter) {
// If there is anything to slice
if(mediaSeqNo > 0) {
allBandwidths.forEach(bw => this.segments[bw] = this.segments[bw].slice(mediaSeqNo - 1));
let targetUri = this.mediaSequences[mediaSeqNo].segments[allBandwidths[0]][0].uri;
let targetPos = 0;
for (let i = mediaSeqNo; i < this.segments[allBandwidths[0]].length; i++) {
if (this.segments[allBandwidths[0]][i].uri === targetUri) {
targetPos = i;
}
}
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
}

// Find nearest BW in SFL and prepend them to the corresponding segments bandwidth
allBandwidths.forEach(bw => {
let nearestBw = this._getNearestBandwidthInList(bw, Object.keys(additionalSegments));
this.segments[bw] = additionalSegments[nearestBw].concat(this.segments[bw]);
});

if (!this._isEmpty(this.audioSegments)) {
// TODO: Prepend segs to all audio tracks, in all audio groups
}

} else {
if(mediaSeqNo >= 0) {
let size = this.mediaSequences[mediaSeqNo].segments[allBandwidths[0]].length;
let targetUri = this.mediaSequences[mediaSeqNo].segments[allBandwidths[0]][0].uri;
let targetPos = 0;
for (let i = mediaSeqNo; i < this.segments[allBandwidths[0]].length; i++) {
// Should be True once
if (this.segments[allBandwidths[0]][i].uri === targetUri) {
targetPos = i;
}
}
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
}

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 segs to all audio tracks, in all audio groups
}
Expand Down
28 changes: 17 additions & 11 deletions spec/hlsvod_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2745,7 +2745,7 @@ describe("HLSVod reload media sequences", () => {
}).then(() => {
mockVod2.load(mockMasterManifest2, mockMediaManifest2)
.then(() => {
mockVod2.reload(0, vod1segments)
mockVod2.reload(0, vod1segments, null)
.then(() => {
expect(vod1segments).toEqual(mockVod2.getLiveMediaSequenceSegments(0));
done();
Expand All @@ -2763,6 +2763,7 @@ describe("HLSVod reload media sequences", () => {
.load(mockMasterManifest1, mockMediaManifest1)
.then(() => {
vod1segments = mockVod.getLiveMediaSequenceSegments(1);
Object.keys(vod1segments).forEach( bw => vod1segments[bw].unshift({discontinuity: true}));
}).then(() => {
mockVod2.load(mockMasterManifest2, mockMediaManifest2)
.then(() => {
Expand All @@ -2784,14 +2785,15 @@ describe("HLSVod reload media sequences", () => {
.load(mockMasterManifest1, mockMediaManifest1)
.then(() => {
vod1segments = mockVod.getLiveMediaSequenceSegments(1);
Object.keys(vod1segments).forEach( bw => vod1segments[bw].push({discontinuity: true}));
}).then(() => {
mockVod2.load(mockMasterManifest2, mockMediaManifest2)
.then(() => {
let topSegment = mockVod2.getLiveMediaSequenceSegments(6)['401000'][0];
mockVod2.reload(7, vod1segments)
let topSegmentPreReload = mockVod2.getLiveMediaSequenceSegments(6)['401000'][0];
mockVod2.reload(6, vod1segments, null)
.then(() => {
let size = mockVod2.getLiveMediaSequenceSegments(1)['401000'].length;
expect(mockVod2.getLiveMediaSequenceSegments(1)['401000'][size - 1]).toEqual(topSegment);
expect(mockVod2.getLiveMediaSequenceSegments(1)['401000'][size - 1]).toEqual(topSegmentPreReload);
done();
});
});
Expand All @@ -2807,14 +2809,15 @@ describe("HLSVod reload media sequences", () => {
.load(mockMasterManifest1, mockMediaManifest1)
.then(() => {
vod1segments = mockVod.getLiveMediaSequenceSegments(1);
Object.keys(vod1segments).forEach( bw => vod1segments[bw].unshift({discontinuity: true}));
}).then(() => {
mockVod2.load(mockMasterManifest2, mockMediaManifest2)
.then(() => {
let size = mockVod2.getLiveMediaSequenceSegments(7)['401000'].length;
let bottomSegment = mockVod2.getLiveMediaSequenceSegments(7)['401000'][size -1];
let bottomSegmentPreReload = mockVod2.getLiveMediaSequenceSegments(7)['401000'][size -1];
mockVod2.reload(7, vod1segments, null, true)
.then(() => {
expect(mockVod2.getLiveMediaSequenceSegments(mockVod2.getLiveMediaSequencesCount() - 2)['401000'][0]).toEqual(bottomSegment);
expect(mockVod2.getLiveMediaSequenceSegments(mockVod2.getLiveMediaSequencesCount() - 2)['401000'][0]).toEqual(bottomSegmentPreReload);
done();
});
});
Expand All @@ -2830,14 +2833,15 @@ describe("HLSVod reload media sequences", () => {
.load(mockMasterManifest1, mockMediaManifest1)
.then(() => {
vod1segments = mockVod.getLiveMediaSequenceSegments(1);
Object.keys(vod1segments).forEach( bw => vod1segments[bw].push({discontinuity: true}));
}).then(() => {
mockVod2.load(mockMasterManifest2, mockMediaManifest2)
.then(() => {
let topSegment = mockVod2.getLiveMediaSequenceSegments(12)['401000'][0];
mockVod2.reload(13, vod1segments)
let topSegmentPreReload = mockVod2.getLiveMediaSequenceSegments(12)['401000'][0];
mockVod2.reload(12, vod1segments, null)
.then(() => {
let size = mockVod2.getLiveMediaSequenceSegments(1)['401000'].length;
expect(mockVod2.getLiveMediaSequenceSegments(1)['401000'][size - 1]).toEqual(topSegment);
expect(mockVod2.getLiveMediaSequenceSegments(1)['401000'][size - 1]).toEqual(topSegmentPreReload);
done();
});
});
Expand All @@ -2853,14 +2857,16 @@ describe("HLSVod reload media sequences", () => {
.load(mockMasterManifest1, mockMediaManifest1)
.then(() => {
vod1segments = mockVod.getLiveMediaSequenceSegments(1);
Object.keys(vod1segments).forEach( bw => vod1segments[bw].unshift({discontinuity: true}));
}).then(() => {
mockVod2.load(mockMasterManifest2, mockMediaManifest2)
.then(() => {
let size = mockVod2.getLiveMediaSequenceSegments(12)['401000'].length;
let bottomSegment = mockVod2.getLiveMediaSequenceSegments(12)['401000'][size -1];

let bottomSegmentPreReload = mockVod2.getLiveMediaSequenceSegments(12)['401000'][size -1];
mockVod2.reload(12, vod1segments, null, true)
.then(() => {
expect(mockVod2.getLiveMediaSequenceSegments(mockVod2.getLiveMediaSequencesCount() - 2)['401000'][0]).toEqual(bottomSegment);
expect(mockVod2.getLiveMediaSequenceSegments(mockVod2.getLiveMediaSequencesCount() - 2)['401000'][0]).toEqual(bottomSegmentPreReload);
done();
});
});
Expand Down

0 comments on commit 9b967f2

Please sign in to comment.