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

ScreenObtainer.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. //TODO: remove chromeExtensionId (deprecated)
  205. (config.desktopSharingChromeExtId || config.chromeExtensionId),
  206. {
  207. getStream: true,
  208. //TODO: remove desktopSharingSources (deprecated).
  209. sources: (config.desktopSharingChromeSources ||
  210. config.desktopSharingSources)
  211. },
  212. function (response) {
  213. if (!response) {
  214. failCallback(chrome.runtime.lastError);
  215. return;
  216. }
  217. console.log("Response from extension: " + response);
  218. if (response.streamId) {
  219. APP.RTC.getUserMediaWithConstraints(
  220. ['desktop'],
  221. function (stream) {
  222. streamCallback(stream);
  223. },
  224. failCallback,
  225. null, null, null,
  226. response.streamId);
  227. } else {
  228. failCallback("Extension failed to get the stream");
  229. }
  230. }
  231. );
  232. }
  233. /**
  234. * Asks Chrome extension to call chooseDesktopMedia and gets chrome 'desktop'
  235. * stream for returned stream token.
  236. */
  237. function obtainScreenFromExtension(streamCallback, failCallback) {
  238. if (chromeExtInstalled) {
  239. doGetStreamFromExtension(streamCallback, failCallback);
  240. } else {
  241. if (chromeExtUpdateRequired) {
  242. alert(
  243. 'Jitsi Desktop Streamer requires update. ' +
  244. 'Changes will take effect after next Chrome restart.');
  245. }
  246. chrome.webstore.install(
  247. getWebStoreInstallUrl(),
  248. function (arg) {
  249. console.log("Extension installed successfully", arg);
  250. chromeExtInstalled = true;
  251. // We need to give a moment for the endpoint to become available
  252. window.setTimeout(function () {
  253. doGetStreamFromExtension(streamCallback, failCallback);
  254. }, 500);
  255. },
  256. function (arg) {
  257. console.log("Failed to install the extension", arg);
  258. failCallback(arg);
  259. APP.UI.messageHandler.showError("dialog.error",
  260. "dialog.failtoinstall");
  261. }
  262. );
  263. }
  264. }
  265. /**
  266. * Initializes <link rel=chrome-webstore-item /> with extension id set in
  267. * config.js to support inline installs. Host site must be selected as main
  268. * website of published extension.
  269. */
  270. function initInlineInstalls()
  271. {
  272. $("link[rel=chrome-webstore-item]").attr("href", getWebStoreInstallUrl());
  273. }
  274. function initChromeExtension() {
  275. // Initialize Chrome extension inline installs
  276. initInlineInstalls();
  277. // Check if extension is installed
  278. checkChromeExtInstalled(function (installed, updateRequired) {
  279. chromeExtInstalled = installed;
  280. chromeExtUpdateRequired = updateRequired;
  281. console.info(
  282. "Chrome extension installed: " + chromeExtInstalled +
  283. " updateRequired: " + chromeExtUpdateRequired);
  284. });
  285. }
  286. /**
  287. * Obtains a screen capture stream on Firefox.
  288. * @param callback
  289. * @param errorCallback
  290. */
  291. ScreenObtainer.prototype.obtainScreenOnFirefox =
  292. function (callback, errorCallback) {
  293. var self = this;
  294. var extensionRequired = false;
  295. if (config.desktopSharingFirefoxMaxVersionExtRequired === -1 ||
  296. (config.desktopSharingFirefoxMaxVersionExtRequired >= 0 &&
  297. RTCBrowserType.getFirefoxVersion() <=
  298. config.desktopSharingFirefoxMaxVersionExtRequired)) {
  299. extensionRequired = true;
  300. console.log("Jidesha extension required on firefox version " +
  301. RTCBrowserType.getFirefoxVersion());
  302. }
  303. if (!extensionRequired || firefoxExtInstalled === true) {
  304. obtainWebRTCScreen(callback, errorCallback);
  305. return;
  306. }
  307. if (reDetectFirefoxExtension) {
  308. reDetectFirefoxExtension = false;
  309. initFirefoxExtensionDetection();
  310. }
  311. // Give it some (more) time to initialize, and assume lack of extension if
  312. // it hasn't.
  313. if (firefoxExtInstalled === null) {
  314. window.setTimeout(
  315. function() {
  316. if (firefoxExtInstalled === null)
  317. firefoxExtInstalled = false;
  318. self.obtainScreenOnFirefox(callback, errorCallback);
  319. },
  320. 300
  321. );
  322. console.log("Waiting for detection of jidesha on firefox to finish.");
  323. return;
  324. }
  325. // We need an extension and it isn't installed.
  326. // Make sure we check for the extension when the user clicks again.
  327. firefoxExtInstalled = null;
  328. reDetectFirefoxExtension = true;
  329. // Prompt the user to install the extension
  330. this.eventEmitter.emit(DesktopSharingEventTypes.FIREFOX_EXTENSION_NEEDED,
  331. config.desktopSharingFirefoxExtensionURL);
  332. // Make sure desktopsharing knows that we failed, so that it doesn't get
  333. // stuck in 'switching' mode.
  334. errorCallback('Firefox extension required.');
  335. };
  336. /**
  337. * Starts the detection of an installed jidesha extension for firefox.
  338. */
  339. function initFirefoxExtensionDetection() {
  340. if (config.desktopSharingFirefoxDisabled) {
  341. return;
  342. }
  343. if (firefoxExtInstalled === false || firefoxExtInstalled === true)
  344. return;
  345. if (!config.desktopSharingFirefoxExtId) {
  346. firefoxExtInstalled = false;
  347. return;
  348. }
  349. var img = document.createElement('img');
  350. img.onload = function(){
  351. console.log("Detected firefox screen sharing extension.");
  352. firefoxExtInstalled = true;
  353. };
  354. img.onerror = function(){
  355. console.log("Detected lack of firefox screen sharing extension.");
  356. firefoxExtInstalled = false;
  357. };
  358. // The jidesha extension exposes an empty image file under the url:
  359. // "chrome://EXT_ID/content/DOMAIN.png"
  360. // Where EXT_ID is the ID of the extension with "@" replaced by ".", and
  361. // DOMAIN is a domain whitelisted by the extension.
  362. var src = "chrome://" +
  363. (config.desktopSharingFirefoxExtId.replace('@', '.')) +
  364. "/content/" + document.location.hostname + ".png";
  365. img.setAttribute('src', src);
  366. }
  367. module.exports = ScreenObtainer;