您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

JitsiParticipant.js 7.6KB

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