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.

RTCBrowserType.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. import { getLogger } from 'jitsi-meet-logger';
  2. let browserVersion; // eslint-disable-line prefer-const
  3. let currentBrowser;
  4. const logger = getLogger(__filename);
  5. const RTCBrowserType = {
  6. RTC_BROWSER_CHROME: 'rtc_browser.chrome',
  7. RTC_BROWSER_OPERA: 'rtc_browser.opera',
  8. RTC_BROWSER_FIREFOX: 'rtc_browser.firefox',
  9. RTC_BROWSER_IEXPLORER: 'rtc_browser.iexplorer',
  10. RTC_BROWSER_EDGE: 'rtc_browser.edge',
  11. RTC_BROWSER_SAFARI: 'rtc_browser.safari',
  12. RTC_BROWSER_NWJS: 'rtc_browser.nwjs',
  13. RTC_BROWSER_ELECTRON: 'rtc_browser.electron',
  14. RTC_BROWSER_REACT_NATIVE: 'rtc_browser.react-native',
  15. /**
  16. * Tells whether or not the <tt>MediaStream/tt> is removed from
  17. * the <tt>PeerConnection</tt> and disposed on video mute (in order to turn
  18. * off the camera device).
  19. * @return {boolean} <tt>true</tt> if the current browser supports this
  20. * strategy or <tt>false</tt> otherwise.
  21. */
  22. doesVideoMuteByStreamRemove() {
  23. return !(RTCBrowserType.isFirefox() || RTCBrowserType.isEdge());
  24. },
  25. /**
  26. * Gets current browser type.
  27. * @returns {string}
  28. */
  29. getBrowserType() {
  30. return currentBrowser;
  31. },
  32. /**
  33. * Gets current browser name, split from the type.
  34. * @returns {string}
  35. */
  36. getBrowserName() {
  37. const isAndroid = navigator.userAgent.indexOf('Android') !== -1;
  38. if (isAndroid) {
  39. return 'android';
  40. }
  41. return currentBrowser.split('rtc_browser.')[1];
  42. },
  43. /**
  44. * Checks if current browser is Chrome.
  45. * @returns {boolean}
  46. */
  47. isChrome() {
  48. return currentBrowser === RTCBrowserType.RTC_BROWSER_CHROME;
  49. },
  50. /**
  51. * Checks if current browser is Opera.
  52. * @returns {boolean}
  53. */
  54. isOpera() {
  55. return currentBrowser === RTCBrowserType.RTC_BROWSER_OPERA;
  56. },
  57. /**
  58. * Checks if current browser is Firefox.
  59. * @returns {boolean}
  60. */
  61. isFirefox() {
  62. return currentBrowser === RTCBrowserType.RTC_BROWSER_FIREFOX;
  63. },
  64. /**
  65. * Checks if current browser is Internet Explorer.
  66. * @returns {boolean}
  67. */
  68. isIExplorer() {
  69. return currentBrowser === RTCBrowserType.RTC_BROWSER_IEXPLORER;
  70. },
  71. /**
  72. * Checks if current browser is Microsoft Edge.
  73. * @returns {boolean}
  74. */
  75. isEdge() {
  76. return currentBrowser === RTCBrowserType.RTC_BROWSER_EDGE;
  77. },
  78. /**
  79. * Checks if current browser is Safari.
  80. * @returns {boolean}
  81. */
  82. isSafari() {
  83. return currentBrowser === RTCBrowserType.RTC_BROWSER_SAFARI;
  84. },
  85. /**
  86. * Checks if current environment is NWJS.
  87. * @returns {boolean}
  88. */
  89. isNWJS() {
  90. return currentBrowser === RTCBrowserType.RTC_BROWSER_NWJS;
  91. },
  92. /**
  93. * Checks if current environment is Electron.
  94. * @returns {boolean}
  95. */
  96. isElectron() {
  97. return currentBrowser === RTCBrowserType.RTC_BROWSER_ELECTRON;
  98. },
  99. /**
  100. * Check whether or not the current browser support peer to peer connections
  101. * @return {boolean} <tt>true</tt> if p2p is supported or <tt>false</tt>
  102. * otherwise.
  103. */
  104. isP2PSupported() {
  105. return !RTCBrowserType.isEdge();
  106. },
  107. /**
  108. * Checks if current environment is React Native.
  109. * @returns {boolean}
  110. */
  111. isReactNative() {
  112. return currentBrowser === RTCBrowserType.RTC_BROWSER_REACT_NATIVE;
  113. },
  114. /**
  115. * Checks if Temasys RTC plugin is used.
  116. * @returns {boolean}
  117. */
  118. isTemasysPluginUsed() {
  119. // Temasys do not support Microsoft Edge:
  120. // http://support.temasys.com.sg/support/solutions/articles/
  121. // 5000654345-can-the-temasys-webrtc-plugin-be-used-with-microsoft-edge-
  122. return (
  123. RTCBrowserType.isSafari()
  124. || (RTCBrowserType.isIExplorer()
  125. && RTCBrowserType.getIExplorerVersion() < 12)
  126. );
  127. },
  128. /**
  129. * Checks if the current browser triggers 'onmute'/'onunmute' events when
  130. * user's connection is interrupted and the video stops playback.
  131. * @returns {*|boolean} 'true' if the event is supported or 'false'
  132. * otherwise.
  133. */
  134. isVideoMuteOnConnInterruptedSupported() {
  135. return RTCBrowserType.isChrome();
  136. },
  137. /**
  138. * Returns Firefox version.
  139. * @returns {number|null}
  140. */
  141. getFirefoxVersion() {
  142. return RTCBrowserType.isFirefox() ? browserVersion : null;
  143. },
  144. /**
  145. * Returns Chrome version.
  146. * @returns {number|null}
  147. */
  148. getChromeVersion() {
  149. return RTCBrowserType.isChrome() ? browserVersion : null;
  150. },
  151. /**
  152. * Returns Internet Explorer version.
  153. *
  154. * @returns {number|null}
  155. */
  156. getIExplorerVersion() {
  157. return RTCBrowserType.isIExplorer() ? browserVersion : null;
  158. },
  159. /**
  160. * Returns Edge version.
  161. *
  162. * @returns {number|null}
  163. */
  164. getEdgeVersion() {
  165. return RTCBrowserType.isEdge() ? browserVersion : null;
  166. },
  167. usesPlanB() {
  168. return !RTCBrowserType.usesUnifiedPlan();
  169. },
  170. usesUnifiedPlan() {
  171. return RTCBrowserType.isFirefox();
  172. },
  173. /**
  174. * Checks if the current browser reports upload and download bandwidth
  175. * statistics.
  176. * @return {boolean}
  177. */
  178. supportsBandwidthStatistics() {
  179. // FIXME bandwidth stats are currently not implemented for FF on our
  180. // side, but not sure if not possible ?
  181. return !RTCBrowserType.isFirefox() && !RTCBrowserType.isEdge();
  182. },
  183. /**
  184. * Checks if the current browser supports WebRTC datachannels.
  185. * @return {boolean}
  186. */
  187. supportsDataChannels() {
  188. // NOTE: Edge does not yet implement DataChannel.
  189. return !RTCBrowserType.isEdge();
  190. },
  191. /**
  192. * Checks if the current browser reports round trip time statistics for
  193. * the ICE candidate pair.
  194. * @return {boolean}
  195. */
  196. supportsRTTStatistics() {
  197. // Firefox does not seem to report RTT for ICE candidate pair:
  198. // eslint-disable-next-line max-len
  199. // https://www.w3.org/TR/webrtc-stats/#dom-rtcicecandidatepairstats-currentroundtriptime
  200. // It does report mozRTT for RTP streams, but at the time of this
  201. // writing it's value does not make sense most of the time
  202. // (is reported as 1):
  203. // https://bugzilla.mozilla.org/show_bug.cgi?id=1241066
  204. // For Chrome and others we rely on 'googRtt'.
  205. return !RTCBrowserType.isFirefox() && !RTCBrowserType.isEdge();
  206. },
  207. /**
  208. * Whether jitsi-meet supports simulcast on the current browser.
  209. * @returns {boolean}
  210. */
  211. supportsSimulcast() {
  212. return RTCBrowserType.isChrome()
  213. || RTCBrowserType.isElectron()
  214. || RTCBrowserType.isNWJS();
  215. },
  216. supportsRtx() {
  217. return !RTCBrowserType.isFirefox();
  218. }
  219. // Add version getters for other browsers when needed
  220. };
  221. /**
  222. * detectOpera() must be called before detectChrome() !!!
  223. * otherwise Opera wil be detected as Chrome
  224. */
  225. function detectChrome() {
  226. if (navigator.webkitGetUserMedia) {
  227. currentBrowser = RTCBrowserType.RTC_BROWSER_CHROME;
  228. const userAgent = navigator.userAgent.toLowerCase();
  229. // We can assume that user agent is chrome, because it's
  230. // enforced when 'ext' streaming method is set
  231. const ver = parseInt(userAgent.match(/chrome\/(\d+)\./)[1], 10);
  232. logger.log(`This appears to be Chrome, ver: ${ver}`);
  233. return ver;
  234. }
  235. return null;
  236. }
  237. /**
  238. *
  239. */
  240. function detectOpera() {
  241. const userAgent = navigator.userAgent;
  242. if (userAgent.match(/Opera|OPR/)) {
  243. currentBrowser = RTCBrowserType.RTC_BROWSER_OPERA;
  244. const version = userAgent.match(/(Opera|OPR) ?\/?(\d+)\.?/)[2];
  245. logger.info(`This appears to be Opera, ver: ${version}`);
  246. return version;
  247. }
  248. return null;
  249. }
  250. /**
  251. *
  252. */
  253. function detectFirefox() {
  254. if (navigator.mozGetUserMedia) {
  255. currentBrowser = RTCBrowserType.RTC_BROWSER_FIREFOX;
  256. const version = parseInt(
  257. navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
  258. logger.log(`This appears to be Firefox, ver: ${version}`);
  259. return version;
  260. }
  261. return null;
  262. }
  263. /**
  264. *
  265. */
  266. function detectSafari() {
  267. if (/^((?!chrome).)*safari/i.test(navigator.userAgent)) {
  268. currentBrowser = RTCBrowserType.RTC_BROWSER_SAFARI;
  269. logger.info('This appears to be Safari');
  270. // FIXME detect Safari version when needed
  271. return 1;
  272. }
  273. return null;
  274. }
  275. /**
  276. * Detects IE.
  277. */
  278. function detectIE() {
  279. let version;
  280. const ua = window.navigator.userAgent;
  281. const msie = ua.indexOf('MSIE ');
  282. if (msie > 0) {
  283. // IE 10 or older => return version number
  284. version = parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
  285. }
  286. const trident = ua.indexOf('Trident/');
  287. if (!version && trident > 0) {
  288. // IE 11 => return version number
  289. const rv = ua.indexOf('rv:');
  290. version = parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
  291. }
  292. if (version) {
  293. currentBrowser = RTCBrowserType.RTC_BROWSER_IEXPLORER;
  294. logger.info(`This appears to be IExplorer, ver: ${version}`);
  295. }
  296. return version;
  297. }
  298. /**
  299. * Detects Edge.
  300. */
  301. function detectEdge() {
  302. let version;
  303. const ua = window.navigator.userAgent;
  304. const edge = ua.indexOf('Edge/');
  305. if (!version && edge > 0) {
  306. version = parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
  307. }
  308. if (version) {
  309. currentBrowser = RTCBrowserType.RTC_BROWSER_EDGE;
  310. logger.info(`This appears to be Edge, ver: ${version}`);
  311. }
  312. return version;
  313. }
  314. /**
  315. * Detects Electron environment.
  316. */
  317. function detectElectron() {
  318. const userAgent = navigator.userAgent;
  319. if (userAgent.match(/Electron/)) {
  320. currentBrowser = RTCBrowserType.RTC_BROWSER_ELECTRON;
  321. const version = userAgent.match(/Electron\/([\d.]+)/)[1];
  322. logger.info(`This appears to be Electron, ver: ${version}`);
  323. return version;
  324. }
  325. return null;
  326. }
  327. /**
  328. *
  329. */
  330. function detectNWJS() {
  331. const userAgent = navigator.userAgent;
  332. if (userAgent.match(/JitsiMeetNW/)) {
  333. currentBrowser = RTCBrowserType.RTC_BROWSER_NWJS;
  334. const version = userAgent.match(/JitsiMeetNW\/([\d.]+)/)[1];
  335. logger.info(`This appears to be JitsiMeetNW, ver: ${version}`);
  336. return version;
  337. }
  338. return null;
  339. }
  340. /**
  341. *
  342. */
  343. function detectReactNative() {
  344. const match
  345. = navigator.userAgent.match(/\b(react[ \t_-]*native)(?:\/(\S+))?/i);
  346. let version;
  347. // If we're remote debugging a React Native app, it may be treated as
  348. // Chrome. Check navigator.product as well and always return some version
  349. // even if we can't get the real one.
  350. if (match || navigator.product === 'ReactNative') {
  351. currentBrowser = RTCBrowserType.RTC_BROWSER_REACT_NATIVE;
  352. let name;
  353. if (match && match.length > 2) {
  354. name = match[1];
  355. version = match[2];
  356. }
  357. name || (name = 'react-native');
  358. version || (version = 'unknown');
  359. console.info(`This appears to be ${name}, ver: ${version}`);
  360. } else {
  361. // We're not running in a React Native environment.
  362. version = null;
  363. }
  364. return version;
  365. }
  366. /**
  367. *
  368. */
  369. function detectBrowser() {
  370. let version;
  371. const detectors = [
  372. detectReactNative,
  373. detectElectron,
  374. detectNWJS,
  375. detectOpera,
  376. detectChrome,
  377. detectFirefox,
  378. detectEdge,
  379. detectIE,
  380. detectSafari
  381. ];
  382. // Try all browser detectors
  383. for (let i = 0; i < detectors.length; i++) {
  384. version = detectors[i]();
  385. if (version) {
  386. return version;
  387. }
  388. }
  389. logger.warn('Browser type defaults to Safari ver 1');
  390. currentBrowser = RTCBrowserType.RTC_BROWSER_SAFARI;
  391. return 1;
  392. }
  393. browserVersion = detectBrowser();
  394. export default RTCBrowserType;