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.

ParticipantView.native.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { Text, View } from 'react-native';
  4. import { connect } from 'react-redux';
  5. import { translate } from '../../i18n';
  6. import { JitsiParticipantConnectionStatus } from '../../lib-jitsi-meet';
  7. import {
  8. MEDIA_TYPE,
  9. shouldRenderVideoTrack,
  10. VideoTrack
  11. } from '../../media';
  12. import { prefetch } from '../../../mobile/image-cache';
  13. import { Container, TintedView } from '../../react';
  14. import { TestHint } from '../../testing/components';
  15. import { getTrackByMediaTypeAndParticipant } from '../../tracks';
  16. import Avatar from './Avatar';
  17. import {
  18. getAvatarURL,
  19. getParticipantById,
  20. getParticipantDisplayName
  21. } from '../functions';
  22. import styles from './styles';
  23. /**
  24. * The type of the React {@link Component} props of {@link ParticipantView}.
  25. */
  26. type Props = {
  27. /**
  28. * The indicator which determines whether conferencing is in audio-only
  29. * mode.
  30. *
  31. * @private
  32. */
  33. _audioOnly: boolean,
  34. /**
  35. * The source (e.g. URI, URL) of the avatar image of the participant with
  36. * {@link #participantId}.
  37. *
  38. * @private
  39. */
  40. _avatar: string,
  41. /**
  42. * The connection status of the participant. Her video will only be rendered
  43. * if the connection status is 'active'; otherwise, the avatar will be
  44. * rendered. If undefined, 'active' is presumed.
  45. *
  46. * @private
  47. */
  48. _connectionStatus: string,
  49. /**
  50. * The name of the participant which this component represents.
  51. *
  52. * @private
  53. */
  54. _participantName: string,
  55. /**
  56. * The video Track of the participant with {@link #participantId}.
  57. */
  58. _videoTrack: Object,
  59. /**
  60. * The avatar size.
  61. */
  62. avatarSize: number,
  63. /**
  64. * Callback to invoke when the {@code ParticipantView} is clicked/pressed.
  65. */
  66. onPress: Function,
  67. /**
  68. * The ID of the participant (to be) depicted by {@link ParticipantView}.
  69. *
  70. * @public
  71. */
  72. participantId: string,
  73. /**
  74. * True if the avatar of the depicted participant is to be shown should the
  75. * avatar be available and the video of the participant is not to be shown;
  76. * otherwise, false. If undefined, defaults to true.
  77. */
  78. showAvatar: boolean,
  79. /**
  80. * True if the video of the depicted participant is to be shown should the
  81. * video be available. If undefined, defaults to true.
  82. */
  83. showVideo: boolean,
  84. /**
  85. * The style, if any, to apply to {@link ParticipantView} in addition to its
  86. * default style.
  87. */
  88. style: Object,
  89. /**
  90. * The function to translate human-readable text.
  91. */
  92. t: Function,
  93. /**
  94. * The test hint id which can be used to locate the {@code ParticipantView}
  95. * on the jitsi-meet-torture side. If not provided, the
  96. * {@code participantId} with the following format will be used:
  97. * {@code `org.jitsi.meet.Participant#${participantId}`}
  98. */
  99. testHintId: ?string,
  100. /**
  101. * Indicates if the connectivity info label should be shown, if appropriate.
  102. * It will be shown in case the connection is interrupted.
  103. */
  104. useConnectivityInfoLabel: boolean,
  105. /**
  106. * The z-order of the {@link Video} of {@link ParticipantView} in the
  107. * stacking space of all {@code Video}s. For more details, refer to the
  108. * {@code zOrder} property of the {@code Video} class for React Native.
  109. */
  110. zOrder: number,
  111. /**
  112. * Indicates whether zooming (pinch to zoom and/or drag) is enabled.
  113. */
  114. zoomEnabled: boolean
  115. };
  116. /**
  117. * Implements a React Component which depicts a specific participant's avatar
  118. * and video.
  119. *
  120. * @extends Component
  121. */
  122. class ParticipantView extends Component<Props> {
  123. /**
  124. * Renders the connection status label, if appropriate.
  125. *
  126. * @param {string} connectionStatus - The status of the participant's
  127. * connection.
  128. * @private
  129. * @returns {ReactElement|null}
  130. */
  131. _renderConnectionInfo(connectionStatus) {
  132. let messageKey;
  133. switch (connectionStatus) {
  134. case JitsiParticipantConnectionStatus.INACTIVE:
  135. messageKey = 'connection.LOW_BANDWIDTH';
  136. break;
  137. case JitsiParticipantConnectionStatus.INTERRUPTED:
  138. messageKey = 'connection.USER_CONNECTION_INTERRUPTED';
  139. break;
  140. default:
  141. return null;
  142. }
  143. const {
  144. avatarSize,
  145. _participantName: displayName,
  146. t
  147. } = this.props;
  148. // XXX Consider splitting this component into 2: one for the large view
  149. // and one for the thumbnail. Some of these don't apply to both.
  150. const containerStyle = {
  151. ...styles.connectionInfoContainer,
  152. width: avatarSize * 1.5
  153. };
  154. return (
  155. <View
  156. pointerEvents = 'box-none'
  157. style = { containerStyle }>
  158. <Text style = { styles.connectionInfoText }>
  159. { t(messageKey, { displayName }) }
  160. </Text>
  161. </View>
  162. );
  163. }
  164. /**
  165. * Implements React's {@link Component#render()}.
  166. *
  167. * @inheritdoc
  168. * @returns {ReactElement}
  169. */
  170. render() {
  171. const {
  172. onPress,
  173. _avatar: avatar,
  174. _connectionStatus: connectionStatus,
  175. _videoTrack: videoTrack
  176. } = this.props;
  177. // Is the video to be rendered?
  178. // FIXME It's currently impossible to have true as the value of
  179. // waitForVideoStarted because videoTrack's state videoStarted will be
  180. // updated only after videoTrack is rendered.
  181. // XXX Note that, unlike on web, we don't render video when the
  182. // connection status is interrupted, this is because the renderer
  183. // doesn't retain the last frame forever, so we would end up with a
  184. // black screen.
  185. const waitForVideoStarted = false;
  186. let renderVideo
  187. = !this.props._audioOnly
  188. && (connectionStatus
  189. === JitsiParticipantConnectionStatus.ACTIVE)
  190. && shouldRenderVideoTrack(videoTrack, waitForVideoStarted);
  191. // Is the avatar to be rendered?
  192. let renderAvatar = Boolean(!renderVideo && avatar);
  193. // The consumer of this ParticipantView is allowed to forbid showing the
  194. // video if the private logic of this ParticipantView determines that
  195. // the video could be rendered.
  196. renderVideo = renderVideo && _toBoolean(this.props.showVideo, true);
  197. // The consumer of this ParticipantView is allowed to forbid showing the
  198. // avatar if the private logic of this ParticipantView determines that
  199. // the avatar could be rendered.
  200. renderAvatar = renderAvatar && _toBoolean(this.props.showAvatar, true);
  201. // If the connection has problems, we will "tint" the video / avatar.
  202. const useTint
  203. = connectionStatus === JitsiParticipantConnectionStatus.INACTIVE
  204. || connectionStatus
  205. === JitsiParticipantConnectionStatus.INTERRUPTED;
  206. const testHintId
  207. = this.props.testHintId
  208. ? this.props.testHintId
  209. : `org.jitsi.meet.Participant#${this.props.participantId}`;
  210. return (
  211. <Container
  212. onClick = { renderVideo ? undefined : onPress }
  213. style = {{
  214. ...styles.participantView,
  215. ...this.props.style
  216. }}
  217. touchFeedback = { false }>
  218. <TestHint
  219. id = { testHintId }
  220. onPress = { onPress }
  221. value = '' />
  222. { renderVideo
  223. && <VideoTrack
  224. onPress = { renderVideo ? onPress : undefined }
  225. videoTrack = { videoTrack }
  226. waitForVideoStarted = { waitForVideoStarted }
  227. zOrder = { this.props.zOrder }
  228. zoomEnabled = { this.props.zoomEnabled } /> }
  229. { renderAvatar
  230. && <Avatar
  231. size = { this.props.avatarSize }
  232. uri = { avatar } /> }
  233. { useTint
  234. // If the connection has problems, tint the video / avatar.
  235. && <TintedView /> }
  236. { this.props.useConnectivityInfoLabel
  237. && this._renderConnectionInfo(connectionStatus) }
  238. </Container>
  239. );
  240. }
  241. }
  242. /**
  243. * Converts the specified value to a boolean value. If the specified value is
  244. * undefined, returns the boolean value of undefinedValue.
  245. *
  246. * @param {any} value - The value to convert to a boolean value should it not be
  247. * undefined.
  248. * @param {any} undefinedValue - The value to convert to a boolean value should
  249. * the specified value be undefined.
  250. * @private
  251. * @returns {boolean}
  252. */
  253. function _toBoolean(value, undefinedValue) {
  254. return Boolean(typeof value === 'undefined' ? undefinedValue : value);
  255. }
  256. /**
  257. * Maps (parts of) the redux state to the associated {@link ParticipantView}'s
  258. * props.
  259. *
  260. * @param {Object} state - The redux state.
  261. * @param {Object} ownProps - The React {@code Component} props passed to the
  262. * associated (instance of) {@code ParticipantView}.
  263. * @private
  264. * @returns {{
  265. * _audioOnly: boolean,
  266. * _avatar: string,
  267. * _connectionStatus: string,
  268. * _participantName: string,
  269. * _videoTrack: Track
  270. * }}
  271. */
  272. function _mapStateToProps(state, ownProps) {
  273. const { participantId } = ownProps;
  274. const participant = getParticipantById(state, participantId);
  275. let avatar;
  276. let connectionStatus;
  277. let participantName;
  278. if (participant) {
  279. avatar = getAvatarURL(participant);
  280. connectionStatus = participant.connectionStatus;
  281. participantName = getParticipantDisplayName(state, participant.id);
  282. // Avatar (on React Native) now has the ability to generate an
  283. // automatically-colored default image when no URI/URL is specified or
  284. // when it fails to load. In order to make the coloring permanent(ish)
  285. // per participant, Avatar will need something permanent(ish) per
  286. // perticipant, obviously. A participant's ID is such a piece of data.
  287. // But the local participant changes her ID as she joins, leaves.
  288. // TODO @lyubomir: The participants may change their avatar URLs at
  289. // runtime which means that, if their old and new avatar URLs fail to
  290. // download, Avatar will change their automatically-generated colors.
  291. avatar || participant.local || (avatar = `#${participant.id}`);
  292. // ParticipantView knows before Avatar that an avatar URL will be used
  293. // so it's advisable to prefetch here.
  294. avatar && prefetch({ uri: avatar });
  295. }
  296. return {
  297. _audioOnly: state['features/base/conference'].audioOnly,
  298. _avatar: avatar,
  299. _connectionStatus:
  300. connectionStatus
  301. || JitsiParticipantConnectionStatus.ACTIVE,
  302. _participantName: participantName,
  303. _videoTrack:
  304. getTrackByMediaTypeAndParticipant(
  305. state['features/base/tracks'],
  306. MEDIA_TYPE.VIDEO,
  307. participantId)
  308. };
  309. }
  310. export default translate(connect(_mapStateToProps)(ParticipantView));