-
Notifications
You must be signed in to change notification settings - Fork 437
Expand file tree
/
Copy pathdash-playlist-loader.test.js
More file actions
3023 lines (2582 loc) · 92.7 KB
/
Copy pathdash-playlist-loader.test.js
File metadata and controls
3023 lines (2582 loc) · 92.7 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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import QUnit from 'qunit';
import sinon from 'sinon';
import window from 'videojs-global-compat/window';
import {
default as DashPlaylistLoader,
updateMain,
compareSidxEntry,
filterChangedSidxMappings,
parseMainXml
} from '../src/dash-playlist-loader';
import parseSidx from 'mux.js/lib/tools/parse-sidx';
import xhrFactory from '../src/xhr';
import {generateSidxKey} from 'mpd-parser';
import {
useFakeEnvironment,
standardXHRResponse,
urlTo
} from './test-helpers';
// needed for plugin registration
import '../src/videojs-http-streaming';
import testDataManifests from 'create-test-data!manifests';
import { sidx as sidxResponse } from 'create-test-data!segments';
import {mp4VideoInit as mp4VideoInitSegment} from 'create-test-data!segments';
QUnit.module('DASH Playlist Loader: unit', {
beforeEach(assert) {
this.env = useFakeEnvironment(assert);
this.clock = this.env.clock;
this.requests = this.env.requests;
this.fakeVhs = {
xhr: xhrFactory()
};
this.standardXHRResponse = (request, data) => {
standardXHRResponse(request, data);
// Because SegmentLoader#fillBuffer_ is now scheduled asynchronously
// we have to use clock.tick to get the expected side effects of
// SegmentLoader#handleUpdateEnd_
this.clock.tick(1);
};
},
afterEach() {
this.env.restore();
}
});
QUnit.test('can getKeyIdSet from a playlist', function(assert) {
const loader = new DashPlaylistLoader('variant.mpd', this.fakeVhs);
const keyId = '188743e1-bd62-400e-92d9-748f8c753d1a';
// Test uppercase keyId from playlist.
const uppercaseKeyId = '800AACAA-5229-58AE-8880-62B5695DB6BF';
// We currently only pass keyId for widevine content protection.
const playlist = {
contentProtection: {
mp4protection: {
attributes: {
'cenc:default_KID': keyId
}
}
}
};
let keyIdSet = loader.getKeyIdSet(playlist);
assert.ok(keyIdSet.size);
assert.ok(keyIdSet.has(keyId.replace(/-/g, '')), 'keyId is expected hex string');
playlist.contentProtection.mp4protection.attributes['cenc:default_KID'] = uppercaseKeyId;
keyIdSet = loader.getKeyIdSet(playlist);
assert.ok(keyIdSet.has(uppercaseKeyId.replace(/-/g, '').toLowerCase()), 'keyId is expected lowercase hex string');
});
QUnit.test('updateMain: returns falsy when there are no changes', function(assert) {
const main = {
playlists: {
length: 1,
0: {
uri: '0',
id: '0',
segments: []
}
},
mediaGroups: {
AUDIO: {
audio: {
'audio-main': {
attributes: {
NAME: 'audio'
},
playlists: {
length: 1,
0: {
playlists: {}
}
}
}
}
},
SUBTITLES: {}
},
duration: 0,
minimumUpdatePeriod: 0,
timelineStarts: []
};
assert.deepEqual(updateMain(main, main), null);
});
QUnit.test('updateMain: updates playlists', function(assert) {
const main = {
playlists: [{
uri: '0',
id: '0'
},
{
uri: '1',
id: '1',
segments: []
}],
mediaGroups: {
AUDIO: {},
SUBTITLES: {}
},
duration: 0,
minimumUpdatePeriod: 0,
timelineStarts: []
};
// Only the first playlist is changed
const update = {
playlists: [{
id: '0',
uri: '0',
segments: []
},
{
uri: '1',
id: '1',
segments: []
}],
mediaGroups: {
AUDIO: {},
SUBTITLES: {}
},
duration: 0,
minimumUpdatePeriod: 0,
timelineStarts: []
};
assert.deepEqual(
updateMain(main, update),
{
playlists: [{
id: '0',
uri: '0',
segments: []
},
{
uri: '1',
id: '1',
segments: []
}],
mediaGroups: {
AUDIO: {},
SUBTITLES: {}
},
duration: 0,
minimumUpdatePeriod: 0,
timelineStarts: []
}
);
});
QUnit.test('updateMain: updates mediaGroups', function(assert) {
const main = {
playlists: {
length: 1,
0: {
id: '0',
uri: '0',
segments: []
}
},
mediaGroups: {
AUDIO: {
audio: {
'audio-main': {
playlists: [{
id: '0',
uri: '0',
test: 'old text',
segments: []
}]
}
}
},
SUBTITLES: {}
},
duration: 0,
minimumUpdatePeriod: 0
};
const update = {
playlists: {
length: 1,
0: {
uri: '0',
segments: []
}
},
mediaGroups: {
AUDIO: {
audio: {
'audio-main': {
playlists: [{
id: '0',
uri: '0',
resolvedUri: '0',
test: 'new text',
segments: [{
uri: 's'
}]
}]
}
}
},
SUBTITLES: {}
},
duration: 0,
minimumUpdatePeriod: 0
};
assert.ok(
updateMain(main, update),
'the mediaGroups were updated'
);
});
QUnit.test('updateMain: updates playlists and mediaGroups', function(assert) {
const main = {
duration: 10,
minimumUpdatePeriod: 0,
timelineStarts: [],
mediaGroups: {
AUDIO: {
audio: {
main: {
playlists: [{
mediaSequence: 0,
attributes: {},
id: 'audio-0-uri',
uri: 'audio-0-uri',
resolvedUri: urlTo('audio-0-uri'),
segments: [{
duration: 10,
uri: 'audio-segment-0-uri',
resolvedUri: urlTo('audio-segment-0-uri')
}]
}]
}
}
}
},
playlists: [{
mediaSequence: 0,
attributes: {
BANDWIDTH: 9
},
id: 'playlist-0-uri',
uri: 'playlist-0-uri',
resolvedUri: urlTo('playlist-0-uri'),
segments: [{
duration: 10,
uri: 'segment-0-uri',
resolvedUri: urlTo('segment-0-uri')
}]
}]
};
const update = {
duration: 20,
minimumUpdatePeriod: 0,
timelineStarts: [],
mediaGroups: {
AUDIO: {
audio: {
main: {
playlists: [{
mediaSequence: 1,
attributes: {},
id: 'audio-0-uri',
uri: 'audio-0-uri',
resolvedUri: urlTo('audio-0-uri'),
segments: [{
duration: 10,
uri: 'audio-segment-0-uri',
resolvedUri: urlTo('audio-segment-0-uri')
}]
}]
}
}
}
},
playlists: [{
mediaSequence: 1,
attributes: {
BANDWIDTH: 9
},
id: 'playlist-0-uri',
uri: 'playlist-0-uri',
resolvedUri: urlTo('playlist-0-uri'),
segments: [{
duration: 10,
uri: 'segment-0-uri',
resolvedUri: urlTo('segment-0-uri')
}]
}]
};
main.playlists['playlist-0-uri'] = main.playlists[0];
main.playlists['audio-0-uri'] = main.mediaGroups.AUDIO.audio.main.playlists[0];
assert.deepEqual(
updateMain(main, update),
{
duration: 20,
minimumUpdatePeriod: 0,
timelineStarts: [],
mediaGroups: {
AUDIO: {
audio: {
main: {
playlists: [{
mediaSequence: 1,
attributes: {},
id: 'audio-0-uri',
uri: 'audio-0-uri',
resolvedUri: urlTo('audio-0-uri'),
segments: [{
duration: 10,
uri: 'audio-segment-0-uri',
resolvedUri: urlTo('audio-segment-0-uri')
}]
}]
}
}
}
},
playlists: [{
mediaSequence: 1,
attributes: {
BANDWIDTH: 9
},
id: 'playlist-0-uri',
uri: 'playlist-0-uri',
resolvedUri: urlTo('playlist-0-uri'),
segments: [{
duration: 10,
uri: 'segment-0-uri',
resolvedUri: urlTo('segment-0-uri')
}]
}]
},
'updates playlists and media groups'
);
});
QUnit.test('updateMain: updates minimumUpdatePeriod', function(assert) {
const main = {
playlists: {
length: 1,
0: {
uri: '0',
id: '0',
segments: []
}
},
mediaGroups: {
AUDIO: {},
SUBTITLES: {}
},
duration: 0,
minimumUpdatePeriod: 0,
timelineStarts: []
};
const update = {
playlists: {
length: 1,
0: {
uri: '0',
id: '0',
segments: []
}
},
mediaGroups: {
AUDIO: {},
SUBTITLES: {}
},
duration: 0,
minimumUpdatePeriod: 2,
timelineStarts: []
};
assert.deepEqual(
updateMain(main, update),
{
playlists: {
length: 1,
0: {
uri: '0',
id: '0',
segments: []
}
},
mediaGroups: {
AUDIO: {},
SUBTITLES: {}
},
duration: 0,
minimumUpdatePeriod: 2,
timelineStarts: []
}
);
});
QUnit.test('updateMain: requires sidxMapping.sidx to add sidx segments', function(assert) {
const prev = {
playlists: [{
uri: '0',
id: 0,
segments: [],
sidx: {
resolvedUri: 'https://example.com/foo.mp4',
uri: 'foo.mp4',
duration: 10,
byterange: {
offset: 2,
length: 4
}
}
}],
mediaGroups: {
AUDIO: {},
SUBTITLES: {}
}
};
const next = {
playlists: [{
uri: '0',
id: 0,
segments: [],
sidx: {
resolvedUri: 'https://example.com/foo.mp4',
uri: 'foo.mp4',
duration: 10,
byterange: {
offset: 2,
length: 4
}
}
}],
mediaGroups: {
AUDIO: {},
SUBTITLES: {}
}
};
const sidxMapping = {};
const key = generateSidxKey(prev.playlists[0].sidx);
sidxMapping[key] = {sidxInfo: {uri: 'foo', key}};
assert.deepEqual(
updateMain(prev, next, sidxMapping),
null,
'no update'
);
sidxMapping[key].sidx = parseSidx(sidxResponse().subarray(8));
const result = updateMain(prev, next, sidxMapping);
assert.ok(result, 'result returned');
assert.equal(result.playlists[0].segments.length, 1, 'added one segment from sidx');
});
QUnit.test('compareSidxEntry: will not add new sidx info to a mapping', function(assert) {
const playlists = {
0: {
sidx: {
byterange: {
offset: 0,
length: 10
},
uri: '0'
}
},
1: {
sidx: {
byterange: {
offset: 10,
length: 29
},
uri: '1'
}
}
};
const oldSidxMapping = {
'0-0-9': {
sidx: new Uint8Array(),
sidxInfo: playlists[0].sidx
}
};
const result = compareSidxEntry(playlists, oldSidxMapping);
assert.notOk(result['1-10-29'], 'new playlists are not returned');
assert.ok(result['0-0-9'], 'matching playlists are returned');
assert.strictEqual(Object.keys(result).length, 1, 'only one sidx');
});
QUnit.test('compareSidxEntry: will remove non-matching sidxes from a mapping', function(assert) {
const playlists = [
{
uri: '0',
id: '0',
sidx: {
byterange: {
offset: 0,
length: 10
}
}
}
];
const oldSidxMapping = {
'0-0-9': {
sidx: new Uint8Array(),
sidxInfo: {
byterange: {
offset: 1,
length: 3
}
}
}
};
const result = compareSidxEntry(playlists, oldSidxMapping);
assert.strictEqual(Object.keys(result).length, 0, 'no sidxes in mapping');
});
QUnit.test('filterChangedSidxMappings: removes change sidx info from mapping', function(assert) {
const loader = new DashPlaylistLoader('dash-sidx.mpd', this.fakeVhs);
loader.load();
// main
this.standardXHRResponse(this.requests.shift());
// container request
this.standardXHRResponse(this.requests.shift(), mp4VideoInitSegment().subarray(0, 10));
// sidx byterange request
this.standardXHRResponse(this.requests.shift(), sidxResponse());
const childPlaylist = loader.main.mediaGroups.AUDIO.audio.en.playlists[0];
const childLoader = new DashPlaylistLoader(childPlaylist, this.fakeVhs, false, loader);
childLoader.load();
this.clock.tick(1);
// audio playlist container request
this.standardXHRResponse(this.requests.shift(), mp4VideoInitSegment().subarray(0, 10));
// audio sidx byterange request
this.standardXHRResponse(this.requests.shift(), sidxResponse());
const oldSidxMapping = loader.sidxMapping_;
let newSidxMapping = filterChangedSidxMappings(
loader.main,
loader.sidxMapping_
);
assert.deepEqual(
newSidxMapping,
oldSidxMapping,
'if no sidx info changed, return the same object'
);
const playlists = loader.main.playlists;
const oldVideoKey = generateSidxKey(playlists['0-placeholder-uri-0'].sidx);
const oldAudioEnKey = generateSidxKey(playlists['0-placeholder-uri-AUDIO-audio-audio'].sidx);
let mainXml = loader.mainXml_.replace(/(indexRange)=\"\d+-\d+\"/, '$1="201-400"');
// should change the video playlist
let newMain = parseMainXml({
mainXml,
srcUrl: loader.srcUrl,
clientOffset: loader.clientOffset_
});
newSidxMapping = filterChangedSidxMappings(
newMain,
loader.sidxMapping_
);
const newVideoKey = `${playlists['0-placeholder-uri-0'].sidx.uri}-201-400`;
assert.notOk(
newSidxMapping[oldVideoKey],
'old video playlist mapping is not returned'
);
assert.notOk(
newSidxMapping[newVideoKey],
'new video playlists are not returned'
);
assert.ok(
newSidxMapping[oldAudioEnKey],
'audio group mapping is returned as it is unchanged'
);
// should change the English audio group
mainXml = loader.mainXml_.replace(/(indexRange)=\"\d+-\d+\"/g, '$1="201-400"');
newMain = parseMainXml({
mainXml,
srcUrl: loader.srcUrl,
clientOffset: loader.clientOffset_
});
newSidxMapping = filterChangedSidxMappings(
newMain,
loader.sidxMapping_
);
assert.notOk(
newSidxMapping[oldAudioEnKey],
'audio group English is removed'
);
});
QUnit.test('addSidxSegments_: creates an XHR request for a sidx range', function(assert) {
const loader = new DashPlaylistLoader('dash.mpd', this.fakeVhs);
const sidxInfo = {
resolvedUri: 'sidx.mp4',
byterange: {
offset: 10,
length: 10
}
};
const playlist = {
uri: 'fakeplaylist',
id: 'fakeplaylist',
segments: [sidxInfo],
sidx: sidxInfo
};
const callback = sinon.stub();
loader.addSidxSegments_(playlist, loader.state, callback);
assert.strictEqual(this.requests[0].uri, sidxInfo.resolvedUri, 'uri requested is correct');
this.standardXHRResponse(this.requests.shift(), mp4VideoInitSegment().subarray(0, 10));
assert.strictEqual(this.requests[0].uri, sidxInfo.resolvedUri, 'uri requested is correct');
this.standardXHRResponse(this.requests.shift());
assert.strictEqual(callback.callCount, 1, 'callback was called');
});
QUnit.test('addSidxSegments_: does not re-request bytes from container request', function(assert) {
const loader = new DashPlaylistLoader('dash.mpd', this.fakeVhs);
const sidxInfo = {
resolvedUri: 'sidx.mp4',
byterange: {
offset: 0,
length: 600
}
};
const playlist = {
uri: 'fakeplaylist',
id: 'fakeplaylist',
segments: [sidxInfo],
sidx: sidxInfo
};
const callback = sinon.stub();
loader.addSidxSegments_(playlist, loader.state, callback);
assert.strictEqual(this.requests[0].uri, sidxInfo.resolvedUri, 'uri requested is correct');
assert.strictEqual(this.requests.length, 1, 'one xhr request');
const data = new Uint8Array(600);
data.set(mp4VideoInitSegment().subarray(0, 10));
this.standardXHRResponse(this.requests.shift(), data);
assert.equal(this.requests.length, 0, 'no more requests');
assert.strictEqual(callback.callCount, 1, 'callback was called');
});
QUnit.test('addSidxSegments_: adds/triggers error on invalid container', function(assert) {
const loader = new DashPlaylistLoader('dash.mpd', this.fakeVhs);
const sidxInfo = {
resolvedUri: 'sidx.mp4',
byterange: {
offset: 0,
length: 10
}
};
const playlist = {
uri: 'fakeplaylist',
id: 'fakeplaylist',
segments: [sidxInfo],
sidx: sidxInfo
};
let triggeredError = false;
const callback = sinon.stub();
loader.on('error', () => {
triggeredError = true;
});
loader.addSidxSegments_(playlist, loader.state, callback);
assert.strictEqual(this.requests[0].uri, sidxInfo.resolvedUri, 'uri requested is correct');
assert.strictEqual(this.requests.length, 1, 'one xhr request');
this.standardXHRResponse(this.requests.shift());
assert.equal(this.requests.length, 0, 'no more requests');
assert.ok(triggeredError, 'triggered an error');
assert.deepEqual(loader.error, {
playlistExclusionDuration: Infinity,
code: 2,
internal: true,
message: 'Unsupported unknown container type for sidx segment at URL: sidx.mp4',
playlist,
response: '',
status: 200
}, 'error as expected');
});
QUnit.test('constructor throws if the playlist url is empty or undefined', function(assert) {
assert.throws(function() {
DashPlaylistLoader();
}, 'requires an argument');
assert.throws(function() {
DashPlaylistLoader('');
}, 'does not accept the empty string');
});
QUnit.test('constructor sets srcUrl and other properties', function(assert) {
const loader = new DashPlaylistLoader('dash.mpd', this.fakeVhs);
assert.strictEqual(loader.state, 'HAVE_NOTHING', 'correct state');
assert.deepEqual(loader.loadedPlaylists_, {}, 'correct loadedPlaylist state');
assert.equal(loader.mainPlaylistLoader_, loader, 'mainPlaylistLoader should be self');
assert.ok(loader.isMain_, 'should be set as main');
assert.notOk(loader.childPlaylist_, 'should be no childPlaylist_');
assert.strictEqual(loader.srcUrl, 'dash.mpd', 'set the srcUrl');
const childLoader = new DashPlaylistLoader({}, this.fakeVhs, false, loader);
assert.strictEqual(childLoader.state, 'HAVE_NOTHING', 'correct state');
assert.deepEqual(childLoader.loadedPlaylists_, {}, 'correct loadedPlaylist state');
assert.ok(childLoader.mainPlaylistLoader_, 'should be a mainPlaylistLoader');
assert.notEqual(childLoader.mainPlaylistLoader_, childLoader, 'should not be a mainPlaylistLoader');
assert.notOk(childLoader.isMain_, 'should not be main');
assert.deepEqual(
childLoader.childPlaylist_, {},
'should be a childPlaylist_'
);
assert.notOk(childLoader.srcUrl, 'should be no srcUrl');
});
QUnit.test('dispose: aborts pending manifest request', function(assert) {
const loader = new DashPlaylistLoader('dash.mpd', this.fakeVhs);
loader.load();
this.clock.tick(1);
assert.equal(this.requests.length, 1, 'one request');
assert.notOk(this.requests[0].aborted, 'request not aborted');
assert.ok(this.requests[0].onreadystatechange, 'onreadystatechange handler exists');
loader.dispose();
assert.equal(this.requests.length, 1, 'one request');
assert.ok(this.requests[0].aborted, 'request aborted');
assert.notOk(
this.requests[0].onreadystatechange,
'onreadystatechange handler does not exist'
);
});
QUnit.test('load: will start an unstarted loader', function(assert) {
const loader = new DashPlaylistLoader('dash.mpd', this.fakeVhs);
const origHasPendingRequest = loader.hasPendingRequest;
let loadedPlaylists = 0;
let loadedMetadata = 0;
loader.on('loadedplaylist', () => {
loadedPlaylists++;
});
loader.on('loadedmetadata', () => {
loadedMetadata++;
});
assert.notOk(loader.started, 'begins unstarted');
loader.load();
assert.strictEqual(loader.started, true, 'load should start the loader');
assert.strictEqual(this.requests.length, 1, 'should request the manifest');
assert.strictEqual(loader.state, 'HAVE_NOTHING', 'state has not changed');
// pretend there's a pending media request so
// media isn't selected automatically
loader.hasPendingRequest = () => true;
this.standardXHRResponse(this.requests.shift());
assert.strictEqual(loader.state, 'HAVE_MAIN_MANIFEST', 'state is updated');
assert.strictEqual(loadedPlaylists, 1, 'one loadedplaylist');
assert.strictEqual(loadedMetadata, 0, 'no loadedmetadata');
loader.hasPendingRequest = origHasPendingRequest;
loader.load();
assert.strictEqual(loader.started, true, 'still loaded');
assert.strictEqual(loadedPlaylists, 2, 'two loadedplaylists');
assert.strictEqual(loadedMetadata, 0, 'no loadedmetadata');
assert.strictEqual(this.requests.length, 0, 'no request made');
assert.strictEqual(loader.state, 'HAVE_MAIN_MANIFEST', 'no state change');
loader.media(loader.main.playlists[0]);
this.clock.tick(1);
assert.strictEqual(loadedPlaylists, 3, '3 loadedplaylists');
assert.strictEqual(loadedMetadata, 1, 'one loadedmetadata');
loader.load(true);
assert.strictEqual(loadedPlaylists, 3, 'does not fire 4th loadedplaylist');
assert.strictEqual(loadedMetadata, 1, 'does not fire 2nd loadedmetadata');
const loadSpy = sinon.spy(loader, 'load');
// half of one target duration = 1s
this.clock.tick(1000);
assert.strictEqual(loadSpy.callCount, 1, 'load was called again');
});
QUnit.test('load: will not request manifest when started', function(assert) {
const loader = new DashPlaylistLoader('dash.mpd', this.fakeVhs);
const origHasPendingRequest = loader.hasPendingRequest;
let loadedPlaylists = 0;
let loadedMetadata = 0;
loader.on('loadedplaylist', () => {
loadedPlaylists++;
});
loader.on('loadedmetadata', () => {
loadedMetadata++;
});
loader.load();
// pretend there's a pending media request so
// media isn't selected automatically
loader.hasPendingRequest = () => true;
this.standardXHRResponse(this.requests.shift());
assert.strictEqual(loader.state, 'HAVE_MAIN_MANIFEST', 'state is updated');
assert.strictEqual(loadedPlaylists, 1, 'one loadedplaylist');
assert.strictEqual(loadedMetadata, 0, 'no loadedmetadata');
loader.hasPendingRequest = origHasPendingRequest;
loader.load();
assert.strictEqual(loader.started, true, 'still loaded');
assert.strictEqual(loadedPlaylists, 2, 'two loadedplaylists');
assert.strictEqual(loadedMetadata, 0, 'no loadedmetadata');
assert.strictEqual(this.requests.length, 0, 'no request made');
assert.strictEqual(loader.state, 'HAVE_MAIN_MANIFEST', 'no state change');
});
QUnit.test('load: will retry if this is the final rendition', function(assert) {
const loader = new DashPlaylistLoader('dash.mpd', this.fakeVhs);
let loadedPlaylists = 0;
let loadedMetadata = 0;
loader.on('loadedplaylist', () => {
loadedPlaylists++;
});
loader.on('loadedmetadata', () => {
loadedMetadata++;
});
assert.notOk(loader.started, 'begins unstarted');
loader.load();
this.standardXHRResponse(this.requests.shift());
loader.media(loader.main.playlists[0]);
this.clock.tick(1);
assert.strictEqual(loadedPlaylists, 2, 'two loadedplaylists');
assert.strictEqual(loadedMetadata, 1, 'one loadedmetadata');
loader.load(true);
assert.strictEqual(loadedPlaylists, 2, 'does not fire 3rd loadedplaylist');
assert.strictEqual(loadedMetadata, 1, 'does not fire 2nd loadedmetadata');
const loadSpy = sinon.spy(loader, 'load');
// half of one target duration = 1s
this.clock.tick(1000);
assert.strictEqual(loadSpy.callCount, 1, 'load was called again');
});
QUnit.test('media: get returns currently active media playlist', function(assert) {
const loader = new DashPlaylistLoader('dash.mpd', this.fakeVhs);
const origHasPendingRequest = loader.hasPendingRequest;
// setup loader
loader.load();
// pretend there's a pending media request so
// media isn't selected automatically
loader.hasPendingRequest = () => true;
this.standardXHRResponse(this.requests.shift());
assert.strictEqual(loader.state, 'HAVE_MAIN_MANIFEST', 'state should be HAVE_MAIN_MANIFEST');
assert.strictEqual(loader.media(), undefined, 'no media set yet');
loader.hasPendingRequest = origHasPendingRequest;
loader.media(loader.main.playlists[0]);
this.clock.tick(1);
assert.strictEqual(
loader.media().uri,
loader.main.playlists[0].uri,
'set the correct media playlist'
);
});
QUnit.test('media: does not set media if getter is called', function(assert) {
const loader = new DashPlaylistLoader('dash.mpd', this.fakeVhs);
const origHasPendingRequest = loader.hasPendingRequest;
let loadedPlaylists = 0;
let loadedMetadata = 0;
loader.on(['loadedplaylist', 'loadedmetadata'], (e) => {
if (e.type === 'loadedplaylist') {
loadedPlaylists++;
} else if (e.type === 'loadedmetadata') {
loadedMetadata++;
}
});
loader.load();
// pretend there's a pending media request so
// media isn't selected automatically
loader.hasPendingRequest = () => true;
this.standardXHRResponse(this.requests.shift());
assert.strictEqual(loader.state, 'HAVE_MAIN_MANIFEST', 'state should be HAVE_MAIN_MANIFEST');
assert.strictEqual(loadedPlaylists, 1, 'one loadedplaylist');
assert.strictEqual(loadedMetadata, 0, 'no loadedmetadata');
loader.hasPendingRequest = origHasPendingRequest;
loader.media(null);
assert.strictEqual(loader.state, 'HAVE_MAIN_MANIFEST', 'state should stay HAVE_MAIN_MANIFEST');
assert.strictEqual(loadedPlaylists, 1, 'still one loadedplaylist');
assert.strictEqual(loadedMetadata, 0, 'still no loadedmetadata');
loader.media(undefined);
assert.strictEqual(loader.state, 'HAVE_MAIN_MANIFEST', 'state should stay HAVE_MAIN_MANIFEST');
assert.strictEqual(loadedPlaylists, 1, 'still one loadedplaylist');
assert.strictEqual(loadedMetadata, 0, 'still no loadedmetadata');
});
QUnit.test('media: errors if called in incorrect state', function(assert) {
const loader = new DashPlaylistLoader('dash.mpd', this.fakeVhs);
assert.strictEqual(loader.state, 'HAVE_NOTHING', 'state should be HAVE_NOTHING');
assert.throws(
() => loader.media('0'),
/Cannot switch media playlist from HAVE_NOTHING/,
'should throw an error if media is called without a main playlist'
);
});
QUnit.test('media: setting media causes an asynchronous action', function(assert) {
const loader = new DashPlaylistLoader('dash.mpd', this.fakeVhs);
const origHasPendingRequest = loader.hasPendingRequest;
let loadedPlaylists = 0;
let loadedMetadata = 0;
loader.on(['loadedplaylist', 'loadedmetadata'], (e) => {
if (e.type === 'loadedplaylist') {
loadedPlaylists++;
} else if (e.type === 'loadedmetadata') {
loadedMetadata++;
}
});
// setup loader
loader.load();
// pretend there's a pending media request so
// media isn't selected automatically
loader.hasPendingRequest = () => true;
this.standardXHRResponse(this.requests.shift());
loader.hasPendingRequest = origHasPendingRequest;
assert.strictEqual(loader.state, 'HAVE_MAIN_MANIFEST', 'correct state before media call');
assert.strictEqual(loadedPlaylists, 1, 'one loadedplaylist before media is loaded');
assert.strictEqual(loadedMetadata, 0, 'no loadedmetadata before media is loaded');
assert.notOk(loader.hasPendingRequest(), 'no pending asynchronous actions');
// set initial media
loader.media(loader.main.playlists[0]);
assert.ok(loader.hasPendingRequest(), 'has asynchronous action pending');
assert.strictEqual(loader.state, 'HAVE_MAIN_MANIFEST', 'state is still HAVE_MAIN_MANIFEST');
assert.strictEqual(loadedPlaylists, 1, 'still one loadedplaylist');
assert.strictEqual(loadedMetadata, 0, 'still no loadedmetadata');
// runs any pending async actions
this.clock.tick(0);