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

RTCBrowserType.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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();
  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.isReactNative();
  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();
  182. },
  183. /**
  184. * Checks if the current browser reports round trip time statistics for
  185. * the ICE candidate pair.
  186. * @return {boolean}
  187. */
  188. supportsRTTStatistics() {
  189. // Firefox does not seem to report RTT for ICE candidate pair:
  190. // eslint-disable-next-line max-len
  191. // https://www.w3.org/TR/webrtc-stats/#dom-rtcicecandidatepairstats-currentroundtriptime
  192. // It does report mozRTT for RTP streams, but at the time of this
  193. // writing it's value does not make sense most of the time
  194. // (is reported as 1):
  195. // https://bugzilla.mozilla.org/show_bug.cgi?id=1241066
  196. // For Chrome and others we rely on 'googRtt'.
  197. return !RTCBrowserType.isFirefox();
  198. },
  199. /**
  200. * Whether jitsi-meet supports simulcast on the current browser.
  201. * @returns {boolean}
  202. */
  203. supportsSimulcast() {
  204. return RTCBrowserType.isChrome()
  205. || RTCBrowserType.isElectron()
  206. || RTCBrowserType.isNWJS();
  207. },
  208. supportsRtx() {
  209. return !RTCBrowserType.isFirefox();
  210. }
  211. // Add version getters for other browsers when needed
  212. };
  213. /**
  214. * detectOpera() must be called before detectChrome() !!!
  215. * otherwise Opera wil be detected as Chrome
  216. */
  217. function detectChrome() {
  218. if (navigator.webkitGetUserMedia) {
  219. currentBrowser = RTCBrowserType.RTC_BROWSER_CHROME;
  220. const userAgent = navigator.userAgent.toLowerCase();
  221. // We can assume that user agent is chrome, because it's
  222. // enforced when 'ext' streaming method is set
  223. const ver = parseInt(userAgent.match(/chrome\/(\d+)\./)[1], 10);
  224. logger.log(`This appears to be Chrome, ver: ${ver}`);
  225. return ver;
  226. }
  227. return null;
  228. }
  229. /**
  230. *
  231. */
  232. function detectOpera() {
  233. const userAgent = navigator.userAgent;
  234. if (userAgent.match(/Opera|OPR/)) {
  235. currentBrowser = RTCBrowserType.RTC_BROWSER_OPERA;
  236. const version = userAgent.match(/(Opera|OPR) ?\/?(\d+)\.?/)[2];
  237. logger.info(`This appears to be Opera, ver: ${version}`);
  238. return version;
  239. }
  240. return null;
  241. }
  242. /**
  243. *
  244. */
  245. function detectFirefox() {
  246. if (navigator.mozGetUserMedia) {
  247. currentBrowser = RTCBrowserType.RTC_BROWSER_FIREFOX;
  248. const version = parseInt(
  249. navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
  250. logger.log(`This appears to be Firefox, ver: ${version}`);
  251. return version;
  252. }
  253. return null;
  254. }
  255. /**
  256. *
  257. */
  258. function detectSafari() {
  259. if (/^((?!chrome).)*safari/i.test(navigator.userAgent)) {
  260. currentBrowser = RTCBrowserType.RTC_BROWSER_SAFARI;
  261. logger.info('This appears to be Safari');
  262. // FIXME detect Safari version when needed
  263. return 1;
  264. }
  265. return null;
  266. }
  267. /**
  268. * Detects IE.
  269. */
  270. function detectIE() {
  271. let version;
  272. const ua = window.navigator.userAgent;
  273. const msie = ua.indexOf('MSIE ');
  274. if (msie > 0) {
  275. // IE 10 or older => return version number
  276. version = parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
  277. }
  278. const trident = ua.indexOf('Trident/');
  279. if (!version && trident > 0) {
  280. // IE 11 => return version number
  281. const rv = ua.indexOf('rv:');
  282. version = parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
  283. }
  284. if (version) {
  285. currentBrowser = RTCBrowserType.RTC_BROWSER_IEXPLORER;
  286. logger.info(`This appears to be IExplorer, ver: ${version}`);
  287. }
  288. return version;
  289. }
  290. /**
  291. * Detects Edge.
  292. */
  293. function detectEdge() {
  294. let version;
  295. const ua = window.navigator.userAgent;
  296. const edge = ua.indexOf('Edge/');
  297. if (!version && edge > 0) {
  298. version = parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
  299. }
  300. if (version) {
  301. currentBrowser = RTCBrowserType.RTC_BROWSER_EDGE;
  302. logger.info(`This appears to be Edge, ver: ${version}`);
  303. }
  304. return version;
  305. }
  306. /**
  307. * Detects Electron environment.
  308. */
  309. function detectElectron() {
  310. const userAgent = navigator.userAgent;
  311. if (userAgent.match(/Electron/)) {
  312. currentBrowser = RTCBrowserType.RTC_BROWSER_ELECTRON;
  313. const version = userAgent.match(/Electron\/([\d.]+)/)[1];
  314. logger.info(`This appears to be Electron, ver: ${version}`);
  315. return version;
  316. }
  317. return null;
  318. }
  319. /**
  320. *
  321. */
  322. function detectNWJS() {
  323. const userAgent = navigator.userAgent;
  324. if (userAgent.match(/JitsiMeetNW/)) {
  325. currentBrowser = RTCBrowserType.RTC_BROWSER_NWJS;
  326. const version = userAgent.match(/JitsiMeetNW\/([\d.]+)/)[1];
  327. logger.info(`This appears to be JitsiMeetNW, ver: ${version}`);
  328. return version;
  329. }
  330. return null;
  331. }
  332. /**
  333. *
  334. */
  335. function detectReactNative() {
  336. const match
  337. = navigator.userAgent.match(/\b(react[ \t_-]*native)(?:\/(\S+))?/i);
  338. let version;
  339. // If we're remote debugging a React Native app, it may be treated as
  340. // Chrome. Check navigator.product as well and always return some version
  341. // even if we can't get the real one.
  342. if (match || navigator.product === 'ReactNative') {
  343. currentBrowser = RTCBrowserType.RTC_BROWSER_REACT_NATIVE;
  344. let name;
  345. if (match && match.length > 2) {
  346. name = match[1];
  347. version = match[2];
  348. }
  349. name || (name = 'react-native');
  350. version || (version = 'unknown');
  351. console.info(`This appears to be ${name}, ver: ${version}`);
  352. } else {
  353. // We're not running in a React Native environment.
  354. version = null;
  355. }
  356. return version;
  357. }
  358. /**
  359. *
  360. */
  361. function detectBrowser() {
  362. let version;
  363. const detectors = [
  364. detectReactNative,
  365. detectElectron,
  366. detectNWJS,
  367. detectOpera,
  368. detectChrome,
  369. detectFirefox,
  370. detectEdge,
  371. detectIE,
  372. detectSafari
  373. ];
  374. // Try all browser detectors
  375. for (let i = 0; i < detectors.length; i++) {
  376. version = detectors[i]();
  377. if (version) {
  378. return version;
  379. }
  380. }
  381. logger.warn('Browser type defaults to Safari ver 1');
  382. currentBrowser = RTCBrowserType.RTC_BROWSER_SAFARI;
  383. return 1;
  384. }
  385. browserVersion = detectBrowser();
  386. export default RTCBrowserType;