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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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(getWebStoreInstallUrl(this.options));
  222. this.checkForChromeExtensionOnInterval(options,
  223. streamCallback, failCallback, e);
  224. return;
  225. }
  226. var msg = "Failed to install the extension from "
  227. + getWebStoreInstallUrl(this.options);
  228. logger.log(msg, e);
  229. failCallback(new JitsiTrackError(
  230. JitsiTrackErrors.CHROME_EXTENSION_INSTALLATION_ERROR,
  231. msg
  232. ));
  233. },
  234. checkForChromeExtensionOnInterval: function (options,
  235. streamCallback, failCallback, e) {
  236. if ( CHROME_EXTENSION_POPUP_ERROR !== e &&
  237. (e.name !== JitsiTrackErrors.CHROME_EXTENSION_GENERIC_ERROR ||
  238. !e.message ||
  239. e.message.message !== CHROME_NO_EXTENSION_ERROR_MSG)) {
  240. this.handleExtensionInstallationError(null, streamCallback,
  241. failCallback, e);
  242. return;
  243. }
  244. if (options.checkAgain() === false) {
  245. failCallback(new JitsiTrackError(
  246. JitsiTrackErrors.CHROME_EXTENSION_INSTALLATION_ERROR));
  247. return;
  248. }
  249. var args = arguments;
  250. var self = this;
  251. window.setTimeout(function () {
  252. doGetStreamFromExtension(self.options,
  253. function () {
  254. chromeExtInstalled = true;
  255. streamCallback.apply(null, arguments);
  256. }, self.checkForChromeExtensionOnInterval.bind(
  257. self, options, streamCallback, failCallback));
  258. }, options.interval);
  259. }
  260. };
  261. /**
  262. * Obtains a desktop stream using getUserMedia.
  263. * For this to work on Chrome, the
  264. * 'chrome://flags/#enable-usermedia-screen-capture' flag must be enabled.
  265. *
  266. * On firefox, the document's domain must be white-listed in the
  267. * 'media.getusermedia.screensharing.allowed_domains' preference in
  268. * 'about:config'.
  269. */
  270. function obtainWebRTCScreen(options, streamCallback, failCallback) {
  271. GUM(
  272. ['screen'],
  273. streamCallback,
  274. failCallback
  275. );
  276. }
  277. /**
  278. * Constructs inline install URL for Chrome desktop streaming extension.
  279. * The 'chromeExtensionId' must be defined in options parameter.
  280. * @param options supports "desktopSharingChromeExtId" and "chromeExtensionId"
  281. * @returns {string}
  282. */
  283. function getWebStoreInstallUrl(options)
  284. {
  285. //TODO remove chromeExtensionId (deprecated)
  286. return "https://chrome.google.com/webstore/detail/" +
  287. (options.desktopSharingChromeExtId || options.chromeExtensionId);
  288. }
  289. /**
  290. * Checks whether an update of the Chrome extension is required.
  291. * @param minVersion minimal required version
  292. * @param extVersion current extension version
  293. * @returns {boolean}
  294. */
  295. function isUpdateRequired(minVersion, extVersion) {
  296. try {
  297. var s1 = minVersion.split('.');
  298. var s2 = extVersion.split('.');
  299. var len = Math.max(s1.length, s2.length);
  300. for (var i = 0; i < len; i++) {
  301. var n1 = 0,
  302. n2 = 0;
  303. if (i < s1.length)
  304. n1 = parseInt(s1[i]);
  305. if (i < s2.length)
  306. n2 = parseInt(s2[i]);
  307. if (isNaN(n1) || isNaN(n2)) {
  308. return true;
  309. } else if (n1 !== n2) {
  310. return n1 > n2;
  311. }
  312. }
  313. // will happen if both versions have identical numbers in
  314. // their components (even if one of them is longer, has more components)
  315. return false;
  316. }
  317. catch (e) {
  318. GlobalOnErrorHandler.callErrorHandler(e);
  319. logger.error("Failed to parse extension version", e);
  320. return true;
  321. }
  322. }
  323. function checkChromeExtInstalled(callback, options) {
  324. if (typeof chrome === "undefined" || !chrome || !chrome.runtime) {
  325. // No API, so no extension for sure
  326. callback(false, false);
  327. return;
  328. }
  329. chrome.runtime.sendMessage(
  330. //TODO: remove chromeExtensionId (deprecated)
  331. (options.desktopSharingChromeExtId || options.chromeExtensionId),
  332. { getVersion: true },
  333. function (response) {
  334. if (!response || !response.version) {
  335. // Communication failure - assume that no endpoint exists
  336. logger.warn(
  337. "Extension not installed?: ", chrome.runtime.lastError);
  338. callback(false, false);
  339. return;
  340. }
  341. // Check installed extension version
  342. var extVersion = response.version;
  343. logger.log('Extension version is: ' + extVersion);
  344. //TODO: remove minChromeExtVersion (deprecated)
  345. var updateRequired
  346. = isUpdateRequired(
  347. (options.desktopSharingChromeMinExtVersion ||
  348. options.minChromeExtVersion),
  349. extVersion);
  350. callback(!updateRequired, updateRequired);
  351. }
  352. );
  353. }
  354. function doGetStreamFromExtension(options, streamCallback, failCallback) {
  355. // Sends 'getStream' msg to the extension.
  356. // Extension id must be defined in the config.
  357. chrome.runtime.sendMessage(
  358. //TODO: remove chromeExtensionId (deprecated)
  359. (options.desktopSharingChromeExtId || options.chromeExtensionId),
  360. {
  361. getStream: true,
  362. //TODO: remove desktopSharingSources (deprecated).
  363. sources: (options.desktopSharingChromeSources ||
  364. options.desktopSharingSources)
  365. },
  366. function (response) {
  367. if (!response) {
  368. // possibly re-wraping error message to make code consistent
  369. var lastError = chrome.runtime.lastError;
  370. failCallback(lastError instanceof Error
  371. ? lastError
  372. : new JitsiTrackError(
  373. JitsiTrackErrors.CHROME_EXTENSION_GENERIC_ERROR,
  374. lastError));
  375. return;
  376. }
  377. logger.log("Response from extension: ", response);
  378. if (response.streamId) {
  379. GUM(
  380. ['desktop'],
  381. function (stream) {
  382. streamCallback(stream);
  383. },
  384. failCallback,
  385. {desktopStream: response.streamId});
  386. } else {
  387. // As noted in Chrome Desktop Capture API:
  388. // If user didn't select any source (i.e. canceled the prompt)
  389. // then the callback is called with an empty streamId.
  390. if(response.streamId === "")
  391. {
  392. failCallback(new JitsiTrackError(
  393. JitsiTrackErrors.CHROME_EXTENSION_USER_CANCELED));
  394. return;
  395. }
  396. failCallback(new JitsiTrackError(
  397. JitsiTrackErrors.CHROME_EXTENSION_GENERIC_ERROR,
  398. response.error));
  399. }
  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(function (installed, updateRequired) {
  422. chromeExtInstalled = installed;
  423. chromeExtUpdateRequired = updateRequired;
  424. logger.info(
  425. "Chrome extension installed: " + chromeExtInstalled +
  426. " updateRequired: " + chromeExtUpdateRequired);
  427. }, options);
  428. }
  429. /**
  430. * Starts the detection of an installed jidesha extension for firefox.
  431. * @param options supports "desktopSharingFirefoxDisabled",
  432. * "desktopSharingFirefoxExtId" and "chromeExtensionId"
  433. */
  434. function initFirefoxExtensionDetection(options) {
  435. if (options.desktopSharingFirefoxDisabled) {
  436. return;
  437. }
  438. if (firefoxExtInstalled === false || firefoxExtInstalled === true)
  439. return;
  440. if (!options.desktopSharingFirefoxExtId) {
  441. firefoxExtInstalled = false;
  442. return;
  443. }
  444. var img = document.createElement('img');
  445. img.onload = function(){
  446. logger.log("Detected firefox screen sharing extension.");
  447. firefoxExtInstalled = true;
  448. };
  449. img.onerror = function(){
  450. logger.log("Detected lack of firefox screen sharing extension.");
  451. firefoxExtInstalled = false;
  452. };
  453. // The jidesha extension exposes an empty image file under the url:
  454. // "chrome://EXT_ID/content/DOMAIN.png"
  455. // Where EXT_ID is the ID of the extension with "@" replaced by ".", and
  456. // DOMAIN is a domain whitelisted by the extension.
  457. var src = "chrome://" +
  458. (options.desktopSharingFirefoxExtId.replace('@', '.')) +
  459. "/content/" + document.location.hostname + ".png";
  460. img.setAttribute('src', src);
  461. }
  462. module.exports = ScreenObtainer;