forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment_base.js
371 lines (327 loc) · 13 KB
/
segment_base.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
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.dash.SegmentBase');
goog.require('goog.asserts');
goog.require('shaka.dash.MpdUtils');
goog.require('shaka.log');
goog.require('shaka.media.InitSegmentReference');
goog.require('shaka.media.Mp4SegmentIndexParser');
goog.require('shaka.media.SegmentIndex');
goog.require('shaka.media.WebmSegmentIndexParser');
goog.require('shaka.util.Error');
goog.require('shaka.util.ManifestParserUtils');
goog.require('shaka.util.ObjectUtils');
goog.require('shaka.util.XmlUtils');
goog.requireType('shaka.dash.DashParser');
goog.requireType('shaka.media.PresentationTimeline');
goog.requireType('shaka.media.SegmentReference');
/**
* @summary A set of functions for parsing SegmentBase elements.
*/
shaka.dash.SegmentBase = class {
/**
* Creates an init segment reference from a Context object.
*
* @param {shaka.dash.DashParser.Context} context
* @param {function(?shaka.dash.DashParser.InheritanceFrame):Element} callback
* @return {shaka.media.InitSegmentReference}
*/
static createInitSegment(context, callback) {
const MpdUtils = shaka.dash.MpdUtils;
const XmlUtils = shaka.util.XmlUtils;
const ManifestParserUtils = shaka.util.ManifestParserUtils;
const initialization =
MpdUtils.inheritChild(context, callback, 'Initialization');
if (!initialization) {
return null;
}
let resolvedUris = context.representation.baseUris;
const uri = initialization.getAttribute('sourceURL');
if (uri) {
resolvedUris = ManifestParserUtils.resolveUris(
context.representation.baseUris, [uri]);
}
let startByte = 0;
let endByte = null;
const range =
XmlUtils.parseAttr(initialization, 'range', XmlUtils.parseRange);
if (range) {
startByte = range.start;
endByte = range.end;
}
const getUris = () => resolvedUris;
const qualityInfo = shaka.dash.SegmentBase.createQualityInfo(context);
return new shaka.media.InitSegmentReference(
getUris, startByte, endByte, qualityInfo);
}
/**
* Creates a new StreamInfo object.
*
* @param {shaka.dash.DashParser.Context} context
* @param {shaka.dash.DashParser.RequestSegmentCallback} requestSegment
* @return {shaka.dash.DashParser.StreamInfo}
*/
static createStreamInfo(context, requestSegment) {
goog.asserts.assert(context.representation.segmentBase,
'Should only be called with SegmentBase');
// Since SegmentBase does not need updates, simply treat any call as
// the initial parse.
const MpdUtils = shaka.dash.MpdUtils;
const SegmentBase = shaka.dash.SegmentBase;
const XmlUtils = shaka.util.XmlUtils;
const unscaledPresentationTimeOffset = Number(MpdUtils.inheritAttribute(
context, SegmentBase.fromInheritance_, 'presentationTimeOffset')) || 0;
const timescaleStr = MpdUtils.inheritAttribute(
context, SegmentBase.fromInheritance_, 'timescale');
let timescale = 1;
if (timescaleStr) {
timescale = XmlUtils.parsePositiveInt(timescaleStr) || 1;
}
const scaledPresentationTimeOffset =
(unscaledPresentationTimeOffset / timescale) || 0;
const initSegmentReference =
SegmentBase.createInitSegment(context, SegmentBase.fromInheritance_);
// Throws an immediate error if the format is unsupported.
SegmentBase.checkSegmentIndexRangeSupport_(context, initSegmentReference);
// 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);
return {
generateSegmentIndex: () => {
return SegmentBase.generateSegmentIndex_(
shallowCopyOfContext, requestSegment, initSegmentReference,
scaledPresentationTimeOffset);
},
};
}
/**
* Creates a SegmentIndex for the given URIs and context.
*
* @param {shaka.dash.DashParser.Context} context
* @param {shaka.dash.DashParser.RequestSegmentCallback} requestSegment
* @param {shaka.media.InitSegmentReference} initSegmentReference
* @param {!Array.<string>} uris
* @param {number} startByte
* @param {?number} endByte
* @param {number} scaledPresentationTimeOffset
* @return {!Promise.<shaka.media.SegmentIndex>}
*/
static async generateSegmentIndexFromUris(
context, requestSegment, initSegmentReference, uris, startByte,
endByte, scaledPresentationTimeOffset) {
// Unpack context right away, before we start an async process.
// This immunizes us against changes to the context object later.
/** @type {shaka.media.PresentationTimeline} */
const presentationTimeline = context.presentationTimeline;
const fitLast = !context.dynamic || !context.periodInfo.isLastPeriod;
const periodStart = context.periodInfo.start;
const periodDuration = context.periodInfo.duration;
const containerType = context.representation.mimeType.split('/')[1];
// Create a local variable to bind to so we can set to null to help the GC.
let localRequest = requestSegment;
let segmentIndex = null;
const responses = [
localRequest(uris, startByte, endByte, /* isInit= */ false),
containerType == 'webm' ?
localRequest(
initSegmentReference.getUris(),
initSegmentReference.startByte,
initSegmentReference.endByte,
/* isInit= */ true) :
null,
];
localRequest = null;
const results = await Promise.all(responses);
const indexData = results[0];
const initData = results[1] || null;
/** @type {Array.<!shaka.media.SegmentReference>} */
let references = null;
const timestampOffset = periodStart - scaledPresentationTimeOffset;
const appendWindowStart = periodStart;
const appendWindowEnd = periodDuration ?
periodStart + periodDuration : Infinity;
if (containerType == 'mp4') {
references = shaka.media.Mp4SegmentIndexParser.parse(
indexData, startByte, uris, initSegmentReference, timestampOffset,
appendWindowStart, appendWindowEnd);
} else {
goog.asserts.assert(initData, 'WebM requires init data');
references = shaka.media.WebmSegmentIndexParser.parse(
indexData, initData, uris, initSegmentReference, timestampOffset,
appendWindowStart, appendWindowEnd);
}
presentationTimeline.notifySegments(references);
// Since containers are never updated, we don't need to store the
// segmentIndex in the map.
goog.asserts.assert(!segmentIndex,
'Should not call generateSegmentIndex twice');
segmentIndex = new shaka.media.SegmentIndex(references);
if (fitLast) {
segmentIndex.fit(appendWindowStart, appendWindowEnd, /* isNew= */ true);
}
return segmentIndex;
}
/**
* @param {?shaka.dash.DashParser.InheritanceFrame} frame
* @return {Element}
* @private
*/
static fromInheritance_(frame) {
return frame.segmentBase;
}
/**
* Compute the byte range of the segment index from the container.
*
* @param {shaka.dash.DashParser.Context} context
* @return {?{start: number, end: number}}
* @private
*/
static computeIndexRange_(context) {
const MpdUtils = shaka.dash.MpdUtils;
const SegmentBase = shaka.dash.SegmentBase;
const XmlUtils = shaka.util.XmlUtils;
const representationIndex = MpdUtils.inheritChild(
context, SegmentBase.fromInheritance_, 'RepresentationIndex');
const indexRangeElem = MpdUtils.inheritAttribute(
context, SegmentBase.fromInheritance_, 'indexRange');
let indexRange = XmlUtils.parseRange(indexRangeElem || '');
if (representationIndex) {
indexRange = XmlUtils.parseAttr(
representationIndex, 'range', XmlUtils.parseRange, indexRange);
}
return indexRange;
}
/**
* Compute the URIs of the segment index from the container.
*
* @param {shaka.dash.DashParser.Context} context
* @return {!Array.<string>}
* @private
*/
static computeIndexUris_(context) {
const ManifestParserUtils = shaka.util.ManifestParserUtils;
const MpdUtils = shaka.dash.MpdUtils;
const SegmentBase = shaka.dash.SegmentBase;
const representationIndex = MpdUtils.inheritChild(
context, SegmentBase.fromInheritance_, 'RepresentationIndex');
let indexUris = context.representation.baseUris;
if (representationIndex) {
const representationUri = representationIndex.getAttribute('sourceURL');
if (representationUri) {
indexUris = ManifestParserUtils.resolveUris(
context.representation.baseUris, [representationUri]);
}
}
return indexUris;
}
/**
* Check if this type of segment index is supported. This allows for
* immediate errors during parsing, as opposed to an async error from
* createSegmentIndex().
*
* Also checks for a valid byte range, which is not required for callers from
* SegmentTemplate.
*
* @param {shaka.dash.DashParser.Context} context
* @param {shaka.media.InitSegmentReference} initSegmentReference
* @private
*/
static checkSegmentIndexRangeSupport_(context, initSegmentReference) {
const SegmentBase = shaka.dash.SegmentBase;
SegmentBase.checkSegmentIndexSupport(context, initSegmentReference);
const indexRange = SegmentBase.computeIndexRange_(context);
if (!indexRange) {
shaka.log.error(
'SegmentBase does not contain sufficient segment information:',
'the SegmentBase does not contain @indexRange',
'or a RepresentationIndex element.',
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);
}
}
/**
* Check if this type of segment index is supported. This allows for
* immediate errors during parsing, as opposed to an async error from
* createSegmentIndex().
*
* @param {shaka.dash.DashParser.Context} context
* @param {shaka.media.InitSegmentReference} initSegmentReference
*/
static checkSegmentIndexSupport(context, initSegmentReference) {
const ContentType = shaka.util.ManifestParserUtils.ContentType;
const contentType = context.representation.contentType;
const containerType = context.representation.mimeType.split('/')[1];
if (contentType != ContentType.TEXT && containerType != 'mp4' &&
containerType != 'webm') {
shaka.log.error(
'SegmentBase specifies an unsupported container type.',
context.representation);
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.MANIFEST,
shaka.util.Error.Code.DASH_UNSUPPORTED_CONTAINER);
}
if ((containerType == 'webm') && !initSegmentReference) {
shaka.log.error(
'SegmentBase does not contain sufficient segment information:',
'the SegmentBase uses a WebM container,',
'but does not contain an Initialization element.',
context.representation);
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.MANIFEST,
shaka.util.Error.Code.DASH_WEBM_MISSING_INIT);
}
}
/**
* Generate a SegmentIndex from a Context object.
*
* @param {shaka.dash.DashParser.Context} context
* @param {shaka.dash.DashParser.RequestSegmentCallback} requestSegment
* @param {shaka.media.InitSegmentReference} initSegmentReference
* @param {number} scaledPresentationTimeOffset
* @return {!Promise.<shaka.media.SegmentIndex>}
* @private
*/
static generateSegmentIndex_(
context, requestSegment, initSegmentReference,
scaledPresentationTimeOffset) {
const SegmentBase = shaka.dash.SegmentBase;
const indexUris = SegmentBase.computeIndexUris_(context);
const indexRange = SegmentBase.computeIndexRange_(context);
goog.asserts.assert(indexRange, 'Index range should not be null!');
return shaka.dash.SegmentBase.generateSegmentIndexFromUris(
context, requestSegment, initSegmentReference, indexUris,
indexRange.start, indexRange.end,
scaledPresentationTimeOffset);
}
/**
* Create a MediaQualityInfo object from a Context object.
*
* @param {!shaka.dash.DashParser.Context} context
* @return {!shaka.extern.MediaQualityInfo}
*/
static createQualityInfo(context) {
const representation = context.representation;
return {
bandwidth: context.bandwidth,
audioSamplingRate: representation.audioSamplingRate,
codecs: representation.codecs,
contentType: representation.contentType,
frameRate: representation.frameRate || null,
height: representation.height || null,
mimeType: representation.mimeType,
channelsCount: representation.numChannels,
pixelAspectRatio: representation.pixelAspectRatio || null,
width: representation.width || null,
};
}
};