You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BrowserCapabilities.js 14KB

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