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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /* global chrome, $, alert */
  2. import JitsiTrackError from '../../JitsiTrackError';
  3. import * as JitsiTrackErrors from '../../JitsiTrackErrors';
  4. import RTCBrowserType from './RTCBrowserType';
  5. const logger = require('jitsi-meet-logger').getLogger(__filename);
  6. const GlobalOnErrorHandler = require('../util/GlobalOnErrorHandler');
  7. /**
  8. * Indicates whether the Chrome desktop sharing extension is installed.
  9. * @type {boolean}
  10. */
  11. let chromeExtInstalled = false;
  12. /**
  13. * Indicates whether an update of the Chrome desktop sharing extension is
  14. * required.
  15. * @type {boolean}
  16. */
  17. let 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. let 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. let reDetectFirefoxExtension = false;
  30. let gumFunction = null;
  31. /**
  32. * The error returned by chrome when trying to start inline installation from
  33. * popup.
  34. */
  35. const CHROME_EXTENSION_POPUP_ERROR
  36. = 'Inline installs can not be initiated from pop-up windows.';
  37. /**
  38. * The error returned by chrome when trying to start inline installation from
  39. * iframe.
  40. */
  41. const CHROME_EXTENSION_IFRAME_ERROR
  42. = 'Chrome Web Store installations can only be started by the top frame.';
  43. /**
  44. * The error returned by chrome when trying to start inline installation
  45. * not from the "main" whitelisted site.
  46. * @type {string}
  47. */
  48. const CHROME_EXTENSION_INLINE_ERROR
  49. = 'Installs can only be initiated by one of'
  50. + ' the Chrome Web Store item\'s verified sites.';
  51. /**
  52. * The error returned by chrome when trying to start inline installation
  53. * with extension that doesn't support inline installation.
  54. *
  55. * @type {string}
  56. */
  57. const CHROME_EXTENSION_INLINE_NOT_SUPPORTED_ERROR
  58. = 'Inline installation is not supported for this item. '
  59. + 'The user will be redirected to the Chrome Web Store.';
  60. /**
  61. * The error message returned by chrome when the extension is installed.
  62. */
  63. const CHROME_NO_EXTENSION_ERROR_MSG // eslint-disable-line no-unused-vars
  64. = 'Could not establish connection. Receiving end does not exist.';
  65. /**
  66. * The error message returned by chrome when the extension install action needs
  67. * to be initiated by a user gesture.
  68. * @type {string}
  69. */
  70. const CHROME_USER_GESTURE_REQ_ERROR
  71. = 'Chrome Web Store installations can only be initated by a user gesture.';
  72. /**
  73. * Handles obtaining a stream from a screen capture on different browsers.
  74. */
  75. const ScreenObtainer = {
  76. /**
  77. * If not <tt>null</tt> it means that the initialization process is still in
  78. * progress. It is used to make desktop stream request wait and continue
  79. * after it's done.
  80. * {@type Promise|null}
  81. */
  82. intChromeExtPromise: null,
  83. obtainStream: null,
  84. /**
  85. * Initializes the function used to obtain a screen capture
  86. * (this.obtainStream).
  87. *
  88. * @param {object} options
  89. * @param {boolean} [options.disableDesktopSharing]
  90. * @param {boolean} [options.desktopSharingChromeDisabled]
  91. * @param {boolean} [options.desktopSharingChromeExtId]
  92. * @param {boolean} [options.desktopSharingFirefoxDisabled]
  93. * @param {boolean} [options.desktopSharingFirefoxExtId] (deprecated)
  94. * @param {Function} gum GUM method
  95. */
  96. init(options = {
  97. disableDesktopSharing: false,
  98. desktopSharingChromeDisabled: false,
  99. desktopSharingChromeExtId: null,
  100. desktopSharingFirefoxDisabled: false,
  101. desktopSharingFirefoxExtId: null
  102. }, gum) {
  103. // eslint-disable-next-line no-param-reassign
  104. this.options = options = options || {};
  105. gumFunction = gum;
  106. this.obtainStream
  107. = this.options.disableDesktopSharing
  108. ? null : this._createObtainStreamMethod(options);
  109. if (!this.obtainStream) {
  110. logger.info('Desktop sharing disabled');
  111. }
  112. },
  113. /**
  114. * Returns a method which will be used to obtain the screen sharing stream
  115. * (based on the browser type).
  116. *
  117. * @param {object} options passed from {@link init} - check description
  118. * there
  119. * @returns {Function}
  120. * @private
  121. */
  122. _createObtainStreamMethod(options) {
  123. if (RTCBrowserType.isNWJS()) {
  124. return (_, onSuccess, onFailure) => {
  125. window.JitsiMeetNW.obtainDesktopStream(
  126. onSuccess,
  127. (error, constraints) => {
  128. let jitsiError;
  129. // FIXME:
  130. // This is very very dirty fix for recognising that the
  131. // user have clicked the cancel button from the Desktop
  132. // sharing pick window. The proper solution would be to
  133. // detect this in the NWJS application by checking the
  134. // streamId === "". Even better solution would be to
  135. // stop calling GUM from the NWJS app and just pass the
  136. // streamId to lib-jitsi-meet. This way the desktop
  137. // sharing implementation for NWJS and chrome extension
  138. // will be the same and lib-jitsi-meet will be able to
  139. // control the constraints, check the streamId, etc.
  140. //
  141. // I cannot find documentation about "InvalidStateError"
  142. // but this is what we are receiving from GUM when the
  143. // streamId for the desktop sharing is "".
  144. if (error && error.name === 'InvalidStateError') {
  145. jitsiError = new JitsiTrackError(
  146. JitsiTrackErrors.CHROME_EXTENSION_USER_CANCELED
  147. );
  148. } else {
  149. jitsiError = new JitsiTrackError(
  150. error, constraints, [ 'desktop' ]);
  151. }
  152. (typeof onFailure === 'function')
  153. && onFailure(jitsiError);
  154. });
  155. };
  156. } else if (RTCBrowserType.isElectron()) {
  157. return this.obtainScreenOnElectron;
  158. } else if (RTCBrowserType.isTemasysPluginUsed()) {
  159. // XXX Don't require Temasys unless it's to be used because it
  160. // doesn't run on React Native, for example.
  161. const plugin
  162. = require('./adapter.screenshare').WebRTCPlugin.plugin;
  163. if (!plugin.HasScreensharingFeature) {
  164. logger.warn(
  165. 'Screensharing not supported by this plugin version');
  166. return null;
  167. } else if (!plugin.isScreensharingAvailable) {
  168. logger.warn(
  169. 'Screensharing not available with Temasys plugin on'
  170. + ' this site');
  171. return null;
  172. }
  173. logger.info('Using Temasys plugin for desktop sharing');
  174. return obtainWebRTCScreen;
  175. } else if (RTCBrowserType.isChrome()) {
  176. if (RTCBrowserType.getChromeVersion() < 34) {
  177. logger.info('Chrome extension not supported until ver 34');
  178. return null;
  179. } else if (options.desktopSharingChromeDisabled
  180. || options.desktopSharingChromeMethod === false
  181. || !options.desktopSharingChromeExtId) {
  182. // TODO: desktopSharingChromeMethod is deprecated, remove.
  183. return null;
  184. }
  185. logger.info('Using Chrome extension for desktop sharing');
  186. this.intChromeExtPromise
  187. = initChromeExtension(options).then(() => {
  188. this.intChromeExtPromise = null;
  189. });
  190. return this.obtainScreenFromExtension;
  191. } else if (RTCBrowserType.isFirefox()) {
  192. if (options.desktopSharingFirefoxDisabled) {
  193. return null;
  194. } else if (window.location.protocol === 'http:') {
  195. logger.log('Screen sharing is not supported over HTTP. '
  196. + 'Use of HTTPS is required.');
  197. return null;
  198. }
  199. initFirefoxExtensionDetection(options);
  200. return this.obtainScreenOnFirefox;
  201. }
  202. logger.log(
  203. 'Screen sharing not supported by the current browser: ',
  204. RTCBrowserType.getBrowserType(),
  205. RTCBrowserType.getBrowserName());
  206. return null;
  207. },
  208. /**
  209. * Checks whether obtaining a screen capture is supported in the current
  210. * environment.
  211. * @returns {boolean}
  212. */
  213. isSupported() {
  214. return this.obtainStream !== null;
  215. },
  216. /**
  217. * Obtains a screen capture stream on Firefox.
  218. * @param callback
  219. * @param errorCallback
  220. */
  221. obtainScreenOnFirefox(options, callback, errorCallback) {
  222. let extensionRequired = false;
  223. const { desktopSharingFirefoxMaxVersionExtRequired } = this.options;
  224. if (desktopSharingFirefoxMaxVersionExtRequired === -1
  225. || (desktopSharingFirefoxMaxVersionExtRequired >= 0
  226. && RTCBrowserType.getFirefoxVersion()
  227. <= desktopSharingFirefoxMaxVersionExtRequired)) {
  228. extensionRequired = true;
  229. logger.log(
  230. `Jidesha extension required on firefox version ${
  231. RTCBrowserType.getFirefoxVersion()}`);
  232. }
  233. if (!extensionRequired || firefoxExtInstalled === true) {
  234. obtainWebRTCScreen(options, callback, errorCallback);
  235. return;
  236. }
  237. if (reDetectFirefoxExtension) {
  238. reDetectFirefoxExtension = false;
  239. initFirefoxExtensionDetection(this.options);
  240. }
  241. // Give it some (more) time to initialize, and assume lack of
  242. // extension if it hasn't.
  243. if (firefoxExtInstalled === null) {
  244. window.setTimeout(
  245. () => {
  246. if (firefoxExtInstalled === null) {
  247. firefoxExtInstalled = false;
  248. }
  249. this.obtainScreenOnFirefox(callback, errorCallback);
  250. },
  251. 300);
  252. logger.log(
  253. 'Waiting for detection of jidesha on firefox to finish.');
  254. return;
  255. }
  256. // We need an extension and it isn't installed.
  257. // Make sure we check for the extension when the user clicks again.
  258. firefoxExtInstalled = null;
  259. reDetectFirefoxExtension = true;
  260. // Make sure desktopsharing knows that we failed, so that it doesn't get
  261. // stuck in 'switching' mode.
  262. errorCallback(
  263. new JitsiTrackError(JitsiTrackErrors.FIREFOX_EXTENSION_NEEDED));
  264. },
  265. /**
  266. * Obtains a screen capture stream on Electron.
  267. *
  268. * @param {Object} [options] - Screen sharing options.
  269. * @param {Array<string>} [options.desktopSharingSources] - Array with the
  270. * sources that have to be displayed in the desktop picker window ('screen',
  271. * 'window', etc.).
  272. * @param onSuccess - Success callback.
  273. * @param onFailure - Failure callback.
  274. */
  275. obtainScreenOnElectron(options = {}, onSuccess, onFailure) {
  276. if (window.JitsiMeetScreenObtainer
  277. && window.JitsiMeetScreenObtainer.openDesktopPicker) {
  278. window.JitsiMeetScreenObtainer.openDesktopPicker(
  279. {
  280. desktopSharingSources:
  281. options.desktopSharingSources
  282. || this.options.desktopSharingChromeSources
  283. },
  284. (streamId, streamType) =>
  285. onGetStreamResponse(
  286. {
  287. streamId,
  288. streamType
  289. },
  290. onSuccess,
  291. onFailure
  292. ),
  293. err => onFailure(new JitsiTrackError(
  294. JitsiTrackErrors.ELECTRON_DESKTOP_PICKER_ERROR,
  295. err
  296. ))
  297. );
  298. } else {
  299. onFailure(new JitsiTrackError(
  300. JitsiTrackErrors.ELECTRON_DESKTOP_PICKER_NOT_FOUND));
  301. }
  302. },
  303. /**
  304. * Asks Chrome extension to call chooseDesktopMedia and gets chrome
  305. * 'desktop' stream for returned stream token.
  306. */
  307. obtainScreenFromExtension(options, streamCallback, failCallback) {
  308. if (this.intChromeExtPromise !== null) {
  309. this.intChromeExtPromise.then(() => {
  310. this.obtainScreenFromExtension(
  311. options, streamCallback, failCallback);
  312. });
  313. return;
  314. }
  315. const {
  316. desktopSharingChromeExtId,
  317. desktopSharingChromeSources
  318. } = this.options;
  319. const gumOptions = {
  320. desktopSharingChromeExtId,
  321. desktopSharingChromeSources:
  322. options.desktopSharingSources
  323. || desktopSharingChromeSources
  324. };
  325. if (chromeExtInstalled) {
  326. doGetStreamFromExtension(
  327. gumOptions,
  328. streamCallback,
  329. failCallback);
  330. } else {
  331. if (chromeExtUpdateRequired) {
  332. /* eslint-disable no-alert */
  333. alert(
  334. 'Jitsi Desktop Streamer requires update. '
  335. + 'Changes will take effect after next Chrome restart.');
  336. /* eslint-enable no-alert */
  337. }
  338. try {
  339. chrome.webstore.install(
  340. getWebStoreInstallUrl(this.options),
  341. arg => {
  342. logger.log('Extension installed successfully', arg);
  343. chromeExtInstalled = true;
  344. // We need to give a moment to the endpoint to become
  345. // available.
  346. waitForExtensionAfterInstall(this.options, 200, 10)
  347. .then(() => {
  348. doGetStreamFromExtension(
  349. gumOptions,
  350. streamCallback,
  351. failCallback);
  352. })
  353. .catch(() => {
  354. this.handleExtensionInstallationError(options,
  355. streamCallback, failCallback);
  356. });
  357. },
  358. this.handleExtensionInstallationError.bind(this,
  359. options, streamCallback, failCallback)
  360. );
  361. } catch (e) {
  362. this.handleExtensionInstallationError(options, streamCallback,
  363. failCallback, e);
  364. }
  365. }
  366. },
  367. /* eslint-disable max-params */
  368. handleExtensionInstallationError(options, streamCallback, failCallback, e) {
  369. const webStoreInstallUrl = getWebStoreInstallUrl(this.options);
  370. if ((CHROME_EXTENSION_POPUP_ERROR === e
  371. || CHROME_EXTENSION_IFRAME_ERROR === e
  372. || CHROME_EXTENSION_INLINE_ERROR === e
  373. || CHROME_EXTENSION_INLINE_NOT_SUPPORTED_ERROR === e)
  374. && options.interval > 0
  375. && typeof options.checkAgain === 'function'
  376. && typeof options.listener === 'function') {
  377. options.listener('waitingForExtension', webStoreInstallUrl);
  378. this.checkForChromeExtensionOnInterval(options, streamCallback,
  379. failCallback, e);
  380. return;
  381. }
  382. const msg
  383. = `Failed to install the extension from ${webStoreInstallUrl}`;
  384. logger.log(msg, e);
  385. const error
  386. = e === CHROME_USER_GESTURE_REQ_ERROR
  387. ? JitsiTrackErrors.CHROME_EXTENSION_USER_GESTURE_REQUIRED
  388. : JitsiTrackErrors.CHROME_EXTENSION_INSTALLATION_ERROR;
  389. failCallback(new JitsiTrackError(error, msg));
  390. },
  391. /* eslint-enable max-params */
  392. checkForChromeExtensionOnInterval(options, streamCallback, failCallback) {
  393. if (options.checkAgain() === false) {
  394. failCallback(new JitsiTrackError(
  395. JitsiTrackErrors.CHROME_EXTENSION_INSTALLATION_ERROR));
  396. return;
  397. }
  398. waitForExtensionAfterInstall(this.options, options.interval, 1)
  399. .then(() => {
  400. chromeExtInstalled = true;
  401. options.listener('extensionFound');
  402. this.obtainScreenFromExtension(options,
  403. streamCallback, failCallback);
  404. })
  405. .catch(() => {
  406. this.checkForChromeExtensionOnInterval(options,
  407. streamCallback, failCallback);
  408. });
  409. }
  410. };
  411. /**
  412. * Obtains a desktop stream using getUserMedia.
  413. * For this to work on Chrome, the
  414. * 'chrome://flags/#enable-usermedia-screen-capture' flag must be enabled.
  415. *
  416. * On firefox, the document's domain must be white-listed in the
  417. * 'media.getusermedia.screensharing.allowed_domains' preference in
  418. * 'about:config'.
  419. */
  420. function obtainWebRTCScreen(options, streamCallback, failCallback) {
  421. gumFunction(
  422. [ 'screen' ],
  423. stream => streamCallback({ stream }),
  424. failCallback
  425. );
  426. }
  427. /**
  428. * Constructs inline install URL for Chrome desktop streaming extension.
  429. * The 'chromeExtensionId' must be defined in options parameter.
  430. * @param options supports "desktopSharingChromeExtId"
  431. * @returns {string}
  432. */
  433. function getWebStoreInstallUrl(options) {
  434. return (
  435. `https://chrome.google.com/webstore/detail/${
  436. options.desktopSharingChromeExtId}`);
  437. }
  438. /**
  439. * Checks whether an update of the Chrome extension is required.
  440. * @param minVersion minimal required version
  441. * @param extVersion current extension version
  442. * @returns {boolean}
  443. */
  444. function isUpdateRequired(minVersion, extVersion) {
  445. try {
  446. const s1 = minVersion.split('.');
  447. const s2 = extVersion.split('.');
  448. const len = Math.max(s1.length, s2.length);
  449. for (let i = 0; i < len; i++) {
  450. let n1 = 0,
  451. n2 = 0;
  452. if (i < s1.length) {
  453. n1 = parseInt(s1[i], 10);
  454. }
  455. if (i < s2.length) {
  456. n2 = parseInt(s2[i], 10);
  457. }
  458. if (isNaN(n1) || isNaN(n2)) {
  459. return true;
  460. } else if (n1 !== n2) {
  461. return n1 > n2;
  462. }
  463. }
  464. // will happen if both versions have identical numbers in
  465. // their components (even if one of them is longer, has more components)
  466. return false;
  467. } catch (e) {
  468. GlobalOnErrorHandler.callErrorHandler(e);
  469. logger.error('Failed to parse extension version', e);
  470. return true;
  471. }
  472. }
  473. /**
  474. *
  475. * @param callback
  476. * @param options
  477. */
  478. function checkChromeExtInstalled(callback, options) {
  479. if (typeof chrome === 'undefined' || !chrome || !chrome.runtime) {
  480. // No API, so no extension for sure
  481. callback(false, false);
  482. return;
  483. }
  484. chrome.runtime.sendMessage(
  485. options.desktopSharingChromeExtId,
  486. { getVersion: true },
  487. response => {
  488. if (!response || !response.version) {
  489. // Communication failure - assume that no endpoint exists
  490. logger.warn(
  491. 'Extension not installed?: ', chrome.runtime.lastError);
  492. callback(false, false);
  493. return;
  494. }
  495. // Check installed extension version
  496. const extVersion = response.version;
  497. logger.log(`Extension version is: ${extVersion}`);
  498. const updateRequired
  499. = isUpdateRequired(
  500. options.desktopSharingChromeMinExtVersion,
  501. extVersion);
  502. callback(!updateRequired, updateRequired);
  503. }
  504. );
  505. }
  506. /**
  507. *
  508. * @param options
  509. * @param streamCallback
  510. * @param failCallback
  511. */
  512. function doGetStreamFromExtension(options, streamCallback, failCallback) {
  513. // Sends 'getStream' msg to the extension.
  514. // Extension id must be defined in the config.
  515. chrome.runtime.sendMessage(
  516. options.desktopSharingChromeExtId,
  517. {
  518. getStream: true,
  519. sources: options.desktopSharingChromeSources
  520. },
  521. response => {
  522. if (!response) {
  523. // possibly re-wraping error message to make code consistent
  524. const lastError = chrome.runtime.lastError;
  525. failCallback(lastError instanceof Error
  526. ? lastError
  527. : new JitsiTrackError(
  528. JitsiTrackErrors.CHROME_EXTENSION_GENERIC_ERROR,
  529. lastError));
  530. return;
  531. }
  532. logger.log('Response from extension: ', response);
  533. onGetStreamResponse(response, streamCallback, failCallback);
  534. }
  535. );
  536. }
  537. /**
  538. * Initializes <link rel=chrome-webstore-item /> with extension id set in
  539. * config.js to support inline installs. Host site must be selected as main
  540. * website of published extension.
  541. * @param options supports "desktopSharingChromeExtId"
  542. */
  543. function initInlineInstalls(options) {
  544. if ($('link[rel=chrome-webstore-item]').length === 0) {
  545. $('head').append('<link rel="chrome-webstore-item">');
  546. }
  547. $('link[rel=chrome-webstore-item]').attr('href',
  548. getWebStoreInstallUrl(options));
  549. }
  550. /**
  551. *
  552. * @param options
  553. *
  554. * @return {Promise} - a Promise resolved once the initialization process is
  555. * finished.
  556. */
  557. function initChromeExtension(options) {
  558. // Initialize Chrome extension inline installs
  559. initInlineInstalls(options);
  560. return new Promise(resolve => {
  561. // Check if extension is installed
  562. checkChromeExtInstalled((installed, updateRequired) => {
  563. chromeExtInstalled = installed;
  564. chromeExtUpdateRequired = updateRequired;
  565. logger.info(
  566. `Chrome extension installed: ${
  567. chromeExtInstalled} updateRequired: ${
  568. chromeExtUpdateRequired}`);
  569. resolve();
  570. }, options);
  571. });
  572. }
  573. /**
  574. * Checks "retries" times on every "waitInterval"ms whether the ext is alive.
  575. * @param {Object} options the options passed to ScreanObtainer.obtainStream
  576. * @param {int} waitInterval the number of ms between retries
  577. * @param {int} retries the number of retries
  578. * @returns {Promise} returns promise that will be resolved when the extension
  579. * is alive and rejected if the extension is not alive even after "retries"
  580. * checks
  581. */
  582. function waitForExtensionAfterInstall(options, waitInterval, retries) {
  583. if (retries === 0) {
  584. return Promise.reject();
  585. }
  586. return new Promise((resolve, reject) => {
  587. let currentRetries = retries;
  588. const interval = window.setInterval(() => {
  589. checkChromeExtInstalled(installed => {
  590. if (installed) {
  591. window.clearInterval(interval);
  592. resolve();
  593. } else {
  594. currentRetries--;
  595. if (currentRetries === 0) {
  596. reject();
  597. window.clearInterval(interval);
  598. }
  599. }
  600. }, options);
  601. }, waitInterval);
  602. });
  603. }
  604. /**
  605. * Handles response from external application / extension and calls GUM to
  606. * receive the desktop streams or reports error.
  607. * @param {object} response
  608. * @param {string} response.streamId - the streamId for the desktop stream
  609. * @param {string} response.error - error to be reported.
  610. * @param {Function} onSuccess - callback for success.
  611. * @param {Function} onFailure - callback for failure.
  612. */
  613. function onGetStreamResponse(
  614. { streamId, streamType, error },
  615. onSuccess,
  616. onFailure) {
  617. if (streamId) {
  618. gumFunction(
  619. [ 'desktop' ],
  620. stream => onSuccess({
  621. stream,
  622. sourceId: streamId,
  623. sourceType: streamType
  624. }),
  625. onFailure,
  626. { desktopStream: streamId });
  627. } else {
  628. // As noted in Chrome Desktop Capture API:
  629. // If user didn't select any source (i.e. canceled the prompt)
  630. // then the callback is called with an empty streamId.
  631. if (streamId === '') {
  632. onFailure(new JitsiTrackError(
  633. JitsiTrackErrors.CHROME_EXTENSION_USER_CANCELED));
  634. return;
  635. }
  636. onFailure(new JitsiTrackError(
  637. JitsiTrackErrors.CHROME_EXTENSION_GENERIC_ERROR,
  638. error));
  639. }
  640. }
  641. /**
  642. * Starts the detection of an installed jidesha extension for firefox.
  643. * @param options supports "desktopSharingFirefoxDisabled",
  644. * "desktopSharingFirefoxExtId"
  645. */
  646. function initFirefoxExtensionDetection(options) {
  647. if (options.desktopSharingFirefoxDisabled) {
  648. return;
  649. }
  650. if (firefoxExtInstalled === false || firefoxExtInstalled === true) {
  651. return;
  652. }
  653. if (!options.desktopSharingFirefoxExtId) {
  654. firefoxExtInstalled = false;
  655. return;
  656. }
  657. const img = document.createElement('img');
  658. img.onload = () => {
  659. logger.log('Detected firefox screen sharing extension.');
  660. firefoxExtInstalled = true;
  661. };
  662. img.onerror = () => {
  663. logger.log('Detected lack of firefox screen sharing extension.');
  664. firefoxExtInstalled = false;
  665. };
  666. // The jidesha extension exposes an empty image file under the url:
  667. // "chrome://EXT_ID/content/DOMAIN.png"
  668. // Where EXT_ID is the ID of the extension with "@" replaced by ".", and
  669. // DOMAIN is a domain whitelisted by the extension.
  670. const extId = options.desktopSharingFirefoxExtId.replace('@', '.');
  671. const domain = document.location.hostname;
  672. const src = `chrome://${extId}/content/${domain}.png`;
  673. img.setAttribute('src', src);
  674. }
  675. export default ScreenObtainer;