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.

VideoSettingsContent.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { translate } from '../../../../base/i18n';
  4. import { equals } from '../../../../base/redux';
  5. import Video from '../../../../base/media/components/Video';
  6. import { createLocalVideoTracks } from '../../../functions';
  7. const videoClassName = 'video-preview-video flipVideoX';
  8. /**
  9. * The type of the React {@code Component} props of {@link VideoSettingsContent}.
  10. */
  11. export type Props = {
  12. /**
  13. * The deviceId of the camera device currently being used.
  14. */
  15. currentCameraDeviceId: string,
  16. /**
  17. * Callback invoked to change current camera.
  18. */
  19. setVideoInputDevice: Function,
  20. /**
  21. * Invoked to obtain translated strings.
  22. */
  23. t: Function,
  24. /**
  25. * Callback invoked to toggle the settings popup visibility.
  26. */
  27. toggleVideoSettings: Function,
  28. /**
  29. * All the camera device ids currently connected.
  30. */
  31. videoDeviceIds: string[],
  32. };
  33. /**
  34. * The type of the React {@code Component} state of {@link VideoSettingsContent}.
  35. */
  36. type State = {
  37. /**
  38. * An array of all the jitsiTracks and eventual errors.
  39. */
  40. trackData: Object[],
  41. };
  42. /**
  43. * Implements a React {@link Component} which displays a list of video
  44. * previews to choose from.
  45. *
  46. * @extends Component
  47. */
  48. class VideoSettingsContent extends Component<Props, State> {
  49. _componentWasUnmounted: boolean;
  50. /**
  51. * Initializes a new {@code VideoSettingsContent} instance.
  52. *
  53. * @param {Object} props - The read-only properties with which the new
  54. * instance is to be initialized.
  55. */
  56. constructor(props) {
  57. super(props);
  58. this.state = {
  59. trackData: new Array(props.videoDeviceIds.length).fill({
  60. jitsiTrack: null
  61. })
  62. };
  63. }
  64. /**
  65. * Creates and updates the track data.
  66. *
  67. * @returns {void}
  68. */
  69. async _setTracks() {
  70. this._disposeTracks(this.state.trackData);
  71. const trackData = await createLocalVideoTracks(
  72. this.props.videoDeviceIds,
  73. );
  74. // In case the component gets unmounted before the tracks are created
  75. // avoid a leak by not setting the state
  76. if (this._componentWasUnmounted) {
  77. this._disposeTracks(trackData);
  78. } else {
  79. this.setState({
  80. trackData
  81. });
  82. }
  83. }
  84. /**
  85. * Destroys all the tracks from trackData object.
  86. *
  87. * @param {Object[]} trackData - An array of tracks that are to be disposed.
  88. * @returns {Promise<void>}
  89. */
  90. _disposeTracks(trackData) {
  91. trackData.forEach(({ jitsiTrack }) => {
  92. jitsiTrack && jitsiTrack.dispose();
  93. });
  94. }
  95. /**
  96. * Returns the click handler used when selecting the video preview.
  97. *
  98. * @param {string} deviceId - The id of the camera device.
  99. * @returns {Function}
  100. */
  101. _onEntryClick(deviceId) {
  102. return () => {
  103. this.props.setVideoInputDevice(deviceId);
  104. this.props.toggleVideoSettings();
  105. };
  106. }
  107. /**
  108. * Renders a preview entry.
  109. *
  110. * @param {Object} data - The track data.
  111. * @param {number} index - The index of the entry.
  112. * @returns {React$Node}
  113. */
  114. _renderPreviewEntry(data, index) {
  115. const { error, jitsiTrack, deviceId } = data;
  116. const { currentCameraDeviceId, t } = this.props;
  117. const isSelected = deviceId === currentCameraDeviceId;
  118. const key = `vp-${index}`;
  119. const className = 'video-preview-entry';
  120. if (error) {
  121. return (
  122. <div
  123. className = { className }
  124. key = { key }>
  125. <div className = 'video-preview-error'>{t(error)}</div>
  126. </div>
  127. );
  128. }
  129. const props: Object = {
  130. className,
  131. key
  132. };
  133. const label = jitsiTrack && jitsiTrack.getTrackLabel();
  134. if (isSelected) {
  135. props.className = `${className} video-preview-entry--selected`;
  136. } else {
  137. props.onClick = this._onEntryClick(deviceId);
  138. }
  139. return (
  140. <div { ...props }>
  141. <div className = 'video-preview-label'>{label}</div>
  142. <div className = 'video-preview-overlay' />
  143. <Video
  144. className = { videoClassName }
  145. videoTrack = {{ jitsiTrack }} />
  146. </div>
  147. );
  148. }
  149. /**
  150. * Implements React's {@link Component#componentDidMount}.
  151. *
  152. * @inheritdoc
  153. */
  154. componentDidMount() {
  155. this._setTracks();
  156. }
  157. /**
  158. * Implements React's {@link Component#componentWillUnmount}.
  159. *
  160. * @inheritdoc
  161. */
  162. componentWillUnmount() {
  163. this._componentWasUnmounted = true;
  164. this._disposeTracks(this.state.trackData);
  165. }
  166. /**
  167. * Implements React's {@link Component#componentDidUpdate}.
  168. *
  169. * @inheritdoc
  170. */
  171. componentDidUpdate(prevProps) {
  172. if (!equals(this.props.videoDeviceIds, prevProps.videoDeviceIds)) {
  173. this._setTracks();
  174. }
  175. }
  176. /**
  177. * Implements React's {@link Component#render}.
  178. *
  179. * @inheritdoc
  180. */
  181. render() {
  182. const { trackData } = this.state;
  183. return (
  184. <div className = 'video-preview'>
  185. {trackData.map((data, i) => this._renderPreviewEntry(data, i))}
  186. </div>
  187. );
  188. }
  189. }
  190. export default translate(VideoSettingsContent);