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.

functions.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // @flow
  2. import { getGravatarURL } from '@jitsi/js-utils/avatar';
  3. import type { Store } from 'redux';
  4. import { JitsiParticipantConnectionStatus } from '../lib-jitsi-meet';
  5. import { MEDIA_TYPE, shouldRenderVideoTrack } from '../media';
  6. import { toState } from '../redux';
  7. import { getTrackByMediaTypeAndParticipant, isRemoteTrackMuted } from '../tracks';
  8. import { createDeferred } from '../util';
  9. import {
  10. JIGASI_PARTICIPANT_ICON,
  11. MAX_DISPLAY_NAME_LENGTH,
  12. PARTICIPANT_ROLE
  13. } from './constants';
  14. import { preloadImage } from './preloadImage';
  15. declare var config: Object;
  16. declare var interfaceConfig: Object;
  17. /**
  18. * Temp structures for avatar urls to be checked/preloaded.
  19. */
  20. const AVATAR_QUEUE = [];
  21. const AVATAR_CHECKED_URLS = new Map();
  22. /* eslint-disable arrow-body-style, no-unused-vars */
  23. const AVATAR_CHECKER_FUNCTIONS = [
  24. (participant, _) => {
  25. return participant && participant.isJigasi ? JIGASI_PARTICIPANT_ICON : null;
  26. },
  27. (participant, _) => {
  28. return participant && participant.avatarURL ? participant.avatarURL : null;
  29. },
  30. (participant, store) => {
  31. if (participant && participant.email) {
  32. return getGravatarURL(participant.email, store.getState()['features/base/config'].gravatarBaseURL);
  33. }
  34. return null;
  35. }
  36. ];
  37. /* eslint-enable arrow-body-style, no-unused-vars */
  38. /**
  39. * Resolves the first loadable avatar URL for a participant.
  40. *
  41. * @param {Object} participant - The participant to resolve avatars for.
  42. * @param {Store} store - Redux store.
  43. * @returns {Promise}
  44. */
  45. export function getFirstLoadableAvatarUrl(participant: Object, store: Store<any, any>) {
  46. const deferred = createDeferred();
  47. const fullPromise = deferred.promise
  48. .then(() => _getFirstLoadableAvatarUrl(participant, store))
  49. .then(src => {
  50. if (AVATAR_QUEUE.length) {
  51. const next = AVATAR_QUEUE.shift();
  52. next.resolve();
  53. }
  54. return src;
  55. });
  56. if (AVATAR_QUEUE.length) {
  57. AVATAR_QUEUE.push(deferred);
  58. } else {
  59. deferred.resolve();
  60. }
  61. return fullPromise;
  62. }
  63. /**
  64. * Returns local participant from Redux state.
  65. *
  66. * @param {(Function|Object|Participant[])} stateful - The redux state
  67. * features/base/participants, the (whole) redux state, or redux's
  68. * {@code getState} function to be used to retrieve the state
  69. * features/base/participants.
  70. * @returns {(Participant|undefined)}
  71. */
  72. export function getLocalParticipant(stateful: Object | Function) {
  73. const participants = _getAllParticipants(stateful);
  74. return participants.find(p => p.local);
  75. }
  76. /**
  77. * Normalizes a display name so then no invalid values (padding, length...etc)
  78. * can be set.
  79. *
  80. * @param {string} name - The display name to set.
  81. * @returns {string}
  82. */
  83. export function getNormalizedDisplayName(name: string) {
  84. if (!name || !name.trim()) {
  85. return undefined;
  86. }
  87. return name.trim().substring(0, MAX_DISPLAY_NAME_LENGTH);
  88. }
  89. /**
  90. * Returns participant by ID from Redux state.
  91. *
  92. * @param {(Function|Object|Participant[])} stateful - The redux state
  93. * features/base/participants, the (whole) redux state, or redux's
  94. * {@code getState} function to be used to retrieve the state
  95. * features/base/participants.
  96. * @param {string} id - The ID of the participant to retrieve.
  97. * @private
  98. * @returns {(Participant|undefined)}
  99. */
  100. export function getParticipantById(
  101. stateful: Object | Function, id: string): ?Object {
  102. const participants = _getAllParticipants(stateful);
  103. return participants.find(p => p.id === id);
  104. }
  105. /**
  106. * Returns a count of the known participants in the passed in redux state,
  107. * excluding any fake participants.
  108. *
  109. * @param {(Function|Object|Participant[])} stateful - The redux state
  110. * features/base/participants, the (whole) redux state, or redux's
  111. * {@code getState} function to be used to retrieve the state
  112. * features/base/participants.
  113. * @returns {number}
  114. */
  115. export function getParticipantCount(stateful: Object | Function) {
  116. return getParticipants(stateful).length;
  117. }
  118. /**
  119. * Returns a count of the known participants in the passed in redux state,
  120. * including fake participants.
  121. *
  122. * @param {(Function|Object|Participant[])} stateful - The redux state
  123. * features/base/participants, the (whole) redux state, or redux's
  124. * {@code getState} function to be used to retrieve the state
  125. * features/base/participants.
  126. * @returns {number}
  127. */
  128. export function getParticipantCountWithFake(stateful: Object | Function) {
  129. return _getAllParticipants(stateful).length;
  130. }
  131. /**
  132. * Returns participant's display name.
  133. *
  134. * FIXME: Remove the hardcoded strings once interfaceConfig is stored in redux
  135. * and merge with a similarly named method in {@code conference.js}.
  136. *
  137. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  138. * {@code getState} function to be used to retrieve the state.
  139. * @param {string} id - The ID of the participant's display name to retrieve.
  140. * @returns {string}
  141. */
  142. export function getParticipantDisplayName(
  143. stateful: Object | Function,
  144. id: string) {
  145. const participant = getParticipantById(stateful, id);
  146. if (participant) {
  147. if (participant.name) {
  148. return participant.name;
  149. }
  150. if (participant.local) {
  151. return typeof interfaceConfig === 'object'
  152. ? interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME
  153. : 'me';
  154. }
  155. }
  156. return typeof interfaceConfig === 'object'
  157. ? interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME
  158. : 'Fellow Jitster';
  159. }
  160. /**
  161. * Returns the presence status of a participant associated with the passed id.
  162. *
  163. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  164. * {@code getState} function to be used to retrieve the state.
  165. * @param {string} id - The id of the participant.
  166. * @returns {string} - The presence status.
  167. */
  168. export function getParticipantPresenceStatus(
  169. stateful: Object | Function, id: string) {
  170. if (!id) {
  171. return undefined;
  172. }
  173. const participantById = getParticipantById(stateful, id);
  174. if (!participantById) {
  175. return undefined;
  176. }
  177. return participantById.presence;
  178. }
  179. /**
  180. * Selectors for getting all known participants with fake participants filtered
  181. * out.
  182. *
  183. * @param {(Function|Object|Participant[])} stateful - The redux state
  184. * features/base/participants, the (whole) redux state, or redux's
  185. * {@code getState} function to be used to retrieve the state
  186. * features/base/participants.
  187. * @returns {Participant[]}
  188. */
  189. export function getParticipants(stateful: Object | Function) {
  190. return _getAllParticipants(stateful).filter(p => !p.isFakeParticipant);
  191. }
  192. /**
  193. * Returns the participant which has its pinned state set to truthy.
  194. *
  195. * @param {(Function|Object|Participant[])} stateful - The redux state
  196. * features/base/participants, the (whole) redux state, or redux's
  197. * {@code getState} function to be used to retrieve the state
  198. * features/base/participants.
  199. * @returns {(Participant|undefined)}
  200. */
  201. export function getPinnedParticipant(stateful: Object | Function) {
  202. return _getAllParticipants(stateful).find(p => p.pinned);
  203. }
  204. /**
  205. * Returns array of participants from Redux state.
  206. *
  207. * @param {(Function|Object|Participant[])} stateful - The redux state
  208. * features/base/participants, the (whole) redux state, or redux's
  209. * {@code getState} function to be used to retrieve the state
  210. * features/base/participants.
  211. * @private
  212. * @returns {Participant[]}
  213. */
  214. function _getAllParticipants(stateful) {
  215. return (
  216. Array.isArray(stateful)
  217. ? stateful
  218. : toState(stateful)['features/base/participants'] || []);
  219. }
  220. /**
  221. * Returns the youtube fake participant.
  222. * At the moment it is considered the youtube participant the only fake participant in the list.
  223. *
  224. * @param {(Function|Object|Participant[])} stateful - The redux state
  225. * features/base/participants, the (whole) redux state, or redux's
  226. * {@code getState} function to be used to retrieve the state
  227. * features/base/participants.
  228. * @private
  229. * @returns {Participant}
  230. */
  231. export function getYoutubeParticipant(stateful: Object | Function) {
  232. const participants = _getAllParticipants(stateful);
  233. return participants.filter(p => p.isFakeParticipant)[0];
  234. }
  235. /**
  236. * Returns true if the participant is a moderator.
  237. *
  238. * @param {string} participant - Participant object.
  239. * @returns {boolean}
  240. */
  241. export function isParticipantModerator(participant: Object) {
  242. return participant?.role === PARTICIPANT_ROLE.MODERATOR;
  243. }
  244. /**
  245. * Returns true if all of the meeting participants are moderators.
  246. *
  247. * @param {Object|Function} stateful -Object or function that can be resolved
  248. * to the Redux state.
  249. * @returns {boolean}
  250. */
  251. export function isEveryoneModerator(stateful: Object | Function) {
  252. const participants = _getAllParticipants(stateful);
  253. return participants.every(isParticipantModerator);
  254. }
  255. /**
  256. * Checks a value and returns true if it's a preloaded icon object.
  257. *
  258. * @param {?string | ?Object} icon - The icon to check.
  259. * @returns {boolean}
  260. */
  261. export function isIconUrl(icon: ?string | ?Object) {
  262. return Boolean(icon) && typeof icon === 'object';
  263. }
  264. /**
  265. * Returns true if the current local participant is a moderator in the
  266. * conference.
  267. *
  268. * @param {Object|Function} stateful - Object or function that can be resolved
  269. * to the Redux state.
  270. * @returns {boolean}
  271. */
  272. export function isLocalParticipantModerator(stateful: Object | Function) {
  273. const state = toState(stateful);
  274. const localParticipant = getLocalParticipant(state);
  275. if (!localParticipant) {
  276. return false;
  277. }
  278. return localParticipant.role === PARTICIPANT_ROLE.MODERATOR;
  279. }
  280. /**
  281. * Returns true if the video of the participant should be rendered.
  282. * NOTE: This is currently only used on mobile.
  283. *
  284. * @param {Object|Function} stateful - Object or function that can be resolved
  285. * to the Redux state.
  286. * @param {string} id - The ID of the participant.
  287. * @returns {boolean}
  288. */
  289. export function shouldRenderParticipantVideo(stateful: Object | Function, id: string) {
  290. const state = toState(stateful);
  291. const participant = getParticipantById(state, id);
  292. if (!participant) {
  293. return false;
  294. }
  295. /* First check if we have an unmuted video track. */
  296. const videoTrack
  297. = getTrackByMediaTypeAndParticipant(state['features/base/tracks'], MEDIA_TYPE.VIDEO, id);
  298. if (!shouldRenderVideoTrack(videoTrack, /* waitForVideoStarted */ false)) {
  299. return false;
  300. }
  301. /* Then check if the participant connection is active. */
  302. const connectionStatus = participant.connectionStatus || JitsiParticipantConnectionStatus.ACTIVE;
  303. if (connectionStatus !== JitsiParticipantConnectionStatus.ACTIVE) {
  304. return false;
  305. }
  306. /* Then check if audio-only mode is not active. */
  307. const audioOnly = state['features/base/audio-only'].enabled;
  308. if (!audioOnly) {
  309. return true;
  310. }
  311. /* Last, check if the participant is sharing their screen and they are on stage. */
  312. const screenShares = state['features/video-layout'].screenShares || [];
  313. const largeVideoParticipantId = state['features/large-video'].participantId;
  314. const participantIsInLargeVideoWithScreen
  315. = participant.id === largeVideoParticipantId && screenShares.includes(participant.id);
  316. return participantIsInLargeVideoWithScreen;
  317. }
  318. /**
  319. * Figures out the value of mutedWhileDisconnected status by taking into
  320. * account remote participant's network connectivity and video muted status.
  321. * The flag is set to <tt>true</tt> if remote participant's video gets muted
  322. * during his media connection disruption. This is to prevent black video
  323. * being render on the thumbnail, because even though once the video has
  324. * been played the image usually remains on the video element it seems that
  325. * after longer period of the video element being hidden this image can be
  326. * lost.
  327. *
  328. * @param {Object|Function} stateful - Object or function that can be resolved
  329. * to the Redux state.
  330. * @param {string} participantID - The ID of the participant.
  331. * @param {string} [connectionStatus] - A connection status to be used.
  332. * @returns {boolean} - The mutedWhileDisconnected value.
  333. */
  334. export function figureOutMutedWhileDisconnectedStatus(
  335. stateful: Function | Object, participantID: string, connectionStatus: ?string) {
  336. const state = toState(stateful);
  337. const participant = getParticipantById(state, participantID);
  338. if (!participant || participant.local) {
  339. return undefined;
  340. }
  341. const isActive = (connectionStatus || participant.connectionStatus) === JitsiParticipantConnectionStatus.ACTIVE;
  342. const isVideoMuted = isRemoteTrackMuted(state['features/base/tracks'], MEDIA_TYPE.VIDEO, participantID);
  343. let mutedWhileDisconnected = participant.mutedWhileDisconnected || false;
  344. if (!isActive && isVideoMuted) {
  345. mutedWhileDisconnected = true;
  346. } else if (isActive && !isVideoMuted) {
  347. mutedWhileDisconnected = false;
  348. }
  349. return mutedWhileDisconnected;
  350. }
  351. /**
  352. * Resolves the first loadable avatar URL for a participant.
  353. *
  354. * @param {Object} participant - The participant to resolve avatars for.
  355. * @param {Store} store - Redux store.
  356. * @returns {?string}
  357. */
  358. async function _getFirstLoadableAvatarUrl(participant, store) {
  359. for (let i = 0; i < AVATAR_CHECKER_FUNCTIONS.length; i++) {
  360. const url = AVATAR_CHECKER_FUNCTIONS[i](participant, store);
  361. if (url) {
  362. if (AVATAR_CHECKED_URLS.has(url)) {
  363. if (AVATAR_CHECKED_URLS.get(url)) {
  364. return url;
  365. }
  366. } else {
  367. try {
  368. const finalUrl = await preloadImage(url);
  369. AVATAR_CHECKED_URLS.set(finalUrl, true);
  370. return finalUrl;
  371. } catch (e) {
  372. AVATAR_CHECKED_URLS.set(url, false);
  373. }
  374. }
  375. }
  376. }
  377. return undefined;
  378. }