Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ScreenObtainer.js 23KB

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