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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /* global Strophe, $ */
  2. /* jshint -W101 */
  3. var logger = require("jitsi-meet-logger").getLogger(__filename);
  4. var RTC = require("./modules/RTC/RTC");
  5. var XMPPEvents = require("./service/xmpp/XMPPEvents");
  6. var RTCEvents = require("./service/RTC/RTCEvents");
  7. var EventEmitter = require("events");
  8. var JitsiConferenceEvents = require("./JitsiConferenceEvents");
  9. var JitsiConferenceErrors = require("./JitsiConferenceErrors");
  10. var JitsiParticipant = require("./JitsiParticipant");
  11. var Statistics = require("./modules/statistics/statistics");
  12. var JitsiDTMFManager = require('./modules/DTMF/JitsiDTMFManager');
  13. var JitsiTrackEvents = require("./JitsiTrackEvents");
  14. /**
  15. * Creates a JitsiConference object with the given name and properties.
  16. * Note: this constructor is not a part of the public API (objects should be
  17. * created using JitsiConnection.createConference).
  18. * @param options.config properties / settings related to the conference that will be created.
  19. * @param options.name the name of the conference
  20. * @param options.connection the JitsiConnection object for this JitsiConference.
  21. * @constructor
  22. */
  23. function JitsiConference(options) {
  24. if(!options.name || options.name.toLowerCase() !== options.name) {
  25. logger.error("Invalid conference name (no conference name passed or it"
  26. + "contains invalid characters like capital letters)!");
  27. return;
  28. }
  29. this.options = options;
  30. this.connection = this.options.connection;
  31. this.xmpp = this.connection.xmpp;
  32. this.eventEmitter = new EventEmitter();
  33. this.room = this.xmpp.createRoom(this.options.name, null, null, this.options.config);
  34. this.room.updateDeviceAvailability(RTC.getDeviceAvailability());
  35. this.rtc = new RTC(this.room, options);
  36. if(!RTC.options.disableAudioLevels)
  37. this.statistics = new Statistics();
  38. setupListeners(this);
  39. this.participants = {};
  40. this.lastActiveSpeaker = null;
  41. this.dtmfManager = null;
  42. this.somebodySupportsDTMF = false;
  43. }
  44. /**
  45. * Joins the conference.
  46. * @param password {string} the password
  47. */
  48. JitsiConference.prototype.join = function (password) {
  49. if(this.room)
  50. this.room.join(password, this.connection.tokenPassword);
  51. };
  52. /**
  53. * Leaves the conference.
  54. */
  55. JitsiConference.prototype.leave = function () {
  56. if(this.xmpp && this.room)
  57. this.xmpp.leaveRoom(this.room.roomjid);
  58. this.room = null;
  59. };
  60. /**
  61. * Returns the local tracks.
  62. */
  63. JitsiConference.prototype.getLocalTracks = function () {
  64. if (this.rtc) {
  65. return this.rtc.localStreams;
  66. } else {
  67. return [];
  68. }
  69. };
  70. /**
  71. * Attaches a handler for events(For example - "participant joined".) in the conference. All possible event are defined
  72. * in JitsiConferenceEvents.
  73. * @param eventId the event ID.
  74. * @param handler handler for the event.
  75. *
  76. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  77. */
  78. JitsiConference.prototype.on = function (eventId, handler) {
  79. if(this.eventEmitter)
  80. this.eventEmitter.on(eventId, handler);
  81. };
  82. /**
  83. * Removes event listener
  84. * @param eventId the event ID.
  85. * @param [handler] optional, the specific handler to unbind
  86. *
  87. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  88. */
  89. JitsiConference.prototype.off = function (eventId, handler) {
  90. if(this.eventEmitter)
  91. this.eventEmitter.removeListener(eventId, handler);
  92. };
  93. // Common aliases for event emitter
  94. JitsiConference.prototype.addEventListener = JitsiConference.prototype.on;
  95. JitsiConference.prototype.removeEventListener = JitsiConference.prototype.off;
  96. /**
  97. * Receives notifications from another participants for commands / custom events
  98. * (send by sendPresenceCommand method).
  99. * @param command {String} the name of the command
  100. * @param handler {Function} handler for the command
  101. */
  102. JitsiConference.prototype.addCommandListener = function (command, handler) {
  103. if(this.room)
  104. this.room.addPresenceListener(command, handler);
  105. };
  106. /**
  107. * Removes command listener
  108. * @param command {String} the name of the command
  109. */
  110. JitsiConference.prototype.removeCommandListener = function (command) {
  111. if(this.room)
  112. this.room.removePresenceListener(command);
  113. };
  114. /**
  115. * Sends text message to the other participants in the conference
  116. * @param message the text message.
  117. */
  118. JitsiConference.prototype.sendTextMessage = function (message) {
  119. if(this.room)
  120. this.room.sendMessage(message);
  121. };
  122. /**
  123. * Send presence command.
  124. * @param name the name of the command.
  125. * @param values Object with keys and values that will be send.
  126. **/
  127. JitsiConference.prototype.sendCommand = function (name, values) {
  128. if(this.room) {
  129. this.room.addToPresence(name, values);
  130. this.room.sendPresence();
  131. }
  132. };
  133. /**
  134. * Send presence command one time.
  135. * @param name the name of the command.
  136. * @param values Object with keys and values that will be send.
  137. **/
  138. JitsiConference.prototype.sendCommandOnce = function (name, values) {
  139. this.sendCommand(name, values);
  140. this.removeCommand(name);
  141. };
  142. /**
  143. * Send presence command.
  144. * @param name the name of the command.
  145. * @param values Object with keys and values that will be send.
  146. * @param persistent if false the command will be sent only one time
  147. **/
  148. JitsiConference.prototype.removeCommand = function (name) {
  149. if(this.room)
  150. this.room.removeFromPresence(name);
  151. };
  152. /**
  153. * Sets the display name for this conference.
  154. * @param name the display name to set
  155. */
  156. JitsiConference.prototype.setDisplayName = function(name) {
  157. if(this.room){
  158. this.room.addToPresence("nick", {attributes: {xmlns: 'http://jabber.org/protocol/nick'}, value: name});
  159. this.room.sendPresence();
  160. }
  161. };
  162. /**
  163. * Adds JitsiLocalTrack object to the conference.
  164. * @param track the JitsiLocalTrack object.
  165. */
  166. JitsiConference.prototype.addTrack = function (track) {
  167. this.room.addStream(track.getOriginalStream(), function () {
  168. this.rtc.addLocalStream(track);
  169. var muteHandler = this._fireMuteChangeEvent.bind(this, track);
  170. var stopHandler = this.removeTrack.bind(this, track);
  171. var audioLevelHandler = this._fireAudioLevelChangeEvent.bind(this);
  172. track.addEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED, muteHandler);
  173. track.addEventListener(JitsiTrackEvents.TRACK_STOPPED, stopHandler);
  174. track.addEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, audioLevelHandler);
  175. this.addEventListener(JitsiConferenceEvents.TRACK_REMOVED, function (someTrack) {
  176. if (someTrack !== track) {
  177. return;
  178. }
  179. track.removeEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED, muteHandler);
  180. track.removeEventListener(JitsiTrackEvents.TRACK_STOPPED, stopHandler);
  181. track.removeEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, audioLevelHandler);
  182. });
  183. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  184. }.bind(this));
  185. };
  186. /**
  187. * Fires TRACK_AUDIO_LEVEL_CHANGED change conference event.
  188. * @param audioLevel the audio level
  189. */
  190. JitsiConference.prototype._fireAudioLevelChangeEvent = function (audioLevel) {
  191. this.eventEmitter.emit(
  192. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  193. this.myUserId(), audioLevel);
  194. };
  195. /**
  196. * Fires TRACK_MUTE_CHANGED change conference event.
  197. * @param track the JitsiTrack object related to the event.
  198. */
  199. JitsiConference.prototype._fireMuteChangeEvent = function (track) {
  200. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  201. };
  202. /**
  203. * Removes JitsiLocalTrack object to the conference.
  204. * @param track the JitsiLocalTrack object.
  205. */
  206. JitsiConference.prototype.removeTrack = function (track) {
  207. this.room.removeStream(track.getOriginalStream(), function(){
  208. this.rtc.removeLocalStream(track);
  209. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  210. }.bind(this));
  211. };
  212. /**
  213. * Get role of the local user.
  214. * @returns {string} user role: 'moderator' or 'none'
  215. */
  216. JitsiConference.prototype.getRole = function () {
  217. return this.room.role;
  218. };
  219. /**
  220. * Check if local user is moderator.
  221. * @returns {boolean} true if local user is moderator, false otherwise.
  222. */
  223. JitsiConference.prototype.isModerator = function () {
  224. return this.room.isModerator();
  225. };
  226. /**
  227. * Set password for the room.
  228. * @param {string} password new password for the room.
  229. * @returns {Promise}
  230. */
  231. JitsiConference.prototype.lock = function (password) {
  232. if (!this.isModerator()) {
  233. return Promise.reject();
  234. }
  235. var conference = this;
  236. return new Promise(function (resolve, reject) {
  237. conference.xmpp.lockRoom(password, function () {
  238. resolve();
  239. }, function (err) {
  240. reject(err);
  241. }, function () {
  242. reject(JitsiConferenceErrors.PASSWORD_REQUIRED);
  243. });
  244. });
  245. };
  246. /**
  247. * Remove password from the room.
  248. * @returns {Promise}
  249. */
  250. JitsiConference.prototype.unlock = function () {
  251. return this.lock(undefined);
  252. };
  253. /**
  254. * Elects the participant with the given id to be the selected participant or the speaker.
  255. * @param id the identifier of the participant
  256. */
  257. JitsiConference.prototype.selectParticipant = function(participantId) {
  258. if (this.rtc) {
  259. this.rtc.selectedEndpoint(participantId);
  260. }
  261. };
  262. /**
  263. *
  264. * @param id the identifier of the participant
  265. */
  266. JitsiConference.prototype.pinParticipant = function(participantId) {
  267. if (this.rtc) {
  268. this.rtc.pinEndpoint(participantId);
  269. }
  270. };
  271. /**
  272. * Returns the list of participants for this conference.
  273. * @return Array<JitsiParticipant> a list of participant identifiers containing all conference participants.
  274. */
  275. JitsiConference.prototype.getParticipants = function() {
  276. return Object.keys(this.participants).map(function (key) {
  277. return this.participants[key];
  278. }, this);
  279. };
  280. /**
  281. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  282. * undefined if there isn't one).
  283. * @param id the id of the participant.
  284. */
  285. JitsiConference.prototype.getParticipantById = function(id) {
  286. return this.participants[id];
  287. };
  288. JitsiConference.prototype.onMemberJoined = function (jid, email, nick) {
  289. var id = Strophe.getResourceFromJid(jid);
  290. if (id === 'focus') {
  291. return;
  292. }
  293. var participant = new JitsiParticipant(id, this, nick);
  294. this.participants[id] = participant;
  295. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, id, participant);
  296. this.xmpp.connection.disco.info(
  297. jid, "node", function(iq) {
  298. participant._supportsDTMF = $(iq).find(
  299. '>query>feature[var="urn:xmpp:jingle:dtmf:0"]').length > 0;
  300. this.updateDTMFSupport();
  301. }.bind(this)
  302. );
  303. };
  304. JitsiConference.prototype.onMemberLeft = function (jid) {
  305. var id = Strophe.getResourceFromJid(jid);
  306. var participant = this.participants[id];
  307. delete this.participants[id];
  308. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id, participant);
  309. };
  310. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  311. var id = Strophe.getResourceFromJid(jid);
  312. var participant = this.getParticipantById(id);
  313. if (!participant) {
  314. return;
  315. }
  316. participant._role = role;
  317. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  318. };
  319. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  320. var id = Strophe.getResourceFromJid(jid);
  321. var participant = this.getParticipantById(id);
  322. if (!participant) {
  323. return;
  324. }
  325. participant._displayName = displayName;
  326. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  327. };
  328. JitsiConference.prototype.onTrackAdded = function (track) {
  329. var id = track.getParticipantId();
  330. var participant = this.getParticipantById(id);
  331. if (!participant) {
  332. return;
  333. }
  334. // add track to JitsiParticipant
  335. participant._tracks.push(track);
  336. var emitter = this.eventEmitter;
  337. track.addEventListener(
  338. JitsiTrackEvents.TRACK_STOPPED,
  339. function () {
  340. // remove track from JitsiParticipant
  341. var pos = participant._tracks.indexOf(track);
  342. if (pos > -1) {
  343. participant._tracks.splice(pos, 1);
  344. }
  345. emitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  346. }
  347. );
  348. track.addEventListener(
  349. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  350. function () {
  351. emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  352. }
  353. );
  354. track.addEventListener(
  355. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  356. function (audioLevel) {
  357. emitter.emit(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, id, audioLevel);
  358. }
  359. );
  360. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  361. };
  362. JitsiConference.prototype.updateDTMFSupport = function () {
  363. var somebodySupportsDTMF = false;
  364. var participants = this.getParticipants();
  365. // check if at least 1 participant supports DTMF
  366. for (var i = 0; i < participants.length; i += 1) {
  367. if (participants[i].supportsDTMF()) {
  368. somebodySupportsDTMF = true;
  369. break;
  370. }
  371. }
  372. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  373. this.somebodySupportsDTMF = somebodySupportsDTMF;
  374. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  375. }
  376. };
  377. /**
  378. * Allows to check if there is at least one user in the conference
  379. * that supports DTMF.
  380. * @returns {boolean} true if somebody supports DTMF, false otherwise
  381. */
  382. JitsiConference.prototype.isDTMFSupported = function () {
  383. return this.somebodySupportsDTMF;
  384. };
  385. /**
  386. * Returns the local user's ID
  387. * @return {string} local user's ID
  388. */
  389. JitsiConference.prototype.myUserId = function () {
  390. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  391. };
  392. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  393. if (!this.dtmfManager) {
  394. var connection = this.xmpp.connection.jingle.activecall.peerconnection;
  395. if (!connection) {
  396. logger.warn("cannot sendTones: no conneciton");
  397. return;
  398. }
  399. var tracks = this.getLocalTracks().filter(function (track) {
  400. return track.isAudioTrack();
  401. });
  402. if (!tracks.length) {
  403. logger.warn("cannot sendTones: no local audio stream");
  404. return;
  405. }
  406. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  407. }
  408. this.dtmfManager.sendTones(tones, duration, pause);
  409. };
  410. /**
  411. * Setups the listeners needed for the conference.
  412. * @param conference the conference
  413. */
  414. function setupListeners(conference) {
  415. conference.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  416. conference.rtc.onIncommingCall(event);
  417. if(conference.statistics)
  418. conference.statistics.startRemoteStats(event.peerconnection);
  419. });
  420. conference.room.addListener(XMPPEvents.REMOTE_STREAM_RECEIVED,
  421. function (data, sid, thessrc) {
  422. var track = conference.rtc.createRemoteStream(data, sid, thessrc);
  423. if (track) {
  424. conference.onTrackAdded(track);
  425. }
  426. }
  427. );
  428. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  429. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  430. });
  431. // FIXME
  432. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  433. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  434. // });
  435. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  436. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  437. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  438. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  439. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  440. });
  441. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  442. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  443. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  444. });
  445. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  446. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  447. });
  448. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function () {
  449. conference.eventEmitter.emit(JitsiConferenceEvents.SETUP_FAILED);
  450. });
  451. conference.room.addListener(XMPPEvents.MESSAGE_RECEIVED, function (jid, displayName, txt, myJid, ts) {
  452. var id = Strophe.getResourceFromJid(jid);
  453. conference.eventEmitter.emit(JitsiConferenceEvents.MESSAGE_RECEIVED, id, txt, ts);
  454. });
  455. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  456. if(conference.lastActiveSpeaker !== id && conference.room) {
  457. conference.lastActiveSpeaker = id;
  458. conference.eventEmitter.emit(JitsiConferenceEvents.ACTIVE_SPEAKER_CHANGED, id);
  459. }
  460. });
  461. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  462. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  463. });
  464. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  465. function (lastNEndpoints, endpointsEnteringLastN) {
  466. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  467. lastNEndpoints, endpointsEnteringLastN);
  468. });
  469. conference.xmpp.addListener(XMPPEvents.PASSWORD_REQUIRED, function () {
  470. conference.eventEmitter.emit(JitsiConferenceErrors.PASSWORD_REQUIRED);
  471. });
  472. if(conference.statistics) {
  473. //FIXME: Maybe remove event should not be associated with the conference.
  474. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  475. var userId = null;
  476. var jid = conference.room.getJidBySSRC(ssrc);
  477. if (!jid)
  478. return;
  479. conference.rtc.setAudioLevel(jid, level);
  480. });
  481. conference.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  482. function () {
  483. conference.statistics.dispose();
  484. });
  485. // FIXME: Maybe we should move this.
  486. // RTC.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  487. // conference.room.updateDeviceAvailability(devices);
  488. // });
  489. }
  490. }
  491. module.exports = JitsiConference;