123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589 |
- // @flow
-
- import Spinner from '@atlaskit/spinner';
- import Bourne from '@hapi/bourne';
- import { jitsiLocalStorage } from '@jitsi/js-utils/jitsi-local-storage';
- import React, { useState, useEffect, useCallback, useRef } from 'react';
- import uuid from 'uuid';
-
- import { Dialog, hideDialog, openDialog } from '../../base/dialog';
- import { translate } from '../../base/i18n';
- import { Icon, IconCloseSmall, IconPlusCircle, IconShareDesktop } from '../../base/icons';
- import { browser, JitsiTrackErrors } from '../../base/lib-jitsi-meet';
- import { createLocalTrack } from '../../base/lib-jitsi-meet/functions';
- import { VIDEO_TYPE } from '../../base/media';
- import { connect } from '../../base/redux';
- import { updateSettings } from '../../base/settings';
- import { Tooltip } from '../../base/tooltip';
- import { getLocalVideoTrack } from '../../base/tracks';
- import { showErrorNotification } from '../../notifications';
- import { toggleBackgroundEffect } from '../actions';
- import { VIRTUAL_BACKGROUND_TYPE } from '../constants';
- import { resizeImage, toDataURL } from '../functions';
- import logger from '../logger';
-
- import VirtualBackgroundPreview from './VirtualBackgroundPreview';
-
-
- type Image = {
- tooltip?: string,
- id: string,
- src: string
- }
-
- // The limit of virtual background uploads is 24. When the number
- // of uploads is 25 we trigger the deleteStoredImage function to delete
- // the first/oldest uploaded background.
- const backgroundsLimit = 25;
- const images: Array<Image> = [
- {
- tooltip: 'image1',
- id: '1',
- src: 'images/virtual-background/background-1.jpg'
- },
- {
- tooltip: 'image2',
- id: '2',
- src: 'images/virtual-background/background-2.jpg'
- },
- {
- tooltip: 'image3',
- id: '3',
- src: 'images/virtual-background/background-3.jpg'
- },
- {
- tooltip: 'image4',
- id: '4',
- src: 'images/virtual-background/background-4.jpg'
- },
- {
- tooltip: 'image5',
- id: '5',
- src: 'images/virtual-background/background-5.jpg'
- },
- {
- tooltip: 'image6',
- id: '6',
- src: 'images/virtual-background/background-6.jpg'
- },
- {
- tooltip: 'image7',
- id: '7',
- src: 'images/virtual-background/background-7.jpg'
- }
- ];
- type Props = {
-
- /**
- * The current local flip x status.
- */
- _localFlipX: boolean,
-
- /**
- * Returns the jitsi track that will have backgraund effect applied.
- */
- _jitsiTrack: Object,
-
- /**
- * Returns the selected thumbnail identifier.
- */
- _selectedThumbnail: string,
-
- /**
- * Returns the selected virtual background object.
- */
- _virtualBackground: Object,
-
- /**
- * The redux {@code dispatch} function.
- */
- dispatch: Function,
-
- /**
- * The initial options copied in the state for the {@code VirtualBackground} component.
- *
- * NOTE: currently used only for electron in order to open the dialog in the correct state after desktop sharing
- * selection.
- */
- initialOptions: Object,
-
- /**
- * Invoked to obtain translated strings.
- */
- t: Function
- };
-
- const onError = event => {
- event.target.style.display = 'none';
- };
-
-
- /**
- * Maps (parts of) the redux state to the associated props for the
- * {@code VirtualBackground} component.
- *
- * @param {Object} state - The Redux state.
- * @private
- * @returns {{Props}}
- */
- function _mapStateToProps(state): Object {
- const { localFlipX } = state['features/base/settings'];
-
- return {
- _localFlipX: Boolean(localFlipX),
- _virtualBackground: state['features/virtual-background'],
- _selectedThumbnail: state['features/virtual-background'].selectedThumbnail,
- _jitsiTrack: getLocalVideoTrack(state['features/base/tracks'])?.jitsiTrack
- };
- }
-
- const VirtualBackgroundDialog = translate(connect(_mapStateToProps)(VirtualBackground));
-
- /**
- * Renders virtual background dialog.
- *
- * @returns {ReactElement}
- */
- function VirtualBackground({
- _localFlipX,
- _jitsiTrack,
- _selectedThumbnail,
- _virtualBackground,
- dispatch,
- initialOptions,
- t
- }: Props) {
- const [ options, setOptions ] = useState({ ...initialOptions });
- const localImages = jitsiLocalStorage.getItem('virtualBackgrounds');
- const [ storedImages, setStoredImages ] = useState<Array<Image>>((localImages && Bourne.parse(localImages)) || []);
- const [ loading, setLoading ] = useState(false);
- const uploadImageButton: Object = useRef(null);
- const [ activeDesktopVideo ] = useState(_virtualBackground?.virtualSource?.videoType === VIDEO_TYPE.DESKTOP
- ? _virtualBackground.virtualSource
- : null);
- const [ initialVirtualBackground ] = useState(_virtualBackground);
- const deleteStoredImage = useCallback(e => {
- const imageId = e.currentTarget.getAttribute('data-imageid');
-
- setStoredImages(storedImages.filter(item => item.id !== imageId));
- }, [ storedImages ]);
-
- const deleteStoredImageKeyPress = useCallback(e => {
- if (e.key === ' ' || e.key === 'Enter') {
- e.preventDefault();
- deleteStoredImage(e);
- }
- }, [ deleteStoredImage ]);
-
- /**
- * Updates stored images on local storage.
- */
- useEffect(() => {
- try {
- jitsiLocalStorage.setItem('virtualBackgrounds', JSON.stringify(storedImages));
- } catch (err) {
- // Preventing localStorage QUOTA_EXCEEDED_ERR
- err && setStoredImages(storedImages.slice(1));
- }
- if (storedImages.length === backgroundsLimit) {
- setStoredImages(storedImages.slice(1));
- }
- }, [ storedImages ]);
-
-
- const enableBlur = useCallback(async () => {
- setOptions({
- backgroundType: VIRTUAL_BACKGROUND_TYPE.BLUR,
- enabled: true,
- blurValue: 25,
- selectedThumbnail: 'blur'
- });
- logger.info('"Blur" option setted for virtual background preview!');
-
- }, []);
-
- const enableBlurKeyPress = useCallback(e => {
- if (e.key === ' ' || e.key === 'Enter') {
- e.preventDefault();
- enableBlur();
- }
- }, [ enableBlur ]);
-
- const enableSlideBlur = useCallback(async () => {
- setOptions({
- backgroundType: VIRTUAL_BACKGROUND_TYPE.BLUR,
- enabled: true,
- blurValue: 8,
- selectedThumbnail: 'slight-blur'
- });
- logger.info('"Slight-blur" option setted for virtual background preview!');
-
- }, []);
-
- const enableSlideBlurKeyPress = useCallback(e => {
- if (e.key === ' ' || e.key === 'Enter') {
- e.preventDefault();
- enableSlideBlur();
- }
- }, [ enableSlideBlur ]);
-
-
- const shareDesktop = useCallback(async () => {
- let isCancelled = false, url;
-
- try {
- url = await createLocalTrack('desktop', '');
- } catch (e) {
- if (e.name === JitsiTrackErrors.SCREENSHARING_USER_CANCELED) {
- isCancelled = true;
- } else {
- logger.error(e);
- }
- }
-
- if (!url) {
- if (!isCancelled) {
- dispatch(showErrorNotification({
- titleKey: 'virtualBackground.desktopShareError'
- }));
- logger.error('Could not create desktop share as a virtual background!');
- }
-
- /**
- * For electron createLocalTrack will open the {@code DesktopPicker} dialog and hide the
- * {@code VirtualBackgroundDialog}. That's why we need to reopen the {@code VirtualBackgroundDialog}
- * and restore the current state through {@code initialOptions} prop.
- */
- if (browser.isElectron()) {
- dispatch(openDialog(VirtualBackgroundDialog, { initialOptions: options }));
- }
-
- return;
- }
-
- const newOptions = {
- backgroundType: VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE,
- enabled: true,
- selectedThumbnail: 'desktop-share',
- url
- };
-
- /**
- * For electron createLocalTrack will open the {@code DesktopPicker} dialog and hide the
- * {@code VirtualBackgroundDialog}. That's why we need to reopen the {@code VirtualBackgroundDialog}
- * and force it to show desktop share virtual background through {@code initialOptions} prop.
- */
- if (browser.isElectron()) {
- dispatch(openDialog(VirtualBackgroundDialog, { initialOptions: newOptions }));
- } else {
- setOptions(newOptions);
- logger.info('"Desktop-share" option setted for virtual background preview!');
- }
- }, [ dispatch, options ]);
-
- const shareDesktopKeyPress = useCallback(e => {
- if (e.key === ' ' || e.key === 'Enter') {
- e.preventDefault();
- shareDesktop();
- }
- }, [ shareDesktop ]);
-
- const removeBackground = useCallback(async () => {
- setOptions({
- enabled: false,
- selectedThumbnail: 'none'
- });
- logger.info('"None" option setted for virtual background preview!');
-
- }, []);
-
- const removeBackgroundKeyPress = useCallback(e => {
- if (e.key === ' ' || e.key === 'Enter') {
- e.preventDefault();
- removeBackground();
- }
- }, [ removeBackground ]);
-
- const setUploadedImageBackground = useCallback(async e => {
- const imageId = e.currentTarget.getAttribute('data-imageid');
- const image = storedImages.find(img => img.id === imageId);
-
- if (image) {
- setOptions({
- backgroundType: 'image',
- enabled: true,
- url: image.src,
- selectedThumbnail: image.id
- });
- logger.info('Uploaded image setted for virtual background preview!');
- }
- }, [ storedImages ]);
-
- const setImageBackground = useCallback(async e => {
- const imageId = e.currentTarget.getAttribute('data-imageid');
- const image = images.find(img => img.id === imageId);
-
- if (image) {
- const url = await toDataURL(image.src);
-
- setOptions({
- backgroundType: 'image',
- enabled: true,
- url,
- selectedThumbnail: image.id
- });
- logger.info('Image setted for virtual background preview!');
-
- setLoading(false);
- }
- }, []);
-
- const uploadImage = useCallback(async e => {
- const reader = new FileReader();
- const imageFile = e.target.files;
-
- reader.readAsDataURL(imageFile[0]);
- reader.onload = async () => {
- const url = await resizeImage(reader.result);
- const uuId = uuid.v4();
-
- setStoredImages([
- ...storedImages,
- {
- id: uuId,
- src: url
- }
- ]);
- setOptions({
- backgroundType: VIRTUAL_BACKGROUND_TYPE.IMAGE,
- enabled: true,
- url,
- selectedThumbnail: uuId
- });
- };
- logger.info('New virtual background image uploaded!');
-
- reader.onerror = () => {
- setLoading(false);
- logger.error('Failed to upload virtual image!');
- };
- }, [ dispatch, storedImages ]);
-
- const uploadImageKeyPress = useCallback(e => {
- if (uploadImageButton.current && (e.key === ' ' || e.key === 'Enter')) {
- e.preventDefault();
- uploadImageButton.current.click();
- }
- }, [ uploadImageButton.current ]);
-
- const setImageBackgroundKeyPress = useCallback(e => {
- if (e.key === ' ' || e.key === 'Enter') {
- e.preventDefault();
- setImageBackground(e);
- }
- }, [ setImageBackground ]);
-
- const setUploadedImageBackgroundKeyPress = useCallback(e => {
- if (e.key === ' ' || e.key === 'Enter') {
- e.preventDefault();
- setUploadedImageBackground(e);
- }
- }, [ setUploadedImageBackground ]);
-
- const applyVirtualBackground = useCallback(async () => {
- if (activeDesktopVideo) {
- await activeDesktopVideo.dispose();
- }
- setLoading(true);
- await dispatch(toggleBackgroundEffect(options, _jitsiTrack));
- await setLoading(false);
- if (_localFlipX && options.backgroundType === VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE) {
- dispatch(updateSettings({
- localFlipX: !_localFlipX
- }));
- } else {
-
- // Set x scale to default value.
- dispatch(updateSettings({
- localFlipX: true
- }));
- }
- dispatch(hideDialog());
- logger.info(`Virtual background type: '${typeof options.backgroundType === 'undefined'
- ? 'none' : options.backgroundType}' applied!`);
- }, [ dispatch, options, _localFlipX ]);
-
- // Prevent the selection of a new virtual background if it has not been applied by default
- const cancelVirtualBackground = useCallback(async () => {
- await setOptions({
- backgroundType: initialVirtualBackground.backgroundType,
- enabled: initialVirtualBackground.backgroundEffectEnabled,
- url: initialVirtualBackground.virtualSource,
- selectedThumbnail: initialVirtualBackground.selectedThumbnail,
- blurValue: initialVirtualBackground.blurValue
- });
- dispatch(hideDialog());
- });
-
- return (
- <Dialog
- hideCancelButton = { false }
- okKey = { 'virtualBackground.apply' }
- onCancel = { cancelVirtualBackground }
- onSubmit = { applyVirtualBackground }
- submitDisabled = { !options || loading }
- titleKey = { 'virtualBackground.title' } >
- <VirtualBackgroundPreview options = { options } />
- {loading ? (
- <div className = 'virtual-background-loading'>
- <Spinner
- isCompleting = { false }
- size = 'medium' />
- </div>
- ) : (
- <div>
- <label
- aria-label = { t('virtualBackground.uploadImage') }
- className = 'file-upload-label'
- htmlFor = 'file-upload'
- onKeyPress = { uploadImageKeyPress }
- tabIndex = { 0 } >
- <Icon
- className = { 'add-background' }
- size = { 20 }
- src = { IconPlusCircle } />
- {t('virtualBackground.addBackground')}
- </label>
- <input
- accept = 'image/*'
- className = 'file-upload-btn'
- id = 'file-upload'
- onChange = { uploadImage }
- ref = { uploadImageButton }
- type = 'file' />
- <div
- className = 'virtual-background-dialog'
- role = 'radiogroup'
- tabIndex = '-1'>
- <Tooltip
- content = { t('virtualBackground.removeBackground') }
- position = { 'top' }>
- <div
- aria-checked = { _selectedThumbnail === 'none' }
- aria-label = { t('virtualBackground.removeBackground') }
- className = { _selectedThumbnail === 'none' ? 'background-option none-selected'
- : 'background-option virtual-background-none' }
- onClick = { removeBackground }
- onKeyPress = { removeBackgroundKeyPress }
- role = 'radio'
- tabIndex = { 0 } >
- {t('virtualBackground.none')}
- </div>
- </Tooltip>
- <Tooltip
- content = { t('virtualBackground.slightBlur') }
- position = { 'top' }>
- <div
- aria-checked = { _selectedThumbnail === 'slight-blur' }
- aria-label = { t('virtualBackground.slightBlur') }
- className = { _selectedThumbnail === 'slight-blur'
- ? 'background-option slight-blur-selected' : 'background-option slight-blur' }
- onClick = { enableSlideBlur }
- onKeyPress = { enableSlideBlurKeyPress }
- role = 'radio'
- tabIndex = { 0 }>
- {t('virtualBackground.slightBlur')}
- </div>
- </Tooltip>
- <Tooltip
- content = { t('virtualBackground.blur') }
- position = { 'top' }>
- <div
- aria-checked = { _selectedThumbnail === 'blur' }
- aria-label = { t('virtualBackground.blur') }
- className = { _selectedThumbnail === 'blur' ? 'background-option blur-selected'
- : 'background-option blur' }
- onClick = { enableBlur }
- onKeyPress = { enableBlurKeyPress }
- role = 'radio'
- tabIndex = { 0 }>
- {t('virtualBackground.blur')}
- </div>
- </Tooltip>
- <Tooltip
- content = { t('virtualBackground.desktopShare') }
- position = { 'top' }>
- <div
- aria-checked = { _selectedThumbnail === 'desktop-share' }
- aria-label = { t('virtualBackground.desktopShare') }
- className = { _selectedThumbnail === 'desktop-share'
- ? 'background-option desktop-share-selected'
- : 'background-option desktop-share' }
- onClick = { shareDesktop }
- onKeyPress = { shareDesktopKeyPress }
- role = 'radio'
- tabIndex = { 0 }>
- <Icon
- className = 'share-desktop-icon'
- size = { 30 }
- src = { IconShareDesktop } />
- </div>
- </Tooltip>
- {images.map(image => (
- <Tooltip
- content = { image.tooltip && t(`virtualBackground.${image.tooltip}`) }
- key = { image.id }
- position = { 'top' }>
- <img
- alt = { image.tooltip && t(`virtualBackground.${image.tooltip}`) }
- aria-checked = { options.selectedThumbnail === image.id
- || _selectedThumbnail === image.id }
- className = {
- options.selectedThumbnail === image.id || _selectedThumbnail === image.id
- ? 'background-option thumbnail-selected' : 'background-option thumbnail' }
- data-imageid = { image.id }
- onClick = { setImageBackground }
- onError = { onError }
- onKeyPress = { setImageBackgroundKeyPress }
- role = 'radio'
- src = { image.src }
- tabIndex = { 0 } />
- </Tooltip>
- ))}
- {storedImages.map((image, index) => (
- <div
- className = { 'thumbnail-container' }
- key = { image.id }>
- <img
- alt = { t('virtualBackground.uploadedImage', { index: index + 1 }) }
- aria-checked = { _selectedThumbnail === image.id }
- className = { _selectedThumbnail === image.id
- ? 'background-option thumbnail-selected' : 'background-option thumbnail' }
- data-imageid = { image.id }
- onClick = { setUploadedImageBackground }
- onError = { onError }
- onKeyPress = { setUploadedImageBackgroundKeyPress }
- role = 'radio'
- src = { image.src }
- tabIndex = { 0 } />
-
- <Icon
- ariaLabel = { t('virtualBackground.deleteImage') }
- className = { 'delete-image-icon' }
- data-imageid = { image.id }
- onClick = { deleteStoredImage }
- onKeyPress = { deleteStoredImageKeyPress }
- role = 'button'
- size = { 15 }
- src = { IconCloseSmall }
- tabIndex = { 0 } />
- </div>
- ))}
- </div>
- </div>
- )}
- </Dialog>
- );
- }
-
- export default VirtualBackgroundDialog;
|