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

ScreenObtainer.js 15KB

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