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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. import { IReduxState } from '../../app/types';
  2. import {
  3. getMultipleVideoSendingSupportFeatureFlag
  4. } from '../config/functions.any';
  5. import { JitsiTrackErrors, browser } from '../lib-jitsi-meet';
  6. import { MEDIA_TYPE, MediaType, VIDEO_TYPE } from '../media/constants';
  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, mediaType: MediaType, state: IReduxState) {
  30. if (!participant) {
  31. return false;
  32. }
  33. const tracks = getTrackState(state);
  34. if (participant?.local) {
  35. return isLocalTrackMuted(tracks, mediaType);
  36. } else if (!participant?.fakeParticipant) {
  37. return isRemoteTrackMuted(tracks, mediaType, participant.id);
  38. }
  39. return true;
  40. }
  41. /**
  42. * Checks if the participant is audio muted.
  43. *
  44. * @param {IParticipant} participant - Participant reference.
  45. * @param {IReduxState} state - Global state.
  46. * @returns {boolean} - Is audio muted for the participant.
  47. */
  48. export function isParticipantAudioMuted(participant: IParticipant, state: IReduxState) {
  49. return isParticipantMediaMuted(participant, MEDIA_TYPE.AUDIO, state);
  50. }
  51. /**
  52. * Checks if the participant is video muted.
  53. *
  54. * @param {IParticipant} participant - Participant reference.
  55. * @param {IReduxState} state - Global state.
  56. * @returns {boolean} - Is video muted for the participant.
  57. */
  58. export function isParticipantVideoMuted(participant: IParticipant, state: IReduxState) {
  59. return isParticipantMediaMuted(participant, MEDIA_TYPE.VIDEO, state);
  60. }
  61. /**
  62. * Returns local audio track.
  63. *
  64. * @param {ITrack[]} tracks - List of all tracks.
  65. * @returns {(Track|undefined)}
  66. */
  67. export function getLocalAudioTrack(tracks: ITrack[]) {
  68. return getLocalTrack(tracks, MEDIA_TYPE.AUDIO);
  69. }
  70. /**
  71. * Returns the local desktop track.
  72. *
  73. * @param {Track[]} tracks - List of all tracks.
  74. * @param {boolean} [includePending] - Indicates whether a local track is to be returned if it is still pending.
  75. * A local track is pending if {@code getUserMedia} is still executing to create it and, consequently, its
  76. * {@code jitsiTrack} property is {@code undefined}. By default a pending local track is not returned.
  77. * @returns {(Track|undefined)}
  78. */
  79. export function getLocalDesktopTrack(tracks: ITrack[], includePending = false) {
  80. return (
  81. getLocalTracks(tracks, includePending)
  82. .find(t => t.mediaType === MEDIA_TYPE.SCREENSHARE || t.videoType === VIDEO_TYPE.DESKTOP));
  83. }
  84. /**
  85. * Returns the stored local desktop jitsiLocalTrack.
  86. *
  87. * @param {IReduxState} state - The redux state.
  88. * @returns {JitsiLocalTrack|undefined}
  89. */
  90. export function getLocalJitsiDesktopTrack(state: IReduxState) {
  91. const track = getLocalDesktopTrack(getTrackState(state));
  92. return track?.jitsiTrack;
  93. }
  94. /**
  95. * Returns local track by media type.
  96. *
  97. * @param {ITrack[]} tracks - List of all tracks.
  98. * @param {MediaType} mediaType - Media type.
  99. * @param {boolean} [includePending] - Indicates whether a local track is to be
  100. * returned if it is still pending. A local track is pending if
  101. * {@code getUserMedia} is still executing to create it and, consequently, its
  102. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  103. * track is not returned.
  104. * @returns {(Track|undefined)}
  105. */
  106. export function getLocalTrack(tracks: ITrack[], mediaType: MediaType, includePending = false) {
  107. return (
  108. getLocalTracks(tracks, includePending)
  109. .find(t => t.mediaType === mediaType));
  110. }
  111. /**
  112. * Returns an array containing the local tracks with or without a (valid)
  113. * {@code JitsiTrack}.
  114. *
  115. * @param {ITrack[]} tracks - An array containing all local tracks.
  116. * @param {boolean} [includePending] - Indicates whether a local track is to be
  117. * returned if it is still pending. A local track is pending if
  118. * {@code getUserMedia} is still executing to create it and, consequently, its
  119. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  120. * track is not returned.
  121. * @returns {Track[]}
  122. */
  123. export function getLocalTracks(tracks: ITrack[], includePending = false) {
  124. // XXX A local track is considered ready only once it has its `jitsiTrack`
  125. // property set by the `TRACK_ADDED` action. Until then there is a stub
  126. // added just before the `getUserMedia` call with a cancellable
  127. // `gumInProgress` property which then can be used to destroy the track that
  128. // has not yet been added to the redux store. Once GUM is cancelled, it will
  129. // never make it to the store nor there will be any
  130. // `TRACK_ADDED`/`TRACK_REMOVED` actions dispatched for it.
  131. return tracks.filter(t => t.local && (t.jitsiTrack || includePending));
  132. }
  133. /**
  134. * Returns local video track.
  135. *
  136. * @param {ITrack[]} tracks - List of all tracks.
  137. * @returns {(Track|undefined)}
  138. */
  139. export function getLocalVideoTrack(tracks: ITrack[]) {
  140. return getLocalTrack(tracks, MEDIA_TYPE.VIDEO);
  141. }
  142. /**
  143. * Returns the stored local video track.
  144. *
  145. * @param {IReduxState} state - The redux state.
  146. * @returns {Object}
  147. */
  148. export function getLocalJitsiVideoTrack(state: IReduxState) {
  149. const track = getLocalVideoTrack(getTrackState(state));
  150. return track?.jitsiTrack;
  151. }
  152. /**
  153. * Returns the stored local audio track.
  154. *
  155. * @param {IReduxState} state - The redux state.
  156. * @returns {Object}
  157. */
  158. export function getLocalJitsiAudioTrack(state: IReduxState) {
  159. const track = getLocalAudioTrack(getTrackState(state));
  160. return track?.jitsiTrack;
  161. }
  162. /**
  163. * Returns track of specified media type for specified participant.
  164. *
  165. * @param {IReduxState} state - The redux state.
  166. * @param {IParticipant} participant - Participant Object.
  167. * @returns {(Track|undefined)}
  168. */
  169. export function getVideoTrackByParticipant(
  170. state: IReduxState,
  171. participant?: IParticipant) {
  172. if (!participant) {
  173. return;
  174. }
  175. const tracks = state['features/base/tracks'];
  176. if (isScreenShareParticipant(participant)) {
  177. return getVirtualScreenshareParticipantTrack(tracks, participant.id);
  178. }
  179. return getTrackByMediaTypeAndParticipant(tracks, MEDIA_TYPE.VIDEO, participant.id);
  180. }
  181. /**
  182. * Returns track of specified media type for specified participant id.
  183. *
  184. * @param {ITrack[]} tracks - List of all tracks.
  185. * @param {MediaType} mediaType - Media type.
  186. * @param {string} participantId - Participant ID.
  187. * @returns {(Track|undefined)}
  188. */
  189. export function getTrackByMediaTypeAndParticipant(
  190. tracks: ITrack[],
  191. mediaType: MediaType,
  192. participantId?: string) {
  193. return tracks.find(
  194. t => Boolean(t.jitsiTrack) && t.participantId === participantId && t.mediaType === mediaType
  195. );
  196. }
  197. /**
  198. * Returns screenshare track of given virtualScreenshareParticipantId.
  199. *
  200. * @param {ITrack[]} tracks - List of all tracks.
  201. * @param {string} virtualScreenshareParticipantId - Virtual Screenshare Participant ID.
  202. * @returns {(Track|undefined)}
  203. */
  204. export function getVirtualScreenshareParticipantTrack(tracks: ITrack[], virtualScreenshareParticipantId: string) {
  205. const ownderId = getVirtualScreenshareParticipantOwnerId(virtualScreenshareParticipantId);
  206. return getScreenShareTrack(tracks, ownderId);
  207. }
  208. /**
  209. * Returns screenshare track of given owner ID.
  210. *
  211. * @param {Track[]} tracks - List of all tracks.
  212. * @param {string} ownerId - Screenshare track owner ID.
  213. * @returns {(Track|undefined)}
  214. */
  215. export function getScreenShareTrack(tracks: ITrack[], ownerId: string) {
  216. return tracks.find(
  217. t => Boolean(t.jitsiTrack)
  218. && t.participantId === ownerId
  219. && (t.mediaType === MEDIA_TYPE.SCREENSHARE || t.videoType === VIDEO_TYPE.DESKTOP)
  220. );
  221. }
  222. /**
  223. * Returns track source name of specified media type for specified participant id.
  224. *
  225. * @param {ITrack[]} tracks - List of all tracks.
  226. * @param {MediaType} mediaType - Media type.
  227. * @param {string} participantId - Participant ID.
  228. * @returns {(string|undefined)}
  229. */
  230. export function getTrackSourceNameByMediaTypeAndParticipant(
  231. tracks: ITrack[],
  232. mediaType: MediaType,
  233. participantId: string) {
  234. const track = getTrackByMediaTypeAndParticipant(
  235. tracks,
  236. mediaType,
  237. participantId);
  238. return track?.jitsiTrack?.getSourceName();
  239. }
  240. /**
  241. * Returns the track if any which corresponds to a specific instance
  242. * of JitsiLocalTrack or JitsiRemoteTrack.
  243. *
  244. * @param {ITrack[]} tracks - List of all tracks.
  245. * @param {(JitsiLocalTrack|JitsiRemoteTrack)} jitsiTrack - JitsiTrack instance.
  246. * @returns {(Track|undefined)}
  247. */
  248. export function getTrackByJitsiTrack(tracks: ITrack[], jitsiTrack: any) {
  249. return tracks.find(t => t.jitsiTrack === jitsiTrack);
  250. }
  251. /**
  252. * Returns tracks of specified media type.
  253. *
  254. * @param {ITrack[]} tracks - List of all tracks.
  255. * @param {MediaType} mediaType - Media type.
  256. * @returns {Track[]}
  257. */
  258. export function getTracksByMediaType(tracks: ITrack[], mediaType: MediaType) {
  259. return tracks.filter(t => t.mediaType === mediaType);
  260. }
  261. /**
  262. * Checks if the first local track in the given tracks set is muted.
  263. *
  264. * @param {ITrack[]} tracks - List of all tracks.
  265. * @param {MediaType} mediaType - The media type of tracks to be checked.
  266. * @returns {boolean} True if local track is muted or false if the track is
  267. * unmuted or if there are no local tracks of the given media type in the given
  268. * set of tracks.
  269. */
  270. export function isLocalTrackMuted(tracks: ITrack[], mediaType: MediaType) {
  271. const track = getLocalTrack(tracks, mediaType);
  272. return !track || track.muted;
  273. }
  274. /**
  275. * Checks if the local video track is of type DESKtOP.
  276. *
  277. * @param {IReduxState} state - The redux state.
  278. * @returns {boolean}
  279. */
  280. export function isLocalVideoTrackDesktop(state: IReduxState) {
  281. const videoTrack = getLocalVideoTrack(getTrackState(state));
  282. return videoTrack && videoTrack.videoType === VIDEO_TYPE.DESKTOP;
  283. }
  284. /**
  285. * Returns true if the remote track of the given media type and the given
  286. * participant is muted, false otherwise.
  287. *
  288. * @param {ITrack[]} tracks - List of all tracks.
  289. * @param {MediaType} mediaType - The media type of tracks to be checked.
  290. * @param {string} participantId - Participant ID.
  291. * @returns {boolean}
  292. */
  293. export function isRemoteTrackMuted(tracks: ITrack[], mediaType: MediaType, participantId: string) {
  294. const track = getTrackByMediaTypeAndParticipant(
  295. tracks, mediaType, participantId);
  296. return !track || track.muted;
  297. }
  298. /**
  299. * Returns whether or not the current environment needs a user interaction with
  300. * the page before any unmute can occur.
  301. *
  302. * @param {IReduxState} state - The redux state.
  303. * @returns {boolean}
  304. */
  305. export function isUserInteractionRequiredForUnmute(state: IReduxState) {
  306. return browser.isUserInteractionRequiredForUnmute()
  307. && window
  308. && window.self !== window.top
  309. && !state['features/base/user-interaction'].interacted;
  310. }
  311. /**
  312. * Mutes or unmutes a specific {@code JitsiLocalTrack}. If the muted state of the specified {@code track} is already in
  313. * accord with the specified {@code muted} value, then does nothing.
  314. *
  315. * @param {JitsiLocalTrack} track - The {@code JitsiLocalTrack} to mute or unmute.
  316. * @param {boolean} muted - If the specified {@code track} is to be muted, then {@code true}; otherwise, {@code false}.
  317. * @param {Object} state - The redux state.
  318. * @returns {Promise}
  319. */
  320. export function setTrackMuted(track: any, muted: boolean, state: IReduxState) {
  321. muted = Boolean(muted); // eslint-disable-line no-param-reassign
  322. // Ignore the check for desktop track muted operation. When the screenshare is terminated by clicking on the
  323. // browser's 'Stop sharing' button, the local stream is stopped before the inactive stream handler is fired.
  324. // We still need to proceed here and remove the track from the peerconnection.
  325. if (track.isMuted() === muted
  326. && !(track.getVideoType() === VIDEO_TYPE.DESKTOP && getMultipleVideoSendingSupportFeatureFlag(state))) {
  327. return Promise.resolve();
  328. }
  329. const f = muted ? 'mute' : 'unmute';
  330. return track[f]().catch((error: Error) => {
  331. // Track might be already disposed so ignore such an error.
  332. if (error.name !== JitsiTrackErrors.TRACK_IS_DISPOSED) {
  333. logger.error(`set track ${f} failed`, error);
  334. return Promise.reject(error);
  335. }
  336. });
  337. }