Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ScreenObtainer.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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({
  156. type: "jitsiError",
  157. errorObject: JitsiTrackErrors.FIREFOX_EXTENSION_NEEDED
  158. });
  159. },
  160. /**
  161. * Asks Chrome extension to call chooseDesktopMedia and gets chrome
  162. * 'desktop' stream for returned stream token.
  163. */
  164. obtainScreenFromExtension: function (streamCallback, failCallback) {
  165. var self = this;
  166. if (chromeExtInstalled) {
  167. doGetStreamFromExtension(this.options, streamCallback,
  168. failCallback);
  169. } else {
  170. if (chromeExtUpdateRequired) {
  171. alert(
  172. 'Jitsi Desktop Streamer requires update. ' +
  173. 'Changes will take effect after next Chrome restart.');
  174. }
  175. try {
  176. chrome.webstore.install(
  177. getWebStoreInstallUrl(this.options),
  178. function (arg) {
  179. logger.log("Extension installed successfully", arg);
  180. chromeExtInstalled = true;
  181. // We need to give a moment for the endpoint to become
  182. // available
  183. window.setTimeout(function () {
  184. doGetStreamFromExtension(self.options,
  185. streamCallback, failCallback);
  186. }, 500);
  187. },
  188. function (arg) {
  189. logger.log("Failed to install the extension from:"
  190. + getWebStoreInstallUrl(self.options), arg);
  191. failCallback({
  192. type: "jitsiError",
  193. errorObject: JitsiTrackErrors
  194. .CHROME_EXTENSION_INSTALLATION_ERROR
  195. });
  196. }
  197. );
  198. } catch(e) {
  199. logger.log("Failed to install the extension from:"
  200. + self.getWebStoreInstallUrl(this.options), arg);
  201. failCallback({
  202. type: "jitsiError",
  203. errorObject:
  204. JitsiTrackErrors.CHROME_EXTENSION_INSTALLATION_ERROR
  205. });
  206. }
  207. }
  208. }
  209. };
  210. /**
  211. * Obtains a desktop stream using getUserMedia.
  212. * For this to work on Chrome, the
  213. * 'chrome://flags/#enable-usermedia-screen-capture' flag must be enabled.
  214. *
  215. * On firefox, the document's domain must be white-listed in the
  216. * 'media.getusermedia.screensharing.allowed_domains' preference in
  217. * 'about:config'.
  218. */
  219. function obtainWebRTCScreen(streamCallback, failCallback) {
  220. GUM(
  221. ['screen'],
  222. streamCallback,
  223. failCallback
  224. );
  225. }
  226. /**
  227. * Constructs inline install URL for Chrome desktop streaming extension.
  228. * The 'chromeExtensionId' must be defined in options parameter.
  229. * @param options supports "desktopSharingChromeExtId" and "chromeExtensionId"
  230. * @returns {string}
  231. */
  232. function getWebStoreInstallUrl(options)
  233. {
  234. //TODO remove chromeExtensionId (deprecated)
  235. return "https://chrome.google.com/webstore/detail/" +
  236. (options.desktopSharingChromeExtId || options.chromeExtensionId);
  237. }
  238. /**
  239. * Checks whether an update of the Chrome extension is required.
  240. * @param minVersion minimal required version
  241. * @param extVersion current extension version
  242. * @returns {boolean}
  243. */
  244. function isUpdateRequired(minVersion, extVersion) {
  245. try {
  246. var s1 = minVersion.split('.');
  247. var s2 = extVersion.split('.');
  248. var len = Math.max(s1.length, s2.length);
  249. for (var i = 0; i < len; i++) {
  250. var n1 = 0,
  251. n2 = 0;
  252. if (i < s1.length)
  253. n1 = parseInt(s1[i]);
  254. if (i < s2.length)
  255. n2 = parseInt(s2[i]);
  256. if (isNaN(n1) || isNaN(n2)) {
  257. return true;
  258. } else if (n1 !== n2) {
  259. return n1 > n2;
  260. }
  261. }
  262. // will happen if both versions have identical numbers in
  263. // their components (even if one of them is longer, has more components)
  264. return false;
  265. }
  266. catch (e) {
  267. logger.error("Failed to parse extension version", e);
  268. return true;
  269. }
  270. }
  271. function checkChromeExtInstalled(callback, options) {
  272. if (!chrome || !chrome.runtime) {
  273. // No API, so no extension for sure
  274. callback(false, false);
  275. return;
  276. }
  277. chrome.runtime.sendMessage(
  278. //TODO: remove chromeExtensionId (deprecated)
  279. (options.desktopSharingChromeExtId || options.chromeExtensionId),
  280. { getVersion: true },
  281. function (response) {
  282. if (!response || !response.version) {
  283. // Communication failure - assume that no endpoint exists
  284. logger.warn(
  285. "Extension not installed?: ", chrome.runtime.lastError);
  286. callback(false, false);
  287. return;
  288. }
  289. // Check installed extension version
  290. var extVersion = response.version;
  291. logger.log('Extension version is: ' + extVersion);
  292. //TODO: remove minChromeExtVersion (deprecated)
  293. var updateRequired
  294. = isUpdateRequired(
  295. (options.desktopSharingChromeMinExtVersion ||
  296. options.minChromeExtVersion),
  297. extVersion);
  298. callback(!updateRequired, updateRequired);
  299. }
  300. );
  301. }
  302. function doGetStreamFromExtension(options, streamCallback, failCallback) {
  303. // Sends 'getStream' msg to the extension.
  304. // Extension id must be defined in the config.
  305. chrome.runtime.sendMessage(
  306. //TODO: remove chromeExtensionId (deprecated)
  307. (options.desktopSharingChromeExtId || options.chromeExtensionId),
  308. {
  309. getStream: true,
  310. //TODO: remove desktopSharingSources (deprecated).
  311. sources: (options.desktopSharingChromeSources ||
  312. options.desktopSharingSources)
  313. },
  314. function (response) {
  315. if (!response) {
  316. failCallback(chrome.runtime.lastError);
  317. return;
  318. }
  319. logger.log("Response from extension: ", response);
  320. if (response.streamId) {
  321. GUM(
  322. ['desktop'],
  323. function (stream) {
  324. streamCallback(stream);
  325. },
  326. failCallback,
  327. {desktopStream: response.streamId});
  328. } else {
  329. // As noted in Chrome Desktop Capture API:
  330. // If user didn't select any source (i.e. canceled the prompt)
  331. // then the callback is called with an empty streamId.
  332. if(response.streamId === "")
  333. {
  334. failCallback({
  335. type: "jitsiError",
  336. errorObject:
  337. JitsiTrackErrors.CHROME_EXTENSION_USER_CANCELED
  338. });
  339. return;
  340. }
  341. failCallback("Extension failed to get the stream");
  342. }
  343. }
  344. );
  345. }
  346. /**
  347. * Initializes <link rel=chrome-webstore-item /> with extension id set in
  348. * config.js to support inline installs. Host site must be selected as main
  349. * website of published extension.
  350. * @param options supports "desktopSharingChromeExtId" and "chromeExtensionId"
  351. */
  352. function initInlineInstalls(options)
  353. {
  354. if($("link[rel=chrome-webstore-item]").length === 0) {
  355. $("head").append("<link rel=\"chrome-webstore-item\">");
  356. }
  357. $("link[rel=chrome-webstore-item]").attr("href",
  358. getWebStoreInstallUrl(options));
  359. }
  360. function initChromeExtension(options) {
  361. // Initialize Chrome extension inline installs
  362. initInlineInstalls(options);
  363. // Check if extension is installed
  364. checkChromeExtInstalled(function (installed, updateRequired) {
  365. chromeExtInstalled = installed;
  366. chromeExtUpdateRequired = updateRequired;
  367. logger.info(
  368. "Chrome extension installed: " + chromeExtInstalled +
  369. " updateRequired: " + chromeExtUpdateRequired);
  370. }, options);
  371. }
  372. /**
  373. * Starts the detection of an installed jidesha extension for firefox.
  374. * @param options supports "desktopSharingFirefoxDisabled",
  375. * "desktopSharingFirefoxExtId" and "chromeExtensionId"
  376. */
  377. function initFirefoxExtensionDetection(options) {
  378. if (options.desktopSharingFirefoxDisabled) {
  379. return;
  380. }
  381. if (firefoxExtInstalled === false || firefoxExtInstalled === true)
  382. return;
  383. if (!options.desktopSharingFirefoxExtId) {
  384. firefoxExtInstalled = false;
  385. return;
  386. }
  387. var img = document.createElement('img');
  388. img.onload = function(){
  389. logger.log("Detected firefox screen sharing extension.");
  390. firefoxExtInstalled = true;
  391. };
  392. img.onerror = function(){
  393. logger.log("Detected lack of firefox screen sharing extension.");
  394. firefoxExtInstalled = false;
  395. };
  396. // The jidesha extension exposes an empty image file under the url:
  397. // "chrome://EXT_ID/content/DOMAIN.png"
  398. // Where EXT_ID is the ID of the extension with "@" replaced by ".", and
  399. // DOMAIN is a domain whitelisted by the extension.
  400. var src = "chrome://" +
  401. (options.desktopSharingFirefoxExtId.replace('@', '.')) +
  402. "/content/" + document.location.hostname + ".png";
  403. img.setAttribute('src', src);
  404. }
  405. module.exports = ScreenObtainer;