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.js 10.0KB

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