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