You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // @flow
  2. import { showWarningNotification } from '../../notifications/actions';
  3. import logger from '../../virtual-background/logger';
  4. import JitsiStreamBackgroundEffect from './JitsiStreamBackgroundEffect';
  5. import createTFLiteModule from './vendor/tflite/tflite';
  6. import createTFLiteSIMDModule from './vendor/tflite/tflite-simd';
  7. const models = {
  8. model96: 'libs/segm_lite_v681.tflite',
  9. model144: 'libs/segm_full_v679.tflite'
  10. };
  11. const segmentationDimensions = {
  12. model96: {
  13. height: 96,
  14. width: 160
  15. },
  16. model144: {
  17. height: 144,
  18. width: 256
  19. }
  20. };
  21. /**
  22. * Creates a new instance of JitsiStreamBackgroundEffect. This loads the Meet background model that is used to
  23. * extract person segmentation.
  24. *
  25. * @param {Object} virtualBackground - The virtual object that contains the background image source and
  26. * the isVirtualBackground flag that indicates if virtual image is activated.
  27. * @param {Function} dispatch - The Redux dispatch function.
  28. * @returns {Promise<JitsiStreamBackgroundEffect>}
  29. */
  30. export async function createVirtualBackgroundEffect(virtualBackground: Object, dispatch: Function) {
  31. if (!MediaStreamTrack.prototype.getSettings && !MediaStreamTrack.prototype.getConstraints) {
  32. throw new Error('JitsiStreamBackgroundEffect not supported!');
  33. }
  34. let tflite;
  35. let wasmCheck;
  36. // Checks if WebAssembly feature is supported or enabled by/in the browser.
  37. // Conditional import of wasm-check package is done to prevent
  38. // the browser from crashing when the user opens the app.
  39. try {
  40. wasmCheck = require('wasm-check');
  41. if (wasmCheck?.feature?.simd) {
  42. tflite = await createTFLiteSIMDModule();
  43. } else {
  44. tflite = await createTFLiteModule();
  45. }
  46. } catch (err) {
  47. logger.error('Looks like WebAssembly is disabled or not supported on this browser');
  48. dispatch(showWarningNotification({
  49. titleKey: 'virtualBackground.webAssemblyWarning',
  50. description: 'WebAssembly disabled or not supported by this browser'
  51. }));
  52. return;
  53. }
  54. const modelBufferOffset = tflite._getModelBufferMemoryOffset();
  55. const modelResponse = await fetch(wasmCheck.feature.simd ? models.model144 : models.model96);
  56. if (!modelResponse.ok) {
  57. throw new Error('Failed to download tflite model!');
  58. }
  59. const model = await modelResponse.arrayBuffer();
  60. tflite.HEAPU8.set(new Uint8Array(model), modelBufferOffset);
  61. tflite._loadModel(model.byteLength);
  62. const options = {
  63. ...wasmCheck.feature.simd ? segmentationDimensions.model144 : segmentationDimensions.model96,
  64. virtualBackground
  65. };
  66. return new JitsiStreamBackgroundEffect(tflite, options);
  67. }