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. return true;
  584. }
  585. /**
  586. * Returns if codec switching SMOOTH is known reliable device support.
  587. *
  588. * Some devices are known not to support <code>SourceBuffer.changeType</code>
  589. * well. These devices should use the reload strategy. If a device
  590. * reports that it supports <code<changeType</code> but supports it unreliably
  591. * it should be disallowed in this method.
  592. *
  593. * @return {boolean}
  594. */
  595. static supportsSmoothCodecSwitching() {
  596. const Platform = shaka.util.Platform;
  597. // All Tizen versions (up to Tizen 8) do not support SMOOTH so far.
  598. // webOS seems to support SMOOTH from webOS 22.
  599. if (Platform.isTizen() || Platform.isPS4() || Platform.isPS5() ||
  600. Platform.isWebOS6()) {
  601. return false;
  602. }
  603. // Older chromecasts without GoogleTV seem to not support SMOOTH properly.
  604. if (Platform.isOlderChromecast()) {
  605. return false;
  606. }
  607. // See: https://chromium-review.googlesource.com/c/chromium/src/+/4577759
  608. if (Platform.isWindows() && Platform.isEdge()) {
  609. return false;
  610. }
  611. return true;
  612. }
  613. /**
  614. * On some platforms, such as v1 Chromecasts, the act of seeking can take a
  615. * significant amount of time.
  616. *
  617. * @return {boolean}
  618. */
  619. static isSeekingSlow() {
  620. const Platform = shaka.util.Platform;
  621. if (Platform.isChromecast()) {
  622. if (Platform.isAndroidCastDevice()) {
  623. // Android-based Chromecasts are new enough to not be a problem.
  624. return false;
  625. } else {
  626. return true;
  627. }
  628. }
  629. return false;
  630. }
  631. /**
  632. * Returns true if MediaKeys is polyfilled by the specified polyfill.
  633. *
  634. * @param {string} polyfillType
  635. * @return {boolean}
  636. */
  637. static isMediaKeysPolyfilled(polyfillType) {
  638. return polyfillType === window.shakaMediaKeysPolyfill;
  639. }
  640. /**
  641. * Detect the maximum resolution that the platform's hardware can handle.
  642. *
  643. * @return {!Promise<shaka.extern.Resolution>}
  644. */
  645. static async detectMaxHardwareResolution() {
  646. const Platform = shaka.util.Platform;
  647. /** @type {shaka.extern.Resolution} */
  648. const maxResolution = {
  649. width: Infinity,
  650. height: Infinity,
  651. };
  652. if (Platform.isChromecast()) {
  653. // In our tests, the original Chromecast seems to have trouble decoding
  654. // above 1080p. It would be a waste to select a higher res anyway, given
  655. // that the device only outputs 1080p to begin with.
  656. // Chromecast has an extension to query the device/display's resolution.
  657. const hasCanDisplayType = window.cast && cast.__platform__ &&
  658. cast.__platform__.canDisplayType;
  659. // Some hub devices can only do 720p. Default to that.
  660. maxResolution.width = 1280;
  661. maxResolution.height = 720;
  662. try {
  663. if (hasCanDisplayType && await cast.__platform__.canDisplayType(
  664. 'video/mp4; codecs="avc1.640028"; width=3840; height=2160')) {
  665. // The device and display can both do 4k. Assume a 4k limit.
  666. maxResolution.width = 3840;
  667. maxResolution.height = 2160;
  668. } else if (hasCanDisplayType && await cast.__platform__.canDisplayType(
  669. 'video/mp4; codecs="avc1.640028"; width=1920; height=1080')) {
  670. // Most Chromecasts can do 1080p.
  671. maxResolution.width = 1920;
  672. maxResolution.height = 1080;
  673. }
  674. } catch (error) {
  675. // This shouldn't generally happen. Log the error.
  676. shaka.log.alwaysError('Failed to check canDisplayType:', error);
  677. // Now ignore the error and let the 720p default stand.
  678. }
  679. } else if (Platform.isTizen()) {
  680. maxResolution.width = 1920;
  681. maxResolution.height = 1080;
  682. try {
  683. if (webapis.systeminfo && webapis.systeminfo.getMaxVideoResolution) {
  684. const maxVideoResolution =
  685. webapis.systeminfo.getMaxVideoResolution();
  686. maxResolution.width = maxVideoResolution.width;
  687. maxResolution.height = maxVideoResolution.height;
  688. } else {
  689. if (webapis.productinfo.is8KPanelSupported &&
  690. webapis.productinfo.is8KPanelSupported()) {
  691. maxResolution.width = 7680;
  692. maxResolution.height = 4320;
  693. } else if (webapis.productinfo.isUdPanelSupported &&
  694. webapis.productinfo.isUdPanelSupported()) {
  695. maxResolution.width = 3840;
  696. maxResolution.height = 2160;
  697. }
  698. }
  699. } catch (e) {
  700. shaka.log.alwaysWarn('Tizen: Error detecting screen size, default ' +
  701. 'screen size 1920x1080.');
  702. }
  703. } else if (Platform.isWebOS()) {
  704. try {
  705. const deviceInfo =
  706. /** @type {{screenWidth: number, screenHeight: number}} */(
  707. JSON.parse(window.PalmSystem.deviceInfo));
  708. // WebOS has always been able to do 1080p. Assume a 1080p limit.
  709. maxResolution.width = Math.max(1920, deviceInfo['screenWidth']);
  710. maxResolution.height = Math.max(1080, deviceInfo['screenHeight']);
  711. } catch (e) {
  712. shaka.log.alwaysWarn('WebOS: Error detecting screen size, default ' +
  713. 'screen size 1920x1080.');
  714. maxResolution.width = 1920;
  715. maxResolution.height = 1080;
  716. }
  717. } else if (Platform.isHisense()) {
  718. let supports4k = null;
  719. if (window.Hisense_Get4KSupportState) {
  720. try {
  721. // eslint-disable-next-line new-cap
  722. supports4k = window.Hisense_Get4KSupportState();
  723. } catch (e) {
  724. shaka.log.debug('Hisense: Failed to get 4K support state', e);
  725. }
  726. }
  727. if (supports4k == null) {
  728. // If API is not there or not working for whatever reason, fallback to
  729. // user agent check, as it contains UHD or FHD info.
  730. supports4k = Platform.userAgentContains_('UHD');
  731. }
  732. if (supports4k) {
  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. } else {
  761. // For Xbox and UWP apps.
  762. let winRT = undefined;
  763. try {
  764. // Try to access to WinRT for WebView, if it's not defined,
  765. // try to access to WinRT for WebView2, if it's not defined either,
  766. // let it throw.
  767. if (typeof Windows !== 'undefined') {
  768. winRT = Windows;
  769. } else {
  770. winRT = chrome.webview.hostObjects.sync.Windows;
  771. }
  772. } catch (e) {}
  773. if (winRT) {
  774. maxResolution.width = 1920;
  775. maxResolution.height = 1080;
  776. try {
  777. const protectionCapabilities =
  778. new winRT.Media.Protection.ProtectionCapabilities();
  779. const protectionResult =
  780. winRT.Media.Protection.ProtectionCapabilityResult;
  781. // isTypeSupported may return "maybe", which means the operation
  782. // is not completed. This means we need to retry
  783. // https://learn.microsoft.com/en-us/uwp/api/windows.media.protection.protectioncapabilityresult?view=winrt-22621
  784. let result = null;
  785. const type =
  786. 'video/mp4;codecs="hvc1,mp4a";features="decode-res-x=3840,' +
  787. 'decode-res-y=2160,decode-bitrate=20000,decode-fps=30,' +
  788. 'decode-bpc=10,display-res-x=3840,display-res-y=2160,' +
  789. 'display-bpc=8"';
  790. const keySystem = 'com.microsoft.playready.recommendation';
  791. do {
  792. result = protectionCapabilities.isTypeSupported(type, keySystem);
  793. } while (result === protectionResult.maybe);
  794. if (result === protectionResult.probably) {
  795. maxResolution.width = 3840;
  796. maxResolution.height = 2160;
  797. }
  798. } catch (e) {
  799. shaka.log.alwaysWarn('Xbox: Error detecting screen size, default ' +
  800. 'screen size 1920x1080.');
  801. }
  802. } else if (Platform.isXboxOne()) {
  803. maxResolution.width = 1920;
  804. maxResolution.height = 1080;
  805. shaka.log.alwaysWarn('Xbox: Error detecting screen size, default ' +
  806. 'screen size 1920x1080.');
  807. }
  808. }
  809. return maxResolution;
  810. }
  811. };
  812. /** @private {shaka.util.Timer} */
  813. shaka.util.Platform.cacheExpirationTimer_ = null;
  814. /** @private {HTMLMediaElement} */
  815. shaka.util.Platform.cachedMediaElement_ = null;