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.

JitsiConference.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. var logger = require("jitsi-meet-logger").getLogger(__filename);
  2. var RTC = require("./modules/RTC/RTC");
  3. var XMPPEvents = require("./service/xmpp/XMPPEvents");
  4. var StreamEventTypes = require("./service/RTC/StreamEventTypes");
  5. var RTCEvents = require("./service/RTC/RTCEvents");
  6. var EventEmitter = require("events");
  7. var JitsiConferenceEvents = require("./JitsiConferenceEvents");
  8. var JitsiParticipant = require("./JitsiParticipant");
  9. var Statistics = require("./modules/statistics/statistics");
  10. /**
  11. * Creates a JitsiConference object with the given name and properties.
  12. * Note: this constructor is not a part of the public API (objects should be
  13. * created using JitsiConnection.createConference).
  14. * @param options.config properties / settings related to the conference that will be created.
  15. * @param options.name the name of the conference
  16. * @param options.connection the JitsiConnection object for this JitsiConference.
  17. * @constructor
  18. */
  19. function JitsiConference(options) {
  20. if(!options.name || options.name.toLowerCase() !== options.name) {
  21. logger.error("Invalid conference name (no conference name passed or it"
  22. + "contains invalid characters like capital letters)!");
  23. return;
  24. }
  25. this.options = options;
  26. this.connection = this.options.connection;
  27. this.xmpp = this.connection.xmpp;
  28. this.eventEmitter = new EventEmitter();
  29. this.room = this.xmpp.createRoom(this.options.name, null, null, this.options.config);
  30. this.room.updateDeviceAvailability(RTC.getDeviceAvailability());
  31. this.rtc = new RTC(this.room, options);
  32. if(!options.config.disableAudioLevels)
  33. this.statistics = new Statistics();
  34. setupListeners(this);
  35. this.participants = {};
  36. this.lastActiveSpeaker = null;
  37. }
  38. /**
  39. * Joins the conference.
  40. * @param password {string} the password
  41. */
  42. JitsiConference.prototype.join = function (password) {
  43. if(this.room)
  44. this.room.join(password, this.connection.tokenPassword);
  45. }
  46. /**
  47. * Leaves the conference.
  48. */
  49. JitsiConference.prototype.leave = function () {
  50. if(this.xmpp)
  51. this.xmpp.leaveRoom(this.room.roomjid);
  52. this.room = null;
  53. }
  54. /**
  55. * Returns the local tracks.
  56. */
  57. JitsiConference.prototype.getLocalTracks = function () {
  58. if(this.rtc)
  59. return this.rtc.localStreams;
  60. };
  61. /**
  62. * Attaches a handler for events(For example - "participant joined".) in the conference. All possible event are defined
  63. * in JitsiConferenceEvents.
  64. * @param eventId the event ID.
  65. * @param handler handler for the event.
  66. *
  67. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  68. */
  69. JitsiConference.prototype.on = function (eventId, handler) {
  70. if(this.eventEmitter)
  71. this.eventEmitter.on(eventId, handler);
  72. }
  73. /**
  74. * Removes event listener
  75. * @param eventId the event ID.
  76. * @param [handler] optional, the specific handler to unbind
  77. *
  78. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  79. */
  80. JitsiConference.prototype.off = function (eventId, handler) {
  81. if(this.eventEmitter)
  82. this.eventEmitter.removeListener(eventId, listener);
  83. }
  84. // Common aliases for event emitter
  85. JitsiConference.prototype.addEventListener = JitsiConference.prototype.on
  86. JitsiConference.prototype.removeEventListener = JitsiConference.prototype.off
  87. /**
  88. * Receives notifications from another participants for commands / custom events(send by sendPresenceCommand method).
  89. * @param command {String} the name of the command
  90. * @param handler {Function} handler for the command
  91. */
  92. JitsiConference.prototype.addCommandListener = function (command, handler) {
  93. if(this.room)
  94. this.room.addPresenceListener(command, handler);
  95. }
  96. /**
  97. * Removes command listener
  98. * @param command {String} the name of the command
  99. */
  100. JitsiConference.prototype.removeCommandListener = function (command) {
  101. if(this.room)
  102. this.room.removePresenceListener(command);
  103. }
  104. /**
  105. * Sends text message to the other participants in the conference
  106. * @param message the text message.
  107. */
  108. JitsiConference.prototype.sendTextMessage = function (message) {
  109. if(this.room)
  110. this.room.sendMessage(message);
  111. }
  112. /**
  113. * Send presence command.
  114. * @param name the name of the command.
  115. * @param values Object with keys and values that will be send.
  116. **/
  117. JitsiConference.prototype.sendCommand = function (name, values) {
  118. if(this.room) {
  119. this.room.addToPresence(name, values);
  120. this.room.sendPresence();
  121. }
  122. }
  123. /**
  124. * Send presence command one time.
  125. * @param name the name of the command.
  126. * @param values Object with keys and values that will be send.
  127. **/
  128. JitsiConference.prototype.sendCommandOnce = function (name, values) {
  129. this.sendCommand(name, values);
  130. this.removeCommand(name);
  131. }
  132. /**
  133. * Send presence command.
  134. * @param name the name of the command.
  135. * @param values Object with keys and values that will be send.
  136. * @param persistent if false the command will be sent only one time
  137. **/
  138. JitsiConference.prototype.removeCommand = function (name) {
  139. if(this.room)
  140. this.room.removeFromPresence(name);
  141. }
  142. /**
  143. * Sets the display name for this conference.
  144. * @param name the display name to set
  145. */
  146. JitsiConference.prototype.setDisplayName = function(name) {
  147. if(this.room){
  148. this.room.addToPresence("nick", {attributes: {xmlns: 'http://jabber.org/protocol/nick'}, value: name});
  149. this.room.sendPresence();
  150. }
  151. }
  152. /**
  153. * Adds JitsiLocalTrack object to the conference.
  154. * @param track the JitsiLocalTrack object.
  155. */
  156. JitsiConference.prototype.addTrack = function (track) {
  157. this.rtc.addLocalStream(track);
  158. this.room.addStream(track.getOriginalStream, function () {});
  159. }
  160. /**
  161. * Removes JitsiLocalTrack object to the conference.
  162. * @param track the JitsiLocalTrack object.
  163. */
  164. JitsiConference.prototype.removeTrack = function (track) {
  165. this.room.removeStream(track.getOriginalStream());
  166. this.rtc.removeLocalStream(track);
  167. }
  168. /**
  169. * Elects the participant with the given id to be the selected participant or the speaker.
  170. * @param id the identifier of the participant
  171. */
  172. JitsiConference.prototype.selectParticipant = function(participantId) {
  173. if (this.rtc) {
  174. this.rtc.selectedEndpoint(participantId);
  175. }
  176. }
  177. /**
  178. *
  179. * @param id the identifier of the participant
  180. */
  181. JitsiConference.prototype.pinParticipant = function(participantId) {
  182. if(this.rtc)
  183. this.rtc.pinEndpoint(participantId);
  184. }
  185. /**
  186. * Returns the list of participants for this conference.
  187. * @return Object a list of participant identifiers containing all conference participants.
  188. */
  189. JitsiConference.prototype.getParticipants = function() {
  190. return this.participants;
  191. }
  192. /**
  193. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  194. * null if there isn't one).
  195. * @param id the id of the participant.
  196. */
  197. JitsiConference.prototype.getParticipantById = function(id) {
  198. if(this.participants)
  199. return this.participants[id];
  200. return null;
  201. }
  202. JitsiConference.prototype.onMemberJoined = function (jid, email, nick) {
  203. if(this.eventEmitter)
  204. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, Strophe.getResourceFromJid(jid));
  205. // this.participants[jid] = new JitsiParticipant();
  206. }
  207. /**
  208. * Returns the local user's ID
  209. * @return {string} local user's ID
  210. */
  211. JitsiConference.prototype.myUserId = function () {
  212. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  213. }
  214. /**
  215. * Setups the listeners needed for the conference.
  216. * @param conference the conference
  217. */
  218. function setupListeners(conference) {
  219. conference.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  220. conference.rtc.onIncommingCall(event);
  221. if(conference.statistics)
  222. conference.statistics.startRemoteStats(event.peerconnection);
  223. });
  224. conference.room.addListener(XMPPEvents.REMOTE_STREAM_RECEIVED,
  225. conference.rtc.createRemoteStream.bind(conference.rtc));
  226. conference.rtc.addListener(StreamEventTypes.EVENT_TYPE_REMOTE_CREATED, function (stream) {
  227. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, stream);
  228. });
  229. conference.rtc.addListener(StreamEventTypes.EVENT_TYPE_REMOTE_ENDED, function (stream) {
  230. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, stream);
  231. });
  232. conference.rtc.addListener(StreamEventTypes.EVENT_TYPE_LOCAL_ENDED, function (stream) {
  233. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, stream);
  234. });
  235. conference.rtc.addListener(StreamEventTypes.TRACK_MUTE_CHANGED, function (track) {
  236. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  237. });
  238. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  239. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  240. });
  241. // FIXME
  242. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  243. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  244. // });
  245. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  246. if(conference.lastActiveSpeaker !== id && conference.room
  247. && conference.myUserId() !== id) {
  248. conference.lastActiveSpeaker = id;
  249. conference.eventEmitter.emit(JitsiConferenceEvents.ACTIVE_SPEAKER_CHANGED, id);
  250. }
  251. });
  252. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  253. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  254. });
  255. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  256. function (lastNEndpoints, endpointsEnteringLastN) {
  257. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  258. lastNEndpoints, endpointsEnteringLastN);
  259. });
  260. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  261. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT,function (jid) {
  262. conference.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, Strophe.getResourceFromJid(jid));
  263. });
  264. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, function (from, displayName) {
  265. conference.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED,
  266. Strophe.getResourceFromJid(from), displayName);
  267. });
  268. if(conference.statistics) {
  269. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  270. var userId = null;
  271. if (ssrc === Statistics.LOCAL_JID) {
  272. userId = conference.myUserId();
  273. } else {
  274. var jid = conference.room.getJidBySSRC(ssrc);
  275. if (!jid)
  276. return;
  277. userId = Strophe.getResourceFromJid(jid);
  278. }
  279. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  280. userId, level);
  281. });
  282. conference.rtc.addListener(StreamEventTypes.EVENT_TYPE_LOCAL_CREATED, function (stream) {
  283. conference.statistics.startLocalStats(stream);
  284. });
  285. conference.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  286. function () {
  287. conference.statistics.dispose();
  288. });
  289. RTC.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  290. conference.room.updateDeviceAvailability(devices);
  291. });
  292. RTC.addListener(StreamEventTypes.TRACK_MUTE_CHANGED, function (track) {
  293. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  294. });
  295. }
  296. }
  297. module.exports = JitsiConference;