Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

index.ts 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* eslint-disable lines-around-comment */
  2. import { IStore } from '../../app/types';
  3. import { showWarningNotification } from '../../notifications/actions';
  4. import { NOTIFICATION_TIMEOUT_TYPE } from '../../notifications/constants';
  5. import { timeout } from '../../virtual-background/functions';
  6. import logger from '../../virtual-background/logger';
  7. import JitsiStreamBackgroundEffect, { IBackgroundEffectOptions } from './JitsiStreamBackgroundEffect';
  8. // @ts-ignore
  9. import createTFLiteModule from './vendor/tflite/tflite';
  10. // @ts-ignore
  11. import createTFLiteSIMDModule from './vendor/tflite/tflite-simd';
  12. const models = {
  13. modelLandscape: 'libs/selfie_segmentation_landscape.tflite'
  14. };
  15. /* eslint-enable lines-around-comment */
  16. let modelBuffer: ArrayBuffer;
  17. let tflite: any;
  18. let wasmCheck;
  19. let isWasmDisabled = false;
  20. const segmentationDimensions = {
  21. modelLandscape: {
  22. height: 144,
  23. width: 256
  24. }
  25. };
  26. /**
  27. * Creates a new instance of JitsiStreamBackgroundEffect. This loads the Meet background model that is used to
  28. * extract person segmentation.
  29. *
  30. * @param {Object} virtualBackground - The virtual object that contains the background image source and
  31. * the isVirtualBackground flag that indicates if virtual image is activated.
  32. * @param {Function} dispatch - The Redux dispatch function.
  33. * @returns {Promise<JitsiStreamBackgroundEffect>}
  34. */
  35. export async function createVirtualBackgroundEffect(virtualBackground: IBackgroundEffectOptions['virtualBackground'],
  36. dispatch?: IStore['dispatch']) {
  37. if (!MediaStreamTrack.prototype.getSettings && !MediaStreamTrack.prototype.getConstraints) {
  38. throw new Error('JitsiStreamBackgroundEffect not supported!');
  39. }
  40. if (isWasmDisabled) {
  41. dispatch?.(showWarningNotification({
  42. titleKey: 'virtualBackground.backgroundEffectError'
  43. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  44. return;
  45. }
  46. // Checks if WebAssembly feature is supported or enabled by/in the browser.
  47. // Conditional import of wasm-check package is done to prevent
  48. // the browser from crashing when the user opens the app.
  49. if (!tflite) {
  50. try {
  51. wasmCheck = require('wasm-check');
  52. const tfliteTimeout = 10000;
  53. if (wasmCheck?.feature?.simd) {
  54. tflite = await timeout(tfliteTimeout, createTFLiteSIMDModule());
  55. } else {
  56. tflite = await timeout(tfliteTimeout, createTFLiteModule());
  57. }
  58. } catch (err: any) {
  59. if (err?.message === '408') {
  60. logger.error('Failed to download tflite model!');
  61. dispatch?.(showWarningNotification({
  62. titleKey: 'virtualBackground.backgroundEffectError'
  63. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  64. } else {
  65. isWasmDisabled = true;
  66. logger.error('Looks like WebAssembly is disabled or not supported on this browser', err);
  67. dispatch?.(showWarningNotification({
  68. titleKey: 'virtualBackground.webAssemblyWarning',
  69. descriptionKey: 'virtualBackground.webAssemblyWarningDescription'
  70. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  71. }
  72. return;
  73. }
  74. }
  75. if (!modelBuffer) {
  76. const modelResponse = await fetch(models.modelLandscape);
  77. if (!modelResponse.ok) {
  78. throw new Error('Failed to download tflite model!');
  79. }
  80. modelBuffer = await modelResponse.arrayBuffer();
  81. tflite.HEAPU8.set(new Uint8Array(modelBuffer), tflite._getModelBufferMemoryOffset());
  82. tflite._loadModel(modelBuffer.byteLength);
  83. }
  84. const options = {
  85. ...segmentationDimensions.modelLandscape,
  86. virtualBackground
  87. };
  88. return new JitsiStreamBackgroundEffect(tflite, options);
  89. }