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

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