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

RTCBrowserType.js 12KB

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