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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import { BrowserDetection } from '@jitsi/js-utils';
  2. import { getLogger } from 'jitsi-meet-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. // 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. * Creates new BrowserCapabilities instance.
  16. */
  17. constructor() {
  18. super();
  19. logger.info(
  20. `This appears to be ${this.getName()}, ver: ${this.getVersion()}`);
  21. }
  22. /**
  23. * Tells whether or not the <tt>MediaStream/tt> is removed from
  24. * the <tt>PeerConnection</tt> and disposed on video mute (in order to turn
  25. * off the camera device).
  26. * @return {boolean} <tt>true</tt> if the current browser supports this
  27. * strategy or <tt>false</tt> otherwise.
  28. */
  29. doesVideoMuteByStreamRemove() {
  30. return this.isChromiumBased() || this.isSafari();
  31. }
  32. /**
  33. * Check whether or not the current browser support peer to peer connections
  34. * @return {boolean} <tt>true</tt> if p2p is supported or <tt>false</tt>
  35. * otherwise.
  36. */
  37. supportsP2P() {
  38. return !this.usesUnifiedPlan();
  39. }
  40. /**
  41. * Checks if the current browser is Chromium based, that is, it's either
  42. * Chrome / Chromium or uses it as its engine, but doesn't identify as
  43. * Chrome.
  44. *
  45. * This includes the following browsers:
  46. * - Chrome and Chromium
  47. * - Other browsers which use the Chrome engine, but are detected as Chrome,
  48. * such as Brave and Vivaldi
  49. * - Browsers which are NOT Chrome but use it as their engine, and have
  50. * custom detection code: Opera, Electron and NW.JS
  51. */
  52. isChromiumBased() {
  53. return this.isChrome()
  54. || this.isElectron()
  55. || this.isNWJS()
  56. || this.isOpera();
  57. }
  58. /**
  59. * Checks whether current running context is a Trusted Web Application.
  60. *
  61. * @returns {boolean} Whether the current context is a TWA.
  62. */
  63. isTwa() {
  64. return 'matchMedia' in window && window.matchMedia('(display-mode:standalone)').matches;
  65. }
  66. /**
  67. * Checks if the current browser is supported.
  68. *
  69. * @returns {boolean} true if the browser is supported, false otherwise.
  70. */
  71. isSupported() {
  72. return (this.isChromiumBased() && this._getChromiumBasedVersion() >= MIN_REQUIRED_CHROME_VERSION)
  73. || this.isFirefox()
  74. || this.isReactNative()
  75. || (this.isSafari() && !this.isVersionLessThan('12.1'));
  76. }
  77. /**
  78. * Returns whether or not the current environment needs a user interaction
  79. * with the page before any unmute can occur.
  80. *
  81. * @returns {boolean}
  82. */
  83. isUserInteractionRequiredForUnmute() {
  84. return this.isFirefox() && this.isVersionLessThan('68');
  85. }
  86. /**
  87. * Checks if the current browser triggers 'onmute'/'onunmute' events when
  88. * user's connection is interrupted and the video stops playback.
  89. * @returns {*|boolean} 'true' if the event is supported or 'false'
  90. * otherwise.
  91. */
  92. supportsVideoMuteOnConnInterrupted() {
  93. return this.isChromiumBased() || this.isReactNative() || this.isSafari();
  94. }
  95. /**
  96. * Checks if the current browser reports upload and download bandwidth
  97. * statistics.
  98. * @return {boolean}
  99. */
  100. supportsBandwidthStatistics() {
  101. // FIXME bandwidth stats are currently not implemented for FF on our
  102. // side, but not sure if not possible ?
  103. return !this.isFirefox() && !this.isSafari();
  104. }
  105. /**
  106. * Checks if the current browser supports setting codec preferences on the transceiver.
  107. * @returns {boolean}
  108. */
  109. supportsCodecPreferences() {
  110. return this.usesUnifiedPlan()
  111. && typeof window.RTCRtpTransceiver !== 'undefined'
  112. && Object.keys(window.RTCRtpTransceiver.prototype).indexOf('setCodecPreferences') > -1
  113. && Object.keys(RTCRtpSender.prototype).indexOf('getCapabilities') > -1
  114. // this is not working on Safari because of the following bug
  115. // https://bugs.webkit.org/show_bug.cgi?id=215567
  116. && !this.isSafari();
  117. }
  118. /**
  119. * Checks if the current browser support the device change event.
  120. * @return {boolean}
  121. */
  122. supportsDeviceChangeEvent() {
  123. return navigator.mediaDevices
  124. && typeof navigator.mediaDevices.ondevicechange !== 'undefined'
  125. && typeof navigator.mediaDevices.addEventListener !== 'undefined';
  126. }
  127. /**
  128. * Checks if the current browser supports RTT statistics for srflx local
  129. * candidates through the legacy getStats() API.
  130. */
  131. supportsLocalCandidateRttStatistics() {
  132. return this.isChromiumBased() || this.isReactNative() || this.isSafari();
  133. }
  134. /**
  135. * Checks if the current browser supports the Long Tasks API that lets us observe
  136. * performance measurement events and be notified of tasks that take longer than
  137. * 50ms to execute on the main thread.
  138. */
  139. supportsPerformanceObserver() {
  140. return typeof window.PerformanceObserver !== 'undefined'
  141. && PerformanceObserver.supportedEntryTypes.indexOf('longtask') > -1;
  142. }
  143. /**
  144. * Checks if the current browser supports audio level stats on the receivers.
  145. */
  146. supportsReceiverStats() {
  147. return typeof window.RTCRtpReceiver !== 'undefined'
  148. && Object.keys(RTCRtpReceiver.prototype).indexOf('getSynchronizationSources') > -1;
  149. }
  150. /**
  151. * Checks if the current browser reports round trip time statistics for
  152. * the ICE candidate pair.
  153. * @return {boolean}
  154. */
  155. supportsRTTStatistics() {
  156. // Firefox does not seem to report RTT for ICE candidate pair:
  157. // eslint-disable-next-line max-len
  158. // https://www.w3.org/TR/webrtc-stats/#dom-rtcicecandidatepairstats-currentroundtriptime
  159. // It does report mozRTT for RTP streams, but at the time of this
  160. // writing it's value does not make sense most of the time
  161. // (is reported as 1):
  162. // https://bugzilla.mozilla.org/show_bug.cgi?id=1241066
  163. // For Chrome and others we rely on 'googRtt'.
  164. return !this.isFirefox();
  165. }
  166. /**
  167. * Checks if the browser uses plan B.
  168. *
  169. * @returns {boolean}
  170. */
  171. usesPlanB() {
  172. return !this.usesUnifiedPlan();
  173. }
  174. /**
  175. * Checks if the browser uses SDP munging for turning on simulcast.
  176. *
  177. * @returns {boolean}
  178. */
  179. usesSdpMungingForSimulcast() {
  180. return this.isChromiumBased() || this.isReactNative() || this.isSafari();
  181. }
  182. /**
  183. * Checks if the browser uses unified plan.
  184. *
  185. * @returns {boolean}
  186. */
  187. usesUnifiedPlan() {
  188. if (this.isFirefox()) {
  189. return true;
  190. }
  191. if (this.isSafari() && typeof window.RTCRtpTransceiver !== 'undefined') {
  192. // https://trac.webkit.org/changeset/236144/webkit/trunk/LayoutTests/webrtc/video-addLegacyTransceiver.html
  193. // eslint-disable-next-line no-undef
  194. return Object.keys(RTCRtpTransceiver.prototype).indexOf('currentDirection') > -1;
  195. }
  196. return false;
  197. }
  198. /**
  199. * Returns whether or not the current browser should be using the new
  200. * getUserMedia flow, which utilizes the adapter shim. This method should
  201. * be temporary and used while migrating all browsers to use adapter and
  202. * the new getUserMedia.
  203. *
  204. * @returns {boolean}
  205. */
  206. usesNewGumFlow() {
  207. if (this.isChromiumBased() || this.isFirefox() || this.isSafari()) {
  208. return true;
  209. }
  210. return false;
  211. }
  212. /**
  213. * Checks if the browser uses webrtc-adapter. All browsers using the new
  214. * getUserMedia flow.
  215. *
  216. * @returns {boolean}
  217. */
  218. usesAdapter() {
  219. return this.usesNewGumFlow();
  220. }
  221. /**
  222. * Checks if the browser uses RIDs/MIDs for siganling the simulcast streams
  223. * to the bridge instead of the ssrcs.
  224. */
  225. usesRidsForSimulcast() {
  226. return false;
  227. }
  228. /**
  229. * Checks if the browser supports getDisplayMedia.
  230. * @returns {boolean} {@code true} if the browser supports getDisplayMedia.
  231. */
  232. supportsGetDisplayMedia() {
  233. return typeof navigator.getDisplayMedia !== 'undefined'
  234. || (typeof navigator.mediaDevices !== 'undefined'
  235. && typeof navigator.mediaDevices.getDisplayMedia
  236. !== 'undefined');
  237. }
  238. /**
  239. * Checks if the browser supports insertable streams, needed for E2EE.
  240. * @returns {boolean} {@code true} if the browser supports insertable streams.
  241. */
  242. supportsInsertableStreams() {
  243. if (!(typeof window.RTCRtpSender !== 'undefined'
  244. && (window.RTCRtpSender.prototype.createEncodedStreams
  245. || window.RTCRtpSender.prototype.createEncodedVideoStreams))) {
  246. return false;
  247. }
  248. // Feature-detect transferable streams which we need to operate in a worker.
  249. // See https://groups.google.com/a/chromium.org/g/blink-dev/c/1LStSgBt6AM/m/hj0odB8pCAAJ
  250. const stream = new ReadableStream();
  251. try {
  252. window.postMessage(stream, '*', [ stream ]);
  253. return true;
  254. } catch {
  255. return false;
  256. }
  257. }
  258. /**
  259. * Whether the browser supports the RED format for audio.
  260. */
  261. supportsAudioRed() {
  262. return Boolean(window.RTCRtpSender
  263. && window.RTCRtpSender.getCapabilities
  264. && window.RTCRtpSender.getCapabilities('audio').codecs.some(codec => codec.mimeType === 'audio/red')
  265. && window.RTCRtpReceiver
  266. && window.RTCRtpReceiver.getCapabilities
  267. && window.RTCRtpReceiver.getCapabilities('audio').codecs.some(codec => codec.mimeType === 'audio/red'));
  268. }
  269. /**
  270. * Checks if the browser supports the "sdpSemantics" configuration option.
  271. * https://webrtc.org/web-apis/chrome/unified-plan/
  272. *
  273. * @returns {boolean}
  274. */
  275. supportsSdpSemantics() {
  276. return this.isChromiumBased();
  277. }
  278. /**
  279. * Returns the version of a Chromium based browser.
  280. *
  281. * @returns {Number}
  282. */
  283. _getChromiumBasedVersion() {
  284. if (this.isChromiumBased()) {
  285. // NW.JS doesn't expose the Chrome version in the UA string.
  286. if (this.isNWJS()) {
  287. // eslint-disable-next-line no-undef
  288. return Number.parseInt(process.versions.chromium, 10);
  289. }
  290. // Here we process all browsers which use the Chrome engine but
  291. // don't necessarily identify as Chrome. We cannot use the version
  292. // comparing functions because the Electron, Opera and NW.JS
  293. // versions are inconsequential here, as we need to know the actual
  294. // Chrome engine version.
  295. const ua = navigator.userAgent;
  296. if (ua.match(/Chrome/)) {
  297. const version
  298. = Number.parseInt(ua.match(/Chrome\/([\d.]+)/)[1], 10);
  299. return version;
  300. }
  301. }
  302. return -1;
  303. }
  304. }