Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

BrowserCapabilities.js 11KB

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