2021-07-20 04:19:06 +08:00
|
|
|
/* NOTICE: This file is a Derivative Work of the original component in Jitsi Meet
|
|
|
|
* See https://github.com/jitsi/jitsi-meet/tree/master/react/features/stream-effects/virtual-background/vendor.
|
|
|
|
* It is partially copied under the Apache Public License 2.0 (see https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
*/
|
|
|
|
|
2021-07-20 21:41:14 +08:00
|
|
|
import {
|
|
|
|
CLEAR_TIMEOUT,
|
|
|
|
TIMEOUT_TICK,
|
|
|
|
SET_TIMEOUT,
|
|
|
|
timerWorkerScript
|
|
|
|
} from './TimeWorker';
|
2021-07-20 04:19:06 +08:00
|
|
|
import {
|
2024-05-29 21:26:11 +08:00
|
|
|
getBasePath,
|
2021-07-20 04:19:06 +08:00
|
|
|
MODELS,
|
|
|
|
getVirtualBgImagePath,
|
|
|
|
} from '/imports/ui/services/virtual-background/service'
|
2022-06-30 21:50:53 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
2021-07-20 21:41:14 +08:00
|
|
|
|
2024-06-04 21:40:54 +08:00
|
|
|
import { simd } from 'wasm-feature-detect';
|
2023-08-16 00:02:59 +08:00
|
|
|
|
2021-07-20 21:41:14 +08:00
|
|
|
const blurValue = '25px';
|
|
|
|
|
2021-12-08 16:45:48 +08:00
|
|
|
function drawImageProp(ctx, img, x, y, w, h, offsetX, offsetY) {
|
2021-12-09 20:51:20 +08:00
|
|
|
if (arguments.length === 2) {
|
|
|
|
x = y = 0;
|
|
|
|
w = ctx.canvas.width;
|
|
|
|
h = ctx.canvas.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default offset is center
|
|
|
|
offsetX = typeof offsetX === 'number' ? offsetX : 0.5;
|
|
|
|
offsetY = typeof offsetY === 'number' ? offsetY : 0.5;
|
|
|
|
|
|
|
|
// Keep bounds [0.0, 1.0]
|
|
|
|
if (offsetX < 0) offsetX = 0;
|
|
|
|
if (offsetY < 0) offsetY = 0;
|
|
|
|
if (offsetX > 1) offsetX = 1;
|
|
|
|
if (offsetY > 1) offsetY = 1;
|
|
|
|
|
|
|
|
const iw = img.width,
|
|
|
|
ih = img.height,
|
|
|
|
r = Math.min(w / iw, h / ih);
|
|
|
|
|
|
|
|
let nw = iw * r,
|
|
|
|
nh = ih * r,
|
|
|
|
cx, cy, cw, ch, ar = 1;
|
|
|
|
|
|
|
|
// Decide which gap to fill
|
|
|
|
if (nw < w) ar = w / nw;
|
|
|
|
if (Math.abs(ar - 1) < 1e-14 && nh < h) ar = h / nh;
|
|
|
|
nw *= ar;
|
|
|
|
nh *= ar;
|
|
|
|
|
|
|
|
// Calc source rectangle
|
|
|
|
cw = iw / (nw / w);
|
|
|
|
ch = ih / (nh / h);
|
|
|
|
cx = (iw - cw) * offsetX;
|
|
|
|
cy = (ih - ch) * offsetY;
|
|
|
|
|
|
|
|
// Make sure source rectangle is valid
|
|
|
|
if (cx < 0) cx = 0;
|
|
|
|
if (cy < 0) cy = 0;
|
|
|
|
if (cw > iw) cw = iw;
|
|
|
|
if (ch > ih) ch = ih;
|
|
|
|
|
|
|
|
ctx.drawImage(img, cx, cy, cw, ch, x, y, w, h);
|
2021-12-08 16:45:48 +08:00
|
|
|
}
|
|
|
|
|
2021-07-20 21:41:14 +08:00
|
|
|
class VirtualBackgroundService {
|
|
|
|
|
|
|
|
_model;
|
|
|
|
_options;
|
|
|
|
_segmentationMask;
|
|
|
|
_inputVideoElement;
|
|
|
|
_outputCanvasElement;
|
|
|
|
_segmentationMask;
|
|
|
|
_segmentationPixelCount;
|
|
|
|
_segmentationMaskCanvas;
|
|
|
|
_segmentationMaskCtx;
|
|
|
|
_virtualImage;
|
|
|
|
_maskFrameTimerWorker;
|
|
|
|
|
|
|
|
constructor(model, options) {
|
|
|
|
this._model = model;
|
|
|
|
this._options = options;
|
2022-08-26 22:50:46 +08:00
|
|
|
this._options.brightness = 100;
|
|
|
|
this._options.wholeImageBrightness = false;
|
2021-07-20 21:41:14 +08:00
|
|
|
if (this._options.virtualBackground.backgroundType === 'image') {
|
|
|
|
this._virtualImage = document.createElement('img');
|
|
|
|
this._virtualImage.crossOrigin = 'anonymous';
|
|
|
|
this._virtualImage.src = this._options.virtualBackground.virtualSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._segmentationPixelCount = this._options.width * this._options.height;
|
|
|
|
|
|
|
|
// Bind event handler so it is only bound once for every instance.
|
|
|
|
this._onMaskFrameTimer = this._onMaskFrameTimer.bind(this);
|
|
|
|
|
|
|
|
this._outputCanvasElement = document.createElement('canvas');
|
|
|
|
this._outputCanvasElement.getContext('2d');
|
|
|
|
this._inputVideoElement = document.createElement('video');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EventHandler onmessage for the maskFrameTimerWorker WebWorker.
|
|
|
|
* @param {EventHandler} response - The onmessage EventHandler parameter.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onMaskFrameTimer(response) {
|
|
|
|
if (response.data.id === TIMEOUT_TICK) {
|
|
|
|
this._renderMask();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the run post processing.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
runPostProcessing() {
|
|
|
|
this._outputCanvasCtx.globalCompositeOperation = 'copy';
|
|
|
|
|
|
|
|
// Draw segmentation mask.
|
|
|
|
//
|
|
|
|
|
|
|
|
// Smooth out the edges.
|
|
|
|
if (this._options.virtualBackground.isVirtualBackground) {
|
|
|
|
this._outputCanvasCtx.filter = 'blur(4px)';
|
2022-08-26 22:50:46 +08:00
|
|
|
} else if (this._options.virtualBackground.backgroundType === 'blur') {
|
2021-07-20 21:41:14 +08:00
|
|
|
this._outputCanvasCtx.filter = 'blur(8px)';
|
|
|
|
}
|
|
|
|
|
|
|
|
this._outputCanvasCtx.drawImage(
|
|
|
|
this._segmentationMaskCanvas,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
this._options.width,
|
|
|
|
this._options.height,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
this._inputVideoElement.width,
|
|
|
|
this._inputVideoElement.height
|
|
|
|
);
|
|
|
|
this._outputCanvasCtx.globalCompositeOperation = 'source-in';
|
|
|
|
this._outputCanvasCtx.filter = 'none';
|
|
|
|
|
|
|
|
// Draw the foreground video.
|
|
|
|
//
|
|
|
|
|
2022-08-26 22:50:46 +08:00
|
|
|
this._outputCanvasCtx.filter = `brightness(${this._options.brightness}%)`;
|
2021-07-20 21:41:14 +08:00
|
|
|
this._outputCanvasCtx.drawImage(this._inputVideoElement, 0, 0);
|
2022-08-26 22:50:46 +08:00
|
|
|
this._outputCanvasCtx.filter = 'none';
|
2021-07-20 21:41:14 +08:00
|
|
|
|
|
|
|
// Draw the background.
|
|
|
|
//
|
|
|
|
|
2022-08-26 22:50:46 +08:00
|
|
|
if (this._options.wholeImageBrightness) {
|
|
|
|
this._outputCanvasCtx.filter = `brightness(${this._options.brightness}%)`;
|
|
|
|
}
|
|
|
|
|
2021-07-20 21:41:14 +08:00
|
|
|
this._outputCanvasCtx.globalCompositeOperation = 'destination-over';
|
|
|
|
if (this._options.virtualBackground.isVirtualBackground) {
|
2021-12-09 20:51:20 +08:00
|
|
|
drawImageProp(
|
|
|
|
this._outputCanvasCtx,
|
|
|
|
this._virtualImage,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
this._inputVideoElement.width,
|
|
|
|
this._inputVideoElement.height,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
);
|
2022-08-26 22:50:46 +08:00
|
|
|
} else if (this._options.virtualBackground.backgroundType === 'blur') {
|
2021-07-20 21:41:14 +08:00
|
|
|
this._outputCanvasCtx.filter = `blur(${blurValue})`;
|
|
|
|
this._outputCanvasCtx.drawImage(this._inputVideoElement, 0, 0);
|
2022-08-26 22:50:46 +08:00
|
|
|
} else {
|
|
|
|
this._outputCanvasCtx.drawImage(this._inputVideoElement, 0, 0);
|
2021-07-20 21:41:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the run Tensorflow Interference.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
runInference() {
|
|
|
|
this._model._runInference();
|
|
|
|
const outputMemoryOffset = this._model._getOutputMemoryOffset() / 4;
|
|
|
|
|
|
|
|
for (let i = 0; i < this._segmentationPixelCount; i++) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
this._segmentationMaskCtx.putImageData(this._segmentationMask, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loop function to render the background mask.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_renderMask() {
|
2022-06-30 21:50:53 +08:00
|
|
|
try {
|
|
|
|
this.resizeSource();
|
|
|
|
this.runInference();
|
|
|
|
this.runPostProcessing();
|
|
|
|
} catch (error) {
|
|
|
|
// TODO This is a high frequency log so that's why it's debug level.
|
|
|
|
// Should be reviewed later when the actual problem with runPostProcessing
|
|
|
|
// throwing on stalled pages/iframes - prlanzarin Jun 30 2022
|
|
|
|
logger.debug({
|
|
|
|
logCode: 'virtualbg_renderMask_failure',
|
|
|
|
extraInfo: {
|
|
|
|
errorMessage: error.message,
|
|
|
|
errorCode: error.code,
|
|
|
|
errorName: error.name,
|
|
|
|
},
|
|
|
|
}, `Virtual background renderMask failed: ${error.message || error.name}`);
|
|
|
|
}
|
2021-07-20 21:41:14 +08:00
|
|
|
|
|
|
|
this._maskFrameTimerWorker.postMessage({
|
|
|
|
id: SET_TIMEOUT,
|
|
|
|
timeMs: 1000 / 30
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the resize source process.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
resizeSource() {
|
|
|
|
this._segmentationMaskCtx.drawImage(
|
|
|
|
this._inputVideoElement,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
this._inputVideoElement.width,
|
|
|
|
this._inputVideoElement.height,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
this._options.width,
|
|
|
|
this._options.height
|
|
|
|
);
|
|
|
|
|
|
|
|
const imageData = this._segmentationMaskCtx.getImageData(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
this._options.width,
|
|
|
|
this._options.height
|
|
|
|
);
|
|
|
|
const inputMemoryOffset = this._model._getInputMemoryOffset() / 4;
|
|
|
|
|
|
|
|
for (let i = 0; i < this._segmentationPixelCount; i++) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
changeBackgroundImage(parameters = null) {
|
2021-07-20 04:19:06 +08:00
|
|
|
const virtualBackgroundImagePath = getVirtualBgImagePath();
|
2022-08-26 22:50:46 +08:00
|
|
|
let name = '';
|
2021-07-20 21:41:14 +08:00
|
|
|
let type = 'blur';
|
2022-08-26 22:50:46 +08:00
|
|
|
let isVirtualBackground = false;
|
2021-07-20 21:41:14 +08:00
|
|
|
if (parameters != null && Object.keys(parameters).length > 0) {
|
2022-08-26 22:50:46 +08:00
|
|
|
name = parameters.name;
|
2021-07-20 21:41:14 +08:00
|
|
|
type = parameters.type;
|
2022-08-26 22:50:46 +08:00
|
|
|
isVirtualBackground = parameters.isVirtualBackground;
|
|
|
|
}
|
|
|
|
this._options.virtualBackground.virtualSource = virtualBackgroundImagePath + name;
|
|
|
|
this._options.virtualBackground.backgroundType = type;
|
|
|
|
this._options.virtualBackground.isVirtualBackground = isVirtualBackground;
|
|
|
|
if (this._options.virtualBackground.backgroundType === 'image') {
|
|
|
|
this._virtualImage = document.createElement('img');
|
|
|
|
this._virtualImage.crossOrigin = 'anonymous';
|
|
|
|
this._virtualImage.src = virtualBackgroundImagePath + name;
|
2021-07-20 21:41:14 +08:00
|
|
|
}
|
2022-04-12 01:09:13 +08:00
|
|
|
if (parameters.customParams) {
|
|
|
|
this._virtualImage.src = parameters.customParams.file;
|
|
|
|
}
|
2021-07-20 21:41:14 +08: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.
|
|
|
|
*/
|
|
|
|
startEffect(stream) {
|
|
|
|
this._maskFrameTimerWorker = new Worker(timerWorkerScript, { name: 'Blur effect worker' });
|
|
|
|
this._maskFrameTimerWorker.onmessage = this._onMaskFrameTimer;
|
|
|
|
|
|
|
|
const firstVideoTrack = stream.getVideoTracks()[0];
|
|
|
|
|
|
|
|
const { height, frameRate, width }
|
|
|
|
= firstVideoTrack.getSettings ? firstVideoTrack.getSettings() : firstVideoTrack.getConstraints();
|
|
|
|
|
|
|
|
this._segmentationMask = new ImageData(this._options.width, this._options.height);
|
|
|
|
this._segmentationMaskCanvas = document.createElement('canvas');
|
|
|
|
this._segmentationMaskCanvas.width = this._options.width;
|
|
|
|
this._segmentationMaskCanvas.height = this._options.height;
|
|
|
|
this._segmentationMaskCtx = this._segmentationMaskCanvas.getContext('2d');
|
|
|
|
|
|
|
|
this._outputCanvasElement.width = parseInt(width, 10);
|
|
|
|
this._outputCanvasElement.height = parseInt(height, 10);
|
|
|
|
this._outputCanvasCtx = this._outputCanvasElement.getContext('2d');
|
|
|
|
this._inputVideoElement.width = parseInt(width, 10);
|
|
|
|
this._inputVideoElement.height = parseInt(height, 10);
|
|
|
|
this._inputVideoElement.autoplay = true;
|
|
|
|
this._inputVideoElement.srcObject = stream;
|
|
|
|
this._inputVideoElement.onloadeddata = () => {
|
|
|
|
this._maskFrameTimerWorker.postMessage({
|
|
|
|
id: SET_TIMEOUT,
|
|
|
|
timeMs: 1000 / 30
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return this._outputCanvasElement.captureStream(parseInt(frameRate, 15));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the capture and render loop.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
stopEffect() {
|
|
|
|
this._maskFrameTimerWorker.postMessage({
|
|
|
|
id: CLEAR_TIMEOUT
|
|
|
|
});
|
|
|
|
|
|
|
|
this._maskFrameTimerWorker.terminate();
|
|
|
|
}
|
|
|
|
|
2022-08-26 22:50:46 +08:00
|
|
|
|
|
|
|
set brightness(value) {
|
|
|
|
this._options.brightness = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
get brightness() {
|
|
|
|
return this._options.brightness;
|
|
|
|
}
|
|
|
|
|
|
|
|
set wholeImageBrightness(value) {
|
|
|
|
this._options.wholeImageBrightness = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
get wholeImageBrightness() {
|
|
|
|
return this._options.wholeImageBrightness;
|
|
|
|
}
|
2021-07-20 21:41:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates VirtualBackgroundService. If parameters are empty, the default
|
|
|
|
* effect is blur. Parameters (if given) must contain the following:
|
|
|
|
* isVirtualBackground (boolean) - false for blur, true for image
|
|
|
|
* backgroundType (string) - 'image' for image, anything else for blur
|
|
|
|
* backgroundFilename (string) - File name that is stored in /public/resources/images/virtual-backgrounds/
|
|
|
|
* @param {Object} parameters
|
|
|
|
* @returns {VirtualBackgroundService}
|
|
|
|
*/
|
|
|
|
export async function createVirtualBackgroundService(parameters = null) {
|
|
|
|
let tflite;
|
|
|
|
let modelResponse;
|
2023-08-16 00:02:59 +08:00
|
|
|
const simdSupported = await simd();
|
2021-07-20 21:41:14 +08:00
|
|
|
|
2024-05-29 21:26:11 +08:00
|
|
|
const BASE_PATH = getBasePath();
|
|
|
|
|
2023-08-16 00:02:59 +08:00
|
|
|
if (simdSupported) {
|
2021-09-24 01:06:42 +08:00
|
|
|
tflite = await window.createTFLiteSIMDModule();
|
2021-07-20 04:19:06 +08:00
|
|
|
modelResponse = await fetch(BASE_PATH+MODELS.model144.path);
|
2021-07-20 21:41:14 +08:00
|
|
|
} else {
|
2021-09-24 01:06:42 +08:00
|
|
|
tflite = await window.createTFLiteModule();
|
2021-07-20 04:19:06 +08:00
|
|
|
modelResponse = await fetch(BASE_PATH+MODELS.model96.path);
|
2021-07-20 21:41:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const modelBufferOffset = tflite._getModelBufferMemoryOffset();
|
2021-07-20 04:19:06 +08:00
|
|
|
const virtualBackgroundImagePath = getVirtualBgImagePath();
|
2021-07-20 21:41:14 +08:00
|
|
|
|
|
|
|
if (parameters == null) {
|
|
|
|
parameters = {};
|
|
|
|
parameters.virtualSource = virtualBackgroundImagePath + '';
|
|
|
|
parameters.backgroundType = 'blur';
|
|
|
|
parameters.isVirtualBackground = false;
|
|
|
|
} else {
|
2022-04-12 01:09:13 +08:00
|
|
|
if (parameters.customParams) {
|
|
|
|
parameters.virtualSource = parameters.customParams.file;
|
|
|
|
} else {
|
|
|
|
parameters.virtualSource = virtualBackgroundImagePath + parameters.backgroundFilename;
|
|
|
|
}
|
2021-07-20 21:41:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!modelResponse.ok) {
|
|
|
|
throw new Error('Failed to download tflite model!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const model = await modelResponse.arrayBuffer();
|
|
|
|
tflite.HEAPU8.set(new Uint8Array(model), modelBufferOffset);
|
|
|
|
tflite._loadModel(model.byteLength);
|
|
|
|
|
|
|
|
const options = {
|
2023-08-16 00:02:59 +08:00
|
|
|
... simdSupported ? MODELS.model144.segmentationDimensions : MODELS.model96.segmentationDimensions,
|
2021-07-20 21:41:14 +08:00
|
|
|
virtualBackground: parameters
|
|
|
|
};
|
|
|
|
|
|
|
|
return new VirtualBackgroundService(tflite, options);
|
|
|
|
}
|
|
|
|
|
2021-07-20 04:19:06 +08:00
|
|
|
export default VirtualBackgroundService;
|