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

ScreenObtainer.js 14KB

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