forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 1
/
segment_template.js
607 lines (534 loc) · 22.5 KB
/
segment_template.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
/*! @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.Iterables');
goog.require('shaka.util.ManifestParserUtils');
goog.require('shaka.util.ObjectUtils');
goog.requireType('shaka.dash.DashParser');
/**
* @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.RequestInitSegmentCallback}
* requestInitSegment
* @param {!Object.<string, !shaka.media.SegmentIndex>} segmentIndexMap
* @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
* @return {shaka.dash.DashParser.StreamInfo}
*/
static createStreamInfo(
context, requestInitSegment, segmentIndexMap, isUpdate,
segmentLimit, periodDurationMap) {
goog.asserts.assert(context.representation.segmentTemplate,
'Should only be called with SegmentTemplate');
const SegmentTemplate = shaka.dash.SegmentTemplate;
const initSegmentReference = SegmentTemplate.createInitSegment_(context);
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, requestInitSegment, initSegmentReference,
info);
},
};
} else if (info.segmentDuration) {
if (!isUpdate) {
context.presentationTimeline.notifyMaxSegmentDuration(
info.segmentDuration);
context.presentationTimeline.notifyMinSegmentStartTime(
context.periodInfo.start);
}
return {
generateSegmentIndex: () => {
return SegmentTemplate.generateSegmentIndexFromDuration_(
shallowCopyOfContext, info, segmentLimit, initSegmentReference,
periodDurationMap);
},
};
} else {
/** @type {shaka.media.SegmentIndex} */
let segmentIndex = null;
let id = 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;
segmentIndex = segmentIndexMap[id];
}
const references = SegmentTemplate.createFromTimeline_(
shallowCopyOfContext, info, initSegmentReference);
const periodStart = context.periodInfo.start;
const periodEnd = context.periodInfo.duration ?
context.periodInfo.start + context.periodInfo.duration : Infinity;
// Don't fit live content, since it might receive more segments.
// Unless that live content is multi-period; it's safe to fit every period
// but the last one, since only the last period might receive new
// segments.
const shouldFit = periodEnd != Infinity;
if (segmentIndex) {
if (shouldFit) {
// Fit the new references before merging them, so that the merge
// algorithm has a more accurate view of their start and end times.
const wrapper = new shaka.media.SegmentIndex(references);
wrapper.fit(periodStart, periodEnd, /* isNew= */ true);
}
segmentIndex.mergeAndEvict(references,
context.presentationTimeline.getSegmentAvailabilityStart());
} else {
context.presentationTimeline.notifySegments(references);
segmentIndex = new shaka.media.SegmentIndex(references);
if (id && context.dynamic) {
segmentIndexMap[id] = segmentIndex;
}
}
if (shouldFit) {
segmentIndex.fit(periodStart, periodEnd);
}
return {
generateSegmentIndex: () => Promise.resolve(segmentIndex),
};
}
}
/**
* @param {?shaka.dash.DashParser.InheritanceFrame} frame
* @return {Element}
* @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 segmentInfo =
MpdUtils.parseSegmentInfo(context, SegmentTemplate.fromInheritance_);
const media = MpdUtils.inheritAttribute(
context, SegmentTemplate.fromInheritance_, 'media');
const index = MpdUtils.inheritAttribute(
context, SegmentTemplate.fromInheritance_, 'index');
return {
segmentDuration: segmentInfo.segmentDuration,
timescale: segmentInfo.timescale,
startNumber: segmentInfo.startNumber,
scaledPresentationTimeOffset: segmentInfo.scaledPresentationTimeOffset,
unscaledPresentationTimeOffset:
segmentInfo.unscaledPresentationTimeOffset,
timeline: segmentInfo.timeline,
mediaTemplate: media,
indexTemplate: index,
};
}
/**
* 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 containes 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.segmentDuration = null;
} else {
goog.asserts.assert(info.timeline, 'There should be a timeline');
shaka.log.info('Using the SegmentTimeline by default.');
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.RequestInitSegmentCallback}
* requestInitSegment
* @param {shaka.media.InitSegmentReference} init
* @param {shaka.dash.SegmentTemplate.SegmentTemplateInfo} info
* @return {!Promise.<shaka.media.SegmentIndex>}
* @private
*/
static generateSegmentIndexFromIndexTemplate_(
context, requestInitSegment, 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, context.bandwidth || null, null);
const resolvedUris = ManifestParserUtils.resolveUris(
context.representation.baseUris, [filledTemplate]);
return shaka.dash.SegmentBase.generateSegmentIndexFromUris(
context, requestInitSegment, 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
* @return {!Promise.<shaka.media.SegmentIndex>}
* @private
*/
static generateSegmentIndexFromDuration_(
context, info, segmentLimit, initSegmentReference, periodDurationMap) {
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 timescale = info.timescale;
const template = info.mediaTemplate;
const bandwidth = context.bandwidth || null;
const id = context.representation.id;
const baseUris = context.representation.baseUris;
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,
];
// 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 = 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;
// What will appear in the actual segment files. The media timestamp is
// what is expected in the $Time$ template.
const segmentMediaTime = segmentPeriodTime +
info.scaledPresentationTimeOffset;
const getUris = () => {
const mediaUri = MpdUtils.fillUriTemplate(
template, id, position, bandwidth,
segmentMediaTime * timescale);
return ManifestParserUtils.resolveUris(baseUris, [mediaUri]);
};
// Relative to the presentation.
const segmentStart = segmentPeriodTime + periodStart;
// Cap the segment end at the period end so that references from the
// next period will fit neatly after it.
const segmentEnd = Math.min(segmentStart + segmentDuration,
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!');
return new shaka.media.SegmentReference(
segmentStart,
segmentEnd,
getUris,
/* startByte= */ 0,
/* endByte= */ null,
initSegmentReference,
timestampOffset,
/* appendWindowStart= */ periodStart,
/* appendWindowEnd= */ getPeriodEnd());
};
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);
segmentIndex.updateEvery(segmentDuration, () => {
// 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 segment references from a timeline.
*
* @param {shaka.dash.DashParser.Context} context
* @param {shaka.dash.SegmentTemplate.SegmentTemplateInfo} info
* @param {shaka.media.InitSegmentReference} initSegmentReference
* @return {!Array.<!shaka.media.SegmentReference>}
* @private
*/
static createFromTimeline_(context, info, initSegmentReference) {
const MpdUtils = shaka.dash.MpdUtils;
const ManifestParserUtils = shaka.util.ManifestParserUtils;
const periodStart = context.periodInfo.start;
const periodDuration = context.periodInfo.duration;
const timestampOffset = periodStart - info.scaledPresentationTimeOffset;
const appendWindowStart = periodStart;
const appendWindowEnd = periodDuration ?
periodStart + periodDuration : Infinity;
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references = [];
const enum_ = (it) => shaka.util.Iterables.enumerate(it);
for (const {i, item: {start, unscaledStart, end}} of enum_(info.timeline)) {
// Note: i = k - 1, where k indicates the k'th segment listed in the MPD.
// (See section 5.3.9.5.3 of the DASH spec.)
const segmentReplacement = i + info.startNumber;
// Consider the presentation time offset in segment uri computation
const timeReplacement = unscaledStart +
info.unscaledPresentationTimeOffset;
const repId = context.representation.id;
const bandwidth = context.bandwidth || null;
const mediaTemplate = info.mediaTemplate;
const baseUris = context.representation.baseUris;
// This callback must not capture any non-local
// variables, such as info, context, etc. Make
// sure any values you reference here have
// been assigned to local variables within the
// loop, or else we will end up with a leak.
const createUris =
() => {
goog.asserts.assert(
mediaTemplate,
'There should be a media template with a timeline');
const mediaUri = MpdUtils.fillUriTemplate(
mediaTemplate, repId,
segmentReplacement, bandwidth || null, timeReplacement);
return ManifestParserUtils
.resolveUris(baseUris, [mediaUri])
.map((g) => {
return g.toString();
});
};
references.push(new shaka.media.SegmentReference(
periodStart + start,
periodStart + end,
createUris,
/* startByte= */ 0,
/* endByte= */ null,
initSegmentReference,
timestampOffset,
appendWindowStart,
appendWindowEnd));
}
return references;
}
/**
* Creates an init segment reference from a context object.
*
* @param {shaka.dash.DashParser.Context} context
* @return {shaka.media.InitSegmentReference}
* @private
*/
static createInitSegment_(context) {
const MpdUtils = shaka.dash.MpdUtils;
const ManifestParserUtils = shaka.util.ManifestParserUtils;
const SegmentTemplate = shaka.dash.SegmentTemplate;
const initialization = MpdUtils.inheritAttribute(
context, SegmentTemplate.fromInheritance_, 'initialization');
if (!initialization) {
return null;
}
const repId = context.representation.id;
const bandwidth = context.bandwidth || null;
const baseUris = context.representation.baseUris;
const getUris = () => {
goog.asserts.assert(initialization, 'Should have returned earler');
const filledTemplate = MpdUtils.fillUriTemplate(
initialization, repId, null, bandwidth, null);
const resolvedUris = ManifestParserUtils.resolveUris(
baseUris, [filledTemplate]);
return resolvedUris;
};
return new shaka.media.InitSegmentReference(getUris, 0, null);
}
};
/**
* @typedef {{
* timescale: number,
* segmentDuration: ?number,
* startNumber: number,
* scaledPresentationTimeOffset: number,
* unscaledPresentationTimeOffset: number,
* timeline: Array.<shaka.dash.MpdUtils.TimeRange>,
* mediaTemplate: ?string,
* indexTemplate: ?string
* }}
* @private
*
* @description
* Contains information about a SegmentTemplate.
*
* @property {number} timescale
* The time-scale of the representation.
* @property {?number} segmentDuration
* The duration of the segments in seconds, if given.
* @property {number} startNumber
* The start number of the segments; 1 or greater.
* @property {number} scaledPresentationTimeOffset
* The presentation time offset of the representation, in seconds.
* @property {number} unscaledPresentationTimeOffset
* The presentation time offset of the representation, in timescale units.
* @property {Array.<shaka.dash.MpdUtils.TimeRange>} timeline
* The timeline of the representation, if given. Times in seconds.
* @property {?string} mediaTemplate
* The media URI template, if given.
* @property {?string} indexTemplate
* The index URI template, if given.
*/
shaka.dash.SegmentTemplate.SegmentTemplateInfo;