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 14KB

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