/
segment_list.js
347 lines (313 loc) · 11.7 KB
/
segment_list.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
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.dash.SegmentList');
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.Functional');
goog.require('shaka.util.ManifestParserUtils');
goog.require('shaka.util.XmlUtils');
goog.requireType('shaka.dash.DashParser');
/**
* @summary A set of functions for parsing SegmentList elements.
*/
shaka.dash.SegmentList = class {
/**
* Creates a new StreamInfo object.
* Updates the existing SegmentIndex, if any.
*
* @param {shaka.dash.DashParser.Context} context
* @param {!Object.<string, !shaka.extern.Stream>} streamMap
* @return {shaka.dash.DashParser.StreamInfo}
*/
static createStreamInfo(context, streamMap) {
goog.asserts.assert(context.representation.segmentList,
'Should only be called with SegmentList');
const SegmentList = shaka.dash.SegmentList;
const initSegmentReference = shaka.dash.SegmentBase.createInitSegment(
context, SegmentList.fromInheritance_);
const info = SegmentList.parseSegmentListInfo_(context);
SegmentList.checkSegmentListInfo_(context, info);
/** @type {shaka.media.SegmentIndex} */
let segmentIndex = null;
let stream = null;
if (context.period.id && context.representation.id) {
// Only check/store the index if period and representation IDs are set.
const id = context.period.id + ',' + context.representation.id;
stream = streamMap[id];
if (stream) {
segmentIndex = stream.segmentIndex;
}
}
const references = SegmentList.createSegmentReferences_(
context.periodInfo.start, context.periodInfo.duration,
info.startNumber, context.representation.baseUris, info,
initSegmentReference);
const isNew = !segmentIndex;
if (segmentIndex) {
const start = context.presentationTimeline.getSegmentAvailabilityStart();
segmentIndex.mergeAndEvict(references, start);
} else {
segmentIndex = new shaka.media.SegmentIndex(references);
}
context.presentationTimeline.notifySegments(references);
if (!context.dynamic || !context.periodInfo.isLastPeriod) {
const periodStart = context.periodInfo.start;
const periodEnd = context.periodInfo.duration ?
context.periodInfo.start + context.periodInfo.duration : Infinity;
segmentIndex.fit(periodStart, periodEnd, isNew);
}
if (stream) {
stream.segmentIndex = segmentIndex;
}
return {
generateSegmentIndex: () => {
if (!segmentIndex || segmentIndex.isEmpty()) {
segmentIndex.merge(references);
}
return Promise.resolve(segmentIndex);
},
};
}
/**
* @param {?shaka.dash.DashParser.InheritanceFrame} frame
* @return {Element}
* @private
*/
static fromInheritance_(frame) {
return frame.segmentList;
}
/**
* Parses the SegmentList items to create an info object.
*
* @param {shaka.dash.DashParser.Context} context
* @return {shaka.dash.SegmentList.SegmentListInfo}
* @private
*/
static parseSegmentListInfo_(context) {
const SegmentList = shaka.dash.SegmentList;
const MpdUtils = shaka.dash.MpdUtils;
const mediaSegments = SegmentList.parseMediaSegments_(context);
const segmentInfo =
MpdUtils.parseSegmentInfo(context, SegmentList.fromInheritance_);
let startNumber = segmentInfo.startNumber;
if (startNumber == 0) {
shaka.log.warning('SegmentList@startNumber must be > 0');
startNumber = 1;
}
let startTime = 0;
if (segmentInfo.segmentDuration) {
// See DASH sec. 5.3.9.5.3
// Don't use presentationTimeOffset for @duration.
startTime = segmentInfo.segmentDuration * (startNumber - 1);
} else if (segmentInfo.timeline && segmentInfo.timeline.length > 0) {
// The presentationTimeOffset was considered in timeline creation.
startTime = segmentInfo.timeline[0].start;
}
return {
segmentDuration: segmentInfo.segmentDuration,
startTime: startTime,
startNumber: startNumber,
scaledPresentationTimeOffset: segmentInfo.scaledPresentationTimeOffset,
timeline: segmentInfo.timeline,
mediaSegments: mediaSegments,
};
}
/**
* Checks whether a SegmentListInfo object is valid.
*
* @param {shaka.dash.DashParser.Context} context
* @param {shaka.dash.SegmentList.SegmentListInfo} info
* @private
*/
static checkSegmentListInfo_(context, info) {
if (!info.segmentDuration && !info.timeline &&
info.mediaSegments.length > 1) {
shaka.log.warning(
'SegmentList does not contain sufficient segment information:',
'the SegmentList specifies multiple segments,',
'but does not specify a segment duration or timeline.',
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);
}
if (!info.segmentDuration && !context.periodInfo.duration &&
!info.timeline && info.mediaSegments.length == 1) {
shaka.log.warning(
'SegmentList does not contain sufficient segment information:',
'the SegmentList specifies one segment,',
'but does not specify a segment duration, period duration,',
'or timeline.',
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);
}
if (info.timeline && info.timeline.length == 0) {
shaka.log.warning(
'SegmentList does not contain sufficient segment information:',
'the SegmentList has an empty timeline.',
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);
}
}
/**
* Creates an array of segment references for the given data.
*
* @param {number} periodStart in seconds.
* @param {?number} periodDuration in seconds.
* @param {number} startNumber
* @param {!Array.<string>} baseUris
* @param {shaka.dash.SegmentList.SegmentListInfo} info
* @param {shaka.media.InitSegmentReference} initSegmentReference
* @return {!Array.<!shaka.media.SegmentReference>}
* @private
*/
static createSegmentReferences_(
periodStart, periodDuration, startNumber, baseUris, info,
initSegmentReference) {
const ManifestParserUtils = shaka.util.ManifestParserUtils;
let max = info.mediaSegments.length;
if (info.timeline && info.timeline.length != info.mediaSegments.length) {
max = Math.min(info.timeline.length, info.mediaSegments.length);
shaka.log.warning(
'The number of items in the segment timeline and the number of ',
'segment URLs do not match, truncating', info.mediaSegments.length,
'to', max);
}
const timestampOffset = periodStart - info.scaledPresentationTimeOffset;
const appendWindowStart = periodStart;
const appendWindowEnd = periodDuration ?
periodStart + periodDuration : Infinity;
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references = [];
let prevEndTime = info.startTime;
for (let i = 0; i < max; i++) {
const segment = info.mediaSegments[i];
const mediaUri = ManifestParserUtils.resolveUris(
baseUris, [segment.mediaUri]);
const startTime = prevEndTime;
let endTime;
if (info.segmentDuration != null) {
endTime = startTime + info.segmentDuration;
} else if (info.timeline) {
// Ignore the timepoint start since they are continuous.
endTime = info.timeline[i].end;
} else {
// If segmentDuration and timeline are null then there must
// be exactly one segment.
goog.asserts.assert(
info.mediaSegments.length == 1 && periodDuration,
'There should be exactly one segment with a Period duration.');
endTime = startTime + periodDuration;
}
const getUris = () => mediaUri;
references.push(
new shaka.media.SegmentReference(
periodStart + startTime,
periodStart + endTime,
getUris,
segment.start,
segment.end,
initSegmentReference,
timestampOffset,
appendWindowStart, appendWindowEnd));
prevEndTime = endTime;
}
return references;
}
/**
* Parses the media URIs from the context.
*
* @param {shaka.dash.DashParser.Context} context
* @return {!Array.<shaka.dash.SegmentList.MediaSegment>}
* @private
*/
static parseMediaSegments_(context) {
const Functional = shaka.util.Functional;
/** @type {!Array.<!Element>} */
const segmentLists = [
context.representation.segmentList,
context.adaptationSet.segmentList,
context.period.segmentList,
].filter(Functional.isNotNull);
const XmlUtils = shaka.util.XmlUtils;
// Search each SegmentList for one with at least one SegmentURL element,
// select the first one, and convert each SegmentURL element to a tuple.
return segmentLists
.map((node) => { return XmlUtils.findChildren(node, 'SegmentURL'); })
.reduce((all, part) => { return all.length > 0 ? all : part; })
.map((urlNode) => {
if (urlNode.getAttribute('indexRange') &&
!context.indexRangeWarningGiven) {
context.indexRangeWarningGiven = true;
shaka.log.warning(
'We do not support the SegmentURL@indexRange attribute on ' +
'SegmentList. We only use the SegmentList@duration ' +
'attribute or SegmentTimeline, which must be accurate.');
}
const uri = urlNode.getAttribute('media');
const range = XmlUtils.parseAttr(
urlNode, 'mediaRange', XmlUtils.parseRange,
{start: 0, end: null});
return {mediaUri: uri, start: range.start, end: range.end};
});
}
};
/**
* @typedef {{
* mediaUri: string,
* start: number,
* end: ?number
* }}
*
* @property {string} mediaUri
* The URI of the segment.
* @property {number} start
* The start byte of the segment.
* @property {?number} end
* The end byte of the segment, or null.
*/
shaka.dash.SegmentList.MediaSegment;
/**
* @typedef {{
* segmentDuration: ?number,
* startTime: number,
* startNumber: number,
* scaledPresentationTimeOffset: number,
* timeline: Array.<shaka.dash.MpdUtils.TimeRange>,
* mediaSegments: !Array.<shaka.dash.SegmentList.MediaSegment>
* }}
* @private
*
* @description
* Contains information about a SegmentList.
*
* @property {?number} segmentDuration
* The duration of the segments, if given.
* @property {number} startTime
* The start time of the first segment, in seconds.
* @property {number} startNumber
* The start number of the segments; 1 or greater.
* @property {number} scaledPresentationTimeOffset
* The scaledPresentationTimeOffset of the representation, in seconds.
* @property {Array.<shaka.dash.MpdUtils.TimeRange>} timeline
* The timeline of the representation, if given. Times in seconds.
* @property {!Array.<shaka.dash.SegmentList.MediaSegment>} mediaSegments
* The URI and byte-ranges of the media segments.
*/
shaka.dash.SegmentList.SegmentListInfo;