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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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/
  95. // 5000654345-can-the-temasys-webrtc-plugin-be-used-with-microsoft-edge-
  96. if (RTCBrowserType.isIExplorer()
  97. && RTCBrowserType.getIExplorerVersion() < 12) {
  98. return true;
  99. }
  100. return RTCBrowserType.isSafari();
  101. },
  102. /**
  103. * Checks if the current browser triggers 'onmute'/'onunmute' events when
  104. * user's connection is interrupted and the video stops playback.
  105. * @returns {*|boolean} 'true' if the event is supported or 'false'
  106. * otherwise.
  107. */
  108. isVideoMuteOnConnInterruptedSupported() {
  109. return RTCBrowserType.isChrome();
  110. },
  111. /**
  112. * Returns Firefox version.
  113. * @returns {number|null}
  114. */
  115. getFirefoxVersion() {
  116. return RTCBrowserType.isFirefox() ? browserVersion : null;
  117. },
  118. /**
  119. * Returns Chrome version.
  120. * @returns {number|null}
  121. */
  122. getChromeVersion() {
  123. return RTCBrowserType.isChrome() ? browserVersion : null;
  124. },
  125. /**
  126. * Returns Internet Explorer version.
  127. *
  128. * @returns {number|null}
  129. */
  130. getIExplorerVersion() {
  131. return RTCBrowserType.isIExplorer() ? browserVersion : null;
  132. },
  133. usesPlanB() {
  134. return (
  135. RTCBrowserType.isChrome()
  136. || RTCBrowserType.isOpera()
  137. || RTCBrowserType.isReactNative()
  138. || RTCBrowserType.isTemasysPluginUsed());
  139. },
  140. usesUnifiedPlan() {
  141. return RTCBrowserType.isFirefox();
  142. },
  143. /**
  144. * Whether the browser is running on an android device.
  145. * @returns {boolean}
  146. */
  147. isAndroid() {
  148. return isAndroid;
  149. },
  150. /**
  151. * Whether jitsi-meet supports simulcast on the current browser.
  152. * @returns {boolean}
  153. */
  154. supportsSimulcast() {
  155. // This mirrors what sdp-simulcast uses (which is used when deciding
  156. // whether to actually enable simulcast or not).
  157. // TODO: the logic should be in one single place.
  158. return window.chrome !== undefined;
  159. }
  160. // Add version getters for other browsers when needed
  161. };
  162. // detectOpera() must be called before detectChrome() !!!
  163. // otherwise Opera wil be detected as Chrome
  164. function detectChrome() {
  165. if (navigator.webkitGetUserMedia) {
  166. currentBrowser = RTCBrowserType.RTC_BROWSER_CHROME;
  167. const userAgent = navigator.userAgent.toLowerCase();
  168. // We can assume that user agent is chrome, because it's
  169. // enforced when 'ext' streaming method is set
  170. const ver = parseInt(userAgent.match(/chrome\/(\d+)\./)[1], 10);
  171. logger.log(`This appears to be Chrome, ver: ${ver}`);
  172. return ver;
  173. }
  174. return null;
  175. }
  176. function detectOpera() {
  177. const userAgent = navigator.userAgent;
  178. if (userAgent.match(/Opera|OPR/)) {
  179. currentBrowser = RTCBrowserType.RTC_BROWSER_OPERA;
  180. const version = userAgent.match(/(Opera|OPR) ?\/?(\d+)\.?/)[2];
  181. logger.info(`This appears to be Opera, ver: ${version}`);
  182. return version;
  183. }
  184. return null;
  185. }
  186. function detectFirefox() {
  187. if (navigator.mozGetUserMedia) {
  188. currentBrowser = RTCBrowserType.RTC_BROWSER_FIREFOX;
  189. const version = parseInt(
  190. navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
  191. logger.log(`This appears to be Firefox, ver: ${version}`);
  192. return version;
  193. }
  194. return null;
  195. }
  196. function detectSafari() {
  197. if (/^((?!chrome).)*safari/i.test(navigator.userAgent)) {
  198. currentBrowser = RTCBrowserType.RTC_BROWSER_SAFARI;
  199. logger.info('This appears to be Safari');
  200. // FIXME detect Safari version when needed
  201. return 1;
  202. }
  203. return null;
  204. }
  205. function detectIE() {
  206. let version;
  207. const ua = window.navigator.userAgent;
  208. const msie = ua.indexOf('MSIE ');
  209. if (msie > 0) {
  210. // IE 10 or older => return version number
  211. version = parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
  212. }
  213. const trident = ua.indexOf('Trident/');
  214. if (!version && trident > 0) {
  215. // IE 11 => return version number
  216. const rv = ua.indexOf('rv:');
  217. version = parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
  218. }
  219. const edge = ua.indexOf('Edge/');
  220. if (!version && edge > 0) {
  221. // IE 12 => return version number
  222. version = parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
  223. }
  224. if (version) {
  225. currentBrowser = RTCBrowserType.RTC_BROWSER_IEXPLORER;
  226. logger.info(`This appears to be IExplorer, ver: ${version}`);
  227. }
  228. return version;
  229. }
  230. /**
  231. * Detects Electron environment.
  232. */
  233. function detectElectron() {
  234. const userAgent = navigator.userAgent;
  235. if (userAgent.match(/Electron/)) {
  236. currentBrowser = RTCBrowserType.RTC_BROWSER_ELECTRON;
  237. const version = userAgent.match(/Electron\/([\d.]+)/)[1];
  238. logger.info(`This appears to be Electron, ver: ${version}`);
  239. return version;
  240. }
  241. return null;
  242. }
  243. function detectNWJS() {
  244. const userAgent = navigator.userAgent;
  245. if (userAgent.match(/JitsiMeetNW/)) {
  246. currentBrowser = RTCBrowserType.RTC_BROWSER_NWJS;
  247. const version = userAgent.match(/JitsiMeetNW\/([\d.]+)/)[1];
  248. logger.info(`This appears to be JitsiMeetNW, ver: ${version}`);
  249. return version;
  250. }
  251. return null;
  252. }
  253. function detectReactNative() {
  254. const match
  255. = navigator.userAgent.match(/\b(react[ \t_-]*native)(?:\/(\S+))?/i);
  256. let version;
  257. // If we're remote debugging a React Native app, it may be treated as
  258. // Chrome. Check navigator.product as well and always return some version
  259. // even if we can't get the real one.
  260. if (match || navigator.product === 'ReactNative') {
  261. currentBrowser = RTCBrowserType.RTC_BROWSER_REACT_NATIVE;
  262. let name;
  263. if (match && match.length > 2) {
  264. name = match[1];
  265. version = match[2];
  266. }
  267. name || (name = 'react-native');
  268. version || (version = 'unknown');
  269. console.info(`This appears to be ${name}, ver: ${version}`);
  270. } else {
  271. // We're not running in a React Native environment.
  272. version = null;
  273. }
  274. return version;
  275. }
  276. function detectBrowser() {
  277. let version;
  278. const detectors = [
  279. detectReactNative,
  280. detectElectron,
  281. detectNWJS,
  282. detectOpera,
  283. detectChrome,
  284. detectFirefox,
  285. detectIE,
  286. detectSafari
  287. ];
  288. // Try all browser detectors
  289. for (let i = 0; i < detectors.length; i++) {
  290. version = detectors[i]();
  291. if (version) {
  292. return version;
  293. }
  294. }
  295. logger.warn('Browser type defaults to Safari ver 1');
  296. currentBrowser = RTCBrowserType.RTC_BROWSER_SAFARI;
  297. return 1;
  298. }
  299. const browserVersion = detectBrowser();
  300. const isAndroid = navigator.userAgent.indexOf('Android') !== -1;
  301. module.exports = RTCBrowserType;