您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

BrowserCapabilities.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import { getLogger } from 'jitsi-meet-logger';
  2. import { BrowserDetection } from 'js-utils';
  3. const logger = getLogger(__filename);
  4. // TODO: Move this code to js-utils.
  5. // NOTE: Now we are extending BrowserDetection in order to preserve
  6. // RTCBrowserType interface but maybe it worth exporting BrowserCapabilities
  7. // and BrowserDetection as separate objects in future.
  8. /**
  9. * Implements browser capabilities for lib-jitsi-meet.
  10. */
  11. export default class BrowserCapabilities extends BrowserDetection {
  12. /**
  13. * Creates new BrowserCapabilities instance.
  14. */
  15. constructor() {
  16. super();
  17. logger.info(
  18. `This appears to be ${this.getName()}, ver: ${this.getVersion()}`);
  19. }
  20. /**
  21. * Tells whether or not the <tt>MediaStream/tt> is removed from
  22. * the <tt>PeerConnection</tt> and disposed on video mute (in order to turn
  23. * off the camera device).
  24. * @return {boolean} <tt>true</tt> if the current browser supports this
  25. * strategy or <tt>false</tt> otherwise.
  26. */
  27. doesVideoMuteByStreamRemove() {
  28. return !(
  29. this.isFirefox()
  30. || this.isEdge()
  31. || this.isReactNative()
  32. || this.isSafariWithWebrtc()
  33. );
  34. }
  35. /**
  36. * Check whether or not the current browser support peer to peer connections
  37. * @return {boolean} <tt>true</tt> if p2p is supported or <tt>false</tt>
  38. * otherwise.
  39. */
  40. supportsP2P() {
  41. return !this.isEdge() && !this.isFirefox();
  42. }
  43. /**
  44. * Checks if the current browser is Chromium based, that is, it's either
  45. * Chrome / Chromium or uses it as its engine, but doesn't identify as
  46. * Chrome.
  47. *
  48. * This includes the following browsers:
  49. * - Chrome and Chromium
  50. * - Other browsers which use the Chrome engine, but are detected as Chrome,
  51. * such as Brave and Vivaldi
  52. * - Browsers which are NOT Chrome but use it as their engine, and have
  53. * custom detection code: Opera, Electron and NW.JS
  54. */
  55. isChromiumBased() {
  56. return this.isChrome()
  57. || this.isElectron()
  58. || this.isNWJS()
  59. || this.isOpera();
  60. }
  61. /**
  62. * Checks if current browser is a Safari and a version of Safari that
  63. * supports native webrtc.
  64. *
  65. * @returns {boolean}
  66. */
  67. isSafariWithWebrtc() {
  68. return this.isSafari()
  69. && !this.isVersionLessThan('11');
  70. }
  71. /**
  72. * Checks if current browser is a Safari and a version of Safari that
  73. * supports VP8.
  74. *
  75. * @returns {boolean}
  76. */
  77. isSafariWithVP8() {
  78. return this.isSafari()
  79. && !this.isVersionLessThan('12.1');
  80. }
  81. /**
  82. * Checks if the current browser is supported.
  83. *
  84. * @returns {boolean} true if the browser is supported, false otherwise.
  85. */
  86. isSupported() {
  87. return this.isChromiumBased()
  88. || this.isFirefox()
  89. || this.isReactNative()
  90. || this.isSafariWithWebrtc();
  91. }
  92. /**
  93. * Checks if the current browser triggers 'onmute'/'onunmute' events when
  94. * user's connection is interrupted and the video stops playback.
  95. * @returns {*|boolean} 'true' if the event is supported or 'false'
  96. * otherwise.
  97. */
  98. supportsVideoMuteOnConnInterrupted() {
  99. return this.isChromiumBased() || this.isReactNative()
  100. || this.isSafariWithVP8();
  101. }
  102. /**
  103. * Checks if the current browser reports upload and download bandwidth
  104. * statistics.
  105. * @return {boolean}
  106. */
  107. supportsBandwidthStatistics() {
  108. // FIXME bandwidth stats are currently not implemented for FF on our
  109. // side, but not sure if not possible ?
  110. return !this.isFirefox() && !this.isEdge()
  111. && !this.isSafariWithWebrtc();
  112. }
  113. /**
  114. * Checks if the current browser supports WebRTC datachannels.
  115. * @return {boolean}
  116. */
  117. supportsDataChannels() {
  118. // NOTE: Edge does not yet implement DataChannel.
  119. return !this.isEdge();
  120. }
  121. /**
  122. * Checks if the current browser support the device change event.
  123. * @return {boolean}
  124. */
  125. supportsDeviceChangeEvent() {
  126. return navigator.mediaDevices
  127. && typeof navigator.mediaDevices.ondevicechange !== 'undefined'
  128. && typeof navigator.mediaDevices.addEventListener !== 'undefined';
  129. }
  130. /**
  131. * Checks if the current browser supports the MediaStream constructor as
  132. * defined by https://www.w3.org/TR/mediacapture-streams/#constructors. In
  133. * cases where there is no support, it maybe be necessary to get audio
  134. * and video in two distinct GUM calls.
  135. * @return {boolean}
  136. */
  137. supportsMediaStreamConstructor() {
  138. return !this.isReactNative();
  139. }
  140. /**
  141. * Checks if the current browser supports RTP statictics collecting.
  142. * Required by {@link RTPStatsCollector}.
  143. *
  144. * @returns {boolean} true if they are supported, false otherwise.
  145. */
  146. supportsRtpStatistics() {
  147. return this.isChromiumBased()
  148. || this.isEdge()
  149. || this.isFirefox()
  150. || this.isReactNative()
  151. || this.isSafariWithWebrtc();
  152. }
  153. /**
  154. * Checks if the current browser supports RTT statistics for srflx local
  155. * candidates through the legacy getStats() API.
  156. */
  157. supportsLocalCandidateRttStatistics() {
  158. return this.isChromiumBased() || this.isReactNative()
  159. || this.isSafariWithVP8();
  160. }
  161. /**
  162. * Checks if the current browser reports round trip time statistics for
  163. * the ICE candidate pair.
  164. * @return {boolean}
  165. */
  166. supportsRTTStatistics() {
  167. // Firefox does not seem to report RTT for ICE candidate pair:
  168. // eslint-disable-next-line max-len
  169. // https://www.w3.org/TR/webrtc-stats/#dom-rtcicecandidatepairstats-currentroundtriptime
  170. // It does report mozRTT for RTP streams, but at the time of this
  171. // writing it's value does not make sense most of the time
  172. // (is reported as 1):
  173. // https://bugzilla.mozilla.org/show_bug.cgi?id=1241066
  174. // For Chrome and others we rely on 'googRtt'.
  175. return !this.isFirefox() && !this.isEdge();
  176. }
  177. /**
  178. * Checks whether the browser supports RTPSender.
  179. *
  180. * @returns {boolean}
  181. */
  182. supportsRtpSender() {
  183. return this.isFirefox() || this.isSafariWithVP8();
  184. }
  185. /**
  186. * Checks whether the browser supports RTX.
  187. *
  188. * @returns {boolean}
  189. */
  190. supportsRtx() {
  191. return !this.isFirefox() && !this.usesUnifiedPlan();
  192. }
  193. /**
  194. * Whether jitsi-meet supports simulcast on the current browser.
  195. * @returns {boolean}
  196. */
  197. supportsSimulcast() {
  198. return this.isChromiumBased() || this.isFirefox()
  199. || this.isSafariWithVP8() || this.isReactNative();
  200. }
  201. /**
  202. * Returns whether or not the current browser can support capturing video,
  203. * be it camera or desktop, and displaying received video.
  204. *
  205. * @returns {boolean}
  206. */
  207. supportsVideo() {
  208. // FIXME: Check if we can use supportsVideoOut and supportsVideoIn. I
  209. // leave the old implementation here in order not to brake something.
  210. // Older versions of Safari using webrtc/adapter do not support video
  211. // due in part to Safari only supporting H264 and the bridge sending VP8
  212. // Newer Safari support VP8 and other WebRTC features.
  213. return !this.isSafariWithWebrtc()
  214. || (this.isSafariWithVP8() && this.usesPlanB());
  215. }
  216. /**
  217. * Checks if the browser uses plan B.
  218. *
  219. * @returns {boolean}
  220. */
  221. usesPlanB() {
  222. return !this.usesUnifiedPlan();
  223. }
  224. /**
  225. * Checks if the browser uses unified plan.
  226. *
  227. * @returns {boolean}
  228. */
  229. usesUnifiedPlan() {
  230. if (this.isFirefox()) {
  231. return true;
  232. }
  233. if (this.isSafariWithVP8()) {
  234. // eslint-disable-next-line max-len
  235. // https://trac.webkit.org/changeset/236144/webkit/trunk/LayoutTests/webrtc/video-addLegacyTransceiver.html
  236. // eslint-disable-next-line no-undef
  237. return Object.keys(RTCRtpTransceiver.prototype)
  238. .indexOf('currentDirection') > -1;
  239. }
  240. return false;
  241. }
  242. /**
  243. * Returns whether or not the current browser should be using the new
  244. * getUserMedia flow, which utilizes the adapter shim. This method should
  245. * be temporary and used while migrating all browsers to use adapter and
  246. * the new getUserMedia.
  247. *
  248. * @returns {boolean}
  249. */
  250. usesNewGumFlow() {
  251. const REQUIRED_CHROME_VERSION = 61;
  252. if (this.isChrome()) {
  253. return !this.isVersionLessThan(REQUIRED_CHROME_VERSION);
  254. }
  255. if (this.isFirefox() || this.isSafariWithWebrtc()) {
  256. return true;
  257. }
  258. if (this.isChromiumBased()) {
  259. return this._getChromiumBasedVersion() >= REQUIRED_CHROME_VERSION;
  260. }
  261. return false;
  262. }
  263. /**
  264. * Checks if the browser uses webrtc-adapter. All browsers using the new
  265. * getUserMedia flow and Edge.
  266. *
  267. * @returns {boolean}
  268. */
  269. usesAdapter() {
  270. return this.usesNewGumFlow() || this.isEdge();
  271. }
  272. /**
  273. * Checks if the browser supports getDisplayMedia.
  274. * @returns {boolean} {@code true} if the browser supports getDisplayMedia.
  275. */
  276. supportsGetDisplayMedia() {
  277. return typeof navigator.getDisplayMedia !== 'undefined'
  278. || (typeof navigator.mediaDevices !== 'undefined'
  279. && typeof navigator.mediaDevices.getDisplayMedia
  280. !== 'undefined');
  281. }
  282. /**
  283. * Checks if the browser supports the "sdpSemantics" configuration option.
  284. * https://webrtc.org/web-apis/chrome/unified-plan/
  285. *
  286. * @returns {boolean}
  287. */
  288. supportsSdpSemantics() {
  289. return this.isChromiumBased() && this._getChromiumBasedVersion() >= 65;
  290. }
  291. /**
  292. * Returns the version of a Chromium based browser.
  293. *
  294. * @returns {Number}
  295. */
  296. _getChromiumBasedVersion() {
  297. if (this.isChromiumBased()) {
  298. // NW.JS doesn't expose the Chrome version in the UA string.
  299. if (this.isNWJS()) {
  300. // eslint-disable-next-line no-undef
  301. return Number.parseInt(process.versions.chromium, 10);
  302. }
  303. // Here we process all browsers which use the Chrome engine but
  304. // don't necessarily identify as Chrome. We cannot use the version
  305. // comparing functions because the Electron, Opera and NW.JS
  306. // versions are inconsequential here, as we need to know the actual
  307. // Chrome engine version.
  308. const ua = navigator.userAgent;
  309. if (ua.match(/Chrome/)) {
  310. const version
  311. = Number.parseInt(ua.match(/Chrome\/([\d.]+)/)[1], 10);
  312. return version;
  313. }
  314. }
  315. return -1;
  316. }
  317. }