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.

ScreenshotCaptureSummary.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 { 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 { getParticipantJid } from './functions';
  17. import { processScreenshot } from './processScreenshot';
  18. import { timerWorkerScript } from './worker';
  19. declare var interfaceConfig: Object;
  20. declare var ImageCapture: any;
  21. /**
  22. * Effect that wraps {@code MediaStream} adding periodic screenshot captures.
  23. * Manipulates the original desktop stream and performs custom processing operations, if implemented.
  24. */
  25. export default class ScreenshotCaptureSummary {
  26. _state: Object;
  27. _currentCanvas: HTMLCanvasElement;
  28. _currentCanvasContext: CanvasRenderingContext2D;
  29. _handleWorkerAction: Function;
  30. _initScreenshotCapture: Function;
  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. }
  51. /**
  52. * Starts the screenshot capture event on a loop.
  53. *
  54. * @param {Track} track - The track that contains the stream from which screenshots are to be sent.
  55. * @returns {Promise} - Promise that resolves once effect has started or rejects if the
  56. * videoType parameter is not desktop.
  57. */
  58. start(track: Object) {
  59. const { videoType } = track;
  60. const stream = track.getOriginalStream();
  61. if (videoType !== 'desktop') {
  62. return;
  63. }
  64. const desktopTrack = stream.getVideoTracks()[0];
  65. const { height, width }
  66. = desktopTrack.getSettings() ?? desktopTrack.getConstraints();
  67. this._streamHeight = height;
  68. this._streamWidth = width;
  69. this._currentCanvas.height = parseInt(height, 10);
  70. this._currentCanvas.width = parseInt(width, 10);
  71. this._imageCapture = new ImageCapture(desktopTrack);
  72. this._initScreenshotCapture();
  73. }
  74. /**
  75. * Stops the ongoing {@code ScreenshotCaptureEffect} by clearing the {@code Worker} interval.
  76. *
  77. * @returns {void}
  78. */
  79. stop() {
  80. this._streamWorker.postMessage({ id: CLEAR_INTERVAL });
  81. }
  82. /**
  83. * Method that is called as soon as the first frame of the video loads from stream.
  84. * The method is used to store the {@code ImageData} object from the first frames
  85. * in order to use it for future comparisons based on which we can process only certain
  86. * screenshots.
  87. *
  88. * @private
  89. * @returns {void}
  90. */
  91. async _initScreenshotCapture() {
  92. const imageBitmap = await this._imageCapture.grabFrame();
  93. this._currentCanvasContext.drawImage(imageBitmap, 0, 0, this._streamWidth, this._streamHeight);
  94. const imageData = this._currentCanvasContext.getImageData(0, 0, this._streamWidth, this._streamHeight);
  95. this._storedImageData = imageData;
  96. this._streamWorker.postMessage({
  97. id: SET_INTERVAL,
  98. timeMs: POLL_INTERVAL
  99. });
  100. }
  101. /**
  102. * Handler of the {@code EventHandler} message that calls the appropriate method based on the parameter's id.
  103. *
  104. * @private
  105. * @param {EventHandler} message - Message received from the Worker.
  106. * @returns {void}
  107. */
  108. _handleWorkerAction(message: Object) {
  109. return message.data.id === INTERVAL_TIMEOUT && this._handleScreenshot();
  110. }
  111. /**
  112. * Method that processes the screenshot.
  113. *
  114. * @private
  115. * @param {ImageData} imageData - The image data of the new screenshot.
  116. * @returns {void}
  117. */
  118. _doProcessScreenshot(imageData) {
  119. sendAnalytics(createScreensharingCaptureTakenEvent());
  120. const conference = getCurrentConference(this._state);
  121. const sessionId = conference.getMeetingUniqueId();
  122. const { connection } = this._state['features/base/connection'];
  123. const jid = connection.getJid();
  124. const timestamp = Date.now();
  125. const { jwt } = this._state['features/base/jwt'];
  126. const meetingFqn = extractFqnFromPath();
  127. const remoteParticipants = getRemoteParticipants(this._state);
  128. const participants = [];
  129. remoteParticipants.forEach(p => participants.push(
  130. getParticipantJid(this._state, p.id)
  131. ));
  132. this._storedImageData = imageData;
  133. processScreenshot(this._currentCanvas, {
  134. jid,
  135. jwt,
  136. sessionId,
  137. timestamp,
  138. meetingFqn,
  139. participants
  140. });
  141. }
  142. /**
  143. * Screenshot handler.
  144. *
  145. * @private
  146. * @returns {void}
  147. */
  148. async _handleScreenshot() {
  149. const imageBitmap = await this._imageCapture.grabFrame();
  150. this._currentCanvasContext.drawImage(imageBitmap, 0, 0, this._streamWidth, this._streamHeight);
  151. const imageData = this._currentCanvasContext.getImageData(0, 0, this._streamWidth, this._streamHeight);
  152. resemble(imageData)
  153. .compareTo(this._storedImageData)
  154. .setReturnEarlyThreshold(PERCENTAGE_LOWER_BOUND)
  155. .onComplete(resultData => {
  156. if (resultData.rawMisMatchPercentage > PERCENTAGE_LOWER_BOUND) {
  157. this._doProcessScreenshot(imageData);
  158. }
  159. });
  160. }
  161. }