You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScreenObtainer.js 13KB

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