playlist.test.js
15.1 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
import Playlist from '../src/playlist';
import PlaylistLoader from '../src/playlist-loader';
import QUnit from 'qunit';
import xhrFactory from '../src/xhr';
import { useFakeEnvironment } from './test-helpers';
QUnit.module('Playlist Duration');
QUnit.test('total duration for live playlists is Infinity', function() {
let duration = Playlist.duration({
segments: [{
duration: 4,
uri: '0.ts'
}]
});
QUnit.equal(duration, Infinity, 'duration is infinity');
});
QUnit.module('Playlist Interval Duration');
QUnit.test('accounts for non-zero starting VOD media sequences', function() {
let duration = Playlist.duration({
mediaSequence: 10,
endList: true,
segments: [{
duration: 10,
uri: '0.ts'
}, {
duration: 10,
uri: '1.ts'
}, {
duration: 10,
uri: '2.ts'
}, {
duration: 10,
uri: '3.ts'
}]
});
QUnit.equal(duration, 4 * 10, 'includes only listed segments');
});
QUnit.test('uses timeline values when available', function() {
let duration = Playlist.duration({
mediaSequence: 0,
endList: true,
segments: [{
start: 0,
uri: '0.ts'
}, {
duration: 10,
end: 2 * 10 + 2,
uri: '1.ts'
}, {
duration: 10,
end: 3 * 10 + 2,
uri: '2.ts'
}, {
duration: 10,
end: 4 * 10 + 2,
uri: '3.ts'
}]
}, 4);
QUnit.equal(duration, 4 * 10 + 2, 'used timeline values');
});
QUnit.test('works when partial timeline information is available', function() {
let duration = Playlist.duration({
mediaSequence: 0,
endList: true,
segments: [{
start: 0,
uri: '0.ts'
}, {
duration: 9,
uri: '1.ts'
}, {
duration: 10,
uri: '2.ts'
}, {
duration: 10,
start: 30.007,
end: 40.002,
uri: '3.ts'
}, {
duration: 10,
end: 50.0002,
uri: '4.ts'
}]
}, 5);
QUnit.equal(duration, 50.0002, 'calculated with mixed intervals');
});
QUnit.test('uses timeline values for the expired duration of live playlists', function() {
let playlist = {
mediaSequence: 12,
segments: [{
duration: 10,
end: 120.5,
uri: '0.ts'
}, {
duration: 9,
uri: '1.ts'
}]
};
let duration;
duration = Playlist.duration(playlist, playlist.mediaSequence);
QUnit.equal(duration, 110.5, 'used segment end time');
duration = Playlist.duration(playlist, playlist.mediaSequence + 1);
QUnit.equal(duration, 120.5, 'used segment end time');
duration = Playlist.duration(playlist, playlist.mediaSequence + 2);
QUnit.equal(duration, 120.5 + 9, 'used segment end time');
});
QUnit.test('looks outside the queried interval for live playlist timeline values',
function() {
let playlist = {
mediaSequence: 12,
segments: [{
duration: 10,
uri: '0.ts'
}, {
duration: 9,
end: 120.5,
uri: '1.ts'
}]
};
let duration;
duration = Playlist.duration(playlist, playlist.mediaSequence);
QUnit.equal(duration, 120.5 - 9 - 10, 'used segment end time');
});
QUnit.test('ignores discontinuity sequences later than the end', function() {
let duration = Playlist.duration({
mediaSequence: 0,
discontinuityStarts: [1, 3],
segments: [{
duration: 10,
uri: '0.ts'
}, {
discontinuity: true,
duration: 9,
uri: '1.ts'
}, {
duration: 10,
uri: '2.ts'
}, {
discontinuity: true,
duration: 10,
uri: '3.ts'
}]
}, 2);
QUnit.equal(duration, 19, 'excluded the later segments');
});
QUnit.test('handles trailing segments without timeline information', function() {
let duration;
let playlist = {
mediaSequence: 0,
endList: true,
segments: [{
start: 0,
end: 10.5,
uri: '0.ts'
}, {
duration: 9,
uri: '1.ts'
}, {
duration: 10,
uri: '2.ts'
}, {
start: 29.45,
end: 39.5,
uri: '3.ts'
}]
};
duration = Playlist.duration(playlist, 3);
QUnit.equal(duration, 29.45, 'calculated duration');
duration = Playlist.duration(playlist, 2);
QUnit.equal(duration, 19.5, 'calculated duration');
});
QUnit.test('uses timeline intervals when segments have them', function() {
let duration;
let playlist = {
mediaSequence: 0,
segments: [{
start: 0,
end: 10,
uri: '0.ts'
}, {
duration: 9,
uri: '1.ts'
}, {
start: 20.1,
end: 30.1,
duration: 10,
uri: '2.ts'
}]
};
duration = Playlist.duration(playlist, 2);
QUnit.equal(duration, 20.1, 'used the timeline-based interval');
duration = Playlist.duration(playlist, 3);
QUnit.equal(duration, 30.1, 'used the timeline-based interval');
});
QUnit.test('counts the time between segments as part of the earlier segment\'s duration',
function() {
let duration = Playlist.duration({
mediaSequence: 0,
endList: true,
segments: [{
start: 0,
end: 10,
uri: '0.ts'
}, {
start: 10.1,
end: 20.1,
duration: 10,
uri: '1.ts'
}]
}, 1);
QUnit.equal(duration, 10.1, 'included the segment gap');
});
QUnit.test('accounts for discontinuities', function() {
let duration = Playlist.duration({
mediaSequence: 0,
endList: true,
discontinuityStarts: [1],
segments: [{
duration: 10,
uri: '0.ts'
}, {
discontinuity: true,
duration: 10,
uri: '1.ts'
}]
}, 2);
QUnit.equal(duration, 10 + 10, 'handles discontinuities');
});
QUnit.test('a non-positive length interval has zero duration', function() {
let playlist = {
mediaSequence: 0,
discontinuityStarts: [1],
segments: [{
duration: 10,
uri: '0.ts'
}, {
discontinuity: true,
duration: 10,
uri: '1.ts'
}]
};
QUnit.equal(Playlist.duration(playlist, 0), 0, 'zero-length duration is zero');
QUnit.equal(Playlist.duration(playlist, 0, false), 0, 'zero-length duration is zero');
QUnit.equal(Playlist.duration(playlist, -1), 0, 'negative length duration is zero');
});
QUnit.module('Playlist Seekable');
QUnit.test('calculates seekable time ranges from the available segments', function() {
let playlist = {
mediaSequence: 0,
segments: [{
duration: 10,
uri: '0.ts'
}, {
duration: 10,
uri: '1.ts'
}],
endList: true
};
let seekable = Playlist.seekable(playlist);
QUnit.equal(seekable.length, 1, 'there are seekable ranges');
QUnit.equal(seekable.start(0), 0, 'starts at zero');
QUnit.equal(seekable.end(0), Playlist.duration(playlist), 'ends at the duration');
});
QUnit.test('master playlists have empty seekable ranges', function() {
let seekable = Playlist.seekable({
playlists: [{
uri: 'low.m3u8'
}, {
uri: 'high.m3u8'
}]
});
QUnit.equal(seekable.length, 0, 'no seekable ranges from a master playlist');
});
QUnit.test('seekable end is three target durations from the actual end of live playlists',
function() {
let seekable = Playlist.seekable({
mediaSequence: 0,
segments: [{
duration: 7,
uri: '0.ts'
}, {
duration: 10,
uri: '1.ts'
}, {
duration: 10,
uri: '2.ts'
}, {
duration: 10,
uri: '3.ts'
}]
});
QUnit.equal(seekable.length, 1, 'there are seekable ranges');
QUnit.equal(seekable.start(0), 0, 'starts at zero');
QUnit.equal(seekable.end(0), 7, 'ends three target durations from the last segment');
});
QUnit.test('only considers available segments', function() {
let seekable = Playlist.seekable({
mediaSequence: 7,
segments: [{
uri: '8.ts',
duration: 10
}, {
uri: '9.ts',
duration: 10
}, {
uri: '10.ts',
duration: 10
}, {
uri: '11.ts',
duration: 10
}]
});
QUnit.equal(seekable.length, 1, 'there are seekable ranges');
QUnit.equal(seekable.start(0), 0, 'starts at the earliest available segment');
QUnit.equal(seekable.end(0),
10,
'ends three target durations from the last available segment');
});
QUnit.test('seekable end accounts for non-standard target durations', function() {
let seekable = Playlist.seekable({
targetDuration: 2,
mediaSequence: 0,
segments: [{
duration: 2,
uri: '0.ts'
}, {
duration: 2,
uri: '1.ts'
}, {
duration: 1,
uri: '2.ts'
}, {
duration: 2,
uri: '3.ts'
}, {
duration: 2,
uri: '4.ts'
}]
});
QUnit.equal(seekable.start(0), 0, 'starts at the earliest available segment');
QUnit.equal(seekable.end(0),
9 - (2 + 2 + 1),
'allows seeking no further than three segments from the end');
});
QUnit.module('Playlist Media Index For Time', {
beforeEach() {
this.env = useFakeEnvironment();
this.clock = this.env.clock;
this.requests = this.env.requests;
this.fakeHls = {
xhr: xhrFactory()
};
},
afterEach() {
this.env.restore();
}
});
QUnit.test('can get media index by playback position for non-live videos', function() {
let media;
let loader = new PlaylistLoader('media.m3u8', this.fakeHls);
loader.load();
this.requests.shift().respond(200, null,
'#EXTM3U\n' +
'#EXT-X-MEDIA-SEQUENCE:0\n' +
'#EXTINF:4,\n' +
'0.ts\n' +
'#EXTINF:5,\n' +
'1.ts\n' +
'#EXTINF:6,\n' +
'2.ts\n' +
'#EXT-X-ENDLIST\n'
);
media = loader.media();
QUnit.equal(Playlist.getMediaIndexForTime_(media, -1), 0,
'the index is never less than zero');
QUnit.equal(Playlist.getMediaIndexForTime_(media, 0), 0, 'time zero is index zero');
QUnit.equal(Playlist.getMediaIndexForTime_(media, 3), 0, 'time three is index zero');
QUnit.equal(Playlist.getMediaIndexForTime_(media, 10), 2, 'time 10 is index 2');
QUnit.equal(Playlist.getMediaIndexForTime_(media, 22), 2,
'time greater than the length is index 2');
});
QUnit.test('returns the lower index when calculating for a segment boundary', function() {
let media;
let loader = new PlaylistLoader('media.m3u8', this.fakeHls);
loader.load();
this.requests.shift().respond(200, null,
'#EXTM3U\n' +
'#EXT-X-MEDIA-SEQUENCE:0\n' +
'#EXTINF:4,\n' +
'0.ts\n' +
'#EXTINF:5,\n' +
'1.ts\n' +
'#EXT-X-ENDLIST\n'
);
media = loader.media();
QUnit.equal(Playlist.getMediaIndexForTime_(media, 4), 1, 'rounds up exact matches');
QUnit.equal(Playlist.getMediaIndexForTime_(media, 3.7), 0, 'rounds down');
QUnit.equal(Playlist.getMediaIndexForTime_(media, 4.5), 1, 'rounds up at 0.5');
});
QUnit.test(
'accounts for non-zero starting segment time when calculating media index',
function() {
let media;
let loader = new PlaylistLoader('media.m3u8', this.fakeHls);
loader.load();
this.requests.shift().respond(200, null,
'#EXTM3U\n' +
'#EXT-X-MEDIA-SEQUENCE:1001\n' +
'#EXTINF:4,\n' +
'1001.ts\n' +
'#EXTINF:5,\n' +
'1002.ts\n'
);
loader.media().segments[0].end = 154;
media = loader.media();
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 45),
-1,
'expired content returns negative one'
);
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 75),
-1,
'expired content returns negative one'
);
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 0),
0,
'time of 0 with no expired time returns first segment'
);
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 50 + 100),
0,
'calculates the earliest available position'
);
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 50 + 100 + 2),
0,
'calculates within the first segment'
);
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 50 + 100 + 2),
0,
'calculates within the first segment'
);
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 50 + 100 + 4),
1,
'calculates within the second segment'
);
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 50 + 100 + 4.5),
1,
'calculates within the second segment'
);
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 50 + 100 + 6),
1,
'calculates within the second segment'
);
loader.media().segments[1].end = 159;
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 159),
2,
'returns number of segments when time is equal to end of last segment'
);
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 159.1),
2,
'returns number of segments when time is past end of last segment'
);
});
QUnit.test('prefers precise segment timing when tracking expired time', function() {
let media;
let loader = new PlaylistLoader('media.m3u8', this.fakeHls);
loader.load();
loader.trigger('firstplay');
this.requests.shift().respond(200, null,
'#EXTM3U\n' +
'#EXT-X-MEDIA-SEQUENCE:1001\n' +
'#EXTINF:4,\n' +
'1001.ts\n' +
'#EXTINF:5,\n' +
'1002.ts\n'
);
// setup the loader with an "imprecise" value as if it had been
// accumulating segment durations as they expire
loader.expired_ = 160;
// annotate the first segment with a start time
// this number would be coming from the Source Buffer in practice
loader.media().segments[0].end = 150;
media = loader.media();
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 149),
0,
'prefers the value on the first segment'
);
// trigger a playlist refresh
this.clock.tick(10 * 1000);
this.requests.shift().respond(200, null,
'#EXTM3U\n' +
'#EXT-X-MEDIA-SEQUENCE:1002\n' +
'#EXTINF:5,\n' +
'1002.ts\n'
);
media = loader.media();
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 150 + 4 + 1),
0,
'tracks precise expired times'
);
});
QUnit.test('accounts for expired time when calculating media index', function() {
let media;
let loader = new PlaylistLoader('media.m3u8', this.fakeHls);
let expired = 150;
loader.load();
this.requests.shift().respond(200, null,
'#EXTM3U\n' +
'#EXT-X-MEDIA-SEQUENCE:1001\n' +
'#EXTINF:4,\n' +
'1001.ts\n' +
'#EXTINF:5,\n' +
'1002.ts\n'
);
media = loader.media();
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 0, expired),
-1,
'expired content returns a negative index'
);
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 75, expired),
-1,
'expired content returns a negative index'
);
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 50 + 100, expired),
0,
'calculates the earliest available position'
);
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 50 + 100 + 2, expired),
0,
'calculates within the first segment'
);
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 50 + 100 + 2, expired),
0,
'calculates within the first segment'
);
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 50 + 100 + 4.5, expired),
1,
'calculates within the second segment'
);
QUnit.equal(
Playlist.getMediaIndexForTime_(media, 50 + 100 + 6, expired),
1,
'calculates within the second segment'
);
});
QUnit.test('returns index 0 when time is 0 and expired is falsy', function() {
QUnit.equal(
Playlist.getMediaIndexForTime_({segments: []}, 0, 0),
0,
'returns 0 when time is 0 and expired is 0'
);
QUnit.equal(
Playlist.getMediaIndexForTime_({segments: []}, 0),
0,
'returns 0 when time is 0 and expired is undefined'
);
});