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

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