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

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