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 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. // @flow
  2. import { getGravatarURL } from '@jitsi/js-utils/avatar';
  3. import type { Store } from 'redux';
  4. import { i18next } from '../../base/i18n';
  5. import { isStageFilmstripAvailable } from '../../filmstrip/functions';
  6. import { GRAVATAR_BASE_URL, isCORSAvatarURL } from '../avatar';
  7. import { getMultipleVideoSupportFeatureFlag, getSourceNameSignalingFeatureFlag } from '../config';
  8. import { JitsiParticipantConnectionStatus } from '../lib-jitsi-meet';
  9. import { MEDIA_TYPE, shouldRenderVideoTrack } from '../media';
  10. import { toState } from '../redux';
  11. import { getScreenShareTrack, getTrackByMediaTypeAndParticipant } from '../tracks';
  12. import { createDeferred } from '../util';
  13. import { JIGASI_PARTICIPANT_ICON, MAX_DISPLAY_NAME_LENGTH, PARTICIPANT_ROLE } from './constants';
  14. import { preloadImage } from './preloadImage';
  15. /**
  16. * Temp structures for avatar urls to be checked/preloaded.
  17. */
  18. const AVATAR_QUEUE = [];
  19. const AVATAR_CHECKED_URLS = new Map();
  20. /* eslint-disable arrow-body-style, no-unused-vars */
  21. const AVATAR_CHECKER_FUNCTIONS = [
  22. (participant, _) => {
  23. return participant && participant.isJigasi ? JIGASI_PARTICIPANT_ICON : null;
  24. },
  25. (participant, _) => {
  26. return participant && participant.avatarURL ? participant.avatarURL : null;
  27. },
  28. (participant, store) => {
  29. if (participant && participant.email) {
  30. // TODO: remove once libravatar has deployed their new scaled up infra. -saghul
  31. const gravatarBaseURL
  32. = store.getState()['features/base/config'].gravatarBaseURL ?? GRAVATAR_BASE_URL;
  33. return getGravatarURL(participant.email, gravatarBaseURL);
  34. }
  35. return null;
  36. }
  37. ];
  38. /* eslint-enable arrow-body-style, no-unused-vars */
  39. /**
  40. * Resolves the first loadable avatar URL for a participant.
  41. *
  42. * @param {Object} participant - The participant to resolve avatars for.
  43. * @param {Store} store - Redux store.
  44. * @returns {Promise}
  45. */
  46. export function getFirstLoadableAvatarUrl(participant: Object, store: Store<any, any>) {
  47. const deferred = createDeferred();
  48. const fullPromise = deferred.promise
  49. .then(() => _getFirstLoadableAvatarUrl(participant, store))
  50. .then(result => {
  51. if (AVATAR_QUEUE.length) {
  52. const next = AVATAR_QUEUE.shift();
  53. next.resolve();
  54. }
  55. return result;
  56. });
  57. if (AVATAR_QUEUE.length) {
  58. AVATAR_QUEUE.push(deferred);
  59. } else {
  60. deferred.resolve();
  61. }
  62. return fullPromise;
  63. }
  64. /**
  65. * Returns local participant from Redux state.
  66. *
  67. * @param {(Function|Object)} stateful - 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 state = toState(stateful)['features/base/participants'];
  74. return state.local;
  75. }
  76. /**
  77. * Returns local screen share participant from Redux state.
  78. *
  79. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  80. * {@code getState} function to be used to retrieve the state features/base/participants.
  81. * @returns {(Participant|undefined)}
  82. */
  83. export function getLocalScreenShareParticipant(stateful: Object | Function) {
  84. const state = toState(stateful)['features/base/participants'];
  85. return state.localScreenShare;
  86. }
  87. /**
  88. * Returns screenshare participant.
  89. *
  90. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's {@code getState} function to be used to
  91. * retrieve the state features/base/participants.
  92. * @param {string} id - The owner ID of the screenshare participant to retrieve.
  93. * @returns {(Participant|undefined)}
  94. */
  95. export function getVirtualScreenshareParticipantByOwnerId(stateful: Object | Function, id: string) {
  96. const state = toState(stateful);
  97. if (getMultipleVideoSupportFeatureFlag(state)) {
  98. const track = getScreenShareTrack(state['features/base/tracks'], id);
  99. return getParticipantById(stateful, track?.jitsiTrack.getSourceName());
  100. }
  101. return;
  102. }
  103. /**
  104. * Normalizes a display name so then no invalid values (padding, length...etc)
  105. * can be set.
  106. *
  107. * @param {string} name - The display name to set.
  108. * @returns {string}
  109. */
  110. export function getNormalizedDisplayName(name: string) {
  111. if (!name || !name.trim()) {
  112. return undefined;
  113. }
  114. return name.trim().substring(0, MAX_DISPLAY_NAME_LENGTH);
  115. }
  116. /**
  117. * Returns participant by ID from Redux state.
  118. *
  119. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  120. * {@code getState} function to be used to retrieve the state
  121. * features/base/participants.
  122. * @param {string} id - The ID of the participant to retrieve.
  123. * @private
  124. * @returns {(Participant|undefined)}
  125. */
  126. export function getParticipantById(
  127. stateful: Object | Function, id: string): ?Object {
  128. const state = toState(stateful)['features/base/participants'];
  129. const { local, localScreenShare, remote } = state;
  130. return remote.get(id)
  131. || (local?.id === id ? local : undefined)
  132. || (localScreenShare?.id === id ? localScreenShare : undefined);
  133. }
  134. /**
  135. * Returns the participant with the ID matching the passed ID or the local participant if the ID is
  136. * undefined.
  137. *
  138. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  139. * {@code getState} function to be used to retrieve the state
  140. * features/base/participants.
  141. * @param {string|undefined} [participantID] - An optional partipantID argument.
  142. * @returns {Participant|undefined}
  143. */
  144. export function getParticipantByIdOrUndefined(stateful: Object | Function, participantID: ?string) {
  145. return participantID ? getParticipantById(stateful, participantID) : getLocalParticipant(stateful);
  146. }
  147. /**
  148. * Returns a count of the known participants in the passed in redux state,
  149. * excluding any fake participants.
  150. *
  151. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  152. * {@code getState} function to be used to retrieve the state
  153. * features/base/participants.
  154. * @returns {number}
  155. */
  156. export function getParticipantCount(stateful: Object | Function) {
  157. const state = toState(stateful);
  158. const {
  159. local,
  160. remote,
  161. fakeParticipants,
  162. sortedRemoteVirtualScreenshareParticipants
  163. } = state['features/base/participants'];
  164. if (getSourceNameSignalingFeatureFlag(state)) {
  165. return remote.size - fakeParticipants.size - sortedRemoteVirtualScreenshareParticipants.size + (local ? 1 : 0);
  166. }
  167. return remote.size - fakeParticipants.size + (local ? 1 : 0);
  168. }
  169. /**
  170. * Returns participant ID of the owner of a virtual screenshare participant.
  171. *
  172. * @param {string} id - The ID of the virtual screenshare participant.
  173. * @private
  174. * @returns {(string|undefined)}
  175. */
  176. export function getVirtualScreenshareParticipantOwnerId(id: string) {
  177. return id.split('-')[0];
  178. }
  179. /**
  180. * Returns the Map with fake participants.
  181. *
  182. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  183. * {@code getState} function to be used to retrieve the state
  184. * features/base/participants.
  185. * @returns {Map<string, Participant>} - The Map with fake participants.
  186. */
  187. export function getFakeParticipants(stateful: Object | Function) {
  188. return toState(stateful)['features/base/participants'].fakeParticipants;
  189. }
  190. /**
  191. * Returns a count of the known remote participants in the passed in redux state.
  192. *
  193. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  194. * {@code getState} function to be used to retrieve the state
  195. * features/base/participants.
  196. * @returns {number}
  197. */
  198. export function getRemoteParticipantCount(stateful: Object | Function) {
  199. const state = toState(stateful)['features/base/participants'];
  200. if (getSourceNameSignalingFeatureFlag(state)) {
  201. return state.remote.size - state.sortedRemoteVirtualScreenshareParticipants.size;
  202. }
  203. return state.remote.size;
  204. }
  205. /**
  206. * Returns a count of the known participants in the passed in redux state,
  207. * including fake participants.
  208. *
  209. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  210. * {@code getState} function to be used to retrieve the state
  211. * features/base/participants.
  212. * @returns {number}
  213. */
  214. export function getParticipantCountWithFake(stateful: Object | Function) {
  215. const state = toState(stateful);
  216. const { local, localScreenShare, remote } = state['features/base/participants'];
  217. if (getSourceNameSignalingFeatureFlag(state)) {
  218. return remote.size + (local ? 1 : 0) + (localScreenShare ? 1 : 0);
  219. }
  220. return remote.size + (local ? 1 : 0);
  221. }
  222. /**
  223. * Returns participant's display name.
  224. *
  225. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's {@code getState} function to be used to
  226. * retrieve the state.
  227. * @param {string} id - The ID of the participant's display name to retrieve.
  228. * @returns {string}
  229. */
  230. export function getParticipantDisplayName(stateful: Object | Function, id: string) {
  231. const participant = getParticipantById(stateful, id);
  232. const {
  233. defaultLocalDisplayName,
  234. defaultRemoteDisplayName
  235. } = toState(stateful)['features/base/config'];
  236. if (participant) {
  237. if (participant.isVirtualScreenshareParticipant) {
  238. return getScreenshareParticipantDisplayName(stateful, id);
  239. }
  240. if (participant.name) {
  241. return participant.name;
  242. }
  243. if (participant.local) {
  244. return defaultLocalDisplayName;
  245. }
  246. }
  247. return defaultRemoteDisplayName;
  248. }
  249. /**
  250. * Returns screenshare participant's display name.
  251. *
  252. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's {@code getState} function to be used to
  253. * retrieve the state.
  254. * @param {string} id - The ID of the screenshare participant's display name to retrieve.
  255. * @returns {string}
  256. */
  257. export function getScreenshareParticipantDisplayName(stateful: Object | Function, id: string) {
  258. const owner = getParticipantById(stateful, getVirtualScreenshareParticipantOwnerId(id));
  259. const name = owner.name;
  260. return i18next.t('screenshareDisplayName', { name });
  261. }
  262. /**
  263. * Returns the presence status of a participant associated with the passed id.
  264. *
  265. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  266. * {@code getState} function to be used to retrieve the state.
  267. * @param {string} id - The id of the participant.
  268. * @returns {string} - The presence status.
  269. */
  270. export function getParticipantPresenceStatus(
  271. stateful: Object | Function, id: string) {
  272. if (!id) {
  273. return undefined;
  274. }
  275. const participantById = getParticipantById(stateful, id);
  276. if (!participantById) {
  277. return undefined;
  278. }
  279. return participantById.presence;
  280. }
  281. /**
  282. * Returns true if there is at least 1 participant with screen sharing feature and false otherwise.
  283. *
  284. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  285. * {@code getState} function to be used to retrieve the state.
  286. * @returns {boolean}
  287. */
  288. export function haveParticipantWithScreenSharingFeature(stateful: Object | Function) {
  289. return toState(stateful)['features/base/participants'].haveParticipantWithScreenSharingFeature;
  290. }
  291. /**
  292. * Selectors for getting all remote participants.
  293. *
  294. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  295. * {@code getState} function to be used to retrieve the state
  296. * features/base/participants.
  297. * @returns {Map<string, Object>}
  298. */
  299. export function getRemoteParticipants(stateful: Object | Function) {
  300. return toState(stateful)['features/base/participants'].remote;
  301. }
  302. /**
  303. * Selectors for the getting the remote participants in the order that they are displayed in the filmstrip.
  304. *
  305. @param {(Function|Object)} stateful - The (whole) redux state, or redux's {@code getState} function to be used to
  306. * retrieve the state features/filmstrip.
  307. * @returns {Array<string>}
  308. */
  309. export function getRemoteParticipantsSorted(stateful: Object | Function) {
  310. return toState(stateful)['features/filmstrip'].remoteParticipants;
  311. }
  312. /**
  313. * Returns the participant which has its pinned state set to truthy.
  314. *
  315. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  316. * {@code getState} function to be used to retrieve the state
  317. * features/base/participants.
  318. * @returns {(Participant|undefined)}
  319. */
  320. export function getPinnedParticipant(stateful: Object | Function) {
  321. const state = toState(stateful);
  322. const { pinnedParticipant } = state['features/base/participants'];
  323. const stageFilmstrip = isStageFilmstripAvailable(state);
  324. if (stageFilmstrip) {
  325. const { activeParticipants } = state['features/filmstrip'];
  326. const id = activeParticipants.find(p => p.pinned)?.participantId;
  327. return id ? getParticipantById(stateful, id) : undefined;
  328. }
  329. if (!pinnedParticipant) {
  330. return undefined;
  331. }
  332. return getParticipantById(stateful, pinnedParticipant);
  333. }
  334. /**
  335. * Returns true if the participant is a moderator.
  336. *
  337. * @param {string} participant - Participant object.
  338. * @returns {boolean}
  339. */
  340. export function isParticipantModerator(participant: Object) {
  341. return participant?.role === PARTICIPANT_ROLE.MODERATOR;
  342. }
  343. /**
  344. * Returns the dominant speaker participant.
  345. *
  346. * @param {(Function|Object)} stateful - The (whole) redux state or redux's
  347. * {@code getState} function to be used to retrieve the state features/base/participants.
  348. * @returns {Participant} - The participant from the redux store.
  349. */
  350. export function getDominantSpeakerParticipant(stateful: Object | Function) {
  351. const state = toState(stateful)['features/base/participants'];
  352. const { dominantSpeaker } = state;
  353. if (!dominantSpeaker) {
  354. return undefined;
  355. }
  356. return getParticipantById(stateful, dominantSpeaker);
  357. }
  358. /**
  359. * Returns true if all of the meeting participants are moderators.
  360. *
  361. * @param {Object|Function} stateful -Object or function that can be resolved
  362. * to the Redux state.
  363. * @returns {boolean}
  364. */
  365. export function isEveryoneModerator(stateful: Object | Function) {
  366. const state = toState(stateful)['features/base/participants'];
  367. return state.everyoneIsModerator === true;
  368. }
  369. /**
  370. * Checks a value and returns true if it's a preloaded icon object.
  371. *
  372. * @param {?string | ?Object} icon - The icon to check.
  373. * @returns {boolean}
  374. */
  375. export function isIconUrl(icon: ?string | ?Object) {
  376. return Boolean(icon) && (typeof icon === 'object' || typeof icon === 'function');
  377. }
  378. /**
  379. * Returns true if the current local participant is a moderator in the
  380. * conference.
  381. *
  382. * @param {Object|Function} stateful - Object or function that can be resolved
  383. * to the Redux state.
  384. * @returns {boolean}
  385. */
  386. export function isLocalParticipantModerator(stateful: Object | Function) {
  387. const state = toState(stateful)['features/base/participants'];
  388. const { local } = state;
  389. if (!local) {
  390. return false;
  391. }
  392. return isParticipantModerator(local);
  393. }
  394. /**
  395. * Returns true if the video of the participant should be rendered.
  396. * NOTE: This is currently only used on mobile.
  397. *
  398. * @param {Object|Function} stateful - Object or function that can be resolved
  399. * to the Redux state.
  400. * @param {string} id - The ID of the participant.
  401. * @returns {boolean}
  402. */
  403. export function shouldRenderParticipantVideo(stateful: Object | Function, id: string) {
  404. const state = toState(stateful);
  405. const participant = getParticipantById(state, id);
  406. if (!participant) {
  407. return false;
  408. }
  409. /* First check if we have an unmuted video track. */
  410. const videoTrack
  411. = getTrackByMediaTypeAndParticipant(state['features/base/tracks'], MEDIA_TYPE.VIDEO, id);
  412. if (!shouldRenderVideoTrack(videoTrack, /* waitForVideoStarted */ false)) {
  413. return false;
  414. }
  415. /* Then check if the participant connection is active. */
  416. const connectionStatus = participant.connectionStatus || JitsiParticipantConnectionStatus.ACTIVE;
  417. if (connectionStatus !== JitsiParticipantConnectionStatus.ACTIVE) {
  418. return false;
  419. }
  420. /* Then check if audio-only mode is not active. */
  421. const audioOnly = state['features/base/audio-only'].enabled;
  422. if (!audioOnly) {
  423. return true;
  424. }
  425. /* Last, check if the participant is sharing their screen and they are on stage. */
  426. const remoteScreenShares = state['features/video-layout'].remoteScreenShares || [];
  427. const largeVideoParticipantId = state['features/large-video'].participantId;
  428. const participantIsInLargeVideoWithScreen
  429. = participant.id === largeVideoParticipantId && remoteScreenShares.includes(participant.id);
  430. return participantIsInLargeVideoWithScreen;
  431. }
  432. /**
  433. * Resolves the first loadable avatar URL for a participant.
  434. *
  435. * @param {Object} participant - The participant to resolve avatars for.
  436. * @param {Store} store - Redux store.
  437. * @returns {?string}
  438. */
  439. async function _getFirstLoadableAvatarUrl(participant, store) {
  440. for (let i = 0; i < AVATAR_CHECKER_FUNCTIONS.length; i++) {
  441. const url = AVATAR_CHECKER_FUNCTIONS[i](participant, store);
  442. if (url !== null) {
  443. if (AVATAR_CHECKED_URLS.has(url)) {
  444. const { isLoadable, isUsingCORS } = AVATAR_CHECKED_URLS.get(url) || {};
  445. if (isLoadable) {
  446. return {
  447. isUsingCORS,
  448. src: url
  449. };
  450. }
  451. } else {
  452. try {
  453. const { corsAvatarURLs } = store.getState()['features/base/config'];
  454. const { isUsingCORS, src } = await preloadImage(url, isCORSAvatarURL(url, corsAvatarURLs));
  455. AVATAR_CHECKED_URLS.set(src, {
  456. isLoadable: true,
  457. isUsingCORS
  458. });
  459. return {
  460. isUsingCORS,
  461. src
  462. };
  463. } catch (e) {
  464. AVATAR_CHECKED_URLS.set(url, {
  465. isLoadable: false,
  466. isUsingCORS: false
  467. });
  468. }
  469. }
  470. }
  471. }
  472. return undefined;
  473. }
  474. /**
  475. * Get the participants queue with raised hands.
  476. *
  477. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  478. * {@code getState} function to be used to retrieve the state
  479. * features/base/participants.
  480. * @returns {Array<Object>}
  481. */
  482. export function getRaiseHandsQueue(stateful: Object | Function): Array<Object> {
  483. const { raisedHandsQueue } = toState(stateful)['features/base/participants'];
  484. return raisedHandsQueue;
  485. }
  486. /**
  487. * Returns whether the given participant has his hand raised or not.
  488. *
  489. * @param {Object} participant - The participant.
  490. * @returns {boolean} - Whether participant has raise hand or not.
  491. */
  492. export function hasRaisedHand(participant: Object): boolean {
  493. return Boolean(participant && participant.raisedHandTimestamp);
  494. }