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.any.ts 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. import { IReduxState, IStore } from '../../app/types';
  2. import {
  3. getMultipleVideoSendingSupportFeatureFlag
  4. } from '../config/functions.any';
  5. import { JitsiTrackErrors, browser } from '../lib-jitsi-meet';
  6. import { gumPending } from '../media/actions';
  7. import { CAMERA_FACING_MODE, MEDIA_TYPE, MediaType, VIDEO_TYPE } from '../media/constants';
  8. import { IMediaState } from '../media/reducer';
  9. import { IGUMPendingState } from '../media/types';
  10. import {
  11. getVirtualScreenshareParticipantOwnerId,
  12. isScreenShareParticipant
  13. } from '../participants/functions';
  14. import { IParticipant } from '../participants/types';
  15. import logger from './logger';
  16. import { ITrack } from './types';
  17. /**
  18. * Returns root tracks state.
  19. *
  20. * @param {IReduxState} state - Global state.
  21. * @returns {Object} Tracks state.
  22. */
  23. export const getTrackState = (state: IReduxState) => state['features/base/tracks'];
  24. /**
  25. * Checks if the passed media type is muted for the participant.
  26. *
  27. * @param {IParticipant} participant - Participant reference.
  28. * @param {MediaType} mediaType - Media type.
  29. * @param {IReduxState} state - Global state.
  30. * @returns {boolean} - Is the media type muted for the participant.
  31. */
  32. export function isParticipantMediaMuted(participant: IParticipant | undefined,
  33. mediaType: MediaType, state: IReduxState) {
  34. if (!participant) {
  35. return false;
  36. }
  37. const tracks = getTrackState(state);
  38. if (participant?.local) {
  39. return isLocalTrackMuted(tracks, mediaType);
  40. } else if (!participant?.fakeParticipant) {
  41. return isRemoteTrackMuted(tracks, mediaType, participant.id);
  42. }
  43. return true;
  44. }
  45. /**
  46. * Checks if the participant is audio muted.
  47. *
  48. * @param {IParticipant} participant - Participant reference.
  49. * @param {IReduxState} state - Global state.
  50. * @returns {boolean} - Is audio muted for the participant.
  51. */
  52. export function isParticipantAudioMuted(participant: IParticipant, state: IReduxState) {
  53. return isParticipantMediaMuted(participant, MEDIA_TYPE.AUDIO, state);
  54. }
  55. /**
  56. * Checks if the participant is video muted.
  57. *
  58. * @param {IParticipant} participant - Participant reference.
  59. * @param {IReduxState} state - Global state.
  60. * @returns {boolean} - Is video muted for the participant.
  61. */
  62. export function isParticipantVideoMuted(participant: IParticipant | undefined, state: IReduxState) {
  63. return isParticipantMediaMuted(participant, MEDIA_TYPE.VIDEO, state);
  64. }
  65. /**
  66. * Returns local audio track.
  67. *
  68. * @param {ITrack[]} tracks - List of all tracks.
  69. * @returns {(Track|undefined)}
  70. */
  71. export function getLocalAudioTrack(tracks: ITrack[]) {
  72. return getLocalTrack(tracks, MEDIA_TYPE.AUDIO);
  73. }
  74. /**
  75. * Returns the local desktop track.
  76. *
  77. * @param {Track[]} tracks - List of all tracks.
  78. * @param {boolean} [includePending] - Indicates whether a local track is to be returned if it is still pending.
  79. * A local track is pending if {@code getUserMedia} is still executing to create it and, consequently, its
  80. * {@code jitsiTrack} property is {@code undefined}. By default a pending local track is not returned.
  81. * @returns {(Track|undefined)}
  82. */
  83. export function getLocalDesktopTrack(tracks: ITrack[], includePending = false) {
  84. return (
  85. getLocalTracks(tracks, includePending)
  86. .find(t => t.mediaType === MEDIA_TYPE.SCREENSHARE || t.videoType === VIDEO_TYPE.DESKTOP));
  87. }
  88. /**
  89. * Returns the stored local desktop jitsiLocalTrack.
  90. *
  91. * @param {IReduxState} state - The redux state.
  92. * @returns {JitsiLocalTrack|undefined}
  93. */
  94. export function getLocalJitsiDesktopTrack(state: IReduxState) {
  95. const track = getLocalDesktopTrack(getTrackState(state));
  96. return track?.jitsiTrack;
  97. }
  98. /**
  99. * Returns local track by media type.
  100. *
  101. * @param {ITrack[]} tracks - List of all tracks.
  102. * @param {MediaType} mediaType - Media type.
  103. * @param {boolean} [includePending] - Indicates whether a local track is to be
  104. * returned if it is still pending. A local track is pending if
  105. * {@code getUserMedia} is still executing to create it and, consequently, its
  106. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  107. * track is not returned.
  108. * @returns {(Track|undefined)}
  109. */
  110. export function getLocalTrack(tracks: ITrack[], mediaType: MediaType, includePending = false) {
  111. return (
  112. getLocalTracks(tracks, includePending)
  113. .find(t => t.mediaType === mediaType));
  114. }
  115. /**
  116. * Returns an array containing the local tracks with or without a (valid)
  117. * {@code JitsiTrack}.
  118. *
  119. * @param {ITrack[]} tracks - An array containing all local tracks.
  120. * @param {boolean} [includePending] - Indicates whether a local track is to be
  121. * returned if it is still pending. A local track is pending if
  122. * {@code getUserMedia} is still executing to create it and, consequently, its
  123. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  124. * track is not returned.
  125. * @returns {Track[]}
  126. */
  127. export function getLocalTracks(tracks: ITrack[], includePending = false) {
  128. // XXX A local track is considered ready only once it has its `jitsiTrack`
  129. // property set by the `TRACK_ADDED` action. Until then there is a stub
  130. // added just before the `getUserMedia` call with a cancellable
  131. // `gumInProgress` property which then can be used to destroy the track that
  132. // has not yet been added to the redux store. Once GUM is cancelled, it will
  133. // never make it to the store nor there will be any
  134. // `TRACK_ADDED`/`TRACK_REMOVED` actions dispatched for it.
  135. return tracks.filter(t => t.local && (t.jitsiTrack || includePending));
  136. }
  137. /**
  138. * Returns local video track.
  139. *
  140. * @param {ITrack[]} tracks - List of all tracks.
  141. * @returns {(Track|undefined)}
  142. */
  143. export function getLocalVideoTrack(tracks: ITrack[]) {
  144. return getLocalTrack(tracks, MEDIA_TYPE.VIDEO);
  145. }
  146. /**
  147. * Returns the stored local video track.
  148. *
  149. * @param {IReduxState} state - The redux state.
  150. * @returns {Object}
  151. */
  152. export function getLocalJitsiVideoTrack(state: IReduxState) {
  153. const track = getLocalVideoTrack(getTrackState(state));
  154. return track?.jitsiTrack;
  155. }
  156. /**
  157. * Returns the stored local audio track.
  158. *
  159. * @param {IReduxState} state - The redux state.
  160. * @returns {Object}
  161. */
  162. export function getLocalJitsiAudioTrack(state: IReduxState) {
  163. const track = getLocalAudioTrack(getTrackState(state));
  164. return track?.jitsiTrack;
  165. }
  166. /**
  167. * Returns track of specified media type for specified participant.
  168. *
  169. * @param {IReduxState} state - The redux state.
  170. * @param {IParticipant} participant - Participant Object.
  171. * @returns {(Track|undefined)}
  172. */
  173. export function getVideoTrackByParticipant(
  174. state: IReduxState,
  175. participant?: IParticipant) {
  176. if (!participant) {
  177. return;
  178. }
  179. const tracks = state['features/base/tracks'];
  180. if (isScreenShareParticipant(participant)) {
  181. return getVirtualScreenshareParticipantTrack(tracks, participant.id);
  182. }
  183. return getTrackByMediaTypeAndParticipant(tracks, MEDIA_TYPE.VIDEO, participant.id);
  184. }
  185. /**
  186. * Returns track of specified media type for specified participant id.
  187. *
  188. * @param {ITrack[]} tracks - List of all tracks.
  189. * @param {MediaType} mediaType - Media type.
  190. * @param {string} participantId - Participant ID.
  191. * @returns {(Track|undefined)}
  192. */
  193. export function getTrackByMediaTypeAndParticipant(
  194. tracks: ITrack[],
  195. mediaType: MediaType,
  196. participantId?: string) {
  197. return tracks.find(
  198. t => Boolean(t.jitsiTrack) && t.participantId === participantId && t.mediaType === mediaType
  199. );
  200. }
  201. /**
  202. * Returns track for specified participant id.
  203. *
  204. * @param {ITrack[]} tracks - List of all tracks.
  205. * @param {string} participantId - Participant ID.
  206. * @returns {(Track[]|undefined)}
  207. */
  208. export function getTrackByParticipantId(tracks: ITrack[], participantId: string) {
  209. return tracks.filter(t => t.participantId === participantId);
  210. }
  211. /**
  212. * Returns screenshare track of given virtualScreenshareParticipantId.
  213. *
  214. * @param {ITrack[]} tracks - List of all tracks.
  215. * @param {string} virtualScreenshareParticipantId - Virtual Screenshare Participant ID.
  216. * @returns {(Track|undefined)}
  217. */
  218. export function getVirtualScreenshareParticipantTrack(tracks: ITrack[], virtualScreenshareParticipantId: string) {
  219. const ownderId = getVirtualScreenshareParticipantOwnerId(virtualScreenshareParticipantId);
  220. return getScreenShareTrack(tracks, ownderId);
  221. }
  222. /**
  223. * Returns screenshare track of given owner ID.
  224. *
  225. * @param {Track[]} tracks - List of all tracks.
  226. * @param {string} ownerId - Screenshare track owner ID.
  227. * @returns {(Track|undefined)}
  228. */
  229. export function getScreenShareTrack(tracks: ITrack[], ownerId: string) {
  230. return tracks.find(
  231. t => Boolean(t.jitsiTrack)
  232. && t.participantId === ownerId
  233. && (t.mediaType === MEDIA_TYPE.SCREENSHARE || t.videoType === VIDEO_TYPE.DESKTOP)
  234. );
  235. }
  236. /**
  237. * Returns track source name of specified media type for specified participant id.
  238. *
  239. * @param {ITrack[]} tracks - List of all tracks.
  240. * @param {MediaType} mediaType - Media type.
  241. * @param {string} participantId - Participant ID.
  242. * @returns {(string|undefined)}
  243. */
  244. export function getTrackSourceNameByMediaTypeAndParticipant(
  245. tracks: ITrack[],
  246. mediaType: MediaType,
  247. participantId: string) {
  248. const track = getTrackByMediaTypeAndParticipant(
  249. tracks,
  250. mediaType,
  251. participantId);
  252. return track?.jitsiTrack?.getSourceName();
  253. }
  254. /**
  255. * Returns the track if any which corresponds to a specific instance
  256. * of JitsiLocalTrack or JitsiRemoteTrack.
  257. *
  258. * @param {ITrack[]} tracks - List of all tracks.
  259. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} jitsiTrack - JitsiTrack instance.
  260. * @returns {(Track|undefined)}
  261. */
  262. export function getTrackByJitsiTrack(tracks: ITrack[], jitsiTrack: any) {
  263. return tracks.find(t => t.jitsiTrack === jitsiTrack);
  264. }
  265. /**
  266. * Returns tracks of specified media type.
  267. *
  268. * @param {ITrack[]} tracks - List of all tracks.
  269. * @param {MediaType} mediaType - Media type.
  270. * @returns {Track[]}
  271. */
  272. export function getTracksByMediaType(tracks: ITrack[], mediaType: MediaType) {
  273. return tracks.filter(t => t.mediaType === mediaType);
  274. }
  275. /**
  276. * Checks if the first local track in the given tracks set is muted.
  277. *
  278. * @param {ITrack[]} tracks - List of all tracks.
  279. * @param {MediaType} mediaType - The media type of tracks to be checked.
  280. * @returns {boolean} True if local track is muted or false if the track is
  281. * unmuted or if there are no local tracks of the given media type in the given
  282. * set of tracks.
  283. */
  284. export function isLocalTrackMuted(tracks: ITrack[], mediaType: MediaType) {
  285. const track = getLocalTrack(tracks, mediaType);
  286. return !track || track.muted;
  287. }
  288. /**
  289. * Checks if the local video track is of type DESKtOP.
  290. *
  291. * @param {IReduxState} state - The redux state.
  292. * @returns {boolean}
  293. */
  294. export function isLocalVideoTrackDesktop(state: IReduxState) {
  295. const desktopTrack = getLocalDesktopTrack(getTrackState(state));
  296. return desktopTrack !== undefined && !desktopTrack.muted;
  297. }
  298. /**
  299. * Returns true if the remote track of the given media type and the given
  300. * participant is muted, false otherwise.
  301. *
  302. * @param {ITrack[]} tracks - List of all tracks.
  303. * @param {MediaType} mediaType - The media type of tracks to be checked.
  304. * @param {string} participantId - Participant ID.
  305. * @returns {boolean}
  306. */
  307. export function isRemoteTrackMuted(tracks: ITrack[], mediaType: MediaType, participantId: string) {
  308. const track = getTrackByMediaTypeAndParticipant(tracks, mediaType, participantId);
  309. return !track || track.muted;
  310. }
  311. /**
  312. * Returns whether or not the current environment needs a user interaction with
  313. * the page before any unmute can occur.
  314. *
  315. * @param {IReduxState} state - The redux state.
  316. * @returns {boolean}
  317. */
  318. export function isUserInteractionRequiredForUnmute(state: IReduxState) {
  319. return browser.isUserInteractionRequiredForUnmute()
  320. && window
  321. && window.self !== window.top
  322. && !state['features/base/user-interaction'].interacted;
  323. }
  324. /**
  325. * Sets the GUM pending state for the passed track operation (mute/unmute) and media type.
  326. * NOTE: We need this only for web.
  327. *
  328. * @param {IGUMPendingState} status - The new GUM pending status.
  329. * @param {MediaType} mediaType - The media type related to the operation (audio or video).
  330. * @param {boolean} muted - True if the operation is mute and false for unmute.
  331. * @param {Function} dispatch - The dispatch method.
  332. * @returns {void}
  333. */
  334. export function _setGUMPendingState(
  335. status: IGUMPendingState,
  336. mediaType: MediaType,
  337. muted: boolean,
  338. dispatch?: IStore['dispatch']) {
  339. if (!muted && dispatch && typeof APP !== 'undefined') {
  340. dispatch(gumPending([ mediaType ], status));
  341. }
  342. }
  343. /**
  344. * Mutes or unmutes a specific {@code JitsiLocalTrack}. If the muted state of the specified {@code track} is already in
  345. * accord with the specified {@code muted} value, then does nothing.
  346. *
  347. * @param {JitsiLocalTrack} track - The {@code JitsiLocalTrack} to mute or unmute.
  348. * @param {boolean} muted - If the specified {@code track} is to be muted, then {@code true}; otherwise, {@code false}.
  349. * @param {Object} state - The redux state.
  350. * @param {Function} dispatch - The dispatch method.
  351. * @returns {Promise}
  352. */
  353. export function setTrackMuted(track: any, muted: boolean, state: IReduxState | IMediaState,
  354. dispatch?: IStore['dispatch']) {
  355. muted = Boolean(muted); // eslint-disable-line no-param-reassign
  356. // Ignore the check for desktop track muted operation. When the screenshare is terminated by clicking on the
  357. // browser's 'Stop sharing' button, the local stream is stopped before the inactive stream handler is fired.
  358. // We still need to proceed here and remove the track from the peerconnection.
  359. if (track.isMuted() === muted
  360. && !(track.getVideoType() === VIDEO_TYPE.DESKTOP && getMultipleVideoSendingSupportFeatureFlag(state))) {
  361. return Promise.resolve();
  362. }
  363. const f = muted ? 'mute' : 'unmute';
  364. const mediaType = track.getType();
  365. _setGUMPendingState(IGUMPendingState.PENDING_UNMUTE, mediaType, muted, dispatch);
  366. return track[f]().then((result: any) => {
  367. _setGUMPendingState(IGUMPendingState.NONE, mediaType, muted, dispatch);
  368. return result;
  369. })
  370. .catch((error: Error) => {
  371. _setGUMPendingState(IGUMPendingState.NONE, mediaType, muted, dispatch);
  372. // Track might be already disposed so ignore such an error.
  373. if (error.name !== JitsiTrackErrors.TRACK_IS_DISPOSED) {
  374. logger.error(`set track ${f} failed`, error);
  375. return Promise.reject(error);
  376. }
  377. });
  378. }
  379. /**
  380. * Logs the current track state for a participant.
  381. *
  382. * @param {ITrack[]} tracksState - The tracks from redux.
  383. * @param {string} participantId - The ID of the participant.
  384. * @param {string} reason - The reason for the track change.
  385. * @returns {void}
  386. */
  387. export function logTracksForParticipant(tracksState: ITrack[], participantId: string, reason?: string) {
  388. if (!participantId) {
  389. return;
  390. }
  391. const tracks = getTrackByParticipantId(tracksState, participantId);
  392. const logStringPrefix = `Track state for participant ${participantId} changed`;
  393. const trackStateStrings = tracks.map(t => `{type: ${t.mediaType}, videoType: ${t.videoType}, muted: ${
  394. t.muted}, isReceivingData: ${t.isReceivingData}, jitsiTrack: ${t.jitsiTrack?.toString()}}`);
  395. const tracksLogMsg = trackStateStrings.length > 0 ? `\n${trackStateStrings.join('\n')}` : ' No tracks available!';
  396. logger.debug(`${logStringPrefix}${reason ? `(reason: ${reason})` : ''}:${tracksLogMsg}`);
  397. }
  398. /**
  399. * Gets the default camera facing mode.
  400. *
  401. * @param {Object} state - The redux state.
  402. * @returns {string} - The camera facing mode.
  403. */
  404. export function getCameraFacingMode(state: IReduxState) {
  405. return state['features/base/config'].cameraFacingMode ?? CAMERA_FACING_MODE.USER;
  406. }