forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsegment_template.js
More file actions
1199 lines (1061 loc) · 40.5 KB
/
segment_template.js
File metadata and controls
1199 lines (1061 loc) · 40.5 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
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.dash.SegmentTemplate');
goog.require('goog.asserts');
goog.require('shaka.dash.MpdUtils');
goog.require('shaka.dash.SegmentBase');
goog.require('shaka.log');
goog.require('shaka.media.InitSegmentReference');
goog.require('shaka.media.SegmentIndex');
goog.require('shaka.media.SegmentReference');
goog.require('shaka.util.Error');
goog.require('shaka.util.IReleasable');
goog.require('shaka.util.ManifestParserUtils');
goog.require('shaka.util.ObjectUtils');
goog.require('shaka.util.StringUtils');
goog.require('shaka.util.TXml');
goog.requireType('shaka.dash.DashParser');
goog.requireType('shaka.media.PresentationTimeline');
/**
* @summary A set of functions for parsing SegmentTemplate elements.
*/
shaka.dash.SegmentTemplate = class {
/**
* Creates a new StreamInfo object.
* Updates the existing SegmentIndex, if any.
*
* @param {shaka.dash.DashParser.Context} context
* @param {shaka.dash.DashParser.RequestSegmentCallback} requestSegment
* @param {!Object<string, !shaka.extern.Stream>} streamMap
* @param {boolean} isUpdate True if the manifest is being updated.
* @param {number} segmentLimit The maximum number of segments to generate for
* a SegmentTemplate with fixed duration.
* @param {!Object<string, number>} periodDurationMap
* @param {shaka.extern.aesKey|undefined} aesKey
* @param {?number} lastSegmentNumber
* @param {boolean} isPatchUpdate
* @return {shaka.dash.DashParser.StreamInfo}
*/
static createStreamInfo(
context, requestSegment, streamMap, isUpdate, segmentLimit,
periodDurationMap, aesKey, lastSegmentNumber, isPatchUpdate) {
goog.asserts.assert(context.representation.segmentTemplate,
'Should only be called with SegmentTemplate ' +
'or segment info defined');
const MpdUtils = shaka.dash.MpdUtils;
const SegmentTemplate = shaka.dash.SegmentTemplate;
const TimelineSegmentIndex = shaka.dash.TimelineSegmentIndex;
if (!isPatchUpdate && !context.representation.initialization) {
context.representation.initialization =
MpdUtils.inheritAttribute(
context, SegmentTemplate.fromInheritance_, 'initialization');
}
const initSegmentReference = context.representation.initialization ?
SegmentTemplate.createInitSegment_(context, aesKey) : null;
/** @type {shaka.dash.SegmentTemplate.SegmentTemplateInfo} */
const info = SegmentTemplate.parseSegmentTemplateInfo_(context);
SegmentTemplate.checkSegmentTemplateInfo_(context, info);
// Direct fields of context will be reassigned by the parser before
// generateSegmentIndex is called. So we must make a shallow copy first,
// and use that in the generateSegmentIndex callbacks.
const shallowCopyOfContext =
shaka.util.ObjectUtils.shallowCloneObject(context);
if (info.indexTemplate) {
shaka.dash.SegmentBase.checkSegmentIndexSupport(
context, initSegmentReference);
return {
generateSegmentIndex: () => {
return SegmentTemplate.generateSegmentIndexFromIndexTemplate_(
shallowCopyOfContext, requestSegment, initSegmentReference,
info);
},
};
} else if (info.segmentDuration) {
if (!isUpdate &&
context.adaptationSet.contentType !== 'image' &&
context.adaptationSet.contentType !== 'text') {
const periodStart = context.periodInfo.start;
const periodId = context.period.id;
const initialPeriodDuration = context.periodInfo.duration;
const periodDuration =
(periodId != null && periodDurationMap[periodId]) ||
initialPeriodDuration;
const periodEnd = periodDuration ?
(periodStart + periodDuration) : Infinity;
context.presentationTimeline.notifyMaxSegmentDuration(
info.segmentDuration);
context.presentationTimeline.notifyPeriodDuration(
periodStart, periodEnd);
}
return {
generateSegmentIndex: () => {
return SegmentTemplate.generateSegmentIndexFromDuration_(
shallowCopyOfContext, info, segmentLimit, initSegmentReference,
periodDurationMap, aesKey, lastSegmentNumber,
context.representation.segmentSequenceCadence);
},
};
} else {
/** @type {shaka.media.SegmentIndex} */
let segmentIndex = null;
let id = null;
let stream = null;
if (context.period.id && context.representation.id) {
// Only check/store the index if period and representation IDs are set.
id = context.period.id + ',' + context.representation.id;
stream = streamMap[id];
if (stream) {
segmentIndex = stream.segmentIndex;
}
}
const periodStart = context.periodInfo.start;
const periodEnd = context.periodInfo.duration ? periodStart +
context.periodInfo.duration : Infinity;
shaka.log.debug(`New manifest ${periodStart} - ${periodEnd}`);
if (!segmentIndex) {
shaka.log.debug(`Creating TSI with end ${periodEnd}`);
segmentIndex = new TimelineSegmentIndex(
info,
context.representation.id,
context.bandwidth,
context.representation.getBaseUris,
context.urlParams,
periodStart,
periodEnd,
initSegmentReference,
aesKey,
context.representation.segmentSequenceCadence,
);
} else {
const tsi = /** @type {!TimelineSegmentIndex} */(segmentIndex);
tsi.appendTemplateInfo(
info, periodStart, periodEnd, initSegmentReference);
const availabilityStart =
context.presentationTimeline.getSegmentAvailabilityStart();
tsi.evict(availabilityStart);
}
if (info.timeline &&
context.adaptationSet.contentType !== 'image' &&
context.adaptationSet.contentType !== 'text') {
const timeline = info.timeline;
context.presentationTimeline.notifyTimeRange(
timeline,
periodStart);
}
if (stream && context.dynamic) {
stream.segmentIndex = segmentIndex;
}
return {
generateSegmentIndex: () => {
// If segmentIndex is deleted, or segmentIndex's references are
// released by closeSegmentIndex(), we should set the value of
// segmentIndex again.
if (segmentIndex instanceof shaka.dash.TimelineSegmentIndex &&
segmentIndex.isEmpty()) {
segmentIndex.appendTemplateInfo(info, periodStart,
periodEnd, initSegmentReference);
}
return Promise.resolve(segmentIndex);
},
};
}
}
/**
* Ingests Patch MPD segments into timeline.
*
* @param {!shaka.dash.DashParser.Context} context
* @param {shaka.extern.xml.Node} patchNode
*/
static modifyTimepoints(context, patchNode) {
const MpdUtils = shaka.dash.MpdUtils;
const SegmentTemplate = shaka.dash.SegmentTemplate;
const TXml = shaka.util.TXml;
const timelineNode = MpdUtils.inheritChild(context,
SegmentTemplate.fromInheritance_, 'SegmentTimeline');
goog.asserts.assert(timelineNode, 'timeline node not found');
const timepoints = TXml.findChildren(timelineNode, 'S');
goog.asserts.assert(timepoints, 'timepoints should exist');
TXml.modifyNodes(timepoints, patchNode);
timelineNode.children = timepoints;
}
/**
* Removes all segments from timeline.
*
* @param {!shaka.dash.DashParser.Context} context
*/
static removeTimepoints(context) {
const MpdUtils = shaka.dash.MpdUtils;
const SegmentTemplate = shaka.dash.SegmentTemplate;
const timelineNode = MpdUtils.inheritChild(context,
SegmentTemplate.fromInheritance_, 'SegmentTimeline');
goog.asserts.assert(timelineNode, 'timeline node not found');
timelineNode.children = [];
}
/**
* @param {?shaka.dash.DashParser.InheritanceFrame} frame
* @return {?shaka.extern.xml.Node}
* @private
*/
static fromInheritance_(frame) {
return frame.segmentTemplate;
}
/**
* Parses a SegmentTemplate element into an info object.
*
* @param {shaka.dash.DashParser.Context} context
* @return {shaka.dash.SegmentTemplate.SegmentTemplateInfo}
* @private
*/
static parseSegmentTemplateInfo_(context) {
const SegmentTemplate = shaka.dash.SegmentTemplate;
const MpdUtils = shaka.dash.MpdUtils;
const StringUtils = shaka.util.StringUtils;
const segmentInfo =
MpdUtils.parseSegmentInfo(context, SegmentTemplate.fromInheritance_);
const media = MpdUtils.inheritAttribute(
context, SegmentTemplate.fromInheritance_, 'media');
const index = MpdUtils.inheritAttribute(
context, SegmentTemplate.fromInheritance_, 'index');
const k = MpdUtils.inheritAttribute(
context, SegmentTemplate.fromInheritance_, 'k');
let numChunks = 0;
if (k) {
numChunks = parseInt(k, 10);
}
return {
unscaledSegmentDuration: segmentInfo.unscaledSegmentDuration,
segmentDuration: segmentInfo.segmentDuration,
timescale: segmentInfo.timescale,
startNumber: segmentInfo.startNumber,
scaledPresentationTimeOffset: segmentInfo.scaledPresentationTimeOffset,
unscaledPresentationTimeOffset:
segmentInfo.unscaledPresentationTimeOffset,
timeline: segmentInfo.timeline,
mediaTemplate: media && StringUtils.htmlUnescape(media),
indexTemplate: index,
mimeType: context.representation.mimeType,
codecs: context.representation.codecs,
bandwidth: context.bandwidth,
numChunks: numChunks,
};
}
/**
* Verifies a SegmentTemplate info object.
*
* @param {shaka.dash.DashParser.Context} context
* @param {shaka.dash.SegmentTemplate.SegmentTemplateInfo} info
* @private
*/
static checkSegmentTemplateInfo_(context, info) {
let n = 0;
n += info.indexTemplate ? 1 : 0;
n += info.timeline ? 1 : 0;
n += info.segmentDuration ? 1 : 0;
if (n == 0) {
shaka.log.error(
'SegmentTemplate does not contain any segment information:',
'the SegmentTemplate must contain either an index URL template',
'a SegmentTimeline, or a segment duration.',
context.representation);
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.MANIFEST,
shaka.util.Error.Code.DASH_NO_SEGMENT_INFO);
} else if (n != 1) {
shaka.log.warning(
'SegmentTemplate contains multiple segment information sources:',
'the SegmentTemplate should only contain an index URL template,',
'a SegmentTimeline or a segment duration.',
context.representation);
if (info.indexTemplate) {
shaka.log.info('Using the index URL template by default.');
info.timeline = null;
info.unscaledSegmentDuration = null;
info.segmentDuration = null;
} else {
goog.asserts.assert(info.timeline, 'There should be a timeline');
shaka.log.info('Using the SegmentTimeline by default.');
info.unscaledSegmentDuration = null;
info.segmentDuration = null;
}
}
if (!info.indexTemplate && !info.mediaTemplate) {
shaka.log.error(
'SegmentTemplate does not contain sufficient segment information:',
'the SegmentTemplate\'s media URL template is missing.',
context.representation);
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.MANIFEST,
shaka.util.Error.Code.DASH_NO_SEGMENT_INFO);
}
}
/**
* Generates a SegmentIndex from an index URL template.
*
* @param {shaka.dash.DashParser.Context} context
* @param {shaka.dash.DashParser.RequestSegmentCallback} requestSegment
* @param {shaka.media.InitSegmentReference} init
* @param {shaka.dash.SegmentTemplate.SegmentTemplateInfo} info
* @return {!Promise<shaka.media.SegmentIndex>}
* @private
*/
static generateSegmentIndexFromIndexTemplate_(
context, requestSegment, init, info) {
const MpdUtils = shaka.dash.MpdUtils;
const ManifestParserUtils = shaka.util.ManifestParserUtils;
goog.asserts.assert(info.indexTemplate, 'must be using index template');
const filledTemplate = MpdUtils.fillUriTemplate(
info.indexTemplate, context.representation.id,
null, null, context.bandwidth || null, null);
const resolvedUris = ManifestParserUtils.resolveUris(
context.representation.getBaseUris(), [filledTemplate]);
return shaka.dash.SegmentBase.generateSegmentIndexFromUris(
context, requestSegment, init, resolvedUris, 0, null,
info.scaledPresentationTimeOffset);
}
/**
* Generates a SegmentIndex from fixed-duration segments.
*
* @param {shaka.dash.DashParser.Context} context
* @param {shaka.dash.SegmentTemplate.SegmentTemplateInfo} info
* @param {number} segmentLimit The maximum number of segments to generate.
* @param {shaka.media.InitSegmentReference} initSegmentReference
* @param {!Object<string, number>} periodDurationMap
* @param {shaka.extern.aesKey|undefined} aesKey
* @param {?number} lastSegmentNumber
* @param {number} segmentSequenceCadence
* @return {!Promise<shaka.media.SegmentIndex>}
* @private
*/
static generateSegmentIndexFromDuration_(
context, info, segmentLimit, initSegmentReference, periodDurationMap,
aesKey, lastSegmentNumber, segmentSequenceCadence) {
goog.asserts.assert(info.mediaTemplate,
'There should be a media template with duration');
const MpdUtils = shaka.dash.MpdUtils;
const ManifestParserUtils = shaka.util.ManifestParserUtils;
const presentationTimeline = context.presentationTimeline;
// Capture values that could change as the parsing context moves on to
// other parts of the manifest.
const periodStart = context.periodInfo.start;
const periodId = context.period.id;
const initialPeriodDuration = context.periodInfo.duration;
// For multi-period live streams the period duration may not be known until
// the following period appears in an updated manifest. periodDurationMap
// provides the updated period duration.
const getPeriodEnd = () => {
const periodDuration =
(periodId != null && periodDurationMap[periodId]) ||
initialPeriodDuration;
const periodEnd = periodDuration ?
(periodStart + periodDuration) : Infinity;
return periodEnd;
};
const segmentDuration = info.segmentDuration;
goog.asserts.assert(
segmentDuration != null, 'Segment duration must not be null!');
const startNumber = info.startNumber;
const template = info.mediaTemplate;
const bandwidth = context.bandwidth || null;
const id = context.representation.id;
const getBaseUris = context.representation.getBaseUris;
const urlParams = context.urlParams;
const timestampOffset = periodStart - info.scaledPresentationTimeOffset;
// Computes the range of presentation timestamps both within the period and
// available. This is an intersection of the period range and the
// availability window.
const computeAvailablePeriodRange = () => {
return [
Math.max(
presentationTimeline.getSegmentAvailabilityStart(),
periodStart),
Math.min(
presentationTimeline.getSegmentAvailabilityEnd(),
getPeriodEnd()),
];
};
// Computes the range of absolute positions both within the period and
// available. The range is inclusive. These are the positions for which we
// will generate segment references.
const computeAvailablePositionRange = () => {
// In presentation timestamps.
const availablePresentationTimes = computeAvailablePeriodRange();
goog.asserts.assert(availablePresentationTimes.every(isFinite),
'Available presentation times must be finite!');
goog.asserts.assert(availablePresentationTimes.every((x) => x >= 0),
'Available presentation times must be positive!');
goog.asserts.assert(segmentDuration != null,
'Segment duration must not be null!');
// In period-relative timestamps.
const availablePeriodTimes =
availablePresentationTimes.map((x) => x - periodStart);
// These may sometimes be reversed ([1] <= [0]) if the period is
// completely unavailable. The logic will still work if this happens,
// because we will simply generate no references.
// In period-relative positions (0-based).
const availablePeriodPositions = [
Math.ceil(availablePeriodTimes[0] / segmentDuration),
Math.ceil(availablePeriodTimes[1] / segmentDuration) - 1,
];
// For Low Latency we can request the partial current position.
if (context.representation.availabilityTimeOffset) {
availablePeriodPositions[1]++;
}
// In absolute positions.
const availablePresentationPositions =
availablePeriodPositions.map((x) => x + startNumber);
return availablePresentationPositions;
};
// For Live, we must limit the initial SegmentIndex in size, to avoid
// consuming too much CPU or memory for content with gigantic
// timeShiftBufferDepth (which can have values up to and including
// Infinity).
const range = computeAvailablePositionRange();
const minPosition = context.dynamic ?
Math.max(range[0], range[1] - segmentLimit + 1) :
range[0];
const maxPosition = lastSegmentNumber || range[1];
const references = [];
const createReference = (position) => {
// These inner variables are all scoped to the inner loop, and can be used
// safely in the callback below.
goog.asserts.assert(segmentDuration != null,
'Segment duration must not be null!');
// Relative to the period start.
const positionWithinPeriod = position - startNumber;
const segmentPeriodTime = positionWithinPeriod * segmentDuration;
const unscaledSegmentDuration = info.unscaledSegmentDuration;
goog.asserts.assert(unscaledSegmentDuration != null,
'Segment duration must not be null!');
// The original media timestamp from the timeline is what is expected in
// the $Time$ template. (Or based on duration, in this case.) It should
// not be adjusted with presentationTimeOffset or the Period start.
let timeReplacement = positionWithinPeriod * unscaledSegmentDuration;
if ('BigInt' in window && timeReplacement > Number.MAX_SAFE_INTEGER) {
timeReplacement =
BigInt(positionWithinPeriod) * BigInt(unscaledSegmentDuration);
}
// Relative to the presentation.
const segmentStart = segmentPeriodTime + periodStart;
const trueSegmentEnd = segmentStart + segmentDuration;
// Cap the segment end at the period end so that references from the
// next period will fit neatly after it.
const segmentEnd = Math.min(trueSegmentEnd, getPeriodEnd());
// This condition will be true unless the segmentStart was >= periodEnd.
// If we've done the position calculations correctly, this won't happen.
goog.asserts.assert(segmentStart < segmentEnd,
'Generated a segment outside of the period!');
const partialSegmentRefs = [];
const numChunks = info.numChunks;
if (numChunks) {
const partialDuration = (segmentEnd - segmentStart) / numChunks;
for (let i = 0; i < numChunks; i++) {
const start = segmentStart + partialDuration * i;
const end = start + partialDuration;
const subNumber = i + 1;
const getPartialUris = () => {
const mediaUri = MpdUtils.fillUriTemplate(
template, id, position, subNumber, bandwidth, timeReplacement);
return ManifestParserUtils.resolveUris(
getBaseUris(), [mediaUri], urlParams());
};
const partial = new shaka.media.SegmentReference(
start,
end,
getPartialUris,
/* startByte= */ 0,
/* endByte= */ null,
initSegmentReference,
timestampOffset,
/* appendWindowStart= */ periodStart,
/* appendWindowEnd= */ getPeriodEnd(),
/* partialReferences= */ [],
/* tilesLayout= */ '',
/* tileDuration= */ null,
/* syncTime= */ null,
shaka.media.SegmentReference.Status.AVAILABLE,
aesKey);
partial.codecs = context.representation.codecs;
partial.mimeType = context.representation.mimeType;
if (segmentSequenceCadence == 0) {
if (i > 0) {
partial.markAsNonIndependent();
}
} else if ((i % segmentSequenceCadence) != 0) {
partial.markAsNonIndependent();
}
partialSegmentRefs.push(partial);
}
}
const getUris = () => {
if (numChunks) {
return [];
}
const mediaUri = MpdUtils.fillUriTemplate(
template, id, position, /* subNumber= */ null, bandwidth,
timeReplacement);
return ManifestParserUtils.resolveUris(
getBaseUris(), [mediaUri], urlParams());
};
const ref = new shaka.media.SegmentReference(
segmentStart,
segmentEnd,
getUris,
/* startByte= */ 0,
/* endByte= */ null,
initSegmentReference,
timestampOffset,
/* appendWindowStart= */ periodStart,
/* appendWindowEnd= */ getPeriodEnd(),
partialSegmentRefs,
/* tilesLayout= */ '',
/* tileDuration= */ null,
/* syncTime= */ null,
shaka.media.SegmentReference.Status.AVAILABLE,
aesKey,
partialSegmentRefs.length > 0);
ref.codecs = context.representation.codecs;
ref.mimeType = context.representation.mimeType;
ref.bandwidth = context.bandwidth;
// This is necessary information for thumbnail streams:
ref.trueEndTime = trueSegmentEnd;
return ref;
};
for (let position = minPosition; position <= maxPosition; ++position) {
const reference = createReference(position);
references.push(reference);
}
/** @type {shaka.media.SegmentIndex} */
const segmentIndex = new shaka.media.SegmentIndex(references);
// If the availability timeline currently ends before the period, we will
// need to add references over time.
const willNeedToAddReferences =
presentationTimeline.getSegmentAvailabilityEnd() < getPeriodEnd();
// When we start a live stream with a period that ends within the
// availability window we will not need to add more references, but we will
// need to evict old references.
const willNeedToEvictReferences = presentationTimeline.isLive();
if (willNeedToAddReferences || willNeedToEvictReferences) {
// The period continues to get longer over time, so check for new
// references once every |segmentDuration| seconds.
// We clamp to |minPosition| in case the initial range was reversed and no
// references were generated. Otherwise, the update would start creating
// negative positions for segments in periods which begin in the future.
let nextPosition = Math.max(minPosition, maxPosition + 1);
let updateTime = segmentDuration;
// For low latency we need to evict very frequently.
if (context.representation.availabilityTimeOffset) {
updateTime = 0.1;
}
segmentIndex.updateEvery(updateTime, () => {
// Evict any references outside the window.
const availabilityStartTime =
presentationTimeline.getSegmentAvailabilityStart();
segmentIndex.evict(availabilityStartTime);
// Compute any new references that need to be added.
const [_, maxPosition] = computeAvailablePositionRange();
const references = [];
while (nextPosition <= maxPosition) {
const reference = createReference(nextPosition);
references.push(reference);
nextPosition++;
}
// The timer must continue firing until the entire period is
// unavailable, so that all references will be evicted.
if (availabilityStartTime > getPeriodEnd() && !references.length) {
// Signal stop.
return null;
}
return references;
});
}
return Promise.resolve(segmentIndex);
}
/**
* Creates an init segment reference from a context object.
*
* @param {shaka.dash.DashParser.Context} context
* @param {shaka.extern.aesKey|undefined} aesKey
* @return {shaka.media.InitSegmentReference}
* @private
*/
static createInitSegment_(context, aesKey) {
const MpdUtils = shaka.dash.MpdUtils;
const ManifestParserUtils = shaka.util.ManifestParserUtils;
const SegmentTemplate = shaka.dash.SegmentTemplate;
let initialization = context.representation.initialization;
if (!initialization) {
initialization = MpdUtils.inheritAttribute(
context, SegmentTemplate.fromInheritance_, 'initialization');
}
if (!initialization) {
return null;
}
initialization = shaka.util.StringUtils.htmlUnescape(initialization);
const repId = context.representation.id;
const bandwidth = context.bandwidth || null;
const getBaseUris = context.representation.getBaseUris;
const urlParams = context.urlParams;
const getUris = () => {
goog.asserts.assert(initialization, 'Should have returned earlier');
const filledTemplate = MpdUtils.fillUriTemplate(
initialization, repId, null, null, bandwidth, null);
const resolvedUris = ManifestParserUtils.resolveUris(
getBaseUris(), [filledTemplate], urlParams());
return resolvedUris;
};
const qualityInfo = shaka.dash.SegmentBase.createQualityInfo(context);
const ref = new shaka.media.InitSegmentReference(
getUris,
/* startByte= */ 0,
/* endByte= */ null,
qualityInfo,
/* timescale= */ null,
/* segmentData= */ null,
aesKey);
ref.codecs = context.representation.codecs;
ref.mimeType = context.representation.mimeType;
return ref;
}
};
/**
* A SegmentIndex that returns segments references on demand from
* a segment timeline.
*
* @extends shaka.media.SegmentIndex
* @implements {shaka.util.IReleasable}
* @implements {Iterable<!shaka.media.SegmentReference>}
*
* @private
*
*/
shaka.dash.TimelineSegmentIndex = class extends shaka.media.SegmentIndex {
/**
*
* @param {!shaka.dash.SegmentTemplate.SegmentTemplateInfo} templateInfo
* @param {?string} representationId
* @param {number} bandwidth
* @param {function(): Array<string>} getBaseUris
* @param {function():string} urlParams
* @param {number} periodStart
* @param {number} periodEnd
* @param {shaka.media.InitSegmentReference} initSegmentReference
* @param {shaka.extern.aesKey|undefined} aesKey
* @param {number} segmentSequenceCadence
*/
constructor(templateInfo, representationId, bandwidth, getBaseUris,
urlParams, periodStart, periodEnd, initSegmentReference,
aesKey, segmentSequenceCadence) {
super([]);
/** @private {?shaka.dash.SegmentTemplate.SegmentTemplateInfo} */
this.templateInfo_ = templateInfo;
/** @private {?string} */
this.representationId_ = representationId;
/** @private {number} */
this.bandwidth_ = bandwidth;
/** @private {function(): Array<string>} */
this.getBaseUris_ = getBaseUris;
/** @private {function():string} */
this.urlParams_ = urlParams;
/** @private {number} */
this.periodStart_ = periodStart;
/** @private {number} */
this.periodEnd_ = periodEnd;
/** @private {shaka.media.InitSegmentReference} */
this.initSegmentReference_ = initSegmentReference;
/** @private {shaka.extern.aesKey|undefined} */
this.aesKey_ = aesKey;
/** @private {number} */
this.segmentSequenceCadence_ = segmentSequenceCadence;
this.fitTimeline();
}
/**
* @override
*/
getNumReferences() {
if (this.templateInfo_) {
return this.templateInfo_.timeline.length;
} else {
return 0;
}
}
/**
* @override
*/
release() {
super.release();
this.templateInfo_ = null;
// We cannot release other fields, as segment index can
// be recreated using only template info.
}
/**
* @override
*/
evict(time) {
if (!this.templateInfo_) {
return;
}
shaka.log.debug(`${this.representationId_} Evicting at ${time}`);
let numToEvict = 0;
const timeline = this.templateInfo_.timeline;
for (let i = 0; i < timeline.length; i += 1) {
const range = timeline[i];
const end = range.end + this.periodStart_;
const start = range.start + this.periodStart_;
if (end <= time) {
shaka.log.debug(`Evicting ${start} - ${end}`);
numToEvict += 1;
} else {
break;
}
}
if (numToEvict > 0) {
this.templateInfo_.timeline = timeline.slice(numToEvict);
if (this.references.length >= numToEvict) {
this.references = this.references.slice(numToEvict);
}
this.numEvicted_ += numToEvict;
if (this.getNumReferences() === 0) {
this.release();
}
}
}
/**
* Merge new template info
* @param {shaka.dash.SegmentTemplate.SegmentTemplateInfo} info
* @param {number} periodStart
* @param {number} periodEnd
* @param {shaka.media.InitSegmentReference} initSegmentReference
*/
appendTemplateInfo(info, periodStart, periodEnd, initSegmentReference) {
this.updateInitSegmentReference(initSegmentReference);
if (!this.templateInfo_) {
this.templateInfo_ = info;
this.periodStart_ = periodStart;
this.periodEnd_ = periodEnd;
} else {
const currentTimeline = this.templateInfo_.timeline;
if (this.templateInfo_.mediaTemplate !== info.mediaTemplate) {
this.templateInfo_.mediaTemplate = info.mediaTemplate;
}
// Append timeline
let newEntries;
if (currentTimeline.length) {
const lastCurrentEntry = currentTimeline[currentTimeline.length - 1];
newEntries = info.timeline.filter((entry) => {
return entry.end > lastCurrentEntry.end;
});
} else {
newEntries = info.timeline.slice();
}
if (newEntries.length > 0) {
shaka.log.debug(`Appending ${newEntries.length} entries`);
this.templateInfo_.timeline.push(...newEntries);
}
if (this.periodEnd_ !== periodEnd) {
this.periodEnd_ = periodEnd;
}
}
this.fitTimeline();
}
/**
* Updates the init segment reference and propagates the update to all
* references.
* @param {shaka.media.InitSegmentReference} initSegmentReference
*/
updateInitSegmentReference(initSegmentReference) {
if (this.initSegmentReference_ === initSegmentReference) {
return;
}
this.initSegmentReference_ = initSegmentReference;
for (const reference of this.references) {
if (reference) {
reference.updateInitSegmentReference(initSegmentReference);
}
}
}
/**
*
* @param {number} time
*/
isBeforeFirstEntry(time) {
const hasTimeline = this.templateInfo_ &&
this.templateInfo_.timeline && this.templateInfo_.timeline.length;
if (hasTimeline) {
const timeline = this.templateInfo_.timeline;
return time < timeline[0].start + this.periodStart_;
} else {
return false;
}
}
/**
* Fit timeline entries to period boundaries
*/
fitTimeline() {
if (this.getIsImmutable()) {
return;
}
const timeline = this.templateInfo_.timeline;
goog.asserts.assert(timeline, 'Timeline should be non-null!');
const newTimeline = [];
for (const range of timeline) {
if (range.start >= this.periodEnd_) {
// Starts after end of period.
} else if (range.end <= 0) {
// Ends before start of period.
} else {
// Usable.
newTimeline.push(range);
}
}
this.templateInfo_.timeline = newTimeline;
this.evict(this.periodStart_);
// Do NOT adjust last range to match period end! With high precision
// timestamps several recalculations may give wrong results on less precise
// platforms. To mitigate that, we're using cached |periodEnd_| value in
// find/get() methods whenever possible.
}
/**
* @override
*/
find(time) {
shaka.log.debug(`Find ${time}`);
if (this.isBeforeFirstEntry(time)) {
return this.numEvicted_;
}
if (!this.templateInfo_) {
return null;
}
const timeline = this.templateInfo_.timeline;
// Early exit if the time isn't within this period
if (time < this.periodStart_ || time >= this.periodEnd_) {
return null;
}
const lastIndex = timeline.length - 1;
for (let i = 0; i < timeline.length; i++) {
const range = timeline[i];
const start = range.start + this.periodStart_;
// A rounding error can cause /time/ to equal e.endTime or fall in between
// the references by a fraction of a second. To account for this, we use
// the start of the next segment as /end/, unless this is the last
// reference, in which case we use the period end as the /end/
let end;
if (i < lastIndex) {
end = timeline[i + 1].start + this.periodStart_;
} else if (this.periodEnd_ === Infinity) {
end = range.end + this.periodStart_;
} else {
end = this.periodEnd_;
}
if ((time >= start) && (time < end)) {
return i + this.numEvicted_;
}
}
return null;
}
/**
* @override
*/
get(position) {
const correctedPosition = position - this.numEvicted_;
if (correctedPosition < 0 ||
correctedPosition >= this.getNumReferences() || !this.templateInfo_) {
return null;
}
let ref = this.references[correctedPosition];
if (!ref) {
const range = this.templateInfo_.timeline[correctedPosition];
const segmentReplacement = range.segmentPosition;