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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import JitsiTrackError from '../../JitsiTrackError';
  2. import * as JitsiTrackErrors from '../../JitsiTrackErrors';
  3. import browser from '../browser';
  4. const logger = require('jitsi-meet-logger').getLogger(__filename);
  5. let gumFunction = null;
  6. /**
  7. * Handles obtaining a stream from a screen capture on different browsers.
  8. */
  9. const ScreenObtainer = {
  10. /**
  11. * If not <tt>null</tt> it means that the initialization process is still in
  12. * progress. It is used to make desktop stream request wait and continue
  13. * after it's done.
  14. * {@type Promise|null}
  15. */
  16. obtainStream: null,
  17. /**
  18. * Initializes the function used to obtain a screen capture
  19. * (this.obtainStream).
  20. *
  21. * @param {object} options
  22. * @param {Function} gum GUM method
  23. */
  24. init(options = {}, gum) {
  25. this.options = options;
  26. gumFunction = gum;
  27. this.obtainStream = this._createObtainStreamMethod();
  28. if (!this.obtainStream) {
  29. logger.info('Desktop sharing disabled');
  30. }
  31. },
  32. /**
  33. * Returns a method which will be used to obtain the screen sharing stream
  34. * (based on the browser type).
  35. *
  36. * @returns {Function}
  37. * @private
  38. */
  39. _createObtainStreamMethod() {
  40. if (browser.isNWJS()) {
  41. return (_, onSuccess, onFailure) => {
  42. window.JitsiMeetNW.obtainDesktopStream(
  43. onSuccess,
  44. (error, constraints) => {
  45. let jitsiError;
  46. // FIXME:
  47. // This is very very dirty fix for recognising that the
  48. // user have clicked the cancel button from the Desktop
  49. // sharing pick window. The proper solution would be to
  50. // detect this in the NWJS application by checking the
  51. // streamId === "". Even better solution would be to
  52. // stop calling GUM from the NWJS app and just pass the
  53. // streamId to lib-jitsi-meet. This way the desktop
  54. // sharing implementation for NWJS and chrome extension
  55. // will be the same and lib-jitsi-meet will be able to
  56. // control the constraints, check the streamId, etc.
  57. //
  58. // I cannot find documentation about "InvalidStateError"
  59. // but this is what we are receiving from GUM when the
  60. // streamId for the desktop sharing is "".
  61. if (error && error.name === 'InvalidStateError') {
  62. jitsiError = new JitsiTrackError(
  63. JitsiTrackErrors.SCREENSHARING_USER_CANCELED
  64. );
  65. } else {
  66. jitsiError = new JitsiTrackError(
  67. error, constraints, [ 'desktop' ]);
  68. }
  69. (typeof onFailure === 'function')
  70. && onFailure(jitsiError);
  71. });
  72. };
  73. } else if (browser.isElectron()) {
  74. return this.obtainScreenOnElectron;
  75. } else if (browser.supportsGetDisplayMedia()) {
  76. return this.obtainScreenFromGetDisplayMedia;
  77. }
  78. logger.log('Screen sharing not supported on ', browser.getName());
  79. return null;
  80. },
  81. /**
  82. * Checks whether obtaining a screen capture is supported in the current
  83. * environment.
  84. * @returns {boolean}
  85. */
  86. isSupported() {
  87. return this.obtainStream !== null;
  88. },
  89. /**
  90. * Obtains a screen capture stream on Electron.
  91. *
  92. * @param {Object} [options] - Screen sharing options.
  93. * @param {Array<string>} [options.desktopSharingSources] - Array with the
  94. * sources that have to be displayed in the desktop picker window ('screen',
  95. * 'window', etc.).
  96. * @param onSuccess - Success callback.
  97. * @param onFailure - Failure callback.
  98. */
  99. obtainScreenOnElectron(options = {}, onSuccess, onFailure) {
  100. if (window.JitsiMeetScreenObtainer
  101. && window.JitsiMeetScreenObtainer.openDesktopPicker) {
  102. const { desktopSharingSources, gumOptions } = options;
  103. window.JitsiMeetScreenObtainer.openDesktopPicker(
  104. {
  105. desktopSharingSources: desktopSharingSources
  106. || this.options.desktopSharingChromeSources
  107. },
  108. (streamId, streamType, screenShareAudio = false) =>
  109. onGetStreamResponse(
  110. {
  111. response: {
  112. streamId,
  113. streamType,
  114. screenShareAudio
  115. },
  116. gumOptions
  117. },
  118. onSuccess,
  119. onFailure
  120. ),
  121. err => onFailure(new JitsiTrackError(
  122. JitsiTrackErrors.ELECTRON_DESKTOP_PICKER_ERROR,
  123. err
  124. ))
  125. );
  126. } else {
  127. onFailure(new JitsiTrackError(
  128. JitsiTrackErrors.ELECTRON_DESKTOP_PICKER_NOT_FOUND));
  129. }
  130. },
  131. /**
  132. * Obtains a screen capture stream using getDisplayMedia.
  133. *
  134. * @param callback - The success callback.
  135. * @param errorCallback - The error callback.
  136. */
  137. obtainScreenFromGetDisplayMedia(options, callback, errorCallback) {
  138. logger.info('Using getDisplayMedia for screen sharing');
  139. let getDisplayMedia;
  140. if (navigator.getDisplayMedia) {
  141. getDisplayMedia = navigator.getDisplayMedia.bind(navigator);
  142. } else {
  143. // eslint-disable-next-line max-len
  144. getDisplayMedia = navigator.mediaDevices.getDisplayMedia.bind(navigator.mediaDevices);
  145. }
  146. getDisplayMedia({ video: true,
  147. audio: true })
  148. .then(stream => {
  149. let applyConstraintsPromise;
  150. if (stream
  151. && stream.getTracks()
  152. && stream.getTracks().length > 0) {
  153. const videoTrack = stream.getVideoTracks()[0];
  154. // Apply video track constraint.
  155. if (videoTrack) {
  156. applyConstraintsPromise = videoTrack.applyConstraints(options.trackOptions);
  157. }
  158. } else {
  159. applyConstraintsPromise = Promise.resolve();
  160. }
  161. applyConstraintsPromise.then(() =>
  162. callback({
  163. stream,
  164. sourceId: stream.id
  165. }));
  166. })
  167. .catch(() =>
  168. errorCallback(new JitsiTrackError(JitsiTrackErrors
  169. .SCREENSHARING_USER_CANCELED)));
  170. }
  171. };
  172. /**
  173. * Handles response from external application / extension and calls GUM to
  174. * receive the desktop streams or reports error.
  175. * @param {object} options
  176. * @param {object} options.response
  177. * @param {string} options.response.streamId - the streamId for the desktop
  178. * stream.
  179. * @param {bool} options.response.screenShareAudio - Used by electron clients to
  180. * enable system audio screen sharing.
  181. * @param {string} options.response.error - error to be reported.
  182. * @param {object} options.gumOptions - options passed to GUM.
  183. * @param {Function} onSuccess - callback for success.
  184. * @param {Function} onFailure - callback for failure.
  185. * @param {object} gumOptions - options passed to GUM.
  186. */
  187. function onGetStreamResponse(
  188. options = {
  189. response: {},
  190. gumOptions: {}
  191. },
  192. onSuccess,
  193. onFailure) {
  194. const { streamId, streamType, screenShareAudio, error } = options.response || {};
  195. if (streamId) {
  196. const gumOptions = {
  197. desktopStream: streamId,
  198. screenShareAudio,
  199. ...options.gumOptions
  200. };
  201. gumFunction([ 'desktop' ], gumOptions)
  202. .then(stream => onSuccess({
  203. stream,
  204. sourceId: streamId,
  205. sourceType: streamType
  206. }), onFailure);
  207. } else {
  208. // As noted in Chrome Desktop Capture API:
  209. // If user didn't select any source (i.e. canceled the prompt)
  210. // then the callback is called with an empty streamId.
  211. if (streamId === '') {
  212. onFailure(new JitsiTrackError(
  213. JitsiTrackErrors.SCREENSHARING_USER_CANCELED));
  214. return;
  215. }
  216. onFailure(new JitsiTrackError(
  217. JitsiTrackErrors.SCREENSHARING_GENERIC_ERROR,
  218. error));
  219. }
  220. }
  221. export default ScreenObtainer;