modified lib-jitsi-meet dev repo
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 14KB

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