Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ScreenshotCaptureEffect.js 6.1KB

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