Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

VideoSettingsContent.tsx 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import React, { Component } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { connect } from 'react-redux';
  4. import { IReduxState, IStore } from '../../../../app/types';
  5. import { openDialog } from '../../../../base/dialog/actions';
  6. import { translate } from '../../../../base/i18n/functions';
  7. import { IconImage } from '../../../../base/icons/svg';
  8. import Video from '../../../../base/media/components/Video.web';
  9. import { equals } from '../../../../base/redux/functions';
  10. import { updateSettings } from '../../../../base/settings/actions';
  11. import Checkbox from '../../../../base/ui/components/web/Checkbox';
  12. import ContextMenu from '../../../../base/ui/components/web/ContextMenu';
  13. import ContextMenuItem from '../../../../base/ui/components/web/ContextMenuItem';
  14. import ContextMenuItemGroup from '../../../../base/ui/components/web/ContextMenuItemGroup';
  15. import VirtualBackgroundDialog from '../../../../virtual-background/components/VirtualBackgroundDialog';
  16. import { createLocalVideoTracks } from '../../../functions.web';
  17. const videoClassName = 'video-preview-video flipVideoX';
  18. /**
  19. * The type of the React {@code Component} props of {@link VideoSettingsContent}.
  20. */
  21. export interface IProps extends WithTranslation {
  22. /**
  23. * Callback to change the flip state.
  24. */
  25. changeFlip: (flip: boolean) => void;
  26. /**
  27. * The deviceId of the camera device currently being used.
  28. */
  29. currentCameraDeviceId: string;
  30. /**
  31. * Whether or not the local video is flipped.
  32. */
  33. localFlipX: boolean;
  34. /**
  35. * Open virtual background dialog.
  36. */
  37. selectBackground: () => void;
  38. /**
  39. * Callback invoked to change current camera.
  40. */
  41. setVideoInputDevice: Function;
  42. /**
  43. * Callback invoked to toggle the settings popup visibility.
  44. */
  45. toggleVideoSettings: Function;
  46. /**
  47. * All the camera device ids currently connected.
  48. */
  49. videoDeviceIds: string[];
  50. }
  51. /**
  52. * The type of the React {@code Component} state of {@link VideoSettingsContent}.
  53. */
  54. type State = {
  55. /**
  56. * An array of all the jitsiTracks and eventual errors.
  57. */
  58. trackData: { deviceId: string; error?: string; jitsiTrack: any | null; }[];
  59. };
  60. /**
  61. * Implements a React {@link Component} which displays a list of video
  62. * previews to choose from.
  63. *
  64. * @augments Component
  65. */
  66. class VideoSettingsContent extends Component<IProps, State> {
  67. _componentWasUnmounted: boolean;
  68. /**
  69. * Initializes a new {@code VideoSettingsContent} instance.
  70. *
  71. * @param {Object} props - The read-only properties with which the new
  72. * instance is to be initialized.
  73. */
  74. constructor(props: IProps) {
  75. super(props);
  76. this._onToggleFlip = this._onToggleFlip.bind(this);
  77. this.state = {
  78. trackData: new Array(props.videoDeviceIds.length).fill({
  79. jitsiTrack: null
  80. })
  81. };
  82. }
  83. /**
  84. * Toggles local video flip state.
  85. *
  86. * @returns {void}
  87. */
  88. _onToggleFlip() {
  89. const { localFlipX, changeFlip } = this.props;
  90. changeFlip(!localFlipX);
  91. }
  92. /**
  93. * Creates and updates the track data.
  94. *
  95. * @returns {void}
  96. */
  97. async _setTracks() {
  98. this._disposeTracks(this.state.trackData);
  99. const trackData = await createLocalVideoTracks(this.props.videoDeviceIds, 5000);
  100. // In case the component gets unmounted before the tracks are created
  101. // avoid a leak by not setting the state
  102. if (this._componentWasUnmounted) {
  103. this._disposeTracks(trackData);
  104. } else {
  105. this.setState({
  106. trackData
  107. });
  108. }
  109. }
  110. /**
  111. * Destroys all the tracks from trackData object.
  112. *
  113. * @param {Object[]} trackData - An array of tracks that are to be disposed.
  114. * @returns {Promise<void>}
  115. */
  116. _disposeTracks(trackData: { jitsiTrack: any; }[]) {
  117. trackData.forEach(({ jitsiTrack }) => {
  118. jitsiTrack?.dispose();
  119. });
  120. }
  121. /**
  122. * Returns the click handler used when selecting the video preview.
  123. *
  124. * @param {string} deviceId - The id of the camera device.
  125. * @returns {Function}
  126. */
  127. _onEntryClick(deviceId: string) {
  128. return () => {
  129. this.props.setVideoInputDevice(deviceId);
  130. this.props.toggleVideoSettings();
  131. };
  132. }
  133. /**
  134. * Renders a preview entry.
  135. *
  136. * @param {Object} data - The track data.
  137. * @param {number} index - The index of the entry.
  138. * @returns {React$Node}
  139. */
  140. _renderPreviewEntry(data: { deviceId: string; error?: string; jitsiTrack: any | null; }, index: number) {
  141. const { error, jitsiTrack, deviceId } = data;
  142. const { currentCameraDeviceId, t } = this.props;
  143. const isSelected = deviceId === currentCameraDeviceId;
  144. const key = `vp-${index}`;
  145. const className = 'video-preview-entry';
  146. const tabIndex = '0';
  147. if (error) {
  148. return (
  149. <div
  150. className = { className }
  151. key = { key }
  152. tabIndex = { -1 } >
  153. <div className = 'video-preview-error'>{t(error)}</div>
  154. </div>
  155. );
  156. }
  157. const props: any = {
  158. className,
  159. key,
  160. tabIndex
  161. };
  162. const label = jitsiTrack?.getTrackLabel();
  163. if (isSelected) {
  164. props['aria-checked'] = true;
  165. props.className = `${className} video-preview-entry--selected`;
  166. } else {
  167. props.onClick = this._onEntryClick(deviceId);
  168. props.onKeyPress = (e: React.KeyboardEvent) => {
  169. if (e.key === ' ' || e.key === 'Enter') {
  170. e.preventDefault();
  171. props.onClick();
  172. }
  173. };
  174. }
  175. return (
  176. <div
  177. { ...props }
  178. role = 'radio'>
  179. <div className = 'video-preview-label'>
  180. {label && <div className = 'video-preview-label-text'>
  181. <span>{label}</span>
  182. </div>}
  183. </div>
  184. <Video
  185. className = { videoClassName }
  186. playsinline = { true }
  187. videoTrack = {{ jitsiTrack }} />
  188. </div>
  189. );
  190. }
  191. /**
  192. * Implements React's {@link Component#componentDidMount}.
  193. *
  194. * @inheritdoc
  195. */
  196. componentDidMount() {
  197. this._setTracks();
  198. }
  199. /**
  200. * Implements React's {@link Component#componentWillUnmount}.
  201. *
  202. * @inheritdoc
  203. */
  204. componentWillUnmount() {
  205. this._componentWasUnmounted = true;
  206. this._disposeTracks(this.state.trackData);
  207. }
  208. /**
  209. * Implements React's {@link Component#componentDidUpdate}.
  210. *
  211. * @inheritdoc
  212. */
  213. componentDidUpdate(prevProps: IProps) {
  214. if (!equals(this.props.videoDeviceIds, prevProps.videoDeviceIds)) {
  215. this._setTracks();
  216. }
  217. }
  218. /**
  219. * Implements React's {@link Component#render}.
  220. *
  221. * @inheritdoc
  222. */
  223. render() {
  224. const { trackData } = this.state;
  225. const { selectBackground, t, localFlipX } = this.props;
  226. return (
  227. <ContextMenu
  228. aria-labelledby = 'video-settings-button'
  229. className = 'video-preview-container'
  230. hidden = { false }
  231. id = 'video-settings-dialog'
  232. role = 'radiogroup'
  233. tabIndex = { -1 }>
  234. <ContextMenuItemGroup>
  235. {trackData.map((data, i) => this._renderPreviewEntry(data, i))}
  236. </ContextMenuItemGroup>
  237. <ContextMenuItemGroup>
  238. <ContextMenuItem
  239. accessibilityLabel = 'virtualBackground.title'
  240. icon = { IconImage }
  241. onClick = { selectBackground }
  242. text = { t('virtualBackground.title') } />
  243. <div
  244. className = 'video-preview-checkbox-container'
  245. // eslint-disable-next-line react/jsx-no-bind
  246. onClick = { e => e.stopPropagation() }>
  247. <Checkbox
  248. checked = { localFlipX }
  249. label = { t('videothumbnail.mirrorVideo') }
  250. onChange = { this._onToggleFlip } />
  251. </div>
  252. </ContextMenuItemGroup>
  253. </ContextMenu>
  254. );
  255. }
  256. }
  257. const mapStateToProps = (state: IReduxState) => {
  258. const { localFlipX } = state['features/base/settings'];
  259. return {
  260. localFlipX: Boolean(localFlipX)
  261. };
  262. };
  263. const mapDispatchToProps = (dispatch: IStore['dispatch']) => {
  264. return {
  265. selectBackground: () => dispatch(openDialog(VirtualBackgroundDialog)),
  266. changeFlip: (flip: boolean) => {
  267. dispatch(updateSettings({
  268. localFlipX: flip
  269. }));
  270. }
  271. };
  272. };
  273. export default translate(connect(mapStateToProps, mapDispatchToProps)(VideoSettingsContent));