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.

VirtualBackgroundDialog.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // @flow
  2. /* eslint-disable react/jsx-no-bind, no-return-assign */
  3. import Spinner from '@atlaskit/spinner';
  4. import { jitsiLocalStorage } from '@jitsi/js-utils/jitsi-local-storage';
  5. import React, { useState, useEffect } from 'react';
  6. import uuid from 'uuid';
  7. import { Dialog, hideDialog } from '../../base/dialog';
  8. import { translate } from '../../base/i18n';
  9. import { Icon, IconCloseSmall, IconPlusCircle, IconShareDesktop } from '../../base/icons';
  10. import { createLocalTrack } from '../../base/lib-jitsi-meet/functions';
  11. import { VIDEO_TYPE } from '../../base/media';
  12. import { connect } from '../../base/redux';
  13. import { getLocalVideoTrack } from '../../base/tracks';
  14. import { showErrorNotification } from '../../notifications';
  15. import { toggleBackgroundEffect } from '../actions';
  16. import { VIRTUAL_BACKGROUND_TYPE } from '../constants';
  17. import { resizeImage, toDataURL } from '../functions';
  18. import logger from '../logger';
  19. import VirtualBackgroundPreview from './VirtualBackgroundPreview';
  20. // The limit of virtual background uploads is 24. When the number
  21. // of uploads is 25 we trigger the deleteStoredImage function to delete
  22. // the first/oldest uploaded background.
  23. const backgroundsLimit = 25;
  24. const images = [
  25. {
  26. id: '1',
  27. src: 'images/virtual-background/background-1.jpg'
  28. },
  29. {
  30. id: '2',
  31. src: 'images/virtual-background/background-2.jpg'
  32. },
  33. {
  34. id: '3',
  35. src: 'images/virtual-background/background-3.jpg'
  36. },
  37. {
  38. id: '4',
  39. src: 'images/virtual-background/background-4.jpg'
  40. },
  41. {
  42. id: '5',
  43. src: 'images/virtual-background/background-5.jpg'
  44. },
  45. {
  46. id: '6',
  47. src: 'images/virtual-background/background-6.jpg'
  48. },
  49. {
  50. id: '7',
  51. src: 'images/virtual-background/background-7.jpg'
  52. }
  53. ];
  54. type Props = {
  55. /**
  56. * Returns the jitsi track that will have backgraund effect applied.
  57. */
  58. _jitsiTrack: Object,
  59. /**
  60. * Returns the selected thumbnail identifier.
  61. */
  62. _selectedThumbnail: string,
  63. /**
  64. * Returns the selected virtual source object.
  65. */
  66. _virtualSource: Object,
  67. /**
  68. * The redux {@code dispatch} function.
  69. */
  70. dispatch: Function,
  71. /**
  72. * Invoked to obtain translated strings.
  73. */
  74. t: Function
  75. };
  76. /**
  77. * Renders virtual background dialog.
  78. *
  79. * @returns {ReactElement}
  80. */
  81. function VirtualBackground({ _jitsiTrack, _selectedThumbnail, _virtualSource, dispatch, t }: Props) {
  82. const [ options, setOptions ] = useState({});
  83. const localImages = jitsiLocalStorage.getItem('virtualBackgrounds');
  84. const [ storedImages, setStoredImages ] = useState((localImages && JSON.parse(localImages)) || []);
  85. const [ loading, isloading ] = useState(false);
  86. const [ activeDesktopVideo ] = useState(_virtualSource?.videoType === VIDEO_TYPE.DESKTOP ? _virtualSource : null);
  87. const deleteStoredImage = image => {
  88. setStoredImages(storedImages.filter(item => item !== image));
  89. };
  90. /**
  91. * Updates stored images on local storage.
  92. */
  93. useEffect(() => {
  94. try {
  95. jitsiLocalStorage.setItem('virtualBackgrounds', JSON.stringify(storedImages));
  96. } catch (err) {
  97. // Preventing localStorage QUOTA_EXCEEDED_ERR
  98. err && deleteStoredImage(storedImages[0]);
  99. }
  100. if (storedImages.length === backgroundsLimit) {
  101. deleteStoredImage(storedImages[0]);
  102. }
  103. }, [ storedImages ]);
  104. const enableBlur = async (blurValue, selection) => {
  105. setOptions({
  106. backgroundType: VIRTUAL_BACKGROUND_TYPE.BLUR,
  107. enabled: true,
  108. blurValue,
  109. selectedThumbnail: selection
  110. });
  111. };
  112. const removeBackground = async () => {
  113. setOptions({
  114. enabled: false,
  115. selectedThumbnail: 'none'
  116. });
  117. };
  118. const shareDesktop = async selection => {
  119. const url = await createLocalTrack('desktop', '');
  120. if (!url) {
  121. dispatch(showErrorNotification({
  122. titleKey: 'virtualBackground.desktopShareError'
  123. }));
  124. logger.error('Could not create desktop share as a virtual background!');
  125. return;
  126. }
  127. setOptions({
  128. backgroundType: VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE,
  129. enabled: true,
  130. selectedThumbnail: selection,
  131. url
  132. });
  133. };
  134. const setUploadedImageBackground = async image => {
  135. setOptions({
  136. backgroundType: VIRTUAL_BACKGROUND_TYPE.IMAGE,
  137. enabled: true,
  138. url: image.src,
  139. selectedThumbnail: image.id
  140. });
  141. };
  142. const setImageBackground = async image => {
  143. const url = await toDataURL(image.src);
  144. setOptions({
  145. backgroundType: VIRTUAL_BACKGROUND_TYPE.IMAGE,
  146. enabled: true,
  147. url,
  148. selectedThumbnail: image.id
  149. });
  150. };
  151. const uploadImage = async imageFile => {
  152. const reader = new FileReader();
  153. reader.readAsDataURL(imageFile[0]);
  154. reader.onload = async () => {
  155. const url = await resizeImage(reader.result);
  156. const uuId = uuid.v4();
  157. setStoredImages([
  158. ...storedImages,
  159. {
  160. id: uuId,
  161. src: url
  162. }
  163. ]);
  164. setOptions({
  165. backgroundType: VIRTUAL_BACKGROUND_TYPE.IMAGE,
  166. enabled: true,
  167. url,
  168. selectedThumbnail: uuId
  169. });
  170. };
  171. reader.onerror = () => {
  172. isloading(false);
  173. logger.error('Failed to upload virtual image!');
  174. };
  175. };
  176. const applyVirtualBackground = async () => {
  177. if (activeDesktopVideo) {
  178. await activeDesktopVideo.dispose();
  179. }
  180. isloading(true);
  181. await dispatch(toggleBackgroundEffect(options, _jitsiTrack));
  182. await isloading(false);
  183. dispatch(hideDialog());
  184. };
  185. return (
  186. <Dialog
  187. hideCancelButton = { false }
  188. okKey = { 'virtualBackground.apply' }
  189. onSubmit = { applyVirtualBackground }
  190. submitDisabled = { !options || loading }
  191. titleKey = { 'virtualBackground.title' } >
  192. <VirtualBackgroundPreview options = { options } />
  193. {loading ? (
  194. <div className = 'virtual-background-loading'>
  195. <Spinner
  196. isCompleting = { false }
  197. size = 'medium' />
  198. </div>
  199. ) : (
  200. <div>
  201. <label
  202. className = 'file-upload-label'
  203. htmlFor = 'file-upload'>
  204. <Icon
  205. className = { 'add-background' }
  206. size = { 20 }
  207. src = { IconPlusCircle } />
  208. {t('virtualBackground.addBackground')}
  209. </label>
  210. <input
  211. accept = 'image/*'
  212. className = 'file-upload-btn'
  213. id = 'file-upload'
  214. onChange = { e => uploadImage(e.target.files) }
  215. type = 'file' />
  216. <div className = 'virtual-background-dialog'>
  217. <div
  218. className = { _selectedThumbnail === 'none' ? 'none-selected' : 'virtual-background-none' }
  219. onClick = { removeBackground }>
  220. {t('virtualBackground.none')}
  221. </div>
  222. <div
  223. className = { _selectedThumbnail === 'slight-blur'
  224. ? 'slight-blur-selected' : 'slight-blur' }
  225. onClick = { () => enableBlur(8, 'slight-blur') }>
  226. {t('virtualBackground.slightBlur')}
  227. </div>
  228. <div
  229. className = { _selectedThumbnail === 'blur' ? 'blur-selected' : 'blur' }
  230. onClick = { () => enableBlur(25, 'blur') }>
  231. {t('virtualBackground.blur')}
  232. </div>
  233. <div
  234. className = { _selectedThumbnail === 'desktop-share'
  235. ? 'desktop-share-selected'
  236. : 'desktop-share' }
  237. onClick = { () => shareDesktop('desktop-share') }>
  238. <Icon
  239. className = 'share-desktop-icon'
  240. size = { 30 }
  241. src = { IconShareDesktop } />
  242. </div>
  243. {images.map((image, index) => (
  244. <img
  245. className = {
  246. options.selectedThumbnail === image.id || _selectedThumbnail === image.id
  247. ? 'thumbnail-selected'
  248. : 'thumbnail'
  249. }
  250. key = { index }
  251. onClick = { () => setImageBackground(image) }
  252. onError = { event => event.target.style.display = 'none' }
  253. src = { image.src } />
  254. ))}
  255. {storedImages.map((image, index) => (
  256. <div
  257. className = { 'thumbnail-container' }
  258. key = { index }>
  259. <img
  260. className = { _selectedThumbnail === image.id ? 'thumbnail-selected' : 'thumbnail' }
  261. onClick = { () => setUploadedImageBackground(image) }
  262. onError = { event => event.target.style.display = 'none' }
  263. src = { image.src } />
  264. <Icon
  265. className = { 'delete-image-icon' }
  266. onClick = { () => deleteStoredImage(image) }
  267. size = { 15 }
  268. src = { IconCloseSmall } />
  269. </div>
  270. ))}
  271. </div>
  272. </div>
  273. )}
  274. </Dialog>
  275. );
  276. }
  277. /**
  278. * Maps (parts of) the redux state to the associated props for the
  279. * {@code VirtualBackground} component.
  280. *
  281. * @param {Object} state - The Redux state.
  282. * @private
  283. * @returns {{Props}}
  284. */
  285. function _mapStateToProps(state): Object {
  286. return {
  287. _virtualSource: state['features/virtual-background'].virtualSource,
  288. _selectedThumbnail: state['features/virtual-background'].selectedThumbnail,
  289. _jitsiTrack: getLocalVideoTrack(state['features/base/tracks'])?.jitsiTrack
  290. };
  291. }
  292. export default translate(connect(_mapStateToProps)(VirtualBackground));