forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest_converter.js
315 lines (276 loc) · 8.98 KB
/
manifest_converter.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
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.offline.ManifestConverter');
goog.require('goog.asserts');
goog.require('shaka.media.InitSegmentReference');
goog.require('shaka.media.PresentationTimeline');
goog.require('shaka.media.SegmentIndex');
goog.require('shaka.media.SegmentReference');
goog.require('shaka.offline.OfflineUri');
goog.require('shaka.util.ManifestParserUtils');
/**
* Utility class for converting database manifest objects back to normal
* player-ready objects. Used by the offline system to convert on-disk
* objects back to the in-memory objects.
*/
shaka.offline.ManifestConverter = class {
/**
* Create a new manifest converter. Need to know the mechanism and cell that
* the manifest is from so that all segments paths can be created.
*
* @param {string} mechanism
* @param {string} cell
*/
constructor(mechanism, cell) {
/** @private {string} */
this.mechanism_ = mechanism;
/** @private {string} */
this.cell_ = cell;
}
/**
* Convert a |shaka.extern.ManifestDB| object to a |shaka.extern.Manifest|
* object.
*
* @param {shaka.extern.ManifestDB} manifestDB
* @return {shaka.extern.Manifest}
*/
fromManifestDB(manifestDB) {
const timeline = new shaka.media.PresentationTimeline(null, 0);
timeline.setDuration(manifestDB.duration);
/** @type {!Array.<shaka.extern.StreamDB>} */
const audioStreams =
manifestDB.streams.filter((streamDB) => this.isAudio_(streamDB));
/** @type {!Array.<shaka.extern.StreamDB>} */
const videoStreams =
manifestDB.streams.filter((streamDB) => this.isVideo_(streamDB));
/** @type {!Map.<number, shaka.extern.Variant>} */
const variants = this.createVariants(audioStreams, videoStreams, timeline);
/** @type {!Array.<shaka.extern.Stream>} */
const textStreams =
manifestDB.streams.filter((streamDB) => this.isText_(streamDB))
.map((streamDB) => this.fromStreamDB_(streamDB, timeline));
/** @type {!Array.<shaka.extern.Stream>} */
const imageStreams =
manifestDB.streams.filter((streamDB) => this.isImage_(streamDB))
.map((streamDB) => this.fromStreamDB_(streamDB, timeline));
const drmInfos = manifestDB.drmInfo ? [manifestDB.drmInfo] : [];
if (manifestDB.drmInfo) {
for (const variant of variants.values()) {
if (variant.audio && variant.audio.encrypted) {
variant.audio.drmInfos = drmInfos;
}
if (variant.video && variant.video.encrypted) {
variant.video.drmInfos = drmInfos;
}
}
}
return {
presentationTimeline: timeline,
minBufferTime: 2,
offlineSessionIds: manifestDB.sessionIds,
variants: Array.from(variants.values()),
textStreams: textStreams,
imageStreams: imageStreams,
sequenceMode: manifestDB.sequenceMode || false,
};
}
/**
* Recreates Variants from audio and video StreamDB collections.
*
* @param {!Array.<!shaka.extern.StreamDB>} audios
* @param {!Array.<!shaka.extern.StreamDB>} videos
* @param {shaka.media.PresentationTimeline} timeline
* @return {!Map.<number, !shaka.extern.Variant>}
*/
createVariants(audios, videos, timeline) {
// Get all the variant ids from all audio and video streams.
/** @type {!Set.<number>} */
const variantIds = new Set();
for (const streamDB of audios) {
for (const id of streamDB.variantIds) {
variantIds.add(id);
}
}
for (const streamDB of videos) {
for (const id of streamDB.variantIds) {
variantIds.add(id);
}
}
/** @type {!Map.<number, shaka.extern.Variant>} */
const variantMap = new Map();
for (const id of variantIds) {
variantMap.set(id, this.createEmptyVariant_(id));
}
// Assign each audio stream to its variants.
for (const audio of audios) {
/** @type {shaka.extern.Stream} */
const stream = this.fromStreamDB_(audio, timeline);
for (const variantId of audio.variantIds) {
const variant = variantMap.get(variantId);
goog.asserts.assert(
!variant.audio, 'A variant should only have one audio stream');
variant.language = stream.language;
variant.primary = variant.primary || stream.primary;
variant.audio = stream;
}
}
// Assign each video stream to its variants.
for (const video of videos) {
/** @type {shaka.extern.Stream} */
const stream = this.fromStreamDB_(video, timeline);
for (const variantId of video.variantIds) {
const variant = variantMap.get(variantId);
goog.asserts.assert(
!variant.video, 'A variant should only have one video stream');
variant.primary = variant.primary || stream.primary;
variant.video = stream;
}
}
return variantMap;
}
/**
* @param {shaka.extern.StreamDB} streamDB
* @param {shaka.media.PresentationTimeline} timeline
* @return {shaka.extern.Stream}
* @private
*/
fromStreamDB_(streamDB, timeline) {
/** @type {!Array.<!shaka.media.SegmentReference>} */
const segments = streamDB.segments.map(
(segment, index) => this.fromSegmentDB_(index, segment));
timeline.notifySegments(segments);
/** @type {!shaka.media.SegmentIndex} */
const segmentIndex = new shaka.media.SegmentIndex(segments);
/** @type {shaka.extern.Stream} */
const stream = {
id: streamDB.id,
originalId: streamDB.originalId,
createSegmentIndex: () => Promise.resolve(),
segmentIndex,
mimeType: streamDB.mimeType,
codecs: streamDB.codecs,
width: streamDB.width || undefined,
height: streamDB.height || undefined,
frameRate: streamDB.frameRate,
pixelAspectRatio: streamDB.pixelAspectRatio,
hdr: streamDB.hdr,
kind: streamDB.kind,
encrypted: streamDB.encrypted,
drmInfos: [],
keyIds: streamDB.keyIds,
language: streamDB.language,
label: streamDB.label,
type: streamDB.type,
primary: streamDB.primary,
trickModeVideo: null,
emsgSchemeIdUris: null,
roles: streamDB.roles,
forced: streamDB.forced,
channelsCount: streamDB.channelsCount,
audioSamplingRate: streamDB.audioSamplingRate,
spatialAudio: streamDB.spatialAudio,
closedCaptions: streamDB.closedCaptions,
tilesLayout: streamDB.tilesLayout,
};
return stream;
}
/**
* @param {number} index
* @param {shaka.extern.SegmentDB} segmentDB
* @return {!shaka.media.SegmentReference}
* @private
*/
fromSegmentDB_(index, segmentDB) {
/** @type {!shaka.offline.OfflineUri} */
const uri = shaka.offline.OfflineUri.segment(
this.mechanism_, this.cell_, segmentDB.dataKey);
const initSegmentReference = segmentDB.initSegmentKey != null ?
this.fromInitSegmentDB_(segmentDB.initSegmentKey) : null;
return new shaka.media.SegmentReference(
segmentDB.startTime,
segmentDB.endTime,
() => [uri.toString()],
/* startByte= */ 0,
/* endByte= */ null,
initSegmentReference,
segmentDB.timestampOffset,
segmentDB.appendWindowStart,
segmentDB.appendWindowEnd,
/* partialReferences= */ [],
segmentDB.tilesLayout || '');
}
/**
* @param {number} key
* @return {!shaka.media.InitSegmentReference}
* @private
*/
fromInitSegmentDB_(key) {
/** @type {!shaka.offline.OfflineUri} */
const uri = shaka.offline.OfflineUri.segment(
this.mechanism_, this.cell_, key);
return new shaka.media.InitSegmentReference(
() => [uri.toString()],
/* startBytes= */ 0,
/* endBytes= */ null );
}
/**
* @param {shaka.extern.StreamDB} streamDB
* @return {boolean}
* @private
*/
isAudio_(streamDB) {
const ContentType = shaka.util.ManifestParserUtils.ContentType;
return streamDB.type == ContentType.AUDIO;
}
/**
* @param {shaka.extern.StreamDB} streamDB
* @return {boolean}
* @private
*/
isVideo_(streamDB) {
const ContentType = shaka.util.ManifestParserUtils.ContentType;
return streamDB.type == ContentType.VIDEO;
}
/**
* @param {shaka.extern.StreamDB} streamDB
* @return {boolean}
* @private
*/
isText_(streamDB) {
const ContentType = shaka.util.ManifestParserUtils.ContentType;
return streamDB.type == ContentType.TEXT;
}
/**
* @param {shaka.extern.StreamDB} streamDB
* @return {boolean}
* @private
*/
isImage_(streamDB) {
const ContentType = shaka.util.ManifestParserUtils.ContentType;
return streamDB.type == ContentType.IMAGE;
}
/**
* Creates an empty Variant.
*
* @param {number} id
* @return {!shaka.extern.Variant}
* @private
*/
createEmptyVariant_(id) {
return {
id: id,
language: '',
disabledUntilTime: 0,
primary: false,
audio: null,
video: null,
bandwidth: 0,
allowedByApplication: true,
allowedByKeySystem: true,
decodingInfos: [],
};
}
};