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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /* global chrome, $, alert */
  2. var AdapterJS = require("./adapter.screenshare");
  3. var GlobalOnErrorHandler = require("../util/GlobalOnErrorHandler");
  4. var logger = require("jitsi-meet-logger").getLogger(__filename);
  5. var RTCBrowserType = require("./RTCBrowserType");
  6. import JitsiTrackError from "../../JitsiTrackError";
  7. import * as JitsiTrackErrors from "../../JitsiTrackErrors";
  8. /**
  9. * Indicates whether the Chrome desktop sharing extension is installed.
  10. * @type {boolean}
  11. */
  12. var chromeExtInstalled = false;
  13. /**
  14. * Indicates whether an update of the Chrome desktop sharing extension is
  15. * required.
  16. * @type {boolean}
  17. */
  18. var chromeExtUpdateRequired = false;
  19. /**
  20. * Whether the jidesha extension for firefox is installed for the domain on
  21. * which we are running. Null designates an unknown value.
  22. * @type {null}
  23. */
  24. var firefoxExtInstalled = null;
  25. /**
  26. * If set to true, detection of an installed firefox extension will be started
  27. * again the next time obtainScreenOnFirefox is called (e.g. next time the
  28. * user tries to enable screen sharing).
  29. */
  30. var reDetectFirefoxExtension = false;
  31. var GUM = null;
  32. /**
  33. * The error returned by chrome when trying to start inline installation from
  34. * popup.
  35. */
  36. const CHROME_EXTENSION_POPUP_ERROR
  37. = "Inline installs can not be initiated from pop-up windows.";
  38. /**
  39. * The error message returned by chrome when the extension is installed.
  40. */
  41. const CHROME_NO_EXTENSION_ERROR_MSG // eslint-disable-line no-unused-vars
  42. = "Could not establish connection. Receiving end does not exist.";
  43. /**
  44. * Handles obtaining a stream from a screen capture on different browsers.
  45. */
  46. var ScreenObtainer = {
  47. obtainStream: null,
  48. /**
  49. * Initializes the function used to obtain a screen capture
  50. * (this.obtainStream).
  51. *
  52. * If the browser is Chrome, it uses the value of
  53. * 'options.desktopSharingChromeMethod' (or 'options.desktopSharing') to
  54. * decide whether to use the a Chrome extension (if the value is 'ext'),
  55. * use the "screen" media source (if the value is 'webrtc'),
  56. * or disable screen capture (if the value is other).
  57. * Note that for the "screen" media source to work the
  58. * 'chrome://flags/#enable-usermedia-screen-capture' flag must be set.
  59. * @param options {object}
  60. * @param gum {Function} GUM method
  61. */
  62. init(options, gum) {
  63. var obtainDesktopStream = null;
  64. this.options = options = options || {};
  65. GUM = gum;
  66. if (RTCBrowserType.isFirefox())
  67. initFirefoxExtensionDetection(options);
  68. // TODO remove this, options.desktopSharing is deprecated.
  69. var chromeMethod =
  70. (options.desktopSharingChromeMethod || options.desktopSharing);
  71. if (RTCBrowserType.isNWJS()) {
  72. obtainDesktopStream = (options, onSuccess, onFailure) => {
  73. window.JitsiMeetNW.obtainDesktopStream (
  74. onSuccess,
  75. (error, constraints) => {
  76. var jitsiError;
  77. // FIXME:
  78. // This is very very durty fix for recognising that the
  79. // user have clicked the cancel button from the Desktop
  80. // sharing pick window. The proper solution would be to
  81. // detect this in the NWJS application by checking the
  82. // streamId === "". Even better solution would be to
  83. // stop calling GUM from the NWJS app and just pass the
  84. // streamId to lib-jitsi-meet. This way the desktop
  85. // sharing implementation for NWJS and chrome extension
  86. // will be the same and lib-jitsi-meet will be able to
  87. // control the constraints, check the streamId, etc.
  88. //
  89. // I cannot find documentation about "InvalidStateError"
  90. // but this is what we are receiving from GUM when the
  91. // streamId for the desktop sharing is "".
  92. if (error && error.name == "InvalidStateError") {
  93. jitsiError = new JitsiTrackError(
  94. JitsiTrackErrors.CHROME_EXTENSION_USER_CANCELED
  95. );
  96. } else {
  97. jitsiError = new JitsiTrackError(
  98. error, constraints, ["desktop"]);
  99. }
  100. (typeof(onFailure) === "function") &&
  101. onFailure(jitsiError);
  102. });
  103. };
  104. } else if (RTCBrowserType.isTemasysPluginUsed()) {
  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 (chromeMethod == "ext") {
  118. if (RTCBrowserType.getChromeVersion() >= 34) {
  119. obtainDesktopStream =
  120. this.obtainScreenFromExtension;
  121. logger.info("Using Chrome extension for desktop sharing");
  122. initChromeExtension(options);
  123. } else {
  124. logger.info("Chrome extension not supported until ver 34");
  125. }
  126. } else if (chromeMethod == "webrtc") {
  127. obtainDesktopStream = obtainWebRTCScreen;
  128. logger.info("Using Chrome WebRTC for desktop sharing");
  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(reason => {
  227. this.handleExtensionInstallationError(options,
  228. streamCallback, failCallback, reason);
  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" and "chromeExtensionId"
  292. * @returns {string}
  293. */
  294. function getWebStoreInstallUrl(options)
  295. {
  296. //TODO remove chromeExtensionId (deprecated)
  297. return "https://chrome.google.com/webstore/detail/" +
  298. (options.desktopSharingChromeExtId || options.chromeExtensionId);
  299. }
  300. /**
  301. * Checks whether an update of the Chrome extension is required.
  302. * @param minVersion minimal required version
  303. * @param extVersion current extension version
  304. * @returns {boolean}
  305. */
  306. function isUpdateRequired(minVersion, extVersion) {
  307. try {
  308. var s1 = minVersion.split('.');
  309. var s2 = extVersion.split('.');
  310. var len = Math.max(s1.length, s2.length);
  311. for (var i = 0; i < len; i++) {
  312. var n1 = 0,
  313. n2 = 0;
  314. if (i < s1.length)
  315. n1 = parseInt(s1[i]);
  316. if (i < s2.length)
  317. n2 = parseInt(s2[i]);
  318. if (isNaN(n1) || isNaN(n2)) {
  319. return true;
  320. } else if (n1 !== n2) {
  321. return n1 > n2;
  322. }
  323. }
  324. // will happen if both versions have identical numbers in
  325. // their components (even if one of them is longer, has more components)
  326. return false;
  327. }
  328. catch (e) {
  329. GlobalOnErrorHandler.callErrorHandler(e);
  330. logger.error("Failed to parse extension version", e);
  331. return true;
  332. }
  333. }
  334. function checkChromeExtInstalled(callback, options) {
  335. if (typeof chrome === "undefined" || !chrome || !chrome.runtime) {
  336. // No API, so no extension for sure
  337. callback(false, false);
  338. return;
  339. }
  340. chrome.runtime.sendMessage(
  341. //TODO: remove chromeExtensionId (deprecated)
  342. (options.desktopSharingChromeExtId || options.chromeExtensionId),
  343. { getVersion: true },
  344. response => {
  345. if (!response || !response.version) {
  346. // Communication failure - assume that no endpoint exists
  347. logger.warn(
  348. "Extension not installed?: ", chrome.runtime.lastError);
  349. callback(false, false);
  350. return;
  351. }
  352. // Check installed extension version
  353. var extVersion = response.version;
  354. logger.log('Extension version is: ' + extVersion);
  355. //TODO: remove minChromeExtVersion (deprecated)
  356. var updateRequired
  357. = isUpdateRequired(
  358. (options.desktopSharingChromeMinExtVersion ||
  359. options.minChromeExtVersion),
  360. extVersion);
  361. callback(!updateRequired, updateRequired);
  362. }
  363. );
  364. }
  365. function doGetStreamFromExtension(options, streamCallback, failCallback) {
  366. // Sends 'getStream' msg to the extension.
  367. // Extension id must be defined in the config.
  368. chrome.runtime.sendMessage(
  369. //TODO: remove chromeExtensionId (deprecated)
  370. (options.desktopSharingChromeExtId || options.chromeExtensionId),
  371. {
  372. getStream: true,
  373. //TODO: remove desktopSharingSources (deprecated).
  374. sources: (options.desktopSharingChromeSources ||
  375. options.desktopSharingSources)
  376. },
  377. response => {
  378. if (!response) {
  379. // possibly re-wraping error message to make code consistent
  380. var lastError = chrome.runtime.lastError;
  381. failCallback(lastError instanceof Error
  382. ? lastError
  383. : new JitsiTrackError(
  384. JitsiTrackErrors.CHROME_EXTENSION_GENERIC_ERROR,
  385. lastError));
  386. return;
  387. }
  388. logger.log("Response from extension: ", response);
  389. if (response.streamId) {
  390. GUM(
  391. ['desktop'],
  392. stream => streamCallback(stream),
  393. failCallback,
  394. { desktopStream: response.streamId });
  395. } else {
  396. // As noted in Chrome Desktop Capture API:
  397. // If user didn't select any source (i.e. canceled the prompt)
  398. // then the callback is called with an empty streamId.
  399. if(response.streamId === "")
  400. {
  401. failCallback(new JitsiTrackError(
  402. JitsiTrackErrors.CHROME_EXTENSION_USER_CANCELED));
  403. return;
  404. }
  405. failCallback(new JitsiTrackError(
  406. JitsiTrackErrors.CHROME_EXTENSION_GENERIC_ERROR,
  407. response.error));
  408. }
  409. }
  410. );
  411. }
  412. /**
  413. * Initializes <link rel=chrome-webstore-item /> with extension id set in
  414. * config.js to support inline installs. Host site must be selected as main
  415. * website of published extension.
  416. * @param options supports "desktopSharingChromeExtId" and "chromeExtensionId"
  417. */
  418. function initInlineInstalls(options)
  419. {
  420. if($("link[rel=chrome-webstore-item]").length === 0) {
  421. $("head").append("<link rel=\"chrome-webstore-item\">");
  422. }
  423. $("link[rel=chrome-webstore-item]").attr("href",
  424. getWebStoreInstallUrl(options));
  425. }
  426. function initChromeExtension(options) {
  427. // Initialize Chrome extension inline installs
  428. initInlineInstalls(options);
  429. // Check if extension is installed
  430. checkChromeExtInstalled((installed, updateRequired) => {
  431. chromeExtInstalled = installed;
  432. chromeExtUpdateRequired = updateRequired;
  433. logger.info(
  434. "Chrome extension installed: " + chromeExtInstalled +
  435. " updateRequired: " + chromeExtUpdateRequired);
  436. }, options);
  437. }
  438. /**
  439. * Checks "retries" times on every "waitInterval"ms whether the ext is alive.
  440. * @param {Object} options the options passed to ScreanObtainer.obtainStream
  441. * @param {int} waitInterval the number of ms between retries
  442. * @param {int} retries the number of retries
  443. * @returns {Promise} returns promise that will be resolved when the extension
  444. * is alive and rejected if the extension is not alive even after "retries"
  445. * checks
  446. */
  447. function waitForExtensionAfterInstall(options, waitInterval, retries) {
  448. if(retries === 0) {
  449. return Promise.reject();
  450. }
  451. return new Promise((resolve, reject) => {
  452. let currentRetries = retries;
  453. let interval = window.setInterval(() => {
  454. checkChromeExtInstalled( (installed) => {
  455. if(installed) {
  456. window.clearInterval(interval);
  457. resolve();
  458. } else {
  459. currentRetries--;
  460. if(currentRetries === 0) {
  461. reject();
  462. window.clearInterval(interval);
  463. }
  464. }
  465. }, options);
  466. }, waitInterval);
  467. });
  468. }
  469. /**
  470. * Starts the detection of an installed jidesha extension for firefox.
  471. * @param options supports "desktopSharingFirefoxDisabled",
  472. * "desktopSharingFirefoxExtId" and "chromeExtensionId"
  473. */
  474. function initFirefoxExtensionDetection(options) {
  475. if (options.desktopSharingFirefoxDisabled) {
  476. return;
  477. }
  478. if (firefoxExtInstalled === false || firefoxExtInstalled === true)
  479. return;
  480. if (!options.desktopSharingFirefoxExtId) {
  481. firefoxExtInstalled = false;
  482. return;
  483. }
  484. var img = document.createElement('img');
  485. img.onload = () => {
  486. logger.log("Detected firefox screen sharing extension.");
  487. firefoxExtInstalled = true;
  488. };
  489. img.onerror = () => {
  490. logger.log("Detected lack of firefox screen sharing extension.");
  491. firefoxExtInstalled = false;
  492. };
  493. // The jidesha extension exposes an empty image file under the url:
  494. // "chrome://EXT_ID/content/DOMAIN.png"
  495. // Where EXT_ID is the ID of the extension with "@" replaced by ".", and
  496. // DOMAIN is a domain whitelisted by the extension.
  497. var src = "chrome://" +
  498. (options.desktopSharingFirefoxExtId.replace('@', '.')) +
  499. "/content/" + document.location.hostname + ".png";
  500. img.setAttribute('src', src);
  501. }
  502. module.exports = ScreenObtainer;