fix(virtual-backgrounds) cache loaded model

There is no need to fetch and apply the model every time we want to
createe the effect since it doesn't change.
This commit is contained in:
Saúl Ibarra Corretgé 2022-08-15 13:02:47 +02:00 committed by Saúl Ibarra Corretgé
parent f412ff7f11
commit 0bfa1027a9
1 changed files with 21 additions and 19 deletions

View File

@ -1,5 +1,3 @@
// @flow
import { NOTIFICATION_TIMEOUT_TYPE } from '../../notifications';
import { showWarningNotification } from '../../notifications/actions';
import { timeout } from '../../virtual-background/functions';
@ -12,6 +10,7 @@ const models = {
modelLandscape: 'libs/selfie_segmentation_landscape.tflite'
};
let modelBuffer;
let tflite;
let wasmCheck;
let isWasmDisabled = false;
@ -37,11 +36,19 @@ export async function createVirtualBackgroundEffect(virtualBackground: Object, d
throw new Error('JitsiStreamBackgroundEffect not supported!');
}
if (isWasmDisabled) {
dispatch(showWarningNotification({
titleKey: 'virtualBackground.backgroundEffectError'
}, NOTIFICATION_TIMEOUT_TYPE.LONG));
return;
}
// Checks if WebAssembly feature is supported or enabled by/in the browser.
// Conditional import of wasm-check package is done to prevent
// the browser from crashing when the user opens the app.
if (!tflite && !isWasmDisabled) {
if (!tflite) {
try {
wasmCheck = require('wasm-check');
const tfliteTimeout = 10000;
@ -68,26 +75,21 @@ export async function createVirtualBackgroundEffect(virtualBackground: Object, d
return;
}
} else if (isWasmDisabled) {
dispatch(showWarningNotification({
titleKey: 'virtualBackground.backgroundEffectError'
}, NOTIFICATION_TIMEOUT_TYPE.LONG));
return;
}
const modelBufferOffset = tflite._getModelBufferMemoryOffset();
if (!modelBuffer) {
const modelResponse = await fetch(models.modelLandscape);
if (!modelResponse.ok) {
throw new Error('Failed to download tflite model!');
}
const model = await modelResponse.arrayBuffer();
modelBuffer = await modelResponse.arrayBuffer();
tflite.HEAPU8.set(new Uint8Array(model), modelBufferOffset);
tflite.HEAPU8.set(new Uint8Array(modelBuffer), tflite._getModelBufferMemoryOffset());
tflite._loadModel(model.byteLength);
tflite._loadModel(modelBuffer.byteLength);
}
const options = {
...segmentationDimensions.modelLandscape,