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

BrowserCapabilities.js 12KB

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