Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ScreenObtainer.js 16KB

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