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

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