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