forked from videojs/http-streaming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource-updater.test.js
1363 lines (1104 loc) · 44.2 KB
/
source-updater.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 window from 'global/window';
import QUnit from 'qunit';
import videojs from 'video.js';
import SourceUpdater from '../src/source-updater';
import {mp4VideoInit, mp4AudioInit, mp4Video, mp4Audio} from 'create-test-data!segments';
import { timeRangesEqual } from './custom-assertions.js';
import { QUOTA_EXCEEDED_ERR } from '../src/error-codes';
import {createTimeRanges} from '../src/util/vjs-compat';
const checkInitialDuration = function({duration}) {
QUnit.assert.ok(Number.isNaN(duration), 'starting duration as expected');
};
const concatSegments = (...segments) => {
let byteLength = segments.reduce((acc, cv) => {
acc += cv.byteLength;
return acc;
}, 0);
const dest = new Uint8Array(byteLength);
while (segments.length) {
const segment = segments.shift();
dest.set(segment, byteLength - segment.byteLength);
byteLength -= segment.byteLength;
}
return dest;
};
const mp4VideoTotal = () => concatSegments(mp4VideoInit(), mp4Video());
const mp4AudioTotal = () => concatSegments(mp4AudioInit(), mp4Audio());
QUnit.module('Source Updater', {
beforeEach() {
this.fixture = document.getElementById('qunit-fixture');
this.video = document.createElement('video');
this.fixture.appendChild(this.video);
this.mediaSource = new window.MediaSource();
// need to attach the real media source to a video element for the media source to
// change to an open ready state
this.sourceUpdater = new SourceUpdater(this.mediaSource);
this.objurls = [];
this.createObjectURL = (mediaSource) => {
const url_ = URL.createObjectURL(mediaSource);
this.objurls.push(url_);
return url_;
};
this.video.src = this.createObjectURL(this.mediaSource);
// This is normally done at the top level of the plugin, but will not happen in
// an isolated module.
this.sourceUpdater.initializedEme();
// wait for the source to open (or error) before running through tests
return new Promise((accept, reject) => {
this.mediaSource.addEventListener('sourceopen', accept);
this.mediaSource.addEventListener('error', reject);
});
},
afterEach() {
this.sourceUpdater.dispose();
this.video.src = '';
this.video.removeAttribute('src');
while (this.fixture.firstChild) {
this.fixture.removeChild(this.fixture.firstChild);
}
this.objurls.forEach(function(url_) {
URL.revokeObjectURL(url_);
});
this.objurls.length = 0;
this.video = null;
}
});
QUnit.test('verifies that sourcebuffer is in source buffers list before attempting actions', function(assert) {
this.sourceUpdater.dispose();
const actionCalls = {
videoRemoveSourceBuffer: 0,
videoAppendBuffer: 0,
videoRemove: 0,
videoTimestampOffset: 0,
videoBuffered: 0,
videoAbort: 0,
videoChangeType: 0,
audioRemoveSourceBuffer: 0,
audioAppendBuffer: 0,
audioRemove: 0,
audioTimestampOffset: 0,
audioBuffered: 0,
audioAbort: 0,
audioChangeType: 0
};
const createMediaSource = () => {
const mediaSource = new videojs.EventTarget();
mediaSource.readyState = 'open';
mediaSource.sourceBuffers = [];
mediaSource.removeSourceBuffer = (sb) => {
if (sb.type_ === 'video') {
actionCalls.videoRemoveSourceBuffer++;
} else {
actionCalls.audioRemoveSourceBuffer++;
}
};
mediaSource.addSourceBuffer = (mime) => {
const type = (/^audio/).test(mime) ? 'audio' : 'video';
const sb = new videojs.EventTarget();
sb.appendBuffer = () => {
actionCalls[`${type}AppendBuffer`]++;
};
sb.remove = () => {
actionCalls[`${type}Remove`]++;
};
sb.abort = () => {
actionCalls[`${type}Abort`]++;
};
sb.changeType = () => {
actionCalls[`${type}ChangeType`]++;
};
sb.type_ = type;
Object.defineProperty(sb, 'buffered', {
get: () => {
actionCalls[`${type}Buffered`]++;
return createTimeRanges([0, 15]);
}
});
Object.defineProperty(sb, 'timestampOffset', {
get: () => {
return 444;
},
set: () => {
actionCalls[`${type}TimestampOffset`]++;
}
});
return sb;
};
return mediaSource;
};
const runTestFunctions = () => {
this.sourceUpdater.canChangeType = () => true;
this.sourceUpdater.canRemoveSourceBuffer = () => true;
this.sourceUpdater.appendBuffer({type: 'video', bytes: []});
this.sourceUpdater.videoBuffer.trigger('updateend');
this.sourceUpdater.appendBuffer({type: 'audio', bytes: []});
this.sourceUpdater.audioBuffer.trigger('updateend');
this.sourceUpdater.audioBuffered();
this.sourceUpdater.videoBuffered();
this.sourceUpdater.buffered();
this.sourceUpdater.removeVideo(0, 1);
this.sourceUpdater.videoBuffer.trigger('updateend');
this.sourceUpdater.removeAudio(0, 1);
this.sourceUpdater.audioBuffer.trigger('updateend');
this.sourceUpdater.changeType('audio', 'foo');
this.sourceUpdater.changeType('video', 'bar');
this.sourceUpdater.abort('audio');
this.sourceUpdater.abort('video');
this.sourceUpdater.audioTimestampOffset(123);
this.sourceUpdater.videoTimestampOffset(123);
this.sourceUpdater.removeSourceBuffer('video');
this.sourceUpdater.removeSourceBuffer('audio');
};
this.sourceUpdater = new SourceUpdater(createMediaSource());
this.sourceUpdater.initializedEme();
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2',
video: 'avc1.4d400d'
});
assert.ok(this.sourceUpdater.videoBuffer, 'has video buffer');
assert.ok(this.sourceUpdater.audioBuffer, 'has audio buffer');
this.sourceUpdater.mediaSource.sourceBuffers = [];
runTestFunctions();
Object.keys(actionCalls).forEach((name) => {
assert.equal(actionCalls[name], 0, `no ${name} without sourcebuffer in list`);
});
this.sourceUpdater.dispose();
this.sourceUpdater = new SourceUpdater(createMediaSource());
this.sourceUpdater.initializedEme();
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2',
video: 'avc1.4d400d'
});
assert.ok(this.sourceUpdater.videoBuffer, 'has video buffer');
assert.ok(this.sourceUpdater.audioBuffer, 'has audio buffer');
this.sourceUpdater.mediaSource.sourceBuffers = [
this.sourceUpdater.videoBuffer,
this.sourceUpdater.audioBuffer
];
runTestFunctions();
assert.deepEqual(actionCalls, {
audioAbort: 1,
audioAppendBuffer: 1,
audioBuffered: 12,
audioChangeType: 1,
audioRemove: 1,
audioRemoveSourceBuffer: 1,
audioTimestampOffset: 1,
videoAbort: 1,
videoAppendBuffer: 1,
videoBuffered: 12,
videoChangeType: 1,
videoRemove: 1,
videoRemoveSourceBuffer: 1,
videoTimestampOffset: 1
}, 'calls functions correctly with sourcebuffer in list');
});
QUnit.test('waits for sourceopen to create source buffers', function(assert) {
this.sourceUpdater.dispose();
this.video.src = '';
this.video.removeAttribute('src');
this.video = document.createElement('video');
this.mediaSource = new window.MediaSource();
// need to attach the real media source to a video element for the media source to
// change to an open ready state
this.video.src = this.createObjectURL(this.mediaSource);
this.sourceUpdater = new SourceUpdater(this.mediaSource);
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2',
video: 'avc1.4d400d'
});
assert.notOk(this.sourceUpdater.audioBuffer, 'no audio buffer');
assert.notOk(this.sourceUpdater.videoBuffer, 'no video buffer');
// wait for the source to open (or error) before running through tests
return new Promise((accept, reject) => {
this.mediaSource.addEventListener('sourceopen', () => {
assert.ok(this.sourceUpdater.audioBuffer, 'audio buffer created');
assert.ok(this.sourceUpdater.videoBuffer, 'video buffer created');
accept();
});
this.mediaSource.addEventListener('error', reject);
});
});
QUnit.test('source buffer creation is queued', function(assert) {
// wait for the source to open (or error) before running through tests
return new Promise((accept, reject) => {
this.sourceUpdater.dispose();
this.video.src = '';
this.video.removeAttribute('src');
this.video = document.createElement('video');
this.mediaSource = new window.MediaSource();
this.mediaSource.addEventListener('sourceopen', () => {
assert.equal(this.sourceUpdater.queue.length, 3, 'three things in queue');
assert.equal(this.sourceUpdater.pendingQueue, null, 'nothing in pendingQueue');
assert.deepEqual(
this.sourceUpdater.queue.map((i) => i.name),
['addSourceBuffer', 'addSourceBuffer', 'appendBuffer'],
'queue is as expected'
);
accept();
});
// need to attach the real media source to a video element for the media source to
// change to an open ready state
this.video.src = this.createObjectURL(this.mediaSource);
this.sourceUpdater = new SourceUpdater(this.mediaSource);
this.mediaSource.addEventListener('error', reject);
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2',
video: 'avc1.4d400d'
});
this.sourceUpdater.appendBuffer({type: 'video', bytes: mp4VideoTotal()});
});
});
QUnit.test('initial values', function(assert) {
const videoOffset = this.sourceUpdater.videoTimestampOffset();
const audioOffset = this.sourceUpdater.audioTimestampOffset();
assert.equal(videoOffset, 0, 'initial video timestamp offset is 0');
assert.equal(audioOffset, 0, 'initial audio timestamp offset is 0');
assert.equal(this.sourceUpdater.ready(), false, 'not ready by default');
assert.equal(this.sourceUpdater.updating(), false, 'not updating by default');
});
QUnit.test('can set audio timestamp offset', function(assert) {
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2'
});
this.sourceUpdater.audioTimestampOffset(999);
assert.equal(this.sourceUpdater.audioTimestampOffset(), 999, 'set audio timestamp offset');
});
QUnit.test('can set video timestamp offset', function(assert) {
this.sourceUpdater.createSourceBuffers({
video: 'avc1.4d400d'
});
this.sourceUpdater.videoTimestampOffset(999);
assert.equal(this.sourceUpdater.videoTimestampOffset(), 999, 'set video timestamp offset');
});
QUnit.test('can set audio and video timestamp offsets independently', function(assert) {
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2',
video: 'avc1.4d400d'
});
this.sourceUpdater.audioTimestampOffset(111);
this.sourceUpdater.videoTimestampOffset(999);
assert.equal(this.sourceUpdater.audioTimestampOffset(), 111, 'set audio timestamp offset');
assert.equal(this.sourceUpdater.videoTimestampOffset(), 999, 'set video timestamp offset');
});
QUnit.test('setting video timestamp offset without buffer is a noop', function(assert) {
// only create the audio buffer
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2'
});
this.sourceUpdater.videoTimestampOffset(999);
assert.equal(this.sourceUpdater.videoTimestampOffset(), 0, 'offset stays at initial value');
});
QUnit.test('setting audio timestamp offset without buffer is a noop', function(assert) {
// only create the video buffer
this.sourceUpdater.createSourceBuffers({
video: 'avc1.4d400d'
});
this.sourceUpdater.audioTimestampOffset(999);
assert.equal(this.sourceUpdater.audioTimestampOffset(), 0, 'offset stays at initial value');
});
QUnit.test('ready with a video buffer', function(assert) {
this.sourceUpdater.initializedEme();
this.sourceUpdater.createSourceBuffers({
video: 'avc1.4d400d'
});
assert.ok(this.sourceUpdater.ready(), 'source updater has started');
});
QUnit.test('ready with an audio buffer', function(assert) {
this.sourceUpdater.initializedEme();
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2'
});
assert.ok(this.sourceUpdater.ready(), 'source updater is ready');
});
QUnit.test('ready with both an audio and video buffer', function(assert) {
this.sourceUpdater.initializedEme();
this.sourceUpdater.createSourceBuffers({
video: 'avc1.4d400d',
audio: 'mp4a.40.2'
});
assert.ok(this.sourceUpdater.ready(), 'source updater is ready');
});
QUnit.test('ready once source buffers created and eme initialized', function(assert) {
// the module initializes by default
this.sourceUpdater.initializedEme_ = false;
assert.notOk(this.sourceUpdater.ready(), 'source updater is not ready');
this.sourceUpdater.createSourceBuffers({
video: 'avc1.4d400d',
audio: 'mp4a.40.2'
});
assert.notOk(this.sourceUpdater.ready(), 'source updater is not ready');
this.sourceUpdater.initializedEme();
assert.ok(this.sourceUpdater.ready(), 'source updater is ready');
});
QUnit.test('audioBuffered can append to and get the audio buffer', function(assert) {
const done = assert.async();
assert.equal(this.sourceUpdater.audioBuffered().length, 0, 'no buffered time range');
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2'
});
assert.equal(this.sourceUpdater.audioBuffered().length, 0, 'no buffered time range');
this.sourceUpdater.appendBuffer({type: 'audio', bytes: mp4AudioTotal()}, () => {
assert.equal(this.sourceUpdater.audioBuffered().length, 1, 'has buffered time range');
assert.ok(this.sourceUpdater.audioBuffered().end(0) > 0, 'buffered content');
done();
});
});
QUnit.test('videoBuffered can append to and gets the video buffer', function(assert) {
const done = assert.async();
assert.equal(this.sourceUpdater.videoBuffered().length, 0, 'no buffered time range');
this.sourceUpdater.createSourceBuffers({
video: 'avc1.4D001E'
});
assert.equal(this.sourceUpdater.videoBuffered().length, 0, 'no buffered time range');
this.sourceUpdater.appendBuffer({type: 'video', bytes: mp4VideoTotal()}, () => {
assert.equal(this.sourceUpdater.videoBuffered().length, 1, 'has buffered time range');
assert.ok(this.sourceUpdater.videoBuffered().end(0) > 0, 'buffered content');
done();
});
});
QUnit.test('buffered returns audio buffer when only audio', function(assert) {
const done = assert.async();
assert.equal(this.sourceUpdater.buffered().length, 0, 'no buffered time range');
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2'
});
assert.equal(this.sourceUpdater.buffered().length, 0, 'no buffered time range');
this.sourceUpdater.appendBuffer({type: 'audio', bytes: mp4AudioTotal()}, () => {
assert.equal(this.sourceUpdater.buffered().length, 1, 'has buffered time range');
assert.ok(this.sourceUpdater.buffered().end(0) > 0, 'buffered content');
done();
});
});
QUnit.test('buffered returns video buffer when only video', function(assert) {
const done = assert.async();
assert.equal(this.sourceUpdater.buffered().length, 0, 'no buffered time range');
this.sourceUpdater.createSourceBuffers({
video: 'avc1.4D001E'
});
assert.equal(this.sourceUpdater.buffered().length, 0, 'no buffered time range');
this.sourceUpdater.appendBuffer({type: 'video', bytes: mp4VideoTotal()}, () => {
assert.equal(this.sourceUpdater.buffered().length, 1, 'has buffered time range');
assert.ok(this.sourceUpdater.buffered().end(0) > 0, 'buffered content');
done();
});
});
QUnit.test('buffered returns intersection of audio and video buffers', function(assert) {
const origAudioBuffer = this.sourceUpdater.audioBuffer;
const origVideoBuffer = this.sourceUpdater.videoBuffer;
const origMediaSource = this.sourceUpdater.mediaSource;
// mocking the buffered ranges in this test because it's tough to know how much each
// browser will actually buffer
this.sourceUpdater.audioBuffer = {
buffered: createTimeRanges([[1, 2], [5.5, 5.6], [10.5, 11]])
};
this.sourceUpdater.videoBuffer = {
buffered: createTimeRanges([[1.25, 1.5], [5.1, 6.1], [10.5, 10.9]])
};
this.sourceUpdater.mediaSource = {
sourceBuffers: [
this.sourceUpdater.audioBuffer,
this.sourceUpdater.videoBuffer
]
};
timeRangesEqual(
this.sourceUpdater.buffered(),
createTimeRanges([[1.25, 1.5], [5.5, 5.6], [10.5, 10.9]]),
'buffered is intersection'
);
this.sourceUpdater.audioBuffer = origAudioBuffer;
this.sourceUpdater.videoBuffer = origVideoBuffer;
this.sourceUpdater.mediaSource = origMediaSource;
});
QUnit.test('buffered returns audio buffered if no video buffer', function(assert) {
const origAudioBuffer = this.sourceUpdater.audioBuffer;
// mocking the buffered ranges in this test because it's tough to know how much each
// browser will actually buffer
this.sourceUpdater.audioBuffer = {
buffered: createTimeRanges([[1, 2], [5.5, 5.6], [10.5, 11]])
};
timeRangesEqual(
this.sourceUpdater.buffered(),
this.sourceUpdater.audioBuffered(),
'buffered is audio'
);
this.sourceUpdater.audioBuffer = origAudioBuffer;
});
QUnit.test('buffered returns video buffered if no audio buffer', function(assert) {
const origVideoBuffer = this.sourceUpdater.videoBuffer;
// mocking the buffered ranges in this test because it's tough to know how much each
// browser will actually buffer
this.sourceUpdater.videoBuffer = {
buffered: createTimeRanges([[1.25, 1.5], [5.1, 6.1], [10.5, 10.9]])
};
timeRangesEqual(
this.sourceUpdater.buffered(),
this.sourceUpdater.videoBuffered(),
'buffered is video'
);
this.sourceUpdater.videoBuffer = origVideoBuffer;
});
QUnit.test('removeAudio removes audio buffer', function(assert) {
const done = assert.async();
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2'
});
this.sourceUpdater.appendBuffer({type: 'audio', bytes: mp4AudioTotal()}, () => {
assert.equal(this.sourceUpdater.buffered().length, 1, 'has buffered time range');
assert.ok(this.sourceUpdater.buffered().end(0) > 0, 'buffered content');
this.sourceUpdater.removeAudio(0, this.sourceUpdater.buffered().end(0), () => {
assert.equal(this.sourceUpdater.buffered().length, 0, 'no buffered conent');
done();
});
});
});
QUnit.test('removeVideo removes video buffer', function(assert) {
const done = assert.async();
this.sourceUpdater.createSourceBuffers({
video: 'avc1.4D001E'
});
this.sourceUpdater.appendBuffer({type: 'video', bytes: mp4VideoTotal()}, () => {
assert.equal(this.sourceUpdater.buffered().length, 1, 'has buffered time range');
assert.ok(this.sourceUpdater.buffered().end(0) > 0, 'buffered content');
this.sourceUpdater.removeVideo(0, this.sourceUpdater.buffered().end(0), () => {
assert.equal(this.sourceUpdater.buffered().length, 0, 'no buffered content');
done();
});
});
});
QUnit.test('removeAudio does not remove video buffer', function(assert) {
const done = assert.async();
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2',
video: 'avc1.4D001E'
});
this.sourceUpdater.appendBuffer({type: 'video', bytes: mp4VideoTotal()}, () => {
assert.ok(this.sourceUpdater.videoBuffered().end(0) > 0, 'buffered audio content');
this.sourceUpdater.appendBuffer({type: 'audio', bytes: mp4AudioTotal()}, () => {
assert.ok(this.sourceUpdater.audioBuffered().end(0) > 0, 'buffered video content');
this.sourceUpdater.removeAudio(0, this.sourceUpdater.audioBuffered().end(0), () => {
assert.equal(this.sourceUpdater.audioBuffered().length, 0, 'removed audio content');
assert.equal(this.sourceUpdater.videoBuffered().length, 1, 'has buffered video time range');
assert.ok(this.sourceUpdater.videoBuffered().end(0) > 0, 'did not remove video content');
done();
});
});
});
});
QUnit.test('removeVideo does not remove audio buffer', function(assert) {
const done = assert.async();
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2',
video: 'avc1.4D001E'
});
this.sourceUpdater.appendBuffer({type: 'video', bytes: mp4VideoTotal()}, () => {
assert.ok(this.sourceUpdater.videoBuffered().end(0) > 0, 'buffered audio content');
this.sourceUpdater.appendBuffer({type: 'audio', bytes: mp4AudioTotal()}, () => {
assert.ok(this.sourceUpdater.audioBuffered().end(0) > 0, 'buffered video content');
this.sourceUpdater.removeVideo(0, this.sourceUpdater.videoBuffered().end(0), () => {
assert.equal(this.sourceUpdater.videoBuffered().length, 0, 'removed video content');
assert.equal(this.sourceUpdater.audioBuffered().length, 1, 'has buffered audio time range');
assert.ok(this.sourceUpdater.audioBuffered().end(0) > 0, 'did not remove audio content');
done();
});
});
});
});
QUnit.test(
'audioQueueCallback calls callback immediately if queue is empty',
function(assert) {
// Source buffer must exist for the callback to run. This case isn't tested, as it isn't
// required behavior (at the moment), but is necessary to know for this test.
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2'
});
let executedCallback = false;
this.sourceUpdater.audioQueueCallback(() => {
executedCallback = true;
});
assert.ok(executedCallback, 'executed callback');
}
);
QUnit.test(
'videoQueueCallback calls callback immediately if queue is empty',
function(assert) {
// Source buffer must exist for the callback to run. This case isn't tested, as it isn't
// required behavior (at the moment), but is necessary to know for this test.
this.sourceUpdater.createSourceBuffers({
video: 'avc1.4D001E'
});
let executedCallback = false;
this.sourceUpdater.videoQueueCallback(() => {
executedCallback = true;
});
assert.ok(executedCallback, 'executed callback');
}
);
QUnit.test(
'audioQueueCallback calls callback after queue empties if queue is not empty',
function(assert) {
const done = assert.async();
// Source buffer must exist for the callback to run. This case isn't tested, as it isn't
// required behavior (at the moment), but is necessary to know for this test.
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2'
});
let executedCallback = false;
let appendedAudio = false;
this.sourceUpdater.appendBuffer({type: 'audio', bytes: mp4AudioTotal()}, () => {
appendedAudio = true;
assert.notOk(executedCallback, 'haven\'t executed callback');
setTimeout(() => {
assert.ok(executedCallback, 'executed callback');
done();
}, 1);
});
assert.notOk(appendedAudio, 'haven\'t appended audio before callback is queued');
this.sourceUpdater.audioQueueCallback(() => {
executedCallback = true;
});
}
);
QUnit.test(
'videoQueueCallback calls callback after queue empties if queue is not empty',
function(assert) {
const done = assert.async();
// Source buffer must exist for the callback to run. This case isn't tested, as it isn't
// required behavior (at the moment), but is necessary to know for this test.
this.sourceUpdater.createSourceBuffers({
video: 'avc1.4D001E'
});
let executedCallback = false;
let appendedVideo = false;
this.sourceUpdater.appendBuffer({type: 'video', bytes: mp4VideoTotal()}, () => {
appendedVideo = true;
assert.notOk(executedCallback, 'haven\'t executed callback');
setTimeout(() => {
assert.ok(executedCallback, 'executed callback');
done();
}, 1);
});
assert.notOk(appendedVideo, 'haven\'t appended video before callback is queued');
this.sourceUpdater.videoQueueCallback(() => {
executedCallback = true;
});
}
);
QUnit.test(
'audioQueueCallback does not call video queue callback after queue empties',
function(assert) {
const done = assert.async();
// Source buffer must exist for the callback to run. This case isn't tested, as it isn't
// required behavior (at the moment), but is necessary to know for this test.
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2',
video: 'avc1.4D001E'
});
let executedVideoCallback = false;
let appendedAudio = false;
// we have to append video
this.sourceUpdater.appendBuffer({type: 'video', bytes: mp4VideoTotal()}, () => {
this.sourceUpdater.appendBuffer({type: 'audio', bytes: mp4AudioTotal()}, () => {
appendedAudio = true;
assert.notOk(executedVideoCallback, 'haven\'t executed callback');
setTimeout(() => {
assert.notOk(executedVideoCallback, 'haven\'t executed callback');
done();
}, 0);
});
// add a video queue entry so that the video queue callback doesn't immediately run
this.sourceUpdater.queuePending.video = {};
assert.notOk(appendedAudio, 'haven\'t appended audio before callback is queued');
this.sourceUpdater.videoQueueCallback(() => {
executedVideoCallback = true;
});
});
}
);
QUnit.test(
'videoQueueCallback does not call audio queue callback after queue empties',
function(assert) {
const done = assert.async();
// Source buffer must exist for the callback to run. This case isn't tested, as it isn't
// required behavior (at the moment), but is necessary to know for this test.
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2',
video: 'avc1.4D001E'
});
let executedAudioCallback = false;
let appendedVideo = false;
this.sourceUpdater.appendBuffer({type: 'video', bytes: mp4VideoTotal()}, () => {
appendedVideo = true;
assert.notOk(executedAudioCallback, 'haven\'t executed callback');
setTimeout(() => {
assert.notOk(executedAudioCallback, 'haven\'t executed callback');
done();
}, 0);
});
// add a video queue entry so that the video queue callback doesn't immediately run
this.sourceUpdater.queuePending.audio = {};
assert.notOk(appendedVideo, 'haven\'t appended video before callback is queued');
this.sourceUpdater.audioQueueCallback(() => {
executedAudioCallback = true;
});
}
);
QUnit.test('updating returns true if audio buffer is updating', function(assert) {
const done = assert.async();
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2'
});
assert.notOk(this.sourceUpdater.updating(), 'not updating by default');
this.sourceUpdater.appendBuffer({type: 'audio', bytes: mp4AudioTotal()}, () => {
assert.notOk(this.sourceUpdater.updating(), 'not updating after append');
done();
});
assert.ok(this.sourceUpdater.updating(), 'updating during audio append');
});
QUnit.test('updating returns true if video buffer is updating', function(assert) {
const done = assert.async();
this.sourceUpdater.createSourceBuffers({
video: 'avc1.4D001E'
});
assert.notOk(this.sourceUpdater.updating(), 'not updating by default');
this.sourceUpdater.appendBuffer({type: 'video', bytes: mp4VideoTotal()}, () => {
assert.notOk(this.sourceUpdater.updating(), 'not updating after append');
done();
});
assert.ok(this.sourceUpdater.updating(), 'updating during append');
});
QUnit.test(
'updating returns true if either audio or video buffer is updating',
function(assert) {
const done = assert.async();
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2',
video: 'avc1.4D001E'
});
assert.notOk(this.sourceUpdater.updating(), 'not updating by default');
this.sourceUpdater.appendBuffer({type: 'video', bytes: mp4VideoTotal()}, () => {
assert.notOk(this.sourceUpdater.updating(), 'not updating after append');
this.sourceUpdater.appendBuffer({type: 'audio', bytes: mp4AudioTotal()}, () => {
assert.notOk(this.sourceUpdater.updating(), 'not updating after append');
done();
});
assert.ok(this.sourceUpdater.updating(), 'updating during append');
});
assert.ok(this.sourceUpdater.updating(), 'updating during append');
}
);
QUnit.test('dispose aborts and clears out audio and video buffers', function(assert) {
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2',
video: 'avc1.4D001E'
});
// while this maintains internal logic of source updater (knowing the properties), it is
// good for this test to verify that those properties are cleared out
assert.ok(this.sourceUpdater.audioBuffer, 'have an audio buffer');
assert.ok(this.sourceUpdater.videoBuffer, 'have a video buffer');
// Let the original aborts run so that we don't mock out any behaviors.
const origAudioAbort =
this.sourceUpdater.audioBuffer.abort.bind(this.sourceUpdater.audioBuffer);
const origVideoAbort =
this.sourceUpdater.videoBuffer.abort.bind(this.sourceUpdater.videoBuffer);
let abortedAudio = false;
let abortedVideo = false;
this.sourceUpdater.audioBuffer.abort = () => {
abortedAudio = true;
origAudioAbort();
};
this.sourceUpdater.videoBuffer.abort = () => {
abortedVideo = true;
origVideoAbort();
};
this.sourceUpdater.dispose();
assert.ok(abortedAudio, 'aborted audio');
assert.ok(abortedVideo, 'aborted video');
assert.notOk(this.sourceUpdater.audioBuffer, 'removed audioBuffer reference');
assert.notOk(this.sourceUpdater.videoBuffer, 'removed videoBuffer reference');
});
QUnit.test('no error passed by default in done callback', function(assert) {
const done = assert.async();
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2'
});
this.sourceUpdater.appendBuffer({type: 'audio', bytes: mp4AudioTotal()}, (error) => {
assert.notOk(error, 'no error');
done();
});
});
QUnit.test('audio source buffer error passed in done callback', function(assert) {
const done = assert.async();
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2'
});
const corruptVideoSegment = mp4VideoTotal();
// throw some bad data in the segment
Array.prototype.fill.call(corruptVideoSegment, 5, 100, 500);
// errors when appending video to an audio buffer
this.sourceUpdater.appendBuffer({type: 'audio', bytes: corruptVideoSegment}, (error) => {
assert.ok(error, 'error passed back');
done();
});
});
QUnit.test('video source buffer error passed in done callback', function(assert) {
const done = assert.async();
this.sourceUpdater.createSourceBuffers({
video: 'avc1.4D001E'
});
const corruptAudioSegment = mp4AudioTotal();
// throw some bad data in the segment
Array.prototype.fill.call(corruptAudioSegment, 5, 100, 500);
// errors when appending audio to a video buffer
this.sourceUpdater.appendBuffer({type: 'video', bytes: corruptAudioSegment}, (error) => {
assert.ok(error, 'error passed back');
done();
});
});
QUnit.test(
'setDuration processes immediately if not waiting on source buffers',
function(assert) {
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2',
video: 'avc1.4D001E'
});
checkInitialDuration(this.mediaSource);
this.sourceUpdater.setDuration(11);
assert.equal(this.mediaSource.duration, 11, 'set duration on media source');
}
);
QUnit.test('setDuration waits for audio buffer to finish updating', function(assert) {
const done = assert.async();
assert.expect(5);
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2'
});
assert.notOk(this.sourceUpdater.updating(), 'not updating by default');
this.sourceUpdater.appendBuffer({type: 'audio', bytes: mp4AudioTotal()}, () => {
// duration is set to infinity if content is appended before an explicit duration is
// set https://w3c.github.io/media-source/#sourcebuffer-init-segment-received
assert.equal(this.mediaSource.duration, Infinity, 'duration not set on media source');
});
this.sourceUpdater.setDuration(11, () => {
assert.equal(this.mediaSource.duration, 11, 'set duration on media source');
done();
});
checkInitialDuration(this.mediaSource);
assert.ok(this.sourceUpdater.updating(), 'updating during appends');
});
QUnit.test('setDuration waits for video buffer to finish updating', function(assert) {
const done = assert.async();
assert.expect(5);
this.sourceUpdater.createSourceBuffers({
audio: 'mp4a.40.2',
video: 'avc1.4D001E'
});
assert.notOk(this.sourceUpdater.updating(), 'not updating by default');
this.sourceUpdater.appendBuffer({type: 'video', bytes: mp4VideoTotal()}, () => {
// duration is set to infinity if content is appended before an explicit duration is
// set https://w3c.github.io/media-source/#sourcebuffer-init-segment-received
assert.equal(this.mediaSource.duration, Infinity, 'duration not set on media source');
});
this.sourceUpdater.setDuration(11, () => {
assert.equal(this.mediaSource.duration, 11, 'set duration on media source');
done();
});
checkInitialDuration(this.mediaSource);