Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ScreenObtainer.js 16KB

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