-
Notifications
You must be signed in to change notification settings - Fork 125
/
manifest_parser.ts
324 lines (306 loc) · 11.6 KB
/
manifest_parser.ts
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
/**
* Copyright 2015 CANAL+ Group
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import config from "../../config";
import { formatError } from "../../errors";
import features from "../../features";
import log from "../../log";
import Manifest from "../../manifest";
import {
IDashParserResponse,
ILoadedResource,
} from "../../parsers/manifest/dash/parsers_types";
import objectAssign from "../../utils/object_assign";
import request from "../../utils/request";
import {
strToUtf8,
utf8ToStr,
} from "../../utils/string_parsing";
import { CancellationSignal } from "../../utils/task_canceller";
import {
IManifestParserOptions,
IManifestParserRequestScheduler,
IManifestParserResult,
IRequestedData,
ITransportOptions,
} from "../types";
export default function generateManifestParser(
options : ITransportOptions
) : (
manifestData : IRequestedData<unknown>,
parserOptions : IManifestParserOptions,
onWarnings : (warnings : Error[]) => void,
cancelSignal : CancellationSignal,
scheduleRequest : IManifestParserRequestScheduler
) => IManifestParserResult | Promise<IManifestParserResult>
{
const { aggressiveMode,
referenceDateTime } = options;
const serverTimeOffset = options.serverSyncInfos !== undefined ?
options.serverSyncInfos.serverTimestamp - options.serverSyncInfos.clientTime :
undefined;
return function manifestParser(
manifestData : IRequestedData<unknown>,
parserOptions : IManifestParserOptions,
onWarnings : (warnings : Error[]) => void,
cancelSignal : CancellationSignal,
scheduleRequest : IManifestParserRequestScheduler
) : IManifestParserResult | Promise<IManifestParserResult> {
const { responseData } = manifestData;
const argClockOffset = parserOptions.externalClockOffset;
const url = manifestData.url ?? parserOptions.originalUrl;
const optAggressiveMode = aggressiveMode === true;
const externalClockOffset = serverTimeOffset ?? argClockOffset;
const unsafelyBaseOnPreviousManifest = parserOptions.unsafeMode ?
parserOptions.previousManifest :
null;
const dashParserOpts = { aggressiveMode: optAggressiveMode,
unsafelyBaseOnPreviousManifest,
url,
referenceDateTime,
externalClockOffset };
const parsers = features.dashParsers;
if (parsers.wasm === null ||
parsers.wasm.status === "uninitialized" ||
parsers.wasm.status === "failure")
{
log.debug("DASH: WASM MPD Parser not initialized. Running JS one.");
return runDefaultJsParser();
} else {
const manifestAB = getManifestAsArrayBuffer(responseData);
if (!doesXmlSeemsUtf8Encoded(manifestAB)) {
log.info("DASH: MPD doesn't seem to be UTF-8-encoded. " +
"Running JS parser instead of the WASM one.");
return runDefaultJsParser();
}
if (parsers.wasm.status === "initialized") {
log.debug("DASH: Running WASM MPD Parser.");
const parsed = parsers.wasm.runWasmParser(manifestAB, dashParserOpts);
return processMpdParserResponse(parsed);
} else {
log.debug("DASH: Awaiting WASM initialization before parsing the MPD.");
const initProm = parsers.wasm.waitForInitialization()
.catch(() => { /* ignore errors, we will check the status later */ });
return initProm.then(() => {
if (parsers.wasm === null || parsers.wasm.status !== "initialized") {
log.warn("DASH: WASM MPD parser initialization failed. " +
"Running JS parser instead");
return runDefaultJsParser();
}
log.debug("DASH: Running WASM MPD Parser.");
const parsed = parsers.wasm.runWasmParser(manifestAB, dashParserOpts);
return processMpdParserResponse(parsed);
});
}
}
/**
* Parse the MPD through the default JS-written parser (as opposed to the
* WebAssembly one).
* If it is not defined, throws.
* @returns {Observable}
*/
function runDefaultJsParser() {
if (parsers.js === null) {
throw new Error("No MPD parser is imported");
}
const manifestDoc = getManifestAsDocument(responseData);
const parsedManifest = parsers.js(manifestDoc, dashParserOpts);
return processMpdParserResponse(parsedManifest);
}
/**
* Process return of one of the MPD parser.
* If it asks for a resource, load it then continue.
* @param {Object} parserResponse - Response returned from a MPD parser.
* @returns {Observable}
*/
function processMpdParserResponse(
parserResponse : IDashParserResponse<string> | IDashParserResponse<ArrayBuffer>
) : IManifestParserResult | Promise<IManifestParserResult> {
if (parserResponse.type === "done") {
if (parserResponse.value.warnings.length > 0) {
onWarnings(parserResponse.value.warnings);
}
if (cancelSignal.isCancelled) {
return Promise.reject(cancelSignal.cancellationError);
}
const manifest = new Manifest(parserResponse.value.parsed, options);
return { manifest, url };
}
const { value } = parserResponse;
const externalResources = value.urls.map(resourceUrl => {
return scheduleRequest(() => {
const defaultTimeout = config.getCurrent().DEFAULT_REQUEST_TIMEOUT;
return value.format === "string" ? request({ url: resourceUrl,
responseType: "text",
timeout: defaultTimeout,
cancelSignal }) :
request({ url: resourceUrl,
responseType: "arraybuffer",
timeout: defaultTimeout,
cancelSignal });
}).then((res) => {
if (value.format === "string") {
if (typeof res.responseData !== "string") {
throw new Error("External DASH resources should have been a string");
}
return objectAssign(res, {
responseData: {
success: true as const,
data: res.responseData },
});
} else {
if (!(res.responseData instanceof ArrayBuffer)) {
throw new Error("External DASH resources should have been ArrayBuffers");
}
return objectAssign(res, {
responseData: {
success: true as const,
data: res.responseData },
});
}
}, (err) => {
const error = formatError(err, {
defaultCode: "PIPELINE_PARSE_ERROR",
defaultReason: "An unknown error occured when parsing ressources.",
});
return objectAssign({}, {
size: undefined,
requestDuration: undefined,
responseData: {
success: false as const,
error },
});
});
});
return Promise.all(externalResources).then(loadedResources => {
if (value.format === "string") {
assertLoadedResourcesFormatString(loadedResources);
return processMpdParserResponse(value.continue(loadedResources));
} else {
assertLoadedResourcesFormatArrayBuffer(loadedResources);
return processMpdParserResponse(value.continue(loadedResources));
}
});
}
};
}
/**
* Throw if the given input is not in the expected format.
* Allows to enforce runtime type-checking as compile-time type-checking here is
* difficult to enforce.
*
* @param loadedResource
* @returns
*/
function assertLoadedResourcesFormatString(
loadedResources : Array<ILoadedResource<string | ArrayBuffer>>
) : asserts loadedResources is Array<ILoadedResource<string>> {
if (__ENVIRONMENT__.CURRENT_ENV === __ENVIRONMENT__.PRODUCTION as number) {
return;
}
loadedResources.forEach((loadedResource) => {
const { responseData } = loadedResource;
if (responseData.success && typeof responseData.data === "string") {
return;
} else if (!responseData.success) {
return;
}
throw new Error("Invalid data given to the LoadedRessource");
});
}
/**
* Throw if the given input is not in the expected format.
* Allows to enforce runtime type-checking as compile-time type-checking here is
* difficult to enforce.
*
* @param loadedResource
* @returns
*/
function assertLoadedResourcesFormatArrayBuffer(
loadedResources : Array<ILoadedResource<string | ArrayBuffer>>
) : asserts loadedResources is Array<ILoadedResource<ArrayBuffer>> {
if (__ENVIRONMENT__.CURRENT_ENV === __ENVIRONMENT__.PRODUCTION as number) {
return;
}
loadedResources.forEach((loadedResource) => {
const { responseData } = loadedResource;
if (responseData.success && responseData.data instanceof ArrayBuffer) {
return;
} else if (!responseData.success) {
return;
}
throw new Error("Invalid data given to the LoadedRessource");
});
}
/**
* Try to convert a Manifest from an unknown format to a `Document` format.
* Useful to exploit DOM-parsing APIs to quickly parse an XML Manifest.
*
* Throws if the format cannot be converted.
* @param {*} manifestSrc
* @returns {Document}
*/
function getManifestAsDocument(manifestSrc : unknown) : Document {
if (manifestSrc instanceof ArrayBuffer) {
return new DOMParser()
.parseFromString(utf8ToStr(new Uint8Array(manifestSrc)), "text/xml");
} else if (typeof manifestSrc === "string") {
return new DOMParser().parseFromString(manifestSrc, "text/xml");
} else if (manifestSrc instanceof Document) {
return manifestSrc;
} else {
throw new Error("DASH Manifest Parser: Unrecognized Manifest format");
}
}
/**
* Try to convert a Manifest from an unknown format to an `ArrayBuffer` format.
* Throws if the format cannot be converted.
* @param {*} manifestSrc
* @returns {ArrayBuffer}
*/
function getManifestAsArrayBuffer(manifestSrc : unknown) : ArrayBuffer {
if (manifestSrc instanceof ArrayBuffer) {
return manifestSrc;
} else if (typeof manifestSrc === "string") {
return strToUtf8(manifestSrc).buffer;
} else if (manifestSrc instanceof Document) {
return strToUtf8(manifestSrc.documentElement.innerHTML).buffer;
} else {
throw new Error("DASH Manifest Parser: Unrecognized Manifest format");
}
}
/**
* Returns true if the given XML appears to be encoded in UTF-8.
*
* For now, this function can return a lot of false positives, but it should
* mostly work with real use cases.
* @param {ArrayBuffer} xmlData
* @returns {boolean}
*/
function doesXmlSeemsUtf8Encoded(
xmlData : ArrayBuffer
) : boolean {
const dv = new DataView(xmlData);
if (dv.getUint16(0) === 0xEFBB && dv.getUint8(2) === 0XBF) {
// (UTF-8 BOM)
return true;
} else if (dv.getUint16(0) === 0xFEFF || dv.getUint16(0) === 0xFFFe) {
// (UTF-16 BOM)
return false;
}
// TODO check encoding from request mimeType and text declaration?
// https://www.w3.org/TR/xml/#sec-TextDecl
return true;
}