Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

VideoSettingsContent.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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'>
  140. {label && <div className = 'video-preview-label-container'>
  141. <div className = 'video-preview-label-text'>
  142. <span>{label}</span></div>
  143. </div>}
  144. </div>
  145. <div className = 'video-preview-overlay' />
  146. <Video
  147. className = { videoClassName }
  148. playsinline = { true }
  149. videoTrack = {{ jitsiTrack }} />
  150. </div>
  151. );
  152. }
  153. /**
  154. * Implements React's {@link Component#componentDidMount}.
  155. *
  156. * @inheritdoc
  157. */
  158. componentDidMount() {
  159. this._setTracks();
  160. }
  161. /**
  162. * Implements React's {@link Component#componentWillUnmount}.
  163. *
  164. * @inheritdoc
  165. */
  166. componentWillUnmount() {
  167. this._componentWasUnmounted = true;
  168. this._disposeTracks(this.state.trackData);
  169. }
  170. /**
  171. * Implements React's {@link Component#componentDidUpdate}.
  172. *
  173. * @inheritdoc
  174. */
  175. componentDidUpdate(prevProps) {
  176. if (!equals(this.props.videoDeviceIds, prevProps.videoDeviceIds)) {
  177. this._setTracks();
  178. }
  179. }
  180. /**
  181. * Implements React's {@link Component#render}.
  182. *
  183. * @inheritdoc
  184. */
  185. render() {
  186. const { trackData } = this.state;
  187. return (
  188. <div className = 'video-preview-container'>
  189. <div className = 'video-preview'>
  190. {trackData.map((data, i) => this._renderPreviewEntry(data, i))}
  191. </div>
  192. </div>
  193. );
  194. }
  195. }
  196. export default translate(VideoSettingsContent);