Skip to content

Commit

Permalink
Added a function to get delta times for each media sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
birme committed Jan 19, 2021
1 parent b081f06 commit dfe834c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
27 changes: 26 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class HLSVod {
this.discontinuities = {};
this.rangeMetadata = null;
this.matchedBandwidths = {};
this.deltaTimes = [];
}

toJSON() {
Expand All @@ -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);
}
Expand All @@ -78,6 +80,7 @@ class HLSVod {
this.usageProfileMapping = de.usageProfileMapping;
this.usageProfileMappingRev = de.usageProfileMappingRev;
this.discontinuities = de.discontinuities;
this.deltaTimes = de.deltaTimes;
}

/**
Expand Down Expand Up @@ -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 ----

Expand Down Expand Up @@ -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];
Expand All @@ -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();
}
Expand Down
34 changes: 34 additions & 0 deletions spec/hlsvod_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})
});
});

0 comments on commit dfe834c

Please sign in to comment.