Source: lib/util/platform.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.util.Platform');
  7. goog.require('shaka.log');
  8. goog.require('shaka.util.DrmUtils');
  9. goog.require('shaka.util.Timer');
  10. /**
  11. * A wrapper for platform-specific functions.
  12. *
  13. * @final
  14. */
  15. shaka.util.Platform = class {
  16. /**
  17. * Check if the current platform supports media source. We assume that if
  18. * the current platform supports media source, then we can use media source
  19. * as per its design.
  20. *
  21. * @return {boolean}
  22. */
  23. static supportsMediaSource() {
  24. const mediaSource = window.ManagedMediaSource || window.MediaSource;
  25. // Browsers that lack a media source implementation will have no reference
  26. // to |window.MediaSource|. Platforms that we see having problematic media
  27. // source implementations will have this reference removed via a polyfill.
  28. if (!mediaSource) {
  29. return false;
  30. }
  31. // Some very old MediaSource implementations didn't have isTypeSupported.
  32. if (!mediaSource.isTypeSupported) {
  33. return false;
  34. }
  35. return true;
  36. }
  37. /**
  38. * Returns true if the media type is supported natively by the platform.
  39. *
  40. * @param {string} mimeType
  41. * @return {boolean}
  42. */
  43. static supportsMediaType(mimeType) {
  44. const video = shaka.util.Platform.anyMediaElement();
  45. return video.canPlayType(mimeType) != '';
  46. }
  47. /**
  48. * Check if the current platform is MS Edge.
  49. *
  50. * @return {boolean}
  51. */
  52. static isEdge() {
  53. // Legacy Edge contains "Edge/version".
  54. // Chromium-based Edge contains "Edg/version" (no "e").
  55. if (navigator.userAgent.match(/Edge?\//)) {
  56. return true;
  57. }
  58. return false;
  59. }
  60. /**
  61. * Check if the current platform is Legacy Edge.
  62. *
  63. * @return {boolean}
  64. */
  65. static isLegacyEdge() {
  66. // Legacy Edge contains "Edge/version".
  67. // Chromium-based Edge contains "Edg/version" (no "e").
  68. if (navigator.userAgent.match(/Edge\//)) {
  69. return true;
  70. }
  71. return false;
  72. }
  73. /**
  74. * Check if the current platform is MS IE.
  75. *
  76. * @return {boolean}
  77. */
  78. static isIE() {
  79. return shaka.util.Platform.userAgentContains_('Trident/');
  80. }
  81. /**
  82. * Check if the current platform is an Xbox One.
  83. *
  84. * @return {boolean}
  85. */
  86. static isXboxOne() {
  87. return shaka.util.Platform.userAgentContains_('Xbox One');
  88. }
  89. /**
  90. * Check if the current platform is a Tizen TV.
  91. *
  92. * @return {boolean}
  93. */
  94. static isTizen() {
  95. return shaka.util.Platform.userAgentContains_('Tizen');
  96. }
  97. /**
  98. * Check if the current platform is a Tizen 6 TV.
  99. *
  100. * @return {boolean}
  101. */
  102. static isTizen6() {
  103. return shaka.util.Platform.userAgentContains_('Tizen 6');
  104. }
  105. /**
  106. * Check if the current platform is a Tizen 5.0 TV.
  107. *
  108. * @return {boolean}
  109. */
  110. static isTizen5_0() {
  111. return shaka.util.Platform.userAgentContains_('Tizen 5.0');
  112. }
  113. /**
  114. * Check if the current platform is a Tizen 5 TV.
  115. *
  116. * @return {boolean}
  117. */
  118. static isTizen5() {
  119. return shaka.util.Platform.userAgentContains_('Tizen 5');
  120. }
  121. /**
  122. * Check if the current platform is a Tizen 4 TV.
  123. *
  124. * @return {boolean}
  125. */
  126. static isTizen4() {
  127. return shaka.util.Platform.userAgentContains_('Tizen 4');
  128. }
  129. /**
  130. * Check if the current platform is a Tizen 3 TV.
  131. *
  132. * @return {boolean}
  133. */
  134. static isTizen3() {
  135. return shaka.util.Platform.userAgentContains_('Tizen 3');
  136. }
  137. /**
  138. * Check if the current platform is a Tizen 2 TV.
  139. *
  140. * @return {boolean}
  141. */
  142. static isTizen2() {
  143. return shaka.util.Platform.userAgentContains_('Tizen 2');
  144. }
  145. /**
  146. * Check if the current platform is a WebOS.
  147. *
  148. * @return {boolean}
  149. */
  150. static isWebOS() {
  151. return shaka.util.Platform.userAgentContains_('Web0S');
  152. }
  153. /**
  154. * Check if the current platform is a WebOS 3.
  155. *
  156. * @return {boolean}
  157. */
  158. static isWebOS3() {
  159. // See: https://webostv.developer.lge.com/develop/specifications/web-api-and-web-engine#useragent-string
  160. return shaka.util.Platform.isWebOS() &&
  161. shaka.util.Platform.chromeVersion() === 38;
  162. }
  163. /**
  164. * Check if the current platform is a WebOS 4.
  165. *
  166. * @return {boolean}
  167. */
  168. static isWebOS4() {
  169. // See: https://webostv.developer.lge.com/develop/specifications/web-api-and-web-engine#useragent-string
  170. return shaka.util.Platform.isWebOS() &&
  171. shaka.util.Platform.chromeVersion() === 53;
  172. }
  173. /**
  174. * Check if the current platform is a WebOS 5.
  175. *
  176. * @return {boolean}
  177. */
  178. static isWebOS5() {
  179. // See: https://webostv.developer.lge.com/develop/specifications/web-api-and-web-engine#useragent-string
  180. return shaka.util.Platform.isWebOS() &&
  181. shaka.util.Platform.chromeVersion() === 68;
  182. }
  183. /**
  184. * Check if the current platform is a Google Chromecast.
  185. *
  186. * @return {boolean}
  187. */
  188. static isChromecast() {
  189. return shaka.util.Platform.userAgentContains_('CrKey');
  190. }
  191. /**
  192. * Check if the current platform is a Google Chromecast with Android
  193. * (i.e. Chromecast with GoogleTV).
  194. *
  195. * @return {boolean}
  196. */
  197. static isAndroidCastDevice() {
  198. const Platform = shaka.util.Platform;
  199. return Platform.isChromecast() && Platform.isAndroid();
  200. }
  201. /**
  202. * Check if the current platform is a Google Chromecast with Fuchsia
  203. * (i.e. Google Nest Hub).
  204. *
  205. * @return {boolean}
  206. */
  207. static isFuchsiaCastDevice() {
  208. const Platform = shaka.util.Platform;
  209. return Platform.isChromecast() && Platform.isFuchsia();
  210. }
  211. /**
  212. * Returns a major version number for Chrome, or Chromium-based browsers.
  213. *
  214. * For example:
  215. * - Chrome 106.0.5249.61 returns 106.
  216. * - Edge 106.0.1370.34 returns 106 (since this is based on Chromium).
  217. * - Safari returns null (since this is independent of Chromium).
  218. *
  219. * @return {?number} A major version number or null if not Chromium-based.
  220. */
  221. static chromeVersion() {
  222. if (!shaka.util.Platform.isChrome()) {
  223. return null;
  224. }
  225. // Looking for something like "Chrome/106.0.0.0".
  226. const match = navigator.userAgent.match(/Chrome\/(\d+)/);
  227. if (match) {
  228. return parseInt(match[1], /* base= */ 10);
  229. }
  230. return null;
  231. }
  232. /**
  233. * Check if the current platform is Google Chrome.
  234. *
  235. * @return {boolean}
  236. */
  237. static isChrome() {
  238. // The Edge Legacy user agent will also contain the "Chrome" keyword, so we
  239. // need to make sure this is not Edge Legacy.
  240. return shaka.util.Platform.userAgentContains_('Chrome') &&
  241. !shaka.util.Platform.isLegacyEdge();
  242. }
  243. /**
  244. * Check if the current platform is Firefox.
  245. *
  246. * @return {boolean}
  247. */
  248. static isFirefox() {
  249. return shaka.util.Platform.userAgentContains_('Firefox');
  250. }
  251. /**
  252. * Check if the current platform is from Apple.
  253. *
  254. * Returns true on all iOS browsers and on desktop Safari.
  255. *
  256. * Returns false for non-Safari browsers on macOS, which are independent of
  257. * Apple.
  258. *
  259. * @return {boolean}
  260. */
  261. static isApple() {
  262. return !!navigator.vendor && navigator.vendor.includes('Apple') &&
  263. !shaka.util.Platform.isTizen() &&
  264. !shaka.util.Platform.isEOS() &&
  265. !shaka.util.Platform.isAPL() &&
  266. !shaka.util.Platform.isVirginMedia() &&
  267. !shaka.util.Platform.isOrange() &&
  268. !shaka.util.Platform.isPS4() &&
  269. !shaka.util.Platform.isAmazonFireTV() &&
  270. !shaka.util.Platform.isWPE() &&
  271. !shaka.util.Platform.isZenterio() &&
  272. !shaka.util.Platform.isSkyQ();
  273. }
  274. /**
  275. * Check if the current platform is Playstation 5.
  276. *
  277. * Returns true on Playstation 5 browsers.
  278. *
  279. * Returns false for Playstation 5 browsers
  280. *
  281. * @return {boolean}
  282. */
  283. static isPS5() {
  284. return shaka.util.Platform.userAgentContains_('PlayStation 5');
  285. }
  286. /**
  287. * Check if the current platform is Playstation 4.
  288. */
  289. static isPS4() {
  290. return shaka.util.Platform.userAgentContains_('PlayStation 4');
  291. }
  292. /**
  293. * Check if the current platform is Hisense.
  294. */
  295. static isHisense() {
  296. return shaka.util.Platform.userAgentContains_('Hisense') ||
  297. shaka.util.Platform.userAgentContains_('VIDAA');
  298. }
  299. /**
  300. * Check if the current platform is Virgin Media device.
  301. */
  302. static isVirginMedia() {
  303. return shaka.util.Platform.userAgentContains_('VirginMedia');
  304. }
  305. /**
  306. * Check if the current platform is Orange.
  307. */
  308. static isOrange() {
  309. return shaka.util.Platform.userAgentContains_('SOPOpenBrowser');
  310. }
  311. /**
  312. * Check if the current platform is SkyQ STB.
  313. */
  314. static isSkyQ() {
  315. return shaka.util.Platform.userAgentContains_('Sky_STB');
  316. }
  317. /**
  318. * Check if the current platform is Amazon Fire TV.
  319. * https://developer.amazon.com/docs/fire-tv/identify-amazon-fire-tv-devices.html
  320. *
  321. * @return {boolean}
  322. */
  323. static isAmazonFireTV() {
  324. return shaka.util.Platform.userAgentContains_('AFT');
  325. }
  326. /**
  327. * Check if the current platform is Comcast X1.
  328. * @return {boolean}
  329. */
  330. static isWPE() {
  331. return shaka.util.Platform.userAgentContains_('WPE');
  332. }
  333. /**
  334. * Check if the current platform is Deutsche Telecom Zenterio STB.
  335. * @return {boolean}
  336. */
  337. static isZenterio() {
  338. return shaka.util.Platform.userAgentContains_('DT_STB_BCM');
  339. }
  340. /**
  341. * Returns a major version number for Safari, or Safari-based iOS browsers.
  342. *
  343. * For example:
  344. * - Safari 13.0.4 on macOS returns 13.
  345. * - Safari on iOS 13.3.1 returns 13.
  346. * - Chrome on iOS 13.3.1 returns 13 (since this is based on Safari/WebKit).
  347. * - Chrome on macOS returns null (since this is independent of Apple).
  348. *
  349. * Returns null on Firefox on iOS, where this version information is not
  350. * available.
  351. *
  352. * @return {?number} A major version number or null if not iOS.
  353. */
  354. static safariVersion() {
  355. // All iOS browsers and desktop Safari will return true for isApple().
  356. if (!shaka.util.Platform.isApple()) {
  357. return null;
  358. }
  359. // This works for iOS Safari and desktop Safari, which contain something
  360. // like "Version/13.0" indicating the major Safari or iOS version.
  361. let match = navigator.userAgent.match(/Version\/(\d+)/);
  362. if (match) {
  363. return parseInt(match[1], /* base= */ 10);
  364. }
  365. // This works for all other browsers on iOS, which contain something like
  366. // "OS 13_3" indicating the major & minor iOS version.
  367. match = navigator.userAgent.match(/OS (\d+)(?:_\d+)?/);
  368. if (match) {
  369. return parseInt(match[1], /* base= */ 10);
  370. }
  371. return null;
  372. }
  373. /**
  374. * Check if the current platform is Apple Safari
  375. * or Safari-based iOS browsers.
  376. *
  377. * @return {boolean}
  378. */
  379. static isSafari() {
  380. return !!shaka.util.Platform.safariVersion();
  381. }
  382. /**
  383. * Check if the current platform is an EOS set-top box.
  384. *
  385. * @return {boolean}
  386. */
  387. static isEOS() {
  388. return shaka.util.Platform.userAgentContains_('PC=EOS');
  389. }
  390. /**
  391. * Check if the current platform is an APL set-top box.
  392. *
  393. * @return {boolean}
  394. */
  395. static isAPL() {
  396. return shaka.util.Platform.userAgentContains_('PC=APL');
  397. }
  398. /**
  399. * Guesses if the platform is a mobile one (iOS or Android).
  400. *
  401. * @return {boolean}
  402. */
  403. static isMobile() {
  404. if (/(?:iPhone|iPad|iPod|Android)/.test(navigator.userAgent)) {
  405. // This is Android, iOS, or iPad < 13.
  406. return true;
  407. }
  408. // Starting with iOS 13 on iPad, the user agent string no longer has the
  409. // word "iPad" in it. It looks very similar to desktop Safari. This seems
  410. // to be intentional on Apple's part.
  411. // See: https://forums.developer.apple.com/thread/119186
  412. //
  413. // So if it's an Apple device with multi-touch support, assume it's a mobile
  414. // device. If some future iOS version starts masking their user agent on
  415. // both iPhone & iPad, this clause should still work. If a future
  416. // multi-touch desktop Mac is released, this will need some adjustment.
  417. //
  418. // As of January 2020, this is mainly used to adjust the default UI config
  419. // for mobile devices, so it's low risk if something changes to break this
  420. // detection.
  421. return shaka.util.Platform.isApple() && navigator.maxTouchPoints > 1;
  422. }
  423. /**
  424. * Return true if the platform is a Mac, regardless of the browser.
  425. *
  426. * @return {boolean}
  427. */
  428. static isMac() {
  429. // Try the newer standard first.
  430. if (navigator.userAgentData && navigator.userAgentData.platform) {
  431. return navigator.userAgentData.platform.toLowerCase() == 'macos';
  432. }
  433. // Fall back to the old API, with less strict matching.
  434. if (!navigator.platform) {
  435. return false;
  436. }
  437. return navigator.platform.toLowerCase().includes('mac');
  438. }
  439. /**
  440. * Return true if the platform is a Windows, regardless of the browser.
  441. *
  442. * @return {boolean}
  443. */
  444. static isWindows() {
  445. // Try the newer standard first.
  446. if (navigator.userAgentData && navigator.userAgentData.platform) {
  447. return navigator.userAgentData.platform.toLowerCase() == 'windows';
  448. }
  449. // Fall back to the old API, with less strict matching.
  450. if (!navigator.platform) {
  451. return false;
  452. }
  453. return navigator.platform.toLowerCase().includes('windows');
  454. }
  455. /**
  456. * Return true if the platform is a Android, regardless of the browser.
  457. *
  458. * @return {boolean}
  459. */
  460. static isAndroid() {
  461. return shaka.util.Platform.userAgentContains_('Android');
  462. }
  463. /**
  464. * Return true if the platform is a Fuchsia, regardless of the browser.
  465. *
  466. * @return {boolean}
  467. */
  468. static isFuchsia() {
  469. return shaka.util.Platform.userAgentContains_('Fuchsia');
  470. }
  471. /**
  472. * Return true if the platform is controlled by a remote control.
  473. *
  474. * @return {boolean}
  475. */
  476. static isSmartTV() {
  477. const Platform = shaka.util.Platform;
  478. if (Platform.isTizen() || Platform.isWebOS() ||
  479. Platform.isXboxOne() || Platform.isPS4() ||
  480. Platform.isPS5() || Platform.isAmazonFireTV() ||
  481. Platform.isEOS() || Platform.isAPL() ||
  482. Platform.isVirginMedia() || Platform.isOrange() ||
  483. Platform.isWPE() || Platform.isChromecast() ||
  484. Platform.isHisense() || Platform.isZenterio()) {
  485. return true;
  486. }
  487. return false;
  488. }
  489. /**
  490. * Check if the user agent contains a key. This is the best way we know of
  491. * right now to detect platforms. If there is a better way, please send a
  492. * PR.
  493. *
  494. * @param {string} key
  495. * @return {boolean}
  496. * @private
  497. */
  498. static userAgentContains_(key) {
  499. const userAgent = navigator.userAgent || '';
  500. return userAgent.includes(key);
  501. }
  502. /**
  503. * For canPlayType queries, we just need any instance.
  504. *
  505. * First, use a cached element from a previous query.
  506. * Second, search the page for one.
  507. * Third, create a temporary one.
  508. *
  509. * Cached elements expire in one second so that they can be GC'd or removed.
  510. *
  511. * @return {!HTMLMediaElement}
  512. */
  513. static anyMediaElement() {
  514. const Platform = shaka.util.Platform;
  515. if (Platform.cachedMediaElement_) {
  516. return Platform.cachedMediaElement_;
  517. }
  518. if (!Platform.cacheExpirationTimer_) {
  519. Platform.cacheExpirationTimer_ = new shaka.util.Timer(() => {
  520. Platform.cachedMediaElement_ = null;
  521. });
  522. }
  523. Platform.cachedMediaElement_ = /** @type {HTMLMediaElement} */(
  524. document.getElementsByTagName('video')[0] ||
  525. document.getElementsByTagName('audio')[0]);
  526. if (!Platform.cachedMediaElement_) {
  527. Platform.cachedMediaElement_ = /** @type {!HTMLMediaElement} */(
  528. document.createElement('video'));
  529. }
  530. Platform.cacheExpirationTimer_.tickAfter(/* seconds= */ 1);
  531. return Platform.cachedMediaElement_;
  532. }
  533. /**
  534. * Returns true if the platform requires encryption information in all init
  535. * segments. For such platforms, MediaSourceEngine will attempt to work
  536. * around a lack of such info by inserting fake encryption information into
  537. * initialization segments.
  538. *
  539. * @param {?string} keySystem
  540. * @return {boolean}
  541. * @see https://github.com/shaka-project/shaka-player/issues/2759
  542. */
  543. static requiresEncryptionInfoInAllInitSegments(keySystem) {
  544. const Platform = shaka.util.Platform;
  545. const isPlayReady = shaka.util.DrmUtils.isPlayReadyKeySystem(keySystem);
  546. return Platform.isTizen() || Platform.isXboxOne() || Platform.isOrange() ||
  547. (Platform.isEdge() && Platform.isWindows() && isPlayReady);
  548. }
  549. /**
  550. * Returns true if the platform supports SourceBuffer "sequence mode".
  551. *
  552. * @return {boolean}
  553. */
  554. static supportsSequenceMode() {
  555. const Platform = shaka.util.Platform;
  556. if (Platform.isTizen3() || Platform.isTizen2() ||
  557. Platform.isWebOS3() || Platform.isPS4()) {
  558. return false;
  559. }
  560. return true;
  561. }
  562. /**
  563. * Returns if codec switching SMOOTH is known reliable device support.
  564. *
  565. * Some devices are known not to support `MediaSource.changeType`
  566. * well. These devices should use the reload strategy. If a device
  567. * reports that it supports `changeType` but support it unreliably
  568. * it should be added to this list.
  569. *
  570. * @return {boolean}
  571. */
  572. static supportsSmoothCodecSwitching() {
  573. const Platform = shaka.util.Platform;
  574. if (Platform.isTizen2() || Platform.isTizen3() || Platform.isTizen4() ||
  575. Platform.isTizen5() || Platform.isTizen6() || Platform.isWebOS3() ||
  576. Platform.isWebOS4() || Platform.isWebOS5() || Platform.isPS4() ||
  577. Platform.isPS5()) {
  578. return false;
  579. }
  580. // Older chromecasts without GoogleTV seem to not support SMOOTH properly.
  581. if (Platform.isChromecast() && !Platform.isAndroidCastDevice() &&
  582. !Platform.isFuchsiaCastDevice()) {
  583. return false;
  584. }
  585. // See: https://chromium-review.googlesource.com/c/chromium/src/+/4577759
  586. if (Platform.isWindows() && Platform.isEdge()) {
  587. return false;
  588. }
  589. return true;
  590. }
  591. /**
  592. * On some platforms, such as v1 Chromecasts, the act of seeking can take a
  593. * significant amount of time.
  594. *
  595. * @return {boolean}
  596. */
  597. static isSeekingSlow() {
  598. const Platform = shaka.util.Platform;
  599. if (Platform.isChromecast()) {
  600. if (Platform.isAndroidCastDevice()) {
  601. // Android-based Chromecasts are new enough to not be a problem.
  602. return false;
  603. } else {
  604. return true;
  605. }
  606. }
  607. return false;
  608. }
  609. /**
  610. * Returns true if MediaKeys is polyfilled
  611. *
  612. * @param {string=} polyfillType
  613. * @return {boolean}
  614. */
  615. static isMediaKeysPolyfilled(polyfillType) {
  616. if (polyfillType) {
  617. return polyfillType === window.shakaMediaKeysPolyfill;
  618. }
  619. return !!window.shakaMediaKeysPolyfill;
  620. }
  621. /**
  622. * Detect the maximum resolution that the platform's hardware can handle.
  623. *
  624. * @return {!Promise.<shaka.extern.Resolution>}
  625. */
  626. static async detectMaxHardwareResolution() {
  627. const Platform = shaka.util.Platform;
  628. /** @type {shaka.extern.Resolution} */
  629. const maxResolution = {
  630. width: Infinity,
  631. height: Infinity,
  632. };
  633. if (Platform.isChromecast()) {
  634. // In our tests, the original Chromecast seems to have trouble decoding
  635. // above 1080p. It would be a waste to select a higher res anyway, given
  636. // that the device only outputs 1080p to begin with.
  637. // Chromecast has an extension to query the device/display's resolution.
  638. const hasCanDisplayType = window.cast && cast.__platform__ &&
  639. cast.__platform__.canDisplayType;
  640. // Some hub devices can only do 720p. Default to that.
  641. maxResolution.width = 1280;
  642. maxResolution.height = 720;
  643. try {
  644. if (hasCanDisplayType && await cast.__platform__.canDisplayType(
  645. 'video/mp4; codecs="avc1.640028"; width=3840; height=2160')) {
  646. // The device and display can both do 4k. Assume a 4k limit.
  647. maxResolution.width = 3840;
  648. maxResolution.height = 2160;
  649. } else if (hasCanDisplayType && await cast.__platform__.canDisplayType(
  650. 'video/mp4; codecs="avc1.640028"; width=1920; height=1080')) {
  651. // Most Chromecasts can do 1080p.
  652. maxResolution.width = 1920;
  653. maxResolution.height = 1080;
  654. }
  655. } catch (error) {
  656. // This shouldn't generally happen. Log the error.
  657. shaka.log.alwaysError('Failed to check canDisplayType:', error);
  658. // Now ignore the error and let the 720p default stand.
  659. }
  660. } else if (Platform.isTizen()) {
  661. maxResolution.width = 1920;
  662. maxResolution.height = 1080;
  663. try {
  664. if (webapis.systeminfo && webapis.systeminfo.getMaxVideoResolution) {
  665. const maxVideoResolution =
  666. webapis.systeminfo.getMaxVideoResolution();
  667. maxResolution.width = maxVideoResolution.width;
  668. maxResolution.height = maxVideoResolution.height;
  669. } else {
  670. if (webapis.productinfo.is8KPanelSupported &&
  671. webapis.productinfo.is8KPanelSupported()) {
  672. maxResolution.width = 7680;
  673. maxResolution.height = 4320;
  674. } else if (webapis.productinfo.isUdPanelSupported &&
  675. webapis.productinfo.isUdPanelSupported()) {
  676. maxResolution.width = 3840;
  677. maxResolution.height = 2160;
  678. }
  679. }
  680. } catch (e) {
  681. shaka.log.alwaysWarn('Tizen: Error detecting screen size, default ' +
  682. 'screen size 1920x1080.');
  683. }
  684. } else if (Platform.isXboxOne()) {
  685. maxResolution.width = 1920;
  686. maxResolution.height = 1080;
  687. try {
  688. let winRT = undefined;
  689. // Try to access to WinRT for WebView, if it's not defined,
  690. // try to access to WinRT for WebView2, if it's not defined either,
  691. // let it throw.
  692. if (typeof Windows !== 'undefined') {
  693. winRT = Windows;
  694. } else {
  695. winRT = chrome.webview.hostObjects.sync.Windows;
  696. }
  697. const protectionCapabilities =
  698. new winRT.Media.Protection.ProtectionCapabilities();
  699. const protectionResult =
  700. winRT.Media.Protection.ProtectionCapabilityResult;
  701. // isTypeSupported may return "maybe", which means the operation is not
  702. // completed. This means we need to retry
  703. // https://learn.microsoft.com/en-us/uwp/api/windows.media.protection.protectioncapabilityresult?view=winrt-22621
  704. let result = null;
  705. const type =
  706. 'video/mp4;codecs="hvc1,mp4a";features="decode-res-x=3840,' +
  707. 'decode-res-y=2160,decode-bitrate=20000,decode-fps=30,' +
  708. 'decode-bpc=10,display-res-x=3840,display-res-y=2160,' +
  709. 'display-bpc=8"';
  710. const keySystem = 'com.microsoft.playready.recommendation';
  711. do {
  712. result = protectionCapabilities.isTypeSupported(type, keySystem);
  713. } while (result === protectionResult.maybe);
  714. if (result === protectionResult.probably) {
  715. maxResolution.width = 3840;
  716. maxResolution.height = 2160;
  717. }
  718. } catch (e) {
  719. shaka.log.alwaysWarn('Xbox: Error detecting screen size, default ' +
  720. 'screen size 1920x1080.');
  721. }
  722. } else if (Platform.isWebOS()) {
  723. try {
  724. const deviceInfo =
  725. /** @type {{screenWidth: number, screenHeight: number}} */(
  726. JSON.parse(window.PalmSystem.deviceInfo));
  727. // WebOS has always been able to do 1080p. Assume a 1080p limit.
  728. maxResolution.width = Math.max(1920, deviceInfo['screenWidth']);
  729. maxResolution.height = Math.max(1080, deviceInfo['screenHeight']);
  730. } catch (e) {
  731. shaka.log.alwaysWarn('WebOS: Error detecting screen size, default ' +
  732. 'screen size 1920x1080.');
  733. maxResolution.width = 1920;
  734. maxResolution.height = 1080;
  735. }
  736. } else if (Platform.isHisense()) {
  737. // eslint-disable-next-line new-cap
  738. if (window.Hisense_Get4KSupportState &&
  739. // eslint-disable-next-line new-cap
  740. window.Hisense_Get4KSupportState()) {
  741. maxResolution.width = 3840;
  742. maxResolution.height = 2160;
  743. } else {
  744. maxResolution.width = 1920;
  745. maxResolution.height = 1080;
  746. }
  747. } else if (Platform.isPS4() || Platform.isPS5()) {
  748. let supports4K = false;
  749. try {
  750. const result = await window.msdk.device.getDisplayInfo();
  751. supports4K = result.resolution === '4K';
  752. } catch (e) {
  753. try {
  754. const result = await window.msdk.device.getDisplayInfoImmediate();
  755. supports4K = result.resolution === '4K';
  756. } catch (e) {
  757. shaka.log.alwaysWarn(
  758. 'PlayStation: Failed to get the display info:', e);
  759. }
  760. }
  761. if (supports4K) {
  762. maxResolution.width = 3840;
  763. maxResolution.height = 2160;
  764. } else {
  765. maxResolution.width = 1920;
  766. maxResolution.height = 1080;
  767. }
  768. }
  769. return maxResolution;
  770. }
  771. };
  772. /** @private {shaka.util.Timer} */
  773. shaka.util.Platform.cacheExpirationTimer_ = null;
  774. /** @private {HTMLMediaElement} */
  775. shaka.util.Platform.cachedMediaElement_ = null;