forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment_index_unit.js
1079 lines (926 loc) · 37.9 KB
/
segment_index_unit.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
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
describe('SegmentIndex', /** @suppress {accessControls} */ () => {
const actual1 = makeReference(uri(0), 0, 10);
const actual2 = makeReference(uri(20), 10, 20);
const actual3 = makeReference(uri(20), 20, 30);
describe('find', () => {
it('finds the correct references', () => {
// One reference.
let index = new shaka.media.SegmentIndex([actual1]);
let pos1 = index.find(5);
expect(pos1).toBe(0);
// Two references.
index = new shaka.media.SegmentIndex([actual1, actual2]);
pos1 = index.find(5);
let pos2 = index.find(15);
expect(pos1).toBe(0);
expect(pos2).toBe(1);
// Three references.
index = new shaka.media.SegmentIndex([actual1, actual2, actual3]);
pos1 = index.find(5);
pos2 = index.find(15);
const pos3 = index.find(25);
expect(pos1).toBe(0);
expect(pos2).toBe(1);
expect(pos3).toBe(2);
});
it('works if time == first start time', () => {
const actual = makeReference(uri(10), 10, 20);
const index = new shaka.media.SegmentIndex([actual]);
const pos = index.find(10);
goog.asserts.assert(pos != null, 'Null position!');
const ref = index.get(pos);
expect(ref).toBe(actual);
});
it('works with two references if time == second start time', () => {
const actual1 = makeReference(uri(10), 10, 20);
const actual2 = makeReference(uri(20), 20, 30);
const index = new shaka.media.SegmentIndex([actual1, actual2]);
const pos = index.find(20);
goog.asserts.assert(pos != null, 'Null position!');
const ref = index.get(pos);
expect(ref).toBe(actual2);
});
it('works with two references if time == first end time', () => {
const actual1 = makeReference(uri(10), 10, 20.12);
const actual2 = makeReference(uri(20), 20.13, 30);
const index = new shaka.media.SegmentIndex([actual1, actual2]);
const pos = index.find(20.12);
goog.asserts.assert(pos != null, 'Null position!');
const ref = index.get(pos);
expect(ref).toBe(actual1);
});
it('works with time is between first endTime and second startTime', () => {
const actual1 = makeReference(uri(10), 10, 20.12111);
const actual2 = makeReference(uri(20), 20.12113, 30);
const index = new shaka.media.SegmentIndex([actual1, actual2]);
const pos = index.find(20.12112);
goog.asserts.assert(pos != null, 'Null position!');
const ref = index.get(pos);
expect(ref).toBe(actual1);
});
it('returns the first segment if time < first start time', () => {
const actual = makeReference(uri(10), 10, 20);
const index = new shaka.media.SegmentIndex([actual]);
const pos = index.find(5);
goog.asserts.assert(pos != null, 'Null position!');
const ref = index.get(pos);
expect(ref).toBe(actual);
});
it('returns null if time == last end time', () => {
const actual = makeReference(uri(10), 10, 20);
const index = new shaka.media.SegmentIndex([actual]);
const pos = index.find(20);
expect(pos).toBeNull();
});
it('returns null if time > last end time', () => {
const actual = makeReference(uri(10), 10, 20);
const index = new shaka.media.SegmentIndex([actual]);
const pos = index.find(21);
expect(pos).toBeNull();
});
});
describe('get', () => {
it('returns the correct references', () => {
// One reference.
let index = new shaka.media.SegmentIndex([actual1]);
let r1 = index.get(0);
expect(r1).toEqual(actual1);
// Two references.
index = new shaka.media.SegmentIndex([actual1, actual2]);
r1 = index.get(0);
let r2 = index.get(1);
expect(r1).toEqual(actual1);
expect(r2).toEqual(actual2);
// Three references.
index = new shaka.media.SegmentIndex([actual1, actual2, actual3]);
r1 = index.get(0);
r2 = index.get(1);
let r3 = index.get(2);
expect(r1).toEqual(actual1);
expect(r2).toEqual(actual2);
expect(r3).toEqual(actual3);
// Two references with offset.
index = new shaka.media.SegmentIndex([actual2, actual3]);
r2 = index.get(0);
r3 = index.get(1);
expect(r2).toEqual(actual2);
expect(r3).toEqual(actual3);
// One reference with offset.
index = new shaka.media.SegmentIndex([actual3]);
r3 = index.get(0);
expect(r3).toEqual(actual3);
});
it('returns null with zero references', () => {
const index = new shaka.media.SegmentIndex([]);
expect(index.get(0)).toBeNull();
});
it('returns null if position < 0', () => {
const index = new shaka.media.SegmentIndex([actual1, actual2, actual3]);
expect(index.get(-1)).toBeNull();
});
it('returns null for unknown positions', () => {
const index = new shaka.media.SegmentIndex([actual1, actual2, actual3]);
expect(index.get(3)).toBeNull();
expect(index.get(-1)).toBeNull();
});
});
describe('fit', () => {
it('clamps references to the window bounds', () => {
// These negative numbers can occur due to presentationTimeOffset in DASH.
const references = [
makeReference(uri(0), -10, -3),
makeReference(uri(1), -3, 4),
makeReference(uri(2), 4, 11),
makeReference(uri(3), 11, 18),
makeReference(uri(4), 18, 25),
];
const index = new shaka.media.SegmentIndex(references);
expect(index.references).toEqual(references);
// Get the position and reference of the segment at time 5.
const positionAtTimeFive = index.find(5);
goog.asserts.assert(positionAtTimeFive != null, 'Null position!');
const referenceAtTimeFive = index.get(positionAtTimeFive);
index.fit(/* windowStart= */ 0, /* windowEnd= */ 15);
const newReferences = [
/* ref 0 dropped because it ends before the period starts */
makeReference(uri(1), -3, 4),
makeReference(uri(2), 4, 11),
makeReference(uri(3), 11, 15), // end time clamped to period
/* ref 4 dropped because it starts after the period ends */
];
expect(index.references).toEqual(newReferences);
// The position used to represent this segment should not have changed.
expect(index.find(5)).toBe(positionAtTimeFive);
expect(index.get(positionAtTimeFive)).toBe(referenceAtTimeFive);
});
it('drops references which end exactly at window start', () => {
// The end time is meant to be exclusive, so segments ending at window
// start should be dropped.
const references = [
makeReference(uri(0), -10, 0),
makeReference(uri(1), 0, 10),
];
const index = new shaka.media.SegmentIndex(references);
expect(index.references).toEqual(references);
index.fit(/* windowStart= */ 0, /* windowEnd= */ 10);
const newReferences = [
/* ref 0 dropped because it ends when the window start (at 0) */
makeReference(uri(1), 0, 10),
];
expect(index.references).toEqual(newReferences);
});
it('preserves hls key of the last reference', () => {
// The hls key of the last segment should be preserved.
const references = [
makeReference(uri(0), 0, 5, [],
{method: 'AES-128', firstMediaSequenceNumber: 0}),
makeReference(uri(1), 5, 10, [],
{method: 'AES-128', firstMediaSequenceNumber: 0}),
makeReference(uri(2), 10, 15, [],
{method: 'AES-128', firstMediaSequenceNumber: 0}),
];
const index = new shaka.media.SegmentIndex(references);
expect(index.references).toEqual(references);
index.fit(/* windowStart= */ 0, /* windowEnd= */ 10);
expect(
index.references[index.references.length - 1].hlsAes128Key,
).toEqual({method: 'AES-128', firstMediaSequenceNumber: 0});
});
});
describe('merge', () => {
it('three references into zero references', () => {
const index1 = new shaka.media.SegmentIndex([]);
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references2 = [actual1, actual2, actual3];
index1.merge(references2);
expect(index1.references.length).toBe(3);
expect(index1.references).toEqual(references2);
});
it('zero references into three references', () => {
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references1 = [actual1, actual2, actual3];
const index1 = new shaka.media.SegmentIndex(references1);
index1.merge([]);
expect(index1.references.length).toBe(3);
expect(index1.references).toEqual(references1);
});
it('one reference into one reference at end', () => {
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references1 = [makeReference(uri(10), 10, 20)];
const index1 = new shaka.media.SegmentIndex(references1);
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references2 = [makeReference(uri(20), 20, 30)];
index1.merge(references2);
expect(index1.references.length).toBe(2);
expect(index1.references[0]).toEqual(references1[0]);
expect(index1.references[1]).toEqual(references2[0]);
});
it('one reference into two references at end', () => {
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references1 = [
makeReference(uri(10), 10, 20),
makeReference(uri(20), 20, 30),
];
const index1 = new shaka.media.SegmentIndex(references1);
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references2 = [makeReference(uri(30), 30, 40)];
index1.merge(references2);
expect(index1.references.length).toBe(3);
expect(index1.references[0]).toEqual(references1[0]);
expect(index1.references[1]).toEqual(references1[1]);
expect(index1.references[2]).toEqual(references2[0]);
});
it('two references into one reference at end', () => {
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references1 = [makeReference(uri(20), 20, 30)];
const index1 = new shaka.media.SegmentIndex(references1);
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references2 = [
makeReference(uri(30), 30, 40),
makeReference(uri(40), 40, 50),
];
index1.merge(references2);
expect(index1.references.length).toBe(3);
expect(index1.references[0]).toEqual(references1[0]);
expect(index1.references[1]).toEqual(references2[0]);
expect(index1.references[2]).toEqual(references2[1]);
});
it('last live stream reference when period change', () => {
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references1 = [
makeReference(uri(10), 10, 20),
makeReference(uri(20), 20, 30),
makeReference(uri(30), 30, 49.887),
];
const index1 = new shaka.media.SegmentIndex(references1);
// When the period is changed, fit() will expand last segment to the start
// of the next the period. This simulates an update in which fit() has
// done that.
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references2 = [
makeReference(uri(20), 20, 30),
makeReference(uri(30), 30, 50),
];
index1.merge(references2);
expect(index1.references.length).toBe(3);
expect(index1.references[0]).toEqual(references1[0]);
expect(index1.references[1]).toEqual(references2[0]);
expect(index1.references[2]).toEqual(references2[1]);
});
it('references with partial segments of the same parent segment', () => {
// refs1: [[[0,5][5,10]]]
// refs2: [[[0,5][5,10][10,15]]]
// Merged: [[[0,5][5,10][10,15]]]
/** @type {!Array.<!shaka.media.SegmentReference>} */
const partialRefs1 = [
makeReference(uri(0.5), 0, 5),
makeReference(uri(5.10), 5, 10),
];
/** @type {!Array.<!shaka.media.SegmentReference>} */
const partialRefs2 = [
makeReference(uri(0.5), 0, 5),
makeReference(uri(5.10), 5, 10),
makeReference(uri(10.15), 10, 15),
];
/** @type {!Array.<!shaka.media.SegmentReference>} */
const refs1 = [
makeReference(uri(0.10), 0, 10, partialRefs1),
];
/** @type {!Array.<!shaka.media.SegmentReference>} */
const refs2 = [
makeReference(uri(0.15), 0, 15, partialRefs2),
];
const index1 = new shaka.media.SegmentIndex(refs1);
index1.merge(refs2);
expect(index1.references).toEqual(refs2);
});
it('references with partial segments', () => {
// refs1: [[0,10], [[10,15][15,20]]]
// refs2: [ [[10,15][15,20][20,25]]]
// Merged:[[0,10], [[10,15][15,20][20,25]]]
/** @type {!Array.<!shaka.media.SegmentReference>} */
const partialRefs1 = [
makeReference(uri(10.15), 10, 15),
makeReference(uri(15.20), 15, 20),
];
/** @type {!Array.<!shaka.media.SegmentReference>} */
const partialRefs2 = [
makeReference(uri(10.15), 10, 15),
makeReference(uri(15.20), 15, 20),
makeReference(uri(20.25), 20, 25),
];
/** @type {!Array.<!shaka.media.SegmentReference>} */
const refs1 = [
makeReference(uri(0.10), 0, 10),
makeReference(uri(10.20), 10, 20, partialRefs1),
];
/** @type {!Array.<!shaka.media.SegmentReference>} */
const refs2 = [
makeReference(uri(10.25), 10, 25, partialRefs2),
];
const index1 = new shaka.media.SegmentIndex(refs1);
index1.merge(refs2);
const expectedRefs = [refs1[0]].concat(refs2);
expect(index1.references).toEqual(expectedRefs);
});
it('references and remove old partial segments ', () => {
// refs1: [[0,10], [[10,15][15,20]]]]
// refs2: [ [10,20], [[20,25][25,30]]]
// Merged:[[0,10], [10,20], [[20,25][25,30]]]
/** @type {!Array.<!shaka.media.SegmentReference>} */
const partialRefs1 = [
makeReference(uri(10.15), 10, 15),
makeReference(uri(15.20), 15, 20),
];
/** @type {!Array.<!shaka.media.SegmentReference>} */
const partialRefs2 = [
makeReference(uri(20.25), 20, 25),
makeReference(uri(25.30), 25, 30),
];
/** @type {!Array.<!shaka.media.SegmentReference>} */
const refs1 = [
makeReference(uri(0.10), 0, 10),
makeReference(uri(10.20), 10, 20, partialRefs1),
];
/** @type {!Array.<!shaka.media.SegmentReference>} */
const refs2 = [
makeReference(uri(10.20), 10, 20),
makeReference(uri(20.30), 20, 30, partialRefs2),
];
const index1 = new shaka.media.SegmentIndex(refs1);
index1.merge(refs2);
// Expect the first SegmentReference's partial SegmentReferences removed.
const expectedRefs = [refs1[0]].concat(refs2);
expect(index1.references).toEqual(expectedRefs);
});
it('references with only preload hinted segments', () => {
// refs1: [[(preload-hint)[0,0]]]
// refs2: [[[0,5][5,10]]]
// Merged: [[[0,5][5,10]]]
/** @type {!Array.<!shaka.media.SegmentReference>} */
// A segment with only preload hinted partial segment.
const preloadRefs1 = [
makeReference(uri(0.0), 0, 0),
];
/** @type {!Array.<!shaka.media.SegmentReference>} */
const partialRefs2 = [
// Previous preload hinted segment is replaced with a partial segment.
makeReference(uri(0.5), 0, 5),
makeReference(uri(5.10), 5, 10),
];
/** @type {!Array.<!shaka.media.SegmentReference>} */
const refs1 = [
makeReference(uri(0.0), 0, 0, preloadRefs1),
];
/** @type {!Array.<!shaka.media.SegmentReference>} */
const refs2 = [
makeReference(uri(0.10), 0, 10, partialRefs2),
];
const index1 = new shaka.media.SegmentIndex(refs1);
index1.merge(refs2);
expect(index1.references.length).toBe(1);
expect(index1.references).toEqual(refs2);
});
it('references with preload hinted segments', () => {
// refs1: [[[0,5],(preload-hint)[5,5]]]
// refs2: [[[0,5][5,10][10,15]]]
// Merged: [[[0,5][5,10][10,15]]]
/** @type {!Array.<!shaka.media.SegmentReference>} */
const partialRefs1 = [
makeReference(uri(0.5), 0, 5),
// Preload hinted partial segment
makeReference(uri(5.5), 5, 5),
];
/** @type {!Array.<!shaka.media.SegmentReference>} */
const partialRefs2 = [
makeReference(uri(0.5), 0, 5),
makeReference(uri(5.10), 5, 10),
makeReference(uri(10.15), 10, 15),
];
/** @type {!Array.<!shaka.media.SegmentReference>} */
const refs1 = [
makeReference(uri(0.5), 0, 5, partialRefs1),
];
/** @type {!Array.<!shaka.media.SegmentReference>} */
const refs2 = [
makeReference(uri(0.15), 0, 15, partialRefs2),
];
const index1 = new shaka.media.SegmentIndex(refs1);
index1.merge(refs2);
expect(index1.references.length).toBe(1);
expect(index1.references).toEqual(refs2);
});
});
describe('mergeAndEvict', () => {
it('discards segments that end before the availabilityWindowStart', () => {
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references1 = [
// Assuming ref(0, 10) has been already evicted
makeReference(uri(10), 10, 20),
];
const index1 = new shaka.media.SegmentIndex(references1);
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references2 = [
makeReference(uri(0), 0, 10),
makeReference(uri(10), 10, 20),
makeReference(uri(20), 20, 30),
];
// The first two references end before the availabilityWindowStart, so
// they should be discarded.
index1.mergeAndEvict(references2, 21);
expect(index1.references.length).toBe(1);
expect(index1.references[0]).toEqual(references2[2]);
});
it('discards segments that end before the first old segment', () => {
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references1 = [
// Assuming ref(0, 10) has been already evicted
makeReference(uri(10), 10, 20),
];
const index1 = new shaka.media.SegmentIndex(references1);
const position1 = index1.find(10);
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references2 = [
makeReference(uri(0), 0, 10),
makeReference(uri(10), 10, 20),
makeReference(uri(20), 20, 30),
];
// The new first reference ends before the first old reference starts, so
// it should be discarded. We will never grow the list at the beginning.
index1.mergeAndEvict(references2, 0);
expect(index1.references.length).toBe(2);
expect(index1.references[0]).toEqual(references2[1]);
expect(index1.references[1]).toEqual(references2[2]);
// The positions should be the same, as well.
expect(index1.find(10)).toBe(position1);
goog.asserts.assert(position1 != null, 'Position should not be null!');
expect(index1.get(position1)).toBe(references2[1]);
});
it('does not duplicate references with rounding errors', () => {
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references1 = [
makeReference(uri(10), 10, 20),
makeReference(uri(20), 20, 30),
];
const index1 = new shaka.media.SegmentIndex(references1);
// 0.24 microseconds: an insignificant rounding error.
const tinyError = 0.24e-6;
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references2 = [
makeReference(uri(10), 10, 20),
// The difference between this and the equivalent old reference is an
// insignificant rounding error.
makeReference(uri(20), 20 + tinyError, 30 + tinyError),
makeReference(uri(30), 30 + tinyError, 40),
];
index1.merge(references2);
expect(index1.references.length).toBe(3);
expect(index1.references[0]).toEqual(references1[0]);
// The new references replaced the old one.
expect(index1.references[1]).toEqual(references2[1]);
expect(index1.references[2]).toEqual(references2[2]);
});
});
describe('evict', () => {
/** @type {!shaka.media.SegmentIndex} */
let index1;
beforeEach(() => {
index1 = new shaka.media.SegmentIndex([actual1, actual2, actual3]);
});
it('no segments', () => {
index1.evict(5);
expect(index1.references.length).toBe(3);
});
it('one segment (edge)', () => {
index1.evict(10);
expect(index1.references.length).toBe(2);
expect(index1.references[0]).toEqual(actual2);
expect(index1.references[1]).toEqual(actual3);
});
it('one segment', () => {
index1.evict(11);
expect(index1.references.length).toBe(2);
expect(index1.references[0]).toEqual(actual2);
expect(index1.references[1]).toEqual(actual3);
});
it('two segments (edge)', () => {
index1.evict(20);
expect(index1.references.length).toBe(1);
expect(index1.references[0]).toEqual(actual3);
});
it('two segments', () => {
index1.evict(21);
expect(index1.references.length).toBe(1);
expect(index1.references[0]).toEqual(actual3);
});
it('three segments (edge)', () => {
index1.evict(30);
expect(index1.references.length).toBe(0);
});
it('three segments', () => {
index1.evict(31);
expect(index1.references.length).toBe(0);
});
it('does not change positions', () => {
// Get the position and reference of the segment at time 15.
const positionAtTimeFifteen = index1.find(15);
goog.asserts.assert(positionAtTimeFifteen != null, 'Null position!');
const referenceAtTimeFifteen = index1.get(positionAtTimeFifteen);
index1.evict(10);
// The position should not have changed.
expect(index1.find(15)).toBe(positionAtTimeFifteen);
expect(index1.get(positionAtTimeFifteen)).toBe(referenceAtTimeFifteen);
});
});
describe('SegmentIterator', () => {
const inputRefs = [
makeReference(uri(0.10), 0, 10),
makeReference(uri(10.20), 10, 20),
makeReference(uri(20.30), 20, 30),
];
const additionalRefs = [
makeReference(uri(30.40), 30, 40),
makeReference(uri(40.50), 40, 50),
makeReference(uri(50.60), 50, 60),
];
const partialRefs1 = [
makeReference(uri(10.15), 10, 15),
makeReference(uri(15.20), 15, 20),
];
const partialRefs2 = [
makeReference(uri(20.25), 20, 25),
makeReference(uri(25.30), 25, 30),
];
const inputRefsWithPartial = [
makeReference(uri(0.10), 0, 10),
makeReference(uri(10.20), 10, 20, partialRefs1),
makeReference(uri(20.30), 20, 30, partialRefs2),
];
it('works with Array.from', () => {
const index = new shaka.media.SegmentIndex(inputRefs);
const refs = Array.from(index);
expect(refs).toEqual(inputRefs);
});
it('works with Array.from with partial segments', () => {
const index = new shaka.media.SegmentIndex(inputRefsWithPartial);
const refs = Array.from(index);
const expectedRefs = [inputRefsWithPartial[0]].concat(
partialRefs1, partialRefs2);
expect(refs).toEqual(expectedRefs);
});
it('works with for-of', () => {
const index = new shaka.media.SegmentIndex(inputRefs);
const refs = [];
for (const ref of index) {
refs.push(ref);
}
expect(refs).toEqual(inputRefs);
});
it('works with for-of with partial segments', () => {
const index = new shaka.media.SegmentIndex(inputRefsWithPartial);
const refs = [];
for (const ref of index) {
refs.push(ref);
}
const expectedRefs = [inputRefsWithPartial[0]].concat(
partialRefs1, partialRefs2);
expect(refs).toEqual(expectedRefs);
});
it('works after eviction', () => {
const index = new shaka.media.SegmentIndex(inputRefs);
index.evict(15); // Drop the first ref.
expect(Array.from(index)).toEqual(inputRefs.slice(1));
});
it('resumes iteration after new references are added', () => {
const refs = inputRefs.slice();
const index = new shaka.media.SegmentIndex(refs);
// This simulates the pattern of calls in StreamingEngine when we buffer
// to the edge of a live stream.
const iterator = index[Symbol.iterator]();
iterator.next();
expect(iterator.current()).toBe(inputRefs[0]);
iterator.next();
expect(iterator.current()).toBe(inputRefs[1]);
iterator.next();
expect(iterator.current()).toBe(inputRefs[2]);
iterator.next();
expect(iterator.current()).toBe(null);
// After we reach the end of the iteration, we should still get null back
// on subsequent requests.
expect(iterator.current()).toBe(null);
expect(iterator.current()).toBe(null);
// Although we have reached the end, we should still be able to add new
// references and iterate to them without starting over the iteration.
refs.push(...additionalRefs);
expect(iterator.current()).toBe(additionalRefs[0]);
iterator.next();
expect(iterator.current()).toBe(additionalRefs[1]);
iterator.next();
expect(iterator.current()).toBe(additionalRefs[2]);
iterator.next();
expect(iterator.current()).toBe(null);
});
describe('getIteratorForTime', () => {
it('begins with an independent partial segment', () => {
// This test contains its own segment refs, which are manipulated to
// look a little different from the more general ones used elsewhere.
// In particular, we mark some partial refs as dependent, and we make
// the second partial list much longer.
const partialRefs1 = [
makeReference(uri(10.15), 10, 15),
makeReference(uri(15.20), 15, 20),
];
partialRefs1[1].markAsNonIndependent();
const partialRefs2 = [
makeReference(uri(20.25), 20, 25),
makeReference(uri(25.30), 25, 30),
makeReference(uri(30.35), 30, 35),
makeReference(uri(35.40), 35, 40),
makeReference(uri(40.45), 40, 45),
makeReference(uri(45.50), 45, 50),
makeReference(uri(50.55), 50, 55),
makeReference(uri(55.60), 55, 60),
makeReference(uri(60.65), 60, 65),
makeReference(uri(65.70), 65, 70),
];
// All but the first:
for (const r of partialRefs2.slice(1)) {
r.markAsNonIndependent();
}
const localInputRefs = [
makeReference(uri(0.10), 0, 10),
makeReference(uri(10.20), 10, 20, partialRefs1),
makeReference(uri(20.70), 20, 70, partialRefs2),
];
const index = new shaka.media.SegmentIndex(localInputRefs);
// This time points to partialRefs1[0], which is independent.
const iterator1 = index.getIteratorForTime(11);
expect(iterator1.next().value).toBe(partialRefs1[0]);
// Even though the time would point to partialRefs1[1], that is not
// independent. So it walks back to partialRefs1[0].
const iterator2 = index.getIteratorForTime(16);
expect(iterator2.next().value).toBe(partialRefs1[0]);
// Even though the time would point to partialRefs2[9], that is not
// independent. So it walks all the way back to partialRefs2[0].
const iterator3 = index.getIteratorForTime(69);
expect(iterator3.next().value).toBe(partialRefs2[0]);
});
});
describe('next', () => {
it('starts with the first segment', () => {
const index = new shaka.media.SegmentIndex(inputRefs);
const iterator = index[Symbol.iterator]();
expect(iterator.next().value).toBe(inputRefs[0]);
});
it('iterates through all segments', () => {
const index = new shaka.media.SegmentIndex(inputRefs);
const iterator = index[Symbol.iterator]();
expect(iterator.next().value).toBe(inputRefs[0]);
expect(iterator.next().value).toBe(inputRefs[1]);
expect(iterator.next().value).toBe(inputRefs[2]);
expect(iterator.next().value).toBe(null);
});
it('iterates from getIteratorForTime', () => {
const index = new shaka.media.SegmentIndex(inputRefs);
const iterator = index.getIteratorForTime(10);
expect(iterator.next().value).toBe(inputRefs[1]);
expect(iterator.current()).toBe(inputRefs[1]);
expect(iterator.next().value).toBe(inputRefs[2]);
expect(iterator.current()).toBe(inputRefs[2]);
expect(iterator.next().value).toBe(null);
});
it('iterates from getIteratorForTime with partial segments', () => {
const index = new shaka.media.SegmentIndex(inputRefsWithPartial);
const iterator = index.getIteratorForTime(10);
expect(iterator.next().value).toBe(partialRefs1[0]);
expect(iterator.current()).toBe(partialRefs1[0]);
expect(iterator.next().value).toBe(partialRefs1[1]);
expect(iterator.current()).toBe(partialRefs1[1]);
expect(iterator.next().value).toBe(partialRefs2[0]);
expect(iterator.current()).toBe(partialRefs2[0]);
});
it('iterates through regular and partial segments', () => {
const index = new shaka.media.SegmentIndex(inputRefsWithPartial);
const iterator = index[Symbol.iterator]();
expect(iterator.next().value).toBe(inputRefsWithPartial[0]);
expect(iterator.next().value).toBe(partialRefs1[0]);
expect(iterator.next().value).toBe(partialRefs1[1]);
expect(iterator.next().value).toBe(partialRefs2[0]);
expect(iterator.next().value).toBe(partialRefs2[1]);
expect(iterator.next().value).toBe(null);
});
});
describe('current', () => {
it('starts with null', () => {
const index = new shaka.media.SegmentIndex(inputRefs);
const iterator = index[Symbol.iterator]();
// Technically, next() starts iteration, so current() returns null
// before next() is called.
expect(iterator.current()).toBe(null);
});
it('returns the same thing returned by the previous next() call', () => {
const index = new shaka.media.SegmentIndex(inputRefs);
const iterator = index[Symbol.iterator]();
expect(iterator.next().value).toBe(inputRefs[0]);
expect(iterator.current()).toBe(inputRefs[0]);
expect(iterator.next().value).toBe(inputRefs[1]);
expect(iterator.current()).toBe(inputRefs[1]);
expect(iterator.next().value).toBe(inputRefs[2]);
expect(iterator.current()).toBe(inputRefs[2]);
});
it('returns the same thing returned by the previous next() call ' +
'with partial segments', () => {
const index = new shaka.media.SegmentIndex(inputRefsWithPartial);
// const index = new shaka.media.SegmentIndex(inputRefs);
const iterator = index[Symbol.iterator]();
expect(iterator.next().value).toBe(inputRefsWithPartial[0]);
expect(iterator.current()).toBe(inputRefsWithPartial[0]);
expect(iterator.next().value).toBe(partialRefs1[0]);
expect(iterator.current()).toBe(partialRefs1[0]);
expect(iterator.next().value).toBe(partialRefs1[1]);
expect(iterator.current()).toBe(partialRefs1[1]);
expect(iterator.next().value).toBe(partialRefs2[0]);
expect(iterator.current()).toBe(partialRefs2[0]);
});
});
});
describe('MetaSegmentIndex', () => {
const inputRefs0 = [
makeReference(uri(0), 0, 10),
makeReference(uri(1), 10, 20),
makeReference(uri(2), 20, 30),
];
const inputRefs1 = [
makeReference(uri(3), 30, 40),
makeReference(uri(4), 40, 50),
makeReference(uri(5), 50, 60),
];
const inputRefs2 = [
makeReference(uri(6), 60, 70),
makeReference(uri(7), 70, 80),
makeReference(uri(8), 80, 90),
];
/** @type {!shaka.media.SegmentIndex} */
let index0;
/** @type {!shaka.media.SegmentIndex} */
let index1;
/** @type {!shaka.media.SegmentIndex} */
let index2;
/** @type {!shaka.media.MetaSegmentIndex} */
let metaIndex;
beforeEach(() => {
// Make sure each index has a _copy_ of the input refs, so that the
// original arrays are never modified.
index0 = new shaka.media.SegmentIndex(inputRefs0.slice());
index1 = new shaka.media.SegmentIndex(inputRefs1.slice());
index2 = new shaka.media.SegmentIndex(inputRefs2.slice());
metaIndex = new shaka.media.MetaSegmentIndex();
});
it('combines the contents of several SegmentIndexes', () => {
metaIndex.appendSegmentIndex(index0);
expect(metaIndex.find(0)).toBe(0);
expect(metaIndex.find(10)).toBe(1);
expect(metaIndex.find(20)).toBe(2);
expect(metaIndex.find(30)).toBe(null);
expect(Array.from(metaIndex)).toEqual(inputRefs0);
metaIndex.appendSegmentIndex(index1);
expect(metaIndex.find(0)).toBe(0);
expect(metaIndex.find(10)).toBe(1);
expect(metaIndex.find(20)).toBe(2);
expect(metaIndex.find(30)).toBe(3);
expect(metaIndex.find(40)).toBe(4);
expect(metaIndex.find(50)).toBe(5);
expect(metaIndex.find(60)).toBe(null);
expect(Array.from(metaIndex)).toEqual(inputRefs0.concat(inputRefs1));
metaIndex.appendSegmentIndex(index2);
expect(metaIndex.find(0)).toBe(0);
expect(metaIndex.find(10)).toBe(1);
expect(metaIndex.find(20)).toBe(2);
expect(metaIndex.find(30)).toBe(3);
expect(metaIndex.find(40)).toBe(4);
expect(metaIndex.find(50)).toBe(5);
expect(metaIndex.find(60)).toBe(6);
expect(metaIndex.find(70)).toBe(7);
expect(metaIndex.find(80)).toBe(8);
expect(metaIndex.find(90)).toBe(null);
expect(Array.from(metaIndex)).toEqual(
inputRefs0.concat(inputRefs1, inputRefs2));
});
it('updates through merge', () => {
metaIndex.appendSegmentIndex(index0);
metaIndex.appendSegmentIndex(index1);
expect(Array.from(metaIndex)).toEqual(inputRefs0.concat(inputRefs1));
index1.merge(inputRefs2);
expect(Array.from(metaIndex)).toEqual(
inputRefs0.concat(inputRefs1, inputRefs2));
});
it('tracks evictions with stable positions', () => {
metaIndex.appendSegmentIndex(index0);
metaIndex.appendSegmentIndex(index1);
metaIndex.appendSegmentIndex(index2);
const allRefs = inputRefs0.concat(inputRefs1, inputRefs2);
expect(metaIndex.find(0)).toBe(0);
expect(metaIndex.find(10)).toBe(1);
expect(metaIndex.find(20)).toBe(2);
expect(metaIndex.find(30)).toBe(3);
expect(metaIndex.find(40)).toBe(4);
expect(metaIndex.find(50)).toBe(5);
expect(metaIndex.find(60)).toBe(6);
expect(metaIndex.find(70)).toBe(7);
expect(metaIndex.find(80)).toBe(8);
expect(metaIndex.find(90)).toBe(null);
expect(Array.from(metaIndex)).toEqual(allRefs);
index0.evict(25);
index1.evict(25);
index2.evict(25);
expect(metaIndex.find(20)).toBe(2);
expect(metaIndex.find(30)).toBe(3);
expect(metaIndex.find(40)).toBe(4);
expect(metaIndex.find(50)).toBe(5);
expect(metaIndex.find(60)).toBe(6);
expect(metaIndex.find(70)).toBe(7);
expect(metaIndex.find(80)).toBe(8);
expect(metaIndex.find(90)).toBe(null);
expect(Array.from(metaIndex)).toEqual(allRefs.slice(2));
index0.evict(45);
index1.evict(45);
index2.evict(45);
expect(metaIndex.find(40)).toBe(4);
expect(metaIndex.find(50)).toBe(5);
expect(metaIndex.find(60)).toBe(6);