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.

actions.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // @flow
  2. import { createVirtualBackgroundEffect } from '../stream-effects/virtual-background';
  3. import { BACKGROUND_ENABLED, SET_VIRTUAL_BACKGROUND } from './actionTypes';
  4. import logger from './logger';
  5. /**
  6. * Signals the local participant activate the virtual background video or not.
  7. *
  8. * @param {Object} options - Represents the virtual background setted options.
  9. * @param {Object} jitsiTrack - Represents the jitsi track that will have backgraund effect applied.
  10. * @returns {Promise}
  11. */
  12. export function toggleBackgroundEffect(options: Object, jitsiTrack: Object) {
  13. return async function(dispatch: Object => Object, getState: () => any) {
  14. await dispatch(backgroundEnabled(options.enabled));
  15. await dispatch(setVirtualBackground(options));
  16. const state = getState();
  17. const virtualBackground = state['features/virtual-background'];
  18. if (jitsiTrack) {
  19. try {
  20. if (options.enabled) {
  21. await jitsiTrack.setEffect(await createVirtualBackgroundEffect(virtualBackground, dispatch));
  22. } else {
  23. await jitsiTrack.setEffect(undefined);
  24. dispatch(backgroundEnabled(false));
  25. }
  26. } catch (error) {
  27. dispatch(backgroundEnabled(false));
  28. logger.error('Error on apply background effect:', error);
  29. }
  30. }
  31. };
  32. }
  33. /**
  34. * Sets the selected virtual background image object.
  35. *
  36. * @param {Object} options - Represents the virtual background setted options.
  37. * @returns {{
  38. * type: SET_VIRTUAL_BACKGROUND,
  39. * virtualSource: string,
  40. * blurValue: number,
  41. * type: string,
  42. * }}
  43. */
  44. export function setVirtualBackground(options: Object) {
  45. return {
  46. type: SET_VIRTUAL_BACKGROUND,
  47. virtualSource: options?.url,
  48. blurValue: options?.blurValue,
  49. backgroundType: options?.backgroundType,
  50. selectedThumbnail: options?.selectedThumbnail
  51. };
  52. }
  53. /**
  54. * Signals the local participant that the background effect has been enabled.
  55. *
  56. * @param {boolean} backgroundEffectEnabled - Indicate if virtual background effect is activated.
  57. * @returns {{
  58. * type: BACKGROUND_ENABLED,
  59. * backgroundEffectEnabled: boolean
  60. * }}
  61. */
  62. export function backgroundEnabled(backgroundEffectEnabled: boolean) {
  63. return {
  64. type: BACKGROUND_ENABLED,
  65. backgroundEffectEnabled
  66. };
  67. }