2019-12-17 21:08:39 +00:00
|
|
|
// @flow
|
2021-05-13 14:38:23 +00:00
|
|
|
|
2021-05-26 11:53:14 +00:00
|
|
|
import { VIRTUAL_BACKGROUND_TYPE } from '../../virtual-background/constants';
|
|
|
|
|
2019-07-03 15:38:25 +00:00
|
|
|
import {
|
2020-09-17 16:25:06 +00:00
|
|
|
CLEAR_TIMEOUT,
|
|
|
|
TIMEOUT_TICK,
|
|
|
|
SET_TIMEOUT,
|
2019-07-03 15:38:25 +00:00
|
|
|
timerWorkerScript
|
|
|
|
} from './TimerWorker';
|
2021-02-17 15:03:33 +00:00
|
|
|
|
2019-07-03 15:38:25 +00:00
|
|
|
/**
|
2021-02-18 15:52:47 +00:00
|
|
|
* Represents a modified MediaStream that adds effects to video background.
|
|
|
|
* <tt>JitsiStreamBackgroundEffect</tt> does the processing of the original
|
2019-07-03 15:38:25 +00:00
|
|
|
* video stream.
|
|
|
|
*/
|
2021-02-18 15:52:47 +00:00
|
|
|
export default class JitsiStreamBackgroundEffect {
|
2021-02-17 15:03:33 +00:00
|
|
|
_model: Object;
|
2021-02-25 12:21:03 +00:00
|
|
|
_options: Object;
|
2021-06-24 15:21:34 +00:00
|
|
|
_stream: Object;
|
2021-02-25 12:21:03 +00:00
|
|
|
_segmentationPixelCount: number;
|
2019-12-17 21:08:39 +00:00
|
|
|
_inputVideoElement: HTMLVideoElement;
|
|
|
|
_onMaskFrameTimer: Function;
|
|
|
|
_maskFrameTimerWorker: Worker;
|
|
|
|
_outputCanvasElement: HTMLCanvasElement;
|
2021-02-17 15:03:33 +00:00
|
|
|
_outputCanvasCtx: Object;
|
|
|
|
_segmentationMaskCtx: Object;
|
|
|
|
_segmentationMask: Object;
|
|
|
|
_segmentationMaskCanvas: Object;
|
2019-12-17 21:08:39 +00:00
|
|
|
_renderMask: Function;
|
2021-03-16 09:03:56 +00:00
|
|
|
_virtualImage: HTMLImageElement;
|
2021-05-13 14:38:23 +00:00
|
|
|
_virtualVideo: HTMLVideoElement;
|
2019-12-17 21:08:39 +00:00
|
|
|
isEnabled: Function;
|
|
|
|
startEffect: Function;
|
|
|
|
stopEffect: Function;
|
|
|
|
|
2019-07-03 15:38:25 +00:00
|
|
|
/**
|
|
|
|
* Represents a modified video MediaStream track.
|
|
|
|
*
|
|
|
|
* @class
|
2021-02-25 12:21:03 +00:00
|
|
|
* @param {Object} model - Meet model.
|
|
|
|
* @param {Object} options - Segmentation dimensions.
|
2019-07-03 15:38:25 +00:00
|
|
|
*/
|
2021-05-14 14:53:11 +00:00
|
|
|
constructor(model: Object, options: Object) {
|
2021-02-18 15:52:47 +00:00
|
|
|
this._options = options;
|
|
|
|
|
2021-05-26 11:53:14 +00:00
|
|
|
if (this._options.virtualBackground.backgroundType === VIRTUAL_BACKGROUND_TYPE.IMAGE) {
|
2021-03-16 09:03:56 +00:00
|
|
|
this._virtualImage = document.createElement('img');
|
2021-03-17 15:10:37 +00:00
|
|
|
this._virtualImage.crossOrigin = 'anonymous';
|
2021-03-16 09:03:56 +00:00
|
|
|
this._virtualImage.src = this._options.virtualBackground.virtualSource;
|
2021-02-18 15:52:47 +00:00
|
|
|
}
|
2021-05-26 11:53:14 +00:00
|
|
|
if (this._options.virtualBackground.backgroundType === VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE) {
|
2021-05-13 14:38:23 +00:00
|
|
|
this._virtualVideo = document.createElement('video');
|
|
|
|
this._virtualVideo.autoplay = true;
|
2021-05-14 14:53:11 +00:00
|
|
|
this._virtualVideo.srcObject = this._options?.virtualBackground?.virtualSource?.stream;
|
2021-05-13 14:38:23 +00:00
|
|
|
}
|
2021-02-25 12:21:03 +00:00
|
|
|
this._model = model;
|
|
|
|
this._segmentationPixelCount = this._options.width * this._options.height;
|
2019-07-03 15:38:25 +00:00
|
|
|
|
|
|
|
// Bind event handler so it is only bound once for every instance.
|
|
|
|
this._onMaskFrameTimer = this._onMaskFrameTimer.bind(this);
|
2019-07-08 14:37:15 +00:00
|
|
|
|
|
|
|
// Workaround for FF issue https://bugzilla.mozilla.org/show_bug.cgi?id=1388974
|
2019-12-17 21:08:39 +00:00
|
|
|
this._outputCanvasElement = document.createElement('canvas');
|
2019-07-08 14:37:15 +00:00
|
|
|
this._outputCanvasElement.getContext('2d');
|
2019-07-03 15:38:25 +00:00
|
|
|
this._inputVideoElement = document.createElement('video');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-17 21:08:39 +00:00
|
|
|
* EventHandler onmessage for the maskFrameTimerWorker WebWorker.
|
2019-07-03 15:38:25 +00:00
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {EventHandler} response - The onmessage EventHandler parameter.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2021-04-09 12:17:06 +00:00
|
|
|
_onMaskFrameTimer(response: Object) {
|
2020-09-17 16:25:06 +00:00
|
|
|
if (response.data.id === TIMEOUT_TICK) {
|
2021-04-09 12:17:06 +00:00
|
|
|
this._renderMask();
|
2019-07-03 15:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-17 15:03:33 +00:00
|
|
|
* Represents the run post processing.
|
2019-07-03 15:38:25 +00:00
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2021-02-17 15:03:33 +00:00
|
|
|
runPostProcessing() {
|
2021-06-24 15:21:34 +00:00
|
|
|
|
|
|
|
const track = this._stream.getVideoTracks()[0];
|
|
|
|
const { height, width } = track.getSettings() ?? track.getConstraints();
|
2021-07-08 17:20:12 +00:00
|
|
|
const { backgroundType } = this._options.virtualBackground;
|
2021-06-24 15:21:34 +00:00
|
|
|
|
|
|
|
this._outputCanvasElement.height = height;
|
|
|
|
this._outputCanvasElement.width = width;
|
2021-02-17 15:03:33 +00:00
|
|
|
this._outputCanvasCtx.globalCompositeOperation = 'copy';
|
2020-09-17 16:25:06 +00:00
|
|
|
|
2021-02-17 15:03:33 +00:00
|
|
|
// Draw segmentation mask.
|
2021-03-12 13:41:12 +00:00
|
|
|
|
|
|
|
// Smooth out the edges.
|
2021-07-14 15:23:40 +00:00
|
|
|
this._outputCanvasCtx.filter = backgroundType === VIRTUAL_BACKGROUND_TYPE.IMAGE ? 'blur(4px)' : 'blur(8px)';
|
2021-07-08 17:20:12 +00:00
|
|
|
if (backgroundType === VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE) {
|
|
|
|
// Save current context before applying transformations.
|
|
|
|
this._outputCanvasCtx.save();
|
2021-03-12 13:41:12 +00:00
|
|
|
|
2021-07-08 17:20:12 +00:00
|
|
|
// Flip the canvas and prevent mirror behaviour.
|
|
|
|
this._outputCanvasCtx.scale(-1, 1);
|
|
|
|
this._outputCanvasCtx.translate(-this._outputCanvasElement.width, 0);
|
|
|
|
}
|
2021-02-17 15:03:33 +00:00
|
|
|
this._outputCanvasCtx.drawImage(
|
|
|
|
this._segmentationMaskCanvas,
|
|
|
|
0,
|
2020-09-17 16:25:06 +00:00
|
|
|
0,
|
2021-02-25 12:21:03 +00:00
|
|
|
this._options.width,
|
|
|
|
this._options.height,
|
2020-09-17 16:25:06 +00:00
|
|
|
0,
|
2021-02-17 15:03:33 +00:00
|
|
|
0,
|
|
|
|
this._inputVideoElement.width,
|
|
|
|
this._inputVideoElement.height
|
2019-12-17 21:08:39 +00:00
|
|
|
);
|
2021-07-08 17:20:12 +00:00
|
|
|
if (backgroundType === VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE) {
|
|
|
|
this._outputCanvasCtx.restore();
|
|
|
|
}
|
2021-02-17 15:03:33 +00:00
|
|
|
this._outputCanvasCtx.globalCompositeOperation = 'source-in';
|
|
|
|
this._outputCanvasCtx.filter = 'none';
|
2021-03-12 13:41:12 +00:00
|
|
|
|
|
|
|
// Draw the foreground video.
|
2021-07-08 17:20:12 +00:00
|
|
|
if (backgroundType === VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE) {
|
|
|
|
// Save current context before applying transformations.
|
|
|
|
this._outputCanvasCtx.save();
|
2021-03-12 13:41:12 +00:00
|
|
|
|
2021-07-08 17:20:12 +00:00
|
|
|
// Flip the canvas and prevent mirror behaviour.
|
|
|
|
this._outputCanvasCtx.scale(-1, 1);
|
|
|
|
this._outputCanvasCtx.translate(-this._outputCanvasElement.width, 0);
|
|
|
|
}
|
2021-02-17 15:03:33 +00:00
|
|
|
this._outputCanvasCtx.drawImage(this._inputVideoElement, 0, 0);
|
2021-07-08 17:20:12 +00:00
|
|
|
if (backgroundType === VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE) {
|
|
|
|
this._outputCanvasCtx.restore();
|
|
|
|
}
|
2020-09-17 16:25:06 +00:00
|
|
|
|
2021-03-12 13:41:12 +00:00
|
|
|
// Draw the background.
|
|
|
|
|
2021-02-17 15:03:33 +00:00
|
|
|
this._outputCanvasCtx.globalCompositeOperation = 'destination-over';
|
2021-07-08 17:20:12 +00:00
|
|
|
if (backgroundType === VIRTUAL_BACKGROUND_TYPE.IMAGE
|
|
|
|
|| backgroundType === VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE) {
|
2021-03-24 16:32:45 +00:00
|
|
|
this._outputCanvasCtx.drawImage(
|
2021-07-08 17:20:12 +00:00
|
|
|
backgroundType === VIRTUAL_BACKGROUND_TYPE.IMAGE
|
|
|
|
? this._virtualImage : this._virtualVideo,
|
2021-05-13 14:38:23 +00:00
|
|
|
0,
|
2021-05-14 15:25:00 +00:00
|
|
|
0,
|
2021-06-24 15:21:34 +00:00
|
|
|
this._outputCanvasElement.width,
|
|
|
|
this._outputCanvasElement.height
|
2021-05-13 14:38:23 +00:00
|
|
|
);
|
2021-02-18 15:52:47 +00:00
|
|
|
} else {
|
2021-04-09 12:17:06 +00:00
|
|
|
this._outputCanvasCtx.filter = `blur(${this._options.virtualBackground.blurValue}px)`;
|
2021-02-18 15:52:47 +00:00
|
|
|
this._outputCanvasCtx.drawImage(this._inputVideoElement, 0, 0);
|
|
|
|
}
|
2021-02-17 15:03:33 +00:00
|
|
|
}
|
2020-09-17 16:25:06 +00:00
|
|
|
|
2021-02-17 15:03:33 +00:00
|
|
|
/**
|
|
|
|
* Represents the run Tensorflow Interference.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
runInference() {
|
|
|
|
this._model._runInference();
|
|
|
|
const outputMemoryOffset = this._model._getOutputMemoryOffset() / 4;
|
|
|
|
|
2021-02-25 12:21:03 +00:00
|
|
|
for (let i = 0; i < this._segmentationPixelCount; i++) {
|
2021-02-17 15:03:33 +00:00
|
|
|
const background = this._model.HEAPF32[outputMemoryOffset + (i * 2)];
|
|
|
|
const person = this._model.HEAPF32[outputMemoryOffset + (i * 2) + 1];
|
|
|
|
const shift = Math.max(background, person);
|
|
|
|
const backgroundExp = Math.exp(background - shift);
|
|
|
|
const personExp = Math.exp(person - shift);
|
|
|
|
|
|
|
|
// Sets only the alpha component of each pixel.
|
|
|
|
this._segmentationMask.data[(i * 4) + 3] = (255 * personExp) / (backgroundExp + personExp);
|
2020-09-17 16:25:06 +00:00
|
|
|
}
|
2021-02-17 15:03:33 +00:00
|
|
|
this._segmentationMaskCtx.putImageData(this._segmentationMask, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loop function to render the background mask.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_renderMask() {
|
|
|
|
this.resizeSource();
|
|
|
|
this.runInference();
|
|
|
|
this.runPostProcessing();
|
|
|
|
|
2020-09-17 16:25:06 +00:00
|
|
|
this._maskFrameTimerWorker.postMessage({
|
|
|
|
id: SET_TIMEOUT,
|
|
|
|
timeMs: 1000 / 30
|
|
|
|
});
|
2019-12-17 21:08:39 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 15:03:33 +00:00
|
|
|
/**
|
|
|
|
* Represents the resize source process.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
resizeSource() {
|
|
|
|
this._segmentationMaskCtx.drawImage(
|
|
|
|
this._inputVideoElement,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
this._inputVideoElement.width,
|
|
|
|
this._inputVideoElement.height,
|
|
|
|
0,
|
|
|
|
0,
|
2021-02-25 12:21:03 +00:00
|
|
|
this._options.width,
|
|
|
|
this._options.height
|
2021-02-17 15:03:33 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const imageData = this._segmentationMaskCtx.getImageData(
|
|
|
|
0,
|
|
|
|
0,
|
2021-02-25 12:21:03 +00:00
|
|
|
this._options.width,
|
|
|
|
this._options.height
|
2021-02-17 15:03:33 +00:00
|
|
|
);
|
|
|
|
const inputMemoryOffset = this._model._getInputMemoryOffset() / 4;
|
|
|
|
|
2021-02-25 12:21:03 +00:00
|
|
|
for (let i = 0; i < this._segmentationPixelCount; i++) {
|
2021-02-17 15:03:33 +00:00
|
|
|
this._model.HEAPF32[inputMemoryOffset + (i * 3)] = imageData.data[i * 4] / 255;
|
|
|
|
this._model.HEAPF32[inputMemoryOffset + (i * 3) + 1] = imageData.data[(i * 4) + 1] / 255;
|
|
|
|
this._model.HEAPF32[inputMemoryOffset + (i * 3) + 2] = imageData.data[(i * 4) + 2] / 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 21:08:39 +00:00
|
|
|
/**
|
|
|
|
* Checks if the local track supports this effect.
|
|
|
|
*
|
|
|
|
* @param {JitsiLocalTrack} jitsiLocalTrack - Track to apply effect.
|
|
|
|
* @returns {boolean} - Returns true if this effect can run on the specified track
|
|
|
|
* false otherwise.
|
|
|
|
*/
|
|
|
|
isEnabled(jitsiLocalTrack: Object) {
|
|
|
|
return jitsiLocalTrack.isVideoTrack() && jitsiLocalTrack.videoType === 'camera';
|
2019-07-03 15:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts loop to capture video frame and render the segmentation mask.
|
|
|
|
*
|
|
|
|
* @param {MediaStream} stream - Stream to be used for processing.
|
|
|
|
* @returns {MediaStream} - The stream with the applied effect.
|
|
|
|
*/
|
2019-12-17 21:08:39 +00:00
|
|
|
startEffect(stream: MediaStream) {
|
2021-06-24 15:21:34 +00:00
|
|
|
this._stream = stream;
|
2020-07-22 14:37:17 +00:00
|
|
|
this._maskFrameTimerWorker = new Worker(timerWorkerScript, { name: 'Blur effect worker' });
|
|
|
|
this._maskFrameTimerWorker.onmessage = this._onMaskFrameTimer;
|
2021-06-24 15:21:34 +00:00
|
|
|
const firstVideoTrack = this._stream.getVideoTracks()[0];
|
2019-07-03 15:38:25 +00:00
|
|
|
const { height, frameRate, width }
|
|
|
|
= firstVideoTrack.getSettings ? firstVideoTrack.getSettings() : firstVideoTrack.getConstraints();
|
|
|
|
|
2021-02-25 12:21:03 +00:00
|
|
|
this._segmentationMask = new ImageData(this._options.width, this._options.height);
|
2021-02-17 15:03:33 +00:00
|
|
|
this._segmentationMaskCanvas = document.createElement('canvas');
|
2021-02-25 12:21:03 +00:00
|
|
|
this._segmentationMaskCanvas.width = this._options.width;
|
|
|
|
this._segmentationMaskCanvas.height = this._options.height;
|
2021-02-17 15:03:33 +00:00
|
|
|
this._segmentationMaskCtx = this._segmentationMaskCanvas.getContext('2d');
|
2021-02-18 15:52:47 +00:00
|
|
|
|
2019-12-17 21:08:39 +00:00
|
|
|
this._outputCanvasElement.width = parseInt(width, 10);
|
|
|
|
this._outputCanvasElement.height = parseInt(height, 10);
|
2021-02-17 15:03:33 +00:00
|
|
|
this._outputCanvasCtx = this._outputCanvasElement.getContext('2d');
|
2019-12-17 21:08:39 +00:00
|
|
|
this._inputVideoElement.width = parseInt(width, 10);
|
|
|
|
this._inputVideoElement.height = parseInt(height, 10);
|
2019-07-03 15:38:25 +00:00
|
|
|
this._inputVideoElement.autoplay = true;
|
2021-06-24 15:21:34 +00:00
|
|
|
this._inputVideoElement.srcObject = this._stream;
|
2019-12-17 21:08:39 +00:00
|
|
|
this._inputVideoElement.onloadeddata = () => {
|
|
|
|
this._maskFrameTimerWorker.postMessage({
|
2020-09-17 16:25:06 +00:00
|
|
|
id: SET_TIMEOUT,
|
|
|
|
timeMs: 1000 / 30
|
2019-12-17 21:08:39 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return this._outputCanvasElement.captureStream(parseInt(frameRate, 10));
|
2019-07-03 15:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the capture and render loop.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
stopEffect() {
|
|
|
|
this._maskFrameTimerWorker.postMessage({
|
2020-09-17 16:25:06 +00:00
|
|
|
id: CLEAR_TIMEOUT
|
2019-07-03 15:38:25 +00:00
|
|
|
});
|
2020-07-22 14:37:17 +00:00
|
|
|
|
|
|
|
this._maskFrameTimerWorker.terminate();
|
2019-07-03 15:38:25 +00:00
|
|
|
}
|
|
|
|
}
|