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