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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. // @flow
  2. import { getGravatarURL } from '@jitsi/js-utils/avatar';
  3. import type { Store } from 'redux';
  4. import { isStageFilmstripEnabled } from '../../filmstrip/functions';
  5. import { GRAVATAR_BASE_URL, isCORSAvatarURL } from '../avatar';
  6. import { getSourceNameSignalingFeatureFlag } from '../config';
  7. import { JitsiParticipantConnectionStatus } from '../lib-jitsi-meet';
  8. import { MEDIA_TYPE, shouldRenderVideoTrack } from '../media';
  9. import { toState } from '../redux';
  10. import { getTrackByMediaTypeAndParticipant } from '../tracks';
  11. import { createDeferred } from '../util';
  12. import {
  13. JIGASI_PARTICIPANT_ICON,
  14. MAX_DISPLAY_NAME_LENGTH,
  15. PARTICIPANT_ROLE
  16. } from './constants';
  17. import { preloadImage } from './preloadImage';
  18. /**
  19. * Temp structures for avatar urls to be checked/preloaded.
  20. */
  21. const AVATAR_QUEUE = [];
  22. const AVATAR_CHECKED_URLS = new Map();
  23. /* eslint-disable arrow-body-style, no-unused-vars */
  24. const AVATAR_CHECKER_FUNCTIONS = [
  25. (participant, _) => {
  26. return participant && participant.isJigasi ? JIGASI_PARTICIPANT_ICON : null;
  27. },
  28. (participant, _) => {
  29. return participant && participant.avatarURL ? participant.avatarURL : null;
  30. },
  31. (participant, store) => {
  32. if (participant && participant.email) {
  33. // TODO: remove once libravatar has deployed their new scaled up infra. -saghul
  34. const gravatarBaseURL
  35. = store.getState()['features/base/config'].gravatarBaseURL ?? GRAVATAR_BASE_URL;
  36. return getGravatarURL(participant.email, gravatarBaseURL);
  37. }
  38. return null;
  39. }
  40. ];
  41. /* eslint-enable arrow-body-style, no-unused-vars */
  42. /**
  43. * Resolves the first loadable avatar URL for a participant.
  44. *
  45. * @param {Object} participant - The participant to resolve avatars for.
  46. * @param {Store} store - Redux store.
  47. * @returns {Promise}
  48. */
  49. export function getFirstLoadableAvatarUrl(participant: Object, store: Store<any, any>) {
  50. const deferred = createDeferred();
  51. const fullPromise = deferred.promise
  52. .then(() => _getFirstLoadableAvatarUrl(participant, store))
  53. .then(result => {
  54. if (AVATAR_QUEUE.length) {
  55. const next = AVATAR_QUEUE.shift();
  56. next.resolve();
  57. }
  58. return result;
  59. });
  60. if (AVATAR_QUEUE.length) {
  61. AVATAR_QUEUE.push(deferred);
  62. } else {
  63. deferred.resolve();
  64. }
  65. return fullPromise;
  66. }
  67. /**
  68. * Returns local participant from Redux state.
  69. *
  70. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  71. * {@code getState} function to be used to retrieve the state
  72. * features/base/participants.
  73. * @returns {(Participant|undefined)}
  74. */
  75. export function getLocalParticipant(stateful: Object | Function) {
  76. const state = toState(stateful)['features/base/participants'];
  77. return state.local;
  78. }
  79. /**
  80. * Returns local screen share participant from Redux state.
  81. *
  82. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  83. * {@code getState} function to be used to retrieve the state features/base/participants.
  84. * @returns {(Participant|undefined)}
  85. */
  86. export function getLocalScreenShareParticipant(stateful: Object | Function) {
  87. const state = toState(stateful)['features/base/participants'];
  88. return state.localScreenShare;
  89. }
  90. /**
  91. * Returns screenshare participant.
  92. *
  93. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's {@code getState} function to be used to
  94. * retrieve the state features/base/participants.
  95. * @param {string} id - The owner ID of the screenshare participant to retrieve.
  96. * @returns {(Participant|undefined)}
  97. */
  98. export function getScreenshareParticipantByOwnerId(stateful: Object | Function, id: string) {
  99. const track = getTrackByMediaTypeAndParticipant(
  100. toState(stateful)['features/base/tracks'], MEDIA_TYPE.SCREENSHARE, id
  101. );
  102. return getParticipantById(stateful, track?.jitsiTrack.getSourceName());
  103. }
  104. /**
  105. * Normalizes a display name so then no invalid values (padding, length...etc)
  106. * can be set.
  107. *
  108. * @param {string} name - The display name to set.
  109. * @returns {string}
  110. */
  111. export function getNormalizedDisplayName(name: string) {
  112. if (!name || !name.trim()) {
  113. return undefined;
  114. }
  115. return name.trim().substring(0, MAX_DISPLAY_NAME_LENGTH);
  116. }
  117. /**
  118. * Returns participant by ID from Redux state.
  119. *
  120. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  121. * {@code getState} function to be used to retrieve the state
  122. * features/base/participants.
  123. * @param {string} id - The ID of the participant to retrieve.
  124. * @private
  125. * @returns {(Participant|undefined)}
  126. */
  127. export function getParticipantById(
  128. stateful: Object | Function, id: string): ?Object {
  129. const state = toState(stateful)['features/base/participants'];
  130. const { local, localScreenShare, remote } = state;
  131. return remote.get(id)
  132. || (local?.id === id ? local : undefined)
  133. || (localScreenShare?.id === id ? localScreenShare : undefined);
  134. }
  135. /**
  136. * Returns the participant with the ID matching the passed ID or the local participant if the ID is
  137. * undefined.
  138. *
  139. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  140. * {@code getState} function to be used to retrieve the state
  141. * features/base/participants.
  142. * @param {string|undefined} [participantID] - An optional partipantID argument.
  143. * @returns {Participant|undefined}
  144. */
  145. export function getParticipantByIdOrUndefined(stateful: Object | Function, participantID: ?string) {
  146. return participantID ? getParticipantById(stateful, participantID) : getLocalParticipant(stateful);
  147. }
  148. /**
  149. * Returns a count of the known participants in the passed in redux state,
  150. * excluding any fake participants.
  151. *
  152. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  153. * {@code getState} function to be used to retrieve the state
  154. * features/base/participants.
  155. * @returns {number}
  156. */
  157. export function getParticipantCount(stateful: Object | Function) {
  158. const state = toState(stateful);
  159. const {
  160. local,
  161. remote,
  162. fakeParticipants,
  163. sortedRemoteFakeScreenShareParticipants
  164. } = state['features/base/participants'];
  165. if (getSourceNameSignalingFeatureFlag(state)) {
  166. return remote.size - fakeParticipants.size - sortedRemoteFakeScreenShareParticipants.size + (local ? 1 : 0);
  167. }
  168. return remote.size - fakeParticipants.size + (local ? 1 : 0);
  169. }
  170. /**
  171. * Returns participant ID of the owner of a fake screenshare participant.
  172. *
  173. * @param {string} id - The ID of the fake screenshare participant.
  174. * @private
  175. * @returns {(string|undefined)}
  176. */
  177. export function getFakeScreenShareParticipantOwnerId(id: string) {
  178. return id.split('-')[0];
  179. }
  180. /**
  181. * Returns the Map with fake participants.
  182. *
  183. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  184. * {@code getState} function to be used to retrieve the state
  185. * features/base/participants.
  186. * @returns {Map<string, Participant>} - The Map with fake participants.
  187. */
  188. export function getFakeParticipants(stateful: Object | Function) {
  189. return toState(stateful)['features/base/participants'].fakeParticipants;
  190. }
  191. /**
  192. * Returns a count of the known remote participants in the passed in redux state.
  193. *
  194. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  195. * {@code getState} function to be used to retrieve the state
  196. * features/base/participants.
  197. * @returns {number}
  198. */
  199. export function getRemoteParticipantCount(stateful: Object | Function) {
  200. const state = toState(stateful)['features/base/participants'];
  201. if (getSourceNameSignalingFeatureFlag(state)) {
  202. return state.remote.size - state.sortedRemoteFakeScreenShareParticipants.size;
  203. }
  204. return state.remote.size;
  205. }
  206. /**
  207. * Returns a count of the known participants in the passed in redux state,
  208. * including fake participants.
  209. *
  210. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  211. * {@code getState} function to be used to retrieve the state
  212. * features/base/participants.
  213. * @returns {number}
  214. */
  215. export function getParticipantCountWithFake(stateful: Object | Function) {
  216. const state = toState(stateful);
  217. const { local, localScreenShare, remote } = state['features/base/participants'];
  218. if (getSourceNameSignalingFeatureFlag(state)) {
  219. return remote.size + (local ? 1 : 0) + (localScreenShare ? 1 : 0);
  220. }
  221. return remote.size + (local ? 1 : 0);
  222. }
  223. /**
  224. * Returns participant's display name.
  225. *
  226. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's {@code getState} function to be used to
  227. * retrieve the state.
  228. * @param {string} id - The ID of the participant's display name to retrieve.
  229. * @returns {string}
  230. */
  231. export function getParticipantDisplayName(stateful: Object | Function, id: string) {
  232. const participant = getParticipantById(stateful, id);
  233. const {
  234. defaultLocalDisplayName,
  235. defaultRemoteDisplayName
  236. } = toState(stateful)['features/base/config'];
  237. if (participant) {
  238. if (participant.isFakeScreenShareParticipant) {
  239. return getScreenshareParticipantDisplayName(stateful, id);
  240. }
  241. if (participant.name) {
  242. return participant.name;
  243. }
  244. if (participant.local) {
  245. return defaultLocalDisplayName;
  246. }
  247. }
  248. return defaultRemoteDisplayName;
  249. }
  250. /**
  251. * Returns screenshare participant's display name.
  252. *
  253. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's {@code getState} function to be used to
  254. * retrieve the state.
  255. * @param {string} id - The ID of the screenshare participant's display name to retrieve.
  256. * @returns {string}
  257. */
  258. export function getScreenshareParticipantDisplayName(stateful: Object | Function, id: string) {
  259. const owner = getParticipantById(stateful, getFakeScreenShareParticipantOwnerId(id));
  260. return `${owner.name}'s screen`;
  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 = isStageFilmstripEnabled(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. }