2022-10-04 10:52:09 +00:00
|
|
|
import { IStore } from '../app/types';
|
|
|
|
// eslint-disable-next-line lines-around-comment
|
|
|
|
// @ts-ignore
|
2021-03-12 15:50:57 +00:00
|
|
|
import { createVirtualBackgroundEffect } from '../stream-effects/virtual-background';
|
2021-02-18 15:52:47 +00:00
|
|
|
|
2022-10-07 21:15:48 +00:00
|
|
|
import { BACKGROUND_ENABLED, SET_VIRTUAL_BACKGROUND } from './actionTypes';
|
2021-02-18 15:52:47 +00:00
|
|
|
import logger from './logger';
|
2022-10-20 09:11:27 +00:00
|
|
|
import { IVirtualBackgroundOptions } from './types';
|
2021-02-18 15:52:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Signals the local participant activate the virtual background video or not.
|
|
|
|
*
|
2022-07-14 07:10:08 +00:00
|
|
|
* @param {Object} options - Represents the virtual background set options.
|
2021-05-06 06:54:23 +00:00
|
|
|
* @param {Object} jitsiTrack - Represents the jitsi track that will have backgraund effect applied.
|
2021-02-18 15:52:47 +00:00
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2022-10-20 09:11:27 +00:00
|
|
|
export function toggleBackgroundEffect(options: IVirtualBackgroundOptions, jitsiTrack: any) {
|
2022-10-04 10:52:09 +00:00
|
|
|
return async function(dispatch: IStore['dispatch'], getState: IStore['getState']) {
|
2021-04-09 12:17:06 +00:00
|
|
|
await dispatch(backgroundEnabled(options.enabled));
|
|
|
|
await dispatch(setVirtualBackground(options));
|
2021-02-18 15:52:47 +00:00
|
|
|
const state = getState();
|
|
|
|
const virtualBackground = state['features/virtual-background'];
|
|
|
|
|
2021-05-06 06:54:23 +00:00
|
|
|
if (jitsiTrack) {
|
|
|
|
try {
|
|
|
|
if (options.enabled) {
|
2021-06-17 14:03:39 +00:00
|
|
|
await jitsiTrack.setEffect(await createVirtualBackgroundEffect(virtualBackground, dispatch));
|
2021-05-06 06:54:23 +00:00
|
|
|
} else {
|
|
|
|
await jitsiTrack.setEffect(undefined);
|
|
|
|
dispatch(backgroundEnabled(false));
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2021-02-18 15:52:47 +00:00
|
|
|
dispatch(backgroundEnabled(false));
|
2021-05-06 06:54:23 +00:00
|
|
|
logger.error('Error on apply background effect:', error);
|
2021-02-18 15:52:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the selected virtual background image object.
|
|
|
|
*
|
2022-07-14 07:10:08 +00:00
|
|
|
* @param {Object} options - Represents the virtual background set options.
|
2021-02-18 15:52:47 +00:00
|
|
|
* @returns {{
|
|
|
|
* type: SET_VIRTUAL_BACKGROUND,
|
|
|
|
* virtualSource: string,
|
2021-04-09 12:17:06 +00:00
|
|
|
* blurValue: number,
|
|
|
|
* type: string,
|
2021-02-18 15:52:47 +00:00
|
|
|
* }}
|
|
|
|
*/
|
2022-10-20 09:11:27 +00:00
|
|
|
export function setVirtualBackground(options?: IVirtualBackgroundOptions) {
|
2021-02-18 15:52:47 +00:00
|
|
|
return {
|
|
|
|
type: SET_VIRTUAL_BACKGROUND,
|
2021-04-09 12:17:06 +00:00
|
|
|
virtualSource: options?.url,
|
|
|
|
blurValue: options?.blurValue,
|
2021-04-09 14:25:26 +00:00
|
|
|
backgroundType: options?.backgroundType,
|
|
|
|
selectedThumbnail: options?.selectedThumbnail
|
2021-02-18 15:52:47 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Signals the local participant that the background effect has been enabled.
|
|
|
|
*
|
|
|
|
* @param {boolean} backgroundEffectEnabled - Indicate if virtual background effect is activated.
|
|
|
|
* @returns {{
|
|
|
|
* type: BACKGROUND_ENABLED,
|
2021-04-09 12:17:06 +00:00
|
|
|
* backgroundEffectEnabled: boolean
|
2021-02-18 15:52:47 +00:00
|
|
|
* }}
|
|
|
|
*/
|
|
|
|
export function backgroundEnabled(backgroundEffectEnabled: boolean) {
|
|
|
|
return {
|
|
|
|
type: BACKGROUND_ENABLED,
|
|
|
|
backgroundEffectEnabled
|
|
|
|
};
|
|
|
|
}
|