Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ScreenshotCaptureSummary.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // @flow
  2. import resemble from 'resemblejs';
  3. import 'image-capture';
  4. import './createImageBitmap';
  5. import { createScreensharingCaptureTakenEvent, sendAnalytics } from '../analytics';
  6. import { getCurrentConference } from '../base/conference';
  7. import { getLocalParticipant, getRemoteParticipants } from '../base/participants';
  8. import { extractFqnFromPath } from '../dynamic-branding';
  9. import {
  10. CLEAR_INTERVAL,
  11. INTERVAL_TIMEOUT,
  12. PERCENTAGE_LOWER_BOUND,
  13. POLL_INTERVAL,
  14. SET_INTERVAL
  15. } from './constants';
  16. import { processScreenshot } from './processScreenshot';
  17. import { timerWorkerScript } from './worker';
  18. declare var interfaceConfig: Object;
  19. declare var ImageCapture: any;
  20. /**
  21. * Effect that wraps {@code MediaStream} adding periodic screenshot captures.
  22. * Manipulates the original desktop stream and performs custom processing operations, if implemented.
  23. */
  24. export default class ScreenshotCaptureSummary {
  25. _state: Object;
  26. _currentCanvas: HTMLCanvasElement;
  27. _currentCanvasContext: CanvasRenderingContext2D;
  28. _handleWorkerAction: Function;
  29. _initScreenshotCapture: Function;
  30. _initializedRegion: boolean;
  31. _imageCapture: any;
  32. _streamWorker: Worker;
  33. _streamHeight: any;
  34. _streamWidth: any;
  35. _storedImageData: ImageData;
  36. /**
  37. * Initializes a new {@code ScreenshotCaptureEffect} instance.
  38. *
  39. * @param {Object} state - The redux state.
  40. */
  41. constructor(state: Object) {
  42. this._state = state;
  43. this._currentCanvas = document.createElement('canvas');
  44. this._currentCanvasContext = this._currentCanvas.getContext('2d');
  45. // Bind handlers such that they access the same instance.
  46. this._handleWorkerAction = this._handleWorkerAction.bind(this);
  47. this._initScreenshotCapture = this._initScreenshotCapture.bind(this);
  48. this._streamWorker = new Worker(timerWorkerScript, { name: 'Screenshot capture worker' });
  49. this._streamWorker.onmessage = this._handleWorkerAction;
  50. this._initializedRegion = false;
  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. await fetch(`${_screenshotHistoryRegionUrl}/${sessionId}`, {
  69. method: 'POST',
  70. headers
  71. });
  72. this._initializedRegion = true;
  73. }
  74. /**
  75. * Starts the screenshot capture event on a loop.
  76. *
  77. * @param {Track} track - The track that contains the stream from which screenshots are to be sent.
  78. * @returns {Promise} - Promise that resolves once effect has started or rejects if the
  79. * videoType parameter is not desktop.
  80. */
  81. async start(track: Object) {
  82. const { videoType } = track;
  83. const stream = track.getOriginalStream();
  84. if (videoType !== 'desktop') {
  85. return;
  86. }
  87. const desktopTrack = stream.getVideoTracks()[0];
  88. const { height, width }
  89. = desktopTrack.getSettings() ?? desktopTrack.getConstraints();
  90. this._streamHeight = height;
  91. this._streamWidth = width;
  92. this._currentCanvas.height = parseInt(height, 10);
  93. this._currentCanvas.width = parseInt(width, 10);
  94. this._imageCapture = new ImageCapture(desktopTrack);
  95. if (!this._initializedRegion) {
  96. await this._initRegionSelection();
  97. }
  98. this._initScreenshotCapture();
  99. }
  100. /**
  101. * Stops the ongoing {@code ScreenshotCaptureEffect} by clearing the {@code Worker} interval.
  102. *
  103. * @returns {void}
  104. */
  105. stop() {
  106. this._streamWorker.postMessage({ id: CLEAR_INTERVAL });
  107. }
  108. /**
  109. * Method that is called as soon as the first frame of the video loads from stream.
  110. * The method is used to store the {@code ImageData} object from the first frames
  111. * in order to use it for future comparisons based on which we can process only certain
  112. * screenshots.
  113. *
  114. * @private
  115. * @returns {void}
  116. */
  117. async _initScreenshotCapture() {
  118. const imageBitmap = await this._imageCapture.grabFrame();
  119. this._currentCanvasContext.drawImage(imageBitmap, 0, 0, this._streamWidth, this._streamHeight);
  120. const imageData = this._currentCanvasContext.getImageData(0, 0, this._streamWidth, this._streamHeight);
  121. this._storedImageData = imageData;
  122. this._streamWorker.postMessage({
  123. id: SET_INTERVAL,
  124. timeMs: POLL_INTERVAL
  125. });
  126. }
  127. /**
  128. * Handler of the {@code EventHandler} message that calls the appropriate method based on the parameter's id.
  129. *
  130. * @private
  131. * @param {EventHandler} message - Message received from the Worker.
  132. * @returns {void}
  133. */
  134. _handleWorkerAction(message: Object) {
  135. return message.data.id === INTERVAL_TIMEOUT && this._handleScreenshot();
  136. }
  137. /**
  138. * Method that processes the screenshot.
  139. *
  140. * @private
  141. * @param {ImageData} imageData - The image data of the new screenshot.
  142. * @returns {void}
  143. */
  144. _doProcessScreenshot(imageData) {
  145. sendAnalytics(createScreensharingCaptureTakenEvent());
  146. const conference = getCurrentConference(this._state);
  147. const sessionId = conference.getMeetingUniqueId();
  148. const { connection } = this._state['features/base/connection'];
  149. const jid = connection.getJid();
  150. const timestamp = Date.now();
  151. const { jwt } = this._state['features/base/jwt'];
  152. const meetingFqn = extractFqnFromPath();
  153. const remoteParticipants = getRemoteParticipants(this._state);
  154. const participants = [];
  155. participants.push(getLocalParticipant(this._state).id);
  156. remoteParticipants.forEach(p => participants.push(p.id));
  157. this._storedImageData = imageData;
  158. processScreenshot(this._currentCanvas, {
  159. jid,
  160. jwt,
  161. sessionId,
  162. timestamp,
  163. meetingFqn,
  164. participants
  165. });
  166. }
  167. /**
  168. * Screenshot handler.
  169. *
  170. * @private
  171. * @returns {void}
  172. */
  173. async _handleScreenshot() {
  174. const imageBitmap = await this._imageCapture.grabFrame();
  175. this._currentCanvasContext.drawImage(imageBitmap, 0, 0, this._streamWidth, this._streamHeight);
  176. const imageData = this._currentCanvasContext.getImageData(0, 0, this._streamWidth, this._streamHeight);
  177. resemble(imageData)
  178. .compareTo(this._storedImageData)
  179. .setReturnEarlyThreshold(PERCENTAGE_LOWER_BOUND)
  180. .onComplete(resultData => {
  181. if (resultData.rawMisMatchPercentage > PERCENTAGE_LOWER_BOUND) {
  182. this._doProcessScreenshot(imageData);
  183. }
  184. });
  185. }
  186. }