Source: lib/util/stream_utils.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.util.StreamUtils');
  7. goog.require('goog.asserts');
  8. goog.require('shaka.log');
  9. goog.require('shaka.media.Capabilities');
  10. goog.require('shaka.text.TextEngine');
  11. goog.require('shaka.util.Functional');
  12. goog.require('shaka.util.LanguageUtils');
  13. goog.require('shaka.util.ManifestParserUtils');
  14. goog.require('shaka.util.MimeUtils');
  15. goog.require('shaka.util.MultiMap');
  16. goog.require('shaka.util.ObjectUtils');
  17. goog.require('shaka.util.Platform');
  18. goog.requireType('shaka.media.DrmEngine');
  19. /**
  20. * @summary A set of utility functions for dealing with Streams and Manifests.
  21. * @export
  22. */
  23. shaka.util.StreamUtils = class {
  24. /**
  25. * In case of multiple usable codecs, choose one based on lowest average
  26. * bandwidth and filter out the rest.
  27. * Also filters out variants that have too many audio channels.
  28. * @param {!shaka.extern.Manifest} manifest
  29. * @param {!Array.<string>} preferredVideoCodecs
  30. * @param {!Array.<string>} preferredAudioCodecs
  31. * @param {!Array.<string>} preferredDecodingAttributes
  32. */
  33. static chooseCodecsAndFilterManifest(manifest, preferredVideoCodecs,
  34. preferredAudioCodecs, preferredDecodingAttributes) {
  35. const StreamUtils = shaka.util.StreamUtils;
  36. const MimeUtils = shaka.util.MimeUtils;
  37. let variants = manifest.variants;
  38. // To start, choose the codecs based on configured preferences if available.
  39. if (preferredVideoCodecs.length || preferredAudioCodecs.length) {
  40. variants = StreamUtils.choosePreferredCodecs(variants,
  41. preferredVideoCodecs, preferredAudioCodecs);
  42. }
  43. if (preferredDecodingAttributes.length) {
  44. // group variants by resolution and choose preferred variants only
  45. /** @type {!shaka.util.MultiMap.<shaka.extern.Variant>} */
  46. const variantsByResolutionMap = new shaka.util.MultiMap();
  47. for (const variant of variants) {
  48. variantsByResolutionMap
  49. .push(String(variant.video.width || 0), variant);
  50. }
  51. const bestVariants = [];
  52. variantsByResolutionMap.forEach((width, variantsByResolution) => {
  53. let highestMatch = 0;
  54. let matchingVariants = [];
  55. for (const variant of variantsByResolution) {
  56. const matchCount = preferredDecodingAttributes.filter(
  57. (attribute) => variant.decodingInfos[0][attribute],
  58. ).length;
  59. if (matchCount > highestMatch) {
  60. highestMatch = matchCount;
  61. matchingVariants = [variant];
  62. } else if (matchCount == highestMatch) {
  63. matchingVariants.push(variant);
  64. }
  65. }
  66. bestVariants.push(...matchingVariants);
  67. });
  68. variants = bestVariants;
  69. }
  70. const audioStreamsSet = new Set();
  71. const videoStreamsSet = new Set();
  72. for (const variant of variants) {
  73. if (variant.audio) {
  74. audioStreamsSet.add(variant.audio);
  75. }
  76. if (variant.video) {
  77. videoStreamsSet.add(variant.video);
  78. }
  79. }
  80. const audioStreams = Array.from(audioStreamsSet).sort((v1, v2) => {
  81. return v1.bandwidth - v2.bandwidth;
  82. });
  83. const validAudioIds = [];
  84. const validAudioStreamsMap = new Map();
  85. const getAudioId = (stream) => {
  86. return stream.language + (stream.channelsCount || 0) +
  87. (stream.audioSamplingRate || 0) + stream.roles.join(',') +
  88. stream.label + stream.groupId + stream.fastSwitching;
  89. };
  90. for (const stream of audioStreams) {
  91. const groupId = getAudioId(stream);
  92. const validAudioStreams = validAudioStreamsMap.get(groupId) || [];
  93. if (!validAudioStreams.length) {
  94. validAudioStreams.push(stream);
  95. validAudioIds.push(stream.id);
  96. } else {
  97. const previousStream = validAudioStreams[validAudioStreams.length - 1];
  98. const previousCodec =
  99. MimeUtils.getNormalizedCodec(previousStream.codecs);
  100. const currentCodec =
  101. MimeUtils.getNormalizedCodec(stream.codecs);
  102. if (previousCodec == currentCodec) {
  103. if (!stream.bandwidth || !previousStream.bandwidth ||
  104. stream.bandwidth > previousStream.bandwidth) {
  105. validAudioStreams.push(stream);
  106. validAudioIds.push(stream.id);
  107. }
  108. }
  109. }
  110. validAudioStreamsMap.set(groupId, validAudioStreams);
  111. }
  112. const videoStreams = Array.from(videoStreamsSet)
  113. .sort((v1, v2) => {
  114. if (!v1.bandwidth || !v2.bandwidth) {
  115. return v1.width - v2.width;
  116. }
  117. return v1.bandwidth - v2.bandwidth;
  118. });
  119. const isChangeTypeSupported =
  120. shaka.media.Capabilities.isChangeTypeSupported();
  121. const validVideoIds = [];
  122. const validVideoStreamsMap = new Map();
  123. const getVideoGroupId = (stream) => {
  124. return Math.round(stream.frameRate || 0) + (stream.hdr || '') +
  125. stream.fastSwitching;
  126. };
  127. for (const stream of videoStreams) {
  128. const groupId = getVideoGroupId(stream);
  129. const validVideoStreams = validVideoStreamsMap.get(groupId) || [];
  130. if (!validVideoStreams.length) {
  131. validVideoStreams.push(stream);
  132. validVideoIds.push(stream.id);
  133. } else {
  134. const previousStream = validVideoStreams[validVideoStreams.length - 1];
  135. if (!isChangeTypeSupported) {
  136. const previousCodec =
  137. MimeUtils.getNormalizedCodec(previousStream.codecs);
  138. const currentCodec =
  139. MimeUtils.getNormalizedCodec(stream.codecs);
  140. if (previousCodec !== currentCodec) {
  141. continue;
  142. }
  143. }
  144. if (stream.width > previousStream.width ||
  145. stream.height > previousStream.height) {
  146. validVideoStreams.push(stream);
  147. validVideoIds.push(stream.id);
  148. } else if (stream.width == previousStream.width &&
  149. stream.height == previousStream.height) {
  150. const previousCodec =
  151. MimeUtils.getNormalizedCodec(previousStream.codecs);
  152. const currentCodec =
  153. MimeUtils.getNormalizedCodec(stream.codecs);
  154. if (previousCodec == currentCodec) {
  155. if (!stream.bandwidth || !previousStream.bandwidth ||
  156. stream.bandwidth > previousStream.bandwidth) {
  157. validVideoStreams.push(stream);
  158. validVideoIds.push(stream.id);
  159. }
  160. }
  161. }
  162. }
  163. validVideoStreamsMap.set(groupId, validVideoStreams);
  164. }
  165. // Filter out any variants that don't match, forcing AbrManager to choose
  166. // from a single video codec and a single audio codec possible.
  167. manifest.variants = manifest.variants.filter((variant) => {
  168. const audio = variant.audio;
  169. const video = variant.video;
  170. if (audio) {
  171. if (!validAudioIds.includes(audio.id)) {
  172. shaka.log.debug('Dropping Variant (better codec available)', variant);
  173. return false;
  174. }
  175. }
  176. if (video) {
  177. if (!validVideoIds.includes(video.id)) {
  178. shaka.log.debug('Dropping Variant (better codec available)', variant);
  179. return false;
  180. }
  181. }
  182. return true;
  183. });
  184. }
  185. /**
  186. * Choose the codecs by configured preferred audio and video codecs.
  187. *
  188. * @param {!Array<shaka.extern.Variant>} variants
  189. * @param {!Array.<string>} preferredVideoCodecs
  190. * @param {!Array.<string>} preferredAudioCodecs
  191. * @return {!Array<shaka.extern.Variant>}
  192. */
  193. static choosePreferredCodecs(variants, preferredVideoCodecs,
  194. preferredAudioCodecs) {
  195. let subset = variants;
  196. for (const videoCodec of preferredVideoCodecs) {
  197. const filtered = subset.filter((variant) => {
  198. return variant.video && variant.video.codecs.startsWith(videoCodec);
  199. });
  200. if (filtered.length) {
  201. subset = filtered;
  202. break;
  203. }
  204. }
  205. for (const audioCodec of preferredAudioCodecs) {
  206. const filtered = subset.filter((variant) => {
  207. return variant.audio && variant.audio.codecs.startsWith(audioCodec);
  208. });
  209. if (filtered.length) {
  210. subset = filtered;
  211. break;
  212. }
  213. }
  214. return subset;
  215. }
  216. /**
  217. * Filter the variants in |manifest| to only include the variants that meet
  218. * the given restrictions.
  219. *
  220. * @param {!shaka.extern.Manifest} manifest
  221. * @param {shaka.extern.Restrictions} restrictions
  222. * @param {shaka.extern.Resolution} maxHwResolution
  223. */
  224. static filterByRestrictions(manifest, restrictions, maxHwResolution) {
  225. manifest.variants = manifest.variants.filter((variant) => {
  226. return shaka.util.StreamUtils.meetsRestrictions(
  227. variant, restrictions, maxHwResolution);
  228. });
  229. }
  230. /**
  231. * @param {shaka.extern.Variant} variant
  232. * @param {shaka.extern.Restrictions} restrictions
  233. * Configured restrictions from the user.
  234. * @param {shaka.extern.Resolution} maxHwRes
  235. * The maximum resolution the hardware can handle.
  236. * This is applied separately from user restrictions because the setting
  237. * should not be easily replaced by the user's configuration.
  238. * @return {boolean}
  239. * @export
  240. */
  241. static meetsRestrictions(variant, restrictions, maxHwRes) {
  242. /** @type {function(number, number, number):boolean} */
  243. const inRange = (x, min, max) => {
  244. return x >= min && x <= max;
  245. };
  246. const video = variant.video;
  247. // |video.width| and |video.height| can be undefined, which breaks
  248. // the math, so make sure they are there first.
  249. if (video && video.width && video.height) {
  250. let videoWidth = video.width;
  251. let videoHeight = video.height;
  252. if (videoHeight > videoWidth) {
  253. // Vertical video.
  254. [videoWidth, videoHeight] = [videoHeight, videoWidth];
  255. }
  256. if (!inRange(videoWidth,
  257. restrictions.minWidth,
  258. Math.min(restrictions.maxWidth, maxHwRes.width))) {
  259. return false;
  260. }
  261. if (!inRange(videoHeight,
  262. restrictions.minHeight,
  263. Math.min(restrictions.maxHeight, maxHwRes.height))) {
  264. return false;
  265. }
  266. if (!inRange(video.width * video.height,
  267. restrictions.minPixels,
  268. restrictions.maxPixels)) {
  269. return false;
  270. }
  271. }
  272. // |variant.video.frameRate| can be undefined, which breaks
  273. // the math, so make sure they are there first.
  274. if (variant && variant.video && variant.video.frameRate) {
  275. if (!inRange(variant.video.frameRate,
  276. restrictions.minFrameRate,
  277. restrictions.maxFrameRate)) {
  278. return false;
  279. }
  280. }
  281. // |variant.audio.channelsCount| can be undefined, which breaks
  282. // the math, so make sure they are there first.
  283. if (variant && variant.audio && variant.audio.channelsCount) {
  284. if (!inRange(variant.audio.channelsCount,
  285. restrictions.minChannelsCount,
  286. restrictions.maxChannelsCount)) {
  287. return false;
  288. }
  289. }
  290. if (!inRange(variant.bandwidth,
  291. restrictions.minBandwidth,
  292. restrictions.maxBandwidth)) {
  293. return false;
  294. }
  295. return true;
  296. }
  297. /**
  298. * @param {!Array.<shaka.extern.Variant>} variants
  299. * @param {shaka.extern.Restrictions} restrictions
  300. * @param {shaka.extern.Resolution} maxHwRes
  301. * @return {boolean} Whether the tracks changed.
  302. */
  303. static applyRestrictions(variants, restrictions, maxHwRes) {
  304. let tracksChanged = false;
  305. for (const variant of variants) {
  306. const originalAllowed = variant.allowedByApplication;
  307. variant.allowedByApplication = shaka.util.StreamUtils.meetsRestrictions(
  308. variant, restrictions, maxHwRes);
  309. if (originalAllowed != variant.allowedByApplication) {
  310. tracksChanged = true;
  311. }
  312. }
  313. return tracksChanged;
  314. }
  315. /**
  316. * Alters the given Manifest to filter out any unplayable streams.
  317. *
  318. * @param {shaka.media.DrmEngine} drmEngine
  319. * @param {shaka.extern.Manifest} manifest
  320. * @param {!Array<string>=} preferredKeySystems
  321. */
  322. static async filterManifest(drmEngine, manifest, preferredKeySystems = []) {
  323. await shaka.util.StreamUtils.filterManifestByMediaCapabilities(
  324. drmEngine, manifest, manifest.offlineSessionIds.length > 0,
  325. preferredKeySystems);
  326. shaka.util.StreamUtils.filterTextStreams_(manifest);
  327. await shaka.util.StreamUtils.filterImageStreams_(manifest);
  328. }
  329. /**
  330. * Alters the given Manifest to filter out any streams unsupported by the
  331. * platform via MediaCapabilities.decodingInfo() API.
  332. *
  333. * @param {shaka.media.DrmEngine} drmEngine
  334. * @param {shaka.extern.Manifest} manifest
  335. * @param {boolean} usePersistentLicenses
  336. * @param {!Array<string>} preferredKeySystems
  337. */
  338. static async filterManifestByMediaCapabilities(
  339. drmEngine, manifest, usePersistentLicenses, preferredKeySystems) {
  340. goog.asserts.assert(navigator.mediaCapabilities,
  341. 'MediaCapabilities should be valid.');
  342. await shaka.util.StreamUtils.getDecodingInfosForVariants(
  343. manifest.variants, usePersistentLicenses, /* srcEquals= */ false,
  344. preferredKeySystems);
  345. let keySystem = null;
  346. if (drmEngine) {
  347. const drmInfo = drmEngine.getDrmInfo();
  348. if (drmInfo) {
  349. keySystem = drmInfo.keySystem;
  350. }
  351. }
  352. const StreamUtils = shaka.util.StreamUtils;
  353. manifest.variants = manifest.variants.filter((variant) => {
  354. const supported = StreamUtils.checkVariantSupported_(variant, keySystem);
  355. // Filter out all unsupported variants.
  356. if (!supported) {
  357. shaka.log.debug('Dropping variant - not compatible with platform',
  358. StreamUtils.getVariantSummaryString_(variant));
  359. }
  360. return supported;
  361. });
  362. }
  363. /**
  364. * @param {!shaka.extern.Variant} variant
  365. * @param {?string} keySystem
  366. * @return {boolean}
  367. * @private
  368. */
  369. static checkVariantSupported_(variant, keySystem) {
  370. const ContentType = shaka.util.ManifestParserUtils.ContentType;
  371. const Capabilities = shaka.media.Capabilities;
  372. const ManifestParserUtils = shaka.util.ManifestParserUtils;
  373. const MimeUtils = shaka.util.MimeUtils;
  374. const StreamUtils = shaka.util.StreamUtils;
  375. const isXboxOne = shaka.util.Platform.isXboxOne();
  376. const isFirefoxAndroid = shaka.util.Platform.isFirefox() &&
  377. shaka.util.Platform.isAndroid();
  378. // See: https://github.com/shaka-project/shaka-player/issues/3860
  379. const video = variant.video;
  380. const videoWidth = (video && video.width) || 0;
  381. const videoHeight = (video && video.height) || 0;
  382. // See: https://github.com/shaka-project/shaka-player/issues/3380
  383. // Note: it makes sense to drop early
  384. if (isXboxOne && video && (videoWidth > 1920 || videoHeight > 1080) &&
  385. (video.codecs.includes('avc1.') || video.codecs.includes('avc3.'))) {
  386. return false;
  387. }
  388. if (video) {
  389. let videoCodecs = StreamUtils.getCorrectVideoCodecs(video.codecs);
  390. // For multiplexed streams. Here we must check the audio of the
  391. // stream to see if it is compatible.
  392. if (video.codecs.includes(',')) {
  393. const allCodecs = video.codecs.split(',');
  394. videoCodecs = ManifestParserUtils.guessCodecs(
  395. ContentType.VIDEO, allCodecs);
  396. videoCodecs = StreamUtils.getCorrectVideoCodecs(videoCodecs);
  397. let audioCodecs = ManifestParserUtils.guessCodecs(
  398. ContentType.AUDIO, allCodecs);
  399. audioCodecs = StreamUtils.getCorrectAudioCodecs(
  400. audioCodecs, video.mimeType);
  401. const audioFullType = MimeUtils.getFullOrConvertedType(
  402. video.mimeType, audioCodecs, ContentType.AUDIO);
  403. if (!Capabilities.isTypeSupported(audioFullType)) {
  404. return false;
  405. }
  406. // Update the codec string with the (possibly) converted codecs.
  407. videoCodecs = [videoCodecs, audioCodecs].join(',');
  408. }
  409. const fullType = MimeUtils.getFullOrConvertedType(
  410. video.mimeType, videoCodecs, ContentType.VIDEO);
  411. if (!Capabilities.isTypeSupported(fullType)) {
  412. return false;
  413. }
  414. // Update the codec string with the (possibly) converted codecs.
  415. video.codecs = videoCodecs;
  416. }
  417. const audio = variant.audio;
  418. // See: https://github.com/shaka-project/shaka-player/issues/6111
  419. // It seems that Firefox Android reports that it supports
  420. // Opus + Widevine, but it is not actually supported.
  421. // It makes sense to drop early.
  422. if (isFirefoxAndroid && audio && audio.encrypted &&
  423. audio.codecs.toLowerCase().includes('opus')) {
  424. return false;
  425. }
  426. if (audio) {
  427. const codecs = StreamUtils.getCorrectAudioCodecs(
  428. audio.codecs, audio.mimeType);
  429. const fullType = MimeUtils.getFullOrConvertedType(
  430. audio.mimeType, codecs, ContentType.AUDIO);
  431. if (!Capabilities.isTypeSupported(fullType)) {
  432. return false;
  433. }
  434. // Update the codec string with the (possibly) converted codecs.
  435. audio.codecs = codecs;
  436. }
  437. return variant.decodingInfos.some((decodingInfo) => {
  438. if (!decodingInfo.supported) {
  439. return false;
  440. }
  441. if (keySystem) {
  442. const keySystemAccess = decodingInfo.keySystemAccess;
  443. if (keySystemAccess) {
  444. if (keySystemAccess.keySystem != keySystem) {
  445. return false;
  446. }
  447. }
  448. }
  449. return true;
  450. });
  451. }
  452. /**
  453. * Constructs a string out of an object, similar to the JSON.stringify method.
  454. * Unlike that method, this guarantees that the order of the keys is
  455. * alphabetical, so it can be used as a way to reliably compare two objects.
  456. *
  457. * @param {!Object} obj
  458. * @return {string}
  459. * @private
  460. */
  461. static alphabeticalKeyOrderStringify_(obj) {
  462. const keys = [];
  463. for (const key in obj) {
  464. keys.push(key);
  465. }
  466. // Alphabetically sort the keys, so they will be in a reliable order.
  467. keys.sort();
  468. const terms = [];
  469. for (const key of keys) {
  470. const escapedKey = JSON.stringify(key);
  471. const value = obj[key];
  472. if (value instanceof Object) {
  473. const stringifiedValue =
  474. shaka.util.StreamUtils.alphabeticalKeyOrderStringify_(value);
  475. terms.push(escapedKey + ':' + stringifiedValue);
  476. } else {
  477. const escapedValue = JSON.stringify(value);
  478. terms.push(escapedKey + ':' + escapedValue);
  479. }
  480. }
  481. return '{' + terms.join(',') + '}';
  482. }
  483. /**
  484. * Queries mediaCapabilities for the decoding info for that decoding config,
  485. * and assigns it to the given variant.
  486. * If that query has been done before, instead return a cached result.
  487. * @param {!shaka.extern.Variant} variant
  488. * @param {!Array.<!MediaDecodingConfiguration>} decodingConfigs
  489. * @private
  490. */
  491. static async getDecodingInfosForVariant_(variant, decodingConfigs) {
  492. /**
  493. * @param {?MediaCapabilitiesDecodingInfo} a
  494. * @param {!MediaCapabilitiesDecodingInfo} b
  495. * @return {!MediaCapabilitiesDecodingInfo}
  496. */
  497. const merge = (a, b) => {
  498. if (!a) {
  499. return b;
  500. } else {
  501. const res = shaka.util.ObjectUtils.shallowCloneObject(a);
  502. res.supported = a.supported && b.supported;
  503. res.powerEfficient = a.powerEfficient && b.powerEfficient;
  504. res.smooth = a.smooth && b.smooth;
  505. if (b.keySystemAccess && !res.keySystemAccess) {
  506. res.keySystemAccess = b.keySystemAccess;
  507. }
  508. return res;
  509. }
  510. };
  511. const StreamUtils = shaka.util.StreamUtils;
  512. /** @type {?MediaCapabilitiesDecodingInfo} */
  513. let finalResult = null;
  514. const promises = [];
  515. for (const decodingConfig of decodingConfigs) {
  516. const cacheKey =
  517. StreamUtils.alphabeticalKeyOrderStringify_(decodingConfig);
  518. const cache = StreamUtils.decodingConfigCache_;
  519. if (cache[cacheKey]) {
  520. shaka.log.v2('Using cached results of mediaCapabilities.decodingInfo',
  521. 'for key', cacheKey);
  522. finalResult = merge(finalResult, cache[cacheKey]);
  523. } else {
  524. // Do a final pass-over of the decoding config: if a given stream has
  525. // multiple codecs, that suggests that it switches between those codecs
  526. // at points of the go-through.
  527. // mediaCapabilities by itself will report "not supported" when you
  528. // put in multiple different codecs, so each has to be checked
  529. // individually. So check each and take the worst result, to determine
  530. // overall variant compatibility.
  531. promises.push(StreamUtils
  532. .checkEachDecodingConfigCombination_(decodingConfig).then((res) => {
  533. /** @type {?MediaCapabilitiesDecodingInfo} */
  534. let acc = null;
  535. for (const result of (res || [])) {
  536. acc = merge(acc, result);
  537. }
  538. if (acc) {
  539. cache[cacheKey] = acc;
  540. finalResult = merge(finalResult, acc);
  541. }
  542. }));
  543. }
  544. }
  545. await Promise.all(promises);
  546. if (finalResult) {
  547. variant.decodingInfos.push(finalResult);
  548. }
  549. }
  550. /**
  551. * @param {!MediaDecodingConfiguration} decodingConfig
  552. * @return {!Promise.<?Array.<!MediaCapabilitiesDecodingInfo>>}
  553. * @private
  554. */
  555. static checkEachDecodingConfigCombination_(decodingConfig) {
  556. let videoCodecs = [''];
  557. if (decodingConfig.video) {
  558. videoCodecs = shaka.util.MimeUtils.getCodecs(
  559. decodingConfig.video.contentType).split(',');
  560. }
  561. let audioCodecs = [''];
  562. if (decodingConfig.audio) {
  563. audioCodecs = shaka.util.MimeUtils.getCodecs(
  564. decodingConfig.audio.contentType).split(',');
  565. }
  566. const promises = [];
  567. for (const videoCodec of videoCodecs) {
  568. for (const audioCodec of audioCodecs) {
  569. const copy = shaka.util.ObjectUtils.cloneObject(decodingConfig);
  570. if (decodingConfig.video) {
  571. const mimeType = shaka.util.MimeUtils.getBasicType(
  572. copy.video.contentType);
  573. copy.video.contentType = shaka.util.MimeUtils.getFullType(
  574. mimeType, videoCodec);
  575. }
  576. if (decodingConfig.audio) {
  577. const mimeType = shaka.util.MimeUtils.getBasicType(
  578. copy.audio.contentType);
  579. copy.audio.contentType = shaka.util.MimeUtils.getFullType(
  580. mimeType, audioCodec);
  581. }
  582. promises.push(new Promise((resolve, reject) => {
  583. navigator.mediaCapabilities.decodingInfo(copy).then((res) => {
  584. resolve(res);
  585. }).catch(reject);
  586. }));
  587. }
  588. }
  589. return Promise.all(promises).catch((e) => {
  590. shaka.log.info('MediaCapabilities.decodingInfo() failed.',
  591. JSON.stringify(decodingConfig), e);
  592. return null;
  593. });
  594. }
  595. /**
  596. * Get the decodingInfo results of the variants via MediaCapabilities.
  597. * This should be called after the DrmEngine is created and configured, and
  598. * before DrmEngine sets the mediaKeys.
  599. *
  600. * @param {!Array.<shaka.extern.Variant>} variants
  601. * @param {boolean} usePersistentLicenses
  602. * @param {boolean} srcEquals
  603. * @param {!Array<string>} preferredKeySystems
  604. * @exportDoc
  605. */
  606. static async getDecodingInfosForVariants(variants, usePersistentLicenses,
  607. srcEquals, preferredKeySystems) {
  608. const gotDecodingInfo = variants.some((variant) =>
  609. variant.decodingInfos.length);
  610. if (gotDecodingInfo) {
  611. shaka.log.debug('Already got the variants\' decodingInfo.');
  612. return;
  613. }
  614. // Try to get preferred key systems first to avoid unneeded calls to CDM.
  615. for (const preferredKeySystem of preferredKeySystems) {
  616. let keySystemSatisfied = false;
  617. for (const variant of variants) {
  618. /** @type {!Array.<!Array.<!MediaDecodingConfiguration>>} */
  619. const decodingConfigs = shaka.util.StreamUtils.getDecodingConfigs_(
  620. variant, usePersistentLicenses, srcEquals)
  621. .filter((configs) => {
  622. // All configs in a batch will have the same keySystem.
  623. const config = configs[0];
  624. const keySystem = config.keySystemConfiguration &&
  625. config.keySystemConfiguration.keySystem;
  626. return keySystem === preferredKeySystem;
  627. });
  628. // The reason we are performing this await in a loop rather than
  629. // batching into a `promise.all` is performance related.
  630. // https://github.com/shaka-project/shaka-player/pull/4708#discussion_r1022581178
  631. for (const configs of decodingConfigs) {
  632. // eslint-disable-next-line no-await-in-loop
  633. await shaka.util.StreamUtils.getDecodingInfosForVariant_(
  634. variant, configs);
  635. }
  636. if (variant.decodingInfos.length) {
  637. keySystemSatisfied = true;
  638. }
  639. } // for (const variant of variants)
  640. if (keySystemSatisfied) {
  641. // Return if any preferred key system is already satisfied.
  642. return;
  643. }
  644. } // for (const preferredKeySystem of preferredKeySystems)
  645. for (const variant of variants) {
  646. /** @type {!Array.<!Array.<!MediaDecodingConfiguration>>} */
  647. const decodingConfigs = shaka.util.StreamUtils.getDecodingConfigs_(
  648. variant, usePersistentLicenses, srcEquals)
  649. .filter((configs) => {
  650. // All configs in a batch will have the same keySystem.
  651. const config = configs[0];
  652. const keySystem = config.keySystemConfiguration &&
  653. config.keySystemConfiguration.keySystem;
  654. // Avoid checking preferred systems twice.
  655. return !keySystem || !preferredKeySystems.includes(keySystem);
  656. });
  657. // The reason we are performing this await in a loop rather than
  658. // batching into a `promise.all` is performance related.
  659. // https://github.com/shaka-project/shaka-player/pull/4708#discussion_r1022581178
  660. for (const configs of decodingConfigs) {
  661. // eslint-disable-next-line no-await-in-loop
  662. await shaka.util.StreamUtils.getDecodingInfosForVariant_(
  663. variant, configs);
  664. }
  665. }
  666. }
  667. /**
  668. * Generate a batch of MediaDecodingConfiguration objects to get the
  669. * decodingInfo results for each variant.
  670. * Each batch shares the same DRM information, and represents the various
  671. * fullMimeType combinations of the streams.
  672. * @param {!shaka.extern.Variant} variant
  673. * @param {boolean} usePersistentLicenses
  674. * @param {boolean} srcEquals
  675. * @return {!Array.<!Array.<!MediaDecodingConfiguration>>}
  676. * @private
  677. */
  678. static getDecodingConfigs_(variant, usePersistentLicenses, srcEquals) {
  679. const audio = variant.audio;
  680. const video = variant.video;
  681. const ContentType = shaka.util.ManifestParserUtils.ContentType;
  682. const ManifestParserUtils = shaka.util.ManifestParserUtils;
  683. const MimeUtils = shaka.util.MimeUtils;
  684. const StreamUtils = shaka.util.StreamUtils;
  685. const videoConfigs = [];
  686. const audioConfigs = [];
  687. if (video) {
  688. for (const fullMimeType of video.fullMimeTypes) {
  689. let videoCodecs = MimeUtils.getCodecs(fullMimeType);
  690. // For multiplexed streams with audio+video codecs, the config should
  691. // have AudioConfiguration and VideoConfiguration.
  692. // We ignore the multiplexed audio when there is normal audio also.
  693. if (videoCodecs.includes(',') && !audio) {
  694. const allCodecs = videoCodecs.split(',');
  695. const baseMimeType = MimeUtils.getBasicType(fullMimeType);
  696. videoCodecs = ManifestParserUtils.guessCodecs(
  697. ContentType.VIDEO, allCodecs);
  698. let audioCodecs = ManifestParserUtils.guessCodecs(
  699. ContentType.AUDIO, allCodecs);
  700. audioCodecs = StreamUtils.getCorrectAudioCodecs(
  701. audioCodecs, baseMimeType);
  702. const audioFullType = MimeUtils.getFullOrConvertedType(
  703. baseMimeType, audioCodecs, ContentType.AUDIO);
  704. audioConfigs.push({
  705. contentType: audioFullType,
  706. channels: 2,
  707. bitrate: variant.bandwidth || 1,
  708. samplerate: 1,
  709. spatialRendering: false,
  710. });
  711. }
  712. videoCodecs = StreamUtils.getCorrectVideoCodecs(videoCodecs);
  713. const fullType = MimeUtils.getFullOrConvertedType(
  714. MimeUtils.getBasicType(fullMimeType), videoCodecs,
  715. ContentType.VIDEO);
  716. // VideoConfiguration
  717. const videoConfig = {
  718. contentType: fullType,
  719. // NOTE: Some decoders strictly check the width and height fields and
  720. // won't decode smaller than 64x64. So if we don't have this info (as
  721. // is the case in some of our simpler tests), assume a 64x64
  722. // resolution to fill in this required field for MediaCapabilities.
  723. //
  724. // This became an issue specifically on Firefox on M1 Macs.
  725. width: video.width || 64,
  726. height: video.height || 64,
  727. bitrate: video.bandwidth || variant.bandwidth || 1,
  728. // framerate must be greater than 0, otherwise the config is invalid.
  729. framerate: video.frameRate || 1,
  730. };
  731. if (video.hdr) {
  732. switch (video.hdr) {
  733. case 'SDR':
  734. videoConfig.transferFunction = 'srgb';
  735. break;
  736. case 'PQ':
  737. videoConfig.transferFunction = 'pq';
  738. break;
  739. case 'HLG':
  740. videoConfig.transferFunction = 'hlg';
  741. break;
  742. }
  743. }
  744. if (video.colorGamut) {
  745. videoConfig.colorGamut = video.colorGamut;
  746. }
  747. videoConfigs.push(videoConfig);
  748. }
  749. }
  750. if (audio) {
  751. for (const fullMimeType of audio.fullMimeTypes) {
  752. const baseMimeType = MimeUtils.getBasicType(fullMimeType);
  753. const codecs = StreamUtils.getCorrectAudioCodecs(
  754. MimeUtils.getCodecs(fullMimeType), baseMimeType);
  755. const fullType = MimeUtils.getFullOrConvertedType(
  756. baseMimeType, codecs, ContentType.AUDIO);
  757. // AudioConfiguration
  758. audioConfigs.push({
  759. contentType: fullType,
  760. channels: audio.channelsCount || 2,
  761. bitrate: audio.bandwidth || variant.bandwidth || 1,
  762. samplerate: audio.audioSamplingRate || 1,
  763. spatialRendering: audio.spatialAudio,
  764. });
  765. }
  766. }
  767. // Generate each combination of video and audio config as a separate
  768. // MediaDecodingConfiguration, inside the main "batch".
  769. /** @type {!Array.<!MediaDecodingConfiguration>} */
  770. const mediaDecodingConfigBatch = [];
  771. if (videoConfigs.length == 0) {
  772. videoConfigs.push(null);
  773. }
  774. if (audioConfigs.length == 0) {
  775. audioConfigs.push(null);
  776. }
  777. for (const videoConfig of videoConfigs) {
  778. for (const audioConfig of audioConfigs) {
  779. /** @type {!MediaDecodingConfiguration} */
  780. const mediaDecodingConfig = {
  781. type: srcEquals ? 'file' : 'media-source',
  782. };
  783. if (videoConfig) {
  784. mediaDecodingConfig.video = videoConfig;
  785. }
  786. if (audioConfig) {
  787. mediaDecodingConfig.audio = audioConfig;
  788. }
  789. mediaDecodingConfigBatch.push(mediaDecodingConfig);
  790. }
  791. }
  792. const videoDrmInfos = variant.video ? variant.video.drmInfos : [];
  793. const audioDrmInfos = variant.audio ? variant.audio.drmInfos : [];
  794. const allDrmInfos = videoDrmInfos.concat(audioDrmInfos);
  795. // Return a list containing the mediaDecodingConfig for unencrypted variant.
  796. if (!allDrmInfos.length) {
  797. return [mediaDecodingConfigBatch];
  798. }
  799. // A list of MediaDecodingConfiguration objects created for the variant.
  800. const configs = [];
  801. // Get all the drm info so that we can avoid using nested loops when we
  802. // just need the drm info.
  803. const drmInfoByKeySystems = new Map();
  804. for (const info of allDrmInfos) {
  805. if (!drmInfoByKeySystems.get(info.keySystem)) {
  806. drmInfoByKeySystems.set(info.keySystem, []);
  807. }
  808. drmInfoByKeySystems.get(info.keySystem).push(info);
  809. }
  810. const persistentState =
  811. usePersistentLicenses ? 'required' : 'optional';
  812. const sessionTypes =
  813. usePersistentLicenses ? ['persistent-license'] : ['temporary'];
  814. for (const keySystem of drmInfoByKeySystems.keys()) {
  815. const modifiedMediaDecodingConfigBatch = [];
  816. for (const base of mediaDecodingConfigBatch) {
  817. // Create a copy of the mediaDecodingConfig.
  818. const config = /** @type {!MediaDecodingConfiguration} */
  819. (Object.assign({}, base));
  820. const drmInfos = drmInfoByKeySystems.get(keySystem);
  821. /** @type {!MediaCapabilitiesKeySystemConfiguration} */
  822. const keySystemConfig = {
  823. keySystem: keySystem,
  824. initDataType: 'cenc',
  825. persistentState: persistentState,
  826. distinctiveIdentifier: 'optional',
  827. sessionTypes: sessionTypes,
  828. };
  829. for (const info of drmInfos) {
  830. if (info.initData && info.initData.length) {
  831. const initDataTypes = new Set();
  832. for (const initData of info.initData) {
  833. initDataTypes.add(initData.initDataType);
  834. }
  835. if (initDataTypes.size > 1) {
  836. shaka.log.v2('DrmInfo contains more than one initDataType,',
  837. 'and we use the initDataType of the first initData.',
  838. info);
  839. }
  840. keySystemConfig.initDataType = info.initData[0].initDataType;
  841. }
  842. if (info.distinctiveIdentifierRequired) {
  843. keySystemConfig.distinctiveIdentifier = 'required';
  844. }
  845. if (info.persistentStateRequired) {
  846. keySystemConfig.persistentState = 'required';
  847. }
  848. if (info.sessionType) {
  849. keySystemConfig.sessionTypes = [info.sessionType];
  850. }
  851. if (audio) {
  852. if (!keySystemConfig.audio) {
  853. // KeySystemTrackConfiguration
  854. keySystemConfig.audio = {
  855. encryptionScheme: info.encryptionScheme,
  856. robustness: info.audioRobustness,
  857. };
  858. } else {
  859. keySystemConfig.audio.encryptionScheme =
  860. keySystemConfig.audio.encryptionScheme ||
  861. info.encryptionScheme;
  862. keySystemConfig.audio.robustness =
  863. keySystemConfig.audio.robustness ||
  864. info.audioRobustness;
  865. }
  866. // See: https://github.com/shaka-project/shaka-player/issues/4659
  867. if (keySystemConfig.audio.robustness == '') {
  868. delete keySystemConfig.audio.robustness;
  869. }
  870. }
  871. if (video) {
  872. if (!keySystemConfig.video) {
  873. // KeySystemTrackConfiguration
  874. keySystemConfig.video = {
  875. encryptionScheme: info.encryptionScheme,
  876. robustness: info.videoRobustness,
  877. };
  878. } else {
  879. keySystemConfig.video.encryptionScheme =
  880. keySystemConfig.video.encryptionScheme ||
  881. info.encryptionScheme;
  882. keySystemConfig.video.robustness =
  883. keySystemConfig.video.robustness ||
  884. info.videoRobustness;
  885. }
  886. // See: https://github.com/shaka-project/shaka-player/issues/4659
  887. if (keySystemConfig.video.robustness == '') {
  888. delete keySystemConfig.video.robustness;
  889. }
  890. }
  891. }
  892. config.keySystemConfiguration = keySystemConfig;
  893. modifiedMediaDecodingConfigBatch.push(config);
  894. }
  895. configs.push(modifiedMediaDecodingConfigBatch);
  896. }
  897. return configs;
  898. }
  899. /**
  900. * Generates the correct audio codec for MediaDecodingConfiguration and
  901. * for MediaSource.isTypeSupported.
  902. * @param {string} codecs
  903. * @param {string} mimeType
  904. * @return {string}
  905. */
  906. static getCorrectAudioCodecs(codecs, mimeType) {
  907. // According to RFC 6381 section 3.3, 'fLaC' is actually the correct
  908. // codec string. We still need to map it to 'flac', as some browsers
  909. // currently don't support 'fLaC', while 'flac' is supported by most
  910. // major browsers.
  911. // See https://bugs.chromium.org/p/chromium/issues/detail?id=1422728
  912. if (codecs.toLowerCase() == 'flac') {
  913. if (!shaka.util.Platform.isSafari()) {
  914. return 'flac';
  915. } else {
  916. return 'fLaC';
  917. }
  918. }
  919. // The same is true for 'Opus'.
  920. if (codecs.toLowerCase() === 'opus') {
  921. if (!shaka.util.Platform.isSafari()) {
  922. return 'opus';
  923. } else {
  924. if (shaka.util.MimeUtils.getContainerType(mimeType) == 'mp4') {
  925. return 'Opus';
  926. } else {
  927. return 'opus';
  928. }
  929. }
  930. }
  931. return codecs;
  932. }
  933. /**
  934. * Generates the correct video codec for MediaDecodingConfiguration and
  935. * for MediaSource.isTypeSupported.
  936. * @param {string} codec
  937. * @return {string}
  938. */
  939. static getCorrectVideoCodecs(codec) {
  940. if (codec.includes('avc1')) {
  941. // Convert avc1 codec string from RFC-4281 to RFC-6381 for
  942. // MediaSource.isTypeSupported
  943. // Example, convert avc1.66.30 to avc1.42001e (0x42 == 66 and 0x1e == 30)
  944. const avcdata = codec.split('.');
  945. if (avcdata.length == 3) {
  946. let result = avcdata.shift() + '.';
  947. result += parseInt(avcdata.shift(), 10).toString(16);
  948. result +=
  949. ('000' + parseInt(avcdata.shift(), 10).toString(16)).slice(-4);
  950. return result;
  951. }
  952. } else if (codec == 'vp9') {
  953. // MediaCapabilities supports 'vp09...' codecs, but not 'vp9'. Translate
  954. // vp9 codec strings into 'vp09...', to allow such content to play with
  955. // mediaCapabilities enabled.
  956. // This means profile 0, level 4.1, 8-bit color. This supports 1080p @
  957. // 60Hz. See https://en.wikipedia.org/wiki/VP9#Levels
  958. //
  959. // If we don't have more detailed codec info, assume this profile and
  960. // level because it's high enough to likely accommodate the parameters we
  961. // do have, such as width and height. If an implementation is checking
  962. // the profile and level very strictly, we want older VP9 content to
  963. // still work to some degree. But we don't want to set a level so high
  964. // that it is rejected by a hardware decoder that can't handle the
  965. // maximum requirements of the level.
  966. //
  967. // This became an issue specifically on Firefox on M1 Macs.
  968. return 'vp09.00.41.08';
  969. }
  970. return codec;
  971. }
  972. /**
  973. * Alters the given Manifest to filter out any streams uncompatible with the
  974. * current variant.
  975. *
  976. * @param {?shaka.extern.Variant} currentVariant
  977. * @param {shaka.extern.Manifest} manifest
  978. */
  979. static filterManifestByCurrentVariant(currentVariant, manifest) {
  980. const StreamUtils = shaka.util.StreamUtils;
  981. manifest.variants = manifest.variants.filter((variant) => {
  982. const audio = variant.audio;
  983. const video = variant.video;
  984. if (audio && currentVariant && currentVariant.audio) {
  985. if (!StreamUtils.areStreamsCompatible_(audio, currentVariant.audio)) {
  986. shaka.log.debug('Dropping variant - not compatible with active audio',
  987. 'active audio',
  988. StreamUtils.getStreamSummaryString_(currentVariant.audio),
  989. 'variant.audio',
  990. StreamUtils.getStreamSummaryString_(audio));
  991. return false;
  992. }
  993. }
  994. if (video && currentVariant && currentVariant.video) {
  995. if (!StreamUtils.areStreamsCompatible_(video, currentVariant.video)) {
  996. shaka.log.debug('Dropping variant - not compatible with active video',
  997. 'active video',
  998. StreamUtils.getStreamSummaryString_(currentVariant.video),
  999. 'variant.video',
  1000. StreamUtils.getStreamSummaryString_(video));
  1001. return false;
  1002. }
  1003. }
  1004. return true;
  1005. });
  1006. }
  1007. /**
  1008. * Alters the given Manifest to filter out any unsupported text streams.
  1009. *
  1010. * @param {shaka.extern.Manifest} manifest
  1011. * @private
  1012. */
  1013. static filterTextStreams_(manifest) {
  1014. // Filter text streams.
  1015. manifest.textStreams = manifest.textStreams.filter((stream) => {
  1016. const fullMimeType = shaka.util.MimeUtils.getFullType(
  1017. stream.mimeType, stream.codecs);
  1018. const keep = shaka.text.TextEngine.isTypeSupported(fullMimeType);
  1019. if (!keep) {
  1020. shaka.log.debug('Dropping text stream. Is not supported by the ' +
  1021. 'platform.', stream);
  1022. }
  1023. return keep;
  1024. });
  1025. }
  1026. /**
  1027. * Alters the given Manifest to filter out any unsupported image streams.
  1028. *
  1029. * @param {shaka.extern.Manifest} manifest
  1030. * @private
  1031. */
  1032. static async filterImageStreams_(manifest) {
  1033. const imageStreams = [];
  1034. for (const stream of manifest.imageStreams) {
  1035. let mimeType = stream.mimeType;
  1036. if (mimeType == 'application/mp4' && stream.codecs == 'mjpg') {
  1037. mimeType = 'image/jpg';
  1038. }
  1039. if (!shaka.util.StreamUtils.supportedImageMimeTypes_.has(mimeType)) {
  1040. const minImage = shaka.util.StreamUtils.minImage_.get(mimeType);
  1041. if (minImage) {
  1042. // eslint-disable-next-line no-await-in-loop
  1043. const res = await shaka.util.StreamUtils.isImageSupported_(minImage);
  1044. shaka.util.StreamUtils.supportedImageMimeTypes_.set(mimeType, res);
  1045. } else {
  1046. shaka.util.StreamUtils.supportedImageMimeTypes_.set(mimeType, false);
  1047. }
  1048. }
  1049. const keep =
  1050. shaka.util.StreamUtils.supportedImageMimeTypes_.get(mimeType);
  1051. if (!keep) {
  1052. shaka.log.debug('Dropping image stream. Is not supported by the ' +
  1053. 'platform.', stream);
  1054. } else {
  1055. imageStreams.push(stream);
  1056. }
  1057. }
  1058. manifest.imageStreams = imageStreams;
  1059. }
  1060. /**
  1061. * @param {string} minImage
  1062. * @return {!Promise.<boolean>}
  1063. * @private
  1064. */
  1065. static isImageSupported_(minImage) {
  1066. return new Promise((resolve) => {
  1067. const imageElement = /** @type {HTMLImageElement} */(new Image());
  1068. imageElement.src = minImage;
  1069. if ('decode' in imageElement) {
  1070. imageElement.decode().then(() => {
  1071. resolve(true);
  1072. }).catch(() => {
  1073. resolve(false);
  1074. });
  1075. } else {
  1076. imageElement.onload = imageElement.onerror = () => {
  1077. resolve(imageElement.height === 2);
  1078. };
  1079. }
  1080. });
  1081. }
  1082. /**
  1083. * @param {shaka.extern.Stream} s0
  1084. * @param {shaka.extern.Stream} s1
  1085. * @return {boolean}
  1086. * @private
  1087. */
  1088. static areStreamsCompatible_(s0, s1) {
  1089. // Basic mime types and basic codecs need to match.
  1090. // For example, we can't adapt between WebM and MP4,
  1091. // nor can we adapt between mp4a.* to ec-3.
  1092. // We can switch between text types on the fly,
  1093. // so don't run this check on text.
  1094. if (s0.mimeType != s1.mimeType) {
  1095. return false;
  1096. }
  1097. if (s0.codecs.split('.')[0] != s1.codecs.split('.')[0]) {
  1098. return false;
  1099. }
  1100. return true;
  1101. }
  1102. /**
  1103. * @param {shaka.extern.Variant} variant
  1104. * @return {shaka.extern.Track}
  1105. */
  1106. static variantToTrack(variant) {
  1107. /** @type {?shaka.extern.Stream} */
  1108. const audio = variant.audio;
  1109. /** @type {?shaka.extern.Stream} */
  1110. const video = variant.video;
  1111. /** @type {?string} */
  1112. const audioMimeType = audio ? audio.mimeType : null;
  1113. /** @type {?string} */
  1114. const videoMimeType = video ? video.mimeType : null;
  1115. /** @type {?string} */
  1116. const audioCodec = audio ? audio.codecs : null;
  1117. /** @type {?string} */
  1118. const videoCodec = video ? video.codecs : null;
  1119. /** @type {!Array.<string>} */
  1120. const codecs = [];
  1121. if (videoCodec) {
  1122. codecs.push(videoCodec);
  1123. }
  1124. if (audioCodec) {
  1125. codecs.push(audioCodec);
  1126. }
  1127. /** @type {!Array.<string>} */
  1128. const mimeTypes = [];
  1129. if (video) {
  1130. mimeTypes.push(video.mimeType);
  1131. }
  1132. if (audio) {
  1133. mimeTypes.push(audio.mimeType);
  1134. }
  1135. /** @type {?string} */
  1136. const mimeType = mimeTypes[0] || null;
  1137. /** @type {!Array.<string>} */
  1138. const kinds = [];
  1139. if (audio) {
  1140. kinds.push(audio.kind);
  1141. }
  1142. if (video) {
  1143. kinds.push(video.kind);
  1144. }
  1145. /** @type {?string} */
  1146. const kind = kinds[0] || null;
  1147. /** @type {!Set.<string>} */
  1148. const roles = new Set();
  1149. if (audio) {
  1150. for (const role of audio.roles) {
  1151. roles.add(role);
  1152. }
  1153. }
  1154. if (video) {
  1155. for (const role of video.roles) {
  1156. roles.add(role);
  1157. }
  1158. }
  1159. /** @type {shaka.extern.Track} */
  1160. const track = {
  1161. id: variant.id,
  1162. active: false,
  1163. type: 'variant',
  1164. bandwidth: variant.bandwidth,
  1165. language: variant.language,
  1166. label: null,
  1167. kind: kind,
  1168. width: null,
  1169. height: null,
  1170. frameRate: null,
  1171. pixelAspectRatio: null,
  1172. hdr: null,
  1173. colorGamut: null,
  1174. videoLayout: null,
  1175. mimeType: mimeType,
  1176. audioMimeType: audioMimeType,
  1177. videoMimeType: videoMimeType,
  1178. codecs: codecs.join(', '),
  1179. audioCodec: audioCodec,
  1180. videoCodec: videoCodec,
  1181. primary: variant.primary,
  1182. roles: Array.from(roles),
  1183. audioRoles: null,
  1184. forced: false,
  1185. videoId: null,
  1186. audioId: null,
  1187. channelsCount: null,
  1188. audioSamplingRate: null,
  1189. spatialAudio: false,
  1190. tilesLayout: null,
  1191. audioBandwidth: null,
  1192. videoBandwidth: null,
  1193. originalVideoId: null,
  1194. originalAudioId: null,
  1195. originalTextId: null,
  1196. originalImageId: null,
  1197. accessibilityPurpose: null,
  1198. originalLanguage: null,
  1199. };
  1200. if (video) {
  1201. track.videoId = video.id;
  1202. track.originalVideoId = video.originalId;
  1203. track.width = video.width || null;
  1204. track.height = video.height || null;
  1205. track.frameRate = video.frameRate || null;
  1206. track.pixelAspectRatio = video.pixelAspectRatio || null;
  1207. track.videoBandwidth = video.bandwidth || null;
  1208. track.hdr = video.hdr || null;
  1209. track.colorGamut = video.colorGamut || null;
  1210. track.videoLayout = video.videoLayout || null;
  1211. }
  1212. if (audio) {
  1213. track.audioId = audio.id;
  1214. track.originalAudioId = audio.originalId;
  1215. track.channelsCount = audio.channelsCount;
  1216. track.audioSamplingRate = audio.audioSamplingRate;
  1217. track.audioBandwidth = audio.bandwidth || null;
  1218. track.spatialAudio = audio.spatialAudio;
  1219. track.label = audio.label;
  1220. track.audioRoles = audio.roles;
  1221. track.accessibilityPurpose = audio.accessibilityPurpose;
  1222. track.originalLanguage = audio.originalLanguage;
  1223. }
  1224. return track;
  1225. }
  1226. /**
  1227. * @param {shaka.extern.Stream} stream
  1228. * @return {shaka.extern.Track}
  1229. */
  1230. static textStreamToTrack(stream) {
  1231. const ContentType = shaka.util.ManifestParserUtils.ContentType;
  1232. /** @type {shaka.extern.Track} */
  1233. const track = {
  1234. id: stream.id,
  1235. active: false,
  1236. type: ContentType.TEXT,
  1237. bandwidth: 0,
  1238. language: stream.language,
  1239. label: stream.label,
  1240. kind: stream.kind || null,
  1241. width: null,
  1242. height: null,
  1243. frameRate: null,
  1244. pixelAspectRatio: null,
  1245. hdr: null,
  1246. colorGamut: null,
  1247. videoLayout: null,
  1248. mimeType: stream.mimeType,
  1249. audioMimeType: null,
  1250. videoMimeType: null,
  1251. codecs: stream.codecs || null,
  1252. audioCodec: null,
  1253. videoCodec: null,
  1254. primary: stream.primary,
  1255. roles: stream.roles,
  1256. audioRoles: null,
  1257. forced: stream.forced,
  1258. videoId: null,
  1259. audioId: null,
  1260. channelsCount: null,
  1261. audioSamplingRate: null,
  1262. spatialAudio: false,
  1263. tilesLayout: null,
  1264. audioBandwidth: null,
  1265. videoBandwidth: null,
  1266. originalVideoId: null,
  1267. originalAudioId: null,
  1268. originalTextId: stream.originalId,
  1269. originalImageId: null,
  1270. accessibilityPurpose: stream.accessibilityPurpose,
  1271. originalLanguage: stream.originalLanguage,
  1272. };
  1273. return track;
  1274. }
  1275. /**
  1276. * @param {shaka.extern.Stream} stream
  1277. * @return {shaka.extern.Track}
  1278. */
  1279. static imageStreamToTrack(stream) {
  1280. const ContentType = shaka.util.ManifestParserUtils.ContentType;
  1281. let width = stream.width || null;
  1282. let height = stream.height || null;
  1283. // The stream width and height represent the size of the entire thumbnail
  1284. // sheet, so divide by the layout.
  1285. let reference = null;
  1286. // Note: segmentIndex is built by default for HLS, but not for DASH, but
  1287. // in DASH this information comes at the stream level and not at the
  1288. // segment level.
  1289. if (stream.segmentIndex) {
  1290. reference = stream.segmentIndex.get(0);
  1291. }
  1292. let layout = stream.tilesLayout;
  1293. if (reference) {
  1294. layout = reference.getTilesLayout() || layout;
  1295. }
  1296. if (layout && width != null) {
  1297. width /= Number(layout.split('x')[0]);
  1298. }
  1299. if (layout && height != null) {
  1300. height /= Number(layout.split('x')[1]);
  1301. }
  1302. // TODO: What happens if there are multiple grids, with different
  1303. // layout sizes, inside this image stream?
  1304. /** @type {shaka.extern.Track} */
  1305. const track = {
  1306. id: stream.id,
  1307. active: false,
  1308. type: ContentType.IMAGE,
  1309. bandwidth: stream.bandwidth || 0,
  1310. language: '',
  1311. label: null,
  1312. kind: null,
  1313. width,
  1314. height,
  1315. frameRate: null,
  1316. pixelAspectRatio: null,
  1317. hdr: null,
  1318. colorGamut: null,
  1319. videoLayout: null,
  1320. mimeType: stream.mimeType,
  1321. audioMimeType: null,
  1322. videoMimeType: null,
  1323. codecs: stream.codecs || null,
  1324. audioCodec: null,
  1325. videoCodec: null,
  1326. primary: false,
  1327. roles: [],
  1328. audioRoles: null,
  1329. forced: false,
  1330. videoId: null,
  1331. audioId: null,
  1332. channelsCount: null,
  1333. audioSamplingRate: null,
  1334. spatialAudio: false,
  1335. tilesLayout: layout || null,
  1336. audioBandwidth: null,
  1337. videoBandwidth: null,
  1338. originalVideoId: null,
  1339. originalAudioId: null,
  1340. originalTextId: null,
  1341. originalImageId: stream.originalId,
  1342. accessibilityPurpose: null,
  1343. originalLanguage: null,
  1344. };
  1345. return track;
  1346. }
  1347. /**
  1348. * Generate and return an ID for this track, since the ID field is optional.
  1349. *
  1350. * @param {TextTrack|AudioTrack} html5Track
  1351. * @return {number} The generated ID.
  1352. */
  1353. static html5TrackId(html5Track) {
  1354. if (!html5Track['__shaka_id']) {
  1355. html5Track['__shaka_id'] = shaka.util.StreamUtils.nextTrackId_++;
  1356. }
  1357. return html5Track['__shaka_id'];
  1358. }
  1359. /**
  1360. * @param {TextTrack} textTrack
  1361. * @return {shaka.extern.Track}
  1362. */
  1363. static html5TextTrackToTrack(textTrack) {
  1364. const StreamUtils = shaka.util.StreamUtils;
  1365. /** @type {shaka.extern.Track} */
  1366. const track = StreamUtils.html5TrackToGenericShakaTrack_(textTrack);
  1367. track.active = textTrack.mode != 'disabled';
  1368. track.type = 'text';
  1369. track.originalTextId = textTrack.id;
  1370. if (textTrack.kind == 'captions') {
  1371. // See: https://github.com/shaka-project/shaka-player/issues/6233
  1372. track.mimeType = 'unknown';
  1373. }
  1374. if (textTrack.kind == 'subtitles') {
  1375. track.mimeType = 'text/vtt';
  1376. }
  1377. if (textTrack.kind) {
  1378. track.roles = [textTrack.kind];
  1379. }
  1380. if (textTrack.kind == 'forced') {
  1381. track.forced = true;
  1382. }
  1383. return track;
  1384. }
  1385. /**
  1386. * @param {AudioTrack} audioTrack
  1387. * @return {shaka.extern.Track}
  1388. */
  1389. static html5AudioTrackToTrack(audioTrack) {
  1390. const StreamUtils = shaka.util.StreamUtils;
  1391. /** @type {shaka.extern.Track} */
  1392. const track = StreamUtils.html5TrackToGenericShakaTrack_(audioTrack);
  1393. track.active = audioTrack.enabled;
  1394. track.type = 'variant';
  1395. track.originalAudioId = audioTrack.id;
  1396. if (audioTrack.kind == 'main') {
  1397. track.primary = true;
  1398. }
  1399. if (audioTrack.kind) {
  1400. track.roles = [audioTrack.kind];
  1401. track.audioRoles = [audioTrack.kind];
  1402. track.label = audioTrack.label;
  1403. }
  1404. return track;
  1405. }
  1406. /**
  1407. * Creates a Track object with non-type specific fields filled out. The
  1408. * caller is responsible for completing the Track object with any
  1409. * type-specific information (audio or text).
  1410. *
  1411. * @param {TextTrack|AudioTrack} html5Track
  1412. * @return {shaka.extern.Track}
  1413. * @private
  1414. */
  1415. static html5TrackToGenericShakaTrack_(html5Track) {
  1416. const language = html5Track.language;
  1417. /** @type {shaka.extern.Track} */
  1418. const track = {
  1419. id: shaka.util.StreamUtils.html5TrackId(html5Track),
  1420. active: false,
  1421. type: '',
  1422. bandwidth: 0,
  1423. language: shaka.util.LanguageUtils.normalize(language || 'und'),
  1424. label: html5Track.label,
  1425. kind: html5Track.kind,
  1426. width: null,
  1427. height: null,
  1428. frameRate: null,
  1429. pixelAspectRatio: null,
  1430. hdr: null,
  1431. colorGamut: null,
  1432. videoLayout: null,
  1433. mimeType: null,
  1434. audioMimeType: null,
  1435. videoMimeType: null,
  1436. codecs: null,
  1437. audioCodec: null,
  1438. videoCodec: null,
  1439. primary: false,
  1440. roles: [],
  1441. forced: false,
  1442. audioRoles: null,
  1443. videoId: null,
  1444. audioId: null,
  1445. channelsCount: null,
  1446. audioSamplingRate: null,
  1447. spatialAudio: false,
  1448. tilesLayout: null,
  1449. audioBandwidth: null,
  1450. videoBandwidth: null,
  1451. originalVideoId: null,
  1452. originalAudioId: null,
  1453. originalTextId: null,
  1454. originalImageId: null,
  1455. accessibilityPurpose: null,
  1456. originalLanguage: language,
  1457. };
  1458. return track;
  1459. }
  1460. /**
  1461. * Determines if the given variant is playable.
  1462. * @param {!shaka.extern.Variant} variant
  1463. * @return {boolean}
  1464. */
  1465. static isPlayable(variant) {
  1466. return variant.allowedByApplication &&
  1467. variant.allowedByKeySystem &&
  1468. variant.disabledUntilTime == 0;
  1469. }
  1470. /**
  1471. * Filters out unplayable variants.
  1472. * @param {!Array.<!shaka.extern.Variant>} variants
  1473. * @return {!Array.<!shaka.extern.Variant>}
  1474. */
  1475. static getPlayableVariants(variants) {
  1476. return variants.filter((variant) => {
  1477. return shaka.util.StreamUtils.isPlayable(variant);
  1478. });
  1479. }
  1480. /**
  1481. * Chooses streams according to the given config.
  1482. * Works both for Stream and Track types due to their similarities.
  1483. *
  1484. * @param {!Array<!shaka.extern.Stream>|!Array<!shaka.extern.Track>} streams
  1485. * @param {string} preferredLanguage
  1486. * @param {string} preferredRole
  1487. * @param {boolean} preferredForced
  1488. * @return {!Array<!shaka.extern.Stream>|!Array<!shaka.extern.Track>}
  1489. */
  1490. static filterStreamsByLanguageAndRole(
  1491. streams, preferredLanguage, preferredRole, preferredForced) {
  1492. const LanguageUtils = shaka.util.LanguageUtils;
  1493. /** @type {!Array<!shaka.extern.Stream>|!Array<!shaka.extern.Track>} */
  1494. let chosen = streams;
  1495. // Start with the set of primary streams.
  1496. /** @type {!Array<!shaka.extern.Stream>|!Array<!shaka.extern.Track>} */
  1497. const primary = streams.filter((stream) => {
  1498. return stream.primary;
  1499. });
  1500. if (primary.length) {
  1501. chosen = primary;
  1502. }
  1503. // Now reduce the set to one language. This covers both arbitrary language
  1504. // choice and the reduction of the "primary" stream set to one language.
  1505. const firstLanguage = chosen.length ? chosen[0].language : '';
  1506. chosen = chosen.filter((stream) => {
  1507. return stream.language == firstLanguage;
  1508. });
  1509. // Find the streams that best match our language preference. This will
  1510. // override previous selections.
  1511. if (preferredLanguage) {
  1512. const closestLocale = LanguageUtils.findClosestLocale(
  1513. LanguageUtils.normalize(preferredLanguage),
  1514. streams.map((stream) => stream.language));
  1515. // Only replace |chosen| if we found a locale that is close to our
  1516. // preference.
  1517. if (closestLocale) {
  1518. chosen = streams.filter((stream) => {
  1519. const locale = LanguageUtils.normalize(stream.language);
  1520. return locale == closestLocale;
  1521. });
  1522. }
  1523. }
  1524. // Filter by forced preference
  1525. chosen = chosen.filter((stream) => {
  1526. return stream.forced == preferredForced;
  1527. });
  1528. // Now refine the choice based on role preference.
  1529. if (preferredRole) {
  1530. const roleMatches = shaka.util.StreamUtils.filterStreamsByRole_(
  1531. chosen, preferredRole);
  1532. if (roleMatches.length) {
  1533. return roleMatches;
  1534. } else {
  1535. shaka.log.warning('No exact match for the text role could be found.');
  1536. }
  1537. } else {
  1538. // Prefer text streams with no roles, if they exist.
  1539. const noRoleMatches = chosen.filter((stream) => {
  1540. return stream.roles.length == 0;
  1541. });
  1542. if (noRoleMatches.length) {
  1543. return noRoleMatches;
  1544. }
  1545. }
  1546. // Either there was no role preference, or it could not be satisfied.
  1547. // Choose an arbitrary role, if there are any, and filter out any other
  1548. // roles. This ensures we never adapt between roles.
  1549. const allRoles = chosen.map((stream) => {
  1550. return stream.roles;
  1551. }).reduce(shaka.util.Functional.collapseArrays, []);
  1552. if (!allRoles.length) {
  1553. return chosen;
  1554. }
  1555. return shaka.util.StreamUtils.filterStreamsByRole_(chosen, allRoles[0]);
  1556. }
  1557. /**
  1558. * Filter Streams by role.
  1559. * Works both for Stream and Track types due to their similarities.
  1560. *
  1561. * @param {!Array<!shaka.extern.Stream>|!Array<!shaka.extern.Track>} streams
  1562. * @param {string} preferredRole
  1563. * @return {!Array<!shaka.extern.Stream>|!Array<!shaka.extern.Track>}
  1564. * @private
  1565. */
  1566. static filterStreamsByRole_(streams, preferredRole) {
  1567. return streams.filter((stream) => {
  1568. return stream.roles.includes(preferredRole);
  1569. });
  1570. }
  1571. /**
  1572. * Checks if the given stream is an audio stream.
  1573. *
  1574. * @param {shaka.extern.Stream} stream
  1575. * @return {boolean}
  1576. */
  1577. static isAudio(stream) {
  1578. const ContentType = shaka.util.ManifestParserUtils.ContentType;
  1579. return stream.type == ContentType.AUDIO;
  1580. }
  1581. /**
  1582. * Checks if the given stream is a video stream.
  1583. *
  1584. * @param {shaka.extern.Stream} stream
  1585. * @return {boolean}
  1586. */
  1587. static isVideo(stream) {
  1588. const ContentType = shaka.util.ManifestParserUtils.ContentType;
  1589. return stream.type == ContentType.VIDEO;
  1590. }
  1591. /**
  1592. * Get all non-null streams in the variant as an array.
  1593. *
  1594. * @param {shaka.extern.Variant} variant
  1595. * @return {!Array.<shaka.extern.Stream>}
  1596. */
  1597. static getVariantStreams(variant) {
  1598. const streams = [];
  1599. if (variant.audio) {
  1600. streams.push(variant.audio);
  1601. }
  1602. if (variant.video) {
  1603. streams.push(variant.video);
  1604. }
  1605. return streams;
  1606. }
  1607. /**
  1608. * Indicates if some of the variant's streams are fastSwitching.
  1609. *
  1610. * @param {shaka.extern.Variant} variant
  1611. * @return {boolean}
  1612. */
  1613. static isFastSwitching(variant) {
  1614. if (variant.audio && variant.audio.fastSwitching) {
  1615. return true;
  1616. }
  1617. if (variant.video && variant.video.fastSwitching) {
  1618. return true;
  1619. }
  1620. return false;
  1621. }
  1622. /**
  1623. * Returns a string of a variant, with the attribute values of its audio
  1624. * and/or video streams for log printing.
  1625. * @param {shaka.extern.Variant} variant
  1626. * @return {string}
  1627. * @private
  1628. */
  1629. static getVariantSummaryString_(variant) {
  1630. const summaries = [];
  1631. if (variant.audio) {
  1632. summaries.push(shaka.util.StreamUtils.getStreamSummaryString_(
  1633. variant.audio));
  1634. }
  1635. if (variant.video) {
  1636. summaries.push(shaka.util.StreamUtils.getStreamSummaryString_(
  1637. variant.video));
  1638. }
  1639. return summaries.join(', ');
  1640. }
  1641. /**
  1642. * Returns a string of an audio or video stream for log printing.
  1643. * @param {shaka.extern.Stream} stream
  1644. * @return {string}
  1645. * @private
  1646. */
  1647. static getStreamSummaryString_(stream) {
  1648. // Accepted parameters for Chromecast can be found (internally) at
  1649. // go/cast-mime-params
  1650. if (shaka.util.StreamUtils.isAudio(stream)) {
  1651. return 'type=audio' +
  1652. ' codecs=' + stream.codecs +
  1653. ' bandwidth='+ stream.bandwidth +
  1654. ' channelsCount=' + stream.channelsCount +
  1655. ' audioSamplingRate=' + stream.audioSamplingRate;
  1656. }
  1657. if (shaka.util.StreamUtils.isVideo(stream)) {
  1658. return 'type=video' +
  1659. ' codecs=' + stream.codecs +
  1660. ' bandwidth=' + stream.bandwidth +
  1661. ' frameRate=' + stream.frameRate +
  1662. ' width=' + stream.width +
  1663. ' height=' + stream.height;
  1664. }
  1665. return 'unexpected stream type';
  1666. }
  1667. /**
  1668. * Clears underlying decoding config cache.
  1669. */
  1670. static clearDecodingConfigCache() {
  1671. shaka.util.StreamUtils.decodingConfigCache_ = {};
  1672. }
  1673. };
  1674. /**
  1675. * A cache of results from mediaCapabilities.decodingInfo, indexed by the
  1676. * (stringified) decodingConfig.
  1677. *
  1678. * @type {Object.<(!string), (!MediaCapabilitiesDecodingInfo)>}
  1679. * @private
  1680. */
  1681. shaka.util.StreamUtils.decodingConfigCache_ = {};
  1682. /** @private {number} */
  1683. shaka.util.StreamUtils.nextTrackId_ = 0;
  1684. /**
  1685. * @enum {string}
  1686. */
  1687. shaka.util.StreamUtils.DecodingAttributes = {
  1688. SMOOTH: 'smooth',
  1689. POWER: 'powerEfficient',
  1690. };
  1691. /**
  1692. * @private {!Map.<string, boolean>}
  1693. */
  1694. shaka.util.StreamUtils.supportedImageMimeTypes_ = new Map()
  1695. .set('image/svg+xml', true)
  1696. .set('image/png', true)
  1697. .set('image/jpeg', true)
  1698. .set('image/jpg', true);
  1699. /**
  1700. * @const {string}
  1701. * @private
  1702. */
  1703. shaka.util.StreamUtils.minWebPImage_ = 'data:image/webp;base64,UklGRjoAAABXRU' +
  1704. 'JQVlA4IC4AAACyAgCdASoCAAIALmk0mk0iIiIiIgBoSygABc6WWgAA/veff/0PP8bA//LwY' +
  1705. 'AAA';
  1706. /**
  1707. * @const {string}
  1708. * @private
  1709. */
  1710. shaka.util.StreamUtils.minAvifImage_ = 'data:image/avif;base64,AAAAIGZ0eXBhdm' +
  1711. 'lmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljd' +
  1712. 'AAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEA' +
  1713. 'AAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAA' +
  1714. 'AamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAA' +
  1715. 'xhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAA' +
  1716. 'CVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A=';
  1717. /**
  1718. * @const {!Map.<string, string>}
  1719. * @private
  1720. */
  1721. shaka.util.StreamUtils.minImage_ = new Map()
  1722. .set('image/webp', shaka.util.StreamUtils.minWebPImage_)
  1723. .set('image/avif', shaka.util.StreamUtils.minAvifImage_);