-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathCapabilities.js
480 lines (404 loc) · 17.6 KB
/
Capabilities.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
/**
* The copyright in this software is being made available under the BSD License,
* included below. This software may be subject to other third party and contributor
* rights, including patent rights, and no such rights are granted under this license.
*
* Copyright (c) 2013, Dash Industry Forum.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* * Neither the name of Dash Industry Forum nor the names of its
* contributors may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
import FactoryMaker from '../../core/FactoryMaker.js';
import Constants from '../constants/Constants.js';
import ProtectionConstants from '../constants/ProtectionConstants.js';
import ObjectUtils from './ObjectUtils.js';
import Debug from '../../core/Debug.js';
export function supportsMediaSource() {
let hasManagedMediaSource = ('ManagedMediaSource' in window)
let hasWebKit = ('WebKitMediaSource' in window);
let hasMediaSource = ('MediaSource' in window);
return (hasManagedMediaSource || hasWebKit || hasMediaSource);
}
function Capabilities() {
let instance,
settings,
protectionController,
testedCodecConfigurations,
encryptedMediaSupported,
logger;
const context = this.context;
const objectUtils = ObjectUtils(context).getInstance();
function setup() {
encryptedMediaSupported = false;
testedCodecConfigurations = [];
logger = Debug(context).getInstance().getLogger(instance);
}
function setConfig(config) {
if (!config) {
return;
}
if (config.settings) {
settings = config.settings;
}
if (config.protectionController) {
protectionController = config.protectionController;
}
}
function setProtectionController(data) {
protectionController = data;
}
function areKeyIdsUsable(mediaInfo) {
if (!protectionController || !mediaInfo || !mediaInfo.normalizedKeyIds || mediaInfo.normalizedKeyIds.size === 0) {
return true
}
return protectionController.areKeyIdsUsable(mediaInfo.normalizedKeyIds)
}
function areKeyIdsExpired(mediaInfo) {
if (!protectionController || !mediaInfo || !mediaInfo.normalizedKeyIds || mediaInfo.normalizedKeyIds.size === 0) {
return false
}
return protectionController.areKeyIdsExpired(mediaInfo.normalizedKeyIds)
}
function isProtectionCompatible(previousStreamInfo, newStreamInfo) {
if (!newStreamInfo) {
return true;
}
return !(!previousStreamInfo.isEncrypted && newStreamInfo.isEncrypted);
}
/**
* Returns whether Encrypted Media Extensions are supported on this
* user agent
*
* @return {boolean} true if EME is supported, false otherwise
*/
function supportsEncryptedMedia() {
return encryptedMediaSupported;
}
/**
* Checks whether SourceBuffer.changeType() is available
* @return {boolean} true is changeType() is available
*/
function supportsChangeType() {
return !!window.SourceBuffer && !!SourceBuffer.prototype && !!SourceBuffer.prototype.changeType;
}
/**
* @param {boolean} value
*/
function setEncryptedMediaSupported(value) {
encryptedMediaSupported = value;
}
/**
* Check if a codec is supported by the MediaSource. We use the MediaCapabilities API or the MSE to check.
* @param {object} basicConfiguration
* @param {string} type
* @return {Promise<>}
*/
function runCodecSupportCheck(basicConfiguration, type) {
if (type !== Constants.AUDIO && type !== Constants.VIDEO) {
return Promise.resolve();
}
const configurationsToTest = _getEnhancedConfigurations(basicConfiguration, type);
if (_canUseMediaCapabilitiesApi(basicConfiguration, type)) {
return _checkCodecWithMediaCapabilities(configurationsToTest);
}
_checkCodecWithMse(configurationsToTest);
return Promise.resolve();
}
/**
* Checks whether a codec is supported according to the previously tested configurations.
* Note that you need to call runCodecSupportCheck() first to populate the testedCodecConfigurations array.
* This function only validates codec support based on previously tested configurations.
* @param basicConfiguration
* @param type
* @returns {*|boolean}
*/
function isCodecSupportedBasedOnTestedConfigurations(basicConfiguration, type) {
if (!basicConfiguration || !basicConfiguration.codec || (basicConfiguration.isSupported === false)) {
return false;
}
const configurationsToTest = _getEnhancedConfigurations(basicConfiguration, type);
const testedConfigurations = configurationsToTest
.map((config) => {
return _getTestedCodecConfiguration(config, type);
})
.filter((config) => {
return config !== null && config !== undefined;
})
if (testedConfigurations && testedConfigurations.length > 0) {
return _isConfigSupported(testedConfigurations)
}
return true
}
/**
* MediaCapabilitiesAPI throws an error if one of the attribute is missing. We only use it if we have all required information.
* @return {boolean}
* @private
*/
function _canUseMediaCapabilitiesApi(basicConfiguration, type) {
return _isMediaCapabilitiesApiSupported() && ((basicConfiguration.codec && type === Constants.AUDIO) || (type === Constants.VIDEO && basicConfiguration.codec && basicConfiguration.width && basicConfiguration.height && basicConfiguration.bitrate && basicConfiguration.framerate));
}
function _isMediaCapabilitiesApiSupported() {
return settings.get().streaming.capabilities.useMediaCapabilitiesApi && navigator.mediaCapabilities && navigator.mediaCapabilities.decodingInfo
}
/**
* Check codec support using the MSE
* @param {object} configurationsToTest
* @private
*/
function _checkCodecWithMse(configurationsToTest) {
if (!configurationsToTest || !configurationsToTest.length) {
return;
}
// We only need one config here as we can not add any DRM configuration to the test
const configurationToTest = configurationsToTest[0];
const alreadyTestedConfiguration = _getTestedCodecConfiguration(configurationToTest);
if (alreadyTestedConfiguration) {
return
}
let decodingInfo = {
supported: false
}
if ('ManagedMediaSource' in window && ManagedMediaSource.isTypeSupported(configurationToTest.mediaSourceCodecString)) {
decodingInfo.supported = true;
} else if ('MediaSource' in window && MediaSource.isTypeSupported(configurationToTest.mediaSourceCodecString)) {
decodingInfo.supported = true;
} else if ('WebKitMediaSource' in window && WebKitMediaSource.isTypeSupported(configurationToTest.mediaSourceCodecString)) {
decodingInfo.supported = true;
}
configurationToTest.decodingInfo = decodingInfo;
testedCodecConfigurations.push(configurationToTest);
}
/**
* Check codec support using the MediaCapabilities API
* @param {object} configurationsToTest
* @return {Promise<boolean>}
* @private
*/
function _checkCodecWithMediaCapabilities(configurationsToTest) {
return new Promise((resolve) => {
if (!configurationsToTest || configurationsToTest.length === 0) {
resolve();
return;
}
const promises = configurationsToTest.map((configuration) => {
return _checkSingleConfigurationWithMediaCapabilities(configuration);
})
Promise.allSettled(promises)
.then(() => {
resolve();
})
.catch((e) => {
logger.error(e);
resolve();
})
});
}
function _getEnhancedConfigurations(inputConfig, type) {
let configuration
if (type === Constants.VIDEO) {
configuration = _getGenericMediaCapabilitiesVideoConfig(inputConfig)
} else if (type === Constants.AUDIO) {
configuration = _getGenericMediaCapabilitiesAudioConfig(inputConfig)
}
configuration[type].contentType = inputConfig.codec;
configuration[type].bitrate = parseInt(inputConfig.bitrate);
configuration.type = 'media-source';
let mediaSourceCodecString = inputConfig.codec;
if (inputConfig.width && inputConfig.height) {
mediaSourceCodecString += ';width="' + inputConfig.width + '";height="' + inputConfig.height + '"';
}
configuration.mediaSourceCodecString = mediaSourceCodecString;
return _enhanceGenericConfigurationWithKeySystemConfiguration(configuration, inputConfig, type)
}
function _enhanceGenericConfigurationWithKeySystemConfiguration(genericConfiguration, inputConfig, type) {
if (!inputConfig || !inputConfig.keySystemsMetadata || inputConfig.keySystemsMetadata.length === 0) {
return [genericConfiguration];
}
return inputConfig.keySystemsMetadata.map((keySystemMetadata) => {
const curr = { ...genericConfiguration };
if (keySystemMetadata.ks) {
curr.keySystemConfiguration = {};
if (keySystemMetadata.ks.systemString) {
curr.keySystemConfiguration.keySystem = keySystemMetadata.ks.systemString;
}
let robustnessLevel = ''
if (keySystemMetadata.ks.systemString === ProtectionConstants.WIDEVINE_KEYSTEM_STRING) {
robustnessLevel = ProtectionConstants.ROBUSTNESS_STRINGS.WIDEVINE.SW_SECURE_CRYPTO;
}
const protData = keySystemMetadata.protData
const audioRobustness = (protData && protData.audioRobustness && protData.audioRobustness.length > 0) ? protData.audioRobustness : robustnessLevel;
const videoRobustness = (protData && protData.videoRobustness && protData.videoRobustness.length > 0) ? protData.videoRobustness : robustnessLevel;
if (type === Constants.AUDIO) {
curr.keySystemConfiguration[type] = { robustness: audioRobustness }
} else if (type === Constants.VIDEO) {
curr.keySystemConfiguration[type] = { robustness: videoRobustness }
}
}
return curr
})
}
function _checkSingleConfigurationWithMediaCapabilities(configuration) {
return new Promise((resolve) => {
const alreadyTestedConfiguration = _getTestedCodecConfiguration(configuration);
if (alreadyTestedConfiguration) {
resolve();
return
}
navigator.mediaCapabilities.decodingInfo(configuration)
.then((decodingInfo) => {
configuration.decodingInfo = decodingInfo;
testedCodecConfigurations.push(configuration);
resolve();
})
.catch((e) => {
configuration.decodingInfo = { supported: false };
testedCodecConfigurations.push(configuration);
logger.error(e);
resolve();
})
})
}
function _isConfigSupported(testedConfigurations) {
return testedConfigurations.some((testedConfiguration) => {
return testedConfiguration && testedConfiguration.decodingInfo && testedConfiguration.decodingInfo.supported
});
}
function _getTestedCodecConfiguration(configuration) {
if (!testedCodecConfigurations || testedCodecConfigurations.length === 0 || !configuration) {
return
}
return testedCodecConfigurations.find((current) => {
const audioEqual = _isConfigEqual(configuration, current, Constants.AUDIO);
const videoEqual = _isConfigEqual(configuration, current, Constants.VIDEO);
const keySystemEqual = _isConfigEqual(configuration, current, 'keySystemConfiguration');
return audioEqual && videoEqual && keySystemEqual
})
}
function _isConfigEqual(configuration, current, attribute) {
// Config not present in both of them
if (!configuration[attribute] && !current[attribute]) {
return true
}
// Config present in both we need to compare
if (configuration[attribute] && current[attribute]) {
return objectUtils.areEqual(configuration[attribute], current[attribute])
}
return false
}
function _getGenericMediaCapabilitiesVideoConfig(inputConfig) {
const configuration = {
video: {}
};
if (!inputConfig) {
return configuration;
}
if (inputConfig.width) {
configuration.video.width = inputConfig.width;
}
if (inputConfig.height) {
configuration.video.height = inputConfig.height;
}
if (inputConfig.framerate) {
configuration.video.framerate = parseFloat(inputConfig.framerate)
}
if (inputConfig.hdrMetadataType) {
configuration.video.hdrMetadataType = inputConfig.hdrMetadataType;
}
if (inputConfig.colorGamut) {
configuration.video.colorGamut = inputConfig.colorGamut;
}
if (inputConfig.transferFunction) {
configuration.video.transferFunction = inputConfig.transferFunction;
}
return configuration
}
function _getGenericMediaCapabilitiesAudioConfig(inputConfig) {
const configuration = {
audio: {}
};
if (inputConfig.samplerate) {
configuration.audio.samplerate = inputConfig.samplerate;
}
return configuration
}
/**
* Add additional descriptors to list of descriptors,
* avoid duplicated entries
* @param {array} props
* @param {array} newProps
* @return {array}
* @private
*/
function _addProperties(props, newProps) {
props = props.filter(p => {
return !(p.schemeIdUri && (newProps.some(np => np.schemeIdUri === p.schemeIdUri)));
});
props.push(...newProps);
return props;
}
/**
* Check if a specific EssentialProperty is supported
* @param {DescriptorType} ep
* @return {boolean}
*/
function supportsEssentialProperty(ep) {
let supportedEssentialProps = settings.get().streaming.capabilities.supportedEssentialProperties;
// we already took care of these descriptors with the codecs check
// let's bypass them here
if (settings.get().streaming.capabilities.useMediaCapabilitiesApi && settings.get().streaming.capabilities.filterVideoColorimetryEssentialProperties) {
supportedEssentialProps = _addProperties(supportedEssentialProps,
[
{ schemeIdUri: Constants.COLOUR_PRIMARIES_SCHEME_ID_URI },
{ schemeIdUri: Constants.MATRIX_COEFFICIENTS_SCHEME_ID_URI },
{ schemeIdUri: Constants.TRANSFER_CHARACTERISTICS_SCHEME_ID_URI }
]
);
}
if (settings.get().streaming.capabilities.useMediaCapabilitiesApi && settings.get().streaming.capabilities.filterHDRMetadataFormatEssentialProperties) {
supportedEssentialProps = _addProperties(supportedEssentialProps, [{ schemeIdUri: Constants.HDR_METADATA_FORMAT_SCHEME_ID_URI }]);
}
try {
return ep.inArray(supportedEssentialProps);
} catch (e) {
return true;
}
}
instance = {
areKeyIdsExpired,
areKeyIdsUsable,
isCodecSupportedBasedOnTestedConfigurations,
isProtectionCompatible,
runCodecSupportCheck,
setConfig,
setEncryptedMediaSupported,
setProtectionController,
supportsChangeType,
supportsEncryptedMedia,
supportsEssentialProperty,
supportsMediaSource,
};
setup();
return instance;
}
Capabilities.__dashjs_factory_name = 'Capabilities';
export default FactoryMaker.getSingletonFactory(Capabilities);