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

JitsiStreamBackgroundEffect.js 8.6KB

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