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.

JitsiParticipant.ts 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. import { Strophe } from 'strophe.js';
  2. import JitsiConference from './JitsiConference';
  3. import * as JitsiConferenceEvents from './JitsiConferenceEvents';
  4. import JitsiTrack from './modules/RTC/JitsiTrack';
  5. import { MediaType } from './service/RTC/MediaType';
  6. export interface ISourceInfo {
  7. muted: boolean;
  8. videoType: string;
  9. }
  10. /**
  11. * Represents a participant in (i.e. a member of) a conference.
  12. */
  13. export default class JitsiParticipant {
  14. private _jid: string;
  15. private _id: string;
  16. private _conference: JitsiConference;
  17. private _displayName: string;
  18. private _supportsDTMF: boolean;
  19. private _tracks: JitsiTrack[];
  20. private _role: string;
  21. private _status?: string;
  22. private _hidden: boolean;
  23. private _statsID?: string;
  24. private _properties: Map<string, any>;
  25. private _identity?: object;
  26. private _isReplacing?: boolean;
  27. private _isReplaced?: boolean;
  28. private _isSilent?: boolean;
  29. private _features: Set<string>;
  30. private _sources: Map<MediaType, Map<string, ISourceInfo>>;
  31. private _botType?: string;
  32. private _connectionJid?: string;
  33. /* eslint-disable max-params */
  34. /**
  35. * Initializes a new JitsiParticipant instance.
  36. *
  37. * @constructor
  38. * @param jid the conference XMPP jid
  39. * @param conference
  40. * @param displayName
  41. * @param {Boolean} hidden - True if the new JitsiParticipant instance is to
  42. * represent a hidden participant; otherwise, false.
  43. * @param {string} statsID - optional participant statsID
  44. * @param {string} status - the initial status if any.
  45. * @param {object} identity - the xmpp identity
  46. * @param {boolean?} isReplacing - whether this is a participant replacing another into the meeting.
  47. * @param {boolean?} isReplaced - whether this is a participant to be kicked and replaced into the meeting.
  48. * @param {boolean?} isSilent - whether participant has joined without audio
  49. */
  50. constructor(
  51. jid: string,
  52. conference: JitsiConference,
  53. displayName: string,
  54. hidden: boolean,
  55. statsID?: string,
  56. status?: string,
  57. identity?: object,
  58. isReplacing?: boolean,
  59. isReplaced?: boolean,
  60. isSilent?: boolean
  61. ) {
  62. this._jid = jid;
  63. this._id = Strophe.getResourceFromJid(jid);
  64. this._conference = conference;
  65. this._displayName = displayName;
  66. this._supportsDTMF = false;
  67. this._tracks = [];
  68. this._role = 'none';
  69. this._status = status;
  70. this._hidden = hidden;
  71. this._statsID = statsID;
  72. this._properties = new Map();
  73. this._identity = identity;
  74. this._isReplacing = isReplacing;
  75. this._isReplaced = isReplaced;
  76. this._isSilent = isSilent;
  77. this._features = new Set();
  78. /**
  79. * Remote sources associated with the participant in the following format.
  80. * Map<mediaType, Map<sourceName, ISourceInfo>>
  81. *
  82. * mediaType - 'audio' or 'video'.
  83. * sourceName - name of the remote source.
  84. * ISourceInfo: {
  85. * muted: boolean;
  86. * videoType: string;
  87. * }
  88. */
  89. this._sources = new Map();
  90. }
  91. /**
  92. * Determines whether all JitsiTracks which are of a specific MediaType and which belong to this
  93. * JitsiParticipant are muted.
  94. *
  95. * @param {MediaType} mediaType - The MediaType of the JitsiTracks to be checked.
  96. * @private
  97. * @returns {Boolean} True if all JitsiTracks which are of the specified mediaType and which belong to this
  98. * JitsiParticipant are muted; otherwise, false.
  99. */
  100. _isMediaTypeMuted(mediaType: MediaType): boolean {
  101. return this.getTracks().reduce(
  102. (muted, track) =>
  103. muted && (track.getType() !== mediaType || (track as any).isMuted()),
  104. true);
  105. }
  106. /**
  107. * Sets source info.
  108. * @param {MediaType} mediaType The media type, 'audio' or 'video'.
  109. * @param {boolean} muted The new muted state.
  110. * @param {string} sourceName The name of the source.
  111. * @param {string} videoType The video type of the source.
  112. * @returns {void}
  113. */
  114. _setSources(mediaType: MediaType, muted: boolean, sourceName: string, videoType: string): void {
  115. let sourceByMediaType = this._sources.get(mediaType);
  116. const sourceInfo: ISourceInfo = {
  117. muted,
  118. videoType
  119. };
  120. if (sourceByMediaType?.size) {
  121. sourceByMediaType.set(sourceName, sourceInfo);
  122. return;
  123. }
  124. sourceByMediaType = new Map();
  125. sourceByMediaType.set(sourceName, sourceInfo);
  126. this._sources.set(mediaType, sourceByMediaType);
  127. }
  128. /**
  129. * Returns the bot type for the participant.
  130. *
  131. * @returns {string|undefined} - The bot type of the participant.
  132. */
  133. getBotType(): string | undefined {
  134. return this._botType;
  135. }
  136. /**
  137. * @returns {JitsiConference} The conference that this participant belongs
  138. * to.
  139. */
  140. getConference(): JitsiConference {
  141. return this._conference;
  142. }
  143. /**
  144. * Returns the connection jid for the participant.
  145. *
  146. * @returns {string|undefined} - The connection jid of the participant.
  147. */
  148. getConnectionJid(): string | undefined {
  149. return this._connectionJid;
  150. }
  151. /**
  152. * @returns {String} The human-readable display name of this participant.
  153. */
  154. getDisplayName(): string {
  155. return this._displayName;
  156. }
  157. /**
  158. * Returns a set with the features for the participant.
  159. * @returns {Promise<Set<String>>}
  160. */
  161. getFeatures(): Promise<Set<string>> {
  162. return Promise.resolve(this._features);
  163. }
  164. /**
  165. * @returns {String} The ID of this participant.
  166. */
  167. getId(): string {
  168. return this._id;
  169. }
  170. /**
  171. * Returns the XMPP identity. This is defined by your application in the
  172. * JWT `context` claims section.
  173. *
  174. * @returns {object|undefined} - XMPP user identity.
  175. */
  176. getIdentity(): object | undefined {
  177. return this._identity;
  178. }
  179. /**
  180. * @returns {String} The JID of this participant.
  181. */
  182. getJid(): string {
  183. return this._jid;
  184. }
  185. /**
  186. * Gets the value of a property of this participant.
  187. */
  188. getProperty(name: string): any {
  189. return this._properties.get(name);
  190. }
  191. /**
  192. * @returns {String} The role of this participant.
  193. */
  194. getRole(): string {
  195. return this._role;
  196. }
  197. /**
  198. * Returns the sources associated with this participant.
  199. * @returns Map<string, Map<string, Object>>
  200. */
  201. getSources(): Map<MediaType, Map<string, ISourceInfo>> {
  202. return this._sources;
  203. }
  204. /**
  205. * @returns {String} The stats ID of this participant.
  206. */
  207. getStatsID(): string {
  208. return this._statsID;
  209. }
  210. /**
  211. * @returns {String} The status of the participant.
  212. */
  213. getStatus(): string {
  214. return this._status;
  215. }
  216. /**
  217. * @returns {Array.<JitsiTrack>} The list of media tracks for this
  218. * participant.
  219. */
  220. getTracks(): JitsiTrack[] {
  221. return this._tracks.slice();
  222. }
  223. /**
  224. * @param {MediaType} mediaType
  225. * @returns {Array.<JitsiTrack>} an array of media tracks for this
  226. * participant, for given media type.
  227. */
  228. getTracksByMediaType(mediaType: MediaType): JitsiTrack[] {
  229. return this.getTracks().filter(track => track.getType() === mediaType);
  230. }
  231. /**
  232. * Checks current set features.
  233. * @param {String} feature - the feature to check.
  234. * @return {boolean} <tt>true</tt> if this <tt>participant</tt> contains the
  235. * <tt>feature</tt>.
  236. */
  237. hasFeature(feature: string): boolean {
  238. return this._features.has(feature);
  239. }
  240. /**
  241. * @returns {Boolean} Whether this participant has muted their audio.
  242. */
  243. isAudioMuted(): boolean {
  244. return this._isMediaTypeMuted(MediaType.AUDIO);
  245. }
  246. /**
  247. * @returns {Boolean} Whether this participant is a hidden participant. Some
  248. * special system participants may want to join hidden (like for example the
  249. * recorder).
  250. */
  251. isHidden(): boolean {
  252. return this._hidden;
  253. }
  254. /**
  255. * @returns {Boolean} Whether this participant is a hidden participant. Some
  256. * special system participants may want to join hidden (like for example the
  257. * recorder).
  258. */
  259. isHiddenFromRecorder(): boolean {
  260. return (this._identity as any)?.user?.['hidden-from-recorder'] === 'true';
  261. }
  262. /**
  263. * @returns {Boolean} Whether this participant is a moderator or not.
  264. */
  265. isModerator(): boolean {
  266. return this._role === 'moderator';
  267. }
  268. /**
  269. * @returns {Boolean} Wheter this participants will be replaced by another
  270. * participant in the meeting.
  271. */
  272. isReplaced(): boolean {
  273. return this._isReplaced;
  274. }
  275. /**
  276. * @returns {Boolean} Whether this participant replaces another participant
  277. * from the meeting.
  278. */
  279. isReplacing(): boolean {
  280. return this._isReplacing;
  281. }
  282. /**
  283. * @returns {Boolean} Whether this participant has joined without audio.
  284. */
  285. isSilent(): boolean {
  286. return this._isSilent;
  287. }
  288. /**
  289. * @returns {Boolean} Whether this participant has muted their video.
  290. */
  291. isVideoMuted(): boolean {
  292. return this._isMediaTypeMuted(MediaType.VIDEO);
  293. }
  294. /**
  295. * Sets the bot type for the participant.
  296. * @param {String} newBotType - The new bot type to set.
  297. */
  298. setBotType(newBotType: string): void {
  299. this._botType = newBotType;
  300. }
  301. /**
  302. * Sets the connection jid for the participant.
  303. * @param {String} newJid - The connection jid to set.
  304. */
  305. setConnectionJid(newJid: string): void {
  306. this._connectionJid = newJid;
  307. }
  308. /**
  309. * Set new features.
  310. * @param {Set<String>|undefined} newFeatures - Sets new features.
  311. */
  312. setFeatures(newFeatures?: Set<string>): void {
  313. this._features = newFeatures || new Set();
  314. }
  315. /**
  316. * Sets whether participant is being replaced by another based on jwt.
  317. * @param {boolean} newIsReplaced - whether is being replaced.
  318. */
  319. setIsReplaced(newIsReplaced: boolean): void {
  320. this._isReplaced = newIsReplaced;
  321. }
  322. /**
  323. * Sets whether participant is replacing another based on jwt.
  324. * @param {boolean} newIsReplacing - whether is replacing.
  325. */
  326. setIsReplacing(newIsReplacing: boolean): void {
  327. this._isReplacing = newIsReplacing;
  328. }
  329. /**
  330. * Sets whether participant has joined without audio.
  331. * @param {boolean} newIsSilent - whether is silent.
  332. */
  333. setIsSilent(newIsSilent: boolean): void {
  334. this._isSilent = newIsSilent;
  335. }
  336. /**
  337. * Sets the value of a property of this participant, and fires an event if
  338. * the value has changed.
  339. * @name the name of the property.
  340. * @value the value to set.
  341. */
  342. setProperty(name: string, value: any): void {
  343. const oldValue = this._properties.get(name);
  344. if (value !== oldValue) {
  345. this._properties.set(name, value);
  346. this._conference.eventEmitter.emit(
  347. JitsiConferenceEvents.PARTICIPANT_PROPERTY_CHANGED,
  348. this,
  349. name,
  350. oldValue,
  351. value);
  352. }
  353. }
  354. /**
  355. * Sets a new participant role.
  356. * @param {String} newRole - the new role.
  357. */
  358. setRole(newRole: string): void {
  359. this._role = newRole;
  360. }
  361. /**
  362. *
  363. */
  364. supportsDTMF(): boolean {
  365. return this._supportsDTMF;
  366. }
  367. }