您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ScreenshotCaptureSummary.js 5.8KB

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