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

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