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

ScreenshotCaptureSummary.tsx 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import 'image-capture';
  2. import './createImageBitmap';
  3. import { createScreensharingCaptureTakenEvent } from '../analytics/AnalyticsEvents';
  4. import { sendAnalytics } from '../analytics/functions';
  5. import { IReduxState } from '../app/types';
  6. import { getCurrentConference } from '../base/conference/functions';
  7. import { getLocalParticipant, getRemoteParticipants } from '../base/participants/functions';
  8. import { getBaseUrl } from '../base/util/helpers';
  9. import { extractFqnFromPath } from '../dynamic-branding/functions.any';
  10. import {
  11. CLEAR_TIMEOUT,
  12. POLL_INTERVAL,
  13. SCREENSHOT_QUEUE_LIMIT,
  14. SET_TIMEOUT,
  15. TIMEOUT_TICK
  16. } from './constants';
  17. import logger from './logger';
  18. // eslint-disable-next-line lines-around-comment
  19. // @ts-ignore
  20. import { processScreenshot } from './processScreenshot';
  21. declare let ImageCapture: any;
  22. /**
  23. * Effect that wraps {@code MediaStream} adding periodic screenshot captures.
  24. * Manipulates the original desktop stream and performs custom processing operations, if implemented.
  25. */
  26. export default class ScreenshotCaptureSummary {
  27. _state: IReduxState;
  28. _initializedRegion: boolean;
  29. _imageCapture: ImageCapture;
  30. _streamWorker: Worker;
  31. _queue: Blob[];
  32. /**
  33. * Initializes a new {@code ScreenshotCaptureEffect} instance.
  34. *
  35. * @param {Object} state - The redux state.
  36. */
  37. constructor(state: IReduxState) {
  38. this._state = state;
  39. // Bind handlers such that they access the same instance.
  40. this._handleWorkerAction = this._handleWorkerAction.bind(this);
  41. const baseUrl = `${getBaseUrl()}libs/`;
  42. let workerUrl = `${baseUrl}screenshot-capture-worker.min.js`;
  43. // @ts-ignore
  44. const workerBlob = new Blob([ `importScripts("${workerUrl}");` ], { type: 'application/javascript' });
  45. // @ts-ignore
  46. workerUrl = window.URL.createObjectURL(workerBlob);
  47. this._streamWorker = new Worker(workerUrl, { name: 'Screenshot capture worker' });
  48. this._streamWorker.onmessage = this._handleWorkerAction;
  49. this._initializedRegion = false;
  50. this._queue = [];
  51. }
  52. /**
  53. * Make a call to backend for region selection.
  54. *
  55. * @returns {void}
  56. */
  57. async _initRegionSelection() {
  58. const { _screenshotHistoryRegionUrl } = this._state['features/base/config'];
  59. const conference = getCurrentConference(this._state);
  60. const sessionId = conference?.getMeetingUniqueId();
  61. const { jwt } = this._state['features/base/jwt'];
  62. if (!_screenshotHistoryRegionUrl) {
  63. return;
  64. }
  65. const headers = {
  66. ...jwt && { 'Authorization': `Bearer ${jwt}` }
  67. };
  68. try {
  69. await fetch(`${_screenshotHistoryRegionUrl}/${sessionId}`, {
  70. method: 'POST',
  71. headers
  72. });
  73. } catch (err) {
  74. logger.warn(`Could not create screenshot region: ${err}`);
  75. return;
  76. }
  77. this._initializedRegion = true;
  78. }
  79. /**
  80. * Starts the screenshot capture event on a loop.
  81. *
  82. * @param {JitsiTrack} jitsiTrack - The track that contains the stream from which screenshots are to be sent.
  83. * @returns {Promise} - Promise that resolves once effect has started or rejects if the
  84. * videoType parameter is not desktop.
  85. */
  86. async start(jitsiTrack: any) {
  87. if (!window.OffscreenCanvas) {
  88. logger.warn('Can\'t start screenshot capture, OffscreenCanvas is not available');
  89. return;
  90. }
  91. const { videoType, track } = jitsiTrack;
  92. if (videoType !== 'desktop') {
  93. return;
  94. }
  95. this._imageCapture = new ImageCapture(track);
  96. if (!this._initializedRegion) {
  97. await this._initRegionSelection();
  98. }
  99. this.sendTimeout();
  100. }
  101. /**
  102. * Stops the ongoing {@code ScreenshotCaptureEffect} by clearing the {@code Worker} interval.
  103. *
  104. * @returns {void}
  105. */
  106. stop() {
  107. this._streamWorker.postMessage({ id: CLEAR_TIMEOUT });
  108. }
  109. /**
  110. * Sends to worker the imageBitmap for the next timeout.
  111. *
  112. * @returns {Promise<void>}
  113. */
  114. async sendTimeout() {
  115. let imageBitmap: ImageBitmap | undefined;
  116. if (!this._imageCapture.track || this._imageCapture.track.readyState !== 'live') {
  117. logger.warn('Track is in invalid state');
  118. this.stop();
  119. return;
  120. }
  121. try {
  122. imageBitmap = await this._imageCapture.grabFrame();
  123. } catch (e) {
  124. // ignore error
  125. }
  126. this._streamWorker.postMessage({
  127. id: SET_TIMEOUT,
  128. timeMs: POLL_INTERVAL,
  129. imageBitmap
  130. });
  131. }
  132. /**
  133. * Handler of the {@code EventHandler} message that calls the appropriate method based on the parameter's id.
  134. *
  135. * @private
  136. * @param {EventHandler} message - Message received from the Worker.
  137. * @returns {void}
  138. */
  139. _handleWorkerAction(message: { data: { id: number; imageBlob?: Blob; }; }) {
  140. const { id, imageBlob } = message.data;
  141. this.sendTimeout();
  142. if (id === TIMEOUT_TICK && imageBlob && this._queue.length < SCREENSHOT_QUEUE_LIMIT) {
  143. this._doProcessScreenshot(imageBlob);
  144. }
  145. }
  146. /**
  147. * Method that processes the screenshot.
  148. *
  149. * @private
  150. * @param {Blob} imageBlob - The blob for the current screenshot.
  151. * @returns {void}
  152. */
  153. _doProcessScreenshot(imageBlob: Blob) {
  154. this._queue.push(imageBlob);
  155. sendAnalytics(createScreensharingCaptureTakenEvent());
  156. const conference = getCurrentConference(this._state);
  157. const sessionId = conference?.getMeetingUniqueId();
  158. const { connection } = this._state['features/base/connection'];
  159. const jid = connection?.getJid();
  160. const timestamp = Date.now();
  161. const { jwt } = this._state['features/base/jwt'];
  162. const meetingFqn = extractFqnFromPath();
  163. const remoteParticipants = getRemoteParticipants(this._state);
  164. const participants: Array<string | undefined> = [];
  165. participants.push(getLocalParticipant(this._state)?.id);
  166. remoteParticipants.forEach(p => participants.push(p.id));
  167. processScreenshot(imageBlob, {
  168. jid,
  169. jwt,
  170. sessionId,
  171. timestamp,
  172. meetingFqn,
  173. participants
  174. }).then(() => {
  175. const index = this._queue.indexOf(imageBlob);
  176. if (index > -1) {
  177. this._queue.splice(index, 1);
  178. }
  179. });
  180. }
  181. }