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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 { 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 screenshare track of given virtualScreenshareParticipantId.
  203. *
  204. * @param {ITrack[]} tracks - List of all tracks.
  205. * @param {string} virtualScreenshareParticipantId - Virtual Screenshare Participant ID.
  206. * @returns {(Track|undefined)}
  207. */
  208. export function getVirtualScreenshareParticipantTrack(tracks: ITrack[], virtualScreenshareParticipantId: string) {
  209. const ownderId = getVirtualScreenshareParticipantOwnerId(virtualScreenshareParticipantId);
  210. return getScreenShareTrack(tracks, ownderId);
  211. }
  212. /**
  213. * Returns screenshare track of given owner ID.
  214. *
  215. * @param {Track[]} tracks - List of all tracks.
  216. * @param {string} ownerId - Screenshare track owner ID.
  217. * @returns {(Track|undefined)}
  218. */
  219. export function getScreenShareTrack(tracks: ITrack[], ownerId: string) {
  220. return tracks.find(
  221. t => Boolean(t.jitsiTrack)
  222. && t.participantId === ownerId
  223. && (t.mediaType === MEDIA_TYPE.SCREENSHARE || t.videoType === VIDEO_TYPE.DESKTOP)
  224. );
  225. }
  226. /**
  227. * Returns track source name of specified media type for specified participant id.
  228. *
  229. * @param {ITrack[]} tracks - List of all tracks.
  230. * @param {MediaType} mediaType - Media type.
  231. * @param {string} participantId - Participant ID.
  232. * @returns {(string|undefined)}
  233. */
  234. export function getTrackSourceNameByMediaTypeAndParticipant(
  235. tracks: ITrack[],
  236. mediaType: MediaType,
  237. participantId: string) {
  238. const track = getTrackByMediaTypeAndParticipant(
  239. tracks,
  240. mediaType,
  241. participantId);
  242. return track?.jitsiTrack?.getSourceName();
  243. }
  244. /**
  245. * Returns the track if any which corresponds to a specific instance
  246. * of JitsiLocalTrack or JitsiRemoteTrack.
  247. *
  248. * @param {ITrack[]} tracks - List of all tracks.
  249. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} jitsiTrack - JitsiTrack instance.
  250. * @returns {(Track|undefined)}
  251. */
  252. export function getTrackByJitsiTrack(tracks: ITrack[], jitsiTrack: any) {
  253. return tracks.find(t => t.jitsiTrack === jitsiTrack);
  254. }
  255. /**
  256. * Returns tracks of specified media type.
  257. *
  258. * @param {ITrack[]} tracks - List of all tracks.
  259. * @param {MediaType} mediaType - Media type.
  260. * @returns {Track[]}
  261. */
  262. export function getTracksByMediaType(tracks: ITrack[], mediaType: MediaType) {
  263. return tracks.filter(t => t.mediaType === mediaType);
  264. }
  265. /**
  266. * Checks if the first local track in the given tracks set is muted.
  267. *
  268. * @param {ITrack[]} tracks - List of all tracks.
  269. * @param {MediaType} mediaType - The media type of tracks to be checked.
  270. * @returns {boolean} True if local track is muted or false if the track is
  271. * unmuted or if there are no local tracks of the given media type in the given
  272. * set of tracks.
  273. */
  274. export function isLocalTrackMuted(tracks: ITrack[], mediaType: MediaType) {
  275. const track = getLocalTrack(tracks, mediaType);
  276. return !track || track.muted;
  277. }
  278. /**
  279. * Checks if the local video track is of type DESKtOP.
  280. *
  281. * @param {IReduxState} state - The redux state.
  282. * @returns {boolean}
  283. */
  284. export function isLocalVideoTrackDesktop(state: IReduxState) {
  285. const desktopTrack = getLocalDesktopTrack(getTrackState(state));
  286. return desktopTrack !== undefined && !desktopTrack.muted;
  287. }
  288. /**
  289. * Returns true if the remote track of the given media type and the given
  290. * participant is muted, false otherwise.
  291. *
  292. * @param {ITrack[]} tracks - List of all tracks.
  293. * @param {MediaType} mediaType - The media type of tracks to be checked.
  294. * @param {string} participantId - Participant ID.
  295. * @returns {boolean}
  296. */
  297. export function isRemoteTrackMuted(tracks: ITrack[], mediaType: MediaType, participantId: string) {
  298. const track = getTrackByMediaTypeAndParticipant(
  299. tracks, mediaType, participantId);
  300. return !track || track.muted;
  301. }
  302. /**
  303. * Returns whether or not the current environment needs a user interaction with
  304. * the page before any unmute can occur.
  305. *
  306. * @param {IReduxState} state - The redux state.
  307. * @returns {boolean}
  308. */
  309. export function isUserInteractionRequiredForUnmute(state: IReduxState) {
  310. return browser.isUserInteractionRequiredForUnmute()
  311. && window
  312. && window.self !== window.top
  313. && !state['features/base/user-interaction'].interacted;
  314. }
  315. /**
  316. * Sets the GUM pending state for the passed track operation (mute/unmute) and media type.
  317. * NOTE: We need this only for web.
  318. *
  319. * @param {IGUMPendingState} status - The new GUM pending status.
  320. * @param {MediaType} mediaType - The media type related to the operation (audio or video).
  321. * @param {boolean} muted - True if the operation is mute and false for unmute.
  322. * @param {Function} dispatch - The dispatch method.
  323. * @returns {void}
  324. */
  325. export function _setGUMPendingState(
  326. status: IGUMPendingState,
  327. mediaType: MediaType,
  328. muted: boolean,
  329. dispatch?: IStore['dispatch']) {
  330. if (!muted && dispatch && typeof APP !== 'undefined') {
  331. dispatch(gumPending([ mediaType ], status));
  332. }
  333. }
  334. /**
  335. * Mutes or unmutes a specific {@code JitsiLocalTrack}. If the muted state of the specified {@code track} is already in
  336. * accord with the specified {@code muted} value, then does nothing.
  337. *
  338. * @param {JitsiLocalTrack} track - The {@code JitsiLocalTrack} to mute or unmute.
  339. * @param {boolean} muted - If the specified {@code track} is to be muted, then {@code true}; otherwise, {@code false}.
  340. * @param {Object} state - The redux state.
  341. * @param {Function} dispatch - The dispatch method.
  342. * @returns {Promise}
  343. */
  344. export function setTrackMuted(track: any, muted: boolean, state: IReduxState | IMediaState,
  345. dispatch?: IStore['dispatch']) {
  346. muted = Boolean(muted); // eslint-disable-line no-param-reassign
  347. // Ignore the check for desktop track muted operation. When the screenshare is terminated by clicking on the
  348. // browser's 'Stop sharing' button, the local stream is stopped before the inactive stream handler is fired.
  349. // We still need to proceed here and remove the track from the peerconnection.
  350. if (track.isMuted() === muted
  351. && !(track.getVideoType() === VIDEO_TYPE.DESKTOP && getMultipleVideoSendingSupportFeatureFlag(state))) {
  352. return Promise.resolve();
  353. }
  354. const f = muted ? 'mute' : 'unmute';
  355. const mediaType = track.getType();
  356. _setGUMPendingState(IGUMPendingState.PENDING_UNMUTE, mediaType, muted, dispatch);
  357. return track[f]().then((result: any) => {
  358. _setGUMPendingState(IGUMPendingState.NONE, mediaType, muted, dispatch);
  359. return result;
  360. })
  361. .catch((error: Error) => {
  362. _setGUMPendingState(IGUMPendingState.NONE, mediaType, muted, dispatch);
  363. // Track might be already disposed so ignore such an error.
  364. if (error.name !== JitsiTrackErrors.TRACK_IS_DISPOSED) {
  365. logger.error(`set track ${f} failed`, error);
  366. return Promise.reject(error);
  367. }
  368. });
  369. }