Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

VideoSettingsContent.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. if (isSelected) {
  134. props.className = `${className} video-preview-entry--selected`;
  135. } else {
  136. props.onClick = this._onEntryClick(deviceId);
  137. }
  138. return (
  139. <div { ...props }>
  140. <div className = 'video-preview-overlay' />
  141. <Video
  142. className = { videoClassName }
  143. videoTrack = {{ jitsiTrack }} />
  144. </div>
  145. );
  146. }
  147. /**
  148. * Implements React's {@link Component#componentDidMount}.
  149. *
  150. * @inheritdoc
  151. */
  152. componentDidMount() {
  153. this._setTracks();
  154. }
  155. /**
  156. * Implements React's {@link Component#componentWillUnmount}.
  157. *
  158. * @inheritdoc
  159. */
  160. componentWillUnmount() {
  161. this._componentWasUnmounted = true;
  162. this._disposeTracks(this.state.trackData);
  163. }
  164. /**
  165. * Implements React's {@link Component#componentDidUpdate}.
  166. *
  167. * @inheritdoc
  168. */
  169. componentDidUpdate(prevProps) {
  170. if (!equals(this.props.videoDeviceIds, prevProps.videoDeviceIds)) {
  171. this._setTracks();
  172. }
  173. }
  174. /**
  175. * Implements React's {@link Component#render}.
  176. *
  177. * @inheritdoc
  178. */
  179. render() {
  180. const { trackData } = this.state;
  181. return (
  182. <div>
  183. {trackData.map((data, i) => this._renderPreviewEntry(data, i))}
  184. </div>
  185. );
  186. }
  187. }
  188. export default translate(VideoSettingsContent);