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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import { getLogger } from 'jitsi-meet-logger';
  2. import { Strophe } from 'strophe.js';
  3. import * as JitsiConferenceEvents from './JitsiConferenceEvents';
  4. import { ParticipantConnectionStatus }
  5. from './modules/connectivity/ParticipantConnectionStatus';
  6. import { ERROR_FEATURE_VERSION_MISMATCH } from './modules/xmpp/Caps';
  7. import * as MediaType from './service/RTC/MediaType';
  8. const logger = getLogger(__filename);
  9. /**
  10. * Represents a participant in (i.e. a member of) a conference.
  11. */
  12. export default class JitsiParticipant {
  13. /* eslint-disable max-params */
  14. /**
  15. * Initializes a new JitsiParticipant instance.
  16. *
  17. * @constructor
  18. * @param jid the conference XMPP jid
  19. * @param conference
  20. * @param displayName
  21. * @param {Boolean} hidden - True if the new JitsiParticipant instance is to
  22. * represent a hidden participant; otherwise, false.
  23. * @param {string} statsID - optional participant statsID
  24. * @param {string} status - the initial status if any.
  25. * @param {object} identity - the xmpp identity
  26. */
  27. constructor(jid, conference, displayName, hidden, statsID, status, identity) {
  28. this._jid = jid;
  29. this._id = Strophe.getResourceFromJid(jid);
  30. this._conference = conference;
  31. this._displayName = displayName;
  32. this._supportsDTMF = false;
  33. this._tracks = [];
  34. this._role = 'none';
  35. this._status = status;
  36. this._hidden = hidden;
  37. this._statsID = statsID;
  38. this._connectionStatus = ParticipantConnectionStatus.ACTIVE;
  39. this._properties = {};
  40. this._identity = identity;
  41. this._features = new Set();
  42. }
  43. /* eslint-enable max-params */
  44. /**
  45. * @returns {JitsiConference} The conference that this participant belongs
  46. * to.
  47. */
  48. getConference() {
  49. return this._conference;
  50. }
  51. /**
  52. * Gets the value of a property of this participant.
  53. */
  54. getProperty(name) {
  55. return this._properties[name];
  56. }
  57. /**
  58. * Checks whether this <tt>JitsiParticipant</tt> has any video tracks which
  59. * are muted according to their underlying WebRTC <tt>MediaStreamTrack</tt>
  60. * muted status.
  61. * @return {boolean} <tt>true</tt> if this <tt>participant</tt> contains any
  62. * video <tt>JitsiTrack</tt>s which are muted as defined in
  63. * {@link JitsiTrack.isWebRTCTrackMuted}.
  64. */
  65. hasAnyVideoTrackWebRTCMuted() {
  66. return (
  67. this.getTracks().some(
  68. jitsiTrack =>
  69. jitsiTrack.getType() === MediaType.VIDEO
  70. && jitsiTrack.isWebRTCTrackMuted()));
  71. }
  72. /**
  73. * Updates participant's connection status.
  74. * @param {string} state the current participant connection state.
  75. * {@link ParticipantConnectionStatus}.
  76. * @private
  77. */
  78. _setConnectionStatus(status) {
  79. this._connectionStatus = status;
  80. }
  81. /**
  82. * Return participant's connectivity status.
  83. *
  84. * @returns {string} the connection status
  85. * <tt>ParticipantConnectionStatus</tt> of the user.
  86. * {@link ParticipantConnectionStatus}.
  87. */
  88. getConnectionStatus() {
  89. return this._connectionStatus;
  90. }
  91. /**
  92. * Sets the value of a property of this participant, and fires an event if
  93. * the value has changed.
  94. * @name the name of the property.
  95. * @value the value to set.
  96. */
  97. setProperty(name, value) {
  98. const oldValue = this._properties[name];
  99. if (value !== oldValue) {
  100. this._properties[name] = value;
  101. this._conference.eventEmitter.emit(
  102. JitsiConferenceEvents.PARTICIPANT_PROPERTY_CHANGED,
  103. this,
  104. name,
  105. oldValue,
  106. value);
  107. }
  108. }
  109. /**
  110. * @returns {Array.<JitsiTrack>} The list of media tracks for this
  111. * participant.
  112. */
  113. getTracks() {
  114. return this._tracks.slice();
  115. }
  116. /**
  117. * @param {MediaType} mediaType
  118. * @returns {Array.<JitsiTrack>} an array of media tracks for this
  119. * participant, for given media type.
  120. */
  121. getTracksByMediaType(mediaType) {
  122. return this.getTracks().filter(track => track.getType() === mediaType);
  123. }
  124. /**
  125. * @returns {String} The ID of this participant.
  126. */
  127. getId() {
  128. return this._id;
  129. }
  130. /**
  131. * @returns {String} The JID of this participant.
  132. */
  133. getJid() {
  134. return this._jid;
  135. }
  136. /**
  137. * @returns {String} The human-readable display name of this participant.
  138. */
  139. getDisplayName() {
  140. return this._displayName;
  141. }
  142. /**
  143. * @returns {String} The stats ID of this participant.
  144. */
  145. getStatsID() {
  146. return this._statsID;
  147. }
  148. /**
  149. * @returns {String} The status of the participant.
  150. */
  151. getStatus() {
  152. return this._status;
  153. }
  154. /**
  155. * @returns {Boolean} Whether this participant is a moderator or not.
  156. */
  157. isModerator() {
  158. return this._role === 'moderator';
  159. }
  160. /**
  161. * @returns {Boolean} Whether this participant is a hidden participant. Some
  162. * special system participants may want to join hidden (like for example the
  163. * recorder).
  164. */
  165. isHidden() {
  166. return this._hidden;
  167. }
  168. /**
  169. * @returns {Boolean} Whether this participant has muted their audio.
  170. */
  171. isAudioMuted() {
  172. return this._isMediaTypeMuted(MediaType.AUDIO);
  173. }
  174. /**
  175. * Determines whether all JitsiTracks which are of a specific MediaType and
  176. * which belong to this JitsiParticipant are muted.
  177. *
  178. * @param {MediaType} mediaType - The MediaType of the JitsiTracks to be
  179. * checked.
  180. * @private
  181. * @returns {Boolean} True if all JitsiTracks which are of the specified
  182. * mediaType and which belong to this JitsiParticipant are muted; otherwise,
  183. * false.
  184. */
  185. _isMediaTypeMuted(mediaType) {
  186. return this.getTracks().reduce(
  187. (muted, track) =>
  188. muted && (track.getType() !== mediaType || track.isMuted()),
  189. true);
  190. }
  191. /**
  192. * @returns {Boolean} Whether this participant has muted their video.
  193. */
  194. isVideoMuted() {
  195. return this._isMediaTypeMuted(MediaType.VIDEO);
  196. }
  197. /**
  198. * @returns {String} The role of this participant.
  199. */
  200. getRole() {
  201. return this._role;
  202. }
  203. /**
  204. *
  205. */
  206. supportsDTMF() {
  207. return this._supportsDTMF;
  208. }
  209. /**
  210. * Returns a set with the features for the participant.
  211. * @returns {Promise<Set<String>, Error>}
  212. */
  213. getFeatures() {
  214. return Promise.resolve(this._features);
  215. }
  216. /**
  217. * Returns a set with the features for the participant.
  218. * @param {int} timeout the timeout in ms for reply from the participant.
  219. * @returns {Promise<Set<String>, Error>}
  220. */
  221. queryFeatures(timeout = 5000) {
  222. if (this._getFeaturesPromise) {
  223. return this._getFeaturesPromise;
  224. }
  225. this._getFeaturesPromise = this._conference.xmpp.caps.getFeatures(this._jid, timeout)
  226. .catch(error => {
  227. // Retry on feature version mismatch
  228. if (error === ERROR_FEATURE_VERSION_MISMATCH) {
  229. return this._conference.xmpp.caps.getFeatures(this._jid, timeout);
  230. }
  231. logger.warn(`Failed to discover features of ${this._jid}`, error);
  232. return Promise.reject(error);
  233. });
  234. return this._getFeaturesPromise
  235. .then(result => {
  236. this._getFeaturesPromise = undefined;
  237. return result;
  238. }, error => {
  239. this._getFeaturesPromise = undefined;
  240. throw error;
  241. });
  242. }
  243. /**
  244. * Returns the bot type for the participant.
  245. *
  246. * @returns {string|undefined} - The bot type of the participant.
  247. */
  248. getBotType() {
  249. return this._botType;
  250. }
  251. }