-
Notifications
You must be signed in to change notification settings - Fork 429
/
Copy pathvideojs-http-streaming.test.js
6270 lines (5200 loc) · 176 KB
/
videojs-http-streaming.test.js
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 document from 'global/document';
import videojs from 'video.js';
import Events from 'video.js';
import QUnit from 'qunit';
import testDataManifests from 'create-test-data!manifests';
import {
muxed as muxedSegment,
encryptionKey,
encrypted as encryptedSegment,
audio as audioSegment,
video as videoSegment,
mp4VideoInit as mp4VideoInitSegment,
mp4Video as mp4VideoSegment,
mp4AudioInit as mp4AudioInitSegment,
mp4Audio as mp4AudioSegment
} from 'create-test-data!segments';
import {
useFakeEnvironment,
useFakeMediaSource,
createPlayer,
openMediaSource,
standardXHRResponse,
absoluteUrl,
requestAndAppendSegment,
disposePlaybackWatcher
} from './test-helpers.js';
import {
createPlaylistID,
parseManifest
} from '../src/manifest.js';
/* eslint-disable no-unused-vars */
// we need this so that it can register vhs with videojs
import {
VhsSourceHandler,
VhsHandler,
Vhs,
emeKeySystems,
LOCAL_STORAGE_KEY,
expandDataUri,
setupEmeOptions,
getAllPsshKeySystemsOptions,
waitForKeySessionCreation
} from '../src/videojs-http-streaming';
import window from 'global/window';
// we need this so the plugin registers itself
import 'videojs-contrib-quality-levels';
import 'videojs-contrib-eme';
import {merge, createTimeRanges} from '../src/util/vjs-compat';
import {version as vhsVersion} from '../package.json';
import {version as muxVersion} from 'mux.js/package.json';
import {version as mpdVersion} from 'mpd-parser/package.json';
import {version as m3u8Version} from 'm3u8-parser/package.json';
import {version as aesVersion} from 'aes-decrypter/package.json';
let testOrSkip = 'test';
// some tests just don't work reliably on ie11 or edge
if (videojs.browser.IS_EDGE || videojs.browser.IE_VERSION) {
testOrSkip = 'skip';
}
const ogVhsHandlerSetupQualityLevels = videojs.VhsHandler.prototype.setupQualityLevels_;
// do a shallow copy of the properties of source onto the target object
const mergeShallow = function(target, source) {
let name;
for (name in source) {
target[name] = source[name];
}
};
QUnit.module('VHS', {
beforeEach(assert) {
this.env = useFakeEnvironment(assert);
this.requests = this.env.requests;
this.mse = useFakeMediaSource();
this.clock = this.env.clock;
this.old = {};
if (!videojs.browser.IE_VERSION) {
this.old.devicePixelRatio = window.devicePixelRatio;
window.devicePixelRatio = 1;
}
// store functionality that some tests need to mock
this.old.GlobalOptions = merge(videojs.options);
// force the HLS tech to run
this.old.NativeHlsSupport = videojs.Vhs.supportsNativeHls;
videojs.Vhs.supportsNativeHls = false;
this.old.NativeDashSupport = videojs.Vhs.supportsNativeDash;
videojs.Vhs.supportsNativeDash = false;
this.old.Decrypt = videojs.Vhs.Decrypter;
videojs.Vhs.Decrypter = function() {};
// save and restore browser detection for the Firefox-specific tests
this.old.browser = videojs.browser;
videojs.browser = merge({}, videojs.browser);
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#handleAppendsDone_
this.clock.tick(1);
};
// setup a player
this.player = createPlayer();
this.clock.tick(1);
},
afterEach() {
this.env.restore();
this.mse.restore();
if (this.old.hasOwnProperty('devicePixelRatio')) {
window.devicePixelRatio = this.old.devicePixelRatio;
}
// This seems duplicative of `merge` but tests fail if `merge` is used...
mergeShallow(videojs.options, this.old.GlobalOptions);
videojs.Vhs.supportsNativeHls = this.old.NativeHlsSupport;
videojs.Vhs.supportsNativeDash = this.old.NativeDashSupport;
videojs.Vhs.Decrypter = this.old.Decrypt;
videojs.browser = this.old.browser;
window.localStorage.clear();
this.player.dispose();
}
});
QUnit.test('mse urls are created and revoked', function(assert) {
const old = {
createObjectURL: window.URL.createObjectURL,
revokeObjectURL: window.URL.revokeObjectURL
};
const ids = [];
window.URL.createObjectURL = (...args) => {
const id = old.createObjectURL.apply(window.URL, args);
ids.push(id);
return id;
};
window.URL.revokeObjectURL = (...args) => {
const index = ids.indexOf(args[0]);
if (index !== -1) {
ids.splice(index, 1);
}
return old.revokeObjectURL.apply(window.URL, args);
};
this.player.src({
src: 'manifest/playlist.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
assert.ok(ids.length > 0, 'object urls created');
this.player.dispose();
assert.equal(ids.length, 0, 'all object urls removed');
window.URL.createObjectURL = old.createObjectURL;
window.URL.revokeObjectURL = old.revokeObjectURL;
});
QUnit.test('version is exported', function(assert) {
this.player.src({
src: 'manifest/playlist.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
assert.ok(this.player.tech(true).vhs.version, 'version function');
assert.ok(videojs.VhsHandler.version, 'version function');
assert.deepEqual(this.player.tech(true).vhs.version(), {
'@videojs/http-streaming': vhsVersion,
'mux.js': muxVersion,
'mpd-parser': mpdVersion,
'm3u8-parser': m3u8Version,
'aes-decrypter': aesVersion
}, 'version is correct');
});
QUnit.test('canChangeType is exported', function(assert) {
this.player.src({
src: 'manifest/playlist.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
assert.ok(this.player.tech(true).vhs.canChangeType, 'canChangeType function');
const canChangeType = window.SourceBuffer &&
window.SourceBuffer.prototype &&
typeof window.SourceBuffer.prototype.changeType === 'function';
const assertion = canChangeType ? 'ok' : 'notOk';
assert[assertion](this.player.tech(true).vhs.canChangeType(), 'canChangeType is correct');
});
QUnit.test('tech error may pause loading', function(assert) {
this.player.src({
src: 'manifest/playlist.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
const vhs = this.player.tech_.vhs;
const pc = vhs.playlistController_;
let pauseCalled = false;
pc.pauseLoading = () => {
pauseCalled = true;
};
this.player.tech_.error = () => null;
this.player.tech_.trigger('error');
assert.notOk(pauseCalled, 'no video el error attribute, no pause loading');
this.player.tech_.error = () => 'foo';
this.player.tech_.trigger('error');
assert.ok(pauseCalled, 'video el error and trigger pauses loading');
assert.equal(this.env.log.error.calls, 1, '1 media error logged');
this.env.log.error.reset();
});
QUnit.test('VhsHandler is referenced by player.tech().vhs', function(assert) {
this.player.src({
src: 'manifest/playlist.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
assert.ok(
this.player.tech().vhs instanceof VhsHandler,
'player.tech().vhs references an instance of VhsHandler'
);
assert.equal(this.env.log.warn.calls, 1, 'warning logged');
assert.equal(
this.env.log.warn.args[0][0],
'Using the tech directly can be dangerous. I hope you know what you\'re doing.\n' +
'See https://github.com/videojs/video.js/issues/2617 for more info.\n',
'logged warning'
);
});
QUnit.test('starts playing if autoplay is specified', function(assert) {
this.player.autoplay(true);
this.player.src({
src: 'manifest/playlist.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
// make sure play() is called *after* the media source opens
openMediaSource(this.player, this.clock);
this.standardXHRResponse(this.requests[0]);
assert.ok(!this.player.paused(), 'not paused');
});
QUnit.test('stats are reset on each new source', function(assert) {
const done = assert.async();
this.player.src({
src: 'manifest/playlist.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
// make sure play() is called *after* the media source opens
openMediaSource(this.player, this.clock);
const segment = muxedSegment();
// copy the byte length since the segment bytes get cleared out
const segmentByteLength = segment.byteLength;
assert.ok(segmentByteLength, 'the segment has some number of bytes');
// media
this.standardXHRResponse(this.requests.shift());
this.player.tech(true).vhs.playlistController_.mainSegmentLoader_.one('appending', () => {
assert.equal(
this.player.tech_.vhs.stats.mediaBytesTransferred,
segmentByteLength,
'stat is set'
);
this.player.src({
src: 'manifest/main.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
assert.equal(this.player.tech_.vhs.stats.mediaBytesTransferred, 0, 'stat is reset');
done();
});
// segment 0
this.standardXHRResponse(this.requests.shift(), segment);
});
QUnit.test('XHR requests first byte range on play', function(assert) {
this.player.src({
src: 'manifest/playlist.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
this.player.tech_.triggerReady();
this.clock.tick(1);
this.player.tech_.trigger('play');
openMediaSource(this.player, this.clock);
this.standardXHRResponse(this.requests[0]);
assert.equal(this.requests[1].headers.Range, 'bytes=0-522827');
});
QUnit.test('Seeking requests correct byte range', function(assert) {
this.player.src({
src: 'manifest/playlist.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
this.player.tech_.trigger('play');
openMediaSource(this.player, this.clock);
this.standardXHRResponse(this.requests[0]);
this.clock.tick(1);
this.player.currentTime(41);
this.clock.tick(2);
assert.equal(this.requests[2].headers.Range, 'bytes=2299992-2835603');
});
QUnit.test('autoplay seeks to the live point after playlist load', function(assert) {
let currentTime = 0;
this.player.autoplay(true);
this.player.tech_.currentTime = (ct) => {
currentTime = ct;
};
this.player.src({
src: 'liveStart30sBefore.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
openMediaSource(this.player, this.clock);
this.player.tech_.readyState = () => 4;
this.player.tech_.trigger('play');
this.standardXHRResponse(this.requests.shift());
this.clock.tick(1);
assert.notEqual(currentTime, 0, 'seeked on autoplay');
});
QUnit.test('autoplay seeks to the live point after media source open', function(assert) {
let currentTime = 0;
this.player.autoplay(true);
this.player.tech_.setCurrentTime = (ct) => {
currentTime = ct;
};
this.player.src({
src: 'liveStart30sBefore.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.player.tech_.readyState = () => 4;
this.clock.tick(1);
this.player.tech_.triggerReady();
this.clock.tick(1);
this.standardXHRResponse(this.requests.shift());
openMediaSource(this.player, this.clock);
this.player.tech_.trigger('play');
this.clock.tick(1);
assert.notEqual(currentTime, 0, 'seeked on autoplay');
});
QUnit.test(
'autoplay seeks to the live point after tech fires loadedmetadata in ie11',
function(assert) {
videojs.browser.IE_VERSION = 11;
let currentTime = 0;
this.player.autoplay(true);
this.player.on('seeking', () => {
currentTime = this.player.currentTime();
});
this.player.src({
src: 'liveStart30sBefore.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
openMediaSource(this.player, this.clock);
this.player.tech_.trigger('play');
this.standardXHRResponse(this.requests.shift());
this.clock.tick(1);
assert.equal(currentTime, 0, 'have not played yet');
this.player.tech_.trigger('loadedmetadata');
this.clock.tick(1);
assert.notEqual(currentTime, 0, 'seeked after tech is ready');
}
);
QUnit.test(
'duration is set when the source opens after the playlist is loaded',
function(assert) {
this.player.src({
src: 'media.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
this.player.tech_.triggerReady();
this.clock.tick(1);
this.standardXHRResponse(this.requests.shift());
openMediaSource(this.player, this.clock);
assert.equal(
this.player.tech_.vhs.mediaSource.duration,
40,
'set the duration'
);
}
);
QUnit.test('codecs are passed to the source buffer', function(assert) {
const done = assert.async();
const codecs = [];
this.player.src({
src: 'custom-codecs.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
openMediaSource(this.player, this.clock);
const addSourceBuffer = this.player.tech_.vhs.mediaSource.addSourceBuffer;
this.player.tech_.vhs.mediaSource.addSourceBuffer = function(codec) {
codecs.push(codec);
return addSourceBuffer.call(this, codec);
};
// main
this.requests.shift().respond(
200, null,
'#EXTM3U\n' +
'#EXT-X-STREAM-INF:BANDWIDTH=10,CODECS="avc1.dd00dd, mp4a.40.9"\n' +
'media.m3u8\n'
);
// media
this.standardXHRResponse(this.requests.shift());
// source buffer won't be created until we have our first segment
this.player.tech(true).vhs.playlistController_.mainSegmentLoader_.one('appending', () => {
// always create separate audio and video source buffers
assert.equal(codecs.length, 2, 'created two source buffers');
assert.notEqual(
codecs.indexOf('audio/mp4;codecs="mp4a.40.9"'),
-1,
'specified the audio codec'
);
assert.notEqual(
codecs.indexOf('video/mp4;codecs="avc1.dd00dd"'),
-1,
'specified the video codec'
);
done();
});
// segment 0
this.standardXHRResponse(this.requests.shift(), muxedSegment());
});
QUnit.test('including HLS as a tech does not error', function(assert) {
this.player.dispose();
this.player = createPlayer({
techOrder: ['vhs', 'html5']
});
this.clock.tick(1);
assert.ok(this.player, 'created the player');
assert.equal(this.env.log.warn.calls, 2, 'logged two warnings for deprecations');
});
QUnit.test('creates a PlaylistLoader on init', function(assert) {
this.player.src({
src: 'manifest/playlist.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
openMediaSource(this.player, this.clock);
this.player.src({
src: 'manifest/playlist.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
openMediaSource(this.player, this.clock);
assert.equal(this.requests[0].aborted, true, 'aborted previous src');
this.standardXHRResponse(this.requests[1]);
assert.ok(
this.player.tech_.vhs.playlists.main,
'set the main playlist'
);
assert.ok(
this.player.tech_.vhs.playlists.media(),
'set the media playlist'
);
assert.ok(
this.player.tech_.vhs.playlists.media().segments,
'the segment entries are parsed'
);
assert.strictEqual(
this.player.tech_.vhs.playlists.main.playlists[0],
this.player.tech_.vhs.playlists.media(),
'the playlist is selected'
);
});
QUnit.test('sets the duration if one is available on the playlist', function(assert) {
let events = 0;
this.player.src({
src: 'manifest/media.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
openMediaSource(this.player, this.clock);
this.player.tech_.on('durationchange', function() {
events++;
});
this.standardXHRResponse(this.requests[0]);
assert.equal(
this.player.tech_.vhs.mediaSource.duration,
40,
'set the duration'
);
assert.equal(events, 1, 'durationchange is fired');
});
QUnit.test('estimates individual segment durations if needed', function(assert) {
let changes = 0;
this.player.src({
src: 'http://example.com/manifest/missingExtinf.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
openMediaSource(this.player, this.clock);
this.player.tech_.vhs.mediaSource.duration = NaN;
this.player.tech_.on('durationchange', function() {
changes++;
});
this.standardXHRResponse(this.requests[0]);
assert.strictEqual(
this.player.tech_.vhs.mediaSource.duration,
this.player.tech_.vhs.playlists.media().segments.length * 10,
'duration is updated'
);
assert.strictEqual(changes, 1, 'one durationchange fired');
});
QUnit.test(
'translates seekable by the starting time for live playlists',
function(assert) {
this.player.src({
src: 'media.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
openMediaSource(this.player, this.clock);
this.requests.shift().respond(
200, null,
'#EXTM3U\n' +
'#EXT-X-MEDIA-SEQUENCE:15\n' +
'#EXT-X-TARGETDURATION:10\n' +
'#EXTINF:10,\n' +
'0.ts\n' +
'#EXTINF:10,\n' +
'1.ts\n' +
'#EXTINF:10,\n' +
'2.ts\n' +
'#EXTINF:10,\n' +
'3.ts\n'
);
const seekable = this.player.seekable();
assert.equal(seekable.length, 1, 'one seekable range');
assert.equal(seekable.start(0), 0, 'the earliest possible position is at zero');
assert.equal(seekable.end(0), 10, 'end is relative to the start');
}
);
QUnit.test('starts downloading a segment on loadedmetadata', function(assert) {
const done = assert.async();
this.player.src({
src: 'manifest/media.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
this.player.buffered = function() {
return createTimeRanges(0, 0);
};
openMediaSource(this.player, this.clock);
const segment = muxedSegment();
// copy the byte length since the segment bytes get cleared out
const segmentByteLength = segment.byteLength;
assert.ok(segmentByteLength, 'the segment has some number of bytes');
// media
this.standardXHRResponse(this.requests[0]);
assert.strictEqual(
this.requests[1].url,
absoluteUrl('manifest/media-00001.ts'),
'the first segment is requested'
);
this.player.tech(true).vhs.playlistController_.mainSegmentLoader_.one('appending', () => {
// verify stats
assert.equal(
this.player.tech_.vhs.stats.mediaBytesTransferred,
segmentByteLength,
'transferred the segment byte length'
);
assert.equal(this.player.tech_.vhs.stats.mediaRequests, 1, '1 request');
done();
});
// segment 0
this.standardXHRResponse(this.requests[1], segment);
});
QUnit.test('re-initializes the handler for each source', function(assert) {
let secondPlaylists;
let secondMSE;
this.player.src({
src: 'manifest/main.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
openMediaSource(this.player, this.clock);
const firstPlaylists = this.player.tech_.vhs.playlists;
const firstMSE = this.player.tech_.vhs.mediaSource;
// main
this.standardXHRResponse(this.requests.shift());
// media
this.standardXHRResponse(this.requests.shift());
const pc = this.player.tech_.vhs.playlistController_;
// need a segment request to complete for the source buffers to be created
return requestAndAppendSegment({
request: this.requests.shift(),
mediaSource: pc.mediaSource,
segmentLoader: pc.mainSegmentLoader_,
clock: this.clock,
tickClock: false
}).then(() => {
let audioBufferAborts = 0;
let videoBufferAborts = 0;
pc.mainSegmentLoader_.sourceUpdater_.audioBuffer.abort = () => audioBufferAborts++;
pc.mainSegmentLoader_.sourceUpdater_.videoBuffer.abort = () => videoBufferAborts++;
// allow timeout for making another request
this.clock.tick(1);
assert.equal(this.requests.length, 1, 'made another request');
this.player.src({
src: 'manifest/main.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
openMediaSource(this.player, this.clock);
secondPlaylists = this.player.tech_.vhs.playlists;
secondMSE = this.player.tech_.vhs.mediaSource;
assert.equal(audioBufferAborts, 1, 'aborted the old audio source buffer');
assert.equal(videoBufferAborts, 1, 'aborted the old video source buffer');
assert.ok(this.requests[0].aborted, 'aborted the old segment request');
assert.notStrictEqual(
firstPlaylists,
secondPlaylists,
'the playlist object is not reused'
);
assert.notStrictEqual(firstMSE, secondMSE, 'the media source object is not reused');
});
});
QUnit.test(
'triggers a media source error when an initial playlist request errors',
function(assert) {
this.player.src({
src: 'manifest/main.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
openMediaSource(this.player, this.clock);
this.requests.pop().respond(500);
assert.equal(
this.player.tech_.vhs.mediaSource.error_,
'network',
'a network error is triggered'
);
}
);
QUnit.test(
'triggers a player error when an initial playlist request errors and the media source ' +
'isn\'t open',
function(assert) {
const done = assert.async();
const origError = videojs.log.error;
const errLogs = [];
const endOfStreams = [];
videojs.log.error = (log) => errLogs.push(log);
this.player.src({
src: 'manifest/main.m3u8',
type: 'application/vnd.apple.mpegurl'
});
openMediaSource(this.player, this.clock);
this.player.tech_.vhs.playlistController_.mediaSource.readyState = 'closed';
this.player.on('error', () => {
const error = this.player.error();
assert.equal(error.code, 2, 'error has correct code');
assert.equal(
error.message,
'HLS playlist request error at URL: manifest/main.m3u8.',
'error has correct message'
);
assert.equal(errLogs.length, 1, 'logged an error');
videojs.log.error = origError;
assert.notOk(this.player.tech_.vhs.mediaSource.error_, 'no media source error');
done();
});
this.requests.pop().respond(500);
}
);
QUnit.test('downloads media playlists after loading the main', function(assert) {
const done = assert.async();
this.player.src({
src: 'manifest/main.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
openMediaSource(this.player, this.clock);
this.player.tech_.vhs.bandwidth = 20e10;
// main
this.standardXHRResponse(this.requests[0]);
// media
this.standardXHRResponse(this.requests[1]);
const segment = muxedSegment();
// copy the byte length since the segment bytes get cleared out
const segmentByteLength = segment.byteLength;
assert.ok(segmentByteLength, 'the segment has some number of bytes');
this.player.tech(true).vhs.playlistController_.mainSegmentLoader_.one('appending', () => {
// verify stats
assert.equal(
this.player.tech_.vhs.stats.mediaBytesTransferred,
segmentByteLength,
'transferred the segment byte length'
);
assert.equal(this.player.tech_.vhs.stats.mediaRequests, 1, '1 request');
done();
});
// segment 0
this.standardXHRResponse(this.requests[2], segment);
assert.strictEqual(
this.requests[0].url,
'manifest/main.m3u8',
'main playlist requested'
);
assert.strictEqual(
this.requests[1].url,
absoluteUrl('manifest/media2.m3u8'),
'media playlist requested'
);
assert.strictEqual(
this.requests[2].url,
absoluteUrl('manifest/media2-00001.ts'),
'first segment requested'
);
});
QUnit.test('setting bandwidth resets throughput', function(assert) {
this.player.src({
src: 'manifest/main.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
this.player.tech_.vhs.throughput = 1000;
assert.strictEqual(
this.player.tech_.vhs.throughput,
1000,
'throughput is set'
);
this.player.tech_.vhs.bandwidth = 20e10;
assert.strictEqual(
this.player.tech_.vhs.throughput,
0,
'throughput is reset when bandwidth is specified'
);
});
QUnit.test('a thoughput of zero is ignored in systemBandwidth', function(assert) {
this.player.src({
src: 'manifest/main.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
this.player.tech_.vhs.bandwidth = 20e10;
assert.strictEqual(
this.player.tech_.vhs.throughput,
0,
'throughput is reset when bandwidth is specified'
);
assert.strictEqual(
this.player.tech_.vhs.systemBandwidth,
20e10,
'systemBandwidth is the same as bandwidth'
);
});
QUnit.test(
'systemBandwidth is a combination of thoughput and bandwidth',
function(assert) {
this.player.src({
src: 'manifest/main.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
this.player.tech_.vhs.bandwidth = 20e10;
this.player.tech_.vhs.throughput = 20e10;
// 1 / ( 1 / 20e10 + 1 / 20e10) = 10e10
assert.strictEqual(
this.player.tech_.vhs.systemBandwidth,
10e10,
'systemBandwidth is the combination of bandwidth and throughput'
);
}
);
QUnit.module('NetworkInformationApi', hooks => {
hooks.beforeEach(function(assert) {
this.env = useFakeEnvironment(assert);
this.ogNavigator = window.navigator;
this.clock = this.env.clock;
this.resetNavigatorConnection = (connection = {}) => {
// Need to delete the property before setting since navigator doesn't have a setter
delete window.navigator;
window.navigator = {
connection
};
};
});
hooks.afterEach(function() {
this.env.restore();
window.navigator = this.ogNavigator;
});
QUnit[testOrSkip](
'bandwidth returns networkInformation.downlink when useNetworkInformationApi option is enabled',
function(assert) {
this.resetNavigatorConnection({
downlink: 10
});
this.player = createPlayer({ html5: { vhs: { useNetworkInformationApi: true } } });
this.player.src({
src: 'manifest/main.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
// downlink in bits = 10 * 1000000 = 10e6
assert.strictEqual(
this.player.tech_.vhs.bandwidth,
10e6,
'bandwidth equals networkInfo.downlink represented as bits per second'
);
}
);
QUnit[testOrSkip](
'bandwidth uses player-estimated bandwidth when its value is greater than networkInformation.downLink and both values are >= 10 Mbps',
function(assert) {
this.resetNavigatorConnection({
// 10 Mbps or 10e6
downlink: 10
});
this.player = createPlayer({ html5: { vhs: { useNetworkInformationApi: true } } });
this.player.src({
src: 'manifest/main.m3u8',
type: 'application/vnd.apple.mpegurl'
});
this.clock.tick(1);
this.player.tech_.vhs.bandwidth = 20e6;
assert.strictEqual(
this.player.tech_.vhs.bandwidth,
20e6,
'bandwidth getter returned the player-estimated bandwidth value'
);
}
);
QUnit[testOrSkip](