modified lib-jitsi-meet dev repo
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 13KB

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