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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // @flow
  2. import * as wasmCheck from 'wasm-check';
  3. import { createLocalTrack } from '../../base/lib-jitsi-meet/functions';
  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. * @returns {Promise<JitsiStreamBackgroundEffect>}
  28. */
  29. export async function createVirtualBackgroundEffect(virtualBackground: Object) {
  30. if (!MediaStreamTrack.prototype.getSettings && !MediaStreamTrack.prototype.getConstraints) {
  31. throw new Error('JitsiStreamBackgroundEffect not supported!');
  32. }
  33. let tflite;
  34. let screenSharing;
  35. if (wasmCheck.feature.simd) {
  36. tflite = await createTFLiteSIMDModule();
  37. } else {
  38. tflite = await createTFLiteModule();
  39. }
  40. const modelBufferOffset = tflite._getModelBufferMemoryOffset();
  41. const modelResponse = await fetch(wasmCheck.feature.simd ? models.model144 : models.model96);
  42. if (!modelResponse.ok) {
  43. throw new Error('Failed to download tflite model!');
  44. }
  45. const model = await modelResponse.arrayBuffer();
  46. tflite.HEAPU8.set(new Uint8Array(model), modelBufferOffset);
  47. tflite._loadModel(model.byteLength);
  48. if (virtualBackground.backgroundType === 'desktop-share') {
  49. screenSharing = await createLocalTrack('desktop', '');
  50. }
  51. const options = {
  52. ...wasmCheck.feature.simd ? segmentationDimensions.model144 : segmentationDimensions.model96,
  53. virtualBackground
  54. };
  55. return new JitsiStreamBackgroundEffect(tflite, options, screenSharing);
  56. }