-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
media_capabilities.js
415 lines (375 loc) · 14.6 KB
/
media_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
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.polyfill.MediaCapabilities');
goog.require('shaka.log');
goog.require('shaka.drm.DrmUtils');
goog.require('shaka.media.Capabilities');
goog.require('shaka.polyfill');
goog.require('shaka.util.MimeUtils');
goog.require('shaka.util.Platform');
/**
* @summary A polyfill to provide navigator.mediaCapabilities on all browsers.
* This is necessary for Tizen 3, Xbox One and possibly others we have yet to
* discover.
* @export
*/
shaka.polyfill.MediaCapabilities = class {
/**
* Install the polyfill if needed.
* @suppress {const}
* @export
*/
static install() {
// We can enable MediaCapabilities in Android and Fuchsia devices, but not
// in Linux devices because the implementation is buggy.
// Since MediaCapabilities implementation is buggy in Apple browsers, we
// should always install polyfill for Apple browsers.
// See: https://github.com/shaka-project/shaka-player/issues/3530
// TODO: re-evaluate MediaCapabilities in the future versions of Apple
// Browsers.
// Since MediaCapabilities implementation is buggy in PS5 browsers, we
// should always install polyfill for PS5 browsers.
// See: https://github.com/shaka-project/shaka-player/issues/3582
// TODO: re-evaluate MediaCapabilities in the future versions of PS5
// Browsers.
// Since MediaCapabilities implementation does not exist in PS4 browsers, we
// should always install polyfill.
// Since MediaCapabilities implementation is buggy in Tizen browsers, we
// should always install polyfill for Tizen browsers.
// Since MediaCapabilities implementation is buggy in WebOS browsers, we
// should always install polyfill for WebOS browsers.
// Since MediaCapabilities implementation is buggy in EOS browsers, we
// should always install polyfill for EOS browsers.
// Since MediaCapabilities implementation is buggy in Hisense browsers, we
// should always install polyfill for Hisense browsers.
let canUseNativeMCap = true;
if (shaka.util.Platform.isChromecast() &&
!shaka.util.Platform.isAndroidCastDevice() &&
!shaka.util.Platform.isFuchsiaCastDevice()) {
canUseNativeMCap = false;
}
if (shaka.util.Platform.isApple() ||
shaka.util.Platform.isPS5() ||
shaka.util.Platform.isPS4() ||
shaka.util.Platform.isWebOS() ||
shaka.util.Platform.isTizen() ||
shaka.util.Platform.isEOS() ||
shaka.util.Platform.isHisense() ||
shaka.util.Platform.isComcastX1()) {
canUseNativeMCap = false;
}
if (canUseNativeMCap && navigator.mediaCapabilities) {
shaka.log.info(
'MediaCapabilities: Native mediaCapabilities support found.');
return;
}
shaka.log.info('MediaCapabilities: install');
if (!navigator.mediaCapabilities) {
navigator.mediaCapabilities = /** @type {!MediaCapabilities} */ ({});
}
// Keep the patched MediaCapabilities object from being garbage-collected in
// Safari.
// See https://github.com/shaka-project/shaka-player/issues/3696#issuecomment-1009472718
shaka.polyfill.MediaCapabilities.originalMcap =
navigator.mediaCapabilities;
navigator.mediaCapabilities.decodingInfo =
shaka.polyfill.MediaCapabilities.decodingInfo_;
}
/**
* @param {!MediaDecodingConfiguration} mediaDecodingConfig
* @return {!Promise.<!MediaCapabilitiesDecodingInfo>}
* @private
*/
static async decodingInfo_(mediaDecodingConfig) {
/** @type {!MediaCapabilitiesDecodingInfo} */
const res = {
supported: false,
powerEfficient: true,
smooth: true,
keySystemAccess: null,
configuration: mediaDecodingConfig,
};
const videoConfig = mediaDecodingConfig['video'];
const audioConfig = mediaDecodingConfig['audio'];
if (mediaDecodingConfig.type == 'media-source') {
if (!shaka.util.Platform.supportsMediaSource()) {
return res;
}
if (videoConfig) {
const isSupported =
await shaka.polyfill.MediaCapabilities.checkVideoSupport_(
videoConfig);
if (!isSupported) {
return res;
}
}
if (audioConfig) {
const isSupported =
shaka.polyfill.MediaCapabilities.checkAudioSupport_(audioConfig);
if (!isSupported) {
return res;
}
}
} else if (mediaDecodingConfig.type == 'file') {
if (videoConfig) {
const contentType = videoConfig.contentType;
const isSupported = shaka.util.Platform.supportsMediaType(contentType);
if (!isSupported) {
return res;
}
}
if (audioConfig) {
const contentType = audioConfig.contentType;
const isSupported = shaka.util.Platform.supportsMediaType(contentType);
if (!isSupported) {
return res;
}
}
} else {
// Otherwise not supported.
return res;
}
if (!mediaDecodingConfig.keySystemConfiguration) {
// The variant is supported if it's unencrypted.
res.supported = true;
return res;
} else {
const mcapKeySystemConfig = mediaDecodingConfig.keySystemConfiguration;
const keySystemAccess =
await shaka.polyfill.MediaCapabilities.checkDrmSupport_(
videoConfig, audioConfig, mcapKeySystemConfig);
if (keySystemAccess) {
res.supported = true;
res.keySystemAccess = keySystemAccess;
}
}
return res;
}
/**
* @param {!VideoConfiguration} videoConfig The 'video' field of the
* MediaDecodingConfiguration.
* @return {!Promise<boolean>}
* @private
*/
static async checkVideoSupport_(videoConfig) {
// Use 'shaka.media.Capabilities.isTypeSupported' to check if
// the stream is supported.
// Cast platforms will additionally check canDisplayType(), which
// accepts extended MIME type parameters.
// See: https://github.com/shaka-project/shaka-player/issues/4726
if (shaka.util.Platform.isChromecast()) {
const isSupported =
await shaka.polyfill.MediaCapabilities.canCastDisplayType_(
videoConfig);
return isSupported;
} else if (shaka.util.Platform.isTizen()) {
let extendedType = videoConfig.contentType;
if (videoConfig.width && videoConfig.height) {
extendedType += `; width=${videoConfig.width}`;
extendedType += `; height=${videoConfig.height}`;
}
if (videoConfig.framerate) {
extendedType += `; framerate=${videoConfig.framerate}`;
}
if (videoConfig.bitrate) {
extendedType += `; bitrate=${videoConfig.bitrate}`;
}
return shaka.media.Capabilities.isTypeSupported(extendedType);
}
return shaka.media.Capabilities.isTypeSupported(videoConfig.contentType);
}
/**
* @param {!AudioConfiguration} audioConfig The 'audio' field of the
* MediaDecodingConfiguration.
* @return {boolean}
* @private
*/
static checkAudioSupport_(audioConfig) {
let extendedType = audioConfig.contentType;
if (shaka.util.Platform.isChromecast() && audioConfig.spatialRendering) {
extendedType += '; spatialRendering=true';
}
return shaka.media.Capabilities.isTypeSupported(extendedType);
}
/**
* @param {VideoConfiguration} videoConfig The 'video' field of the
* MediaDecodingConfiguration.
* @param {AudioConfiguration} audioConfig The 'audio' field of the
* MediaDecodingConfiguration.
* @param {!MediaCapabilitiesKeySystemConfiguration} mcapKeySystemConfig The
* 'keySystemConfiguration' field of the MediaDecodingConfiguration.
* @return {Promise<MediaKeySystemAccess>}
* @private
*/
static async checkDrmSupport_(videoConfig, audioConfig, mcapKeySystemConfig) {
const MimeUtils = shaka.util.MimeUtils;
const audioCapabilities = [];
const videoCapabilities = [];
if (mcapKeySystemConfig.audio) {
const capability = {
robustness: mcapKeySystemConfig.audio.robustness || '',
contentType: audioConfig.contentType,
};
// Some Tizen devices seem to misreport AC-3 support, but correctly
// report EC-3 support. So query EC-3 as a fallback for AC-3.
// See https://github.com/shaka-project/shaka-player/issues/2989 for
// details.
if (shaka.util.Platform.isTizen() &&
audioConfig.contentType.includes('codecs="ac-3"')) {
capability.contentType = 'audio/mp4; codecs="ec-3"';
}
if (mcapKeySystemConfig.audio.encryptionScheme) {
capability.encryptionScheme =
mcapKeySystemConfig.audio.encryptionScheme;
}
audioCapabilities.push(capability);
}
if (mcapKeySystemConfig.video) {
const capability = {
robustness: mcapKeySystemConfig.video.robustness || '',
contentType: videoConfig.contentType,
};
if (mcapKeySystemConfig.video.encryptionScheme) {
capability.encryptionScheme =
mcapKeySystemConfig.video.encryptionScheme;
}
videoCapabilities.push(capability);
}
/** @type {MediaKeySystemConfiguration} */
const mediaKeySystemConfig = {
initDataTypes: [mcapKeySystemConfig.initDataType],
distinctiveIdentifier: mcapKeySystemConfig.distinctiveIdentifier,
persistentState: mcapKeySystemConfig.persistentState,
sessionTypes: mcapKeySystemConfig.sessionTypes,
};
// Only add audio / video capabilities if they have valid data.
// Otherwise the query will fail.
if (audioCapabilities.length) {
mediaKeySystemConfig.audioCapabilities = audioCapabilities;
}
if (videoCapabilities.length) {
mediaKeySystemConfig.videoCapabilities = videoCapabilities;
}
const videoMimeType = videoConfig ? videoConfig.contentType : '';
const audioMimeType = audioConfig ? audioConfig.contentType : '';
const videoCodec = MimeUtils.getBasicType(videoMimeType) + ';' +
MimeUtils.getCodecBase(videoMimeType);
const audioCodec = MimeUtils.getBasicType(audioMimeType) + ';' +
MimeUtils.getCodecBase(audioMimeType);
const keySystem = mcapKeySystemConfig.keySystem;
/** @type {MediaKeySystemAccess} */
let keySystemAccess = null;
try {
if (shaka.drm.DrmUtils.hasMediaKeySystemAccess(
videoCodec, audioCodec, keySystem)) {
keySystemAccess = shaka.drm.DrmUtils.getMediaKeySystemAccess(
videoCodec, audioCodec, keySystem);
} else {
keySystemAccess = await navigator.requestMediaKeySystemAccess(
mcapKeySystemConfig.keySystem, [mediaKeySystemConfig]);
shaka.drm.DrmUtils.setMediaKeySystemAccess(
videoCodec, audioCodec, keySystem, keySystemAccess);
}
} catch (e) {
shaka.log.info('navigator.requestMediaKeySystemAccess failed.');
}
return keySystemAccess;
}
/**
* Checks if the given media parameters of the video or audio streams are
* supported by the Cast platform.
* @param {!VideoConfiguration} videoConfig The 'video' field of the
* MediaDecodingConfiguration.
* @return {!Promise<boolean>} `true` when the stream can be displayed on a
* Cast device.
* @private
*/
static async canCastDisplayType_(videoConfig) {
if (!(window.cast &&
cast.__platform__ && cast.__platform__.canDisplayType)) {
shaka.log.warning('Expected cast APIs to be available! Falling back to ' +
'shaka.media.Capabilities.isTypeSupported() for type support.');
return shaka.media.Capabilities.isTypeSupported(videoConfig.contentType);
}
let displayType = videoConfig.contentType;
if (videoConfig.width && videoConfig.height) {
// All Chromecast can support 720p videos
if (videoConfig.width > 1280 || videoConfig.height > 720) {
displayType +=
`; width=${videoConfig.width}; height=${videoConfig.height}`;
}
}
if (videoConfig.framerate) {
// All Chromecast can support a framerate of 24, 25 or 30.
const framerate = Math.round(videoConfig.framerate);
if (framerate < 24 || framerate > 30) {
displayType += `; framerate=${videoConfig.framerate}`;
}
}
// Don't trust Closure types here. Although transferFunction is string or
// undefined, we don't want to count on the input type. A switch statement
// will, however, differentiate between null and undefined. So we default
// to a blank string.
const transferFunction = videoConfig.transferFunction || '';
// Based on internal sources. Googlers, see go/cast-hdr-queries for source.
switch (transferFunction) {
// The empty case falls through to SDR.
case '':
// These are the only 3 values defined by MCap as of November 2024.
case 'srgb':
// https://en.wikipedia.org/wiki/Standard-dynamic-range_video
// https://en.wikipedia.org/wiki/SRGB
// https://en.wikipedia.org/wiki/Rec._709
// This is SDR, standardized in BT 709.
// The platform recognizes "eotf=bt709", but we can also omit it.
break;
case 'pq':
// https://en.wikipedia.org/wiki/Perceptual_quantizer
// This HDR transfer function is standardized as SMPTE ST 2084.
displayType += '; eotf=smpte2084';
break;
case 'hlg':
// https://en.wikipedia.org/wiki/Hybrid_log%E2%80%93gamma
// This HDR transfer function is standardized as ARIB STD-B67.
displayType += '; eotf=arib-std-b67';
break;
default:
// An unrecognized transfer function. Reject this query.
return false;
}
let result = false;
if (displayType in shaka.polyfill.MediaCapabilities
.memoizedCanDisplayTypeRequests_) {
result = shaka.polyfill.MediaCapabilities
.memoizedCanDisplayTypeRequests_[displayType];
} else {
result = await cast.__platform__.canDisplayType(displayType);
shaka.polyfill.MediaCapabilities
.memoizedCanDisplayTypeRequests_[displayType] = result;
}
return result;
}
};
/**
* A copy of the MediaCapabilities instance, to prevent Safari from
* garbage-collecting the polyfilled method on it. We make it public and export
* it to ensure that it is not stripped out by the compiler.
*
* @type {MediaCapabilities}
* @export
*/
shaka.polyfill.MediaCapabilities.originalMcap = null;
/**
* A cache that stores the canDisplayType result of calling
* `cast.__platform__.canDisplayType`.
*
* @type {(Object<(!string), (!boolean)>)}
* @export
*/
shaka.polyfill.MediaCapabilities.memoizedCanDisplayTypeRequests_ = {};
// Install at a lower priority than MediaSource polyfill, so that we have
// MediaSource available first.
shaka.polyfill.register(shaka.polyfill.MediaCapabilities.install, -1);