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

JitsiStreamBlurEffect.js 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // @flow
  2. import {
  3. CLEAR_TIMEOUT,
  4. TIMEOUT_TICK,
  5. SET_TIMEOUT,
  6. timerWorkerScript
  7. } from './TimerWorker';
  8. const blurValue = '25px';
  9. /**
  10. * Represents a modified MediaStream that adds blur to video background.
  11. * <tt>JitsiStreamBlurEffect</tt> does the processing of the original
  12. * video stream.
  13. */
  14. export default class JitsiStreamBlurEffect {
  15. _model: Object;
  16. _options: Object;
  17. _segmentationPixelCount: number;
  18. _inputVideoElement: HTMLVideoElement;
  19. _onMaskFrameTimer: Function;
  20. _maskFrameTimerWorker: Worker;
  21. _outputCanvasElement: HTMLCanvasElement;
  22. _outputCanvasCtx: Object;
  23. _segmentationMaskCtx: Object;
  24. _segmentationMask: Object;
  25. _segmentationMaskCanvas: Object;
  26. _renderMask: Function;
  27. isEnabled: Function;
  28. startEffect: Function;
  29. stopEffect: Function;
  30. /**
  31. * Represents a modified video MediaStream track.
  32. *
  33. * @class
  34. * @param {Object} model - Meet model.
  35. * @param {Object} options - Segmentation dimensions.
  36. */
  37. constructor(model: Object, options: Object) {
  38. this._model = model;
  39. this._options = options;
  40. this._segmentationPixelCount = this._options.width * this._options.height;
  41. // Bind event handler so it is only bound once for every instance.
  42. this._onMaskFrameTimer = this._onMaskFrameTimer.bind(this);
  43. // Workaround for FF issue https://bugzilla.mozilla.org/show_bug.cgi?id=1388974
  44. this._outputCanvasElement = document.createElement('canvas');
  45. this._outputCanvasElement.getContext('2d');
  46. this._inputVideoElement = document.createElement('video');
  47. }
  48. /**
  49. * EventHandler onmessage for the maskFrameTimerWorker WebWorker.
  50. *
  51. * @private
  52. * @param {EventHandler} response - The onmessage EventHandler parameter.
  53. * @returns {void}
  54. */
  55. async _onMaskFrameTimer(response: Object) {
  56. if (response.data.id === TIMEOUT_TICK) {
  57. await this._renderMask();
  58. }
  59. }
  60. /**
  61. * Represents the run post processing.
  62. *
  63. * @returns {void}
  64. */
  65. runPostProcessing() {
  66. this._outputCanvasCtx.globalCompositeOperation = 'copy';
  67. // Draw segmentation mask.
  68. this._outputCanvasCtx.filter = `blur(${blurValue})`;
  69. this._outputCanvasCtx.drawImage(
  70. this._segmentationMaskCanvas,
  71. 0,
  72. 0,
  73. this._options.width,
  74. this._options.height,
  75. 0,
  76. 0,
  77. this._inputVideoElement.width,
  78. this._inputVideoElement.height
  79. );
  80. this._outputCanvasCtx.globalCompositeOperation = 'source-in';
  81. this._outputCanvasCtx.filter = 'none';
  82. this._outputCanvasCtx.drawImage(this._inputVideoElement, 0, 0);
  83. this._outputCanvasCtx.globalCompositeOperation = 'destination-over';
  84. this._outputCanvasCtx.filter = `blur(${blurValue})`;
  85. this._outputCanvasCtx.drawImage(this._inputVideoElement, 0, 0);
  86. }
  87. /**
  88. * Represents the run Tensorflow Interference.
  89. *
  90. * @returns {void}
  91. */
  92. runInference() {
  93. this._model._runInference();
  94. const outputMemoryOffset = this._model._getOutputMemoryOffset() / 4;
  95. for (let i = 0; i < this._segmentationPixelCount; i++) {
  96. const background = this._model.HEAPF32[outputMemoryOffset + (i * 2)];
  97. const person = this._model.HEAPF32[outputMemoryOffset + (i * 2) + 1];
  98. const shift = Math.max(background, person);
  99. const backgroundExp = Math.exp(background - shift);
  100. const personExp = Math.exp(person - shift);
  101. // Sets only the alpha component of each pixel.
  102. this._segmentationMask.data[(i * 4) + 3] = (255 * personExp) / (backgroundExp + personExp);
  103. }
  104. this._segmentationMaskCtx.putImageData(this._segmentationMask, 0, 0);
  105. }
  106. /**
  107. * Loop function to render the background mask.
  108. *
  109. * @private
  110. * @returns {void}
  111. */
  112. _renderMask() {
  113. this.resizeSource();
  114. this.runInference();
  115. this.runPostProcessing();
  116. this._maskFrameTimerWorker.postMessage({
  117. id: SET_TIMEOUT,
  118. timeMs: 1000 / 30
  119. });
  120. }
  121. /**
  122. * Represents the resize source process.
  123. *
  124. * @returns {void}
  125. */
  126. resizeSource() {
  127. this._segmentationMaskCtx.drawImage(
  128. this._inputVideoElement,
  129. 0,
  130. 0,
  131. this._inputVideoElement.width,
  132. this._inputVideoElement.height,
  133. 0,
  134. 0,
  135. this._options.width,
  136. this._options.height
  137. );
  138. const imageData = this._segmentationMaskCtx.getImageData(
  139. 0,
  140. 0,
  141. this._options.width,
  142. this._options.height
  143. );
  144. const inputMemoryOffset = this._model._getInputMemoryOffset() / 4;
  145. for (let i = 0; i < this._segmentationPixelCount; i++) {
  146. this._model.HEAPF32[inputMemoryOffset + (i * 3)] = imageData.data[i * 4] / 255;
  147. this._model.HEAPF32[inputMemoryOffset + (i * 3) + 1] = imageData.data[(i * 4) + 1] / 255;
  148. this._model.HEAPF32[inputMemoryOffset + (i * 3) + 2] = imageData.data[(i * 4) + 2] / 255;
  149. }
  150. }
  151. /**
  152. * Checks if the local track supports this effect.
  153. *
  154. * @param {JitsiLocalTrack} jitsiLocalTrack - Track to apply effect.
  155. * @returns {boolean} - Returns true if this effect can run on the specified track
  156. * false otherwise.
  157. */
  158. isEnabled(jitsiLocalTrack: Object) {
  159. return jitsiLocalTrack.isVideoTrack() && jitsiLocalTrack.videoType === 'camera';
  160. }
  161. /**
  162. * Starts loop to capture video frame and render the segmentation mask.
  163. *
  164. * @param {MediaStream} stream - Stream to be used for processing.
  165. * @returns {MediaStream} - The stream with the applied effect.
  166. */
  167. startEffect(stream: MediaStream) {
  168. this._maskFrameTimerWorker = new Worker(timerWorkerScript, { name: 'Blur effect worker' });
  169. this._maskFrameTimerWorker.onmessage = this._onMaskFrameTimer;
  170. const firstVideoTrack = stream.getVideoTracks()[0];
  171. const { height, frameRate, width }
  172. = firstVideoTrack.getSettings ? firstVideoTrack.getSettings() : firstVideoTrack.getConstraints();
  173. this._segmentationMask = new ImageData(this._options.width, this._options.height);
  174. this._segmentationMaskCanvas = document.createElement('canvas');
  175. this._segmentationMaskCanvas.width = this._options.width;
  176. this._segmentationMaskCanvas.height = this._options.height;
  177. this._segmentationMaskCtx = this._segmentationMaskCanvas.getContext('2d');
  178. this._outputCanvasElement.width = parseInt(width, 10);
  179. this._outputCanvasElement.height = parseInt(height, 10);
  180. this._outputCanvasCtx = this._outputCanvasElement.getContext('2d');
  181. this._inputVideoElement.width = parseInt(width, 10);
  182. this._inputVideoElement.height = parseInt(height, 10);
  183. this._inputVideoElement.autoplay = true;
  184. this._inputVideoElement.srcObject = stream;
  185. this._inputVideoElement.onloadeddata = () => {
  186. this._maskFrameTimerWorker.postMessage({
  187. id: SET_TIMEOUT,
  188. timeMs: 1000 / 30
  189. });
  190. };
  191. return this._outputCanvasElement.captureStream(parseInt(frameRate, 10));
  192. }
  193. /**
  194. * Stops the capture and render loop.
  195. *
  196. * @returns {void}
  197. */
  198. stopEffect() {
  199. this._maskFrameTimerWorker.postMessage({
  200. id: CLEAR_TIMEOUT
  201. });
  202. this._maskFrameTimerWorker.terminate();
  203. }
  204. }