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. }
  273. /**
  274. * Check if the current platform is Playstation 5.
  275. *
  276. * Returns true on Playstation 5 browsers.
  277. *
  278. * Returns false for Playstation 5 browsers
  279. *
  280. * @return {boolean}
  281. */
  282. static isPS5() {
  283. return shaka.util.Platform.userAgentContains_('PlayStation 5');
  284. }
  285. /**
  286. * Check if the current platform is Playstation 4.
  287. */
  288. static isPS4() {
  289. return shaka.util.Platform.userAgentContains_('PlayStation 4');
  290. }
  291. /**
  292. * Check if the current platform is Hisense.
  293. */
  294. static isHisense() {
  295. return shaka.util.Platform.userAgentContains_('Hisense') ||
  296. shaka.util.Platform.userAgentContains_('VIDAA');
  297. }
  298. /**
  299. * Check if the current platform is Virgin Media device.
  300. */
  301. static isVirginMedia() {
  302. return shaka.util.Platform.userAgentContains_('VirginMedia');
  303. }
  304. /**
  305. * Check if the current platform is Orange.
  306. */
  307. static isOrange() {
  308. return shaka.util.Platform.userAgentContains_('SOPOpenBrowser');
  309. }
  310. /**
  311. * Check if the current platform is Amazon Fire TV.
  312. * https://developer.amazon.com/docs/fire-tv/identify-amazon-fire-tv-devices.html
  313. *
  314. * @return {boolean}
  315. */
  316. static isAmazonFireTV() {
  317. return shaka.util.Platform.userAgentContains_('AFT');
  318. }
  319. /**
  320. * Check if the current platform is Comcast X1.
  321. * @return {boolean}
  322. */
  323. static isWPE() {
  324. return shaka.util.Platform.userAgentContains_('WPE');
  325. }
  326. /**
  327. * Check if the current platform is Deutsche Telecom Zenterio STB.
  328. * @return {boolean}
  329. */
  330. static isZenterio() {
  331. return shaka.util.Platform.userAgentContains_('DT_STB_BCM');
  332. }
  333. /**
  334. * Returns a major version number for Safari, or Safari-based iOS browsers.
  335. *
  336. * For example:
  337. * - Safari 13.0.4 on macOS returns 13.
  338. * - Safari on iOS 13.3.1 returns 13.
  339. * - Chrome on iOS 13.3.1 returns 13 (since this is based on Safari/WebKit).
  340. * - Chrome on macOS returns null (since this is independent of Apple).
  341. *
  342. * Returns null on Firefox on iOS, where this version information is not
  343. * available.
  344. *
  345. * @return {?number} A major version number or null if not iOS.
  346. */
  347. static safariVersion() {
  348. // All iOS browsers and desktop Safari will return true for isApple().
  349. if (!shaka.util.Platform.isApple()) {
  350. return null;
  351. }
  352. // This works for iOS Safari and desktop Safari, which contain something
  353. // like "Version/13.0" indicating the major Safari or iOS version.
  354. let match = navigator.userAgent.match(/Version\/(\d+)/);
  355. if (match) {
  356. return parseInt(match[1], /* base= */ 10);
  357. }
  358. // This works for all other browsers on iOS, which contain something like
  359. // "OS 13_3" indicating the major & minor iOS version.
  360. match = navigator.userAgent.match(/OS (\d+)(?:_\d+)?/);
  361. if (match) {
  362. return parseInt(match[1], /* base= */ 10);
  363. }
  364. return null;
  365. }
  366. /**
  367. * Check if the current platform is Apple Safari
  368. * or Safari-based iOS browsers.
  369. *
  370. * @return {boolean}
  371. */
  372. static isSafari() {
  373. return !!shaka.util.Platform.safariVersion();
  374. }
  375. /**
  376. * Check if the current platform is an EOS set-top box.
  377. *
  378. * @return {boolean}
  379. */
  380. static isEOS() {
  381. return shaka.util.Platform.userAgentContains_('PC=EOS');
  382. }
  383. /**
  384. * Check if the current platform is an APL set-top box.
  385. *
  386. * @return {boolean}
  387. */
  388. static isAPL() {
  389. return shaka.util.Platform.userAgentContains_('PC=APL');
  390. }
  391. /**
  392. * Guesses if the platform is a mobile one (iOS or Android).
  393. *
  394. * @return {boolean}
  395. */
  396. static isMobile() {
  397. if (/(?:iPhone|iPad|iPod|Android)/.test(navigator.userAgent)) {
  398. // This is Android, iOS, or iPad < 13.
  399. return true;
  400. }
  401. // Starting with iOS 13 on iPad, the user agent string no longer has the
  402. // word "iPad" in it. It looks very similar to desktop Safari. This seems
  403. // to be intentional on Apple's part.
  404. // See: https://forums.developer.apple.com/thread/119186
  405. //
  406. // So if it's an Apple device with multi-touch support, assume it's a mobile
  407. // device. If some future iOS version starts masking their user agent on
  408. // both iPhone & iPad, this clause should still work. If a future
  409. // multi-touch desktop Mac is released, this will need some adjustment.
  410. //
  411. // As of January 2020, this is mainly used to adjust the default UI config
  412. // for mobile devices, so it's low risk if something changes to break this
  413. // detection.
  414. return shaka.util.Platform.isApple() && navigator.maxTouchPoints > 1;
  415. }
  416. /**
  417. * Return true if the platform is a Mac, regardless of the browser.
  418. *
  419. * @return {boolean}
  420. */
  421. static isMac() {
  422. // Try the newer standard first.
  423. if (navigator.userAgentData && navigator.userAgentData.platform) {
  424. return navigator.userAgentData.platform.toLowerCase() == 'macos';
  425. }
  426. // Fall back to the old API, with less strict matching.
  427. if (!navigator.platform) {
  428. return false;
  429. }
  430. return navigator.platform.toLowerCase().includes('mac');
  431. }
  432. /**
  433. * Return true if the platform is a Windows, regardless of the browser.
  434. *
  435. * @return {boolean}
  436. */
  437. static isWindows() {
  438. // Try the newer standard first.
  439. if (navigator.userAgentData && navigator.userAgentData.platform) {
  440. return navigator.userAgentData.platform.toLowerCase() == 'windows';
  441. }
  442. // Fall back to the old API, with less strict matching.
  443. if (!navigator.platform) {
  444. return false;
  445. }
  446. return navigator.platform.toLowerCase().includes('windows');
  447. }
  448. /**
  449. * Return true if the platform is a Android, regardless of the browser.
  450. *
  451. * @return {boolean}
  452. */
  453. static isAndroid() {
  454. return shaka.util.Platform.userAgentContains_('Android');
  455. }
  456. /**
  457. * Return true if the platform is a Fuchsia, regardless of the browser.
  458. *
  459. * @return {boolean}
  460. */
  461. static isFuchsia() {
  462. return shaka.util.Platform.userAgentContains_('Fuchsia');
  463. }
  464. /**
  465. * Return true if the platform is controlled by a remote control.
  466. *
  467. * @return {boolean}
  468. */
  469. static isSmartTV() {
  470. const Platform = shaka.util.Platform;
  471. if (Platform.isTizen() || Platform.isWebOS() ||
  472. Platform.isXboxOne() || Platform.isPS4() ||
  473. Platform.isPS5() || Platform.isAmazonFireTV() ||
  474. Platform.isEOS() || Platform.isAPL() ||
  475. Platform.isVirginMedia() || Platform.isOrange() ||
  476. Platform.isWPE() || Platform.isChromecast() ||
  477. Platform.isHisense() || Platform.isZenterio()) {
  478. return true;
  479. }
  480. return false;
  481. }
  482. /**
  483. * Check if the user agent contains a key. This is the best way we know of
  484. * right now to detect platforms. If there is a better way, please send a
  485. * PR.
  486. *
  487. * @param {string} key
  488. * @return {boolean}
  489. * @private
  490. */
  491. static userAgentContains_(key) {
  492. const userAgent = navigator.userAgent || '';
  493. return userAgent.includes(key);
  494. }
  495. /**
  496. * For canPlayType queries, we just need any instance.
  497. *
  498. * First, use a cached element from a previous query.
  499. * Second, search the page for one.
  500. * Third, create a temporary one.
  501. *
  502. * Cached elements expire in one second so that they can be GC'd or removed.
  503. *
  504. * @return {!HTMLMediaElement}
  505. */
  506. static anyMediaElement() {
  507. const Platform = shaka.util.Platform;
  508. if (Platform.cachedMediaElement_) {
  509. return Platform.cachedMediaElement_;
  510. }
  511. if (!Platform.cacheExpirationTimer_) {
  512. Platform.cacheExpirationTimer_ = new shaka.util.Timer(() => {
  513. Platform.cachedMediaElement_ = null;
  514. });
  515. }
  516. Platform.cachedMediaElement_ = /** @type {HTMLMediaElement} */(
  517. document.getElementsByTagName('video')[0] ||
  518. document.getElementsByTagName('audio')[0]);
  519. if (!Platform.cachedMediaElement_) {
  520. Platform.cachedMediaElement_ = /** @type {!HTMLMediaElement} */(
  521. document.createElement('video'));
  522. }
  523. Platform.cacheExpirationTimer_.tickAfter(/* seconds= */ 1);
  524. return Platform.cachedMediaElement_;
  525. }
  526. /**
  527. * Returns true if the platform requires encryption information in all init
  528. * segments. For such platforms, MediaSourceEngine will attempt to work
  529. * around a lack of such info by inserting fake encryption information into
  530. * initialization segments.
  531. *
  532. * @param {?string} keySystem
  533. * @return {boolean}
  534. * @see https://github.com/shaka-project/shaka-player/issues/2759
  535. */
  536. static requiresEncryptionInfoInAllInitSegments(keySystem) {
  537. const Platform = shaka.util.Platform;
  538. const isPlayReady = shaka.util.DrmUtils.isPlayReadyKeySystem(keySystem);
  539. return Platform.isTizen() || Platform.isXboxOne() || Platform.isOrange() ||
  540. (Platform.isEdge() && Platform.isWindows() && isPlayReady);
  541. }
  542. /**
  543. * Returns true if the platform supports SourceBuffer "sequence mode".
  544. *
  545. * @return {boolean}
  546. */
  547. static supportsSequenceMode() {
  548. const Platform = shaka.util.Platform;
  549. if (Platform.isTizen3() || Platform.isTizen2() ||
  550. Platform.isWebOS3() || Platform.isPS4()) {
  551. return false;
  552. }
  553. return true;
  554. }
  555. /**
  556. * Returns if codec switching SMOOTH is known reliable device support.
  557. *
  558. * Some devices are known not to support `MediaSource.changeType`
  559. * well. These devices should use the reload strategy. If a device
  560. * reports that it supports `changeType` but support it reliabley
  561. * it should be added to this list.
  562. *
  563. * @return {boolean}
  564. */
  565. static supportsSmoothCodecSwitching() {
  566. const Platform = shaka.util.Platform;
  567. if (Platform.isTizen2() || Platform.isTizen3() || Platform.isTizen4() ||
  568. Platform.isTizen5() || Platform.isTizen6() || Platform.isWebOS3() ||
  569. Platform.isWebOS4() || Platform.isWebOS5()) {
  570. return false;
  571. }
  572. // Older chromecasts without GoogleTV seem to not support SMOOTH properly.
  573. if (Platform.isChromecast() && !Platform.isAndroidCastDevice() &&
  574. !Platform.isFuchsiaCastDevice()) {
  575. return false;
  576. }
  577. // See: https://chromium-review.googlesource.com/c/chromium/src/+/4577759
  578. if (Platform.isWindows() && Platform.isEdge()) {
  579. return false;
  580. }
  581. return true;
  582. }
  583. /**
  584. * On some platforms, such as v1 Chromecasts, the act of seeking can take a
  585. * significant amount of time.
  586. *
  587. * @return {boolean}
  588. */
  589. static isSeekingSlow() {
  590. const Platform = shaka.util.Platform;
  591. if (Platform.isChromecast()) {
  592. if (Platform.isAndroidCastDevice()) {
  593. // Android-based Chromecasts are new enough to not be a problem.
  594. return false;
  595. } else {
  596. return true;
  597. }
  598. }
  599. return false;
  600. }
  601. /**
  602. * Returns true if MediaKeys is polyfilled
  603. *
  604. * @param {string=} polyfillType
  605. * @return {boolean}
  606. */
  607. static isMediaKeysPolyfilled(polyfillType) {
  608. if (polyfillType) {
  609. return polyfillType === window.shakaMediaKeysPolyfill;
  610. }
  611. return !!window.shakaMediaKeysPolyfill;
  612. }
  613. /**
  614. * Detect the maximum resolution that the platform's hardware can handle.
  615. *
  616. * @return {!Promise.<shaka.extern.Resolution>}
  617. */
  618. static async detectMaxHardwareResolution() {
  619. const Platform = shaka.util.Platform;
  620. /** @type {shaka.extern.Resolution} */
  621. const maxResolution = {
  622. width: Infinity,
  623. height: Infinity,
  624. };
  625. if (Platform.isChromecast()) {
  626. // In our tests, the original Chromecast seems to have trouble decoding
  627. // above 1080p. It would be a waste to select a higher res anyway, given
  628. // that the device only outputs 1080p to begin with.
  629. // Chromecast has an extension to query the device/display's resolution.
  630. const hasCanDisplayType = window.cast && cast.__platform__ &&
  631. cast.__platform__.canDisplayType;
  632. // Some hub devices can only do 720p. Default to that.
  633. maxResolution.width = 1280;
  634. maxResolution.height = 720;
  635. try {
  636. if (hasCanDisplayType && await cast.__platform__.canDisplayType(
  637. 'video/mp4; codecs="avc1.640028"; width=3840; height=2160')) {
  638. // The device and display can both do 4k. Assume a 4k limit.
  639. maxResolution.width = 3840;
  640. maxResolution.height = 2160;
  641. } else if (hasCanDisplayType && await cast.__platform__.canDisplayType(
  642. 'video/mp4; codecs="avc1.640028"; width=1920; height=1080')) {
  643. // Most Chromecasts can do 1080p.
  644. maxResolution.width = 1920;
  645. maxResolution.height = 1080;
  646. }
  647. } catch (error) {
  648. // This shouldn't generally happen. Log the error.
  649. shaka.log.alwaysError('Failed to check canDisplayType:', error);
  650. // Now ignore the error and let the 720p default stand.
  651. }
  652. } else if (Platform.isTizen()) {
  653. maxResolution.width = 1920;
  654. maxResolution.height = 1080;
  655. try {
  656. if (webapis.systeminfo && webapis.systeminfo.getMaxVideoResolution) {
  657. const maxVideoResolution =
  658. webapis.systeminfo.getMaxVideoResolution();
  659. maxResolution.width = maxVideoResolution.width;
  660. maxResolution.height = maxVideoResolution.height;
  661. } else {
  662. if (webapis.productinfo.is8KPanelSupported &&
  663. webapis.productinfo.is8KPanelSupported()) {
  664. maxResolution.width = 7680;
  665. maxResolution.height = 4320;
  666. } else if (webapis.productinfo.isUdPanelSupported &&
  667. webapis.productinfo.isUdPanelSupported()) {
  668. maxResolution.width = 3840;
  669. maxResolution.height = 2160;
  670. }
  671. }
  672. } catch (e) {
  673. shaka.log.alwaysWarn('Tizen: Error detecting screen size, default ' +
  674. 'screen size 1920x1080.');
  675. }
  676. } else if (Platform.isXboxOne()) {
  677. maxResolution.width = 1920;
  678. maxResolution.height = 1080;
  679. try {
  680. let winRT = undefined;
  681. // Try to access to WinRT for WebView, if it's not defined,
  682. // try to access to WinRT for WebView2, if it's not defined either,
  683. // let it throw.
  684. if (typeof Windows !== 'undefined') {
  685. winRT = Windows;
  686. } else {
  687. winRT = chrome.webview.hostObjects.sync.Windows;
  688. }
  689. const protectionCapabilities =
  690. new winRT.Media.Protection.ProtectionCapabilities();
  691. const protectionResult =
  692. winRT.Media.Protection.ProtectionCapabilityResult;
  693. // isTypeSupported may return "maybe", which means the operation is not
  694. // completed. This means we need to retry
  695. // https://learn.microsoft.com/en-us/uwp/api/windows.media.protection.protectioncapabilityresult?view=winrt-22621
  696. let result = null;
  697. const type =
  698. 'video/mp4;codecs="hvc1,mp4a";features="decode-res-x=3840,' +
  699. 'decode-res-y=2160,decode-bitrate=20000,decode-fps=30,' +
  700. 'decode-bpc=10,display-res-x=3840,display-res-y=2160,' +
  701. 'display-bpc=8"';
  702. const keySystem = 'com.microsoft.playready.recommendation';
  703. do {
  704. result = protectionCapabilities.isTypeSupported(type, keySystem);
  705. } while (result === protectionResult.maybe);
  706. if (result === protectionResult.probably) {
  707. maxResolution.width = 3840;
  708. maxResolution.height = 2160;
  709. }
  710. } catch (e) {
  711. shaka.log.alwaysWarn('Xbox: Error detecting screen size, default ' +
  712. 'screen size 1920x1080.');
  713. }
  714. } else if (Platform.isWebOS()) {
  715. try {
  716. const deviceInfo =
  717. /** @type {{screenWidth: number, screenHeight: number}} */(
  718. JSON.parse(window.PalmSystem.deviceInfo));
  719. // WebOS has always been able to do 1080p. Assume a 1080p limit.
  720. maxResolution.width = Math.max(1920, deviceInfo['screenWidth']);
  721. maxResolution.height = Math.max(1080, deviceInfo['screenHeight']);
  722. } catch (e) {
  723. shaka.log.alwaysWarn('WebOS: Error detecting screen size, default ' +
  724. 'screen size 1920x1080.');
  725. maxResolution.width = 1920;
  726. maxResolution.height = 1080;
  727. }
  728. } else if (Platform.isHisense()) {
  729. // eslint-disable-next-line new-cap
  730. if (window.Hisense_Get4KSupportState &&
  731. // eslint-disable-next-line new-cap
  732. window.Hisense_Get4KSupportState()) {
  733. maxResolution.width = 3840;
  734. maxResolution.height = 2160;
  735. } else {
  736. maxResolution.width = 1920;
  737. maxResolution.height = 1080;
  738. }
  739. } else if (Platform.isPS4() || Platform.isPS5()) {
  740. let supports4K = false;
  741. try {
  742. const result = await window.msdk.device.getDisplayInfo();
  743. supports4K = result.resolution === '4K';
  744. } catch (e) {
  745. try {
  746. const result = await window.msdk.device.getDisplayInfoImmediate();
  747. supports4K = result.resolution === '4K';
  748. } catch (e) {
  749. shaka.log.alwaysWarn(
  750. 'PlayStation: Failed to get the display info:', e);
  751. }
  752. }
  753. if (supports4K) {
  754. maxResolution.width = 3840;
  755. maxResolution.height = 2160;
  756. } else {
  757. maxResolution.width = 1920;
  758. maxResolution.height = 1080;
  759. }
  760. }
  761. return maxResolution;
  762. }
  763. };
  764. /** @private {shaka.util.Timer} */
  765. shaka.util.Platform.cacheExpirationTimer_ = null;
  766. /** @private {HTMLMediaElement} */
  767. shaka.util.Platform.cachedMediaElement_ = null;