From dfe834c6ad70f04d9fe1deb4e250af385da833c2 Mon Sep 17 00:00:00 2001 From: birme Date: Tue, 19 Jan 2021 15:01:32 +0100 Subject: [PATCH] Added a function to get delta times for each media sequence --- index.js | 27 ++++++++++++++++++++++++++- spec/hlsvod_spec.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d56c74f..f3d51fd 100644 --- a/index.js +++ b/index.js @@ -32,6 +32,7 @@ class HLSVod { this.discontinuities = {}; this.rangeMetadata = null; this.matchedBandwidths = {}; + this.deltaTimes = []; } toJSON() { @@ -51,7 +52,8 @@ class HLSVod { startTimeOffset: this.startTimeOffset, usageProfileMapping: this.usageProfileMapping, usageProfileMappingRev: this.usageProfileMappingRev, - discontinuities: this.discontinuities + discontinuities: this.discontinuities, + deltaTimes: this.deltaTimes, }; return JSON.stringify(serialized); } @@ -78,6 +80,7 @@ class HLSVod { this.usageProfileMapping = de.usageProfileMapping; this.usageProfileMappingRev = de.usageProfileMappingRev; this.discontinuities = de.discontinuities; + this.deltaTimes = de.deltaTimes; } /** @@ -427,6 +430,16 @@ class HLSVod { return this.discontinuities[this.mediaSequences.length - 1]; } + /** + * Get the delta times for each media sequence. + * Returns the sum of the segments' duration for each media sequence and the diff to the previous + * media sequence. E.g. [ 0, 2, 2, -2, ... ] means that the second media sequence is 2 second longer + * than the first one. The fourth one is 2 seconds shorter than the previous media sequence + * + */ + getDeltaTimes() { + return this.deltaTimes.map(o => o.interval); + } // ----- PRIVATE METHODS BELOW ---- @@ -613,6 +626,10 @@ class HLSVod { reject('Failed to init media sequences'); } else { let discSeqNo = 0; + this.deltaTimes.push({ + interval: 0, + }); + let lastMseqSum = 0; for (let seqNo = 0; seqNo < this.mediaSequences.length; seqNo++) { const mseq = this.mediaSequences[seqNo]; const bwIdx = Object.keys(mseq.segments)[0]; @@ -622,6 +639,14 @@ class HLSVod { debug(`Increasing discont sequence ${discSeqNo}`); } this.discontinuities[seqNo] = discSeqNo; + const mseqSum = mseq.segments[bwIdx] ? mseq.segments[bwIdx].map(o => o.duration ? o.duration : 0).reduce((acc, curr) => acc + curr, 0) : 0; + if (seqNo > 0) { + const interval = mseqSum - lastMseqSum; + this.deltaTimes.push({ + interval: interval, + }); + } + lastMseqSum = mseqSum; } resolve(); } diff --git a/spec/hlsvod_spec.js b/spec/hlsvod_spec.js index 33705ff..7987d1b 100644 --- a/spec/hlsvod_spec.js +++ b/spec/hlsvod_spec.js @@ -1515,3 +1515,37 @@ describe("HLSVod time metadata", () => { }); }); }); + +describe("HLSVod delta time", () => { + let mockMasterManifest; + let mockMediaManifest; + + beforeEach(() => { + mockMasterManifest = function() { + return fs.createReadStream('testvectors/hls1/master.m3u8'); + }; + + mockMediaManifest = function(bandwidth) { + return fs.createReadStream('testvectors/hls1/' + bandwidth + '.m3u8'); + }; + }); + + it("is calculated and available for each media sequence", done => { + mockVod = new HLSVod('http://mock.com/mock.m3u8'); + mockVod2 = new HLSVod('http://mock.com/mock2.m3u8'); + mockVod.load(mockMasterManifest, mockMediaManifest) + .then(() => { + return mockVod2.loadAfter(mockVod, mockMasterManifest, mockMediaManifest); + }).then(() => { + let mockVod2Durations = []; + for (let i = 0; i < mockVod2.getLiveMediaSequencesCount(); i++) { + mockVod2Durations.push(mockVod2.getLiveMediaSequenceSegments(i)['1497000'] + .map(o => o.duration ? o.duration : 0).reduce((acc, dur) => acc + dur, 0)); + } + const deltas2 = mockVod2.getDeltaTimes(); + expect(mockVod2Durations.slice(0, 9)).toEqual([ 51.266, 51.266, 51.266, 51.266, 51.266, 54, 54, 54, 54 ]); + expect(deltas2.slice(0, 9)).toEqual([ 0, 0, 0, 0, 0, 2.7340000000000018, 0, 0, 0 ]); + done(); + }) + }); +}); \ No newline at end of file