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.

ScreenshotCaptureEffect.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // @flow
  2. import pixelmatch from 'pixelmatch';
  3. import { getCurrentConference } from '../../base/conference';
  4. import {
  5. CLEAR_INTERVAL,
  6. INTERVAL_TIMEOUT,
  7. PIXEL_LOWER_BOUND,
  8. POLL_INTERVAL,
  9. SET_INTERVAL
  10. } from './constants';
  11. import { processScreenshot } from './processScreenshot';
  12. import { timerWorkerScript } from './worker';
  13. declare var interfaceConfig: Object;
  14. /**
  15. * Effect that wraps {@code MediaStream} adding periodic screenshot captures.
  16. * Manipulates the original desktop stream and performs custom processing operations, if implemented.
  17. */
  18. export default class ScreenshotCaptureEffect {
  19. _state: Object;
  20. _currentCanvas: HTMLCanvasElement;
  21. _currentCanvasContext: CanvasRenderingContext2D;
  22. _videoElement: HTMLVideoElement;
  23. _handleWorkerAction: Function;
  24. _initScreenshotCapture: Function;
  25. _streamWorker: Worker;
  26. _streamHeight: any;
  27. _streamWidth: any;
  28. _storedImageData: Uint8ClampedArray;
  29. /**
  30. * Initializes a new {@code ScreenshotCaptureEffect} instance.
  31. *
  32. * @param {Object} state - The redux state.
  33. */
  34. constructor(state: Object) {
  35. this._state = state;
  36. this._currentCanvas = document.createElement('canvas');
  37. this._currentCanvasContext = this._currentCanvas.getContext('2d');
  38. this._videoElement = document.createElement('video');
  39. // Bind handlers such that they access the same instance.
  40. this._handleWorkerAction = this._handleWorkerAction.bind(this);
  41. this._initScreenshotCapture = this._initScreenshotCapture.bind(this);
  42. this._streamWorker = new Worker(timerWorkerScript, { name: 'Screenshot capture worker' });
  43. this._streamWorker.onmessage = this._handleWorkerAction;
  44. }
  45. /**
  46. * Starts the screenshot capture event on a loop.
  47. *
  48. * @param {MediaStream} stream - The desktop stream from which screenshots are to be sent.
  49. * @param {string} videoType - The type of the media stream.
  50. * @returns {Promise} - Promise that resolves once effect has started or rejects if the
  51. * videoType parameter is not desktop.
  52. */
  53. startEffect(stream: MediaStream, videoType: string) {
  54. return new Promise<void>((resolve, reject) => {
  55. if (videoType !== 'desktop') {
  56. reject();
  57. }
  58. const desktopTrack = stream.getVideoTracks()[0];
  59. const { height, width }
  60. = desktopTrack.getSettings() ?? desktopTrack.getConstraints();
  61. this._streamHeight = height;
  62. this._streamWidth = width;
  63. this._currentCanvas.height = parseInt(height, 10);
  64. this._currentCanvas.width = parseInt(width, 10);
  65. this._videoElement.height = parseInt(height, 10);
  66. this._videoElement.width = parseInt(width, 10);
  67. this._videoElement.srcObject = stream;
  68. this._videoElement.play();
  69. // Store first capture for comparisons in {@code this._handleScreenshot}.
  70. this._videoElement.addEventListener('loadeddata', this._initScreenshotCapture);
  71. resolve();
  72. });
  73. }
  74. /**
  75. * Stops the ongoing {@code ScreenshotCaptureEffect} by clearing the {@code Worker} interval.
  76. *
  77. * @returns {void}
  78. */
  79. stopEffect() {
  80. this._streamWorker.postMessage({ id: CLEAR_INTERVAL });
  81. this._videoElement.removeEventListener('loadeddata', this._initScreenshotCapture);
  82. }
  83. /**
  84. * Method that is called as soon as the first frame of the video loads from stream.
  85. * The method is used to store the {@code ImageData} object from the first frames
  86. * in order to use it for future comparisons based on which we can process only certain
  87. * screenshots.
  88. *
  89. * @private
  90. * @returns {void}
  91. */
  92. _initScreenshotCapture() {
  93. const storedCanvas = document.createElement('canvas');
  94. const storedCanvasContext = storedCanvas.getContext('2d');
  95. storedCanvasContext.drawImage(this._videoElement, 0, 0, this._streamWidth, this._streamHeight);
  96. const { data } = storedCanvasContext.getImageData(0, 0, this._streamWidth, this._streamHeight);
  97. this._storedImageData = data;
  98. this._streamWorker.postMessage({
  99. id: SET_INTERVAL,
  100. timeMs: POLL_INTERVAL
  101. });
  102. }
  103. /**
  104. * Handler of the {@code EventHandler} message that calls the appropriate method based on the parameter's id.
  105. *
  106. * @private
  107. * @param {EventHandler} message - Message received from the Worker.
  108. * @returns {void}
  109. */
  110. _handleWorkerAction(message: Object) {
  111. return message.data.id === INTERVAL_TIMEOUT && this._handleScreenshot();
  112. }
  113. /**
  114. * Method that decides whether an image should be processed based on a preset pixel lower bound.
  115. *
  116. * @private
  117. * @param {integer} nbPixels - The number of pixels of the candidate image.
  118. * @returns {boolean} - Whether the image should be processed or not.
  119. */
  120. _shouldProcessScreenshot(nbPixels: number) {
  121. return nbPixels >= PIXEL_LOWER_BOUND;
  122. }
  123. /**
  124. * Screenshot handler.
  125. *
  126. * @private
  127. * @returns {void}
  128. */
  129. _handleScreenshot() {
  130. this._currentCanvasContext.drawImage(this._videoElement, 0, 0, this._streamWidth, this._streamHeight);
  131. const { data } = this._currentCanvasContext.getImageData(0, 0, this._streamWidth, this._streamHeight);
  132. const diffPixels = pixelmatch(data, this._storedImageData, null, this._streamWidth, this._streamHeight);
  133. if (this._shouldProcessScreenshot(diffPixels)) {
  134. const conference = getCurrentConference(this._state);
  135. const sessionId = conference.getMeetingUniqueId();
  136. const { connection, timeEstablished } = this._state['features/base/connection'];
  137. const jid = connection.getJid();
  138. const timeLapseSeconds = timeEstablished && Math.floor((Date.now() - timeEstablished) / 1000);
  139. const { jwt } = this._state['features/base/jwt'];
  140. this._storedImageData = data;
  141. processScreenshot(this._currentCanvas, {
  142. jid,
  143. jwt,
  144. sessionId,
  145. timeLapseSeconds
  146. });
  147. }
  148. }
  149. }