Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

VirtualBackgroundDialog.js 11KB

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