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.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // @flow
  2. import { createVirtualBackgroundEffect } from '../stream-effects/virtual-background';
  3. import { BACKGROUND_ENABLED, SET_VIRTUAL_BACKGROUND, VIRTUAL_BACKGROUND_TRACK_CHANGED } 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. }
  68. /**
  69. * Signals if the local track was changed due to a changes of the virtual background.
  70. *
  71. * @returns {{
  72. * type: VIRTUAL_BACKGROUND_TRACK_CHANGED
  73. *}}.
  74. */
  75. export function virtualBackgroundTrackChanged() {
  76. return {
  77. type: VIRTUAL_BACKGROUND_TRACK_CHANGED
  78. };
  79. }