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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { translate } from '../../../../base/i18n';
  4. import Video from '../../../../base/media/components/Video';
  5. import { equals } from '../../../../base/redux';
  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(this.props.videoDeviceIds, 5000);
  72. // In case the component gets unmounted before the tracks are created
  73. // avoid a leak by not setting the state
  74. if (this._componentWasUnmounted) {
  75. this._disposeTracks(trackData);
  76. } else {
  77. this.setState({
  78. trackData
  79. });
  80. }
  81. }
  82. /**
  83. * Destroys all the tracks from trackData object.
  84. *
  85. * @param {Object[]} trackData - An array of tracks that are to be disposed.
  86. * @returns {Promise<void>}
  87. */
  88. _disposeTracks(trackData) {
  89. trackData.forEach(({ jitsiTrack }) => {
  90. jitsiTrack && jitsiTrack.dispose();
  91. });
  92. }
  93. /**
  94. * Returns the click handler used when selecting the video preview.
  95. *
  96. * @param {string} deviceId - The id of the camera device.
  97. * @returns {Function}
  98. */
  99. _onEntryClick(deviceId) {
  100. return () => {
  101. this.props.setVideoInputDevice(deviceId);
  102. this.props.toggleVideoSettings();
  103. };
  104. }
  105. /**
  106. * Renders a preview entry.
  107. *
  108. * @param {Object} data - The track data.
  109. * @param {number} index - The index of the entry.
  110. * @returns {React$Node}
  111. */
  112. _renderPreviewEntry(data, index) {
  113. const { error, jitsiTrack, deviceId } = data;
  114. const { currentCameraDeviceId, t } = this.props;
  115. const isSelected = deviceId === currentCameraDeviceId;
  116. const key = `vp-${index}`;
  117. const className = 'video-preview-entry';
  118. if (error) {
  119. return (
  120. <div
  121. className = { className }
  122. key = { key }>
  123. <div className = 'video-preview-error'>{t(error)}</div>
  124. </div>
  125. );
  126. }
  127. const props: Object = {
  128. className,
  129. key
  130. };
  131. const label = jitsiTrack && jitsiTrack.getTrackLabel();
  132. if (isSelected) {
  133. props.className = `${className} video-preview-entry--selected`;
  134. } else {
  135. props.onClick = this._onEntryClick(deviceId);
  136. }
  137. return (
  138. <div { ...props }>
  139. <div className = 'video-preview-label'>{label}</div>
  140. <div className = 'video-preview-overlay' />
  141. <Video
  142. className = { videoClassName }
  143. playsinline = { true }
  144. videoTrack = {{ jitsiTrack }} />
  145. </div>
  146. );
  147. }
  148. /**
  149. * Implements React's {@link Component#componentDidMount}.
  150. *
  151. * @inheritdoc
  152. */
  153. componentDidMount() {
  154. this._setTracks();
  155. }
  156. /**
  157. * Implements React's {@link Component#componentWillUnmount}.
  158. *
  159. * @inheritdoc
  160. */
  161. componentWillUnmount() {
  162. this._componentWasUnmounted = true;
  163. this._disposeTracks(this.state.trackData);
  164. }
  165. /**
  166. * Implements React's {@link Component#componentDidUpdate}.
  167. *
  168. * @inheritdoc
  169. */
  170. componentDidUpdate(prevProps) {
  171. if (!equals(this.props.videoDeviceIds, prevProps.videoDeviceIds)) {
  172. this._setTracks();
  173. }
  174. }
  175. /**
  176. * Implements React's {@link Component#render}.
  177. *
  178. * @inheritdoc
  179. */
  180. render() {
  181. const { trackData } = this.state;
  182. return (
  183. <div className = 'video-preview-container'>
  184. <div className = 'video-preview'>
  185. {trackData.map((data, i) => this._renderPreviewEntry(data, i))}
  186. </div>
  187. </div>
  188. );
  189. }
  190. }
  191. export default translate(VideoSettingsContent);