-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
level-helper.ts
320 lines (290 loc) · 12.2 KB
/
level-helper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import {
adjustSliding,
computeReloadInterval,
mapFragmentIntersection,
mapPartIntersection,
mergeDetails,
} from '../../../src/controller/level-helper';
import { LevelDetails } from '../../../src/loader/level-details';
import { Fragment, Part } from '../../../src/loader/fragment';
import { LoadStats } from '../../../src/loader/load-stats';
import { PlaylistLevelType } from '../../../src/types/loader';
import * as sinon from 'sinon';
import * as chai from 'chai';
import * as sinonChai from 'sinon-chai';
import { AttrList } from '../../../src/utils/attr-list';
chai.use(sinonChai);
const expect = chai.expect;
const generatePlaylist = (sequenceNumbers, offset = 0, duration = 5) => {
const playlist = new LevelDetails('');
playlist.startSN = sequenceNumbers[0];
playlist.endSN = sequenceNumbers[sequenceNumbers.length - 1];
playlist.fragments = sequenceNumbers.map((n, i) => {
const frag = new Fragment(PlaylistLevelType.MAIN, '');
frag.sn = n;
frag.start = i * 5 + offset;
frag.duration = duration;
return frag;
});
return playlist;
};
const getIteratedSequence = (oldPlaylist, newPlaylist) => {
const actual: number[] = [];
mapFragmentIntersection(oldPlaylist, newPlaylist, (oldFrag, newFrag) => {
if (oldFrag.sn !== newFrag.sn) {
throw new Error('Expected old frag and new frag to have the same SN');
}
actual.push(newFrag.sn as number);
});
return actual;
};
describe('LevelHelper Tests', function () {
let sandbox;
beforeEach(function () {
sandbox = sinon.createSandbox();
});
afterEach(function () {
sandbox.restore();
});
describe('mapSegmentIntersection', function () {
it('iterates over the intersection of the fragment arrays', function () {
const oldPlaylist = generatePlaylist([1, 2, 3, 4, 5]);
const newPlaylist = generatePlaylist([3, 4, 5, 6, 7]);
const actual = getIteratedSequence(oldPlaylist, newPlaylist);
expect(actual).to.deep.equal([3, 4, 5]);
});
it('can iterate with one overlapping fragment', function () {
const oldPlaylist = generatePlaylist([1, 2, 3, 4, 5]);
const newPlaylist = generatePlaylist([5, 6, 7, 8, 9]);
const actual = getIteratedSequence(oldPlaylist, newPlaylist);
expect(actual).to.deep.equal([5]);
});
it('can iterate over the entire segment array', function () {
const oldPlaylist = generatePlaylist([1, 2, 3]);
const newPlaylist = generatePlaylist([1, 2, 3]);
const actual = getIteratedSequence(oldPlaylist, newPlaylist);
expect(actual).to.deep.equal([1, 2, 3]);
});
it('can iterate when overlapping happens at the start of the old playlist', function () {
const oldPlaylist = generatePlaylist([5, 6, 7, 8]);
const newPlaylist = generatePlaylist([3, 4, 5, 6]);
const actual = getIteratedSequence(oldPlaylist, newPlaylist);
expect(actual).to.deep.equal([5, 6]);
});
it('never executes the callback if no intersection exists', function () {
const oldPlaylist = generatePlaylist([1, 2, 3, 4, 5]);
const newPlaylist = generatePlaylist([10, 11, 12]);
const actual = getIteratedSequence(oldPlaylist, newPlaylist);
expect(actual).to.deep.equal([]);
});
});
describe('mapPartIntersection', function () {
it('finds overlapping parts and executes callback for each', function () {
const oldFrags = generatePlaylist([1, 2, 3]).fragments;
const newFrags = generatePlaylist([2, 3, 4]).fragments;
const attr = new AttrList('DURATION=1');
const oldParts: Part[] = [
new Part(attr, oldFrags[1], '', 0),
new Part(attr, oldFrags[1], '', 1),
new Part(attr, oldFrags[1], '', 2),
new Part(attr, oldFrags[2], '', 0),
new Part(attr, oldFrags[2], '', 1),
new Part(attr, oldFrags[2], '', 2),
];
const newParts: Part[] = [
new Part(attr, newFrags[1], '', 0),
new Part(attr, newFrags[1], '', 1),
new Part(attr, newFrags[1], '', 2),
new Part(attr, newFrags[2], '', 0),
new Part(attr, newFrags[2], '', 1),
new Part(attr, newFrags[2], '', 2),
];
const intersectionFn = sinon.spy();
mapPartIntersection(oldParts, newParts, intersectionFn);
expect(intersectionFn).to.have.been.calledThrice;
expect(intersectionFn.firstCall).to.have.been.calledWith(
oldParts[3],
newParts[0]
);
expect(intersectionFn.secondCall).to.have.been.calledWith(
oldParts[4],
newParts[1]
);
expect(intersectionFn.thirdCall).to.have.been.calledWith(
oldParts[5],
newParts[2]
);
});
});
describe('adjustSliding', function () {
// generatePlaylist creates fragments with a duration of 5 seconds
it('adds the start time of the first comment segment to all other segment', function () {
const oldPlaylist = generatePlaylist([1, 2, 3]); // start times: 0, 5, 10
const newPlaylist = generatePlaylist([3, 4, 5]);
adjustSliding(oldPlaylist, newPlaylist);
const actual = newPlaylist.fragments.map((f) => f.start);
expect(actual).to.deep.equal([10, 15, 20]);
});
it('does not apply sliding if no common segments exist', function () {
const oldPlaylist = generatePlaylist([1, 2, 3]);
const newPlaylist = generatePlaylist([5, 6, 7]);
adjustSliding(oldPlaylist, newPlaylist);
const actual = newPlaylist.fragments.map((f) => f.start);
expect(actual).to.deep.equal([0, 5, 10]);
});
it('does not apply sliding when segments meet but do not overlap', function () {
const oldPlaylist = generatePlaylist([1, 2, 3]);
const newPlaylist = generatePlaylist([4, 5, 6]);
adjustSliding(oldPlaylist, newPlaylist);
const actual = newPlaylist.fragments.map((f) => f.start);
expect(actual).to.deep.equal([0, 5, 10]);
});
});
describe('mergeDetails', function () {
it('transfers start times where segments overlap, and extrapolates the start of any new segment', function () {
const oldPlaylist = generatePlaylist([1, 2, 3, 4]); // start times: 0, 5, 10, 15
const newPlaylist = generatePlaylist([2, 3, 4, 5]);
mergeDetails(oldPlaylist, newPlaylist);
const actual = newPlaylist.fragments.map((f) => f.start);
expect(actual).to.deep.equal([5, 10, 15, 20]);
});
it('does not change start times when there is no segment overlap', function () {
const oldPlaylist = generatePlaylist([1, 2, 3]);
const newPlaylist = generatePlaylist([5, 6, 7]);
mergeDetails(oldPlaylist, newPlaylist);
const actual = newPlaylist.fragments.map((f) => f.start);
expect(actual).to.deep.equal([0, 5, 10]);
});
it('does not extrapolate if the new playlist starts before the old', function () {
const oldPlaylist = generatePlaylist([3, 4, 5]);
oldPlaylist.fragments.forEach((f) => {
f.start += 10;
});
const newPlaylist = generatePlaylist([1, 2, 3]);
mergeDetails(oldPlaylist, newPlaylist);
const actual = newPlaylist.fragments.map((f) => f.start);
expect(actual).to.deep.equal([0, 5, 10]);
});
it('merges delta playlist updates', function () {
const oldPlaylist = generatePlaylist([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
const newPlaylist = generatePlaylist([10, 11, 12]);
newPlaylist.skippedSegments = 7;
newPlaylist.startSN = 3;
// @ts-ignore
newPlaylist.fragments.unshift(null, null, null, null, null, null, null);
const merged = generatePlaylist([3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 10);
mergeDetails(oldPlaylist, newPlaylist);
expect(newPlaylist.deltaUpdateFailed).to.equal(false);
expect(newPlaylist.fragments.length).to.equal(merged.fragments.length);
newPlaylist.fragments.forEach((frag, i) => {
expect(
frag,
`Fragment sn: ${frag.sn} does not match expected:
actual: ${JSON.stringify(frag)}
expect: ${JSON.stringify(merged.fragments[i])}`
).to.deep.equal(merged.fragments[i]);
});
});
it('marks failed delta playlist updates', function () {
const oldPlaylist = generatePlaylist([1, 2, 3, 4, 5, 6, 7, 8]);
const newPlaylist = generatePlaylist([10, 11, 12]);
newPlaylist.skippedSegments = 5;
newPlaylist.startSN = 5;
// @ts-ignore
newPlaylist.fragments.unshift(null, null, null, null, null);
// FIXME: An expected offset of 50 would be preferred, but there is nothing to sync playlist start with
const merged = generatePlaylist([10, 11, 12], 0);
mergeDetails(oldPlaylist, newPlaylist);
expect(newPlaylist.deltaUpdateFailed).to.equal(true);
expect(newPlaylist.fragments.length).to.equal(3);
newPlaylist.fragments.forEach((frag, i) => {
expect(
frag,
`Fragment sn: ${frag.sn} does not match expected:
actual: ${JSON.stringify(frag)}
expect: ${JSON.stringify(merged.fragments[i])}`
).to.deep.equal(merged.fragments[i]);
});
});
it('merges initSegments', function () {
const oldPlaylist = generatePlaylist([1, 2, 3]);
const oldInitSegment = new Fragment(PlaylistLevelType.MAIN, '');
oldInitSegment.sn = 'initSegment';
oldInitSegment.relurl = 'init.mp4';
oldPlaylist.fragments.forEach((frag) => {
frag.initSegment = oldInitSegment;
});
oldPlaylist.fragmentHint = new Fragment(PlaylistLevelType.MAIN, '');
oldPlaylist.fragmentHint.sn = 4;
oldPlaylist.fragmentHint.initSegment = oldInitSegment;
const newPlaylist = generatePlaylist([2, 3, 4]);
const newInitSegment = new Fragment(PlaylistLevelType.MAIN, '');
newInitSegment.sn = 'initSegment';
newInitSegment.relurl = 'init.mp4';
newPlaylist.fragments.forEach((frag) => {
frag.initSegment = newInitSegment;
});
newPlaylist.fragmentHint = new Fragment(PlaylistLevelType.MAIN, '');
newPlaylist.fragmentHint.sn = 5;
newPlaylist.fragmentHint.initSegment = newInitSegment;
mergeDetails(oldPlaylist, newPlaylist);
newPlaylist.fragments.forEach((frag, i) => {
expect(
frag.initSegment,
`Fragment sn: ${frag.sn} does not have correct initSegment`
).to.equal(oldInitSegment);
});
expect(
newPlaylist.fragmentHint.initSegment,
'fragmentHint does not have correct initSegment'
).to.equal(oldInitSegment);
});
});
describe('computeReloadInterval', function () {
let sandbox;
beforeEach(function () {
sandbox = sinon.createSandbox();
sandbox.stub(performance, 'now').returns(0);
});
afterEach(function () {
sandbox.restore();
});
it('returns the targetduration of the new level if available', function () {
const newPlaylist = generatePlaylist([3, 4], 0, 6);
newPlaylist.targetduration = 5;
newPlaylist.updated = true;
const actual = computeReloadInterval(newPlaylist);
expect(actual).to.equal(5000);
});
it('halves the reload interval if the playlist contains the same segments', function () {
const newPlaylist = generatePlaylist([1, 2]);
newPlaylist.updated = false;
newPlaylist.targetduration = 5;
const actual = computeReloadInterval(newPlaylist);
expect(actual).to.equal(2500);
});
it('rounds the reload interval', function () {
const newPlaylist = generatePlaylist([3, 4], 0, 10);
newPlaylist.targetduration = 5.9999;
newPlaylist.updated = true;
const actual = computeReloadInterval(newPlaylist);
expect(actual).to.equal(6000);
});
it('returns a minimum of half the target duration', function () {
const newPlaylist = generatePlaylist([3, 4]);
newPlaylist.targetduration = 5;
newPlaylist.updated = false;
const actual = computeReloadInterval(newPlaylist);
expect(actual).to.equal(2500);
});
it('returns the last fragment duration when distance to live edge is less than or equal to four target durations', function () {
const newPlaylist = generatePlaylist([3, 4], 0, 2);
newPlaylist.targetduration = 5;
newPlaylist.updated = true;
const actual = computeReloadInterval(newPlaylist, 20000);
expect(actual).to.equal(5000);
const actualLow = computeReloadInterval(newPlaylist, 14000);
expect(actualLow).to.equal(2000);
});
});
});