Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

BrowserCapabilities.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import { BrowserDetection } from '@jitsi/js-utils';
  2. import { getLogger } from '@jitsi/logger';
  3. const logger = getLogger(__filename);
  4. /* Minimum required Chrome / Chromium version. This applies also to derivatives. */
  5. const MIN_REQUIRED_CHROME_VERSION = 72;
  6. const MIN_REQUIRED_SAFARI_VERSION = 14;
  7. const MIN_REQUIRED_IOS_VERSION = 14;
  8. // TODO: Move this code to js-utils.
  9. // NOTE: Now we are extending BrowserDetection in order to preserve
  10. // RTCBrowserType interface but maybe it worth exporting BrowserCapabilities
  11. // and BrowserDetection as separate objects in future.
  12. /**
  13. * Implements browser capabilities for lib-jitsi-meet.
  14. */
  15. export default class BrowserCapabilities extends BrowserDetection {
  16. /**
  17. * Creates new BrowserCapabilities instance.
  18. */
  19. constructor() {
  20. super();
  21. logger.info(
  22. `This appears to be ${this.getName()}, ver: ${this.getVersion()}`);
  23. }
  24. /**
  25. * Tells whether or not the <tt>MediaStream/tt> is removed from the <tt>PeerConnection</tt> and disposed on video
  26. * mute (in order to turn off the camera device). This is needed on Firefox because of the following bug
  27. * https://bugzilla.mozilla.org/show_bug.cgi?id=1735951
  28. *
  29. * @return {boolean} <tt>true</tt> if the current browser supports this strategy or <tt>false</tt> otherwise.
  30. */
  31. doesVideoMuteByStreamRemove() {
  32. return this.isChromiumBased() || this.isWebKitBased() || this.isFirefox();
  33. }
  34. /**
  35. * Checks if the current browser is Chromium based, i.e., it's either Chrome / Chromium or uses it as its engine,
  36. * but doesn't identify as Chrome.
  37. *
  38. * This includes the following browsers:
  39. * - Chrome and Chromium.
  40. * - Other browsers which use the Chrome engine, but are detected as Chrome, such as Brave and Vivaldi.
  41. * - Browsers which are NOT Chrome but use it as their engine, and have custom detection code: Opera, Electron
  42. * and NW.JS.
  43. * This excludes
  44. * - Chrome on iOS since it uses WKWebView.
  45. */
  46. isChromiumBased() {
  47. return (this.isChrome()
  48. || this.isElectron()
  49. || this.isNWJS()
  50. || this.isOpera())
  51. && !this.isWebKitBased();
  52. }
  53. /**
  54. * Checks if the current platform is iOS.
  55. *
  56. * @returns {boolean}
  57. */
  58. isIosBrowser() {
  59. const { userAgent, maxTouchPoints, platform } = navigator;
  60. return Boolean(userAgent.match(/iP(ad|hone|od)/i))
  61. || (maxTouchPoints && maxTouchPoints > 2 && /MacIntel/.test(platform));
  62. }
  63. /**
  64. * Checks if the current browser is WebKit based. It's either
  65. * Safari or uses WebKit as its engine.
  66. *
  67. * This includes Chrome and Firefox on iOS
  68. *
  69. * @returns {boolean}
  70. */
  71. isWebKitBased() {
  72. // https://trac.webkit.org/changeset/236144/webkit/trunk/LayoutTests/webrtc/video-addLegacyTransceiver.html
  73. return this._bowser.isEngine('webkit')
  74. && typeof navigator.mediaDevices !== 'undefined'
  75. && typeof navigator.mediaDevices.getUserMedia !== 'undefined'
  76. && typeof window.RTCRtpTransceiver !== 'undefined'
  77. // eslint-disable-next-line no-undef
  78. && Object.keys(RTCRtpTransceiver.prototype).indexOf('currentDirection') > -1;
  79. }
  80. /**
  81. * Checks whether current running context is a Trusted Web Application.
  82. *
  83. * @returns {boolean} Whether the current context is a TWA.
  84. */
  85. isTwa() {
  86. return 'matchMedia' in window && window.matchMedia('(display-mode:standalone)').matches;
  87. }
  88. /**
  89. * Checks if the current browser is supported.
  90. *
  91. * @returns {boolean} true if the browser is supported, false otherwise.
  92. */
  93. isSupported() {
  94. if (this.isSafari() && this._getSafariVersion() < MIN_REQUIRED_SAFARI_VERSION) {
  95. return false;
  96. }
  97. return (this.isChromiumBased() && this._getChromiumBasedVersion() >= MIN_REQUIRED_CHROME_VERSION)
  98. || this.isFirefox()
  99. || this.isReactNative()
  100. || this.isWebKitBased();
  101. }
  102. /**
  103. * Returns whether the browser is supported for Android
  104. * @returns {boolean} true if the browser is supported for Android devices
  105. */
  106. isSupportedAndroidBrowser() {
  107. return this.isChromiumBased() || this.isFirefox();
  108. }
  109. /**
  110. * Returns whether the browser is supported for iOS
  111. * @returns {boolean} true if the browser is supported for iOS devices
  112. */
  113. isSupportedIOSBrowser() {
  114. return this._getIOSVersion() >= MIN_REQUIRED_IOS_VERSION;
  115. }
  116. /**
  117. * Returns whether or not the current environment needs a user interaction
  118. * with the page before any unmute can occur.
  119. *
  120. * @returns {boolean}
  121. */
  122. isUserInteractionRequiredForUnmute() {
  123. return this.isFirefox() && this.isVersionLessThan('68');
  124. }
  125. /**
  126. * Checks if the current browser triggers 'onmute'/'onunmute' events when
  127. * user's connection is interrupted and the video stops playback.
  128. * @returns {*|boolean} 'true' if the event is supported or 'false'
  129. * otherwise.
  130. */
  131. supportsVideoMuteOnConnInterrupted() {
  132. return this.isChromiumBased() || this.isReactNative();
  133. }
  134. /**
  135. * Checks if the current browser reports upload and download bandwidth
  136. * statistics.
  137. * @return {boolean}
  138. */
  139. supportsBandwidthStatistics() {
  140. // FIXME bandwidth stats are currently not implemented for FF on our
  141. // side, but not sure if not possible ?
  142. return !this.isFirefox() && !this.isWebKitBased();
  143. }
  144. /**
  145. * Checks if the current browser supports setting codec preferences on the transceiver.
  146. * @returns {boolean}
  147. */
  148. supportsCodecPreferences() {
  149. return Boolean(window.RTCRtpTransceiver
  150. && 'setCodecPreferences' in window.RTCRtpTransceiver.prototype
  151. && window.RTCRtpReceiver
  152. && typeof window.RTCRtpReceiver.getCapabilities !== 'undefined')
  153. // this is not working on Safari because of the following bug
  154. // https://bugs.webkit.org/show_bug.cgi?id=215567
  155. && !this.isWebKitBased();
  156. }
  157. /**
  158. * Checks if the current browser support the device change event.
  159. * @return {boolean}
  160. */
  161. supportsDeviceChangeEvent() {
  162. return navigator.mediaDevices
  163. && typeof navigator.mediaDevices.ondevicechange !== 'undefined'
  164. && typeof navigator.mediaDevices.addEventListener !== 'undefined';
  165. }
  166. /**
  167. * Checks if the current browser supports RTT statistics for srflx local
  168. * candidates through the legacy getStats() API.
  169. */
  170. supportsLocalCandidateRttStatistics() {
  171. return this.isChromiumBased() || this.isReactNative() || this.isWebKitBased();
  172. }
  173. /**
  174. * Checks if the current browser supports the Long Tasks API that lets us observe
  175. * performance measurement events and be notified of tasks that take longer than
  176. * 50ms to execute on the main thread.
  177. */
  178. supportsPerformanceObserver() {
  179. return typeof window.PerformanceObserver !== 'undefined'
  180. && PerformanceObserver.supportedEntryTypes.indexOf('longtask') > -1;
  181. }
  182. /**
  183. * Checks if the current browser supports audio level stats on the receivers.
  184. */
  185. supportsReceiverStats() {
  186. return typeof window.RTCRtpReceiver !== 'undefined'
  187. && Object.keys(RTCRtpReceiver.prototype).indexOf('getSynchronizationSources') > -1
  188. // Disable this on Safari because it is reporting 0.000001 as the audio levels for all
  189. // remote audio tracks.
  190. && !this.isWebKitBased();
  191. }
  192. /**
  193. * Checks if the current browser reports round trip time statistics for
  194. * the ICE candidate pair.
  195. * @return {boolean}
  196. */
  197. supportsRTTStatistics() {
  198. // Firefox does not seem to report RTT for ICE candidate pair:
  199. // eslint-disable-next-line max-len
  200. // https://www.w3.org/TR/webrtc-stats/#dom-rtcicecandidatepairstats-currentroundtriptime
  201. // It does report mozRTT for RTP streams, but at the time of this
  202. // writing it's value does not make sense most of the time
  203. // (is reported as 1):
  204. // https://bugzilla.mozilla.org/show_bug.cgi?id=1241066
  205. // For Chrome and others we rely on 'googRtt'.
  206. return !this.isFirefox();
  207. }
  208. /**
  209. * Returns true if VP9 is supported by the client on the browser. VP9 is currently disabled on Firefox and Safari
  210. * because of issues with rendering. Please check https://bugzilla.mozilla.org/show_bug.cgi?id=1492500,
  211. * https://bugs.webkit.org/show_bug.cgi?id=231071 and https://bugs.webkit.org/show_bug.cgi?id=231074 for details.
  212. */
  213. supportsVP9() {
  214. return this.isChromiumBased() || this.isReactNative();
  215. }
  216. /**
  217. * Checks if the browser uses SDP munging for turning on simulcast.
  218. *
  219. * @returns {boolean}
  220. */
  221. usesSdpMungingForSimulcast() {
  222. return this.isChromiumBased() || this.isReactNative() || this.isWebKitBased();
  223. }
  224. /**
  225. * Checks if the browser uses webrtc-adapter. All browsers except React Native do.
  226. *
  227. * @returns {boolean}
  228. */
  229. usesAdapter() {
  230. return !this.isReactNative();
  231. }
  232. /**
  233. * Checks if the browser uses RIDs/MIDs for siganling the simulcast streams
  234. * to the bridge instead of the ssrcs.
  235. */
  236. usesRidsForSimulcast() {
  237. return false;
  238. }
  239. /**
  240. * Checks if the browser supports getDisplayMedia.
  241. * @returns {boolean} {@code true} if the browser supports getDisplayMedia.
  242. */
  243. supportsGetDisplayMedia() {
  244. return typeof navigator.getDisplayMedia !== 'undefined'
  245. || (typeof navigator.mediaDevices !== 'undefined'
  246. && typeof navigator.mediaDevices.getDisplayMedia
  247. !== 'undefined');
  248. }
  249. /**
  250. * Checks if the browser supports WebRTC Encoded Transform, an alternative
  251. * to insertable streams.
  252. *
  253. * NOTE: At the time of this writing the only browser supporting this is
  254. * Safari / WebKit, behind a flag.
  255. *
  256. * @returns {boolean} {@code true} if the browser supports it.
  257. */
  258. supportsEncodedTransform() {
  259. return Boolean(window.RTCRtpScriptTransform);
  260. }
  261. /**
  262. * Checks if the browser supports insertable streams, needed for E2EE.
  263. * @returns {boolean} {@code true} if the browser supports insertable streams.
  264. */
  265. supportsInsertableStreams() {
  266. if (!(typeof window.RTCRtpSender !== 'undefined'
  267. && window.RTCRtpSender.prototype.createEncodedStreams)) {
  268. return false;
  269. }
  270. // Feature-detect transferable streams which we need to operate in a worker.
  271. // See https://groups.google.com/a/chromium.org/g/blink-dev/c/1LStSgBt6AM/m/hj0odB8pCAAJ
  272. const stream = new ReadableStream();
  273. try {
  274. window.postMessage(stream, '*', [ stream ]);
  275. return true;
  276. } catch {
  277. return false;
  278. }
  279. }
  280. /**
  281. * Whether the browser supports the RED format for audio.
  282. */
  283. supportsAudioRed() {
  284. return Boolean(window.RTCRtpSender
  285. && window.RTCRtpSender.getCapabilities
  286. && window.RTCRtpSender.getCapabilities('audio').codecs.some(codec => codec.mimeType === 'audio/red')
  287. && window.RTCRtpReceiver
  288. && window.RTCRtpReceiver.getCapabilities
  289. && window.RTCRtpReceiver.getCapabilities('audio').codecs.some(codec => codec.mimeType === 'audio/red'));
  290. }
  291. /**
  292. * Checks if the browser supports unified plan.
  293. *
  294. * @returns {boolean}
  295. */
  296. supportsUnifiedPlan() {
  297. return !this.isReactNative();
  298. }
  299. /**
  300. * Checks if the browser supports voice activity detection via the @type {VADAudioAnalyser} service.
  301. *
  302. * @returns {boolean}
  303. */
  304. supportsVADDetection() {
  305. return this.isChromiumBased();
  306. }
  307. /**
  308. * Check if the browser supports the RTP RTX feature (and it is usable).
  309. *
  310. * @returns {boolean}
  311. */
  312. supportsRTX() {
  313. // Disable RTX on Firefox up to 96 because we prefer simulcast over RTX
  314. // see https://bugzilla.mozilla.org/show_bug.cgi?id=1738504
  315. return !(this.isFirefox() && this.isVersionLessThan('96'));
  316. }
  317. /**
  318. * Returns the version of a Chromium based browser.
  319. *
  320. * @returns {Number}
  321. */
  322. _getChromiumBasedVersion() {
  323. if (this.isChromiumBased()) {
  324. // NW.JS doesn't expose the Chrome version in the UA string.
  325. if (this.isNWJS()) {
  326. // eslint-disable-next-line no-undef
  327. return Number.parseInt(process.versions.chromium, 10);
  328. }
  329. // Here we process all browsers which use the Chrome engine but
  330. // don't necessarily identify as Chrome. We cannot use the version
  331. // comparing functions because the Electron, Opera and NW.JS
  332. // versions are inconsequential here, as we need to know the actual
  333. // Chrome engine version.
  334. const ua = navigator.userAgent;
  335. if (ua.match(/Chrome/)) {
  336. const version
  337. = Number.parseInt(ua.match(/Chrome\/([\d.]+)/)[1], 10);
  338. return version;
  339. }
  340. }
  341. return -1;
  342. }
  343. /**
  344. * Returns the version of a Safari browser.
  345. *
  346. * @returns {Number}
  347. */
  348. _getSafariVersion() {
  349. if (this.isSafari()) {
  350. return Number.parseInt(this.getVersion(), 10);
  351. }
  352. return -1;
  353. }
  354. /**
  355. * Returns the version of an ios browser.
  356. *
  357. * @returns {Number}
  358. */
  359. _getIOSVersion() {
  360. if (this.isWebKitBased()) {
  361. return Number.parseInt(this.getVersion(), 10);
  362. }
  363. return -1;
  364. }
  365. }