Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

actions.ts 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { IStore } from '../app/types';
  2. import { createVirtualBackgroundEffect } from '../stream-effects/virtual-background';
  3. import { BACKGROUND_ENABLED, SET_VIRTUAL_BACKGROUND } from './actionTypes';
  4. import { VIRTUAL_BACKGROUND_TYPE } from './constants';
  5. import logger from './logger';
  6. import { IVirtualBackground } from './reducer';
  7. /**
  8. * Signals the local participant activate the virtual background video or not.
  9. *
  10. * @param {Object} options - Represents the virtual background set options.
  11. * @param {Object} jitsiTrack - Represents the jitsi track that will have backgraund effect applied.
  12. * @returns {Promise}
  13. */
  14. export function toggleBackgroundEffect(options: IVirtualBackground, jitsiTrack: any) {
  15. return async function(dispatch: IStore['dispatch'], getState: IStore['getState']) {
  16. dispatch(backgroundEnabled(options.backgroundEffectEnabled));
  17. dispatch(setVirtualBackground(options));
  18. const state = getState();
  19. const virtualBackground = state['features/virtual-background'];
  20. if (jitsiTrack) {
  21. try {
  22. if (options.backgroundEffectEnabled) {
  23. await jitsiTrack.setEffect(await createVirtualBackgroundEffect(virtualBackground, dispatch));
  24. } else {
  25. await jitsiTrack.setEffect(undefined);
  26. dispatch(backgroundEnabled(false));
  27. }
  28. } catch (error) {
  29. dispatch(backgroundEnabled(false));
  30. logger.error('Error on apply background effect:', error);
  31. }
  32. }
  33. };
  34. }
  35. /**
  36. * Sets the selected virtual background image object.
  37. *
  38. * @param {Object} options - Represents the virtual background set options.
  39. * @returns {{
  40. * type: SET_VIRTUAL_BACKGROUND,
  41. * virtualSource: string,
  42. * blurValue: number,
  43. * type: string,
  44. * }}
  45. */
  46. export function setVirtualBackground(options?: IVirtualBackground) {
  47. return {
  48. type: SET_VIRTUAL_BACKGROUND,
  49. virtualSource: options?.virtualSource,
  50. blurValue: options?.blurValue,
  51. backgroundType: options?.backgroundType,
  52. selectedThumbnail: options?.selectedThumbnail
  53. };
  54. }
  55. /**
  56. * Signals the local participant that the background effect has been enabled.
  57. *
  58. * @param {boolean} backgroundEffectEnabled - Indicate if virtual background effect is activated.
  59. * @returns {{
  60. * type: BACKGROUND_ENABLED,
  61. * backgroundEffectEnabled: boolean
  62. * }}
  63. */
  64. export function backgroundEnabled(backgroundEffectEnabled?: boolean) {
  65. return {
  66. type: BACKGROUND_ENABLED,
  67. backgroundEffectEnabled
  68. };
  69. }
  70. /**
  71. * Simulates blurred background selection/removal on video background. Used by API only.
  72. *
  73. * @param {JitsiLocalTrack} videoTrack - The targeted video track.
  74. * @param {string} [blurType] - Blur type to apply. Accepted values are 'slight-blur', 'blur' or 'none'.
  75. * @param {boolean} muted - Muted state of the video track.
  76. * @returns {Promise}
  77. */
  78. export function toggleBlurredBackgroundEffect(videoTrack: any, blurType: 'slight-blur' | 'blur' | 'none',
  79. muted: boolean) {
  80. return async function(dispatch: IStore['dispatch'], _getState: IStore['getState']) {
  81. if (muted || !videoTrack || !blurType) {
  82. return;
  83. }
  84. if (blurType === 'none') {
  85. dispatch(toggleBackgroundEffect({
  86. backgroundEffectEnabled: false,
  87. selectedThumbnail: blurType
  88. }, videoTrack));
  89. } else {
  90. dispatch(toggleBackgroundEffect({
  91. backgroundEffectEnabled: true,
  92. backgroundType: VIRTUAL_BACKGROUND_TYPE.BLUR,
  93. blurValue: blurType === 'blur' ? 25 : 8,
  94. selectedThumbnail: blurType
  95. }, videoTrack));
  96. }
  97. };
  98. }