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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 RTCEvents = require("./service/RTC/RTCEvents");
  5. var EventEmitter = require("events");
  6. var JitsiConferenceEvents = require("./JitsiConferenceEvents");
  7. var JitsiParticipant = require("./JitsiParticipant");
  8. var Statistics = require("./modules/statistics/statistics");
  9. var JitsiTrackEvents = require("./JitsiTrackEvents");
  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(!RTC.options.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, handler);
  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
  89. * (send by sendPresenceCommand method).
  90. * @param command {String} the name of the command
  91. * @param handler {Function} handler for the command
  92. */
  93. JitsiConference.prototype.addCommandListener = function (command, handler) {
  94. if(this.room)
  95. this.room.addPresenceListener(command, handler);
  96. }
  97. /**
  98. * Removes command listener
  99. * @param command {String} the name of the command
  100. */
  101. JitsiConference.prototype.removeCommandListener = function (command) {
  102. if(this.room)
  103. this.room.removePresenceListener(command);
  104. }
  105. /**
  106. * Sends text message to the other participants in the conference
  107. * @param message the text message.
  108. */
  109. JitsiConference.prototype.sendTextMessage = function (message) {
  110. if(this.room)
  111. this.room.sendMessage(message);
  112. }
  113. /**
  114. * Send presence command.
  115. * @param name the name of the command.
  116. * @param values Object with keys and values that will be send.
  117. **/
  118. JitsiConference.prototype.sendCommand = function (name, values) {
  119. if(this.room) {
  120. this.room.addToPresence(name, values);
  121. this.room.sendPresence();
  122. }
  123. }
  124. /**
  125. * Send presence command one time.
  126. * @param name the name of the command.
  127. * @param values Object with keys and values that will be send.
  128. **/
  129. JitsiConference.prototype.sendCommandOnce = function (name, values) {
  130. this.sendCommand(name, values);
  131. this.removeCommand(name);
  132. }
  133. /**
  134. * Send presence command.
  135. * @param name the name of the command.
  136. * @param values Object with keys and values that will be send.
  137. * @param persistent if false the command will be sent only one time
  138. **/
  139. JitsiConference.prototype.removeCommand = function (name) {
  140. if(this.room)
  141. this.room.removeFromPresence(name);
  142. }
  143. /**
  144. * Sets the display name for this conference.
  145. * @param name the display name to set
  146. */
  147. JitsiConference.prototype.setDisplayName = function(name) {
  148. if(this.room){
  149. this.room.addToPresence("nick", {attributes: {xmlns: 'http://jabber.org/protocol/nick'}, value: name});
  150. this.room.sendPresence();
  151. }
  152. }
  153. /**
  154. * Adds JitsiLocalTrack object to the conference.
  155. * @param track the JitsiLocalTrack object.
  156. */
  157. JitsiConference.prototype.addTrack = function (track) {
  158. this.room.addStream(track.getOriginalStream(), function () {
  159. this.rtc.addLocalStream(track);
  160. var muteHandler = this._fireMuteChangeEvent.bind(this, track);
  161. var stopHandler = this.removeTrack.bind(this, track);
  162. var audioLevelHandler = this._fireAudioLevelChangeEvent.bind(this);
  163. track.addEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED, muteHandler);
  164. track.addEventListener(JitsiTrackEvents.TRACK_STOPPED, stopHandler);
  165. track.addEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  166. audioLevelHandler);
  167. this.addEventListener(JitsiConferenceEvents.TRACK_REMOVED, function (track) {
  168. track.removeEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED,
  169. muteHandler);
  170. track.removeEventListener(JitsiTrackEvents.TRACK_STOPPED,
  171. stopHandler);
  172. track.removeEventListener(
  173. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, audioLevelHandler);
  174. })
  175. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  176. }.bind(this));
  177. }
  178. /**
  179. * Fires TRACK_AUDIO_LEVEL_CHANGED change conference event.
  180. * @param audioLevel the audio level
  181. */
  182. JitsiConference.prototype._fireAudioLevelChangeEvent = function (audioLevel) {
  183. this.eventEmitter.emit(
  184. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  185. this.myUserId(), audioLevel);
  186. }
  187. /**
  188. * Fires TRACK_MUTE_CHANGED change conference event.
  189. * @param track the JitsiTrack object related to the event.
  190. */
  191. JitsiConference.prototype._fireMuteChangeEvent = function (track) {
  192. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  193. };
  194. /**
  195. * Removes JitsiLocalTrack object to the conference.
  196. * @param track the JitsiLocalTrack object.
  197. */
  198. JitsiConference.prototype.removeTrack = function (track) {
  199. this.room.removeStream(track.getOriginalStream(), function(){
  200. this.rtc.removeLocalStream(track);
  201. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  202. }.bind(this));
  203. }
  204. /**
  205. * Elects the participant with the given id to be the selected participant or the speaker.
  206. * @param id the identifier of the participant
  207. */
  208. JitsiConference.prototype.selectParticipant = function(participantId) {
  209. if (this.rtc) {
  210. this.rtc.selectedEndpoint(participantId);
  211. }
  212. }
  213. /**
  214. *
  215. * @param id the identifier of the participant
  216. */
  217. JitsiConference.prototype.pinParticipant = function(participantId) {
  218. if(this.rtc)
  219. this.rtc.pinEndpoint(participantId);
  220. }
  221. /**
  222. * Returns the list of participants for this conference.
  223. * @return Object a list of participant identifiers containing all conference participants.
  224. */
  225. JitsiConference.prototype.getParticipants = function() {
  226. return this.participants;
  227. }
  228. /**
  229. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  230. * null if there isn't one).
  231. * @param id the id of the participant.
  232. */
  233. JitsiConference.prototype.getParticipantById = function(id) {
  234. if(this.participants)
  235. return this.participants[id];
  236. return null;
  237. }
  238. JitsiConference.prototype.onMemberJoined = function (jid, email, nick) {
  239. if(this.eventEmitter)
  240. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, Strophe.getResourceFromJid(jid));
  241. // this.participants[jid] = new JitsiParticipant();
  242. }
  243. /**
  244. * Returns the local user's ID
  245. * @return {string} local user's ID
  246. */
  247. JitsiConference.prototype.myUserId = function () {
  248. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  249. }
  250. /**
  251. * Setups the listeners needed for the conference.
  252. * @param conference the conference
  253. */
  254. function setupListeners(conference) {
  255. conference.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  256. conference.rtc.onIncommingCall(event);
  257. if(conference.statistics)
  258. conference.statistics.startRemoteStats(event.peerconnection);
  259. });
  260. conference.room.addListener(XMPPEvents.REMOTE_STREAM_RECEIVED,
  261. function (data, sid, thessrc) {
  262. var track = conference.rtc.createRemoteStream(data, sid, thessrc);
  263. if(!track)
  264. return;
  265. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED,
  266. track);
  267. track.addEventListener(JitsiTrackEvents.TRACK_STOPPED,
  268. function () {
  269. conference.eventEmitter.emit(
  270. JitsiConferenceEvents.TRACK_REMOVED, track);
  271. });
  272. track.addEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED,
  273. function () {
  274. conference.eventEmitter.emit(
  275. JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  276. });
  277. var userId = track.getParitcipantId();
  278. track.addEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  279. function (audioLevel) {
  280. conference.eventEmitter.emit(
  281. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  282. userId, audioLevel);
  283. });
  284. }
  285. );
  286. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  287. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  288. });
  289. // FIXME
  290. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  291. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  292. // });
  293. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED,
  294. conference.onMemberJoined.bind(conference));
  295. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT,function (jid) {
  296. conference.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT,
  297. Strophe.getResourceFromJid(jid));
  298. });
  299. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED,
  300. function (from, displayName) {
  301. conference.eventEmitter.emit(
  302. JitsiConferenceEvents.DISPLAY_NAME_CHANGED,
  303. Strophe.getResourceFromJid(from), displayName);
  304. });
  305. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED,
  306. function () {
  307. conference.eventEmitter.emit(
  308. JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  309. });
  310. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  311. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  312. });
  313. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function() {
  314. conference.eventEmitter.emit(JitsiConferenceEvents.SETUP_FAILED);
  315. });
  316. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  317. if(conference.lastActiveSpeaker !== id && conference.room) {
  318. conference.lastActiveSpeaker = id;
  319. conference.eventEmitter.emit(JitsiConferenceEvents.ACTIVE_SPEAKER_CHANGED, id);
  320. }
  321. });
  322. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  323. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  324. });
  325. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  326. function (lastNEndpoints, endpointsEnteringLastN) {
  327. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  328. lastNEndpoints, endpointsEnteringLastN);
  329. });
  330. if(conference.statistics) {
  331. //FIXME: Maybe remove event should not be associated with the conference.
  332. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  333. var userId = null;
  334. var jid = conference.room.getJidBySSRC(ssrc);
  335. if (!jid)
  336. return;
  337. conference.rtc.setAudioLevel(jid, level);
  338. });
  339. conference.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  340. function () {
  341. conference.statistics.dispose();
  342. });
  343. // FIXME: Maybe we should move this.
  344. // RTC.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  345. // conference.room.updateDeviceAvailability(devices);
  346. // });
  347. }
  348. }
  349. module.exports = JitsiConference;