-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathbuffer-controller-operations.ts
More file actions
1107 lines (1001 loc) · 35.6 KB
/
buffer-controller-operations.ts
File metadata and controls
1107 lines (1001 loc) · 35.6 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 { expect, use } from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import BufferController from '../../../src/controller/buffer-controller';
import { FragmentTracker } from '../../../src/controller/fragment-tracker';
import { ErrorDetails, ErrorTypes } from '../../../src/errors';
import { Events } from '../../../src/events';
import Hls from '../../../src/hls';
import { ElementaryStreamTypes, Fragment } from '../../../src/loader/fragment';
import M3U8Parser from '../../../src/loader/m3u8-parser';
import { PlaylistLevelType } from '../../../src/types/loader';
import { ChunkMetadata } from '../../../src/types/transmuxer';
import {
MockMediaElement,
MockMediaSource,
type MockSourceBuffer,
} from '../../mocks/mock-media';
import type BufferOperationQueue from '../../../src/controller/buffer-operation-queue';
import type {
BufferOperation,
BufferOperationQueues,
SourceBufferName,
SourceBufferTrackSet,
} from '../../../src/types/buffer';
import type {
ComponentAPI,
NetworkComponentAPI,
} from '../../../src/types/component-api';
import type { BufferAppendingData } from '../../../src/types/events';
use(sinonChai);
const sandbox = sinon.createSandbox();
type HlsTestable = Omit<Hls, 'networkControllers' | 'coreComponents'> & {
coreComponents: ComponentAPI[];
networkControllers: NetworkComponentAPI[];
};
const queueNames: Array<SourceBufferName> = ['audio', 'video'];
function getSourceBufferTracks(bufferController: BufferController) {
return (bufferController as any).tracks as SourceBufferTrackSet;
}
function getSourceBufferTrack(
bufferController: BufferController,
type: SourceBufferName,
) {
return getSourceBufferTracks(bufferController)[type];
}
function setSourceBufferBufferedRange(
bufferController: BufferController,
type: SourceBufferName,
start: number,
end: number,
) {
const sb = getSourceBufferTrack(bufferController, type)
?.buffer as unknown as MockSourceBuffer;
sb.setBuffered(start, end);
}
function evokeTrimBuffers(hls: HlsTestable) {
const frag = new Fragment(PlaylistLevelType.MAIN, '');
hls.trigger(Events.FRAG_CHANGED, { frag });
}
describe('BufferController with attached media', function () {
let timers: sinon.SinonFakeTimers;
let hls: HlsTestable;
let fragmentTracker: FragmentTracker;
let bufferController: BufferController;
let operationQueue: BufferOperationQueue;
let triggerSpy: sinon.SinonSpy;
let setTimeoutSpy: sinon.SinonSpy;
let clearTimeoutSpy: sinon.SinonSpy;
let shiftAndExecuteNextSpy: sinon.SinonSpy;
let queueAppendBlockerSpy: sinon.SinonSpy;
let mockMedia: MockMediaElement;
let mockMediaSource: MockMediaSource;
beforeEach(function () {
timers = sinon.useFakeTimers({ shouldClearNativeTimers: true } as any);
hls = new Hls({
// debug: true,
}) as unknown as HlsTestable;
fragmentTracker = new FragmentTracker(hls as unknown as Hls);
hls.networkControllers.forEach((component) => component.destroy());
hls.networkControllers.length = 0;
hls.coreComponents.forEach((component) => component.destroy());
hls.coreComponents.length = 0;
bufferController = new BufferController(
hls as unknown as Hls,
fragmentTracker,
);
operationQueue = (bufferController as any).operationQueue;
// MEDIA_ATTACHING
(bufferController as any).media = mockMedia = new MockMediaElement();
(bufferController as any).mediaSource = mockMediaSource =
new MockMediaSource();
// checkPendingTracks > createSourceBuffers
hls.trigger(Events.BUFFER_CODECS, {
audio: {
id: 'audio',
container: 'audio/mp4',
},
video: {
id: 'main',
container: 'video/mp4',
},
});
triggerSpy = sandbox.spy(hls, 'trigger');
setTimeoutSpy = sandbox.spy(self, 'setTimeout');
clearTimeoutSpy = sandbox.spy(self, 'clearTimeout');
shiftAndExecuteNextSpy = sandbox.spy(operationQueue, 'shiftAndExecuteNext');
queueAppendBlockerSpy = sandbox.spy(operationQueue, 'appendBlocker');
});
afterEach(function () {
sandbox.restore();
timers.restore();
hls.destroy();
});
it('cycles the SourceBuffer operation queue on updateend', function () {
const currentOnComplete = sandbox.spy();
const currentOperation: BufferOperation = {
label: '',
execute: () => {},
onStart: () => {},
onComplete: currentOnComplete,
onError: () => {},
};
const nextExecute = sandbox.spy();
const nextOperation: BufferOperation = {
label: '',
execute: nextExecute,
onStart: () => {},
onComplete: () => {},
onError: () => {},
};
queueNames.forEach((name, i) => {
const currentQueue = (operationQueue as any).queues[
name
] as BufferOperation[];
currentQueue.push(currentOperation, nextOperation);
const track = getSourceBufferTrack(bufferController, name);
expect(bufferController)
.to.have.property('tracks')
.which.has.property(name);
if (!track) {
return;
}
expect(track).to.have.property('buffer');
const buffer = track.buffer;
if (!buffer) {
return;
}
buffer.dispatchEvent(new Event('updateend'));
expect(
currentOnComplete,
'onComplete should have been called on the current operation',
).to.have.callCount(i + 1);
expect(
shiftAndExecuteNextSpy,
'The queue should have been cycled',
).to.have.callCount(i + 1);
});
});
it('does not cycle the SourceBuffer operation queue on error', function () {
const onError = sandbox.spy();
const operation: BufferOperation = {
label: '',
execute: () => {},
onStart: () => {},
onComplete: () => {},
onError,
};
queueNames.forEach((name, i) => {
const currentQueue = (
(operationQueue as any).queues as BufferOperationQueues
)[name];
currentQueue.push(operation);
const errorEvent = new Event('error');
getSourceBufferTrack(bufferController, name)?.buffer?.dispatchEvent(
errorEvent,
);
const sbErrorObject = triggerSpy.getCall(0).lastArg.error;
expect(
onError,
'onError should have been called on the current operation',
).to.have.callCount(i + 1);
expect(
onError,
'onError should be called with an error object',
).to.have.been.calledWith(sbErrorObject);
expect(sbErrorObject.message).equals(
'audio SourceBuffer error. MediaSource readyState: open',
);
expect(
triggerSpy,
'ERROR should have been triggered in response to the SourceBuffer error',
).to.have.been.calledWith(Events.ERROR, {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.BUFFER_APPENDING_ERROR,
sourceBufferName: triggerSpy.getCall(0).lastArg.sourceBufferName,
error: triggerSpy.getCall(0).lastArg.error,
fatal: false,
});
expect(shiftAndExecuteNextSpy, 'The queue should not have been cycled').to
.have.not.been.called;
});
});
describe('onBufferAppending', function () {
it('should enqueue and execute an append operation', function () {
const queueAppendSpy = sandbox.spy(operationQueue, 'append');
queueNames.forEach((name, i) => {
const track = getSourceBufferTrack(bufferController, name);
const buffer = track?.buffer;
expect(buffer).to.not.be.undefined;
if (!buffer) {
return;
}
const segmentData = new Uint8Array();
const frag = new Fragment(PlaylistLevelType.MAIN, '');
const chunkMeta = new ChunkMetadata(0, 0, 0, 0);
const data: BufferAppendingData = {
parent: PlaylistLevelType.MAIN,
type: name,
data: segmentData,
frag,
part: null,
chunkMeta,
};
hls.trigger(Events.BUFFER_APPENDING, data);
expect(
queueAppendSpy,
'The append operation should have been enqueued',
).to.have.callCount(i + 1);
buffer.dispatchEvent(new Event('updateend'));
expect(
track.ended,
`The ${name} SourceBufferTrack should not be marked "ended" after an append occurred`,
).to.be.false;
expect(
buffer.appendBuffer,
'appendBuffer should have been called with the remuxed data',
).to.have.been.calledWith(segmentData);
expect(
triggerSpy,
'BUFFER_APPENDED should be triggered upon completion of the operation',
).to.have.been.calledWith(Events.BUFFER_APPENDED, {
parent: 'main',
type: name,
timeRanges: {
audio: getSourceBufferTrack(bufferController, 'audio')?.buffer
?.buffered,
video: getSourceBufferTrack(bufferController, 'video')?.buffer
?.buffered,
},
frag,
part: null,
chunkMeta,
});
expect(
shiftAndExecuteNextSpy,
'The queue should have been cycled',
).to.have.callCount(i + 1);
});
});
it('should not set timeout during buffer append operation when appendTimeout is Infinity', function () {
queueNames.forEach((name, i) => {
const track = getSourceBufferTrack(bufferController, name);
const buffer = track?.buffer;
expect(buffer).to.not.be.undefined;
if (!buffer) {
return;
}
const segmentData = new Uint8Array();
const frag = new Fragment(PlaylistLevelType.MAIN, '');
const chunkMeta = new ChunkMetadata(0, 0, 0, 0);
const data: BufferAppendingData = {
parent: PlaylistLevelType.MAIN,
type: name,
data: segmentData,
frag,
part: null,
chunkMeta,
};
hls.trigger(Events.BUFFER_APPENDING, data);
expect(setTimeoutSpy).to.not.have.been.called;
});
});
it('should set timeout during buffer append operation when appendTimeout is finite', function () {
hls.config.appendTimeout = 5000;
queueNames.forEach((name, i) => {
const track = getSourceBufferTrack(bufferController, name);
const buffer = track?.buffer;
expect(buffer).to.not.be.undefined;
if (!buffer) {
return;
}
const segmentData = new Uint8Array();
const frag = new Fragment(PlaylistLevelType.MAIN, '');
const chunkMeta = new ChunkMetadata(0, 0, 0, 0);
const data: BufferAppendingData = {
parent: PlaylistLevelType.MAIN,
type: name,
data: segmentData,
frag,
part: null,
chunkMeta,
};
hls.trigger(Events.BUFFER_APPENDING, data);
expect(setTimeoutSpy).to.have.callCount(i + 1);
});
});
it('should clear timeout on successful buffer append completion', function () {
hls.config.appendTimeout = 5000;
queueNames.forEach((name, i) => {
const track = getSourceBufferTrack(bufferController, name);
const buffer = track?.buffer;
expect(buffer).to.not.be.undefined;
if (!buffer) {
return;
}
const segmentData = new Uint8Array();
const frag = new Fragment(PlaylistLevelType.MAIN, '');
const chunkMeta = new ChunkMetadata(0, 0, 0, 0);
const data: BufferAppendingData = {
parent: PlaylistLevelType.MAIN,
type: name,
data: segmentData,
frag,
part: null,
chunkMeta,
};
hls.trigger(Events.BUFFER_APPENDING, data);
const timeoutId = track?.bufferAppendTimeoutId;
expect(timeoutId).to.be.a('number');
expect(setTimeoutSpy).to.have.callCount(i + 1);
buffer.dispatchEvent(new Event('updateend'));
expect(clearTimeoutSpy).to.have.been.calledWith(timeoutId);
expect(track?.bufferAppendTimeoutId).to.be.undefined;
});
});
it('should clear timeout on buffer append error', function () {
hls.config.appendTimeout = 5000;
queueNames.forEach((name, i) => {
const track = getSourceBufferTrack(bufferController, name);
const buffer = track?.buffer;
expect(buffer).to.not.be.undefined;
if (!buffer) {
return;
}
const segmentData = new Uint8Array();
const frag = new Fragment(PlaylistLevelType.MAIN, '');
const chunkMeta = new ChunkMetadata(0, 0, 0, 0);
const data: BufferAppendingData = {
parent: PlaylistLevelType.MAIN,
type: name,
data: segmentData,
frag,
part: null,
chunkMeta,
};
hls.trigger(Events.BUFFER_APPENDING, data);
const timeoutId = track?.bufferAppendTimeoutId;
expect(timeoutId).to.be.a('number');
expect(setTimeoutSpy).to.have.callCount(i + 1);
buffer.dispatchEvent(new Event('error'));
expect(clearTimeoutSpy).to.have.been.calledWith(timeoutId);
expect(track?.bufferAppendTimeoutId).to.be.undefined;
});
});
it('should handle timeout during buffer append operation', function () {
hls.config.appendTimeout = 1000;
queueNames.forEach((name, i) => {
const track = getSourceBufferTrack(bufferController, name);
const buffer = track?.buffer;
expect(buffer).to.not.be.undefined;
if (!buffer) {
return;
}
const segmentData = new Uint8Array();
const frag = new Fragment(PlaylistLevelType.MAIN, '');
const chunkMeta = new ChunkMetadata(0, 0, 0, 0);
const data: BufferAppendingData = {
parent: PlaylistLevelType.MAIN,
type: name,
data: segmentData,
frag,
part: null,
chunkMeta,
};
hls.trigger(Events.BUFFER_APPENDING, data);
expect(setTimeoutSpy).to.have.callCount(i + 1);
// expect 2*default*target-duration
expect(setTimeoutSpy).to.have.been.calledWith(sinon.match.func, 20000);
// forward timer
timers.tick(20000);
expect(buffer.abort).to.have.callCount(1);
expect(clearTimeoutSpy).to.have.callCount(i + 1);
const [, errorEvent] = triggerSpy.lastCall.args;
expect(errorEvent.error.message).to.equal(`${name}-append-timeout`);
});
});
it('should calculate timeout based on level target duration', function () {
hls.config.appendTimeout = 1000;
const level = `#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:6
#EXTINF:6
1.seg
#EXTINF:6
2.seg
#EXT-X-ENDLIST
`;
const details = M3U8Parser.parseLevelPlaylist(
level,
'http://domain/test.m3u8',
0,
PlaylistLevelType.MAIN,
0,
null,
);
mockMediaSource.duration = Infinity;
//update details
hls.trigger(Events.LEVEL_UPDATED, { details, level: 1 });
queueNames.forEach((name, i) => {
const track = getSourceBufferTrack(bufferController, name);
const buffer = track?.buffer;
expect(buffer).to.not.be.undefined;
if (!buffer) {
return;
}
const segmentData = new Uint8Array();
const frag = new Fragment(PlaylistLevelType.MAIN, '');
const chunkMeta = new ChunkMetadata(0, 0, 0, 0);
const data: BufferAppendingData = {
parent: PlaylistLevelType.MAIN,
type: name,
data: segmentData,
frag,
part: null,
chunkMeta,
};
hls.trigger(Events.BUFFER_APPENDING, data);
expect(setTimeoutSpy).to.have.callCount(i + 1);
// expect 2*level-target-duration from playlist
expect(setTimeoutSpy).to.have.been.calledWith(sinon.match.func, 12000);
});
});
it('should calculate timeout based on buffered time', function () {
hls.config.appendTimeout = 1000;
const level = `#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:6
#EXTINF:6
1.seg
#EXTINF:6
2.seg
#EXT-X-ENDLIST
`;
const details = M3U8Parser.parseLevelPlaylist(
level,
'http://domain/test.m3u8',
0,
PlaylistLevelType.MAIN,
0,
null,
);
mockMediaSource.duration = Infinity;
//update details
hls.trigger(Events.LEVEL_UPDATED, { details, level: 1 });
queueNames.forEach((name, i) => {
const track = getSourceBufferTrack(bufferController, name);
const buffer = track?.buffer;
expect(buffer).to.not.be.undefined;
if (!buffer) {
return;
}
const segmentData = new Uint8Array();
const frag = new Fragment(PlaylistLevelType.MAIN, '');
const chunkMeta = new ChunkMetadata(0, 0, 0, 0);
const data: BufferAppendingData = {
parent: PlaylistLevelType.MAIN,
type: name,
data: segmentData,
frag,
part: null,
chunkMeta,
};
setSourceBufferBufferedRange(bufferController, name, 0, 30);
hls.trigger(Events.BUFFER_APPENDING, data);
expect(setTimeoutSpy).to.have.callCount(i + 1);
// buffered is [0, 30], so we expect it to be 30000ms
expect(setTimeoutSpy).to.have.been.calledWith(sinon.match.func, 30000);
});
});
it('should calculate timeout based on provided configuration', function () {
hls.config.appendTimeout = 40000;
const level = `#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:6
#EXTINF:6
1.seg
#EXTINF:6
2.seg
#EXT-X-ENDLIST
`;
const details = M3U8Parser.parseLevelPlaylist(
level,
'http://domain/test.m3u8',
0,
PlaylistLevelType.MAIN,
0,
null,
);
mockMediaSource.duration = Infinity;
//update details
hls.trigger(Events.LEVEL_UPDATED, { details, level: 1 });
queueNames.forEach((name, i) => {
const track = getSourceBufferTrack(bufferController, name);
const buffer = track?.buffer;
expect(buffer).to.not.be.undefined;
if (!buffer) {
return;
}
const segmentData = new Uint8Array();
const frag = new Fragment(PlaylistLevelType.MAIN, '');
const chunkMeta = new ChunkMetadata(0, 0, 0, 0);
const data: BufferAppendingData = {
parent: PlaylistLevelType.MAIN,
type: name,
data: segmentData,
frag,
part: null,
chunkMeta,
};
setSourceBufferBufferedRange(bufferController, name, 0, 30);
hls.trigger(Events.BUFFER_APPENDING, data);
expect(setTimeoutSpy).to.have.callCount(i + 1);
// 2*level-target-duration is 12, buffered is [0, 30], but configured value is 40, so use 40
expect(setTimeoutSpy).to.have.been.calledWith(sinon.match.func, 40000);
});
});
it('should clear timeout when track is reset', function () {
hls.config.appendTimeout = 5000;
const segmentData = new Uint8Array([1, 2, 3, 4]);
const frag = new Fragment(PlaylistLevelType.MAIN, '');
const chunkMeta = new ChunkMetadata(0, 0, 0, 0);
const data: BufferAppendingData = {
parent: PlaylistLevelType.MAIN,
type: 'video',
data: segmentData,
frag,
part: null,
chunkMeta,
};
hls.trigger(Events.BUFFER_APPENDING, data);
const videoTrack = getSourceBufferTrack(bufferController, 'video');
const timeoutId = videoTrack?.bufferAppendTimeoutId;
expect(timeoutId).to.be.a('number');
// Reset buffer
hls.trigger(Events.BUFFER_RESET, undefined);
expect(clearTimeoutSpy).to.have.been.calledWith(timeoutId);
});
it('should cycle the SourceBuffer operation queue if the sourceBuffer does not exist while appending', function () {
const queueAppendSpy = sandbox.spy(operationQueue, 'append');
const frag = new Fragment(PlaylistLevelType.MAIN, '');
const chunkMeta = new ChunkMetadata(0, 0, 0, 0);
(bufferController as any).resetBuffer('audio');
(bufferController as any).resetBuffer('video');
queueNames.forEach((name, i) => {
hls.trigger(Events.BUFFER_APPENDING, {
parent: PlaylistLevelType.MAIN,
type: name,
data: new Uint8Array(),
frag,
part: null,
chunkMeta,
});
expect(
queueAppendSpy,
'The append operation should have been enqueued',
).to.have.callCount(i + 1);
expect(
shiftAndExecuteNextSpy,
'The queue should have been cycled',
).to.have.callCount(i + 1);
});
expect(triggerSpy).to.have.callCount(4);
const lastCall = triggerSpy.getCall(3);
expect(
triggerSpy,
'Buffer append error event should have been triggered',
).to.have.been.calledWith(Events.ERROR, {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.BUFFER_APPEND_ERROR,
sourceBufferName: lastCall.lastArg.sourceBufferName,
parent: 'main',
frag,
part: null,
chunkMeta,
error: lastCall.lastArg.error,
err: lastCall.lastArg.error,
fatal: false,
errorAction: { action: 0, flags: 0, resolved: true },
});
});
});
describe('onFragParsed', function () {
it('should trigger FRAG_BUFFERED when all audio/video data has been buffered', function () {
const frag = new Fragment(PlaylistLevelType.MAIN, '');
frag.setElementaryStreamInfo(ElementaryStreamTypes.AUDIO, 0, 0, 0, 0);
frag.setElementaryStreamInfo(ElementaryStreamTypes.VIDEO, 0, 0, 0, 0);
hls.trigger(Events.FRAG_PARSED, { frag, part: null });
return new Promise<void>((resolve, reject) => {
hls.on(Events.FRAG_BUFFERED, (event, data) => {
try {
expect(
data.frag,
'The frag emitted in FRAG_BUFFERED should be the frag passed in onFragParsed',
).to.equal(frag);
expect(
data.id,
'The id of the event should be equal to the frag type',
).to.equal(frag.type);
} catch (e) {
reject(e);
}
resolve();
});
});
});
});
describe('onBufferFlushing', function () {
let queueAppendSpy;
beforeEach(function () {
queueAppendSpy = sandbox.spy(operationQueue, 'append');
queueNames.forEach((name) => {
setSourceBufferBufferedRange(bufferController, name, 0, 10);
});
});
it('flushes audio and video buffers if no type arg is specified', function () {
hls.trigger(Events.BUFFER_FLUSHING, {
startOffset: 0,
endOffset: 10,
type: null,
});
expect(
queueAppendSpy,
'A remove operation should have been appended to each queue',
).to.have.been.calledTwice;
queueNames.forEach((name, i) => {
const buffer = getSourceBufferTrack(bufferController, name)?.buffer;
expect(buffer).to.not.be.undefined;
if (!buffer) {
return;
}
expect(
buffer.remove,
`Remove should have been called once on the ${name} SourceBuffer`,
).to.have.been.calledOnce;
expect(
buffer.remove,
'Remove should have been called with the expected range',
).to.have.been.calledWith(0, 10);
buffer.dispatchEvent(new Event('updateend'));
expect(
triggerSpy,
'The BUFFER_FLUSHED event should be called once per buffer',
).to.have.callCount(i + 2);
expect(triggerSpy).to.have.been.calledWith(Events.BUFFER_FLUSHING);
expect(triggerSpy).to.have.been.calledWith(Events.BUFFER_FLUSHED);
expect(
shiftAndExecuteNextSpy,
'The queue should have been cycled',
).to.have.callCount(i + 1);
});
});
it('Does not queue remove operations when there are no SourceBuffers', function () {
(bufferController as any).resetBuffer('audio');
(bufferController as any).resetBuffer('video');
hls.trigger(Events.BUFFER_FLUSHING, {
startOffset: 0,
endOffset: Infinity,
type: null,
});
expect(
queueAppendSpy,
'No remove operations should have been appended',
).to.have.callCount(0);
});
it('Only queues remove operations for existing SourceBuffers', function () {
(bufferController as any).tracks = {
audiovideo: {},
};
(bufferController as any).sourceBuffers = [
['audiovideo', {}],
[null, null],
];
hls.trigger(Events.BUFFER_FLUSHING, {
startOffset: 0,
endOffset: Infinity,
type: null,
});
expect(
queueAppendSpy,
'Queue one remove for muxed "audiovideo" SourceBuffer',
).to.have.been.calledOnce;
});
it('Errors and signals flushed with error when the requested remove range is not valid', function () {
hls.trigger(Events.BUFFER_FLUSHING, {
startOffset: 9001,
endOffset: 9000,
type: null,
});
expect(
queueAppendSpy,
'Two remove operations should have been appended',
).to.have.callCount(2);
expect(
shiftAndExecuteNextSpy,
'The queues should have been cycled',
).to.have.callCount(2);
queueNames.forEach((name) => {
const buffer = getSourceBufferTrack(bufferController, name)?.buffer;
expect(buffer).to.not.be.undefined;
if (!buffer) {
return;
}
expect(
buffer.remove,
`Remove should not have been called on the ${name} buffer`,
).to.have.not.been.called;
});
expect(triggerSpy).to.have.been.calledWith(Events.BUFFER_FLUSHING);
expect(
triggerSpy,
'Only Events.BUFFER_FLUSHING should have been triggered',
).to.have.been.calledThrice;
const err1 = triggerSpy.getCall(1).lastArg.error;
const err2 = triggerSpy.getCall(2).lastArg.error;
expect(triggerSpy).to.have.been.calledWith(Events.BUFFER_FLUSHED, {
start: 0,
end: 0,
type: 'video',
error: err1,
});
expect(err1.message).to.eq(
'Cannot remove invalid range (9001 >= 9000) from the video SourceBuffer',
);
expect(triggerSpy).to.have.been.calledWith(Events.BUFFER_FLUSHED, {
start: 0,
end: 0,
type: 'audio',
error: err2,
});
expect(err2.message).to.eq(
'Cannot remove invalid range (9001 >= 9000) from the audio SourceBuffer',
);
});
});
describe('trimBuffers', function () {
it('exits early if no media is defined', function () {
delete (bufferController as any).media;
evokeTrimBuffers(hls);
expect(triggerSpy).to.have.been.calledWith(Events.FRAG_CHANGED);
expect(triggerSpy).to.not.have.been.calledWith(
Events.BACK_BUFFER_REACHED,
);
expect(triggerSpy).to.not.have.been.calledWith(
Events.LIVE_BACK_BUFFER_REACHED,
);
expect(triggerSpy).to.not.have.been.calledWith(Events.BUFFER_FLUSHING);
});
it('does not remove if the buffer does not exist', function () {
queueNames.forEach((name) => {
setSourceBufferBufferedRange(bufferController, name, 0, 0);
});
evokeTrimBuffers(hls);
(bufferController as any).resetBuffer('audio');
(bufferController as any).resetBuffer('video');
evokeTrimBuffers(hls);
expect(triggerSpy).to.not.have.been.calledWith(Events.BUFFER_FLUSHING);
});
describe('flushBackBuffer', function () {
beforeEach(function () {
(bufferController as any).details = {
levelTargetDuration: 10,
};
hls.config.backBufferLength = 10;
queueNames.forEach((name) => {
setSourceBufferBufferedRange(bufferController, name, 0, 30);
});
mockMedia.currentTime = 30;
});
it('exits early if the backBufferLength config is not a finite number, or less than 0', function () {
(hls.config as any).backBufferLength = null;
evokeTrimBuffers(hls);
hls.config.backBufferLength = -1;
evokeTrimBuffers(hls);
hls.config.backBufferLength = Infinity;
evokeTrimBuffers(hls);
expect(triggerSpy).to.not.have.been.calledWith(Events.BUFFER_FLUSHING);
});
it('should execute a remove operation if backBufferLength is set to 0', function () {
hls.config.backBufferLength = 0;
evokeTrimBuffers(hls);
expect(triggerSpy.withArgs(Events.BUFFER_FLUSHING)).to.have.callCount(
2,
);
});
it('should execute a remove operation if flushing a valid backBuffer range', function () {
evokeTrimBuffers(hls);
expect(triggerSpy.withArgs(Events.BUFFER_FLUSHING)).to.have.callCount(
2,
);
queueNames.forEach((name) => {
expect(
triggerSpy,
`BUFFER_FLUSHING should have been triggered for the ${name} SourceBuffer`,
).to.have.been.calledWith(Events.BUFFER_FLUSHING, {
startOffset: 0,
endOffset: 20,
type: name,
});
});
});
it('should support the deprecated liveBackBufferLength for live content', function () {
(bufferController as any).details.live = true;
hls.config.backBufferLength = Infinity;
hls.config.liveBackBufferLength = 10;
evokeTrimBuffers(hls);
expect(
triggerSpy.withArgs(Events.LIVE_BACK_BUFFER_REACHED),
).to.have.callCount(2);
});
it('removes a maximum of one targetDuration from currentTime at intervals of targetDuration', function () {
mockMedia.currentTime = 25;
hls.config.backBufferLength = 5;
evokeTrimBuffers(hls);
queueNames.forEach((name) => {
expect(
triggerSpy,
`BUFFER_FLUSHING should have been triggered for the ${name} SourceBuffer`,
).to.have.been.calledWith(Events.BUFFER_FLUSHING, {
startOffset: 0,
endOffset: 10,
type: name,
});
});
});
it('removes nothing if no buffered range intersects with back buffer limit', function () {
mockMedia.currentTime = 15;
queueNames.forEach((name) => {
setSourceBufferBufferedRange(bufferController, name, 10, 30);
});
evokeTrimBuffers(hls);
expect(triggerSpy).to.not.have.been.calledWith(Events.BUFFER_FLUSHING);
});
});
describe('flushFrontBuffer', function () {
beforeEach(function () {
(bufferController as any).details = {
levelTargetDuration: 10,
};
hls.config.maxBufferLength = 60;
hls.config.frontBufferFlushThreshold = hls.config.maxBufferLength;
queueNames.forEach((name) => {
setSourceBufferBufferedRange(bufferController, name, 0, 100);
});
mockMedia.currentTime = 0;
});
it('exits early if the frontBufferFlushThreshold config is not a finite number, or less than 0', function () {
(hls.config as any).frontBufferFlushThreshold = null;
evokeTrimBuffers(hls);
hls.config.frontBufferFlushThreshold = -1;
evokeTrimBuffers(hls);
hls.config.frontBufferFlushThreshold = Infinity;
evokeTrimBuffers(hls);
expect(triggerSpy).to.not.have.been.calledWith(Events.BUFFER_FLUSHING);
});
it('should execute a remove operation if flushing a valid frontBuffer range', function () {
queueNames.forEach((name) => {
setSourceBufferBufferedRange(bufferController, name, 150, 200);
});
evokeTrimBuffers(hls);
expect(triggerSpy.withArgs(Events.BUFFER_FLUSHING)).to.have.callCount(
2,
);
queueNames.forEach((name) => {
expect(
triggerSpy,
`BUFFER_FLUSHING should have been triggered for the ${name} SourceBuffer`,
).to.have.been.calledWith(Events.BUFFER_FLUSHING, {
startOffset: 150,
endOffset: Infinity,
type: name,
});
});
});
it('should do nothing if the buffer is contiguous', function () {
evokeTrimBuffers(hls);
expect(triggerSpy).to.not.have.been.calledWith(Events.BUFFER_FLUSHING);
});
it('should use maxBufferLength if frontBufferFlushThreshold < maxBufferLength', function () {
queueNames.forEach((name) => {
setSourceBufferBufferedRange(bufferController, name, 150, 200);
});