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.

ScreenObtainer.js 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /* global chrome, $, alert */
  2. const GlobalOnErrorHandler = require('../util/GlobalOnErrorHandler');
  3. const logger = require('jitsi-meet-logger').getLogger(__filename);
  4. const RTCBrowserType = require('./RTCBrowserType');
  5. import JitsiTrackError from '../../JitsiTrackError';
  6. import * as JitsiTrackErrors from '../../JitsiTrackErrors';
  7. /**
  8. * Indicates whether the Chrome desktop sharing extension is installed.
  9. * @type {boolean}
  10. */
  11. let chromeExtInstalled = false;
  12. /**
  13. * Indicates whether an update of the Chrome desktop sharing extension is
  14. * required.
  15. * @type {boolean}
  16. */
  17. let chromeExtUpdateRequired = false;
  18. /**
  19. * Whether the jidesha extension for firefox is installed for the domain on
  20. * which we are running. Null designates an unknown value.
  21. * @type {null}
  22. */
  23. let firefoxExtInstalled = null;
  24. /**
  25. * If set to true, detection of an installed firefox extension will be started
  26. * again the next time obtainScreenOnFirefox is called (e.g. next time the
  27. * user tries to enable screen sharing).
  28. */
  29. let reDetectFirefoxExtension = false;
  30. let gumFunction = null;
  31. /**
  32. * The error returned by chrome when trying to start inline installation from
  33. * popup.
  34. */
  35. const CHROME_EXTENSION_POPUP_ERROR
  36. = 'Inline installs can not be initiated from pop-up windows.';
  37. /**
  38. * The error returned by chrome when trying to start inline installation from
  39. * iframe.
  40. */
  41. const CHROME_EXTENSION_IFRAME_ERROR
  42. = 'Chrome Web Store installations can only be started by the top frame.';
  43. /**
  44. * The error message returned by chrome when the extension is installed.
  45. */
  46. const CHROME_NO_EXTENSION_ERROR_MSG // eslint-disable-line no-unused-vars
  47. = 'Could not establish connection. Receiving end does not exist.';
  48. /**
  49. * Handles obtaining a stream from a screen capture on different browsers.
  50. */
  51. const ScreenObtainer = {
  52. obtainStream: null,
  53. /**
  54. * Initializes the function used to obtain a screen capture
  55. * (this.obtainStream).
  56. *
  57. * @param options {object}
  58. * @param gum {Function} GUM method
  59. */
  60. init(options, gum) {
  61. let obtainDesktopStream = null;
  62. this.options = options = options || {};
  63. gumFunction = gum;
  64. if (RTCBrowserType.isFirefox()) {
  65. initFirefoxExtensionDetection(options);
  66. }
  67. if (RTCBrowserType.isNWJS()) {
  68. obtainDesktopStream = (options, onSuccess, onFailure) => {
  69. window.JitsiMeetNW.obtainDesktopStream(
  70. onSuccess,
  71. (error, constraints) => {
  72. let jitsiError;
  73. // FIXME:
  74. // This is very very durty fix for recognising that the
  75. // user have clicked the cancel button from the Desktop
  76. // sharing pick window. The proper solution would be to
  77. // detect this in the NWJS application by checking the
  78. // streamId === "". Even better solution would be to
  79. // stop calling GUM from the NWJS app and just pass the
  80. // streamId to lib-jitsi-meet. This way the desktop
  81. // sharing implementation for NWJS and chrome extension
  82. // will be the same and lib-jitsi-meet will be able to
  83. // control the constraints, check the streamId, etc.
  84. //
  85. // I cannot find documentation about "InvalidStateError"
  86. // but this is what we are receiving from GUM when the
  87. // streamId for the desktop sharing is "".
  88. if (error && error.name == 'InvalidStateError') {
  89. jitsiError = new JitsiTrackError(
  90. JitsiTrackErrors.CHROME_EXTENSION_USER_CANCELED
  91. );
  92. } else {
  93. jitsiError = new JitsiTrackError(
  94. error, constraints, ['desktop']);
  95. }
  96. (typeof onFailure === 'function')
  97. && onFailure(jitsiError);
  98. });
  99. };
  100. } else if(RTCBrowserType.isElectron()) {
  101. obtainDesktopStream = (options, onSuccess, onFailure) =>
  102. window.JitsiMeetElectron.obtainDesktopStream(
  103. streamId =>
  104. onGetStreamResponse({streamId}, onSuccess, onFailure),
  105. err => onFailure(new JitsiTrackError(
  106. JitsiTrackErrors.CHROME_EXTENSION_GENERIC_ERROR, err))
  107. );
  108. } else if (RTCBrowserType.isTemasysPluginUsed()) {
  109. // XXX Don't require Temasys unless it's to be used because it
  110. // doesn't run on React Native, for example.
  111. const plugin
  112. = require('./adapter.screenshare').WebRTCPlugin.plugin;
  113. if (plugin.HasScreensharingFeature) {
  114. if (plugin.isScreensharingAvailable) {
  115. obtainDesktopStream = obtainWebRTCScreen;
  116. logger.info('Using Temasys plugin for desktop sharing');
  117. } else {
  118. logger.info(
  119. 'Screensharing not available with Temasys plugin on'
  120. + ' this site');
  121. }
  122. } else {
  123. logger.info(
  124. 'Screensharing not supported by this plugin version');
  125. }
  126. } else if (RTCBrowserType.isChrome()) {
  127. if (options.desktopSharingChromeDisabled
  128. || options.desktopSharingChromeMethod === false
  129. || !options.desktopSharingChromeExtId) {
  130. // TODO: desktopSharingChromeMethod is deprecated, remove.
  131. obtainDesktopStream = null;
  132. } else if (RTCBrowserType.getChromeVersion() >= 34) {
  133. obtainDesktopStream
  134. = this.obtainScreenFromExtension;
  135. logger.info('Using Chrome extension for desktop sharing');
  136. initChromeExtension(options);
  137. } else {
  138. logger.info('Chrome extension not supported until ver 34');
  139. }
  140. } else if (RTCBrowserType.isFirefox()) {
  141. if (options.desktopSharingFirefoxDisabled) {
  142. obtainDesktopStream = null;
  143. } else if (window.location.protocol === 'http:') {
  144. logger.log('Screen sharing is not supported over HTTP. '
  145. + 'Use of HTTPS is required.');
  146. obtainDesktopStream = null;
  147. } else {
  148. obtainDesktopStream = this.obtainScreenOnFirefox;
  149. }
  150. }
  151. if (!obtainDesktopStream) {
  152. logger.info('Desktop sharing disabled');
  153. }
  154. this.obtainStream = obtainDesktopStream;
  155. },
  156. /**
  157. * Checks whether obtaining a screen capture is supported in the current
  158. * environment.
  159. * @returns {boolean}
  160. */
  161. isSupported() {
  162. return this.obtainStream !== null;
  163. },
  164. /**
  165. * Obtains a screen capture stream on Firefox.
  166. * @param callback
  167. * @param errorCallback
  168. */
  169. obtainScreenOnFirefox(options, callback, errorCallback) {
  170. let extensionRequired = false;
  171. const { desktopSharingFirefoxMaxVersionExtRequired } = this.options;
  172. if (desktopSharingFirefoxMaxVersionExtRequired === -1
  173. || (desktopSharingFirefoxMaxVersionExtRequired >= 0
  174. && RTCBrowserType.getFirefoxVersion()
  175. <= desktopSharingFirefoxMaxVersionExtRequired)) {
  176. extensionRequired = true;
  177. logger.log(
  178. `Jidesha extension required on firefox version ${
  179. RTCBrowserType.getFirefoxVersion()}`);
  180. }
  181. if (!extensionRequired || firefoxExtInstalled === true) {
  182. obtainWebRTCScreen(options, callback, errorCallback);
  183. return;
  184. }
  185. if (reDetectFirefoxExtension) {
  186. reDetectFirefoxExtension = false;
  187. initFirefoxExtensionDetection(this.options);
  188. }
  189. // Give it some (more) time to initialize, and assume lack of
  190. // extension if it hasn't.
  191. if (firefoxExtInstalled === null) {
  192. window.setTimeout(
  193. () => {
  194. if (firefoxExtInstalled === null) {
  195. firefoxExtInstalled = false;
  196. }
  197. this.obtainScreenOnFirefox(callback, errorCallback);
  198. },
  199. 300);
  200. logger.log(
  201. 'Waiting for detection of jidesha on firefox to finish.');
  202. return;
  203. }
  204. // We need an extension and it isn't installed.
  205. // Make sure we check for the extension when the user clicks again.
  206. firefoxExtInstalled = null;
  207. reDetectFirefoxExtension = true;
  208. // Make sure desktopsharing knows that we failed, so that it doesn't get
  209. // stuck in 'switching' mode.
  210. errorCallback(
  211. new JitsiTrackError(JitsiTrackErrors.FIREFOX_EXTENSION_NEEDED));
  212. },
  213. /**
  214. * Asks Chrome extension to call chooseDesktopMedia and gets chrome
  215. * 'desktop' stream for returned stream token.
  216. */
  217. obtainScreenFromExtension(options, streamCallback, failCallback) {
  218. if (chromeExtInstalled) {
  219. doGetStreamFromExtension(this.options, streamCallback,
  220. failCallback);
  221. } else {
  222. if (chromeExtUpdateRequired) {
  223. /* eslint-disable no-alert */
  224. alert(
  225. 'Jitsi Desktop Streamer requires update. '
  226. + 'Changes will take effect after next Chrome restart.');
  227. /* eslint-enable no-alert */
  228. }
  229. try {
  230. chrome.webstore.install(
  231. getWebStoreInstallUrl(this.options),
  232. arg => {
  233. logger.log('Extension installed successfully', arg);
  234. chromeExtInstalled = true;
  235. // We need to give a moment to the endpoint to become
  236. // available.
  237. waitForExtensionAfterInstall(this.options, 200, 10)
  238. .then(() => {
  239. doGetStreamFromExtension(this.options,
  240. streamCallback, failCallback);
  241. }).catch(() => {
  242. this.handleExtensionInstallationError(options,
  243. streamCallback, failCallback);
  244. });
  245. },
  246. this.handleExtensionInstallationError.bind(this,
  247. options, streamCallback, failCallback)
  248. );
  249. } catch(e) {
  250. this.handleExtensionInstallationError(options, streamCallback,
  251. failCallback, e);
  252. }
  253. }
  254. },
  255. handleExtensionInstallationError(options, streamCallback, failCallback, e) {
  256. const webStoreInstallUrl = getWebStoreInstallUrl(this.options);
  257. if ((CHROME_EXTENSION_POPUP_ERROR === e
  258. || CHROME_EXTENSION_IFRAME_ERROR === e)
  259. && options.interval > 0
  260. && typeof options.checkAgain === 'function'
  261. && typeof options.listener === 'function') {
  262. options.listener('waitingForExtension', webStoreInstallUrl);
  263. this.checkForChromeExtensionOnInterval(options, streamCallback,
  264. failCallback, e);
  265. return;
  266. }
  267. const msg
  268. = `Failed to install the extension from ${webStoreInstallUrl}`;
  269. logger.log(msg, e);
  270. failCallback(new JitsiTrackError(
  271. JitsiTrackErrors.CHROME_EXTENSION_INSTALLATION_ERROR,
  272. msg));
  273. },
  274. checkForChromeExtensionOnInterval(options, streamCallback, failCallback) {
  275. if (options.checkAgain() === false) {
  276. failCallback(new JitsiTrackError(
  277. JitsiTrackErrors.CHROME_EXTENSION_INSTALLATION_ERROR));
  278. return;
  279. }
  280. waitForExtensionAfterInstall(this.options, options.interval, 1)
  281. .then(() => {
  282. chromeExtInstalled = true;
  283. options.listener('extensionFound');
  284. this.obtainScreenFromExtension(options,
  285. streamCallback, failCallback);
  286. }).catch(() => {
  287. this.checkForChromeExtensionOnInterval(options,
  288. streamCallback, failCallback);
  289. });
  290. }
  291. };
  292. /**
  293. * Obtains a desktop stream using getUserMedia.
  294. * For this to work on Chrome, the
  295. * 'chrome://flags/#enable-usermedia-screen-capture' flag must be enabled.
  296. *
  297. * On firefox, the document's domain must be white-listed in the
  298. * 'media.getusermedia.screensharing.allowed_domains' preference in
  299. * 'about:config'.
  300. */
  301. function obtainWebRTCScreen(options, streamCallback, failCallback) {
  302. gumFunction(['screen'], streamCallback, failCallback);
  303. }
  304. /**
  305. * Constructs inline install URL for Chrome desktop streaming extension.
  306. * The 'chromeExtensionId' must be defined in options parameter.
  307. * @param options supports "desktopSharingChromeExtId"
  308. * @returns {string}
  309. */
  310. function getWebStoreInstallUrl(options) {
  311. return (
  312. `https://chrome.google.com/webstore/detail/${
  313. options.desktopSharingChromeExtId}`);
  314. }
  315. /**
  316. * Checks whether an update of the Chrome extension is required.
  317. * @param minVersion minimal required version
  318. * @param extVersion current extension version
  319. * @returns {boolean}
  320. */
  321. function isUpdateRequired(minVersion, extVersion) {
  322. try {
  323. const s1 = minVersion.split('.');
  324. const s2 = extVersion.split('.');
  325. const len = Math.max(s1.length, s2.length);
  326. for (let i = 0; i < len; i++) {
  327. let n1 = 0,
  328. n2 = 0;
  329. if (i < s1.length) {
  330. n1 = parseInt(s1[i]);
  331. }
  332. if (i < s2.length) {
  333. n2 = parseInt(s2[i]);
  334. }
  335. if (isNaN(n1) || isNaN(n2)) {
  336. return true;
  337. } else if (n1 !== n2) {
  338. return n1 > n2;
  339. }
  340. }
  341. // will happen if both versions have identical numbers in
  342. // their components (even if one of them is longer, has more components)
  343. return false;
  344. } catch (e) {
  345. GlobalOnErrorHandler.callErrorHandler(e);
  346. logger.error('Failed to parse extension version', e);
  347. return true;
  348. }
  349. }
  350. function checkChromeExtInstalled(callback, options) {
  351. if (typeof chrome === 'undefined' || !chrome || !chrome.runtime) {
  352. // No API, so no extension for sure
  353. callback(false, false);
  354. return;
  355. }
  356. chrome.runtime.sendMessage(
  357. options.desktopSharingChromeExtId,
  358. { getVersion: true },
  359. response => {
  360. if (!response || !response.version) {
  361. // Communication failure - assume that no endpoint exists
  362. logger.warn(
  363. 'Extension not installed?: ', chrome.runtime.lastError);
  364. callback(false, false);
  365. return;
  366. }
  367. // Check installed extension version
  368. const extVersion = response.version;
  369. logger.log(`Extension version is: ${extVersion}`);
  370. const updateRequired
  371. = isUpdateRequired(
  372. options.desktopSharingChromeMinExtVersion,
  373. extVersion);
  374. callback(!updateRequired, updateRequired);
  375. }
  376. );
  377. }
  378. function doGetStreamFromExtension(options, streamCallback, failCallback) {
  379. // Sends 'getStream' msg to the extension.
  380. // Extension id must be defined in the config.
  381. chrome.runtime.sendMessage(
  382. options.desktopSharingChromeExtId,
  383. {
  384. getStream: true,
  385. sources: options.desktopSharingChromeSources
  386. },
  387. response => {
  388. if (!response) {
  389. // possibly re-wraping error message to make code consistent
  390. const lastError = chrome.runtime.lastError;
  391. failCallback(lastError instanceof Error
  392. ? lastError
  393. : new JitsiTrackError(
  394. JitsiTrackErrors.CHROME_EXTENSION_GENERIC_ERROR,
  395. lastError));
  396. return;
  397. }
  398. logger.log('Response from extension: ', response);
  399. onGetStreamResponse(response, streamCallback, failCallback);
  400. }
  401. );
  402. }
  403. /**
  404. * Initializes <link rel=chrome-webstore-item /> with extension id set in
  405. * config.js to support inline installs. Host site must be selected as main
  406. * website of published extension.
  407. * @param options supports "desktopSharingChromeExtId"
  408. */
  409. function initInlineInstalls(options) {
  410. if($('link[rel=chrome-webstore-item]').length === 0) {
  411. $('head').append('<link rel="chrome-webstore-item">');
  412. }
  413. $('link[rel=chrome-webstore-item]').attr('href',
  414. getWebStoreInstallUrl(options));
  415. }
  416. function initChromeExtension(options) {
  417. // Initialize Chrome extension inline installs
  418. initInlineInstalls(options);
  419. // Check if extension is installed
  420. checkChromeExtInstalled((installed, updateRequired) => {
  421. chromeExtInstalled = installed;
  422. chromeExtUpdateRequired = updateRequired;
  423. logger.info(
  424. `Chrome extension installed: ${chromeExtInstalled
  425. } updateRequired: ${chromeExtUpdateRequired}`);
  426. }, options);
  427. }
  428. /**
  429. * Checks "retries" times on every "waitInterval"ms whether the ext is alive.
  430. * @param {Object} options the options passed to ScreanObtainer.obtainStream
  431. * @param {int} waitInterval the number of ms between retries
  432. * @param {int} retries the number of retries
  433. * @returns {Promise} returns promise that will be resolved when the extension
  434. * is alive and rejected if the extension is not alive even after "retries"
  435. * checks
  436. */
  437. function waitForExtensionAfterInstall(options, waitInterval, retries) {
  438. if(retries === 0) {
  439. return Promise.reject();
  440. }
  441. return new Promise((resolve, reject) => {
  442. let currentRetries = retries;
  443. const interval = window.setInterval(() => {
  444. checkChromeExtInstalled(installed => {
  445. if(installed) {
  446. window.clearInterval(interval);
  447. resolve();
  448. } else {
  449. currentRetries--;
  450. if(currentRetries === 0) {
  451. reject();
  452. window.clearInterval(interval);
  453. }
  454. }
  455. }, options);
  456. }, waitInterval);
  457. });
  458. }
  459. /**
  460. * Handles response from external application / extension and calls GUM to
  461. * receive the desktop streams or reports error.
  462. * @param {object} response
  463. * @param {string} response.streamId - the streamId for the desktop stream
  464. * @param {string} response.error - error to be reported.
  465. * @param {Function} onSuccess - callback for success.
  466. * @param {Function} onFailure - callback for failure.
  467. */
  468. function onGetStreamResponse(response, onSuccess, onFailure) {
  469. if (response.streamId) {
  470. gumFunction(
  471. ['desktop'],
  472. stream => onSuccess(stream),
  473. onFailure,
  474. { desktopStream: response.streamId });
  475. } else {
  476. // As noted in Chrome Desktop Capture API:
  477. // If user didn't select any source (i.e. canceled the prompt)
  478. // then the callback is called with an empty streamId.
  479. if(response.streamId === '') {
  480. onFailure(new JitsiTrackError(
  481. JitsiTrackErrors.CHROME_EXTENSION_USER_CANCELED));
  482. return;
  483. }
  484. onFailure(new JitsiTrackError(
  485. JitsiTrackErrors.CHROME_EXTENSION_GENERIC_ERROR,
  486. response.error));
  487. }
  488. }
  489. /**
  490. * Starts the detection of an installed jidesha extension for firefox.
  491. * @param options supports "desktopSharingFirefoxDisabled",
  492. * "desktopSharingFirefoxExtId"
  493. */
  494. function initFirefoxExtensionDetection(options) {
  495. if (options.desktopSharingFirefoxDisabled) {
  496. return;
  497. }
  498. if (firefoxExtInstalled === false || firefoxExtInstalled === true) {
  499. return;
  500. }
  501. if (!options.desktopSharingFirefoxExtId) {
  502. firefoxExtInstalled = false;
  503. return;
  504. }
  505. const img = document.createElement('img');
  506. img.onload = () => {
  507. logger.log('Detected firefox screen sharing extension.');
  508. firefoxExtInstalled = true;
  509. };
  510. img.onerror = () => {
  511. logger.log('Detected lack of firefox screen sharing extension.');
  512. firefoxExtInstalled = false;
  513. };
  514. // The jidesha extension exposes an empty image file under the url:
  515. // "chrome://EXT_ID/content/DOMAIN.png"
  516. // Where EXT_ID is the ID of the extension with "@" replaced by ".", and
  517. // DOMAIN is a domain whitelisted by the extension.
  518. const src
  519. = `chrome://${options.desktopSharingFirefoxExtId.replace('@', '.')
  520. }/content/${document.location.hostname}.png`;
  521. img.setAttribute('src', src);
  522. }
  523. module.exports = ScreenObtainer;