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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. const logger = require('jitsi-meet-logger').getLogger(__filename);
  2. let currentBrowser;
  3. const RTCBrowserType = {
  4. RTC_BROWSER_CHROME: 'rtc_browser.chrome',
  5. RTC_BROWSER_OPERA: 'rtc_browser.opera',
  6. RTC_BROWSER_FIREFOX: 'rtc_browser.firefox',
  7. RTC_BROWSER_IEXPLORER: 'rtc_browser.iexplorer',
  8. RTC_BROWSER_SAFARI: 'rtc_browser.safari',
  9. RTC_BROWSER_NWJS: 'rtc_browser.nwjs',
  10. RTC_BROWSER_ELECTRON: 'rtc_browser.electron',
  11. RTC_BROWSER_REACT_NATIVE: 'rtc_browser.react-native',
  12. /**
  13. * Gets current browser type.
  14. * @returns {string}
  15. */
  16. getBrowserType() {
  17. return currentBrowser;
  18. },
  19. /**
  20. * Gets current browser name, split from the type.
  21. * @returns {string}
  22. */
  23. getBrowserName() {
  24. let browser;
  25. if (RTCBrowserType.isAndroid()) {
  26. browser = 'android';
  27. } else {
  28. browser = currentBrowser.split('rtc_browser.')[1];
  29. }
  30. return browser;
  31. },
  32. /**
  33. * Checks if current browser is Chrome.
  34. * @returns {boolean}
  35. */
  36. isChrome() {
  37. return currentBrowser === RTCBrowserType.RTC_BROWSER_CHROME;
  38. },
  39. /**
  40. * Checks if current browser is Opera.
  41. * @returns {boolean}
  42. */
  43. isOpera() {
  44. return currentBrowser === RTCBrowserType.RTC_BROWSER_OPERA;
  45. },
  46. /**
  47. * Checks if current browser is Firefox.
  48. * @returns {boolean}
  49. */
  50. isFirefox() {
  51. return currentBrowser === RTCBrowserType.RTC_BROWSER_FIREFOX;
  52. },
  53. /**
  54. * Checks if current browser is Internet Explorer.
  55. * @returns {boolean}
  56. */
  57. isIExplorer() {
  58. return currentBrowser === RTCBrowserType.RTC_BROWSER_IEXPLORER;
  59. },
  60. /**
  61. * Checks if current browser is Safari.
  62. * @returns {boolean}
  63. */
  64. isSafari() {
  65. return currentBrowser === RTCBrowserType.RTC_BROWSER_SAFARI;
  66. },
  67. /**
  68. * Checks if current environment is NWJS.
  69. * @returns {boolean}
  70. */
  71. isNWJS() {
  72. return currentBrowser === RTCBrowserType.RTC_BROWSER_NWJS;
  73. },
  74. /**
  75. * Checks if current environment is Electron.
  76. * @returns {boolean}
  77. */
  78. isElectron() {
  79. return currentBrowser === RTCBrowserType.RTC_BROWSER_ELECTRON;
  80. },
  81. /**
  82. * Checks if current environment is React Native.
  83. * @returns {boolean}
  84. */
  85. isReactNative() {
  86. return currentBrowser === RTCBrowserType.RTC_BROWSER_REACT_NATIVE;
  87. },
  88. /**
  89. * Checks if Temasys RTC plugin is used.
  90. * @returns {boolean}
  91. */
  92. isTemasysPluginUsed() {
  93. // Temasys do not support Microsoft Edge:
  94. // http://support.temasys.com.sg/support/solutions/articles/5000654345-can-the-temasys-webrtc-plugin-be-used-with-microsoft-edge-
  95. if (RTCBrowserType.isIExplorer()
  96. && RTCBrowserType.getIExplorerVersion() < 12) {
  97. return true;
  98. }
  99. return RTCBrowserType.isSafari();
  100. },
  101. /**
  102. * Checks if the current browser triggers 'onmute'/'onunmute' events when
  103. * user's connection is interrupted and the video stops playback.
  104. * @returns {*|boolean} 'true' if the event is supported or 'false'
  105. * otherwise.
  106. */
  107. isVideoMuteOnConnInterruptedSupported() {
  108. return RTCBrowserType.isChrome();
  109. },
  110. /**
  111. * Returns Firefox version.
  112. * @returns {number|null}
  113. */
  114. getFirefoxVersion() {
  115. return RTCBrowserType.isFirefox() ? browserVersion : null;
  116. },
  117. /**
  118. * Returns Chrome version.
  119. * @returns {number|null}
  120. */
  121. getChromeVersion() {
  122. return RTCBrowserType.isChrome() ? browserVersion : null;
  123. },
  124. /**
  125. * Returns Internet Explorer version.
  126. *
  127. * @returns {number|null}
  128. */
  129. getIExplorerVersion() {
  130. return RTCBrowserType.isIExplorer() ? browserVersion : null;
  131. },
  132. usesPlanB() {
  133. return (
  134. RTCBrowserType.isChrome()
  135. || RTCBrowserType.isOpera()
  136. || RTCBrowserType.isReactNative()
  137. || RTCBrowserType.isTemasysPluginUsed());
  138. },
  139. usesUnifiedPlan() {
  140. return RTCBrowserType.isFirefox();
  141. },
  142. /**
  143. * Whether the browser is running on an android device.
  144. * @returns {boolean}
  145. */
  146. isAndroid() {
  147. return isAndroid;
  148. },
  149. /**
  150. * Whether jitsi-meet supports simulcast on the current browser.
  151. * @returns {boolean}
  152. */
  153. supportsSimulcast() {
  154. // This mirrors what sdp-simulcast uses (which is used when deciding
  155. // whether to actually enable simulcast or not).
  156. // TODO: the logic should be in one single place.
  157. return window.chrome !== undefined;
  158. }
  159. // Add version getters for other browsers when needed
  160. };
  161. // detectOpera() must be called before detectChrome() !!!
  162. // otherwise Opera wil be detected as Chrome
  163. function detectChrome() {
  164. if (navigator.webkitGetUserMedia) {
  165. currentBrowser = RTCBrowserType.RTC_BROWSER_CHROME;
  166. const userAgent = navigator.userAgent.toLowerCase();
  167. // We can assume that user agent is chrome, because it's
  168. // enforced when 'ext' streaming method is set
  169. const ver = parseInt(userAgent.match(/chrome\/(\d+)\./)[1], 10);
  170. logger.log(`This appears to be Chrome, ver: ${ver}`);
  171. return ver;
  172. }
  173. return null;
  174. }
  175. function detectOpera() {
  176. const userAgent = navigator.userAgent;
  177. if (userAgent.match(/Opera|OPR/)) {
  178. currentBrowser = RTCBrowserType.RTC_BROWSER_OPERA;
  179. const version = userAgent.match(/(Opera|OPR) ?\/?(\d+)\.?/)[2];
  180. logger.info(`This appears to be Opera, ver: ${version}`);
  181. return version;
  182. }
  183. return null;
  184. }
  185. function detectFirefox() {
  186. if (navigator.mozGetUserMedia) {
  187. currentBrowser = RTCBrowserType.RTC_BROWSER_FIREFOX;
  188. const version = parseInt(
  189. navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
  190. logger.log(`This appears to be Firefox, ver: ${version}`);
  191. return version;
  192. }
  193. return null;
  194. }
  195. function detectSafari() {
  196. if (/^((?!chrome).)*safari/i.test(navigator.userAgent)) {
  197. currentBrowser = RTCBrowserType.RTC_BROWSER_SAFARI;
  198. logger.info('This appears to be Safari');
  199. // FIXME detect Safari version when needed
  200. return 1;
  201. }
  202. return null;
  203. }
  204. function detectIE() {
  205. let version;
  206. const ua = window.navigator.userAgent;
  207. const msie = ua.indexOf('MSIE ');
  208. if (msie > 0) {
  209. // IE 10 or older => return version number
  210. version = parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
  211. }
  212. const trident = ua.indexOf('Trident/');
  213. if (!version && trident > 0) {
  214. // IE 11 => return version number
  215. const rv = ua.indexOf('rv:');
  216. version = parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
  217. }
  218. const edge = ua.indexOf('Edge/');
  219. if (!version && edge > 0) {
  220. // IE 12 => return version number
  221. version = parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
  222. }
  223. if (version) {
  224. currentBrowser = RTCBrowserType.RTC_BROWSER_IEXPLORER;
  225. logger.info(`This appears to be IExplorer, ver: ${version}`);
  226. }
  227. return version;
  228. }
  229. /**
  230. * Detects Electron environment.
  231. */
  232. function detectElectron() {
  233. const userAgent = navigator.userAgent;
  234. if (userAgent.match(/Electron/)) {
  235. currentBrowser = RTCBrowserType.RTC_BROWSER_ELECTRON;
  236. const version = userAgent.match(/Electron\/([\d.]+)/)[1];
  237. logger.info(`This appears to be Electron, ver: ${version}`);
  238. return version;
  239. }
  240. return null;
  241. }
  242. function detectNWJS() {
  243. const userAgent = navigator.userAgent;
  244. if (userAgent.match(/JitsiMeetNW/)) {
  245. currentBrowser = RTCBrowserType.RTC_BROWSER_NWJS;
  246. const version = userAgent.match(/JitsiMeetNW\/([\d.]+)/)[1];
  247. logger.info(`This appears to be JitsiMeetNW, ver: ${version}`);
  248. return version;
  249. }
  250. return null;
  251. }
  252. function detectReactNative() {
  253. const match
  254. = navigator.userAgent.match(/\b(react[ \t_-]*native)(?:\/(\S+))?/i);
  255. let version;
  256. // If we're remote debugging a React Native app, it may be treated as
  257. // Chrome. Check navigator.product as well and always return some version
  258. // even if we can't get the real one.
  259. if (match || navigator.product === 'ReactNative') {
  260. currentBrowser = RTCBrowserType.RTC_BROWSER_REACT_NATIVE;
  261. let name;
  262. if (match && match.length > 2) {
  263. name = match[1];
  264. version = match[2];
  265. }
  266. name || (name = 'react-native');
  267. version || (version = 'unknown');
  268. console.info(`This appears to be ${name}, ver: ${version}`);
  269. } else {
  270. // We're not running in a React Native environment.
  271. version = null;
  272. }
  273. return version;
  274. }
  275. function detectBrowser() {
  276. let version;
  277. const detectors = [
  278. detectReactNative,
  279. detectElectron,
  280. detectNWJS,
  281. detectOpera,
  282. detectChrome,
  283. detectFirefox,
  284. detectIE,
  285. detectSafari
  286. ];
  287. // Try all browser detectors
  288. for (let i = 0; i < detectors.length; i++) {
  289. version = detectors[i]();
  290. if (version) {
  291. return version;
  292. }
  293. }
  294. logger.warn('Browser type defaults to Safari ver 1');
  295. currentBrowser = RTCBrowserType.RTC_BROWSER_SAFARI;
  296. return 1;
  297. }
  298. const browserVersion = detectBrowser();
  299. const isAndroid = navigator.userAgent.indexOf('Android') != -1;
  300. module.exports = RTCBrowserType;