Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ScreenshotCaptureEffect.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // @flow
  2. import pixelmatch from 'pixelmatch';
  3. import {
  4. CLEAR_INTERVAL,
  5. INTERVAL_TIMEOUT,
  6. PIXEL_LOWER_BOUND,
  7. POLL_INTERVAL,
  8. SET_INTERVAL
  9. } from './constants';
  10. import { getCurrentConference } from '../../base/conference';
  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);
  43. this._streamWorker.onmessage = this._handleWorkerAction;
  44. }
  45. /**
  46. * Checks if the local track supports this effect.
  47. *
  48. * @param {JitsiLocalTrack} jitsiLocalTrack - Targeted local track.
  49. * @returns {boolean} - Returns true if this effect can run on the specified track, false otherwise.
  50. */
  51. isEnabled(jitsiLocalTrack: Object) {
  52. return (
  53. interfaceConfig.ENABLE_SCREENSHOT_CAPTURE
  54. && jitsiLocalTrack.isVideoTrack()
  55. && jitsiLocalTrack.videoType === 'desktop'
  56. );
  57. }
  58. /**
  59. * Starts the screenshot capture event on a loop.
  60. *
  61. * @param {MediaStream} stream - The desktop stream from which screenshots are to be sent.
  62. * @returns {MediaStream} - The same stream, with the interval set.
  63. */
  64. startEffect(stream: MediaStream) {
  65. const desktopTrack = stream.getVideoTracks()[0];
  66. const { height, width }
  67. = desktopTrack.getSettings() ?? desktopTrack.getConstraints();
  68. this._streamHeight = height;
  69. this._streamWidth = width;
  70. this._currentCanvas.height = parseInt(height, 10);
  71. this._currentCanvas.width = parseInt(width, 10);
  72. this._videoElement.height = parseInt(height, 10);
  73. this._videoElement.width = parseInt(width, 10);
  74. this._videoElement.srcObject = stream;
  75. this._videoElement.play();
  76. // Store first capture for comparisons in {@code this._handleScreenshot}.
  77. this._videoElement.addEventListener('loadeddata', this._initScreenshotCapture);
  78. return stream;
  79. }
  80. /**
  81. * Stops the ongoing {@code ScreenshotCaptureEffect} by clearing the {@code Worker} interval.
  82. *
  83. * @returns {void}
  84. */
  85. stopEffect() {
  86. this._streamWorker.postMessage({ id: CLEAR_INTERVAL });
  87. this._videoElement.removeEventListener('loadeddata', this._initScreenshotCapture);
  88. }
  89. /**
  90. * Method that is called as soon as the first frame of the video loads from stream.
  91. * The method is used to store the {@code ImageData} object from the first frames
  92. * in order to use it for future comparisons based on which we can process only certain
  93. * screenshots.
  94. *
  95. * @private
  96. * @returns {void}
  97. */
  98. _initScreenshotCapture() {
  99. const storedCanvas = document.createElement('canvas');
  100. const storedCanvasContext = storedCanvas.getContext('2d');
  101. storedCanvasContext.drawImage(this._videoElement, 0, 0, this._streamWidth, this._streamHeight);
  102. const { data } = storedCanvasContext.getImageData(0, 0, this._streamWidth, this._streamHeight);
  103. this._storedImageData = data;
  104. this._streamWorker.postMessage({
  105. id: SET_INTERVAL,
  106. timeMs: POLL_INTERVAL
  107. });
  108. }
  109. /**
  110. * Handler of the {@code EventHandler} message that calls the appropriate method based on the parameter's id.
  111. *
  112. * @private
  113. * @param {EventHandler} message - Message received from the Worker.
  114. * @returns {void}
  115. */
  116. _handleWorkerAction(message: Object) {
  117. return message.data.id === INTERVAL_TIMEOUT && this._handleScreenshot();
  118. }
  119. /**
  120. * Method that decides whether an image should be processed based on a preset pixel lower bound.
  121. *
  122. * @private
  123. * @param {integer} nbPixels - The number of pixels of the candidate image.
  124. * @returns {boolean} - Whether the image should be processed or not.
  125. */
  126. _shouldProcessScreenshot(nbPixels: number) {
  127. return nbPixels >= PIXEL_LOWER_BOUND;
  128. }
  129. /**
  130. * Screenshot handler.
  131. *
  132. * @private
  133. * @returns {void}
  134. */
  135. _handleScreenshot() {
  136. this._currentCanvasContext.drawImage(this._videoElement, 0, 0, this._streamWidth, this._streamHeight);
  137. const { data } = this._currentCanvasContext.getImageData(0, 0, this._streamWidth, this._streamHeight);
  138. const diffPixels = pixelmatch(data, this._storedImageData, null, this._streamWidth, this._streamHeight);
  139. if (this._shouldProcessScreenshot(diffPixels)) {
  140. const conference = getCurrentConference(this._state);
  141. const sessionId = conference.getMeetingUniqueId();
  142. const { connection, timeEstablished } = this._state['features/base/connection'];
  143. const jid = connection.getJid();
  144. const timeLapseSeconds = timeEstablished && Math.floor((Date.now() - timeEstablished) / 1000);
  145. const { jwt } = this._state['features/base/jwt'];
  146. this._storedImageData = data;
  147. processScreenshot(this._currentCanvas, {
  148. jid,
  149. jwt,
  150. sessionId,
  151. timeLapseSeconds
  152. });
  153. }
  154. }
  155. }