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 20KB

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