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

ScreenshotCaptureSummary.tsx 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. const workerUrl = `${baseUrl}screenshot-capture-worker.min.js`;
  43. this._streamWorker = new Worker(workerUrl, { name: 'Screenshot capture worker' });
  44. this._streamWorker.onmessage = this._handleWorkerAction;
  45. this._initializedRegion = false;
  46. this._queue = [];
  47. }
  48. /**
  49. * Make a call to backend for region selection.
  50. *
  51. * @returns {void}
  52. */
  53. async _initRegionSelection() {
  54. const { _screenshotHistoryRegionUrl } = this._state['features/base/config'];
  55. const conference = getCurrentConference(this._state);
  56. const sessionId = conference?.getMeetingUniqueId();
  57. const { jwt } = this._state['features/base/jwt'];
  58. if (!_screenshotHistoryRegionUrl) {
  59. return;
  60. }
  61. const headers = {
  62. ...jwt && { 'Authorization': `Bearer ${jwt}` }
  63. };
  64. try {
  65. await fetch(`${_screenshotHistoryRegionUrl}/${sessionId}`, {
  66. method: 'POST',
  67. headers
  68. });
  69. } catch (err) {
  70. logger.warn(`Could not create screenshot region: ${err}`);
  71. return;
  72. }
  73. this._initializedRegion = true;
  74. }
  75. /**
  76. * Starts the screenshot capture event on a loop.
  77. *
  78. * @param {JitsiTrack} jitsiTrack - The track that contains the stream from which screenshots are to be sent.
  79. * @returns {Promise} - Promise that resolves once effect has started or rejects if the
  80. * videoType parameter is not desktop.
  81. */
  82. async start(jitsiTrack: any) {
  83. if (!window.OffscreenCanvas) {
  84. logger.warn('Can\'t start screenshot capture, OffscreenCanvas is not available');
  85. return;
  86. }
  87. const { videoType, track } = jitsiTrack;
  88. if (videoType !== 'desktop') {
  89. return;
  90. }
  91. this._imageCapture = new ImageCapture(track);
  92. if (!this._initializedRegion) {
  93. await this._initRegionSelection();
  94. }
  95. this.sendTimeout();
  96. }
  97. /**
  98. * Stops the ongoing {@code ScreenshotCaptureEffect} by clearing the {@code Worker} interval.
  99. *
  100. * @returns {void}
  101. */
  102. stop() {
  103. this._streamWorker.postMessage({ id: CLEAR_TIMEOUT });
  104. }
  105. /**
  106. * Sends to worker the imageBitmap for the next timeout.
  107. *
  108. * @returns {Promise<void>}
  109. */
  110. async sendTimeout() {
  111. let imageBitmap: ImageBitmap | undefined;
  112. if (!this._imageCapture.track || this._imageCapture.track.readyState !== 'live') {
  113. logger.warn('Track is in invalid state');
  114. this.stop();
  115. return;
  116. }
  117. try {
  118. imageBitmap = await this._imageCapture.grabFrame();
  119. } catch (e) {
  120. // ignore error
  121. }
  122. this._streamWorker.postMessage({
  123. id: SET_TIMEOUT,
  124. timeMs: POLL_INTERVAL,
  125. imageBitmap
  126. });
  127. }
  128. /**
  129. * Handler of the {@code EventHandler} message that calls the appropriate method based on the parameter's id.
  130. *
  131. * @private
  132. * @param {EventHandler} message - Message received from the Worker.
  133. * @returns {void}
  134. */
  135. _handleWorkerAction(message: { data: { id: number; imageBlob?: Blob; }; }) {
  136. const { id, imageBlob } = message.data;
  137. this.sendTimeout();
  138. if (id === TIMEOUT_TICK && imageBlob && this._queue.length < SCREENSHOT_QUEUE_LIMIT) {
  139. this._doProcessScreenshot(imageBlob);
  140. }
  141. }
  142. /**
  143. * Method that processes the screenshot.
  144. *
  145. * @private
  146. * @param {Blob} imageBlob - The blob for the current screenshot.
  147. * @returns {void}
  148. */
  149. _doProcessScreenshot(imageBlob: Blob) {
  150. this._queue.push(imageBlob);
  151. sendAnalytics(createScreensharingCaptureTakenEvent());
  152. const conference = getCurrentConference(this._state);
  153. const sessionId = conference?.getMeetingUniqueId();
  154. const { connection } = this._state['features/base/connection'];
  155. const jid = connection?.getJid();
  156. const timestamp = Date.now();
  157. const { jwt } = this._state['features/base/jwt'];
  158. const meetingFqn = extractFqnFromPath();
  159. const remoteParticipants = getRemoteParticipants(this._state);
  160. const participants: Array<string | undefined> = [];
  161. participants.push(getLocalParticipant(this._state)?.id);
  162. remoteParticipants.forEach(p => participants.push(p.id));
  163. processScreenshot(imageBlob, {
  164. jid,
  165. jwt,
  166. sessionId,
  167. timestamp,
  168. meetingFqn,
  169. participants
  170. }).then(() => {
  171. const index = this._queue.indexOf(imageBlob);
  172. if (index > -1) {
  173. this._queue.splice(index, 1);
  174. }
  175. });
  176. }
  177. }