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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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.isFirefox()
  214. || RTCBrowserType.isElectron()
  215. || RTCBrowserType.isNWJS();
  216. },
  217. supportsRtx() {
  218. return !RTCBrowserType.isFirefox();
  219. },
  220. supportsRtpSender() {
  221. return RTCBrowserType.isFirefox();
  222. }
  223. // Add version getters for other browsers when needed
  224. };
  225. /**
  226. * detectOpera() must be called before detectChrome() !!!
  227. * otherwise Opera wil be detected as Chrome
  228. */
  229. function detectChrome() {
  230. if (navigator.webkitGetUserMedia) {
  231. currentBrowser = RTCBrowserType.RTC_BROWSER_CHROME;
  232. // We can assume that user agent is chrome, because it's enforced when
  233. // 'ext' streaming method is set.
  234. const verMatch = navigator.userAgent.match(/chrome\/(\d+)\./i);
  235. const ver = verMatch ? parseInt(verMatch[1], 10) : 'undefined';
  236. logger.log(`This appears to be Chrome, ver: ${ver}`);
  237. return ver;
  238. }
  239. return null;
  240. }
  241. /**
  242. *
  243. */
  244. function detectOpera() {
  245. const userAgent = navigator.userAgent;
  246. if (userAgent.match(/Opera|OPR/)) {
  247. currentBrowser = RTCBrowserType.RTC_BROWSER_OPERA;
  248. const version = userAgent.match(/(Opera|OPR) ?\/?(\d+)\.?/)[2];
  249. logger.info(`This appears to be Opera, ver: ${version}`);
  250. return version;
  251. }
  252. return null;
  253. }
  254. /**
  255. *
  256. */
  257. function detectFirefox() {
  258. if (navigator.mozGetUserMedia) {
  259. currentBrowser = RTCBrowserType.RTC_BROWSER_FIREFOX;
  260. const version = parseInt(
  261. navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
  262. logger.log(`This appears to be Firefox, ver: ${version}`);
  263. return version;
  264. }
  265. return null;
  266. }
  267. /**
  268. *
  269. */
  270. function detectSafari() {
  271. if (/^((?!chrome).)*safari/i.test(navigator.userAgent)) {
  272. currentBrowser = RTCBrowserType.RTC_BROWSER_SAFARI;
  273. logger.info('This appears to be Safari');
  274. // FIXME detect Safari version when needed
  275. return 1;
  276. }
  277. return null;
  278. }
  279. /**
  280. * Detects IE.
  281. */
  282. function detectIE() {
  283. let version;
  284. const ua = window.navigator.userAgent;
  285. const msie = ua.indexOf('MSIE ');
  286. if (msie > 0) {
  287. // IE 10 or older => return version number
  288. version = parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
  289. }
  290. const trident = ua.indexOf('Trident/');
  291. if (!version && trident > 0) {
  292. // IE 11 => return version number
  293. const rv = ua.indexOf('rv:');
  294. version = parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
  295. }
  296. if (version) {
  297. currentBrowser = RTCBrowserType.RTC_BROWSER_IEXPLORER;
  298. logger.info(`This appears to be IExplorer, ver: ${version}`);
  299. }
  300. return version;
  301. }
  302. /**
  303. * Detects Edge.
  304. */
  305. function detectEdge() {
  306. let version;
  307. const ua = window.navigator.userAgent;
  308. const edge = ua.indexOf('Edge/');
  309. if (!version && edge > 0) {
  310. version = parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
  311. }
  312. if (version) {
  313. currentBrowser = RTCBrowserType.RTC_BROWSER_EDGE;
  314. logger.info(`This appears to be Edge, ver: ${version}`);
  315. }
  316. return version;
  317. }
  318. /**
  319. * Detects Electron environment.
  320. */
  321. function detectElectron() {
  322. const userAgent = navigator.userAgent;
  323. if (userAgent.match(/Electron/)) {
  324. currentBrowser = RTCBrowserType.RTC_BROWSER_ELECTRON;
  325. const version = userAgent.match(/Electron\/([\d.]+)/)[1];
  326. logger.info(`This appears to be Electron, ver: ${version}`);
  327. return version;
  328. }
  329. return null;
  330. }
  331. /**
  332. *
  333. */
  334. function detectNWJS() {
  335. const userAgent = navigator.userAgent;
  336. if (userAgent.match(/JitsiMeetNW/)) {
  337. currentBrowser = RTCBrowserType.RTC_BROWSER_NWJS;
  338. const version = userAgent.match(/JitsiMeetNW\/([\d.]+)/)[1];
  339. logger.info(`This appears to be JitsiMeetNW, ver: ${version}`);
  340. return version;
  341. }
  342. return null;
  343. }
  344. /**
  345. *
  346. */
  347. function detectReactNative() {
  348. const match
  349. = navigator.userAgent.match(/\b(react[ \t_-]*native)(?:\/(\S+))?/i);
  350. let version;
  351. // If we're remote debugging a React Native app, it may be treated as
  352. // Chrome. Check navigator.product as well and always return some version
  353. // even if we can't get the real one.
  354. if (match || navigator.product === 'ReactNative') {
  355. currentBrowser = RTCBrowserType.RTC_BROWSER_REACT_NATIVE;
  356. let name;
  357. if (match && match.length > 2) {
  358. name = match[1];
  359. version = match[2];
  360. }
  361. name || (name = 'react-native');
  362. version || (version = 'unknown');
  363. console.info(`This appears to be ${name}, ver: ${version}`);
  364. } else {
  365. // We're not running in a React Native environment.
  366. version = null;
  367. }
  368. return version;
  369. }
  370. /**
  371. *
  372. */
  373. function detectBrowser() {
  374. let version;
  375. const detectors = [
  376. detectReactNative,
  377. detectElectron,
  378. detectNWJS,
  379. detectOpera,
  380. detectChrome,
  381. detectFirefox,
  382. detectEdge,
  383. detectIE,
  384. detectSafari
  385. ];
  386. // Try all browser detectors
  387. for (let i = 0; i < detectors.length; i++) {
  388. version = detectors[i]();
  389. if (version) {
  390. return version;
  391. }
  392. }
  393. logger.warn('Browser type defaults to Safari ver 1');
  394. currentBrowser = RTCBrowserType.RTC_BROWSER_SAFARI;
  395. return 1;
  396. }
  397. browserVersion = detectBrowser();
  398. export default RTCBrowserType;