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

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